Problem with Inventory

Discussion in 'Spigot Plugin Development' started by Grauju, Jan 8, 2020.

  1. Hey,

    right now I'm trying to make an enderchest plugin with only some slots. So I use a chestinv which I want to save in the config.yml. Here is my code:

    Code (Java):
    @EventHandler
        public void onInventoryOpen(PlayerInteractEvent e){
            Player p = (Player) e.getPlayer();
            if(e.getAction() == Action.RIGHT_CLICK_AIR || e.getAction() == Action.RIGHT_CLICK_BLOCK) {
                if(e.getClickedBlock().getType().equals(Material.ENDER_CHEST)) {
                e.setCancelled(true);
                ItemStack disabled = new ItemStack(Material.RED_STAINED_GLASS_PANE);
                ItemMeta disabledm = disabled.getItemMeta();
                disabledm.setDisplayName("§cNo permissions");
                ArrayList<String> lore = new ArrayList<String>();
                lore.add("§7You don't have the permission to use this slot.");
                disabledm.setLore(lore);
                disabled.setItemMeta(disabledm);
               
                p.playSound(p.getLocation(), Sound.BLOCK_ENDER_CHEST_OPEN, 1, 1);
                Inventory inv = Bukkit.createInventory(null, 9*1, "§bEnderchest");
                   
               
                try {
                    p.openInventory(stringToInventory(Config.getConfig().getString(p.getUniqueId().toString() + ".Item1"), p, "§bEnderchest"));
                } catch (ClassNotFoundException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                } catch (IOException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
            }
            }
               
            }


    @EventHandler
        public void onChestClose(InventoryCloseEvent e) {
                Inventory inv = e.getInventory();
                Player p = (Player) e.getPlayer();
            if(p.getOpenInventory().getTitle().equals("§bEnderchest")){
                if(p.hasPermission("enderchest.use.1")) {
                    String itemstring = inventoryToString(inv,1);
                    Config.getConfig().set(p.getUniqueId().toString() + ".Item1", itemstring);
                    Config.saveConfig();
                    Config.reloadConfig();
                }
                       
                    }

    public static String inventoryToString(Inventory inventory, Integer Slots)  {
            try {
                 ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
                 BukkitObjectOutputStream dataOutput = new BukkitObjectOutputStream(outputStream);
                 // Write the size of the inventory
                 dataOutput.writeInt(Slots);
                 // Save every element in the list
                 for (int i = 0; i < Slots; i++) {
                    dataOutput.writeObject(inventory.getItem(i));
                 }
                 // Serialize that array
                 dataOutput.close();
                 return Base64Coder.encodeLines(outputStream.toByteArray());
              } catch (Exception e) {
                 throw new IllegalStateException("Unable to save item stacks.", e);
              }
           }
        public static Inventory stringToInventory(String data, Player p , String InventoryName) throws IOException, ClassNotFoundException {
                 ByteArrayInputStream inputStream = new ByteArrayInputStream(Base64Coder.decodeLines(data));
                 BukkitObjectInputStream dataInput = new BukkitObjectInputStream(inputStream);
                 Inventory inventory = Bukkit.getServer().createInventory(p, 9, InventoryName);
                 // Read the serialized inventory
                 for (int i = 0; i < inventory.getSize(); i++) {
                    inventory.setItem(i, (ItemStack) dataInput.readObject());
                 }
                 dataInput.close();
                 return inventory;
           }
           
     
  2. What's the problem exactly? Does something in your code throw an error? If so post the error.
     
  3. Yes, when I try to open the chest

    Code (Java):
    [09:08:39] [Server thread/WARN]: java.io.OptionalDataException

    [09:08:39] [Server thread/WARN]:     at java.base/java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1606)

    [09:08:39] [Server thread/WARN]:     at java.base/java.io.ObjectInputStream.readObject(ObjectInputStream.java:430)

    [09:08:39] [Server thread/WARN]:     at de.JulsGra.EnderChestSlots.Listener.PlayerInteractListener.stringToInventory(PlayerInteractListener.java:188)

    [09:08:39] [Server thread/WARN]:     at de.JulsGra.EnderChestSlots.Listener.PlayerInteractListener.onInventoryOpen(PlayerInteractListener.java:111)

    [09:08:39] [Server thread/WARN]:     at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

    [09:08:39] [Server thread/WARN]:     at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)

    [09:08:39] [Server thread/WARN]:     at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

    [09:08:39] [Server thread/WARN]:     at java.base/java.lang.reflect.Method.invoke(Method.java:567)

    [09:08:39] [Server thread/WARN]:     at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:316)

    [09:08:39] [Server thread/WARN]:     at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:70)

    [09:08:39] [Server thread/WARN]:     at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:529)

    [09:08:39] [Server thread/WARN]:     at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:514)

    [09:08:39] [Server thread/WARN]:     at org.bukkit.craftbukkit.v1_15_R1.event.CraftEventFactory.callPlayerInteractEvent(CraftEventFactory.java:437)

    [09:08:39] [Server thread/WARN]:     at net.minecraft.server.v1_15_R1.PlayerInteractManager.a(PlayerInteractManager.java:441)

    [09:08:39] [Server thread/WARN]:     at net.minecraft.server.v1_15_R1.PlayerConnection.a(PlayerConnection.java:1250)

    [09:08:39] [Server thread/WARN]:     at net.minecraft.server.v1_15_R1.PacketPlayInUseItem.a(PacketPlayInUseItem.java:27)

    [09:08:39] [Server thread/WARN]:     at net.minecraft.server.v1_15_R1.PacketPlayInUseItem.a(PacketPlayInUseItem.java:1)

    [09:08:39] [Server thread/WARN]:     at net.minecraft.server.v1_15_R1.PlayerConnectionUtils.lambda$0(PlayerConnectionUtils.java:19)

    [09:08:39] [Server thread/WARN]:     at net.minecraft.server.v1_15_R1.TickTask.run(SourceFile:18)

    [09:08:39] [Server thread/WARN]:     at net.minecraft.server.v1_15_R1.IAsyncTaskHandler.executeTask(SourceFile:144)

    [09:08:39] [Server thread/WARN]:     at net.minecraft.server.v1_15_R1.IAsyncTaskHandlerReentrant.executeTask(SourceFile:23)

    [09:08:39] [Server thread/WARN]:     at net.minecraft.server.v1_15_R1.IAsyncTaskHandler.executeNext(SourceFile:118)

    [09:08:39] [Server thread/WARN]:     at net.minecraft.server.v1_15_R1.MinecraftServer.aZ(MinecraftServer.java:917)

    [09:08:39] [Server thread/WARN]:     at net.minecraft.server.v1_15_R1.MinecraftServer.executeNext(MinecraftServer.java:910)

    [09:08:39] [Server thread/WARN]:     at net.minecraft.server.v1_15_R1.IAsyncTaskHandler.awaitTasks(SourceFile:127)

    [09:08:39] [Server thread/WARN]:     at net.minecraft.server.v1_15_R1.MinecraftServer.sleepForTick(MinecraftServer.java:894)

    [09:08:39] [Server thread/WARN]:     at net.minecraft.server.v1_15_R1.MinecraftServer.run(MinecraftServer.java:827)

    [09:08:39] [Server thread/WARN]:     at java.base/java.lang.Thread.run(Thread.java:830)

     
     
  4. I question why in gods name you are serializing inventories with base64, but in any case, the docs for OptionalDataException state that this can only happen with two causes. I believe yours if the first, where with reading the Objects, you hit a primitive. This may be how you are initially saving the inventories.