I am new to PHP and im trying to setup a ServerMenu for the main HUB server where players can select the server they want to connect to but idk how to make it so the user opens a UI when they interact with the item PHP: <?phpnamespace Tetro\NetworkMenu;use pocketmine\plugin\PluginBase;use pocketmine\player\Player;use pocketmine\command\Command;use pocketmine\command\Commandsender;use pocketmine\utils\TextFormat;use jojoe77777\FormAPI\CustomForm;class Main extends PluginBase { public function onEnable() { $this->getServer()->getPluginManager()->registerEvent($this, $this); } public function onCommand($CommandSender $sender, Command $cmd, string $label, array $args) { switch($cmd->getName()) { case "servers": $this->openServerMenu($player); break; case "info": $this->openInfoMenu($player); break; } } public function openServerMenu($player) { $api = $this->getServer()->getPluginManager()->getPlugin("FormAPI"); $form = $api->createSimpleForm(function (Player $sender, int $data, null) { $result = $data; if($result === null) { } }); $form->setTitle(TextFormat::PURPLE . "Server Selector"); $form->addButton(TextFormat::AQUA . "Factions"); $form->addButton(TextFormat::GREEN . "Prison"); $form->addButton(TextFormat::RED . "Exit"); $form->sendToPlayer($player); return $form; } public function openInfoMenu($player) { $api = $this->getServer()->getPluginManager()->getPlugin("FormAPI"); $form = $api->createSimpleForm(function (Player $sender, int $data, null) { $result = $data; if($result === null) { } }); $form->setTitle(TextFormat::PURPLE . "Psycho Network"); $form->setContent(TextFormat::GREEN . "Psycho is a network brought to you by Tetro \nand the Psycho staff team.\n\n" TextFormat::YELLOW . "Social Medias:\n" TextFormat::AQUA . "\nDiscord: \n\n" . TextFormat::PURPLE . "Store: psychope.tebex.io"); $form->addButton(TextFormat::RED . "Exit") $form->sendToPlayer($player); return $form; }}
PHP: public function onInteract(PlayerInteractEvent $event) { $item = Item::get(2, 0, 0); //2 is grass block if ($event->getPlayer()->getItemInHand()->getId() === $item) { $this->getServer()->dispatchcommand($event->getPlayer(), "command here"); } } set prefix to SPLVED
or if you can when player hit block to floor, use: PHP: public function onInteract(PlayerInteractEvent $event) { $item = Item::get(2, 0, 0); //2 is grass block if ($event->getPlayer()->getItemInHand()->getId() === $item) { $this->openServerMenu($player); } } solved.
PHP: public function onEnable(){ $this->getServer()->getPluginManager()->registerEvents($this, $this); //now you registered events} use this code in this function in your Enable function (i reakky can't explain better, give me your code if it has error)
You don’t have implemented Listener, maybe that can cause the problem too. Use: PHP: class Main extends PluginBase implements Listener
oh my god, i see the code full right now, there is a lot of wrongs in your code. wait im writing code for you.
so in first you need to use these statments (after your namespace) : PHP: use jojoe77777\FormAPI;use pocketmine\event\Listener; use pocketmine\event\player\PlayerInteractEvent;use pocketmine\item\Item;use pocketmine\Player;use pocketmine\plugin\PluginBase;use pocketmine\utils\TextFormat; now you should paste this class with all functions instead your class: PHP: class Main extends PluginBase implements Listener{ public function onEnable() { $this->getServer()->getPluginManager()->registerEvents($this, $this); } /** * @param $player * @return mixed */ public function openServerMenu($player) { $api = $this->getServer()->getPluginManager()->getPlugin("FormAPI"); $form = $api->createSimpleForm(function (Player $player, int $data = null) { $result = $data; if ($result === null) { return; } switch ($result) { case "0": $this->getServer()->dispatchcommand($player, "transfer IP PORT"); break; case "1": $this->getServer()->dispatchcommand($player, "transfer IP2 PORT2"); break; } }); $form->setTitle(TextFormat::DARK_PURPLE . "Server Selector"); $form->addButton(TextFormat::AQUA . "Factions"); $form->addButton(TextFormat::GREEN . "Prison"); $form->addButton(TextFormat::RED . "Exit"); $form->sendToPlayer($player); return $form; } /** * @param PlayerInteractEvent $event */ public function onInteract(PlayerInteractEvent $event) { $player = $event->getPlayer(); $item = Item::get(2, 0, 0); //2 is grass block, you can change it to what ever you want if ($player->getInventory()->getItemInHand()->getId() === $item) { $this->openServerMenu($player); $player->sendMessage("Opening Server UI..."); } }} now run your server and enjoy. if it has errors, just give me the error.
Hey im new to coding as you can tell i just have a question about one thing what is this for? ive seen it a few times now is it just notes to the coder or?
i just tried this and it still dosent work nothing happens i have a grass block and i click and nothing happens, help!
its because this code originally came with a config ive just grabbed a part of the code from the main so you'll need to add the ip and port yourself in the code and also give permission to the players to use 'transferserver'
<?php namespace besher\test; use pocketmine\Server; use pocketmine\Player; use pocketmine\plugin\PluginBase; use pocketmine\command\Command; use pocketmine\command\CommandSender; use pocketmine\utils\TextFormat; use jojoe77777\FormAPI; use pocketmine\event\Listener; use pocketmine\event\player\PlayerInteractEvent; use pocketmine\item\Item; class main extends PluginBase implements Listener { public function onEnable() { $this->getServer()->getPluginManager()->registerEvents($this, $this); } public function onCommand(CommandSender $sender, Command $cmd, String $label, Array $args) : bool{ switch($cmd->getName()){ case "core": if($sender instanceof Player){ $this->cosform($sender); } else { $sender->sendMessage("You cant open a ui when you are console"); } } return true; } public function cosForm($player){ $api = $this->getServer()->getPluginManager()->getPlugin("FormAPI"); $form = $api->createSimpleForm(function (Player $player, int $data = null){ $result = $data; if($result === null){ return true; } switch($result){ case 0: $this->openSizeForm($player); break; case 1: $this->openFlyForm($player); break; } }); $form->setTitle("Cosmetics"); $form->setContent("Cosmetics"); $form->addButton("Size"); $form->addButton("Fly"); $form->sendToPlayer($player); return $form; } public function openFlyForm($player){ $api = $this->getServer()->getPluginManager()->getPlugin("FormAPI"); $form = $api->createSimpleForm(function (Player $player, int $data = null){ $result = $data; if($result === null){ return true; } switch($result){ case 0: $player->setAllowFlight("true"); break; case 1: $player->setAllowFlight("false"); break; case 2: $this->cosForm($player); break; } }); $form->setTitle("Fly"); $form->setContent("Enable or Disable Flight"); $form->addButton("Enable Flight"); $form->addButton("Disable Flight"); $form->sendToPlayer($player); return $form; } public function openSizeForm($player){ $api = $this->getServer()->getPluginManager()->getPlugin("FormAPI"); $form = $api->createSimpleForm(function (Player $player, int $data = null){ $result = $data; if($result === null){ return true; } switch($result){ case 0: $player->setScale("5"); break; case 1: $player->setScale("3"); break; case 2: $player->setScale("1"); break; case 3: $this->cosForm($player); break; } }); $form->setTitle("Size"); $form->setContent("Choose a SIZE"); $form->addButton("Large"); $form->addButton("Medium"); $form->addButton("Normal"); $form->addButton("Back"); $form->sendToPlayer($player); return $form; } public function NaviForm($player){ $api = $this->getServer()->getPluginManager()->getPlugin("FormAPI"); $form = $api->createSimpleForm(function (Player $player, int $data = null){ $result = $data; if($result === null){ return true; } switch($result){ case 0: $command = "sw random"; $this->getServer()->getCommandMap()->dispatch($player, $command); break; case 1: $command = "is go"; $this->getServer()->getCommandMap()->dispatch($player, $command); break; case 2: $player->sendMessage("BedWars is currently under construction"); break; case 3: $player->sendMessage("TheBridge is currently under construction"); break; } }); $form->setTitle("§4§6NAVIGATOR"); $form->setContent("§lChoose a MINIGAME you would like to PLAY"); $form->addButton("§6§lSkyWars"); $form->addButton("§l§7SKY§l§aBLOCK"); $form->addButton("§l§4BED§f§lWars"); $form->addButton("§l§cTHEB§l§9RIDGE"); $form->sendToPlayer($player); return $form; } public function onInteract(PlayerInteractEvent $event) { $player = $event->getPlayer(); $item = Item::get(2, 0, 0); //2 is grass block, you can change it to what ever you want if ($player->getInventory()->getItemInHand()->getId() === $item) { $this->cosForm($player); $player->sendMessage("Opening Server UI..."); } } }