Need help with coding

Discussion in 'Plugin Development' started by HYPExMon5ter, Dec 31, 2015.

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

    HYPExMon5ter

    I am trying to give a player as much xp (levels) as they do gold ingots, I want it to use ALL of the gold ingots they have in they're inventory at once and give them how much xp they need. Here is my code so far.

    Code:
             @EventHandler
             public void onInteract(PlayerInteractEvent e) {
                 if (e.getAction() == Action.RIGHT_CLICK_BLOCK) {
                 Player p = e.getPlayer();
                 if(e.getClickedBlock().getType() == Material.GOLD_BLOCK) {
                    if (!(p.getInventory().contains(new ItemStack(Material.GOLD_INGOT, 64)))) {
                        p.sendMessage(ChatColor.DARK_RED + "You do not have 64 Gold Ingots, come back when you do!");
                        return;
                    }
                     p.getInventory().removeItem(new ItemStack (Material.GOLD_INGOT, 64));
                 p.giveExpLevels(30);
                          }
                     }
                 }
     
  2. Offline

    ark9026

    You need to contain your removeIetm and experience levels in an "else" statement :)
     
  3. Offline

    Xerox262

    @ark9026 No he doesn't he returns in the original if, that means anything after that is essentially an else statement.

    By the way, your signature is wrong, Strings don't have a hasPermission method and you can't compare Strings with ==

    @HYPExMon5ter loop through the inventory, check if an item is a gold ingot, if it is then add the levels based on ItemStack#getAmount(); then remove them from the inventory.
     
  4. Offline

    ark9026

    It's good practice to add it in, even if it isn't completely necessary. The signature is more of a joke than anything :p
     
  5. May I ask why you can't compare strings with ==? Not actually sure why.
    I had an issue with comparing ItemStacks earlier, maybe this is the solution.
     
  6. Offline

    Javlin

    You can only compare objects with "==". When you are comparing strings, the objects are different, but the text may be the same.
     
  7. I see. How should ItemStacks be compared? Sorry, you might not have seen as I updated my last post at the last moment.
     
  8. Offline

    Xerox262

    == checks to make sure that they are stored in the same place in memory. .equals() checks to make sure they have the same data.

    When you do "" you are essentially creating another string in memory.

    The only things you should check with == rather than .equals are:
    • If it is null, if you tried to check if something was null with .equals it would throw a null pointer as you called a method on a null object
    • If it is an enum
    • If it is a primitive, something like int, double, float.
    Use != to check that it's not null, then use .equals to compare it.

    Lets say you were doing
    Code:
    ItemStack itemStack = new ItemStack(Material.DIRT);
    
    if (itemStack == new ItemStack(Material.DIRT)) // That would return false because it's two different itemstacks
     
  9. Thanks a bunch! This is probably responsible for half my broken plugins!
     
  10. Offline

    Zombie_Striker

    • Use "==" when comparing memory space (mostly, use it for Enums)
    • Use .equals when comparing data between objects
    • Use .isSimular() when comparing Itemstacks (this is in the case you do not want to check if durability is the same.)
     
  11. Offline

    Mrs. bwfctower

    Even more so, primitives.
     
    Zombie_Striker likes this.
Thread Status:
Not open for further replies.

Share This Page