Why wont this code work?

Discussion in 'Plugin Development' started by theone1000, Jul 1, 2015.

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

    theone1000

    @EventHandler
    public void Interact(PlayerInteractEvent e){
    ItemStack sponge = new ItemStack(Material.SPONGE);

    if(e.getClickedBlock().equals(sponge) && e.getPlayer().getInventory().getItemInHand().equals(Material.STICK)){
    Player player = e.getPlayer();
    player.sendMessage("test");
    }


    im getting no errors it just wont work in game
     
  2. Offline

    justin_393

    Did you register the event?
     
  3. Offline

    theone1000

    Yes.
     
  4. Offline

    Ward1246

    You could try removing the "ItemStack sponge = new ItemStack(Material.SPONGE);" line, and changing the line "if(e.getClickedBlock().equals(sponge) " to "if(e.getClickedBlock().equals(Material.SPONGE) " I think that might fix it.

    I say this because this can be very specific. It looks like you are testing if the player right clicks a ItemStack sponge, instead of a block.
     
  5. Offline

    theone1000

    didn't work tried it thanks for reply

    if(e.getClickedBlock().equals(Material.SPONGE) && e.getPlayer().getInventory().getItemInHand().equals(Material.STICK)){
    player.sendMessage("test");
    }
     
  6. Offline

    justin_393

    Try if(e.getClickedBlock() == Material.SPONGE)
    Same with the second one
    (On phone and it's hard to code)
     
  7. Offline

    Ward1246

    Ok, try this then:
    Code:
    Player player = e.getPlayer();
    
    if(e.getClickedBlock().getType() == Material.SPONGE && player.getInventory().getItemInHand().equals(Material.STICK))) {
    I am just changing the top part so you don't have to get the player all over again. That small difference might do the trick.

    Edit: That should do it, I forgot the .getType(). Now it should work.
     
  8. Offline

    terturl890

    do


    Code:
    @EventHandler
        public void onClick(PlayerInteractEvent e) {
          
            if(!(e.getClickedBlock().getType() == Material.SPONGE)) return;
            if(!(e.getPlayer().getItemInHand().getType() == Material.STICK)) return;
          
            e.getPlayer().sendMessage("Test");
          
        }
    this will be sure that the clickblock MUST be a sponge and that the item MUST be a stick or it wont do anything.
     
  9. Offline

    schwabfl


    getClickedBlock() returns a block, you can't compare it with an ItemStack.
    use
    Code:
    if (e.getClickedBlock().getType() == Material.SPONGE) { }
    getItemInHand() returns an ItemStack, you can't compare it with a Material.
    use
    Code:
    ItemStack itemStack = e.getPlayer().getItemInHand();
    if (itemStack != null && itemStack.getType() == Material.STICK) { }
     
    Konato_K likes this.
  10. Offline

    terturl890

    @schwabfl
    got to it before yah ;P haha but yours is more detailed and described.
     
  11. Offline

    justin_393

    Yes, .getType() is the issue. I completely forgot about that.. Doesn't eclipse usually warn about that?
     
  12. Offline

    Ward1246

    I don't think it could warn about that. It would be awesome if it could though.
     
    Last edited: Jul 1, 2015
  13. Offline

    terturl890

    Eclipse wont warn it because .equals returns an Object.... which is basically everything
     
  14. Offline

    schwabfl

    .equals() returns a boolean, it takes an Object as a parameter
     
    Konato_K likes this.
  15. Offline

    justin_393

    I thought .equals() compares Strings..
     
  16. Offline

    Hawktasard

    @theone1000
    This checks if an item is similar to another item, ignoring the amount.
    Code:java
    1. if(item.isSimilar(otherItem)) {
    2. // TODO Do something
    3. }

    Edit: If you're going to be using a normal sponge use
    Code:java
    1. if(item.getType() == Material.SPONGE)
     
  17. Offline

    Konato_K

    @justin_393 No, equals is a method in the Object class and it compares objects, equalsIgnoreCase is a String-only method.
     
  18. Offline

    schwabfl

    equals() is a method declared in the Object class
    If not overriden, equals(Object other) returns other == this;
    String is not the only class that overrides the equals method
     
Thread Status:
Not open for further replies.

Share This Page