Solved Soup plugin bug

Discussion in 'Plugin Development' started by srspore, Sep 12, 2015.

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

    srspore

    So I've been working on a soup plugin where the player is healed 3.5 hearts when they right click a soup, if the player's health is full it will heal 3.5 hunger chicken nugget things. The empty bowls will refill or not drain sometimes. If I right click a soup while looking at a block it works normally, however when I right click a soup while looking off into the air my health or food will increase but the bowl will either become empty, not become empty, or not become empty and refill other bowls in my hotbar.
    <Link removed> download ,if you want to mess around with it to experience the problem.
    This is my listener class, I think that's where the problem would be but I can add my other classes if anyone wants them. Thank you to anyone who can help me fix this, I've been trying to get his to work all day!
    Code:
    package me.srspore.kit;
    
    import org.bukkit.Bukkit;
    import org.bukkit.Material;
    import org.bukkit.block.Sign;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.block.SignChangeEvent;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    
    public class Soup implements Listener {
        @EventHandler
        public void onPlayerInteract(PlayerInteractEvent e) {
            Player p = (Player) e.getPlayer();
            if ((e.getAction() == Action.RIGHT_CLICK_AIR)
                    || (e.getAction() == Action.RIGHT_CLICK_BLOCK)) {
                if (p.getItemInHand().getType() == Material.MUSHROOM_SOUP) {
                    if (p.getHealth() >= 20) {
                        if (p.getFoodLevel() <= 20) {
                            p.setItemInHand(new ItemStack(Material.BOWL));
                            if (p.getFoodLevel() + 7 > 20) {
                                p.setFoodLevel(20);
                            } else {
                                p.setFoodLevel(p.getFoodLevel() + 7);
                            }
                        }
                    } else {
                        p.setItemInHand(new ItemStack(Material.BOWL));
                        if (p.getHealth() + 7 > 20) {
                            p.setHealth(20);
                        } else {
                            p.setHealth(p.getHealth() + 7);
                        }
                    }
                }
            }
            if (e.getClickedBlock().getState() instanceof Sign
                    && e.getAction() == Action.RIGHT_CLICK_BLOCK) {
                Sign sign = (Sign) e.getClickedBlock().getState();
                if (sign.getLine(1).equalsIgnoreCase("§3[Soup]")) {
                    Inventory inv = Bukkit.getServer().createInventory(null, 54,
                            "Soup");
                    for (int i = 0; i < 54; i++) {
                        inv.addItem(new ItemStack(Material.MUSHROOM_SOUP));
                    }
                    p.openInventory(inv);
                }
            }
        }
    
        @EventHandler
        public void onSignChange(SignChangeEvent e) {
            if (e.getLine(0).equalsIgnoreCase("[Soup]")) {
                e.setLine(0, "");
                e.setLine(1, "§3[Soup]");
            }
        }
    
    }
    
    <Edited by bwfcwalshy: Removed MediaFire link, it is not allowed here. Upload to Dropbox or somewhere similar.>
     
    Last edited by a moderator: Sep 15, 2015
  2. Offline

    TehHypnoz

    Try cancelling the event.
     
  3. Offline

    srspore

    @TehHyponz What do you mean?
     
  4. Offline

    SuperSniper

    @srspore
    Code:
    e.setCancelled(true);
    
     
  5. Offline

    srspore

    @SuperSniper Thank you
    @TehHypnoz Didn't really fix it, is it a problem with "Action.RIGHT_CLICK_AIR" that's what it feels like to me but I don't know what's wrong with it

    @TehHypnoz Sorry I canceled it at the wrong point in my code, it's working now. Thank you so much!
     
    Last edited by a moderator: Sep 15, 2015
  6. Offline

    TehHypnoz

    No problem :)
     
  7. Offline

    Mee8a

  8. @Mee8a It isn't, please report rather than just tagging me.
     
  9. Offline

    srspore

Thread Status:
Not open for further replies.

Share This Page