Hello,I'm trying to edit a cosmetics plugin to add some things I want, like a lightning stick which when you tap it(with interact event),it will remove one "lightning stick",but theres a problem.The first time it was removing all the sticks not the 1 only,and the other times when I searched my problem here, it wasn't do anything,or it was removing any item on hand,or it was giving errors.
PHP: public function onInteract(PlayerInteractEvent $ev) { $index = $ev->getPlayer()->getInventory()->getItemInHand()->getCustomName() == "Lightning stick"; if($index){ $item = Item::get(280); $item->pop(); $level = $ev->getPlayer()->getLevel(); $lightning = new AddEntityPacket(); $lightning->type = 93; $lightning->entityRuntimeId = Entity::$entityCount++; $lightning->metadata = []; $lightning->position = $ev->getPlayer()->asVector3()->add(0, $height = 0); $lightning->yaw = $ev->getPlayer()->getYaw(); $lightning->pitch = $ev->getPlayer()->getPitch(); $ev->getPlayer()->getServer()->broadcastPacket($level->getPlayers(), $lightning); } } }
I tried copy pasting all the possibly working codes to my plugin,but I had problems on every change I had done.
try this PHP: public function onPlayerInteract(PlayerInteractEvent $ev): void{ $player = $ev->getPlayer(); if(($item = $player->getInventory()->getItemInHand())->getCustomName() == "Lightning stick"){ $player->getInventory()->setItemInHand($item->pop()); $lightning = new AddEntityPacket(); $lightning->type = Entity::LIGHTNING_BOLT; $lightning->entityRuntimeId = Entity::$entityCount++; $lightning->metadata = []; $lightning->position = $player; $lightning->yaw = $player->getYaw(); $lightning->pitch = $player->getPitch(); $player->getServer()->broadcastPacket($player->getLevel()->getPlayers(), $lightning); }}