How do we get an item from a players inventory and cast it as an itemstack?

Discussion in 'Plugin Development' started by gabrielmaennl555, Nov 10, 2014.

Thread Status:
Not open for further replies.
  1. Ok so, the name basically says it all :p I would like to know how
    The code im currently using dosnt really work;

    Code:java
    1. if(p.getInventory().contains(Material.NETHER_STAR))
    2. {
    3. for(ItemStack i: p.getInventory())
    4. {
    5. if(i.getAmount() > 5)
    6. {
    7.  
    8. p.getInventory().addItem(e);
    9. i.setAmount(i.getAmount() - 5);
    10. }
    11. }


    Thanks! Also please tag me so I know youve commented, thanks!
     
  2. Offline

    BajanAmerican

    gabrielmaennl555
    "for(ItemStack i : p.getInventory())" should be "for(ItemStack i : p.getInventory().contents())"
     
    mine-care likes this.
  3. Offline

    mine-care

    Originally you get itemstack from players inventory so no need for cast... Also what you want to do? Please be more specific
     
  4. Offline

    xTigerRebornx

    BajanAmerican Inventory implements Iterable<ItemStack>, so what he did works perfectly fine.
    gabrielmaennl555 What about the code doesn't work?
     
  5. Offline

    Konato_K

    Actually inventories are iterable, so there is nothing wrong with using player#getInventory() instead of Inventory#getContents()
     
  6. BajanAmerican mine-care xTigerRebornx Konato_K

    Ok so guys, Im making a command where when the player types it, It checks to see if the player has atleast 5 nether stars (the plugins currency), And if they have 5 nether stars it will give them an item, the item glitches out and it gives them like 10 of them when its suppose to do 1, Here this is the error shown in console:

    Code:
    [07:50:51 INFO]: gabrielmaennl5 issued server command: /eg buy MH
    [07:50:51 ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'eg' in plugin ElementalGear v1.0
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:180) ~[craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
        at org.bukkit.craftbukkit.v1_7_R3.CraftServer.dispatchCommand(CraftServer.java:701) ~[craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
        at net.minecraft.server.v1_7_R3.PlayerConnection.handleCommand(PlayerConnection.java:956) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
        at net.minecraft.server.v1_7_R3.PlayerConnection.a(PlayerConnection.java:817) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
        at net.minecraft.server.v1_7_R3.PacketPlayInChat.a(PacketPlayInChat.java:28) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
        at net.minecraft.server.v1_7_R3.PacketPlayInChat.handle(PacketPlayInChat.java:47) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
        at net.minecraft.server.v1_7_R3.NetworkManager.a(NetworkManager.java:157) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
        at net.minecraft.server.v1_7_R3.ServerConnection.c(SourceFile:134) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
        at net.minecraft.server.v1_7_R3.MinecraftServer.v(MinecraftServer.java:667) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
        at net.minecraft.server.v1_7_R3.DedicatedServer.v(DedicatedServer.java:260) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
        at net.minecraft.server.v1_7_R3.MinecraftServer.u(MinecraftServer.java:558) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
        at net.minecraft.server.v1_7_R3.MinecraftServer.run(MinecraftServer.java:469) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
        at net.minecraft.server.v1_7_R3.ThreadServerApplication.run(SourceFile:628) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
    Caused by: java.lang.NullPointerException
        at ElementalGear.CommandHandler.onCommand(CommandHandler.java:53) ~[?:?]
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
        ... 13 more
    Current code:

    Code:java
    1. if(args[1].equalsIgnoreCase("MH"))
    2. {
    3. if(p.getInventory().contains(Material.NETHER_STAR))
    4. {
    5. for(ItemStack i: p.getInventory())
    6. {
    7. if(i.getAmount() > 5) // <======= LINE 53
    8. {
    9. ItemStack e = Util.createArmor(Material.LEATHER_HELMET, 1, Color.fromRGB(204, 76, 76), "§cMagmatic Armor", "§cMoltern Core", "§ktest");
    10. p.getInventory().addItem(e);
    11. i.setAmount(i.getAmount() - 5);
    12. }
    13. }
    14.  
    15. }


    If I compleatly remove all the item coding, It will work though, but then they will get it for free D;
     
  7. Offline

    tangster132

    To fix your error, you would just need an if check before you call i.getAmount() to check if "i" is not null, then just continue the rest of your code. An empty inventory slot is equal to null. So that's why you will get a NullPointerException sometimes; when you try to call getAmount() on a null value.

    Also, your for loop is currently checking every inventory slot if that slot has more than 5 items. I'm guessing that's probably why you're getting 10 leather helmets instead of 1.
     
    Konato_K likes this.
Thread Status:
Not open for further replies.

Share This Page