TotalFreedomMod/src/main/java/me/totalfreedom/totalfreedommod/ProtectArea.java

393 lines
10 KiB
Java
Raw Normal View History

package me.totalfreedom.totalfreedommod;
import com.google.common.collect.Maps;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.HashMap;
2013-09-05 14:22:59 +00:00
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
2013-09-05 14:22:59 +00:00
import java.util.UUID;
import me.totalfreedom.totalfreedommod.config.ConfigEntry;
import me.totalfreedom.totalfreedommod.util.FLog;
import net.pravian.aero.component.service.AbstractService;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.block.BlockPlaceEvent;
import org.bukkit.util.Vector;
public class ProtectArea extends AbstractService<TotalFreedomMod>
{
public static final double MAX_RADIUS = 50.0;
//
private final Map<String, SerializableProtectedRegion> areas = Maps.newHashMap();
public ProtectArea(TotalFreedomMod plugin)
{
super(plugin);
}
@Override
protected void onStart()
{
if (!ConfigEntry.PROTECTAREA_ENABLED.getBoolean())
{
return;
}
File input = new File(TotalFreedomMod.plugin.getDataFolder(), TotalFreedomMod.PROTECTED_AREA_FILENAME);
try
{
if (input.exists())
{
FileInputStream fis = new FileInputStream(input);
ObjectInputStream ois = new ObjectInputStream(fis);
areas.clear();
areas.putAll((HashMap<String, SerializableProtectedRegion>) ois.readObject());
ois.close();
fis.close();
}
}
catch (Exception ex)
{
input.delete();
FLog.severe(ex);
}
cleanProtectedAreas();
}
@Override
protected void onStop()
{
save();
}
public void save()
{
try
{
FileOutputStream fos = new FileOutputStream(new File(TotalFreedomMod.plugin.getDataFolder(), TotalFreedomMod.PROTECTED_AREA_FILENAME));
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(areas);
oos.close();
fos.close();
}
catch (Exception ex)
{
FLog.severe(ex);
}
}
@EventHandler(priority = EventPriority.NORMAL)
public void onBlockBreak(BlockBreakEvent event)
{
if (!ConfigEntry.PROTECTAREA_ENABLED.getBoolean())
{
return;
}
final Player player = event.getPlayer();
if (plugin.al.isAdmin(player))
{
return;
}
final Location location = event.getBlock().getLocation();
if (isInProtectedArea(location))
{
event.setCancelled(true);
}
}
@EventHandler(priority = EventPriority.NORMAL)
public void onBlockPlace(BlockPlaceEvent event)
{
if (!ConfigEntry.PROTECTAREA_ENABLED.getBoolean())
{
return;
}
final Player player = event.getPlayer();
if (plugin.al.isAdmin(player))
{
return;
}
final Location location = event.getBlock().getLocation();
if (isInProtectedArea(location))
{
event.setCancelled(true);
}
}
public boolean isInProtectedArea(final Location modifyLocation)
{
2013-09-05 14:22:59 +00:00
boolean doSave = false;
boolean inProtectedArea = false;
final Iterator<Map.Entry<String, SerializableProtectedRegion>> it = areas.entrySet().iterator();
2013-09-05 14:22:59 +00:00
while (it.hasNext())
{
2013-09-05 14:22:59 +00:00
final SerializableProtectedRegion region = it.next().getValue();
Location regionCenter = null;
try
{
2013-09-05 14:22:59 +00:00
regionCenter = region.getLocation();
}
catch (SerializableProtectedRegion.CantFindWorldException ex)
{
it.remove();
doSave = true;
continue;
}
2013-09-05 14:22:59 +00:00
if (regionCenter != null)
{
if (modifyLocation.getWorld() == regionCenter.getWorld())
{
final double regionRadius = region.getRadius();
if (modifyLocation.distanceSquared(regionCenter) <= (regionRadius * regionRadius))
{
2013-09-05 14:22:59 +00:00
inProtectedArea = true;
break;
}
}
}
}
2013-09-05 14:22:59 +00:00
if (doSave)
{
save();
2013-09-05 14:22:59 +00:00
}
return inProtectedArea;
}
public boolean isInProtectedArea(final Vector min, final Vector max, final String worldName)
{
2013-09-05 14:22:59 +00:00
boolean doSave = false;
boolean inProtectedArea = false;
final Iterator<Map.Entry<String, SerializableProtectedRegion>> it = areas.entrySet().iterator();
2013-09-05 14:22:59 +00:00
while (it.hasNext())
{
2013-09-05 14:22:59 +00:00
final SerializableProtectedRegion region = it.next().getValue();
Location regionCenter = null;
try
{
regionCenter = region.getLocation();
}
catch (SerializableProtectedRegion.CantFindWorldException ex)
{
2013-09-05 14:22:59 +00:00
it.remove();
doSave = true;
continue;
}
if (regionCenter != null)
{
if (worldName.equals(regionCenter.getWorld().getName()))
{
2013-09-05 14:22:59 +00:00
if (cubeIntersectsSphere(min, max, regionCenter.toVector(), region.getRadius()))
{
2013-09-05 14:22:59 +00:00
inProtectedArea = true;
break;
}
}
}
}
2013-09-05 14:22:59 +00:00
if (doSave)
{
save();
2013-09-05 14:22:59 +00:00
}
return inProtectedArea;
}
private boolean cubeIntersectsSphere(Vector min, Vector max, Vector sphere, double radius)
{
double d = square(radius);
if (sphere.getX() < min.getX())
{
d -= square(sphere.getX() - min.getX());
}
else if (sphere.getX() > max.getX())
{
d -= square(sphere.getX() - max.getX());
}
if (sphere.getY() < min.getY())
{
d -= square(sphere.getY() - min.getY());
}
else if (sphere.getY() > max.getY())
{
d -= square(sphere.getY() - max.getY());
}
if (sphere.getZ() < min.getZ())
{
d -= square(sphere.getZ() - min.getZ());
}
else if (sphere.getZ() > max.getZ())
{
d -= square(sphere.getZ() - max.getZ());
}
return d > 0;
}
private double square(double v)
{
return v * v;
}
public void addProtectedArea(String label, Location location, double radius)
{
areas.put(label.toLowerCase(), new SerializableProtectedRegion(location, radius));
save();
}
public void removeProtectedArea(String label)
{
areas.remove(label.toLowerCase());
save();
}
2013-09-03 20:49:48 +00:00
public void clearProtectedAreas()
2013-08-28 19:40:14 +00:00
{
2013-09-05 14:22:59 +00:00
clearProtectedAreas(true);
2013-08-28 19:40:14 +00:00
}
public void clearProtectedAreas(boolean createSpawnpointProtectedAreas)
{
areas.clear();
2013-09-03 20:49:48 +00:00
2013-09-05 14:22:59 +00:00
if (createSpawnpointProtectedAreas)
2013-08-28 19:40:14 +00:00
{
autoAddSpawnpoints();
}
2013-09-03 20:49:48 +00:00
save();
}
public void cleanProtectedAreas()
2013-09-05 14:22:59 +00:00
{
boolean doSave = false;
final Iterator<Map.Entry<String, SerializableProtectedRegion>> it = areas.entrySet().iterator();
2013-09-05 14:22:59 +00:00
while (it.hasNext())
{
try
{
it.next().getValue().getLocation();
}
catch (SerializableProtectedRegion.CantFindWorldException ex)
{
it.remove();
doSave = true;
}
}
if (doSave)
{
save();
2013-09-05 14:22:59 +00:00
}
}
public Set<String> getProtectedAreaLabels()
{
return areas.keySet();
}
public void autoAddSpawnpoints()
{
if (!ConfigEntry.PROTECTAREA_ENABLED.getBoolean())
{
return;
}
if (ConfigEntry.PROTECTAREA_SPAWNPOINTS.getBoolean())
{
for (World world : Bukkit.getWorlds())
{
addProtectedArea("spawn_" + world.getName(), world.getSpawnLocation(), ConfigEntry.PROTECTAREA_RADIUS.getDouble());
}
}
}
2013-09-05 14:22:59 +00:00
public static class SerializableProtectedRegion implements Serializable
2013-09-05 14:22:59 +00:00
{
private static final long serialVersionUID = 213123517828282L;
2013-09-05 14:22:59 +00:00
private final double x, y, z;
private final double radius;
private final String worldName;
private final UUID worldUUID;
private transient Location location = null;
public SerializableProtectedRegion(final Location location, final double radius)
{
this.x = location.getX();
this.y = location.getY();
this.z = location.getZ();
this.radius = radius;
this.worldName = location.getWorld().getName();
this.worldUUID = location.getWorld().getUID();
this.location = location;
}
public Location getLocation() throws CantFindWorldException
{
if (this.location == null)
{
World world = Bukkit.getWorld(this.worldUUID);
if (world == null)
{
world = Bukkit.getWorld(this.worldName);
}
if (world == null)
{
throw new CantFindWorldException("Can't find world " + this.worldName + ", UUID: " + this.worldUUID.toString());
}
location = new Location(world, x, y, z);
}
return this.location;
}
public double getRadius()
{
return radius;
}
public class CantFindWorldException extends Exception
2013-09-05 14:22:59 +00:00
{
private static final long serialVersionUID = 1L;
2013-09-05 14:22:59 +00:00
public CantFindWorldException(String string)
{
super(string);
}
}
}
}