I making plugin that adding guns in the game. I decided to use NBT to control ammo and maxammo of gun. I using forms to buy guns. Here is the code, how i giving gun to the player: PHP: $item = self::$items[$pdata[0]];$item->setCustomName("§r§b§l" . self::$types[$pdata[0]] . " " . $this->tr($this->translateType($pdata[0]).".name.{$pdata[1]}"));$item->setNamedTagEntry(new ByteTag("isGun", 1));$item->setNamedTagEntry(new IntTag("type", $pdata[0]));$item->setNamedTagEntry(new IntTag("subtype", $pdata[1]));$item->setNamedTagEntry(new ByteTag("isReloading", false));$item->setNamedTagEntry(new IntTag("rounds", 0));$item->setNamedTagEntry(new IntTag("maxRounds", $this->getMaxRound($pdata[0], $pdata[1])));$p->getInventory()->addItem($item); Finally, player gets the gun, which ammo is equals 0, and max ammo is equals number from config. Here is the code that executes when player shooting: PHP: ($tag = $gun->getNamedTagEntry("rounds"))->setValue($tag->getValue() - 1); The following line should subtract 1 from original ammo of gun. But if i gonna dump value of tag "rounds" on every shoot I just getting the old value. Example: I have 999 ammo in my gun. After shoot it should be 998. But sending player his ammo of gun in chat giving me only 999 again p.s. sorry for my bad english pls, i hope you get the thing
Something like this would probably work: PHP: $tag = $gun->getNamedTagEntry("rounds");$tag->setValue($tag->getValue() - 1);$gun->setNamedTagEntry("rounds", $tag);
Thanks for reply, but provided code dont works too. PHP: //on gun shoot$tag = $gun->getNamedTagEntry("rounds");$tag->setValue($tag->getValue() - 1);$gun->setNamedTagEntry($tag);
Hnmm, did you remember to send the new Item $gun to the client? PHP: /**@var Item $gun *///on gun shoot$tag = $gun->getNamedTagEntry("rounds");$tag->setValue($tag->getValue() - 1);$gun->setNamedTagEntry($tag);// Somehow to get the $index, Transaction nowadays is so bad I couldn't resist..$p->getInventory->setItem($slot, $gun); My experience: You should save your gun ammo server-side. Sometimes client sends 2 shoot packet which will result in ammo duplication. Maybe 30 rounds => 50 rounds, depends on the server TPS.
Lol, i actually doesnt even setting the new item to the player. But how to get index of item on interactevent? I dont know...