Advertisement
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Where is the error?

Comments in 'Plugin Development' started by Hotshot_9930, Oct 10, 2015.

  1. Hotshot_9930
    Offline

    Hotshot_9930 Notable Member Plugin Developer

    Joined:
    May 26, 2014
    Posts:
    665
    Plugins:
    2
    Minecraft User:
    HotshotHD
    Im making a deposit sign where players can exchange gold ingots for coins. It works except for 1 error.
    When you tap the sign holdng gold ingots, it creates another stack of gold ingots in your inventory instead of actually removing them.
    So now you have two stacks of gold ingots that are identical, except the original stack doesnt get removed from your hand when you tap the sign, but the duplictae one does.

    Where is the error?
    PHP:
             // Public function onTouch(PlayerInteractEvent $event)
             
    if($this->isDepositSign($tile)) {
                
    $item $player->getInventory()->getItemInHand();
                
    $index $player->getInventory()->getHeldItemIndex();
                if(
    $player->getInventory()->getItemInHand()->getId() == "266") {
                   
    $player->getInventory()->setItem($indexItem::get($item->getId(), 0$item->getCount() - 1));
                   
    $this->player->set("coins"$this->getCoins($player) + 10);
                   
    $this->savePlayerData($player);
                   
    $player->sendPopup("§bGold deposited\n§bNew balance: " $this->getCoins($player));
                  }
                  else {
                    
    $player->sendPopup("§cYou are not holding any §eGold Ingots");
                 }
               }
             }
    Last edited: Oct 10, 2015
  2. TryoneLegendZzz
    Offline

    TryoneLegendZzz Active Member

    Joined:
    May 31, 2015
    Posts:
    201
    Minecraft User:
    SavionLegendZzz
    forget i said that
  3. Hotshot_9930
    Offline

    Hotshot_9930 Notable Member Plugin Developer

    Joined:
    May 26, 2014
    Posts:
    665
    Plugins:
    2
    Minecraft User:
    HotshotHD
    Nvm. I have fixed the problem by changing
    PHP:
    $player->getInventory()->setItem($indexItem::get($item->getId(), 0$item->getCount() - 1));
    to
    PHP:
    $player->getInventory()->setItemInHand(Item::get($item->getId(), 0$item->getCount() - 1));
  4. TryoneLegendZzz
    Offline

    TryoneLegendZzz Active Member

    Joined:
    May 31, 2015
    Posts:
    201
    Minecraft User:
    SavionLegendZzz
    ok i understand :D
  5. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    getId() returns an int. You should compare it against an int (`286`), not a string (`"286"`)...
  6. Hotshot_9930
    Offline

    Hotshot_9930 Notable Member Plugin Developer

    Joined:
    May 26, 2014
    Posts:
    665
    Plugins:
    2
    Minecraft User:
    HotshotHD
    That's true but it still works because I used == and not ===
  7. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Yes, and it is bad practice to compare things incorrectly. I said should.
    Moreover, it is indeed slower in terms of performance to compare an int with a string while you can compare two ints directly. And it shows you have wrong concept if you do that.
    Hotshot_9930 and Taha_The_Hacker like this.

Share This Page

Advertisement