Skip to content

Commit

Permalink
Fix issue #3
Browse files Browse the repository at this point in the history
  • Loading branch information
taylordevs committed Jun 22, 2023
1 parent a52ee60 commit ef64e5e
Show file tree
Hide file tree
Showing 9 changed files with 517 additions and 3 deletions.
Empty file added .poggit
Empty file.
6 changes: 3 additions & 3 deletions plugin.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
---
name: ChestKits
version: 0.0.2
api: 5.0.0
src-namespace-prefix: DavidGlitch04\ChestKits
main: DavidGlitch04\ChestKits\ChestKits
website: https://github.com/taylordevs

permissions:
chestkits.command.allow:
default: op
description: "Allow player to use chestkits"
description: Allow player to use chestkits
...
116 changes: 116 additions & 0 deletions src/DavidGlitch04/ChestKits/ChestKits.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
<?php

namespace DavidGlitch04\ChestKits;

use pocketmine\item\{
Item
};
use pocketmine\plugin\{
Plugin,
PluginBase
};
use DavidGlitch04\ChestKits\{
Command\CKitsCommand,
Task\CheckUpdateTask
};
use pocketmine\{
lang\Language,
player\Player,
utils\Config,
utils\TextFormat
};
use pocketmine\block\VanillaBlocks;
use function strval;
/**
* Class ChestKits
* @package DavidGlitch04\ChestKits
*/
class ChestKits extends PluginBase{
/** @var Config $kits */
public Config $kits;
/** @var Plugin|null $piggyEnchants */
public ?Plugin $piggyEnchants;
/** @var Language $language */
public static Language $language;
/** @var array|string[] $languages */
private array $languages = [
"vie",
"eng"
];
public static function getLanguage(): Language{
return self::$language;
}

/**
* @return void
*/
protected function onEnable(): void
{
$server = $this->getServer();
$server->getPluginManager()->registerEvents(new EventListener($this), $this);
$server->getCommandMap()->register("chestkits", new CKitsCommand($this));
$this->saveResource("kits.yml");
$this->kits = new Config($this->getDataFolder()."kits.yml", Config::YAML);
$this->piggyEnchants = $this->getServer()->getPluginManager()->getPlugin("PiggyCustomEnchants");
$this->initLanguage(strval($this->getConfig()->get("language", "eng")), $this->languages);
$this->checkUpdate();
}

private function checkUpdate(): void{
$this->getServer()->getAsyncPool()->submitTask(new CheckUpdateTask($this->getDescription()->getName(), $this->getDescription()->getVersion()));
}

/**
* @param string $lang
* @param array $languageFiles
*/
public function initLanguage(string $lang, array $languageFiles): void {
$path = $this->getDataFolder() . "languages/";
if (!is_dir($path)) {
@mkdir($path);
}
foreach ($languageFiles as $file) {
if (!is_file($path . $file . ".ini")) {
$this->saveResource("languages/" . $file . ".ini");
}
}
self::$language = new Language($lang, $path);
}

public function getPrefix(): string{
return strval($this->getConfig()->get("prefix", "&c[&aChestkits&c] "));
}

/**
* @param Player $player
* @param string $name
* @param string $lore
*/
public function sendKit(Player $player, string $name, string $lore): void{
$kit = VanillaBlocks::CHEST()->asItem();
$kit->getNamedTag()
->setString("chestkits", $name);
$kit->setCustomName($name);
$kit->setLore(array($lore));
$player->getInventory()->addItem($kit);
}

/**
* @param Item $item
* @return bool
*/
public function isChestKit(Item $item): bool{
return $item->getNamedTag()->getTag("chestkits") !== null;
}

public function getMessage(string $msg, array $replace = null): string
{
$prefix = $this->getPrefix();
if($replace == null){
$msg = ChestKits::getLanguage()->translateString($msg);
} else{
$msg = ChestKits::getLanguage()->translateString($msg, $replace);
}
return TextFormat::colorize($prefix . $msg);
}
}
63 changes: 63 additions & 0 deletions src/DavidGlitch04/ChestKits/Command/CKitsCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

declare(strict_types=1);

namespace DavidGlitch04\ChestKits\Command;

use DavidGlitch04\ChestKits\{
ChestKits,
Form\CKitsForm
};
use pocketmine\command\{
Command,
CommandSender
};
use pocketmine\player\Player;
use pocketmine\plugin\{
Plugin,
PluginOwned
};

/**
* Class CKitsCommand
* @package DavidGlitch04\ChestKits\Command
*/
class CKitsCommand extends Command implements PluginOwned{
/** @var ChestKits $chestkits */
private ChestKits $chestkits;

/**
* CKitsCommand constructor.
* @param ChestKits $chestkits
*/
public function __construct(ChestKits $chestkits) {
$this->chestkits = $chestkits;
parent::__construct("chestkits");
$this->setDescription("Chestkits command");
$this->setPermission("chestkits.command.allow");
}

/**
* @return Plugin
*/
public function getOwningPlugin(): Plugin
{
return $this->chestkits;
}

/**
* @param CommandSender $sender
* @param string $commandLabel
* @param array $args
*/
public function execute(CommandSender $sender, string $commandLabel, array $args): void{
if($sender instanceof Player){
new CKitsForm(
$this->chestkits,
$sender
);
} else{
$this->chestkits->getLogger()->warning("Please use this command in game!");
}
}
}
79 changes: 79 additions & 0 deletions src/DavidGlitch04/ChestKits/Economy/EconomyManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

