Solved Removing certain drops from game given drop name

Discussion in 'Plugin Development' started by OneBillionAndOne, Nov 9, 2015.

Thread Status:
Not open for further replies.
  1. Alright, so ive got a CTF plugin, and what im trying to do is at the end of the game, or when all players leave the game, or the server stops/reloads, it removes the flags. At the beginning of the game, on the first player join, it spawns two flags, a red one and a blue one with metadata naming them "Redflag" and "Blueflag". My code on reload and at the end of the game is giving a null pointer exception, however. Even more odd, one version of the plugin, on my laptop, is not giving an error using almost identical code, at least from spawning and removing, but on my desktop, it gives the error. Here is my flag removal code:
    Code:
            if (!Main.currentarena.isEmpty()){
            List<Entity> entlist = Bukkit.getWorld(this.getConfig().getString(Main.currentarena.get("arena")+".redfs.w")).getEntities();
            for (Entity current : entlist) {
                if (current instanceof Item) {
    // Null pointer exception occurs here:
                    if(((Item) current).getItemStack().getItemMeta().getDisplayName().equals("Redflag")) {
                        current.remove();
                    }else if(((Item) current).getItemStack().getItemMeta().getDisplayName().equals("Blueflag")) {
                        current.remove();
                    }
                }
            }
        }else{
            Bukkit.broadcastMessage("test");
        }    
    My spawning code it relatively simple:
    Code:
            ItemStack redflag = new ItemStack(Material.WOOL, 1,DyeColor.RED.getData());
            ItemStack blueflag = new ItemStack(Material.WOOL, 1,DyeColor.BLUE.getData());
    
    // SPACER
    
            ItemMeta rflag = redflag.getItemMeta();
            ItemMeta bflag = blueflag.getItemMeta();
    
    // SPACER
    
                rflag.setDisplayName("Redflag");
                redflag.setItemMeta(rflag);
                bflag.setDisplayName("Blueflag");
                blueflag.setItemMeta(bflag);
    
    // SPACER
    
                            Bukkit.getWorld(main.getConfig().getString(Main.currentarena.get("arena")+".redfs.w")).dropItemNaturally(RedFlag, redflag).setVelocity(new Vector(0D, 0D, 0D));
                            Bukkit.getWorld(main.getConfig().getString(Main.currentarena.get("arena")+".bluefs.w")).dropItemNaturally(BlueFlag, blueflag).setVelocity(new Vector(0D, 0D, 0D));
    
    Can anyone help? It seems like such a simple problem. Im assuming its an error in the removal portion of the code. The flags spawn without issue and are named correctly.
     
  2. Offline

    Orange Tabby

    @OneBillionAndOne Too fix that error try recasting the entities to an item
    Code:
    if (entities == Item) {
    Item item = (Item) entities
    item.remove()
    }
    @OneBillionAndOne This is my opinion/how I would remove it
    Code:
    List<Item> mylist = new List<>;
    
    Item redFlag Item = bukkit.getWorld("yourWorld").dropItem(put stuff here)
    
    mylist.add(redFlagItem);
    
    // end of game
    
    for (Item item : mylist) {
    item.remove();
    }
    Edited: I know I didn't set that List up right :D

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Nov 9, 2015
  3. This still results in a null pointer exception.

    New code:
    Code:
            if (!Main.currentarena.isEmpty()){
            List<Entity> entlist = Bukkit.getWorld(this.getConfig().getString(Main.currentarena.get("arena")+".redfs.w")).getEntities();
            for (Entity current : entlist) {
                if (current instanceof Item) {
                    Item currenti = (Item) current;
                    if((currenti).getItemStack().getItemMeta().getDisplayName().equals("Redflag")) {
                        currenti.remove();
                    }else if((currenti).getItemStack().getItemMeta().getDisplayName().equals("Blueflag")) {
                        currenti.remove();
                    }
                }
            }
     
  4. Offline

    Lordloss

    Bevore getting the itemMeta you have to check if an itemMeta exists with .hasItemMeta()
     
  5. Offline

    mcdorli

  6. Offline

    Lordloss

    Oh okay maybe i messed this up in my brain, lol. But for what do you use the hasItemMeta if every itemStack has one?
     
  7. Offline

    SuperSniper

    @Lordloss You're able to remove ItemMeta from an ItemStack by setting it to null
     
  8. Offline

    mcdorli

    And if you call the getItemMeta method, and currently the meta is null, then it creates a new one.
     
  9. Ah of course. Thank you! That would make a lot more sense as to why it was throwing an exception. I'll try this out when I get home tonight but for now I will mark this as solved.

    Sorry for the extra bump but I got home and tried what @mcdorli had suggested and it didn't exactly work. But I was able to tweak the code to work. So I figured I would post the final code.
    Code:
            List<Entity> entlist = Bukkit.getWorld(this.getConfig().getString(Main.currentarena.get("arena")+".redfs.w")).getEntities();
            for (Entity current : entlist) {
                if (current instanceof Item) {
                    try{
                    if (((Item) current).getItemStack().getItemMeta().getDisplayName().equals("Redflag") || ((Item) current).getItemStack().getItemMeta().getDisplayName().equals("Blueflag")){
                            current.remove();
                    }
                    } catch (NullPointerException e){
                        // One of the flags is gone.
                    }
                }
            }
    Sorry its a little messy but im quite happy I finally got this bug fixed.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Nov 10, 2015
Thread Status:
Not open for further replies.

Share This Page