InstantSoup

Discussion in 'Plugin Development' started by HungryThirstyDead, Jul 16, 2014.

Thread Status:
Not open for further replies.
  1. Hey Guys,

    HungryThirstyDead here, with a quick little bug. I have this code for an InstaSoup plugin im working on, and I am wondering why its not working:
    Code:
        @EventHandler
        public void Interact(PlayerInteractEvent e) {
        Player p = e.getPlayer();
        ItemStack MS = new ItemStack (Material.MUSHROOM_SOUP);
        if (MC.getConfig().getString("FastSoup").equalsIgnoreCase("enabled")) {
            if(p.getItemInHand().equals(new ItemStack (Material.MUSHROOM_SOUP)) && (e.getAction().equals(Action.RIGHT_CLICK_BLOCK) || e.getAction().equals(Action.RIGHT_CLICK_AIR))) {
                        ItemStack Hand = p.getItemInHand();
                        Damageable d = p;
                        double H = d.getHealth();
                        double Heal = MC.getConfig().getDouble("SoupHeal");
                        if (H <= 20.0 - Heal) {
                            e.setCancelled(true);
                            p.setHealth(H + Heal);
                            if (Hand.getAmount() == 1) {
                                p.setItemInHand(new ItemStack (Material.BOWL));
                            }
                            else if (Hand.getAmount() > 1) {
                                ItemStack MSS = new ItemStack(Material.MUSHROOM_SOUP);
                                MSS.setAmount(Hand.getAmount() - 1);
                                p.setItemInHand(MSS);
                                p.getInventory().addItem(new ItemStack (Material.BOWL));
                            }
                        }
                        else if (!(H <= 19.0 - Heal)) {
                            e.setCancelled(true);
                            p.setHealth(20.0);
                            if (Hand.getAmount() > 1) {
                                ItemStack MSS = new ItemStack(Material.MUSHROOM_SOUP);
                                MSS.setAmount(Hand.getAmount() - 1);
                                p.setItemInHand(MSS);
                                p.getInventory().addItem(new ItemStack (Material.BOWL));
                            }
                            else if (Hand.getAmount() == 1) {
                                p.setItemInHand(new ItemStack (Material.BOWL));
                            }
                        }
                        else if (H == 20.0) {
                            e.setCancelled(true);
                           
                        }
                }
                else {
                    return;
                    }
                }   
        }
    
    Thanks in advance for any help!
     
  2. Offline

    Phantom_64

    What is the output? Post the stacktrace, if there is one.
     
    HungryThirstyDead likes this.
  3. Offline

    xize

    HungryThirstyDead

    Any errors?, if so please post it, however I suspect that one of the first lines is may wrong the one with .equals(new ItemStack()) where your items stacked? because if the size of that ItemStack in the inventory is 2 or lets say 1 then it would never equals because the size on your created item is 0
     
    HungryThirstyDead likes this.
  4. Offline

    TheMcScavenger

    Use:
    Code:java
    1. if(player.getItemInHand().getType().equals(Material.MUSHROOM_SOUP)){
    2. // code
    3. }
     
    HungryThirstyDead likes this.
  5. Offline

    aaomidi

    Also general coding advice, start using more return; and less nested if statements.

    Use a try/catch and print out the Exception.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 9, 2016
    HungryThirstyDead likes this.
  6. Offline

    Necrodoom

    Sounds like TheMcScavenger nailed your problem.
    Although, instead of simply spoonfeeding, he could explain that because you compared itemstack, then it would only return true if the amounts are the same, so you should compare material, to see if the itemstack is of material soup.
     
    HungryThirstyDead likes this.
  7. Thank you all for all your help!
     
Thread Status:
Not open for further replies.

Share This Page