Error when you right click a item and it is set to your chestplate!

Discussion in 'Spigot Plugin Development' started by LuckyLuckiest, Apr 13, 2017.

  1. Hey guys, I did a code that you right click a Shulker box and then it moves it to your inventory!
    But that sends error, any help will be good :D
    Code (Text):
    [20:47:02 FATAL]: Error executing task
    java.util.concurrent.ExecutionException: java.lang.AssertionError: TRAP
            at java.util.concurrent.FutureTask.report(Unknown Source) ~[?:1.8.0_111]
            at java.util.concurrent.FutureTask.get(Unknown Source) ~[?:1.8.0_111]
            at net.minecraft.server.v1_11_R1.SystemUtils.a(SourceFile:47) [spigot.jar:git-Spigot-625bc00-d09304e]
            at net.minecraft.server.v1_11_R1.MinecraftServer.D(MinecraftServer.java:747) [spigot.jar:git-Spigot-625bc00-d09304e]
            at net.minecraft.server.v1_11_R1.DedicatedServer.D(DedicatedServer.java:399) [spigot.jar:git-Spigot-625bc00-d09304e]
            at net.minecraft.server.v1_11_R1.MinecraftServer.C(MinecraftServer.java:678) [spigot.jar:git-Spigot-625bc00-d09304e]
            at net.minecraft.server.v1_11_R1.MinecraftServer.run(MinecraftServer.java:576) [spigot.jar:git-Spigot-625bc00-d09304e]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_111]
    Caused by: java.lang.AssertionError: TRAP
            at net.minecraft.server.v1_11_R1.ItemStack.F(ItemStack.java:94) ~[spigot.jar:git-Spigot-625bc00-d09304e]
            at net.minecraft.server.v1_11_R1.ItemStack.setCount(ItemStack.java:822) ~[spigot.jar:git-Spigot-625bc00-d09304e]
            at net.minecraft.server.v1_11_R1.PlayerInteractManager.a(PlayerInteractManager.java:403) ~[spigot.jar:git-Spigot-625bc00-d09304e]
            at net.minecraft.server.v1_11_R1.PlayerConnection.a(PlayerConnection.java:965) ~[spigot.jar:git-Spigot-625bc00-d09304e]
            at net.minecraft.server.v1_11_R1.PacketPlayInBlockPlace.a(PacketPlayInBlockPlace.java:26) ~[spigot.jar:git-Spigot-625bc00-d09304e]
            at net.minecraft.server.v1_11_R1.PacketPlayInBlockPlace.a(PacketPlayInBlockPlace.java:1) ~[spigot.jar:git-Spigot-625bc00-d09304e]
            at net.minecraft.server.v1_11_R1.PlayerConnectionUtils$1.run(SourceFile:13) ~[spigot.jar:git-Spigot-625bc00-d09304e]
            at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) ~[?:1.8.0_111]
            at java.util.concurrent.FutureTask.run(Unknown Source) ~[?:1.8.0_111]
            at net.minecraft.server.v1_11_R1.SystemUtils.a(SourceFile:46) ~[spigot.jar:git-Spigot-625bc00-d09304e]
            ... 5 more
    Code (Text):
    package com.exoticcube.infectedwar.Events;

    import org.bukkit.Material;
    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.player.PlayerInteractEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.PlayerInventory;

    import com.exoticcube.infectedwar.Main;

    public class BagBackEvent implements Listener {

        @SuppressWarnings("unused")
        private Main core;

        public BagBackEvent(Main pl) {
            core = pl;
        }

        @SuppressWarnings("deprecation")
        @EventHandler
        public void onPlayerRightClick(PlayerInteractEvent e) {
            Action action = e.getAction();

            Player p = e.getPlayer();
            PlayerInventory inv = p.getInventory();

            ItemStack handItem = p.getItemInHand();
            ItemStack chestplateItem = inv.getChestplate();
            Material handItemM = p.getItemInHand().getType();

            Material bsb = Material.BLACK_SHULKER_BOX;
            Material blsb = Material.BLUE_SHULKER_BOX;
            Material brsb = Material.BROWN_SHULKER_BOX;
            Material csb = Material.CYAN_SHULKER_BOX;
            Material gsb = Material.GRAY_SHULKER_BOX;
            Material grsb = Material.GREEN_SHULKER_BOX;
            Material lblsb = Material.LIGHT_BLUE_SHULKER_BOX;
            Material lsb = Material.LIME_SHULKER_BOX;
            Material msb = Material.MAGENTA_SHULKER_BOX;
            Material osb = Material.ORANGE_SHULKER_BOX;
            Material psb = Material.PINK_SHULKER_BOX;
            Material pusb = Material.PURPLE_SHULKER_BOX;
            Material rsb = Material.RED_SHULKER_BOX;
            Material ssb = Material.SILVER_SHULKER_BOX;
            Material wsb = Material.WHITE_SHULKER_BOX;
            Material ysb = Material.YELLOW_SHULKER_BOX;

            if (action == Action.RIGHT_CLICK_BLOCK || action == Action.RIGHT_CLICK_AIR) {
                if (handItemM == bsb || handItemM == blsb || handItemM == brsb || handItemM == csb || handItemM == gsb
                        || handItemM == grsb || handItemM == lblsb || handItemM == lsb || handItemM == msb
                        || handItemM == osb || handItemM == psb || handItemM == pusb || handItemM == rsb || handItemM == ssb
                        || handItemM == wsb || handItemM == ysb) {
                    p.setItemInHand(chestplateItem);
                    inv.setChestplate(handItem);
                }
            }
        }
    }
     
     
  2. FrostedSnowman

    Resource Staff

    chestplate might be null / air. check it
     
  3. Just a side note, instead of checking all of the Materials, just do this:
    Code (Java):
    if(!handItemM.toString().contains("SHULKER_BOX")) return;
    This is a lot less messy and doesn't require you to define all those Materials.
     
    #3 xMrPoi, Apr 13, 2017
    Last edited: Apr 13, 2017
    • Like Like x 1
    • Informative Informative x 1
  4. Replace: p.setItemInHand(chestplateItem); to p.setItemInMainHand(chestplateItem);
     
  5. So I have returned back with bad news. Sadly once you right click it don't work. I appreciate all of your help guys, here is the code:
    Code (Java):
    public class BagBackListener implements Listener {

        @SuppressWarnings("unused")
        private Main core;

        public BagBackListener(Main pl) {
            core = pl;
        }

        @SuppressWarnings("deprecation")
        @EventHandler
        public void onPlayerBoxRightClick(PlayerInteractEvent e) {
            Action action = e.getAction();

            Player p = e.getPlayer();
            PlayerInventory inv = p.getInventory();

            ItemStack handItem = p.getItemInHand();
            ItemStack chestplateItem = inv.getChestplate();
            Material handItemM = p.getItemInHand().getType();

            if (action == Action.RIGHT_CLICK_BLOCK || action == Action.RIGHT_CLICK_AIR) {
                if (handItemM.toString().contains("SHULKER_BOX")) {
                    p.setItemInHand(chestplateItem);
                    inv.setChestplate(handItem);
                } else if (!handItemM.toString().contains("SHULKER_BOX")) {
                    return;
                }
            }
        }
    }
    Code (Text):
    [19:56:16 FATAL]: Error executing task
    java.util.concurrent.ExecutionException: java.lang.AssertionError: TRAP
            at java.util.concurrent.FutureTask.report(Unknown Source) ~[?:1.8.0_111]
            at java.util.concurrent.FutureTask.get(Unknown Source) ~[?:1.8.0_111]
            at net.minecraft.server.v1_11_R1.SystemUtils.a(SourceFile:47) [spigot.jar:git-Spigot-625bc00-d09304e]
            at net.minecraft.server.v1_11_R1.MinecraftServer.D(MinecraftServer.java:747) [spigot.jar:git-Spigot-625bc00-d09304e]
            at net.minecraft.server.v1_11_R1.DedicatedServer.D(DedicatedServer.java:399) [spigot.jar:git-Spigot-625bc00-d09304e]
            at net.minecraft.server.v1_11_R1.MinecraftServer.C(MinecraftServer.java:678) [spigot.jar:git-Spigot-625bc00-d09304e]
            at net.minecraft.server.v1_11_R1.MinecraftServer.run(MinecraftServer.java:576) [spigot.jar:git-Spigot-625bc00-d09304e]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_111]
    Caused by: java.lang.AssertionError: TRAP
            at net.minecraft.server.v1_11_R1.ItemStack.F(ItemStack.java:94) ~[spigot.jar:git-Spigot-625bc00-d09304e]
            at net.minecraft.server.v1_11_R1.ItemStack.setCount(ItemStack.java:822) ~[spigot.jar:git-Spigot-625bc00-d09304e]
            at net.minecraft.server.v1_11_R1.PlayerInteractManager.a(PlayerInteractManager.java:403) ~[spigot.jar:git-Spigot-625bc00-d09304e]
            at net.minecraft.server.v1_11_R1.PlayerConnection.a(PlayerConnection.java:965) ~[spigot.jar:git-Spigot-625bc00-d09304e]
            at net.minecraft.server.v1_11_R1.PacketPlayInBlockPlace.a(PacketPlayInBlockPlace.java:26) ~[spigot.jar:git-Spigot-625bc00-d09304e]
            at net.minecraft.server.v1_11_R1.PacketPlayInBlockPlace.a(PacketPlayInBlockPlace.java:1) ~[spigot.jar:git-Spigot-625bc00-d09304e]
            at net.minecraft.server.v1_11_R1.PlayerConnectionUtils$1.run(SourceFile:13) ~[spigot.jar:git-Spigot-625bc00-d09304e]
            at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) ~[?:1.8.0_111]
            at java.util.concurrent.FutureTask.run(Unknown Source) ~[?:1.8.0_111]
            at net.minecraft.server.v1_11_R1.SystemUtils.a(SourceFile:46) ~[spigot.jar:git-Spigot-625bc00-d09304e]
            ... 5 more
     
    #5 LuckyLuckiest, Apr 20, 2017
    Last edited: Apr 20, 2017
  6. Alright I found that the problem is in
    Code (Java):
    action == Action.RIGHT_CLICK_AIR
    And not in
    Code (Java):
    action == Action.RIGHT_CLICK_BLOCK
    Because this (Action.RIGHT_CLICK_BLOCK) don't have any problem
    How could I solve it?
     
    #6 LuckyLuckiest, Apr 20, 2017
    Last edited: Apr 20, 2017
  7. Thanks for all of your help guys!
    I fixed it. If there is something that could make the code a bit less messy will be appreciated!
    Code (Java):
    public class BagBackListener implements Listener {

        @SuppressWarnings("unused")
        private Main core;

        public BagBackListener(Main pl) {
            core = pl;
        }

        @SuppressWarnings("deprecation")
        @EventHandler
        public void onPlayerBoxRightClick(PlayerInteractEvent e) {
            Action action = e.getAction();

            Player p = e.getPlayer();
            PlayerInventory inv = p.getInventory();

            ItemStack air = new ItemStack(Material.AIR);
            ItemStack handItem = p.getItemInHand();
            ItemStack chestplateItem = inv.getChestplate();
         
            Material handItemM = p.getItemInHand().getType();
         
            boolean item = handItemM.toString().contains("SHULKER_BOX");
         
            if (p.getGameMode() == GameMode.CREATIVE) {
                p.sendMessage(ChatUtil.error("&7You need to be in &8GameMode &7Survival!"));
                return;
            } else if (chestplateItem == air) {
                if (action == Action.RIGHT_CLICK_BLOCK || action == Action.RIGHT_CLICK_AIR) {
                    if (item) {
                        inv.setChestplate(handItem);
                    } else if (!item) {
                        return;
                    }
                }
            } else if (chestplateItem != air) {
                if (action == Action.RIGHT_CLICK_BLOCK || action == Action.RIGHT_CLICK_AIR) {
                    if (item) {
                        p.setItemInHand(chestplateItem);
                        inv.setChestplate(handItem);
                    } else if (!item) {
                        return;
                    }
                }
            }
        }
    }
     
  8. Sure there is. Since it already works, I'll ever so quickly clean it up a bit.
    This'll give the exact same result:
    Code (Java):
    @EventHandler
    public void onPlayerInteract(final PlayerInteractEvent event) {
        if (event.getAction() != Action.RIGHT_CLICK_BLOCK && event.getAction() != Action.RIGHT_CLICK_AIR)
            return;
        final Player player;
        if ((player = event.getPlayer()).getGameMode() == GameMode.CREATIVE) {
            player.sendMessage("\u00a7cError: \u00a77You need to be in\u00a78GameMode \u00a77Survival!");
            return;
        }
        final PlayerInventory inventory;
        if (!(inventory = player.getInventory()).getItemInMainHand().getType().name().contains("BOX")) {
            return;
        }
        final ItemStack chestplate = inventory.getChestplate();
        inventory.setChestplate(inventory.getItemInMainHand());
        inventory.setItemInMainHand(chestplate);
    }
    and yes, yes I'm sorry for not using the ChatColor enum even though I should. I'm aware it may break in the future.