Getting an array of items in the inventory

Discussion in 'Plugin Development' started by eagledude4, Feb 18, 2011.

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

    eagledude4

    I'm making a plugin that basically removes the Items I define from the inventory.

    Code:
    public void onPlayerPickupItem(PlayerInventoryEvent e) {
        Player player = e.getPlayer();
        int[] badItems = {8,7,9,10,11,12,46,49,51,52,259,326,327};
            if (e.getPlayer().getInventory().contains(badItems)) {
                e.getPlayer().getInventory().remove(badItems);
                e.getPlayer().sendMessage("You have acquired an item that is not allowed, and has been removed from your inventory.");
            }
        }
    }
    I'm getting warning that are telling me that I'm not using the right methods for the arguments (the array). What methods do I need to use?
     
  2. Offline

    nossr50

    You can't do it like that
    The inventory contains Items yeah, but they are not ints they are 'ItemStack'
    If you want to see if they are the bad items you run a for loop to check every ItemStack in the inventory for having a bad ID

    Code:
    for (ItemStack herp : getPlayer().getInventory()){
        if(herp.getTypeId() == badItemId || herp.getTypeId() == anotherBadItemId) {
        functionForRemovingBadItems();
        }
    }
    This would work, but you might just want to make a boolean function that returns true or false for an int if it matches your bad item ids instead of making if (this || or this || or this too) kind of checks. I'm still a newbie so I could be wrong on a few things.

    EDIT: and player.getInventory() does return an array, but its an ItemStack array
     
  3. Offline

    eagledude4

    I'm not quite sure how to do that (I'm new too)

    I think this is kind of dumb of me but I did this instead:
    Code:
    public void onPlayerPickupItem(PlayerInventoryEvent e) {
        ItemStack bedrock = e.getPlayer().getInventory().getItem(7);
        ItemStack water1 = e.getPlayer().getInventory().getItem(8);
        ItemStack water2 = e.getPlayer().getInventory().getItem(9);
        ItemStack lava1 = e.getPlayer().getInventory().getItem(10);
        ItemStack lava2 = e.getPlayer().getInventory().getItem(11);
        ItemStack tnt = e.getPlayer().getInventory().getItem(46);
        ItemStack obsidian = e.getPlayer().getInventory().getItem(49);
        ItemStack fire = e.getPlayer().getInventory().getItem(51);
        ItemStack mobspawn = e.getPlayer().getInventory().getItem(52);
        ItemStack lighter = e.getPlayer().getInventory().getItem(259);
        ItemStack waterbucket = e.getPlayer().getInventory().getItem(326);
        ItemStack lavabucket = e.getPlayer().getInventory().getItem(327);
            if (e.getPlayer().getInventory().contains(bedrock)) {
                e.getPlayer().getInventory().removeItem(bedrock);
                e.getPlayer().sendMessage("You have acquired an item that is not allowed, and has been removed from your inventory.");
            }
            if (e.getPlayer().getInventory().contains(water1)) {
                e.getPlayer().getInventory().removeItem(water1);
                e.getPlayer().sendMessage("You have acquired an item that is not allowed, and has been removed from your inventory.");
            }
            if (e.getPlayer().getInventory().contains(water2)) {
                e.getPlayer().getInventory().removeItem(water2);
                e.getPlayer().sendMessage("You have acquired an item that is not allowed, and has been removed from your inventory.");
            }
            if (e.getPlayer().getInventory().contains(lava1)) {
                e.getPlayer().getInventory().removeItem(lava1);
                e.getPlayer().sendMessage("You have acquired an item that is not allowed, and has been removed from your inventory.");
            }
            if (e.getPlayer().getInventory().contains(lava2)) {
                e.getPlayer().getInventory().removeItem(lava2);
                e.getPlayer().sendMessage("You have acquired an item that is not allowed, and has been removed from your inventory.");
            }
            if (e.getPlayer().getInventory().contains(tnt)) {
                e.getPlayer().getInventory().removeItem(tnt);
                e.getPlayer().sendMessage("You have acquired an item that is not allowed, and has been removed from your inventory.");
            }
            if (e.getPlayer().getInventory().contains(obsidian)) {
                e.getPlayer().getInventory().removeItem(obsidian);
                e.getPlayer().sendMessage("You have acquired an item that is not allowed, and has been removed from your inventory.");
            }
            if (e.getPlayer().getInventory().contains(fire)) {
                e.getPlayer().getInventory().removeItem(fire);
                e.getPlayer().sendMessage("You have acquired an item that is not allowed, and has been removed from your inventory.");
            }
            if (e.getPlayer().getInventory().contains(mobspawn)) {
                e.getPlayer().getInventory().removeItem(mobspawn);
                e.getPlayer().sendMessage("You have acquired an item that is not allowed, and has been removed from your inventory.");
            }
            if (e.getPlayer().getInventory().contains(lighter)) {
                e.getPlayer().getInventory().removeItem(lighter);
                e.getPlayer().sendMessage("You have acquired an item that is not allowed, and has been removed from your inventory.");
            }
            if (e.getPlayer().getInventory().contains(waterbucket)) {
                e.getPlayer().getInventory().removeItem(waterbucket);
                e.getPlayer().sendMessage("You have acquired an item that is not allowed, and has been removed from your inventory.");
            }
            if (e.getPlayer().getInventory().contains(lavabucket)) {
                e.getPlayer().getInventory().removeItem(lavabucket);
                e.getPlayer().sendMessage("You have acquired an item that is not allowed, and has been removed from your inventory.");
            }
        }
    }
    This probably isnt suprising but it doesnt work. I tested by spawning in a fire block (51) and it didnt remove the item and give the message

    EDIT: I didn't add the new register event, so I think thats why it didnt work
     
  4. Offline

    Plague

    Oh my god worst code ever....
    I'm not even solving the functionality until you program it like a man and not an animal. Make an array of banned IDs and then in a for loop test them, not zillion ifs with the same code!
     
  5. Offline

    nossr50

    I told you man, you can't do it like that...
     
  6. Offline

    Archelaus

    Code:
    String[] banned = {"lava", "water", "tnt", "bedrock","obsidian", "fire","flint_and_steel","mob"};
    for(ItemStack i : e.getPlayer().getInventory().getContents()){
    for(String s : banned){
    if(i.getType.toString().contains(s)){
    //stuff to do here
    }
    }
    }
    
    

    That should work.
     
  7. Offline

    Acru

    lol!
     
  8. Offline

    eagledude4

    This is the code I have:

    Code:
    public class InvRemovePlayerListener extends PlayerListener {
        public void onPlayerPickupItem(PlayerItemEvent event) {
        String[] banned = {"lava", "water", "tnt", "bedrock","obsidian", "fire","flint_and_steel","mob"};
            for(ItemStack i : event.getPlayer().getInventory().getContents()){
            for(String s : banned){
            if(i.getType.toString().contains(s)) {
                event.getPlayer().sendMessage("You're illegal item has been removed");
                event.getPlayer().getInventory().remove(banned);
            }
        }
    }
    }
    }
    What argument do I use for remove()? also, i.getType can't be resolved.
     
  9. Offline

    Plague

  10. Offline

    eagledude4

    I should of probably figured out that get type needed the brackets on my own, and I propably should of figured out I should of used the variable i for the arguments for remove on my own as well, without eclipse. But yes, I do use it.

    Are the strings for my items case sensative? For example, will water also match water_bucket?

    I also wanted to double check if PluginManager pm = getServer().getPluginManager(); was right
     
  11. Offline

    Plague

    Umm, this is not case sensitivity this is partial matching. And materials do not behave this way only player lookup.

    PluginManager looks OK.
     
  12. Offline

    eagledude4

    When you mean they do not behave this way you mean partial matching does not work for materials? so water would not match water_bucket?

    Also, I'm getting an invalidpluginexception error in the console.
     
  13. Offline

    Plague

    Well water would match water ;) But I understand what you mean and no, materials do not match partially.

    Post the error.
     
  14. Offline

    eagledude4

    where can I get a list of the material names/item names. I'm not sure if water_bucket is correct.

    As for the error, is there a way I can copy and paste the exact error, or do I have to look at what im getting and type it all out. I looked in the server.log (not the server.log.lck) and i didnt see the error
     
  15. Offline

    Plague

    You could always post a screenshot, but even windows console enables you to select text, you have to enable it in options (use the top left button on the window frame to open options).
     
  16. Offline

    eagledude4

    I looked in properties, what do I have to enable?
     
  17. Offline

    Plague

    Huh? I meant screenshot of the console...
     
  18. Offline

    eagledude4

    sorry, I mis interpreted what you said. I edited my reply, but I found out that I had to enable quick edit. Anyway, heres the error:

    Code:
    SEVERE: Could not load plugins\InvRemove.jar in plugins: null
    
    org.bukkit.plugin.InvalidPluginException
    
    at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.j
    
    ava:79)
    
    at org.bukkit.plugin.SimplePluginManager.loadPlugin(SimplePluginManager.
    
    java:117)
    
    at org.bukkit.plugin.SimplePluginManager.loadPlugins(SimplePluginManager
    
    .java:82)
    
    at org.bukkit.craftbukkit.CraftServer.loadPlugins(CraftServer.java:53)
    
    at net.minecraft.server.MinecraftServer.e(MinecraftServer.java:169)
    
    at net.minecraft.server.MinecraftServer.c(MinecraftServer.java:156)
    
    at net.minecraft.server.MinecraftServer.d(MinecraftServer.java:108)
    
    at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:207)
    
    at net.minecraft.server.ThreadServerApplication.run(SourceFile:512)
    
    Caused by: java.lang.ClassNotFoundException: com.bukkit.eagledude4.InvRemove
    
    at java.net.URLClassLoader$1.run(Unknown Source)
    
    at java.security.AccessController.doPrivileged(Native Method)
    
    at java.net.URLClassLoader.findClass(Unknown Source)
    
    at org.bukkit.plugin.java.PluginClassLoader.findClass(PluginClassLoader.
    
    java:30)
    
    at java.lang.ClassLoader.loadClass(Unknown Source)
    
    at java.lang.ClassLoader.loadClass(Unknown Source)
    
    at java.lang.Class.forName0(Native Method)
    
    at java.lang.Class.forName(Unknown Source)
    
    at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.j
    
    ava:73)
     
  19. Offline

    Plague

    Looks like your plugin.yml is wrong, if the package is called "com.bukkit.eagledude4.InvRemove" and you have the main code in "InvRemove.java", the plugin.yml should look like:
    Code:
    name: InvRemove
    main: com.bukkit.eagledude4.InvRemove.InvRemove
    version: 1.0
    If this works, you can edit the name from "com.bukkit..." to something else, as com.bukkit is reserved for official plugins (I know the tutorial said otherwise, it's old).
     
  20. Offline

    eagledude4

    Thanks I had: main: com.bukkit.eagledude4.InvRemove, and not InvRemove.InvRemove
     
  21. Offline

    darknesschaos

    change com.bukkit.... to something else. Soon plugins with that packagename will not load.
    eagledude4.InvRemove.InvRemove will be just fine.
    --- merged: Feb 20, 2011 6:00 AM ---
    oh, and use refactor to simplify things.
     
  22. Offline

    eagledude4

    After I changed the plugin.yml settings, it stopped throwing errors, but I need a reply for my previous question: "where can I get a list of the material names/item names. I'm not sure if water_bucket is correct"
     
  23. Offline

    Edward Hand

  24. Offline

    Plague

    Exactly.

    Now if you don't like the source code itself, you can use minecraft wiki as it looks like they used the names, just put an underscore instead of space.
    http://www.minecraftwiki.net/wiki/Data_values
    I use this because there are pictures :) But of course the source code will be always right and maybe some differences can be found on the wiki.
     
  25. Offline

    eagledude4

    I'd rather use the source code for accuracy. I just finished replacing all the materials, this is how it looks now:

    Code:
    public void onPlayerPickupItem(PlayerItemEvent event) {
        String[] banned = {"BEDROCK", "WATER", "STATIONARY_WATER", "LAVA", "STATIONARY_LAVA", "TNT", "OBSIDIAN", "FIRE", "MOB_SPAWNER", "FLINT_AND_STEEL", "WATER_BUCKET", "LAVA_BUCKET"};
            for(ItemStack i : event.getPlayer().getInventory().getContents()){
            for(String s : banned){
            if(i.getType().toString().contains(s)) {
                event.getPlayer().sendMessage("You're illegal item has been removed");
                event.getPlayer().getInventory().remove(i);
            }
        }  
     
  26. Offline

    DiaAWAY

    if i was you, i'd read up on Enums and how to use them, they're much safer then trying to use strings for this purpose
     
  27. Offline

    eagledude4

    I want to stay away from the complex parts of java if I have to.

    The above code doesn't work. When I pickup WATER (8), and it doesnt give me the message or remove it from the inventory.
     
  28. Offline

    nossr50

    Enums aren't complex, just look em up.
     
  29. Offline

    Plague

    If not enums, just change it to an array of int
    Code:
    int[] banned = { Material.WATER, ... }
    because the string testing is a really slow and bad idea...
     
  30. Offline

    eagledude4

    I'm a little more comfortable with using integers, so I'll go that way until im more comfortable with exploring the functionality of enums.

    int[] badItems = { Material.BEDROCK }; gives me the error cannot convert from material to int,
     
Thread Status:
Not open for further replies.

Share This Page