for ItemStack in PlayerInventory.Contents error

Discussion in 'Plugin Development' started by Raxor1234, Sep 11, 2015.

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

    Raxor1234

    just as the title says. I'm getting an error when i try to do this:

    for (ItemStack i : p.getinventory.getcontents(){
    ERROR > if (i.getAmmount() == 1){ .....

    any time i try to access the itemstack it gives an error. Anyone !? plz help! :-(
     
  2. Offline

    RoboticPlayer

    Please post the error, and some of your other code, as we don't have much information.
     
  3. Offline

    Raxor1234

    for (int in =0; in<=35;in++){
    ItemStack i = p.getInventory().getItem(in);
    Bukkit.getServer().broadcastMessage("" + i.getType().toString());

    for (String l : utils.itemManager.itemCostDatabase.get(key)){
    counter++;
    String splited[] = l.split(":");

    Bukkit.getServer().broadcastMessage(l);
    if (i.getAmount() >= Integer.parseInt(splited[1])){
    if (i.getItemMeta().getDisplayName().equals(splited[0])){
    total++;
    }
    }
    }
    Bukkit.getServer().broadcastMessage("" + counter + ":" + total);
    if (counter >= total){
    p.sendMessage(ChatColor.GREEN + key);
    break;
    } else {
    continue;
    }
    }
    }

    @henderry2019

    --------------------- ERROR ----------------------------
    [13:28:29 ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'c' i
    n plugin Phantasm v0.1
    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[spi
    got188.jar:git-Spigot-6c9b0a1-de5c261]
    at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:14
    1) ~[spigot188.jar:git-Spigot-6c9b0a1-de5c261]
    at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchCommand(CraftServe
    r.java:640) ~[spigot188.jar:git-Spigot-6c9b0a1-de5c261]
    at net.minecraft.server.v1_8_R3.PlayerConnection.handleCommand(PlayerCon
    nection.java:1162) [spigot188.jar:git-Spigot-6c9b0a1-de5c261]
    at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java
    :997) [spigot188.jar:git-Spigot-6c9b0a1-de5c261]
    at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(PacketPlayInChat.java
    :45) [spigot188.jar:git-Spigot-6c9b0a1-de5c261]
    at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(PacketPlayInChat.java
    :1) [spigot188.jar:git-Spigot-6c9b0a1-de5c261]
    at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$1.run(SourceFile:1
    3) [spigot188.jar:git-Spigot-6c9b0a1-de5c261]
    at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [
    ?:1.7.0_79]
    at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.7.0_79]
    at net.minecraft.server.v1_8_R3.SystemUtils.a(SourceFile:44) [spigot188.
    jar:git-Spigot-6c9b0a1-de5c261]
    at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:7
    14) [spigot188.jar:git-Spigot-6c9b0a1-de5c261]
    at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:3
    74) [spigot188.jar:git-Spigot-6c9b0a1-de5c261]
    at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:6
    53) [spigot188.jar:git-Spigot-6c9b0a1-de5c261]
    at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java
    :556) [spigot188.jar:git-Spigot-6c9b0a1-de5c261]
    at java.lang.Thread.run(Unknown Source) [?:1.7.0_79]
    Caused by: java.lang.NullPointerException
    at master.app.onCommand(app.java:54) ~[?:?]
    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spi
    got188.jar:git-Spigot-6c9b0a1-de5c261]
    ... 15 more
    >
    @
     
  4. Offline

    Halmerson

    @Raxor1234
    I believe because you are getting the amount of an unspecified item.
    If that's not it, try doing if p.get inventory().get contents().getAmount((maybe add item here??)).

    Lastly, if the above information did not help you, try, ItemStack i = p.getinventory().get contents();
    Rather than the for statement.

    I did not see the code above, I see you have already done Itemstack i.
    I did notice, you have an int in your getItem(in); I'm not completely sure if that works, because by doing .getItem(in), you're basically saying get item 0. Like I said, I'm not sure if that's the problem, but maybe look into it.

    EDIT by Timtower: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Sep 11, 2015
  5. Offline

    Raxor1234

    @Halmerson
    How can i detect if its an unspecified item?!

    EDIT: also i am trying to get the display name of each item in the player's inventory.
     
  6. Offline

    Halmerson

    @Raxor1234
    How will the item be unspecified?? Is it a custom item plugin?

    I found the solution for the error in ItemStack I think. If you're doing ItemStack i = player.getInventory().getContents(); that is an error. Mainly because instead of ItemStack, it would be inventory. For example : Inventory i = player.getInventory();

    Also, For getting display name you need need an ItemMeta of the item, then item.setDisplayName(String);
     
  7. Offline

    Raxor1234

    @Halmerson
    The item is unspecified when the checked inventory slot is empty.. but I cant find a way to check if it is unspecified, because if I try ( if (i == null)....) gives strange stuff... :((((
     
  8. Offline

    Halmerson

    @Raxor1234
    Maybe try checking if the unspecified slot is Material.AIR

    Or making the unspecified slot air
     
    Last edited: Sep 11, 2015
  9. Offline

    RoboticPlayer

    Please please PLEASE! Use code boxes, I'm not even going to bother attempting to read your code if you don't use code boxes (preferably set it to Java syntax as well).
     
Thread Status:
Not open for further replies.

Share This Page