Spawning chest with player's items upon player's death

Discussion in 'Plugin Development' started by Billythekidzz, Apr 14, 2013.

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

    Billythekidzz

    Hi, I've been trying to make a plugin where if a player dies, a chest spawns on the location where they die with the items they die with. This is what I have written for it:
    Code:
    @EventHandler
    public void ChestSpawn(PlayerDeathEvent event){
        Player player = (Player) event.getEntity();
        player.getInventory();
        ItemStack[] items = player.getInventory().getContents();
        Location loc = player.getLocation();
        Block block = loc.getBlock();
        loc.getBlock().setType(Material.CHEST);
        Chest chest = (Chest)block.getState();
        Inventory inv = chest.getInventory();
        inv.addItem(items);
    }
    }
    However, when a player dies a chest spawns with nothing in it, and I get this error:
    Caused by: java.lang.IllegalArgumentException: Item cannot be null
    at org.apache.commons.lang.Validate.noNullElements(Validate.java:410)
    at org.bukkit.craftbukkit.v1_5_R2.inventory.CraftInventory.addItem(CraftInventory.java:267)
    at com.gmail.griefcraftowner.billyenchants.BillyenchantListener.ChestSpawn(BillyenchantListener.java:407)

    Line 407 is inv.addItem(items);

    Help would be greatly appreciated, thanks. :)

    Also, how would I get the chest to despawn after 5 minutes?

    Bump. Really need to get this done. :/

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

    surtic

    Do you have the full Error Message of this?

    And for the Despawne you need to make a Runnable who Exectue after 5mins and remove the Chest
     
  3. items should not contain any null when passing it to addItem, if you want to pass null, use setcontents (beware: player inv = 4*9, chest = 3*9)
     
  4. Offline

    Billythekidzz

    Thanks. How do I make it so if the inventory size is over 27 it spawns a double chest instead? ferrybig
    Even if the player dies with a wooden plank and nothing else, it still gives this error:

    Caused by: java.lang.IllegalArgumentException: Invalid inventory size; expected 27 or less
    at org.bukkit.craftbukkit.v1_5_R2.inventory.CraftInventory.setContents(CraftInventory.java:66)
    at com.gmail.griefcraftowner.billyenchants.BillyenchantListener.ChestSpawn(BillyenchantListener.java:407)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:425)
    ... 22 more
    21:44:36 [INFO] Billythekidzz hit the ground too hard
    21:44:46 [INFO] Billythekidzz issued server command: /kill
    21:44:46 [SEVERE] Could not pass event PlayerDeathEvent to billyenchant v6.0
    org.bukkit.event.EventException
    at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:427)
    at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62)
    at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:477)
    at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:462)
    at org.bukkit.craftbukkit.v1_5_R2.event.CraftEventFactory.callPlayerDeathEvent(CraftEventFactory.java:335)
    at net.minecraft.server.v1_5_R2.EntityPlayer.die(EntityPlayer.java:285)
    at org.bukkit.craftbukkit.v1_5_R2.entity.CraftLivingEntity.setHealth(CraftLivingEntity.java:72)
    at org.bukkit.command.defaults.KillCommand.execute(KillCommand.java:33)
    at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:187)
    at org.bukkit.craftbukkit.v1_5_R2.CraftServer.dispatchCommand(CraftServer.java:523)
    at net.minecraft.server.v1_5_R2.PlayerConnection.handleCommand(PlayerConnection.java:967)
    at net.minecraft.server.v1_5_R2.PlayerConnection.chat(PlayerConnection.java:885)
    at net.minecraft.server.v1_5_R2.PlayerConnection.a(PlayerConnection.java:840)
    at net.minecraft.server.v1_5_R2.Packet3Chat.handle(Packet3Chat.java:44)
    at net.minecraft.server.v1_5_R2.NetworkManager.b(NetworkManager.java:292)
    at net.minecraft.server.v1_5_R2.PlayerConnection.d(PlayerConnection.java:113)
    at net.minecraft.server.v1_5_R2.ServerConnection.b(SourceFile:35)
    at net.minecraft.server.v1_5_R2.DedicatedServerConnection.b(SourceFile:30)
    at net.minecraft.server.v1_5_R2.MinecraftServer.r(MinecraftServer.java:580)
    at net.minecraft.server.v1_5_R2.DedicatedServer.r(DedicatedServer.java:225)
    at net.minecraft.server.v1_5_R2.MinecraftServer.q(MinecraftServer.java:476)
    at net.minecraft.server.v1_5_R2.MinecraftServer.run(MinecraftServer.java:409)
    at net.minecraft.server.v1_5_R2.ThreadServerApplication.run(SourceFile:573)
    Caused by: java.lang.IllegalArgumentException: Invalid inventory size; expected 27 or less
    at org.bukkit.craftbukkit.v1_5_R2.inventory.CraftInventory.setContents(CraftInventory.java:66)
    at com.gmail.griefcraftowner.billyenchants.BillyenchantListener.ChestSpawn(BillyenchantListener.java:407)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:425)
    ... 22 more

    Any solutions? :O
     
  5. Offline

    Tzeentchful

    Just get a block in a direction reative to the chest and set it aswell.
    Code:java
    1.  
    2. //do check to see if we need a double chest
    3. block.getRelative(BlockFace.NORTH).setType(Material.Chest);
    4.  
     
  6. Offline

    Billythekidzz

    Works, thanks a lot :)

    ferrybig Tzeentchful
    Sorry, one more thing, how do I add armour into the chests? I've got
    ItemStack[] armors = player.getInventory().getArmorContents();But I can only setContents to either the Inventory contents OR Armour contents.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 1, 2016
  7. merge those arrays to each other, here is an example with bytes:
    byte[] one = new byte[]{3,2};;
    byte[] two = new byte[]{1,2,3};
    byte[] combined = new byte[one.length + two.length];
    System.arraycopy(one, 0, combined, 0, one.length);
    System.arraycopy(two, 0, combined, one.length, two.length);
     
Thread Status:
Not open for further replies.

Share This Page