declare(strict_types=1);

namespace DavidGlitch04\ChestKits\Economy;

use Closure;
use cooldogedev\BedrockEconomy\libs\cooldogedev\libSQL\context\ClosureContext;
use onebone\economyapi\EconomyAPI;
use DavidGlitch04\ChestKits\ChestKits;
use pocketmine\player\Player;
use pocketmine\plugin\Plugin;

/**
* Class EconomyManager
* @package DavidGlitch04\ChestKits\Economy
*/
class EconomyManager{
/** @var Plugin|null $eco */
private ?Plugin $eco;
/** @var ChestKits $plugin */
private ChestKits $plugin;

/**
* EconomyManager constructor.
* @param ChestKits $plugin
*/
public function __construct(ChestKits $plugin){
$this->plugin = $plugin;
$manager = $plugin->getServer()->getPluginManager();
$this->eco = $manager->getPlugin("EconomyAPI") ?? $manager->getPlugin("BedrockEconomy") ?? null;
unset($manager);
}

/**
* @param Player $player
* @return int
*/
public function getMoney(Player $player, Closure $callback): void {
switch ($this->eco->getName()){
case "EconomyAPI":
$money = $this->eco->myMoney($player->getName());
assert(is_float($money));
$callback($money);
break;
case "BedrockEconomy":
$this->eco->getAPI()->getPlayerBalance($player->getName(), ClosureContext::create(static function(?int $balance) use($callback) : void{
$callback($balance ?? 0);
}));
break;
default:
$this->eco->getAPI()->getPlayerBalance($player->getName(), ClosureContext::create(static function(?int $balance) use($callback) : void{
$callback($balance ?? 0);
}));
}
}

/**
* @param Player $player
* @param int $amount
* @return bool
*/
public function reduceMoney(Player $player, int $amount, Closure $callback){
if($this->eco == null){
$this->plugin->getLogger()->warning("You not have Economy plugin");
return true;
}
switch ($this->eco->getName()){
case "EconomyAPI":
$callback($this->eco->reduceMoney($player->getName(), $amount) === EconomyAPI::RET_SUCCESS);
break;
case "BedrockEconomy":
$this->eco->getAPI()->subtractFromPlayerBalance($player->getName(), (int) ceil($amount), ClosureContext::create(static function(bool $success) use($callback) : void{
$callback($success);
}));
break;
}
}
}
90 changes: 90 additions & 0 deletions src/DavidGlitch04/ChestKits/EventListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php

namespace DavidGlitch04\ChestKits;

use pocketmine\event\{
Listener,
block\BlockPlaceEvent
};
use pocketmine\item\{Item,
enchantment\EnchantmentInstance,
enchantment\StringToEnchantmentParser,
StringToItemParser,
VanillaItems};
use DaPigGuy\PiggyCustomEnchants\enchants\CustomEnchantManager;

/**
* Class EventListener
* @package DavidGlitch04\ChestKits
*/
class EventListener implements Listener{
/** @var ChestKits $chestkits */
private ChestKits $chestkits;

/**
* EventListener constructor.
* @param ChestKits $chestkits
*/
public function __construct(ChestKits $chestkits)
{
$this->chestkits = $chestkits;
}

/**
* @param BlockPlaceEvent $event
*/
public function onTap(BlockPlaceEvent $event): void{
$item = $event->getItem();
$player = $event->getPlayer();
$world = $player->getWorld();
$position = $event->getBlockAgainst()->getPosition();
if($this->chestkits->isChestKit($item)){
$kitname = $item->getNamedTag()->getString("chestkits");
foreach ($this->chestkits->kits->getAll() as $kits){
if($kits["name"] == $kitname){
foreach($kits["items"] as $itemString){
$world->dropItem($position, $this->loadItem(...explode(":", $itemString)));
}
isset($kits["helmet"]) and $world->dropItem($position, $this->loadItem(...explode(":", $kits["helmet"])));
isset($kits["chestplate"]) and $world->dropItem($position, $this->loadItem(...explode(":", $kits["chestplate"])));
isset($kits["leggings"]) and $world->dropItem($position, $this->loadItem(...explode(":", $kits["leggings"])));
isset($kits["boots"]) and $world->dropItem($position, $this->loadItem(...explode(":", $kits["boots"])));
}
}
$item->setCount($item->getCount() - 1);
$player->getInventory()->setItemInHand($item);
$player->sendMessage($this->chestkits->getMessage("kit.open"));
$event->cancel();
}
}

/**
* @param string $itemName
* @param string $name
* @param mixed ...$enchantments
* @return Item
*/
public function loadItem(string $itemName, int $amount, string $name = "default", ...$enchantments): Item{
$item = StringToItemParser::getInstance()->parse($itemName);
if ($item === null) {
$item = VanillaItems::AIR();
}
$item->setCount($amount);
if(strtolower($name) !== "default"){
$item->setCustomName($name);
}
$enchantment = null;
foreach($enchantments as $key => $name_level){
if($key % 2 === 0){ //Name expected
$enchantment = StringToEnchantmentParser::getInstance()->parse((string)$name_level);
if($enchantment === null && class_exists(CustomEnchantManager::class)){
$enchantment = CustomEnchantManager::getEnchantmentByName((string)$name_level);
}
}elseif($enchantment !== null){
$item->addEnchantment(new EnchantmentInstance($enchantment, (int)$name_level));
}
}

return $item;
}
}
Loading

0 comments on commit ef64e5e

Please sign in to comment.