So what I wanna do is when I someone does /upgrade while holding a Pickaxe it opens a UI, I have that done, but Next that I wanna do is when you press a button for example the Efficiency button on the UI it adds efficiency to the tool for 5xp but the next time you press it, it costs 5xp x (enchant level) and it makes the Efficiency One Level Higher!
i have this on my server, do i wanna teach you how..... sure why not get the original lvl remove the item + lvl by 1 Add item back ez
Code: $efficiencyLevel = $sender->getInventory()->getItemInHand()->getEnchantmentLevel(Enchantment::EFFICIENCY); $newLevel = $efficiencyLevel + 1; $requiredXpLevel = 5 * $newLevel; if($sender->getXpLevel() >= $requiredXpLevel){ $sender->sendMessage("§c§lYou dont got enough XP!"); } if($newLevel > 25) { $sender->sendMessage("§l§cYou cant get over 25 levels of Efficiency"); } else { $ench = Enchantment::getEnchantment(Enchantment::EFFICIENCY); $item = $sender->getInventory()->getItemInHand(); $item->addEnchantment(new EnchantmentInstance($ench, $newLevel)); $player->subtractXpLevels(5 * $newLevel); $sender->getInventory()->setItemInHand($item); } Do u know why this keeps crashing me, It has something to do with the XP, it crashes if you dont got enough XP
PHP: if ($sender instanceof Player) { $efficiencyLevel = $sender->getInventory()->getItemInHand()->getEnchantmentLevel(Enchantment::EFFICIENCY); $newLevel = $efficiencyLevel + 1; $requiredXpLevel = 5 * $newLevel; if ($sender->getXpLevel() < $requiredXpLevel) { #also Fixed the < that was wrong... Also dont need the = sign $sender->sendMessage("§c§lYou dont have enough XP!"); return; #added the return } if ($newLevel > 25) { $sender->sendMessage("§l§cYou cant get over 25 levels of Efficiency"); return; #You forgot this return aswell } else { $ench = Enchantment::getEnchantment(Enchantment::EFFICIENCY); $item = $sender->getInventory()->getItemInHand(); $item->addEnchantment(new EnchantmentInstance($ench, $newLevel)); #remove this its wrong $player->subtractXpLevels(5 * $newLevel); $sender->setXpLevel($sender->getXpLevel() - 5 * $newLevel); #added the right way $sender->getInventory()->setItemInHand($item); }}
Code: public function onCommand(CommandSender $sender, Command $command, string $label, array $args) : bool{ if ($sender instanceof Player and $command->getName() === "upgrade") { if($sender->getInventory()->getItemInHand()->getId() === Item::IRON_PICKAXE){ $this->picForm($sender); } if($sender->getInventory()->getItemInHand()->getId() === Item::DIAMOND_PICKAXE){ $this->picForm($sender); } if($sender->getInventory()->getItemInHand()->getId() === Item::STONE_PICKAXE){ $this->picForm($sender); } if($sender->getInventory()->getItemInHand()->getId() === Item::WOODEN_PICKAXE){ $this->picForm($sender); } }else{ $sender->sendMessage("§cThis cannot be upgraded!"); } return true; } public function picForm(Player $sender) { $form = new SimpleForm(function (Player $sender, $data){ if ($data === null) { return; } switch ($data) { case 0: // Second button (to second page) if ($sender instanceof Player) { $efficiencyLevel = $sender->getInventory()->getItemInHand()->getEnchantmentLevel(Enchantment::EFFICIENCY); $newLevel = $efficiencyLevel + 1; $requiredXpLevel = 5 * $newLevel; if ($sender->getXpLevel() < $requiredXpLevel) { //also Fixed the < that was wrong... Also dont need the = sign $sender->sendMessage("§c§lYou dont have enough XP!"); return; //added the return } if ($newLevel > 25) { $sender->sendMessage("§l§cYou cant get over 25 levels of Efficiency"); return; //You forgot this return aswell } else { $ench = Enchantment::getEnchantment(Enchantment::EFFICIENCY); $item = $sender->getInventory()->getItemInHand(); $item->addEnchantment(new EnchantmentInstance($ench, $newLevel)); //remove this its wrong $player->subtractXpLevels(5 * $newLevel); $sender->setXpLevel($sender->getXpLevel() - 5 * $newLevel); //added the right way $sender->getInventory()->setItemInHand($item); } } break; case 1: // Second butto (to second page) if ($sender instanceof Player) { $unbreakingLevel = $sender->getInventory()->getItemInHand()->getEnchantmentLevel(Enchantment::UNBREAKING); $newLevel = $unbreakingLevel + 1; $requiredXpLevel = 8 * $newLevel; if ($sender->getXpLevel() < $requiredXpLevel) { //also Fixed the < that was wrong... Also dont need the = sign $sender->sendMessage("§c§lYou dont have enough XP!"); return; //added the return } if ($newLevel > 30) { $sender->sendMessage("§l§cYou cant get over 30 levels of Unbreaking"); return; //You forgot this return aswell } else { $ench = Enchantment::getEnchantment(Enchantment::UNBREAKING); $item = $sender->getInventory()->getItemInHand(); $item->addEnchantment(new EnchantmentInstance($ench, $newLevel)); //remove this its wrong $player->subtractXpLevels(5 * $newLevel); $sender->setXpLevel($sender->getXpLevel() - 5 * $newLevel); //added the right way $sender->getInventory()->setItemInHand($item); } } break; } } ); $form->setTitle("§b§lPickaxe Upgrade"); $form->setContent("Upgrade your enchantments!"); $form->addButton("Efficiency\n5xp * Level"); $form->addButton("Unbreaking\n8xp * Level"); $form->addButton("§8Close"); $form->sendToPlayer($sender); } I get this error when I use this code!