Solved Creating Duplicates In InventoryClickEvent

Discussion in 'Plugin Development' started by CodePlaysMinecraft, Feb 1, 2015.

Thread Status:
Not open for further replies.
  1. Hi. I've recently found a glitch in my plugin and I'm not sure how to fix it. When I double click on an item (the GUI), I found out that I can move it, instead of the event cancelling its self. Then, it duplicates its self and updates the inventory. http://gyazo.com/3b9bf1229c55aa2851998000f3f1d293
    Code:
    Code:
    @SuppressWarnings("deprecation")
        @EventHandler
        public void onInventoryClick(InventoryClickEvent event) {
            Player player = (Player) event.getWhoClicked();
    
            if (event.getCurrentItem() == null
                    || !event.getCurrentItem().hasItemMeta()
                    || event.getCurrentItem().getType().equals(Material.AIR)
                    || player.hasPermission("therealm.inventory.move")) {
                event.setCancelled(false);
                return;
            } else {
                event.setCancelled(true);
                player.updateInventory();
            }
    Any help is appreciated! :)

    EDIT: Sometimes on duplication, I get this error:
    Code:
    [11:34:02] [Server thread/WARN]: Failed to handle packet for /[CENSORED]
    java.lang.IndexOutOfBoundsException: Index: 75, Size: 45
        at java.util.ArrayList.rangeCheck(Unknown Source) ~[?:1.8.0_31]
        at java.util.ArrayList.get(Unknown Source) ~[?:1.8.0_31]
        at net.minecraft.server.v1_7_R3.Container.getSlot(Container.java:104) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-66-g43d8943-b3078jnks]
        at net.minecraft.server.v1_7_R3.PlayerConnection.a(PlayerConnection.java:1406) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-66-g43d8943-b3078jnks]
        at net.minecraft.server.v1_7_R3.PacketPlayInWindowClick.a(SourceFile:32) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-66-g43d8943-b3078jnks]
        at net.minecraft.server.v1_7_R3.PacketPlayInWindowClick.handle(SourceFile:10) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-66-g43d8943-b3078jnks]
        at net.minecraft.server.v1_7_R3.NetworkManager.a(NetworkManager.java:157) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-66-g43d8943-b3078jnks]
        at net.minecraft.server.v1_7_R3.ServerConnection.c(SourceFile:134) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-66-g43d8943-b3078jnks]
        at net.minecraft.server.v1_7_R3.MinecraftServer.v(MinecraftServer.java:667) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-66-g43d8943-b3078jnks]
        at net.minecraft.server.v1_7_R3.DedicatedServer.v(DedicatedServer.java:260) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-66-g43d8943-b3078jnks]
        at net.minecraft.server.v1_7_R3.MinecraftServer.u(MinecraftServer.java:558) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-66-g43d8943-b3078jnks]
        at net.minecraft.server.v1_7_R3.MinecraftServer.run(MinecraftServer.java:469) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-66-g43d8943-b3078jnks]
        at net.minecraft.server.v1_7_R3.ThreadServerApplication.run(SourceFile:628) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-66-g43d8943-b3078jnks]
    [11:34:02] [Server thread/INFO]: CodePlaysMC lost connection: Internal server error
     
    Last edited: Feb 1, 2015
  2. Offline

    PreFiXAUT

    @CodePlaysMinecraft
    Hm, the IndexOutOfBoundsException is weird. Is this the full code or is there a setter somewhere below? You should also debug the Code (Check who clicked, how and what he clicked, and which slot it was).

    And is is suposed to block any/nearly all accesses to all Inventorys? If not, be sure to check if it's a custom Inventory first (Track them when you force a Player to open them)
     
  3. @PreFiXAUT Here is the full class (the parts I didn't show you was for the GUI):
    Code:
    package me.CodePlaysMC.TheRealmCore.Inventory;
    
    import me.CodePlaysMC.TheRealmCore.PluginListener;
    import me.CodePlaysMC.TheRealmCore.TheRealmCore;
    import me.CodePlaysMC.TheRealmCore.Utilities.ChatUtilities;
    
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.Material;
    import org.bukkit.Sound;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.inventory.InventoryClickEvent;
    import org.bukkit.inventory.ItemStack;
    
    public class InventoryClick extends PluginListener {
    
        public InventoryClick(TheRealmCore pl) {
            super(pl);
        }
    
        @SuppressWarnings("deprecation")
        @EventHandler
        public void onInventoryClick(InventoryClickEvent event) {
            Player player = (Player) event.getWhoClicked();
    
            if (event.getCurrentItem() == null
                    || !event.getCurrentItem().hasItemMeta()
                    || event.getCurrentItem().getType().equals(Material.AIR)
                    || player.hasPermission("therealm.inventory.move")) {
                event.setCancelled(false);
                return;
            } else {
                event.setCancelled(true);
                player.updateInventory();
            }
    
            if (!ChatColor.stripColor(event.getInventory().getName())
                    .equalsIgnoreCase("THE REALM » Where to?"))
                return;
    
            event.setCancelled(true);
    
            Location location = player.getLocation();
    
            if (event.getCurrentItem() == null
                    || event.getCurrentItem().getType().equals(Material.AIR)
                    || !event.getCurrentItem().hasItemMeta()) {
                player.closeInventory();
                return;
            }
    
            switch (event.getCurrentItem().getType()) {
            case STONE:
                player.closeInventory();
                ChatUtilities.sendMessage(player, "Yay! You clicked on me. :)");
                ChatUtilities.sendMessage(player, "Here's something special.");
                player.getInventory().addItem(new ItemStack(Material.APPLE));
                player.playSound(location, Sound.LEVEL_UP, 1, 0);
                break;
            case STONE_SWORD:
                player.closeInventory();
                ChatUtilities.sendMessage(player,
                        "Congrats on clicking me, I guess?");
                player.playSound(location, Sound.LEVEL_UP, 1, 0);
                break;
            default:
                player.closeInventory();
                break;
            }
        }
    }
    The event is supposed to keep players from moving items around in their inventory.
     
  4. Offline

    PreFiXAUT

    @CodePlaysMinecraft Oh...I should have read the Stacktrace more clearly. Apparently you're trying to get something out of an ArrayList which is out of bounds like when the ArrayList's size is 10, and you try to access the 20th item in the List, it will obviously won't work.

    The thing is, it doesn't descirbe where its caused...is this maybe just a snap of the Stacktrace? If so post please the entire Stacktrace, because I can't find any List in this class at all (maybe im just stupid xD)
     
  5. @PreFiXAUT The error has seemed to stop, but the duplication glitch is still here. Also, the only ArrayList (or any type of list) I use is the lores for the items in the GUI. (Code below.^-^)
    Code:
    ArrayList<String> lore2 = new ArrayList<String>();
            itemMeta2.setDisplayName(ChatColor.GREEN + "Other Random Item");
            lore2.add(ChatColor.WHITE + "I'm some other random item. :)");
            itemMeta2.setLore(lore2);
            item2.setItemMeta(itemMeta2);
     
  6. Offline

    WesJD

  7. @PreFiXAUT Is there any other event that prevents a player from moving items and/or adding items to a players inventory in both survival and creative? (I already have a PlayerItemPickupEvent).
     
  8. Offline

    1Rogue

    Code:java
    1. event.setCancelled(false)


    Never do this, just return out of the method. Additionally, you cancel the event every time:

    Code:java
    1. if (!ChatColor.stripColor(event.getInventory().getName())
    2. .equalsIgnoreCase("THE REALM » Where to?"))
    3. return;
    4.  
    5. event.setCancelled(true);
     
    CodePlaysMinecraft likes this.
  9. @1Rogue Thanks! That single line of code was messing the whole event up. Thanks again!
     
Thread Status:
Not open for further replies.

Share This Page