Issue with my code

Discussion in 'Plugin Development' started by coolmonkeyguy, Mar 24, 2014.

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

    coolmonkeyguy

    For some reason my code gives 2 glass bottles instead of the 1 glass bottle intended which is in the code.

    So the code is supposed to remove the water bottle on the right click and then right after add a glass bottle to the inventory. instead is does that and 2 seconds later it give another glass bottle making 2 glass bottles. But it only does this when you already have a glass bottle in your inventory. what could be the issue?

    Code:
    if(e.getAction() == Action.RIGHT_CLICK_AIR || e.getAction().equals(Action.RIGHT_CLICK_AIR)) {
                if (e.getPlayer().getItemInHand().getType() == Material.getMaterial(373)) {
                    Player player = e.getPlayer();
                    e.getPlayer().sendMessage(ChatColor.BLUE + "[" + ChatColor.LIGHT_PURPLE + "DrugLife" + ChatColor.BLUE +"]" + " " + ChatColor.GREEN + "Don't drink and drive!" );
                    e.getPlayer().addPotionEffect(new PotionEffect(PotionEffectType.CONFUSION ,300,10));
                    e.getPlayer().addPotionEffect(new PotionEffect(PotionEffectType.SATURATION ,400,2));
                    player.playSound(player.getLocation(), Sound.DRINK, 1, 1);
                    e.getPlayer().getInventory().removeItem(new ItemStack(373, 1));
                    e.getPlayer().getInventory().addItem(new ItemStack(374, 1));
                    e.getPlayer().updateInventory();
                   
                      player.playSound(player.getLocation(), Sound.BURP, 1, 1);

    Any help?
     
  2. Offline

    Norbu10

    Post full code please (of the event or something) otherwise i cant see whats wrong
     
  3. Offline

    DrEinsteinium

    coolmonkeyguy Okay, so I see various different problems with your code that look like they come from your confusion between the == operators and the .equals() method. However, I'm not sure if this will fix the problem that you're having. xD

    The .equals() method is used only when comparing objects and the == operator is only used to compare primitive data.

    So for example, if I want to compare two ints or booleans, I would use the == operator because they are primitive types. However, if I wanted to compare two Strings or in this case an Action object, I would use the .equals() method.
     
  4. Offline

    coolmonkeyguy

    Code:
    @SuppressWarnings("deprecation")
    @EventHandler
    public void onPlayerInteract(PlayerInteractEvent e) {
     
    if(e.getAction() == Action.RIGHT_CLICK_AIR || e.getAction().equals(Action.RIGHT_CLICK_AIR)) {
                if (e.getPlayer().getItemInHand().getType() == Material.getMaterial(373)) {
                    Player player = e.getPlayer();
                    e.getPlayer().sendMessage(ChatColor.BLUE + "[" + ChatColor.LIGHT_PURPLE + "DrugLife" + ChatColor.BLUE +"]" + " " + ChatColor.GREEN + "Don't drink and drive!" );
                    e.getPlayer().addPotionEffect(new PotionEffect(PotionEffectType.CONFUSION ,300,10));
                    e.getPlayer().addPotionEffect(new PotionEffect(PotionEffectType.SATURATION ,400,2));
                    player.playSound(player.getLocation(), Sound.DRINK, 1, 1);
                    e.getPlayer().getInventory().removeItem(new ItemStack(373, 1));
                    e.getPlayer().getInventory().addItem(new ItemStack(374, 1));
                    e.getPlayer().updateInventory();
                     
                      player.playSound(player.getLocation(), Sound.BURP, 1, 1);
                   
                }
        }
    If your talking about...
    Code:
    if (e.getPlayer().getItemInHand().getType() == Material.getMaterial(373)) {
    Everything works with checking to see if its a water bottle and it give the effects but the exchange of the water bottle for the glass bottle still does not work properly.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 7, 2016
  5. Offline

    DrEinsteinium

    coolmonkeyguy I know it most likely works, but using the == operator to compare objects is really frowned upon... It will just save you a lot of headaches if you learn the difference between the two ways to compare data now rather than later. ;)
    Code:
    if (e.getPlayer().getItemInHand().getType().equals(Material.getMaterial(373)))
    Edit: I looked up this concept on Google and found this:
    I wasn't entirely right... but close :p
     
  6. Offline

    lycano

    coolmonkeyguy do not use updateInventory(). You really dont need that. If an item does not get removed or added you have a different problem.

    Besides from that use Material instead of the ID. When comparing one type with another also do not use equality char "==" use getType().equals(Material.POTION) this is type save and will save you trouble.

    Same for ItemStack .. do not use ItemId use new ItemStack(Material.POTION) or new ItemStack(Material.GLASS_BOTTLE)
     
Thread Status:
Not open for further replies.

Share This Page