2015-11-15 23:32:04 +00:00
|
|
|
package me.totalfreedom.totalfreedommod;
|
|
|
|
|
2018-07-31 07:05:28 +00:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Arrays;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Iterator;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Map;
|
2018-06-10 19:32:59 +00:00
|
|
|
import lombok.Getter;
|
2015-11-15 23:32:04 +00:00
|
|
|
import me.totalfreedom.totalfreedommod.config.ConfigEntry;
|
|
|
|
import me.totalfreedom.totalfreedommod.util.FUtil;
|
|
|
|
import org.bukkit.Bukkit;
|
2016-08-26 10:34:18 +00:00
|
|
|
import org.bukkit.Chunk;
|
2015-11-15 23:32:04 +00:00
|
|
|
import org.bukkit.World;
|
2018-07-31 07:05:28 +00:00
|
|
|
import org.bukkit.entity.AreaEffectCloud;
|
|
|
|
import org.bukkit.entity.ArmorStand;
|
|
|
|
import org.bukkit.entity.Boat;
|
|
|
|
import org.bukkit.entity.DragonFireball;
|
|
|
|
import org.bukkit.entity.EnderCrystal;
|
|
|
|
import org.bukkit.entity.EnderPearl;
|
|
|
|
import org.bukkit.entity.EnderSignal;
|
|
|
|
import org.bukkit.entity.Entity;
|
|
|
|
import org.bukkit.entity.ExperienceOrb;
|
|
|
|
import org.bukkit.entity.Explosive;
|
|
|
|
import org.bukkit.entity.FallingBlock;
|
|
|
|
import org.bukkit.entity.Fireball;
|
|
|
|
import org.bukkit.entity.Firework;
|
|
|
|
import org.bukkit.entity.Item;
|
|
|
|
import org.bukkit.entity.Minecart;
|
|
|
|
import org.bukkit.entity.Projectile;
|
|
|
|
import org.bukkit.entity.ThrownExpBottle;
|
|
|
|
import org.bukkit.entity.ThrownPotion;
|
2016-05-12 19:40:39 +00:00
|
|
|
import org.bukkit.event.EventHandler;
|
|
|
|
import org.bukkit.event.EventPriority;
|
2016-08-26 10:54:53 +00:00
|
|
|
import org.bukkit.event.entity.ItemSpawnEvent;
|
2018-06-10 19:32:59 +00:00
|
|
|
import org.bukkit.event.player.PlayerDropItemEvent;
|
2015-11-15 23:32:04 +00:00
|
|
|
import org.bukkit.scheduler.BukkitRunnable;
|
|
|
|
import org.bukkit.scheduler.BukkitTask;
|
|
|
|
|
2016-03-01 16:47:01 +00:00
|
|
|
public class EntityWiper extends FreedomService
|
2015-11-15 23:32:04 +00:00
|
|
|
{
|
2015-11-22 18:26:47 +00:00
|
|
|
|
2018-06-10 19:32:59 +00:00
|
|
|
@Getter
|
|
|
|
private boolean enabled;
|
2016-08-26 10:54:53 +00:00
|
|
|
public static final long ENTITY_WIPE_RATE = 5 * 20L;
|
|
|
|
public static final long ITEM_DESPAWN_RATE = 20L * 20L;
|
2016-10-02 20:42:36 +00:00
|
|
|
public static final int CHUNK_ENTITY_MAX = 20;
|
2015-11-15 23:32:04 +00:00
|
|
|
//
|
2016-03-02 19:28:01 +00:00
|
|
|
private final List<Class<? extends Entity>> wipables = new ArrayList<>();
|
2015-11-15 23:32:04 +00:00
|
|
|
//
|
|
|
|
private BukkitTask wipeTask;
|
|
|
|
|
|
|
|
public EntityWiper(TotalFreedomMod plugin)
|
|
|
|
{
|
|
|
|
super(plugin);
|
|
|
|
wipables.add(EnderCrystal.class);
|
|
|
|
wipables.add(EnderSignal.class);
|
2018-01-02 02:46:35 +00:00
|
|
|
wipables.add(EnderPearl.class);
|
2015-11-15 23:32:04 +00:00
|
|
|
wipables.add(ExperienceOrb.class);
|
|
|
|
wipables.add(Projectile.class);
|
|
|
|
wipables.add(FallingBlock.class);
|
|
|
|
wipables.add(Firework.class);
|
|
|
|
wipables.add(Item.class);
|
2016-05-12 19:40:39 +00:00
|
|
|
wipables.add(ThrownPotion.class);
|
|
|
|
wipables.add(ThrownExpBottle.class);
|
|
|
|
wipables.add(AreaEffectCloud.class);
|
2016-08-26 10:34:18 +00:00
|
|
|
wipables.add(Minecart.class);
|
|
|
|
wipables.add(Boat.class);
|
|
|
|
wipables.add(FallingBlock.class);
|
2016-10-02 21:05:03 +00:00
|
|
|
wipables.add(ArmorStand.class);
|
2018-01-02 02:46:35 +00:00
|
|
|
wipables.add(Fireball.class);
|
|
|
|
wipables.add(DragonFireball.class);
|
2015-11-15 23:32:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onStart()
|
|
|
|
{
|
|
|
|
if (!ConfigEntry.AUTO_ENTITY_WIPE.getBoolean())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
wipeTask = new BukkitRunnable()
|
|
|
|
{
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void run()
|
|
|
|
{
|
2016-10-02 20:42:36 +00:00
|
|
|
wipeEntities(false);
|
2015-11-15 23:32:04 +00:00
|
|
|
}
|
2016-08-26 10:54:53 +00:00
|
|
|
}.runTaskTimer(plugin, ENTITY_WIPE_RATE, ENTITY_WIPE_RATE);
|
2015-11-15 23:32:04 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onStop()
|
|
|
|
{
|
|
|
|
FUtil.cancel(wipeTask);
|
|
|
|
wipeTask = null;
|
|
|
|
}
|
|
|
|
|
2016-08-26 10:34:18 +00:00
|
|
|
public boolean isWipeable(Entity entity)
|
2015-11-15 23:32:04 +00:00
|
|
|
{
|
2016-08-26 10:34:18 +00:00
|
|
|
for (Class<? extends Entity> c : wipables)
|
2015-11-15 23:32:04 +00:00
|
|
|
{
|
2016-08-26 10:34:18 +00:00
|
|
|
if (c.isAssignableFrom(entity.getClass()))
|
2015-11-15 23:32:04 +00:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-26 10:34:18 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-10-02 20:42:36 +00:00
|
|
|
public int wipeEntities(boolean force)
|
2016-08-26 10:34:18 +00:00
|
|
|
{
|
|
|
|
int removed = 0;
|
|
|
|
Iterator<World> worlds = Bukkit.getWorlds().iterator();
|
|
|
|
while (worlds.hasNext())
|
2015-11-15 23:32:04 +00:00
|
|
|
{
|
2016-10-02 20:42:36 +00:00
|
|
|
removed += wipeEntities(worlds.next(), force);
|
2016-08-26 10:34:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return removed;
|
|
|
|
}
|
|
|
|
|
2016-10-02 20:42:36 +00:00
|
|
|
public int wipeEntities(World world, boolean force)
|
2016-08-26 10:34:18 +00:00
|
|
|
{
|
|
|
|
int removed = 0;
|
|
|
|
|
2019-07-11 02:13:57 +00:00
|
|
|
boolean wipeExpl = !ConfigEntry.ALLOW_EXPLOSIONS.getBoolean();
|
2016-08-26 10:34:18 +00:00
|
|
|
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()))
|
2015-11-15 23:32:04 +00:00
|
|
|
{
|
2016-08-26 10:34:18 +00:00
|
|
|
entity.remove();
|
|
|
|
removed++;
|
2015-11-15 23:32:04 +00:00
|
|
|
}
|
2016-08-26 10:34:18 +00:00
|
|
|
|
|
|
|
// Only wipeable entities can be wiped (duh!)
|
|
|
|
if (!isWipeable(entity))
|
2015-11-15 23:32:04 +00:00
|
|
|
{
|
2016-08-26 10:34:18 +00:00
|
|
|
continue;
|
2015-11-15 23:32:04 +00:00
|
|
|
}
|
|
|
|
|
2016-08-26 10:34:18 +00:00
|
|
|
Chunk c = entity.getLocation().getChunk();
|
|
|
|
List<Entity> cel = cem.get(c);
|
|
|
|
if (cel == null)
|
2015-11-15 23:32:04 +00:00
|
|
|
{
|
2016-08-26 10:34:18 +00:00
|
|
|
cem.put(c, new ArrayList<>(Arrays.asList(entity)));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
cel.add(entity);
|
2015-11-15 23:32:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-26 10:34:18 +00:00
|
|
|
// Now purge the entities if necessary
|
|
|
|
for (Chunk c : cem.keySet())
|
|
|
|
{
|
|
|
|
List<Entity> cel = cem.get(c);
|
2015-11-15 23:32:04 +00:00
|
|
|
|
2016-10-02 20:42:36 +00:00
|
|
|
if (!force && cel.size() < CHUNK_ENTITY_MAX)
|
2016-08-26 10:34:18 +00:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
2015-11-15 23:32:04 +00:00
|
|
|
|
2016-08-26 10:34:18 +00:00
|
|
|
// Too many entities in this chunk, wipe them all
|
|
|
|
for (Entity e : cel)
|
2015-11-15 23:32:04 +00:00
|
|
|
{
|
2016-08-26 10:34:18 +00:00
|
|
|
e.remove();
|
2018-01-02 02:46:35 +00:00
|
|
|
removed++;
|
2015-11-15 23:32:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return removed;
|
|
|
|
}
|
|
|
|
|
2016-08-26 10:54:53 +00:00
|
|
|
@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
|
|
|
|
public void onItemSpawn(ItemSpawnEvent event)
|
2016-05-12 19:40:39 +00:00
|
|
|
{
|
2016-08-26 10:54:53 +00:00
|
|
|
final Item entity = event.getEntity();
|
2016-05-12 19:40:39 +00:00
|
|
|
|
2016-08-26 10:54:53 +00:00
|
|
|
new BukkitRunnable()
|
2016-05-12 19:40:39 +00:00
|
|
|
{
|
|
|
|
|
2016-08-26 10:54:53 +00:00
|
|
|
@Override
|
|
|
|
public void run()
|
|
|
|
{
|
|
|
|
entity.remove();
|
|
|
|
}
|
|
|
|
}.runTaskLater(plugin, ITEM_DESPAWN_RATE);
|
|
|
|
|
2016-05-12 19:40:39 +00:00
|
|
|
}
|
|
|
|
|
2018-06-10 19:32:59 +00:00
|
|
|
@EventHandler
|
|
|
|
public void onDrop(PlayerDropItemEvent event)
|
|
|
|
{
|
|
|
|
enabled = ConfigEntry.AUTO_ENTITY_WIPE.getBoolean();
|
|
|
|
if (enabled)
|
|
|
|
{
|
|
|
|
event.setCancelled(true);
|
|
|
|
}
|
|
|
|
}
|
2015-11-15 23:32:04 +00:00
|
|
|
}
|