Solved Help with a Inventory Saver sytem

Discussion in 'Plugin Development' started by HimahoYT, Jun 14, 2018.

Thread Status:
Not open for further replies.
  1. Hi, I have a parkor plugin, and I need a inventory saver system, but work in all versions > 1.8 (1.8, 1.9, 1.10.x, 1.11.x and 1.12.x) I have this code works perfectly in 1.8 but in > 1.9 have errors.

    This is the class code:
    Code:
    package me.himahoyt.aparkour.Utils;
    
    import java.util.HashMap;
    import java.util.Map;
    
    import org.bukkit.entity.Player;
    import org.bukkit.inventory.ItemStack;
    
    public class InventorySaver
    {
      public static Map<String, ItemStack[]> inventory = new HashMap<String, ItemStack[]>();
      public static Map<String, ItemStack[]> armour = new HashMap<String, ItemStack[]>();
     
      public static void restorePlayerInventory(Player paramPlayer)
      {
        String str = paramPlayer.getName();
       
        paramPlayer.getInventory().clear();
        paramPlayer.getInventory().setArmorContents(null);
       
        paramPlayer.getInventory().setContents((ItemStack[])inventory.get(str));
        paramPlayer.getInventory().setArmorContents((ItemStack[])armour.get(str));
        paramPlayer.setHealth(20.0D);
        paramPlayer.updateInventory();
      }
     
      public static void savePlayerInventory(Player paramPlayer)
      {
        String str1 = paramPlayer.getName();
       
        ItemStack[] arrayOfItemStack1 = paramPlayer.getInventory().getContents();
        ItemStack[] arrayOfItemStack2 = paramPlayer.getInventory().getArmorContents();
       
        inventory.put(str1, arrayOfItemStack1);
        armour.put(str1, arrayOfItemStack2);
      }
     
      public static void clearPlayerInventory(Player paramPlayer)
      {
        paramPlayer.getInventory().clear();
        paramPlayer.getInventory().setArmorContents(null);
        paramPlayer.updateInventory();
      }
    }
    
    This is the error:
    Code:
    [13:42:11 ERROR]: Could not pass event PlayerInteractEvent to AParkour v3.0.0
    org.bukkit.event.EventException
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:310) ~[spigot.jar:git-Spigot-7d15d07-c194444]
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[spigot.jar:git-Spigot-7d15d07-c194444]
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:502) [spigot.jar:git-Spigot-7d15d07-c194444]
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:487) [spigot.jar:git-Spigot-7d15d07-c194444]
            at org.bukkit.craftbukkit.v1_9_R1.event.CraftEventFactory.callPlayerInteractEvent(CraftEventFactory.java:212) [spigot.jar:git-Spigot-7d15d07-c194444]
            at org.bukkit.craftbukkit.v1_9_R1.event.CraftEventFactory.callPlayerInteractEvent(CraftEventFactory.java:179) [spigot.jar:git-Spigot-7d15d07-c194444]
            at org.bukkit.craftbukkit.v1_9_R1.event.CraftEventFactory.callPlayerInteractEvent(CraftEventFactory.java:175) [spigot.jar:git-Spigot-7d15d07-c194444]
            at net.minecraft.server.v1_9_R1.PlayerConnection.a(PlayerConnection.java:897) [spigot.jar:git-Spigot-7d15d07-c194444]
            at net.minecraft.server.v1_9_R1.PacketPlayInBlockPlace.a(PacketPlayInBlockPlace.java:27) [spigot.jar:git-Spigot-7d15d07-c194444]
            at net.minecraft.server.v1_9_R1.PacketPlayInBlockPlace.a(PacketPlayInBlockPlace.java:1) [spigot.jar:git-Spigot-7d15d07-c194444]
            at net.minecraft.server.v1_9_R1.PlayerConnectionUtils$1.run(SourceFile:13) [spigot.jar:git-Spigot-7d15d07-c194444]
            at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_171]
            at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_171]
            at net.minecraft.server.v1_9_R1.SystemUtils.a(SourceFile:45) [spigot.jar:git-Spigot-7d15d07-c194444]
            at net.minecraft.server.v1_9_R1.MinecraftServer.D(MinecraftServer.java:716) [spigot.jar:git-Spigot-7d15d07-c194444]
            at net.minecraft.server.v1_9_R1.DedicatedServer.D(DedicatedServer.java:400) [spigot.jar:git-Spigot-7d15d07-c194444]
            at net.minecraft.server.v1_9_R1.MinecraftServer.C(MinecraftServer.java:655) [spigot.jar:git-Spigot-7d15d07-c194444]
            at net.minecraft.server.v1_9_R1.MinecraftServer.run(MinecraftServer.java:554) [spigot.jar:git-Spigot-7d15d07-c194444]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_171]
    Caused by: java.lang.IllegalArgumentException: Invalid inventory size; expected 36 or less
            at org.bukkit.craftbukkit.v1_9_R1.inventory.CraftInventory.setContents(CraftInventory.java:65) ~[spigot.jar:git-Spigot-7d15d07-c194444]
            at me.himahoyt.aparkour.Utils.InventorySaver.restorePlayerInventory(InventorySaver.java:21) ~[?:?]
            at me.himahoyt.aparkour.Events.event_Click.onClicker(event_Click.java:90) ~[?:?]
            at sun.reflect.GeneratedMethodAccessor58.invoke(Unknown Source) ~[?:?]
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_171]
            at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_171]
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:306) ~[spigot.jar:git-Spigot-7d15d07-c194444]
            ... 18 more
     
  2. Offline

    ThePandaPlayer

    Well, lets take a look at the error:

    The error tells us that it was caused by an IllegalArgumentException. The message is "Invalid Inventory size; expected 36 or less". Something is wrong with the inventory size.
    The code that caused the error can also be found.
    Code:
    at me.himahoyt.aparkour.Utils.InventorySaver.restorePlayerInventory(InventorySaver.java:21) ~[?:?]
    at me.himahoyt.aparkour.Events.event_Click.onClicker(event_Click.java:90) ~[?:?]
    
    It tells you the source file and then the line number.

    I apologize for such a long post, but I hope this helps!
     
  3. Yes, the error is this line. But I dont know how to solve the problem:
    Line error: "paramPlayer.getInventory().setContents((ItemStack[])inventory.get(str));"
     
  4. Offline

    timtower Administrator Administrator Moderator

    Check what the size is of the stored inventory.
     
  5. @timtower I checked and the size is 41. I think the code save the slots of intentory and the armor slots.
    How I can save only the 36 inventory slots?
     
  6. Offline

    timtower Administrator Administrator Moderator

    @HimahoYT Check first if it also takes the armor slots. Might also be the crafting slots.
     
  7. I dont know how to do that, sorry
     
  8. Offline

    timtower Administrator Administrator Moderator

    Put a piece of diamond armor in there, check the type of the item in a loop.
     
  9. I checked and the armor slots saves in the array
     
Thread Status:
Not open for further replies.

Share This Page