Removing all Entities on the Ground every 5 Seconds

Discussion in 'Plugin Development' started by MineStein, Jul 19, 2014.

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

    MineStein

    Hey, I am looking to erase all of the dropped tiles on the ground every five seconds. I have my scheduler ready, I just need to actually erase them from the server. What do I do?
     
  2. Offline

    DoctorDark

    MineStein

    Use a repeatingScheduler with a 300 tick delay; Loop through all the entities in a world, check if they are an instance of Item and cast them, then check if the (Item)isOnGround and call the remove method.
     
    izarooni and JustinsCool15 like this.
  3. Offline

    MineStein

    DoctorDark Like this?

    It is not working.

    Code:java
    1. Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
    2. public void run() {
    3.  
    4. for (Entity e : Bukkit.getServer().getWorld("world").getEntities()) {
    5. if (e.getType().equals(EntityType.DROPPED_ITEM)) {
    6. e.remove();
    7. } else {
    8. return;
    9. }
    10. }
    11.  
    12. String[] barMessages = new String[] {
    13. "§5Do §d§l/kit list §5for a list of kits!",
    14. "§9Do §3§l/servers §9for a list of all servers!",
    15. "§2Want to support the server? Do §a§l/store §2to donate!",
    16. "§4Tweet us §c§l@NovaMC_Network §4for help!",
    17. "§8Developed by §7§l@MineStien"
    18. };
    19.  
    20. BarAPI.setMessage(barMessages[random.nextInt(barMessages.length)]);
    21. }
    22. },0 , 100);
    23. }
     
  4. Offline

    LCastr0

    Check the loaded chunks before
     
  5. Offline

    MineStein

    LCastr0 How do I check the loaded chunks?
     
  6. Offline

    LCastr0

    Since I'm not home, I'm not completly sure, but something like
    Bukkit.getWorlds().getLoadedChunks()
    Do a for loop with this method and then do another for loop for all the entities in that chunk. Oh, and also, you could check if it's a dropped item using
    entity instanceof Item
     
  7. Offline

    MineStein

    LCastr0 Ok, thanks! I will try it, I have never had to handle chunks before.
     
  8. Offline

    LCastr0

    Good luck, and tell me if you do it. I'm leaving now, but will be back tomorrow's afternoon (my time); PM me if you need anything, or just Tahg me here and i'll answer tomorrow
     
  9. Offline

    MineStein

  10. Offline

    fireblast709

    MineStein nah, all you need to do is removing that
    Code:java
    1. else
    2. {
    3. return;
    4. }
    the rest of the code was perfectly fine
     
    Zupsub likes this.
  11. Offline

    LCastr0

    It's better to check the chunks though.
     
  12. Offline

    fireblast709

    LCastr0 what is your reasoning behind this though, it would be more exact to loop over all entities of all worlds, and Entity#remove should not trigger a chunk load either
     
  13. Offline

    LCastr0

    Well I think it's safer to check the loaded chunks.
     
  14. Offline

    fireblast709

    LCastr0 still lacking the reasoning ;). If chunks are unloaded the entities are despawned anyway, so world.getEntities() should work perfectly fibe
     
    Garris0n and xTigerRebornx like this.
  15. Offline

    LCastr0

    Well I prefer to do my way, but thanks for teaching me this too :)
     
  16. Offline

    MineStein

    For anyone viewing this thread for a solution, here it is

    Code:java
    1. for (Chunk chunks : Bukkit.getServer().getWorld("world").getLoadedChunks()) {
    2. for (Entity entitiesInChunks : chunks.getEntities()) {
    3. if (entitiesInChunks instanceof Item) {
    4. entitiesInChunks.remove();
    5. }
    6. }
    7. }
     
Thread Status:
Not open for further replies.

Share This Page