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

    UnpeeledBanana1

    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) {
    event.setCancelled(true);

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

    inventory.addItem(inv);
    player.getInventory().clear();
    return;
    }


    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

    RoboticPlayer

    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

    UnpeeledBanana1

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

    Code:
    [19:45:00] [Server thread/ERROR]: Could not pass event InventoryClickEvent to BananaESSENTIALS v1.0
    org.bukkit.event.EventException
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:310) ~[paperspigot-1.8.8.jar:git-PaperSpigot-6d0a86a-53fac9f]
        at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[paperspigot-1.8.8.jar:git-PaperSpigot-6d0a86a-53fac9f]
        at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:502) [paperspigot-1.8.8.jar:git-PaperSpigot-6d0a86a-53fac9f]
        at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:487) [paperspigot-1.8.8.jar:git-PaperSpigot-6d0a86a-53fac9f]
        at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java:1633) [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$1.run(SourceFile:13) [paperspigot-1.8.8.jar:git-PaperSpigot-6d0a86a-53fac9f]
        at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_31]
        at java.util.concurrent.FutureTask.run(Unknown 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(MinecraftServer.java:771) [paperspigot-1.8.8.jar:git-PaperSpigot-6d0a86a-53fac9f]
        at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:378) [paperspigot-1.8.8.jar:git-PaperSpigot-6d0a86a-53fac9f]
        at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:710) [paperspigot-1.8.8.jar:git-PaperSpigot-6d0a86a-53fac9f]
        at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:613) [paperspigot-1.8.8.jar:git-PaperSpigot-6d0a86a-53fac9f]
        at java.lang.Thread.run(Unknown Source) [?:1.8.0_31]
    Caused by: java.lang.IllegalArgumentException: Item cannot be null
        at org.apache.commons.lang.Validate.noNullElements(Validate.java:364) ~[paperspigot-1.8.8.jar:git-PaperSpigot-6d0a86a-53fac9f]
        at org.bukkit.craftbukkit.v1_8_R3.inventory.CraftInventory.addItem(CraftInventory.java:265) ~[paperspigot-1.8.8.jar:git-PaperSpigot-6d0a86a-53fac9f]
        at me.UnpeeledBanana1.bananaessentials.commands.gui.inventoryclick.trashI.onInventoryClick(trashI.java:37) ~[?:?]
        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 org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:306) ~[paperspigot-1.8.8.jar:git-PaperSpigot-6d0a86a-53fac9f]
        ... 15 more
    
     
  5. Offline

    RoboticPlayer

    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:
    Code:
    for (ItemStack item : player.getInventory().getContents) {
        if (item == null) return;
        event.getInventory.addItem(item);
        player.getInventory().clear();
    }
    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

    DoggyCode™

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

    EDIT:
    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