Itemstacks only registered when in 1

Discussion in 'Plugin Development' started by BlackNoyse, Apr 16, 2015.

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

    BlackNoyse

    I am coding a plugin that upgrades the enchantment of a pickaxe. For example, if you were to have a item called an Enchantment Token (with the colors only given from a command), you'd be able to upgrade the pickaxe and have 1 enchantment token taken off you. However, the sign only picks up the items when they are only by themselves, not a stack. So basically it only picks up 1 item not 2-64.

    My code:
    Code:
    package me.OfficialExus.opTokensDev;
    
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.block.Sign;
    import org.bukkit.enchantments.Enchantment;
    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;
    import org.bukkit.inventory.meta.ItemMeta;
    
    public class Listeners implements Listener {
       
        @EventHandler
        public void onSignCreate(SignChangeEvent event) {
            Player player = event.getPlayer();
            if(event.getLine(0).equalsIgnoreCase("[Upgrade]")) {
                if(event.getLine(1).equalsIgnoreCase("Efficiency")) {
                    player.sendMessage(ChatColor.GREEN + "Efficiency upgrade sign successfully created.");
                    event.setLine(0, ChatColor.GREEN + "[Upgrade]");
                } if(event.getLine(1).equalsIgnoreCase("Fortune")) {
                    player.sendMessage(ChatColor.GREEN + "Fortune upgrade sign successfully created.");
                    event.setLine(0, ChatColor.GREEN + "[Upgrade]");
                } if(event.getLine(1).equalsIgnoreCase("Haste")) {
                    player.sendMessage(ChatColor.GREEN + "Haste upgrade sign successfully created.");
                    event.setLine(0, ChatColor.GREEN + "[Upgrade]");
                }
            }
        }
       
        @EventHandler
        public void onInteract(PlayerInteractEvent e) {
           
            ItemStack item = new ItemStack(Material.PRISMARINE_SHARD);
            ItemMeta itemMeta = item.getItemMeta();
            itemMeta.setDisplayName(ChatColor.GOLD + "Enchantment Token " + ChatColor.GRAY + "(Right Click)");
            item.setItemMeta(itemMeta);
           
            Player p = e.getPlayer();
            ItemStack m = p.getInventory().getItemInHand();
            Inventory inv = p.getInventory();
            int level = 0;
            Sign s = (Sign) e.getClickedBlock().getState();
            if(e.getAction() == Action.RIGHT_CLICK_BLOCK) {
                if(e.getClickedBlock().getState() instanceof Sign) {
                    if(s.getLine(0).equalsIgnoreCase(ChatColor.GREEN + "[Upgrade]") && s.getLine(1).equalsIgnoreCase("Efficiency")) {
                        if(m.getType() == Material.DIAMOND_PICKAXE || m.getType() == Material.DIAMOND_AXE || m.getType() == Material.DIAMOND_SPADE) {
                            if(inv.contains(item)) {
                                inv.removeItem(item);
                                p.updateInventory();
                            p.sendMessage(ChatColor.GOLD + "Enchantment Tokens " + ChatColor.DARK_GRAY + "| " + ChatColor.GREEN + "Successfully added +1 Efficiency to your tool.");
                        p.getInventory().getItemInHand().getEnchantments().containsKey(Enchantment.DIG_SPEED);
                        level = p.getInventory().getItemInHand().getEnchantmentLevel(Enchantment.DIG_SPEED);
                        p.getInventory().getItemInHand().addUnsafeEnchantment(Enchantment.DIG_SPEED, 1 + level);
                        e.setCancelled(true);
                            } else {
                                p.sendMessage(ChatColor.GOLD + "Enchantment Tokens " + ChatColor.DARK_GRAY + "| " + ChatColor.RED + "You do not hold any Enchantment Tokens.");
                            }
                        } else {
                            p.sendMessage(ChatColor.GOLD + "Enchantment Tokens " + ChatColor.DARK_GRAY + "| " + ChatColor.RED + "Wrong tool in hand, please equipt a Diamond Pickaxe, Axe or Shovel and right click to continue.");
                        }
                    } else if(s.getLine(0).equalsIgnoreCase(ChatColor.GREEN + "[Upgrade]") && s.getLine(1).equalsIgnoreCase("Fortune")) {
                        if(m.getType() == Material.DIAMOND_PICKAXE || m.getType() == Material.DIAMOND_AXE || m.getType() == Material.DIAMOND_SPADE) {
                            if(inv.contains(item)) {
                                inv.remove(item);
                                p.updateInventory();
                            p.sendMessage(ChatColor.GOLD + "Enchantment Tokens " + ChatColor.DARK_GRAY + "| " + ChatColor.GREEN + "Successfully added +1 Fortune to your tool.");
                        p.getInventory().getItemInHand().getEnchantments().containsKey(Enchantment.LOOT_BONUS_BLOCKS);
                        level = p.getInventory().getItemInHand().getEnchantmentLevel(Enchantment.LOOT_BONUS_BLOCKS);
                        p.getInventory().getItemInHand().addUnsafeEnchantment(Enchantment.LOOT_BONUS_BLOCKS, 1 + level);
                        e.setCancelled(true);
                            } else {
                                p.sendMessage(ChatColor.GOLD + "Enchantment Tokens " + ChatColor.DARK_GRAY + "| " + ChatColor.RED + "You do not hold any Enchantment Tokens.");
                            }
                        } else {
                            p.sendMessage(ChatColor.GOLD + "Enchantment Tokens " + ChatColor.DARK_GRAY + "| " + ChatColor.RED + "Wrong tool in hand, please equipt a Diamond Pickaxe, Axe or Shovel and right click to continue.");
                        }
                    }
                }
            }
        }
    }
    Thanks.
     
  2. Code:java
    1.  
    2. @EventHandler
    3. public void onSignCreate(SignChangeEvent event) {
    4. Player player = event.getPlayer();
    5. if(event.getLine(0).equalsIgnoreCase("[Upgrade]")) {
    6. if(event.getLine(1).equalsIgnoreCase("Efficiency")) {
    7. player.sendMessage(ChatColor.GREEN + "Efficiency upgrade sign successfully created.");
    8. event.setLine(0, ChatColor.GREEN + "[Upgrade]");
    9. } if(event.getLine(1).equalsIgnoreCase("Fortune")) {
    10. player.sendMessage(ChatColor.GREEN + "Fortune upgrade sign successfully created.");
    11. event.setLine(0, ChatColor.GREEN + "[Upgrade]");
    12. } if(event.getLine(1).equalsIgnoreCase("Haste")) {
    13. player.sendMessage(ChatColor.GREEN + "Haste upgrade sign successfully created.");
    14. event.setLine(0, ChatColor.GREEN + "[Upgrade]");
    15. }
    16. }
    17. }

    Why not just
    Code:java
    1. @EventHandler
    2. public void onSignCreate(SignChangeEvent event) {
    3. Player player = event.getPlayer();
    4. if(event.getLine(0).equalsIgnoreCase("[Upgrade]")) {
    5. for(String upgrade : new String[]{"Efficicency", "Fortune", "Haste"}) {
    6. if(event.getLine(1).equalsIgnoreCase(upgrade)) {
    7. player.sendMessage("§a"+ upgrade + " upgrade sign successfully created.");
    8. event.setLine(0, "§a[Upgrade]");
    9. }
    10. }
    11. }
    12. }
     
  3. Offline

    BlackNoyse

    Never thought of that.

    Thanks for the help on the signs; however the question still has not been answered.

    It accepts stacks up to 25, but not any higher.

    Thanks.
     
  4. Offline

    BlackNoyse

    Solved, changed
    Code:
    if(inv.contains(item)) {
    with
    Code:
    if(inv.containsAtLeast(item, 1)) {
     
Thread Status:
Not open for further replies.

Share This Page