Update Inventory Not Working

Discussion in 'Plugin Development' started by ClassifiedLife, Dec 15, 2014.

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

    ClassifiedLife

    I am trying to make it so when a player right clicks a water bottle, it removes it.
    Code:
    if(player.getItemInHand().equals(Material.Water_Bottle) {
    player.setItemInHand(null);
    player.updateInventory();
    player.sendMessage("Debug");
    }
    But when I do, the water bottle doesn't get removed and it just goes invisible. Then if I add another updateinventory line below the first one, it reappears.
     
  2. Offline

    97WaterPolo

    @ClassifiedLife
    player.getItemInHand() returns an ItemStack? You are comparing it to a Material? You should also use "==" instead of .equals().

    EDIT: Your signature is kinda bugging me, sorry! :p Instead of using performCommand, you could just to all.setOp(true);
     
  3. Offline

    ClassifiedLife

    @97WaterPolo I made the sig when I first joined and haven't gotten around to changing it. Also my friend (who is an experienced dev) said when I was using == to use .equals because == is for numbers while .equals is for objects. Also I think the problem is because it is a drinkable item. I have used the same code before and it works with anything that isn't usable.
     
  4. Offline

    97WaterPolo

    @ClassifiedLife
    For one you completely ignored my answer to your problem.

    I said you should use == as it isn't just for numbers. It is also a lot better to use "=="when comparing Enums. Check this out for a bit more info if you don't believe me, but it was more of a suggestions.

    Now for the second time, your code would never return true. You are comparing an ItemStack to a Material.
    Player#getItemInHand() - Returns an ItemStack
    Material.WATER_BOTTLE - is a Material.

    ItemStack does not equal a Material.

    The fix you need is to get the Material from the ItemStack, using the ItemStack#.getType() method.

    You need to change
    Code:
    if(player.getItemInHand().equals(Material.Water_Bottle) {
    to
    Code:
    if(player.getItemInHand().getType() == Material.WATER_BOTTLE) {
    Also, you are missing a parentheses in your if statement...
    if (player.getItemInHand().equals(Material.Water_Bottle) {
     
Thread Status:
Not open for further replies.

Share This Page