Transferring a player's inventory to a GUI

Discussion in 'Plugin Development' started by UnpeeledBanana1, Sep 25, 2015.

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


    I am trying to create a trash disposal inventory, and I am attempting to add an option where, when a player clicks a nether star, their inventory is transported to the gui.

    However, when I click the nether star I get a ton of errors on the console.

    Here is the code:

    if (event.getSlot() == 53) {

    ItemStack[] inv = player.getInventory().getContents();
    Inventory inventory = event.getInventory();


    I've tried using "inventory.setContents(inv)" but it's not the method that I want to use, as it removes the nether star.

    Does anyone have any idea how to fix this?
  2. Offline


    Try looping through their inventory, and clearing each item individually if it isn't that nether star.
  3. @UnpeeledBanana1
    Error logs are much appreciated, and please use code tags next time.
  4. Offline


    @megamichiel Sorry about that, i'm sorta new to Bukkit forums, here's the error log:

    [19:45:00] [Server thread/ERROR]: Could not pass event InventoryClickEvent to BananaESSENTIALS v1.0
        at$1.execute( ~[paperspigot-1.8.8.jar:git-PaperSpigot-6d0a86a-53fac9f]
        at org.bukkit.plugin.RegisteredListener.callEvent( ~[paperspigot-1.8.8.jar:git-PaperSpigot-6d0a86a-53fac9f]
        at org.bukkit.plugin.SimplePluginManager.fireEvent( [paperspigot-1.8.8.jar:git-PaperSpigot-6d0a86a-53fac9f]
        at org.bukkit.plugin.SimplePluginManager.callEvent( [paperspigot-1.8.8.jar:git-PaperSpigot-6d0a86a-53fac9f]
        at net.minecraft.server.v1_8_R3.PlayerConnection.a( [paperspigot-1.8.8.jar:git-PaperSpigot-6d0a86a-53fac9f]
        at net.minecraft.server.v1_8_R3.PacketPlayInWindowClick.a(SourceFile:31) [paperspigot-1.8.8.jar:git-PaperSpigot-6d0a86a-53fac9f]
        at net.minecraft.server.v1_8_R3.PacketPlayInWindowClick.a(SourceFile:9) [paperspigot-1.8.8.jar:git-PaperSpigot-6d0a86a-53fac9f]
        at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$ [paperspigot-1.8.8.jar:git-PaperSpigot-6d0a86a-53fac9f]
        at java.util.concurrent.Executors$ Source) [?:1.8.0_31]
        at Source) [?:1.8.0_31]
        at net.minecraft.server.v1_8_R3.SystemUtils.a(SourceFile:44) [paperspigot-1.8.8.jar:git-PaperSpigot-6d0a86a-53fac9f]
        at net.minecraft.server.v1_8_R3.MinecraftServer.B( [paperspigot-1.8.8.jar:git-PaperSpigot-6d0a86a-53fac9f]
        at net.minecraft.server.v1_8_R3.DedicatedServer.B( [paperspigot-1.8.8.jar:git-PaperSpigot-6d0a86a-53fac9f]
        at net.minecraft.server.v1_8_R3.MinecraftServer.A( [paperspigot-1.8.8.jar:git-PaperSpigot-6d0a86a-53fac9f]
        at [paperspigot-1.8.8.jar:git-PaperSpigot-6d0a86a-53fac9f]
        at Source) [?:1.8.0_31]
    Caused by: java.lang.IllegalArgumentException: Item cannot be null
        at org.apache.commons.lang.Validate.noNullElements( ~[paperspigot-1.8.8.jar:git-PaperSpigot-6d0a86a-53fac9f]
        at org.bukkit.craftbukkit.v1_8_R3.inventory.CraftInventory.addItem( ~[paperspigot-1.8.8.jar:git-PaperSpigot-6d0a86a-53fac9f]
        at me.UnpeeledBanana1.bananaessentials.commands.gui.inventoryclick.trashI.onInventoryClick( ~[?:?]
        at sun.reflect.GeneratedMethodAccessor80.invoke(Unknown Source) ~[?:?]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_31]
        at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_31]
        at$1.execute( ~[paperspigot-1.8.8.jar:git-PaperSpigot-6d0a86a-53fac9f]
        ... 15 more
  5. Offline


    It's because you are trying to add an item to the inventory, even if there is nothing in that slot.

    Edit: I think a for loop would serve your better:
    for (ItemStack item : player.getInventory().getContents) {
        if (item == null) return;
    Also, there is no reason to declare a variable if you only use it once.

    Edit2: @megamichiel I ninja'd you :p
    Last edited: Sep 25, 2015
  6. @UnpeeledBanana1
    Ah, I found your issue.
    Inventory#getContents() returns an array of all the items within the inventory in their corresponding slot, but this includes empty slots, so it throws a NullPointerException because the slot is empty and therefor the item in the slot is null. You need to manually iterate over the Inventory#getContents() And check if the item is not null, and then add it.
  7. Offline


    public static Inventory viewInv(Player p) {    
    Inventory inv = Bukkit.createInventory(Player#, InventoryType.PLAYER);
            return inv;
    then to open the inventory do:
    No problem, just make sure that you understand the code.

    and then make sure to clear their own inventory.

    @henderry2019 It's unneccecary to use it when you can just use: Inventory#setContents(ItemStack[]);

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
    Last edited by a moderator: Jun 11, 2016
Thread Status:
Not open for further replies.

Share This Page