Ender Chest Help

Discussion in 'Plugin Development' started by xxCoderForLifexx, May 30, 2015.

Thread Status:
Not open for further replies.
  1. Is there a way to send a players whole inventory to an Ender chest?
     
  2. Offline

    caderape

    @xxCoderForLifexx
    it depends the number of items.
    An enderchest has 27 slot, a player inventory 36

    Loops itemstacks then add it to the enderchest
     
  3. Now you make a good point about the slots so I have a random question. What if I send 36 slots, will the mod just crash or would it only send 27 and leave the 9 left in the players inventory?
     
  4. Offline

    caderape

    @xxCoderForLifexx
    if you set the inventory contents to the enderchest, you will get an error.
    If you add item by item, 27 items will be added and you will get an error for the slot 28 and your code will be break.

    if you loops enderchest.size, that should be good. No error but 9 items will be left.
    ex:
    for (int i = 0; i < enderchest.size; i++)
    enderchest.additem(inventory.getitem(i))

    something like that
     
  5. Thanks I'll try it out I think my problem was I has it set to the inventory size not the ender chest.

    Code:
    Player player = (Player)sender;
          if (player.hasPermission("ender.all"))
          {
            for (int i = 0; i >= 27; i++) {
              player.getEnderChest().addItem(player.getInventory().getItem(i));
            }
    This is what I got and I've tried
    Code:
    Player player = (Player)sender;
          if (player.hasPermission("ender.all"))
          {
            for (int i = 0; i >= 27; i++) {
              player.getEnderChest().addItem(new ItemStack[] { player.getInventory().getItem(i)});
            }
    Still nothing works it gives me the message that it went through but the items just vanish. Because it runs the Player.Inventory.clear()

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 12, 2016
  6. Offline

    I Al Istannen

    @xxCoderForLifexx In your for loop you set i to 0. But your condition is "i >= 27". Because you set i to 0, it can't be bigger than 27 and the for loop doesn't run. And you can just add the itemstack, no need for creating an array.
     
  7. Offline

    FisheyLP

    A working loop would be
    Code:
    for (int i = 0; i < 27; i++) {
     
    xxCoderForLifexx likes this.
  8. Okay thanks I'll try some other methods.

    Thanks I'll give this a shot.

    Code:
    [11:12:47 ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'ende
    r' in plugin Ender_Send v1.0 [Beta Version]
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[cra
    ftbukkit.jar:git-Bukkit-"8c3698b"]
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:18
    0) ~[craftbukkit.jar:git-Bukkit-"8c3698b"]
            at org.bukkit.craftbukkit.v1_8_R1.CraftServer.dispatchCommand(CraftServe
    r.java:623) ~[craftbukkit.jar:git-Bukkit-"8c3698b"]
            at net.minecraft.server.v1_8_R1.PlayerConnection.handleCommand(PlayerCon
    nection.java:1025) [craftbukkit.jar:git-Bukkit-"8c3698b"]
            at net.minecraft.server.v1_8_R1.PlayerConnection.a(PlayerConnection.java
    :886) [craftbukkit.jar:git-Bukkit-"8c3698b"]
            at net.minecraft.server.v1_8_R1.PacketPlayInChat.a(SourceFile:37) [craft
    bukkit.jar:git-Bukkit-"8c3698b"]
            at net.minecraft.server.v1_8_R1.PacketPlayInChat.a(SourceFile:9) [craftb
    ukkit.jar:git-Bukkit-"8c3698b"]
            at net.minecraft.server.v1_8_R1.PacketHandleTask.run(SourceFile:13) [cra
    ftbukkit.jar:git-Bukkit-"8c3698b"]
            at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [
    ?:1.8.0_40]
            at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_40]
            at net.minecraft.server.v1_8_R1.MinecraftServer.z(MinecraftServer.java:6
    45) [craftbukkit.jar:git-Bukkit-"8c3698b"]
            at net.minecraft.server.v1_8_R1.DedicatedServer.z(DedicatedServer.java:2
    84) [craftbukkit.jar:git-Bukkit-"8c3698b"]
            at net.minecraft.server.v1_8_R1.MinecraftServer.y(MinecraftServer.java:6
    00) [craftbukkit.jar:git-Bukkit-"8c3698b"]
            at net.minecraft.server.v1_8_R1.MinecraftServer.run(MinecraftServer.java
    :508) [craftbukkit.jar:git-Bukkit-"8c3698b"]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_40]
    Caused by: java.lang.IllegalArgumentException: Item cannot be null
            at org.apache.commons.lang.Validate.noNullElements(Validate.java:410) ~[
    craftbukkit.jar:git-Bukkit-"8c3698b"]
            at org.bukkit.craftbukkit.v1_8_R1.inventory.CraftInventory.addItem(Craft
    Inventory.java:267) ~[craftbukkit.jar:git-Bukkit-"8c3698b"]
            at me.Coderforlife.Ender.Cmds.onCommand(Cmds.java:50) ~[?:?]
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[cra
    ftbukkit.jar:git-Bukkit-"8c3698b"]
            ... 14 more
    Tried the different methods and this is what I got.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 12, 2016
  9. Offline

    FisheyLP

     
  10. I must of missed that but thanks.

    The log keeps saying that the Item can't be null. I just can't seem to figure out where it is null.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 12, 2016
  11. Offline

    I Al Istannen

    @xxCoderForLifexx On line 50 of onCommand. Remember, the item in a Slot of an Inventory can be null. When there is none. So you would need to check that before adding it to the other.
     
  12. Offline

    FisheyLP

    Show the whole code
     
  13. Code:
    if ((args.length == 1) &&
          (args[0].equalsIgnoreCase("all")) &&
          ((sender instanceof Player)))
        {
          Player player = (Player)sender;
          {
            Inventory i = player.getInventory();
            ItemStack[] o = player.getInventory().getContents().clone();
             for (int p = 0; p < 27; p++) {
              player.getEnderChest().addItem(o);
            }
            
    
         }
            player.sendMessage(ChatColor.RED + "Sending everything to your inventory");
            player.getInventory().clear();
          }
        return true;
    Here's the bit with the command.
    This is the whole class file http://pastebin.com/9yfw1kdn (Didn't want to cludder the forum)
    I was trying differnt methods so the code above does not work at all.
     
  14. Offline

    caderape

    @xxCoderForLifexx as we said, check if the item is not null before to add, or you will get an error
     
    xxCoderForLifexx likes this.
  15. Offline

    MaTaMoR_

    Man.. you're adding the whole inventory everytime in the loop... also you have to check if the item is null :
    Code:
    ItemStack[] content = player.getInventory().getContents();
    for(int i = 0; 27 > i; i++) {
       ItemStack stack = content[i];
       if(stack == null || stack.getType().equals(Material.AIR)) continue;
    
       player.getEnderChest().addItem(stack);
    }
    
    How do you have 10 plugins and don't even know that ?
     
  16. Thanks man. I haven't coded in about 2 years. I'm trying to get it all back.
     
Thread Status:
Not open for further replies.

Share This Page