getEntities without mobs

Discussion in 'Plugin Development' started by Jesspersly, Sep 11, 2019.

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

    Jesspersly

    Hey,
    I'm trying to make a simple "Clear Lag" plugin that just cleans all dropped items in the world for personal exercising,
    I've created this by far:
    Code:
    World world = getServer().getWorld("world"); 
    List<Entity> entList = world.getEntities(); 
    if(entList.isEmpty()) {
    p.sendMessage("No entities to remove!"); 
     return true;
    }
    int entListSize = entList.size(); 
     for (Entity current : entList) { 
    if (current instanceof Item) { 
    p.sendMessage(entList.toString());
    current.remove(); 
    }
    }
    p.sendMessage(ChatColor.BOLD + "" + ChatColor.GREEN + "" + entListSize + " Entities Removed!"); 
     return true;
    }
    
    Thing is, the size of the list contains the mobs in the world too, so whenever I get the "Entities removed" message it's like 100's of mobs.
    Is there any way I can only get the dropped items entities amount?
     
  2. Online

    timtower Administrator Administrator Moderator

  3. Offline

    Jesspersly

  4. Online

    timtower Administrator Administrator Moderator

    @Jesspersly It is a template method
    getEntitiesByClass<Item>()
     
  5. Offline

    Jesspersly

    It gives me an error with the <Item>
     
  6. Online

    timtower Administrator Administrator Moderator

    Hover your mouse over it, what error?
     
  7. Offline

    Jesspersly

    Expression Expected
     
  8. Online

    timtower Administrator Administrator Moderator

    @Jesspersly Can you post your code?
    Nevermind the code, argument is the class, not a template method.
    My bad.
     
  9. Offline

    Jesspersly

    Code:
            if (!(sender instanceof Player)) {
                sender.sendMessage("Player use only!");
                return false;
            }
            Player p = (Player) sender;
            World world = getServer().getWorld("world");                   
            List<Entity> entitiesList = world.getEntities();                   
            if(entitiesList.isEmpty()) {
                p.sendMessage("No entities to remove!");
                return false;
            }
            int entitiesListSize = entitiesList.size();
            for (Entity current : entitiesList) {                       
                if (current instanceof Item) {                     
                    p.sendMessage(entitiesList.toString());
                    current.remove();                               
                }
            }
            p.sendMessage(ChatColor.BOLD + "" + ChatColor.GREEN + "" + entitiesListSize + " Entities Removed!");
            return false;
    
    this is the current code, I'm trying to make it so it only gets the dropped items on the ground, removing them is working, but the number of entities includes the number of mobs too, which I don't want
     
  10. Online

    timtower Administrator Administrator Moderator

    @Jesspersly entitiesList = world.getEntitiesOfClass(Item.class)
    Or might be just Item
     
  11. Offline

    Jesspersly

    That means it doesn't have to be a list right?
     
  12. Online

    timtower Administrator Administrator Moderator

    @Jesspersly It means that I am too lazy to retype the List<Entity> part.
     
  13. Offline

    Jesspersly

    I didn't mean that, I know you were just typing what it was supposed to be, but logically, if the variable gets all of the item entities, it's not supposed to be a list, just a Collection.
     
  14. Online

    timtower Administrator Administrator Moderator

    That is correct.
    Guess what list implements.
     
  15. Offline

    Jesspersly

    it seems like it catches stacked items as 1 entity, is there any way to change it?
     
  16. Online

    timtower Administrator Administrator Moderator

    They are 1 entity.
     
  17. Offline

    KarimAKL

    @Jesspersly Think of stacked items as 1 itemstack in the form of an entity; it has an amount. Use Item#getItemStack() if you want to use methods from the ItemStack class.
     
Thread Status:
Not open for further replies.

Share This Page