Only one line gets executed at a time

Discussion in 'Plugin Development' started by Falketto, Jul 29, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    Falketto

    I'm probably just being blind and overseeing something simple but i really cannot find a way to make it work.

    This is my code:

    Code:java
    1. @EventHandler
    2. public void onRightClick(PlayerInteractEvent ev){
    3.  
    4. if(ev.getAction() == Action.RIGHT_CLICK_BLOCK && ev.getClickedBlock().getState() instanceof Sign){
    5.  
    6. Player p = (Player) ev.getPlayer();
    7. Sign sign = (Sign) ev.getClickedBlock().getState();
    8. String line1 = sign.getLine(1);
    9.  
    10. if(line1 != null){
    11. p.getInventory().addItem(new ItemStack(Material.WOOD_AXE, 1));
    12. p.getInventory().addItem(new ItemStack(Material.WOOD_HOE, 1));
    13. p.getInventory().addItem(new ItemStack(Material.WOOD_PICKAXE, 1));
    14. p.getInventory().addItem(new ItemStack(Material.FISHING_ROD, 1));
    15. p.getInventory().addItem(new ItemStack(Material.WOOD_SPADE, 1));
    16. }
    17. }
    18.  
    19. }


    when i right click the sign it gives me the wooden axe only. the next time i right click the sign i only get the wooden hoe and so on. i have no idea what i did wrong.

    Also, since i'm here already, when i change the condition to
    Code:java
    1. if(line1 == "Gather Tools"){...
    it doesn't work at all. Any ideas on that?

    Thanks a lot in advance
     
  2. Offline

    _Filip

    You aren't updated the inventory
     
    Falketto likes this.
  3. Offline

    fireblast709

    Falketto don't compare Strings with ==, use .equals() or .equalsIgnoreCase() instead. == checks for the same reference, which doesn't work with Strings due to how Java deals with Strings.
     
    CandyCranium and Falketto like this.
  4. Offline

    Falketto

    Thanks to both of you. It worked. May I ask though, why i need to update the inventory and what exactly that does?
     
  5. Offline

    travja

    Falketto You have to update the inventory because otherwise the client doesn't see it has more items to display. When they interact with an "empty" slot that has a tool in it, it would show up. So basically, whenever you are doing things with inventories, update them.
     
    Falketto likes this.
  6. Offline

    Falketto

    Thank you!
     
Thread Status:
Not open for further replies.

Share This Page