SetItem works just once

Discussion in 'Plugin Development' started by Eacrow, May 15, 2020.

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

    Eacrow

    I'm trying to make a plugin that teleport players and separates inventories between worlds. Everything works fine, but the inventory lines works just once.

    I'm using this for saving the inventory in a yml file.
    Code:
    FileConfiguration config = plugin.getConfig();
    config.set("Config.Jugadores."+Jugador.getUniqueId()+".CordsOW.Inventario", Jugador.getInventory().getContents());
    plugin.saveConfig();
    And this for set the inventory.
    Code:
    List<ItemStack> items = (List<ItemStack>) config.getList("Config.Jugadores."+Jugador.getUniqueId()+".CordsOW.Inventario");
    for (int i=0;i<Jugador.getInventory().getSize();i++) {
        Jugador.getInventory().setItem(i,items.get(i)); //Console says the error is in this line
    }
    What am I doing wrong? I'm new to both java and bukkit.
     
  2. Offline

    Tango_

    Can you paste the error here
     
  3. Offline

    Eacrow

    Yeah, sorry I didn't do it before.

    Code:
    [12:53:11] [Server thread/ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'mundo' in plugin uwu v1.0.0
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:47) ~[spigot.jar:git-Spigot-2f5d615-d07a78b]
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:149) ~[spigot.jar:git-Spigot-2f5d615-d07a78b]
        at org.bukkit.craftbukkit.v1_15_R1.CraftServer.dispatchCommand(CraftServer.java:723) ~[spigot.jar:git-Spigot-2f5d615-d07a78b]
        at net.minecraft.server.v1_15_R1.PlayerConnection.handleCommand(PlayerConnection.java:1658) ~[spigot.jar:git-Spigot-2f5d615-d07a78b]
        at net.minecraft.server.v1_15_R1.PlayerConnection.a(PlayerConnection.java:1498) ~[spigot.jar:git-Spigot-2f5d615-d07a78b]
        at net.minecraft.server.v1_15_R1.PacketPlayInChat.a(PacketPlayInChat.java:47) ~[spigot.jar:git-Spigot-2f5d615-d07a78b]
        at net.minecraft.server.v1_15_R1.PacketPlayInChat.a(PacketPlayInChat.java:1) ~[spigot.jar:git-Spigot-2f5d615-d07a78b]
        at net.minecraft.server.v1_15_R1.PlayerConnectionUtils.lambda$0(PlayerConnectionUtils.java:19) ~[spigot.jar:git-Spigot-2f5d615-d07a78b]
        at net.minecraft.server.v1_15_R1.TickTask.run(SourceFile:18) [spigot.jar:git-Spigot-2f5d615-d07a78b]
        at net.minecraft.server.v1_15_R1.IAsyncTaskHandler.executeTask(SourceFile:144) [spigot.jar:git-Spigot-2f5d615-d07a78b]
        at net.minecraft.server.v1_15_R1.IAsyncTaskHandlerReentrant.executeTask(SourceFile:23) [spigot.jar:git-Spigot-2f5d615-d07a78b]
        at net.minecraft.server.v1_15_R1.IAsyncTaskHandler.executeNext(SourceFile:118) [spigot.jar:git-Spigot-2f5d615-d07a78b]
        at net.minecraft.server.v1_15_R1.MinecraftServer.ba(MinecraftServer.java:918) [spigot.jar:git-Spigot-2f5d615-d07a78b]
        at net.minecraft.server.v1_15_R1.MinecraftServer.executeNext(MinecraftServer.java:911) [spigot.jar:git-Spigot-2f5d615-d07a78b]
        at net.minecraft.server.v1_15_R1.IAsyncTaskHandler.awaitTasks(SourceFile:127) [spigot.jar:git-Spigot-2f5d615-d07a78b]
        at net.minecraft.server.v1_15_R1.MinecraftServer.sleepForTick(MinecraftServer.java:895) [spigot.jar:git-Spigot-2f5d615-d07a78b]
        at net.minecraft.server.v1_15_R1.MinecraftServer.run(MinecraftServer.java:828) [spigot.jar:git-Spigot-2f5d615-d07a78b]
        at java.lang.Thread.run(Unknown Source) [?:1.8.0_241]
    Caused by: java.lang.NullPointerException
        at eacrow.Comandos.mundo.onCommand(mundo.java:93) ~[?:?]
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:45) ~[spigot.jar:git-Spigot-2f5d615-d07a78b]
        ... 17 more
     
  4. this means that something is undefined and where it is at (mundo.java:93) if you're in intelij (not sure about eclipse) you can click mundo.java:93 and it will take you there. With the infomation you've provided that's the best i can help you.
     
    Last edited by a moderator: Jun 3, 2020
  5. Offline

    Legendary_zotar

    If you still have the problem, Can you share an example of how it looks when saved in config @Eacrow
     
  6. Offline

    Eacrow

    I had problems with the hard drive and I lost the file, sorry
     
  7. Offline

    caderapee

    @Eacrow Did you check if the inventory saved has 27 slots in the config ? i dun remember if the saving method skip null slot of save it
     
  8. Offline

    Eacrow

    @Legendary_zotar Ok I recreate it, it's stored this way
    Code:
            Inventario:
            - ==: org.bukkit.inventory.ItemStack
              v: 2230
              type: WOODEN_AXE
            - ==: org.bukkit.inventory.ItemStack
              v: 2230
              type: COMPASS
            - ==: org.bukkit.inventory.ItemStack
              v: 2230
              type: OAK_LOG
              amount: 64
            - null
            - null
            - null
            - ==: org.bukkit.inventory.ItemStack
              v: 2230
              type: DIAMOND_BOOTS
            - null
            - null
            - null
            - null
            - null
            - null
            - null
            - null
            - null
            - null
            - null
            - null
            - null
            - null
            - ==: org.bukkit.inventory.ItemStack
              v: 2230
              type: SPRUCE_PLANKS
            - ==: org.bukkit.inventory.ItemStack
              v: 2230
              type: SPRUCE_PLANKS
            - ==: org.bukkit.inventory.ItemStack
              v: 2230
              type: SPRUCE_PLANKS
            - ==: org.bukkit.inventory.ItemStack
              v: 2230
              type: SPRUCE_PLANKS
            - null
            - null
            - null
            - null
            - null
            - null
            - null
            - null
            - ==: org.bukkit.inventory.ItemStack
              v: 2230
              type: SPRUCE_PLANKS
              amount: 60
            - null
            - null
            - ==: org.bukkit.inventory.ItemStack
              v: 2230
              type: CHAINMAIL_BOOTS
              meta:
                ==: ItemMeta
                meta-type: UNSPECIFIC
                Damage: 2
            - null
            - null
            - null
            - null

    @caderapee It seems that it saves more than 27 slots, although it worked, but only once, for it to work again I had to restart the server.
     
  9. Offline

    CraftCreeper6

    @Eacrow
    The error literally says "NullPointerException" when you're passing null values to the setItem method. You can't just save the inventory like that, org.bukkit.inventory.ItemStack means nothing at all to anyone.

    You need a serialization method called something like "serializeInventory" which will save the config like the following:
    Code:
    1:
        type: DIAMOND_BOOTS
        quantity: 1
        enchantments:
               - enchant 1
               - enchant 2
        durability: 10
        etc...
    For each slot in the inventory, incrementing 1 as you go through the slots.

    Then create a deserializeInventory method that will just do the reverse.
     
  10. Offline

    Eacrow

    So why does it work the first time?
     
  11. Offline

    Legendary_zotar

    @Eacrow
    you should modify your code to:
    Save items with they're slot number (so when loading you can set to same slot number)
    and ignore Empty slots so it doesnt save as null
    also some items like "Barrel" well save as null because its a 1.14+ item to fix that you can add "api-version: 1.14" or 1.15 to your plugin.yml
     
Thread Status:
Not open for further replies.

Share This Page