Infinite Potions Glitch

Discussion in 'Plugin Development' started by benzimmer123, Jun 5, 2015.

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

    benzimmer123

    Working on a Duels plugin and when the players leave the duel I try to restore their inventory (which it does), all the potions that player has thrown during the duel then become infinite. So say I join the duel with 3 potions and I use all of them and then restore their inventory all of them 3 potions can be infinitely thrown. Here is my code...

    Thanks in advance! :)

    Code:
        private static HashMap<String, ItemStack[]> armourContents = new HashMap<String, ItemStack[]>();
        private static HashMap<String, ItemStack[]> inventoryContents = new HashMap<String, ItemStack[]>();
        private static HashMap<String, Location> locations = new HashMap<String, Location>();
        private static HashMap<String, Integer> xplevel = new HashMap<String, Integer>();
    
        public void saveInventory(Player player) {
            armourContents.put(player.getName(), player.getInventory().getArmorContents());
            inventoryContents.put(player.getName(), player.getInventory().getContents());
            locations.put(player.getName(), player.getLocation());
            xplevel.put(player.getName(), player.getLevel());
        }
    
        @SuppressWarnings("deprecation")
        public void restoreInventory(Player player) {
            if (plugin.settings.getConfig().getBoolean("SAVE_INVENTORIES")) {
                player.getInventory().clear();
    
                if (inventoryContents.get(player.getName()) != null) {
                    player.getInventory().setContents(inventoryContents.get(player.getName()));
                }
    
                if (armourContents.get(player.getName()) != null) {
                    player.getInventory().setArmorContents(armourContents.get(player.getName()));
                }
    
                if (xplevel.get(player.getName()) != null) {
                    player.setLevel(xplevel.get(player.getName()));
                }
            }
    
            if (plugin.settings.getConfig().getBoolean("SAVE_LOCATIONS")) {
                if (locations.get(player.getName()) != null) {
                    player.teleport(locations.get(player.getName()));
                }
            }
    
            xplevel.remove(player.getName());
            locations.remove(player.getName());
            armourContents.remove(player.getName());
            inventoryContents.remove(player.getName());
    
            player.updateInventory();
        }
     
  2. Offline

    Zombie_Striker

    @benzimmer123
    Since these are just methods that are not ever used, I do not know what you're problem is. Please post all your code.
     
  3. Offline

    benzimmer123

    This is a big project, I'm not going to post all my code as this is all you really need to see, I'm guessing.

    EDIT: This is where I'm actually using the methods...

    Code:
        @EventHandler
        public void PlayerRespawn(PlayerRespawnEvent e) {
            if (temp.contains(e.getPlayer())) {
                new Inventories(plugin).restoreInventory(e.getPlayer());
            }
        }
     
    Last edited: Jun 5, 2015
  4. Offline

    Zombie_Striker

    @benzimmer123
    You should not be doing that. That is creating a new instance every time a player dies. Just create one instance and call that method instead of creating dozens of instances for each player (if 100 players die 10 times, that's 1,000 instancies)

    As for your actual problem, Try looping through the armour contents and adding them individually; that may be your problem.
     
  5. Offline

    benzimmer123

    Alright thanks, I'll give it a go! :)
     
  6. Offline

    benzimmer123

    @Zombie_Striker
    What's wrong with this code?
    Code:
    public class Inventories {
    
        MainClass plugin;
    
        public Inventories(MainClass instance) {
            plugin = instance;
        }
    
        private static HashMap<String, ItemStack[]> armourContents = new HashMap<String, ItemStack[]>();
        private static HashMap<String, ItemStack[]> inventoryContents = new HashMap<String, ItemStack[]>();
        private static HashMap<String, Location> locations = new HashMap<String, Location>();
        private static HashMap<String, Integer> xplevel = new HashMap<String, Integer>();
    
        public void saveInventory(Player player) {
            armourContents.put(player.getName(), player.getInventory().getArmorContents());
            inventoryContents.put(player.getName(), player.getInventory().getContents());
            locations.put(player.getName(), player.getLocation());
            xplevel.put(player.getName(), player.getLevel());
        }
    
        @SuppressWarnings("deprecation")
        public void restoreInventory(Player player) {
            if (plugin.settings.getConfig().getBoolean("SAVE_INVENTORIES")) {
                player.getInventory().clear();
    
                if (inventoryContents.get(player.getName()) != null) {
                    for (Entry<String, ItemStack[]> s : inventoryContents.entrySet()) {
                        player.getInventory().addItem(s.getValue());
                    }
                }
    
                if (armourContents.get(player.getName()) != null) {
                    player.getInventory().setArmorContents(armourContents.get(player.getName()));
                }
    
                if (xplevel.get(player.getName()) != null) {
                    player.setLevel(xplevel.get(player.getName()));
                }
            }
    
            if (plugin.settings.getConfig().getBoolean("SAVE_LOCATIONS")) {
                if (locations.get(player.getName()) != null) {
                    player.teleport(locations.get(player.getName()));
                }
            }
    
            xplevel.remove(player.getName());
            locations.remove(player.getName());
            armourContents.remove(player.getName());
            inventoryContents.remove(player.getName());
    
            player.updateInventory();
        }
    
    }
    
    It Gives this error

    Code:
    [23:55:03 ERROR]: Could not pass event PlayerDeathEvent to Duels v1.0
    org.bukkit.event.EventException
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:294) ~[bukkit.jar:git-Bukkit-1.7.9-R0.2-24-g07d4558-b3116jnks]
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.jav
    a:62) ~[bukkit.jar:git-Bukkit-1.7.9-R0.2-24-g07d4558-b3116jnks]
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.j
    ava:501) [bukkit.jar:git-Bukkit-1.7.9-R0.2-24-g07d4558-b3116jnks]
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.j
    ava:486) [bukkit.jar:git-Bukkit-1.7.9-R0.2-24-g07d4558-b3116jnks]
            at org.bukkit.craftbukkit.v1_7_R4.event.CraftEventFactory.callPlayerDeat
    hEvent(CraftEventFactory.java:380) [bukkit.jar:git-Bukkit-1.7.9-R0.2-24-g07d4558
    -b3116jnks]
            at net.minecraft.server.v1_7_R4.EntityPlayer.die(EntityPlayer.java:369)
    [bukkit.jar:git-Bukkit-1.7.9-R0.2-24-g07d4558-b3116jnks]
            at net.minecraft.server.v1_7_R4.EntityLiving.damageEntity(EntityLiving.j
    ava:736) [bukkit.jar:git-Bukkit-1.7.9-R0.2-24-g07d4558-b3116jnks]
            at net.minecraft.server.v1_7_R4.EntityHuman.damageEntity(EntityHuman.jav
    a:758) [bukkit.jar:git-Bukkit-1.7.9-R0.2-24-g07d4558-b3116jnks]
            at net.minecraft.server.v1_7_R4.EntityPlayer.damageEntity(EntityPlayer.j
    ava:448) [bukkit.jar:git-Bukkit-1.7.9-R0.2-24-g07d4558-b3116jnks]
            at net.minecraft.server.v1_7_R4.EntityHuman.attack(EntityHuman.java:943)
    [bukkit.jar:git-Bukkit-1.7.9-R0.2-24-g07d4558-b3116jnks]
            at net.minecraft.server.v1_7_R4.PlayerConnection.a(PlayerConnection.java
    :1107) [bukkit.jar:git-Bukkit-1.7.9-R0.2-24-g07d4558-b3116jnks]
            at net.minecraft.server.v1_7_R4.PacketPlayInUseEntity.a(SourceFile:55) [
    bukkit.jar:git-Bukkit-1.7.9-R0.2-24-g07d4558-b3116jnks]
            at net.minecraft.server.v1_7_R4.PacketPlayInUseEntity.handle(SourceFile:
    10) [bukkit.jar:git-Bukkit-1.7.9-R0.2-24-g07d4558-b3116jnks]
            at net.minecraft.server.v1_7_R4.NetworkManager.a(NetworkManager.java:157
    ) [bukkit.jar:git-Bukkit-1.7.9-R0.2-24-g07d4558-b3116jnks]
            at net.minecraft.server.v1_7_R4.ServerConnection.c(SourceFile:134) [bukk
    it.jar:git-Bukkit-1.7.9-R0.2-24-g07d4558-b3116jnks]
            at net.minecraft.server.v1_7_R4.MinecraftServer.v(MinecraftServer.java:6
    67) [bukkit.jar:git-Bukkit-1.7.9-R0.2-24-g07d4558-b3116jnks]
            at net.minecraft.server.v1_7_R4.DedicatedServer.v(DedicatedServer.java:2
    58) [bukkit.jar:git-Bukkit-1.7.9-R0.2-24-g07d4558-b3116jnks]
            at net.minecraft.server.v1_7_R4.MinecraftServer.u(MinecraftServer.java:5
    58) [bukkit.jar:git-Bukkit-1.7.9-R0.2-24-g07d4558-b3116jnks]
            at net.minecraft.server.v1_7_R4.MinecraftServer.run(MinecraftServer.java
    :469) [bukkit.jar:git-Bukkit-1.7.9-R0.2-24-g07d4558-b3116jnks]
            at net.minecraft.server.v1_7_R4.ThreadServerApplication.run(SourceFile:6
    28) [bukkit.jar:git-Bukkit-1.7.9-R0.2-24-g07d4558-b3116jnks]
    Caused by: java.lang.IllegalArgumentException: Item cannot be null
            at org.apache.commons.lang.Validate.noNullElements(Validate.java:410) ~[
    bukkit.jar:git-Bukkit-1.7.9-R0.2-24-g07d4558-b3116jnks]
            at org.bukkit.craftbukkit.v1_7_R4.inventory.CraftInventory.addItem(Craft
    Inventory.java:268) ~[bukkit.jar:git-Bukkit-1.7.9-R0.2-24-g07d4558-b3116jnks]
            at minecraft.duels.other.Inventories.restoreInventory(Inventories.java:3
    9) ~[?:?]
            at minecraft.duels.other.Duel.PlayerDeath(Duel.java:164) ~[?:?]
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0
    _25]
            at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0
    _25]
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1
    .8.0_25]
            at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_25]
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:292) ~[bukkit.jar:git-Bukkit-1.7.9-R0.2-24-g07d4558-b3116jnks]
            ... 19 more
     
  7. Offline

    Zombie_Striker

  8. Offline

    benzimmer123

    @Zombie_Striker
    Yes the error is this line, which leads to the class above :p
    Code:
    new Inventories(plugin).restoreInventory(player);
    EDIT: Also this
    Line 39
    Code:
    player.getInventory().addItem(s.getValue());
    Just attempted this code but still creates glitched potions...

    Code:
                player.getInventory().clear();
    
                int num = 0;
    
                if (inventoryContents.get(player.getName()) != null) {
                    for (ItemStack i : inventoryContents.get(player.getName())) {
                        if (i != null) {
                            player.getInventory().setItem(num, i);
    
                            num++;
                        }
                    }
                }
    Code:
    inventoryContents.put(player.getName(), player.getInventory().getContents());
    Code:
    private static HashMap<String, ItemStack[]> inventoryContents = new HashMap<String, ItemStack[]>();
     
    Last edited by a moderator: Jun 11, 2015
  9. Offline

    Zombie_Striker

    @benzimmer123
    All you needed to do was check if 's' was null and if 's.getValue()' was null.
     
  10. Offline

    benzimmer123

    Like this? If so it still prints the same error

    Code:
    if (s != null && s.getValue() != null)
     
  11. Offline

    Zombie_Striker

    @benzimmer123
    I'm an idoit because I forgot to read my own post.
     
  12. Offline

    benzimmer123

    Can you spot anything wrong with either of the codes then?
     
  13. Offline

    Zombie_Striker

    @benzimmer123
    Please post all your classes. I am looking for specifically Duel.Class, but your other classes might also be the problem.
     
  14. Offline

    benzimmer123

    Guessing it's something wrong with this code

    Code:
            if (in1v1.contains(e.getEntity())) {
                in1v1.remove(e.getEntity());
    
                if (plugin.settings.getConfig().getString("1v1." + e.getEntity().getName()) != null) {
                    String pvpplayer = plugin.settings.getConfig().getString("1v1." + e.getEntity().getName());
                    Player player = Bukkit.getPlayerExact(pvpplayer);
                    if (player != null) {
                        in1v1.remove(player);
                        duels.remove(player);
                        Damageable dplayer = (Damageable) player;
                        double health = dplayer.getHealth();
                        health = health / 2;
                        health = Math.round(health);
                        player.getInventory().clear();
                        player.getInventory().setArmorContents(null);
                        player.setHealth(20);
                        player.teleport(player.getWorld().getSpawnLocation());
                        for (PotionEffect effect : player.getActivePotionEffects())
                            player.removePotionEffect(effect.getType());
                        plugin.settings.getConfig().set("1v1." + player.getName(), null);
                        arenaused.remove(player.getName());
    
                        if (!plugin.settings.getConfig().getBoolean("SAVE_LOCATIONS")) {
                            player.teleport(player.getWorld().getSpawnLocation());
                        }
    
                        new Inventories(plugin).restoreInventory(player);
                    }
                }
    
     
  15. Offline

    Zombie_Striker

  16. Offline

    benzimmer123

    Code:
    new Inventories(plugin).restoreInventory(player);
     
  17. Offline

    benzimmer123

  18.  
  19. Offline

    benzimmer123

    @MaTaMoR_
    Already fixed and changed, doesn't fix anything.
     
  20. post your updated code, and the line of the error.
     
  21. Offline

    benzimmer123

    Okay, basically I have found out that the code is working however it doesn't add the items that weren't there since the duel had ended. For example if I splashed my potions then threw my sword those items wouldn't be added to my inventory after. My new code is...

    Inventory Class (open)
    Code:
        MainClass plugin;
    
        public Inventories(MainClass instance) {
            plugin = instance;
        }
    
        HashMap<String, ItemStack[]> armourContents = MainClass.armourContents;
        HashMap<String, ItemStack[]> inventoryContents = MainClass.inventoryContents;
        HashMap<String, Location> locations = MainClass.locations;
        HashMap<String, Integer> xplevel = MainClass.xplevel;
        ArrayList<ItemStack> items = new ArrayList<ItemStack>();
    
        public void saveInventory(Player player) {
            armourContents.put(player.getName(), player.getInventory().getArmorContents());
            inventoryContents.put(player.getName(), player.getInventory().getContents());
            locations.put(player.getName(), player.getLocation());
            xplevel.put(player.getName(), player.getLevel());
        }
    
        @SuppressWarnings("deprecation")
        public void restoreInventory(final Player player) {
            if (plugin.settings.getConfig().getBoolean("SAVE_INVENTORIES")) {
                player.getInventory().clear();
    
                Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
                    public void run() {
                        if (inventoryContents.get(player.getName()) != null) {
                            for (ItemStack i : inventoryContents.get(player.getName())) {
                                player.getInventory().addItem(i);
                            }
                        }
    
                        inventoryContents.remove(player.getName());
                    }
                }, 5);
    
                if (armourContents.get(player.getName()) != null) {
                    player.getInventory().setArmorContents(armourContents.get(player.getName()));
                }
    
                if (xplevel.get(player.getName()) != null) {
                    player.setLevel(xplevel.get(player.getName()));
                }
            }
    
            if (plugin.settings.getConfig().getBoolean("SAVE_LOCATIONS")) {
                if (locations.get(player.getName()) != null) {
                    player.teleport(locations.get(player.getName()));
                }
            }
    
            xplevel.remove(player.getName());
            locations.remove(player.getName());
            armourContents.remove(player.getName());
    
            player.updateInventory();
        }


    DeathEvent (open)
    Code:
    @SuppressWarnings("deprecation")
        @EventHandler
        public void PlayerDeath(PlayerDeathEvent e) {
            if (e.getEntity().getType() != EntityType.PLAYER)
                return;
    
            if (e.getEntity().getKiller() != null) {
                if (e.getEntity().getKiller().getType() == EntityType.PLAYER) {
    
                    Player k = (Player) e.getEntity().getKiller();
                    Player p = (Player) e.getEntity();
    
                    if (in1v1.contains(k) && in1v1.contains(p)) {
                        Damageable dplayer = (Damageable) k;
                        double health = dplayer.getHealth();
                        health = health / 2;
                        health = Math.round(health);
                        p.sendMessage(new Messages(plugin).MATCH_LOST(k, health));
                        k.sendMessage(new Messages(plugin).MATCH_WON(p, health));
                    }
                }
            }
    
            if (in1v1.contains(e.getEntity())) {
                in1v1.remove(e.getEntity());
    
                if (plugin.settings.getConfig().getString("1v1." + e.getEntity().getName()) != null) {
                    String pvpplayer = plugin.settings.getConfig().getString("1v1." + e.getEntity().getName());
                    Player player = Bukkit.getPlayerExact(pvpplayer);
                    if (player != null) {
                        new Methods(plugin).endMatch(player);
                        arenaused.remove(player.getName());
    
                        if (!plugin.settings.getConfig().getBoolean("SAVE_LOCATIONS")) {
                            player.teleport(player.getWorld().getSpawnLocation());
                        }
    
                        new Inventories(plugin).restoreInventory(player);
                    }
                }
    
                if (!plugin.settings.getConfig().getBoolean("DROP_ITEMS")) {
                    e.getDrops().clear();
                }
    
                for (PotionEffect effect : e.getEntity().getActivePotionEffects())
                    e.getEntity().removePotionEffect(effect.getType());
    
                int arena;
    
                arena = arenaused.get(e.getEntity().getName());
    
                plugin.settings.getArenas().set("warps." + arena + ".used", false);
                plugin.settings.getConfig().set("1v1." + e.getEntity().getName(), null);
                plugin.settings.saveConfig();
                plugin.settings.saveArenas();
    
                arenaused.remove(e.getEntity().getName());
    
                if (!temp.contains(e.getEntity())) {
                    temp.add(e.getEntity());
                }
            }
        }


    Respawn Event (open)
    Code:
        @EventHandler
        public void PlayerRespawn(PlayerRespawnEvent e) {
            if (temp.contains(e.getPlayer())) {
                new Inventories(plugin).restoreInventory(e.getPlayer());
    
                temp.remove(e.getPlayer());
            }
        }
     
  22. You have to update the inventory stored
     
  23. Offline

    benzimmer123

    Am I not doing that?
     
  24. You still creating a new instance.
     
  25. Offline

    benzimmer123

    Added the methods to the same class and still had the same problem.
     
    Last edited: Jun 13, 2015
  26. Offline

    _Filip

    *sigh*
     
Thread Status:
Not open for further replies.

Share This Page