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

    Plague

    Oh right, you'd have to use Material.BEDROCK.getId(). Or just make the array "Material[] banned" and get the id inside the loop where you compare.
     
  2. Offline

    eagledude4

    I'm at a loss at what you mean, but I'm going to try and use an integer array and use the .getId() for the materials.

    Code:
    int[] badItems = { Material.BEDROCK.getId() };
                for(ItemStack i : event.getPlayer().getInventory().getContents()){
                for(int b : badItems){
                if(i.contains(b)) {
                    event.getPlayer().sendMessage("You're illegal item has been removed");
                    event.getPlayer().getInventory().remove(b);
                }
                }
            }
    I'm getting the error "the method contains(int) is undefined for the type ItemStack".
    Should I use something other than contains?
     
  3. Offline

    Edward Hand

    Replace
    Code:
    if(i.contains(b))
    with
    Code:
    if(i.getTypeId()==b)
     
  4. Offline

    eagledude4

    Aha! Thank you.
    --- merged: Feb 21, 2011 6:59 PM ---
    While I'm here, what's an easy way to include an amount arg in itemstacks?

    I'm writing another plugin to give 64 of whatever you have in your item held slot.

    Code:
    public void onPlayerCommand(PlayerChatEvent event) {
        Player player = event.getPlayer();
        ItemStack stack = new ItemStack(event.getPlayer().getItemInHand(), 64);
        String[] split = event.getMessage().split(" ");
            if (!player.isOp() && split[0].equalsIgnoreCase("/multiply")) {
                event.getPlayer().getInventory().addItem(stack);
            }
        }
    itemstack, int is undefined, and I can't figure out how to include an amount in the arguments.
     
  5. Offline

    Edward Hand

    change
    Code:
    ItemStack stack = new ItemStack(event.getPlayer().getItemInHand(), 64);
    to
    Code:
    ItemStack stack = new ItemStack(event.getPlayer().getItemInHand().getType(), 64);
     
  6. Offline

    eagledude4

    Damit, saved me with the get type again XD I keep forgetting about it >.> Anyways, thanks alot.
     
  7. Offline

    Plague

    Really, just point the mouse in eclipse and read what the argument type is. Then check what type you input and there you have it, just try .get and autocomplete helps you the rest fo the way, that's how I got into it anyway :)

    Now I meant to use the array this way:
    Code:
    Material[] badItems = { Material.BEDROCK };
    for(ItemStack i : event.getPlayer().getInventory().getContents()){
       for(Material b : badItems){
          if(i == b) {
              event.getPlayer().sendMessage("You're illegal item has been removed");
          }
       }
    }
    It's written from the top of my head, so maybe it's not working, but the idea is what I'm trying to post :)
     
  8. Offline

    Edward Hand

    I believe
    Code:
    if(i == b) {
    should be
    Code:
    if(i.getType().equals(b)) {
    (just to avoid another "ITS NOT WORKING FOR MEH" later)
     
  9. Offline

    eagledude4

    With the help of you guys, i threw this together without errors:

    Code:
    public void onPlayerPickupItem(PlayerItemEvent event) {
            int[] badItems = { Material.BEDROCK.getId() };
                for(ItemStack i : event.getPlayer().getInventory().getContents()){
                for(int b : badItems){
                if(i.getTypeId() == b) {
                    event.getPlayer().sendMessage("You're illegal item has been removed");
                    event.getPlayer().getInventory().remove(b);
                }
                }
            }
        }
    but when i pickup a piece of bedrock, it doesnt get rid of it
     
  10. Offline

    Edward Hand

    Just out of interest, why are you even doing it like this?

    It would be much easier to do:
    Code:
    public void onPlayerPickupItem(PlayerPickupItemEvent event) {
            int[] badItems = { Material.BEDROCK.getId() };
            for(int b : badItems){
                if(event.getItem().getTypeId() == b) {
                    event.getPlayer().sendMessage("You're illegal item has been removed");
                    event.setCancelled(true);
                    return;
                }
            }
        }
    if all you want to do is make them not be able to pick up certain items.

    But that's besides the point. Your function takes the wrong type of argument.

    Change:
    Code:
    public void onPlayerPickupItem(PlayerItemEvent event) {
    to:
    Code:
    public void onPlayerPickupItem(PlayerPickupItemEvent event) {
     
  11. Offline

    eagledude4

    "PlayerPickupItemEvent can't be resolved to a type" and there's no suggestions that I think will work. I also looked through the javadocs earlier and did not see PlayerPickupitemEvent.

    The code I had last was lagging the server. Alot.

    I was doing it like doing things that im somewhat familiar with. I had no clue I could cancel the event entirely like the above code.
     
  12. Offline

    Plague

  13. Offline

    eagledude4

    I don't have it in imports. I guess I should download the new bukkit and add it lol.

    EDIT: This link has been down for 2 days now: http://ci.bukkit.org/browse/BUKKIT
     
  14. Offline

    Edward Hand

  15. Offline

    Plague

  16. Offline

    eagledude4

    Okay, I managed to import the event, but how do I fix getTypeId is undefined for type item? For the line:
    Code:
    if(event.getItem().getTypeId() == b) {

    Also, I added PLAYER_PICKUP_ITEM in the regiter events, do I still need PLAYER_ITEM?
     
  17. Offline

    Edward Hand

  18. Offline

    csma-cd

    FFFFFUUUUUUUUUUUU!!!!!!!!!!!!!!!!!!!!
     
  19. Offline

    Plague

    Yes, because letting people write that way is the good thing to do?
     
  20. Offline

    csma-cd

    I'dbetter shoot the person in the forehead than talk to him like this... that was REALLY mean... :p
     
  21. Offline

    Plague

    Why? Is is not true that writing gazillion IFs instead of a LOOP is just stinking bad?

    And I didn't just say this, I did tell him how to make it better.
     
  22. Offline

    Grum Bukkit Team Member

    Code:
    getInventory().removeItem( new ItemStack( Material.WATER, 10000), new ItemStack( Material.LAVA, 10000), new ItemStack( Material.xxxxx, fake_high_amount) );
    
     
  23. Offline

    eagledude4

    I knew it wasnt right to code it like that, but I thought it was a simple, yet crappy fix. I didn't read up on enough java to understand how to use loops properly and effectively.

    I'm not going to continue with this plugin because it will most likely lagg my server alot by constantly checking the inventory.

    I don't want to start a new topic because its an incredibly easy solution, but how do I get rid of the new warning "incredibly long constructor blahblahblah"? I would probably take a look at another plugins source code and do it myself but I don't know which plugins have fixed this. Eclipse isn't giving me any deprecation warnings about it either, so..
    --- merged: Feb 25, 2011 12:29 AM ---
    It wasn't mean, and how is ending one's life better than giving them constructive criticism?
     
  24. Offline

    Plague

    Thanks eagledude4 ;)

    As for the constructor, just remove it, you don't really need one, everything that needs initialization can be done in onEnable (if that's the proper name of the function).
     
  25. Offline

    eagledude4

    I also have a warning that says im registering events before they're called or something? How would one fix that?
     
  26. Offline

    Nohup

    I know you said that you aren't continuing with this, but in case you are interested I will paste an alternative to the looping for you that may be easier to read:

    Code:
        @Override
        public void onPlayerPickupItem(PlayerPickupItemEvent event)
        {
            switch (event.getItem().getItemStack().getType())
            {
                case BEDROCK:
                case WATER:
                case STATIONARY_WATER:
                case LAVA:
                case STATIONARY_LAVA:
                case TNT:
                case OBSIDIAN:
                case FIRE:
                case MOB_SPAWNER:
                case FLINT_AND_STEEL:
                case WATER_BUCKET:
                case LAVA_BUCKET:
                    event.getPlayer().sendMessage("You can't pick that item up.");
                    event.setCancelled(true);
                    break;
                default:
                    event.setCancelled(false);
            }
        }
    
    
    So basically this statement says if the Material (return type for the getType method above) that the ItemStack is made of is any of the ones listed between BEDROCK and LAVA_BUCKET then don't let the player pick them up, otherwise it is okay. The break statement is the only "tricky" part here in that if you don't have it then it would process both switch blocks. Also, in this case the statement automatically recognizes the Material enumeration and allows you to use the entries in a natural way.

    Anyways, just thought I would offer up an alternative view in case you find use for it.
     
  27. Offline

    Plague

    Well where do you register them? I do it in the onEnable() method.
     
  28. Offline

    eagledude4

    Sorry, I misread the error. It's not my plugin that's giving me the warning.
     
Thread Status:
Not open for further replies.

Share This Page