Improved entitywiper. Fixes #512

This commit is contained in:
JeromSar 2016-08-26 12:34:18 +02:00
parent b153debdef
commit 6a86f66cf6
3 changed files with 73 additions and 39 deletions

View File

@ -1,15 +1,18 @@
package me.totalfreedom.totalfreedommod; package me.totalfreedom.totalfreedommod;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Map;
import me.totalfreedom.totalfreedommod.config.ConfigEntry; import me.totalfreedom.totalfreedommod.config.ConfigEntry;
import me.totalfreedom.totalfreedommod.util.FUtil; import me.totalfreedom.totalfreedommod.util.FUtil;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.Chunk;
import org.bukkit.World; import org.bukkit.World;
import org.bukkit.block.BlockState; import org.bukkit.block.BlockState;
import org.bukkit.entity.AreaEffectCloud; import org.bukkit.entity.AreaEffectCloud;
import org.bukkit.entity.ArmorStand;
import org.bukkit.entity.Boat; import org.bukkit.entity.Boat;
import org.bukkit.entity.EnderCrystal; import org.bukkit.entity.EnderCrystal;
import org.bukkit.entity.EnderSignal; import org.bukkit.entity.EnderSignal;
@ -34,7 +37,8 @@ import org.bukkit.scheduler.BukkitTask;
public class EntityWiper extends FreedomService public class EntityWiper extends FreedomService
{ {
private static final long WIPE_RATE = 5 * 20L; public static final long WIPE_RATE = 5 * 20L;
public static final int CHUNK_ENTITY_MAX = 30;
// //
private final List<Class<? extends Entity>> wipables = new ArrayList<>(); private final List<Class<? extends Entity>> wipables = new ArrayList<>();
// //
@ -53,6 +57,9 @@ public class EntityWiper extends FreedomService
wipables.add(ThrownPotion.class); wipables.add(ThrownPotion.class);
wipables.add(ThrownExpBottle.class); wipables.add(ThrownExpBottle.class);
wipables.add(AreaEffectCloud.class); wipables.add(AreaEffectCloud.class);
wipables.add(Minecart.class);
wipables.add(Boat.class);
wipables.add(FallingBlock.class);
} }
@Override @Override
@ -69,7 +76,7 @@ public class EntityWiper extends FreedomService
@Override @Override
public void run() public void run()
{ {
wipeEntities(!ConfigEntry.ALLOW_EXPLOSIONS.getBoolean(), false); wipeEntities();
} }
}.runTaskTimer(plugin, WIPE_RATE, WIPE_RATE); }.runTaskTimer(plugin, WIPE_RATE, WIPE_RATE);
@ -82,32 +89,11 @@ public class EntityWiper extends FreedomService
wipeTask = null; wipeTask = null;
} }
public boolean canWipe(Entity entity, boolean wipeExplosives, boolean wipeVehicles) public boolean isWipeable(Entity entity)
{ {
if (wipeExplosives) for (Class<? extends Entity> c : wipables)
{ {
if (Explosive.class.isAssignableFrom(entity.getClass())) if (c.isAssignableFrom(entity.getClass()))
{
return true;
}
}
if (wipeVehicles)
{
if (Boat.class.isAssignableFrom(entity.getClass()))
{
return true;
}
else if (Minecart.class.isAssignableFrom(entity.getClass()))
{
return true;
}
}
Iterator<Class<? extends Entity>> it = wipables.iterator();
while (it.hasNext())
{
if (it.next().isAssignableFrom(entity.getClass()))
{ {
return true; return true;
} }
@ -116,22 +102,70 @@ public class EntityWiper extends FreedomService
return false; return false;
} }
public int wipeEntities(boolean wipeExplosives, boolean wipeVehicles) public int wipeEntities()
{ {
int removed = 0; int removed = 0;
Iterator<World> worlds = Bukkit.getWorlds().iterator(); Iterator<World> worlds = Bukkit.getWorlds().iterator();
while (worlds.hasNext()) while (worlds.hasNext())
{ {
Iterator<Entity> entities = worlds.next().getEntities().iterator(); removed += wipeEntities(worlds.next());
while (entities.hasNext()) }
return removed;
}
public int wipeEntities(World world)
{
int removed = 0;
boolean wipeExpl = ConfigEntry.ALLOW_EXPLOSIONS.getBoolean();
Iterator<Entity> entities = world.getEntities().iterator();
// Organise the entities in the world
Map<Chunk, List<Entity>> cem = new HashMap<>();
while (entities.hasNext())
{
final Entity entity = entities.next();
// Explosives
if (wipeExpl && Explosive.class.isAssignableFrom(entity.getClass()))
{ {
Entity entity = entities.next(); entity.remove();
if (canWipe(entity, wipeExplosives, wipeVehicles)) removed++;
{ }
entity.remove();
removed++; // Only wipeable entities can be wiped (duh!)
} if (!isWipeable(entity))
{
continue;
}
Chunk c = entity.getLocation().getChunk();
List<Entity> cel = cem.get(c);
if (cel == null)
{
cem.put(c, new ArrayList<>(Arrays.asList(entity)));
}
else
{
cel.add(entity);
}
}
// Now purge the entities if necessary
for (Chunk c : cem.keySet())
{
List<Entity> cel = cem.get(c);
if (cel.size() < CHUNK_ENTITY_MAX)
{
continue;
}
// Too many entities in this chunk, wipe them all
for (Entity e : cel)
{
e.remove();
} }
} }

View File

@ -15,7 +15,7 @@ public class Command_entitywipe extends FreedomCommand
public boolean run(CommandSender sender, Player playerSender, Command cmd, String commandLabel, String[] args, boolean senderIsConsole) public boolean run(CommandSender sender, Player playerSender, Command cmd, String commandLabel, String[] args, boolean senderIsConsole)
{ {
FUtil.adminAction(sender.getName(), "Removing all server entities.", true); FUtil.adminAction(sender.getName(), "Removing all server entities.", true);
msg((plugin.ew.wipeEntities(true, true)) + " entities removed."); msg((plugin.ew.wipeEntities()) + " entities removed.");
return true; return true;
} }

View File

@ -19,7 +19,7 @@ public class Command_purgeall extends FreedomCommand
FUtil.adminAction(sender.getName(), "Purging all player data", true); FUtil.adminAction(sender.getName(), "Purging all player data", true);
// Purge entities // Purge entities
plugin.ew.wipeEntities(true, true); plugin.ew.wipeEntities();
for (Player player : server.getOnlinePlayers()) for (Player player : server.getOnlinePlayers())
{ {