Skip to content

Instantly share code, notes, and snippets.

@Muqsit
Created April 11, 2024 13:27
Show Gist options
  • Save Muqsit/7619a60a6eaa44ecffee3db14858bdd0 to your computer and use it in GitHub Desktop.
Save Muqsit/7619a60a6eaa44ecffee3db14858bdd0 to your computer and use it in GitHub Desktop.
<?php
declare(strict_types=1);
/**
* @name KitEditorPlugin
* @main muqsit\kiteditorplugin\KitEditorPlugin
* @api 5.0.0
* @version 0.0.1
*/
namespace muqsit\kiteditorplugin{
use muqsit\invmenu\InvMenu;
use muqsit\invmenu\InvMenuHandler;
use pocketmine\command\Command;
use pocketmine\command\CommandSender;
use pocketmine\command\PluginCommand;
use pocketmine\event\inventory\InventoryTransactionEvent;
use pocketmine\event\Listener;
use pocketmine\inventory\PlayerInventory;
use pocketmine\inventory\transaction\action\SlotChangeAction;
use pocketmine\item\Item;
use pocketmine\item\VanillaItems;
use pocketmine\permission\DefaultPermissions;
use pocketmine\player\Player;
use pocketmine\plugin\PluginBase;
final class KitEditorPlugin extends PluginBase implements Listener{
private const TAG_KIT_EDITOR_ITEM = "KitEditor";
protected function onEnable() : void{
if(!InvMenuHandler::isRegistered()){
InvMenuHandler::register($this);
}
$command = new PluginCommand("kiteditor", $this, $this);
$command->setPermission(DefaultPermissions::ROOT_USER);
$this->getServer()->getCommandMap()->register($this->getName(), $command);
$this->getServer()->getPluginManager()->registerEvents($this, $this);
}
/**
* @param Player $player
* @param array<int, Item> $contents
*/
public function sendKitEditorMenu(Player $player, array $contents) : void{
// nbt-tag all kit editor items
foreach($contents as $content){
$content->getNamedTag()->setByte(self::TAG_KIT_EDITOR_ITEM, 1);
}
$menu = InvMenu::create(InvMenu::TYPE_CHEST);
$menu->getInventory()->setContents($contents);
$menu->send($player);
}
public function handleInventoryTransaction(InventoryTransactionEvent $event) : void{
foreach($event->getTransaction()->getActions() as $action){
if(!($action instanceof SlotChangeAction)){
continue;
}
if($action->getTargetItem()->getNamedTag()->getTag(self::TAG_KIT_EDITOR_ITEM) === null){
continue;
}
if($action->getInventory() instanceof PlayerInventory){
// disallow moving items to 'normal player inventory' if item has TAG_KIT_EDITOR_ITEM
$event->cancel();
break;
}
}
}
public function onCommand(CommandSender $sender, Command $command, string $label, array $args) : bool{
if(!($sender instanceof Player)){
return true;
}
$contents = [];
$contents[] = VanillaItems::DIAMOND_HELMET();
$contents[] = VanillaItems::DIAMOND_CHESTPLATE();
$contents[] = VanillaItems::DIAMOND_LEGGINGS();
$contents[] = VanillaItems::DIAMOND_BOOTS();
$this->sendKitEditorMenu($sender, $contents);
return true;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment