1. The forums will be archived and moved to a read only mode in about 2 weeks (mid march).

Solved How nbt works?

Discussion in 'Development' started by Yexeed, Jan 20, 2018.

  1. Yexeed

    Yexeed Slime

    Messages:
    76
    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
     
  2. Yexeed

    Yexeed Slime

    Messages:
    76
    Getting this up. So can anyone help me?
     
  3. Yexeed

    Yexeed Slime

    Messages:
    76
    Getting this up again.
     
  4. instantlyta

    instantlyta Slime

    Messages:
    96
    GitHub:
    intagaming
    Something like this would probably work:
    PHP:
    $tag $gun->getNamedTagEntry("rounds");
    $tag->setValue($tag->getValue() - 1);
    $gun->setNamedTagEntry("rounds"$tag);
     
  5. Yexeed

    Yexeed Slime

    Messages:
    76
    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);
     
  6. instantlyta

    instantlyta Slime

    Messages:
    96
    GitHub:
    intagaming
    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.
     
  7. Yexeed

    Yexeed Slime

    Messages:
    76
    Lol, i actually doesnt even setting the new item to the player. But how to get index of item on interactevent? I dont know...
     
    Daniil129932 likes this.
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.