2012-11-03 19:03:38 +00:00
|
|
|
package me.StevenLawson.TotalFreedomMod;
|
|
|
|
|
|
|
|
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;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.Set;
|
|
|
|
import org.bukkit.Bukkit;
|
|
|
|
import org.bukkit.Location;
|
|
|
|
import org.bukkit.World;
|
2013-09-04 19:17:22 +00:00
|
|
|
import org.bukkit.util.Vector;
|
2012-11-03 19:03:38 +00:00
|
|
|
|
|
|
|
public class TFM_ProtectedArea implements Serializable
|
|
|
|
{
|
2012-11-27 16:00:18 +00:00
|
|
|
private static final long serialVersionUID = -3270338811000937254L;
|
|
|
|
public static final double MAX_RADIUS = 50.0D;
|
2012-11-03 19:03:38 +00:00
|
|
|
private static Map<String, TFM_ProtectedArea> protectedAreas = new HashMap<String, TFM_ProtectedArea>();
|
2013-08-25 16:32:01 +00:00
|
|
|
private final SerializableLocation center;
|
2012-11-03 19:03:38 +00:00
|
|
|
private final double radius;
|
|
|
|
|
|
|
|
private TFM_ProtectedArea(Location root_location, double radius)
|
|
|
|
{
|
2013-08-25 16:32:01 +00:00
|
|
|
this.center = new SerializableLocation(root_location);
|
2012-11-03 19:03:38 +00:00
|
|
|
this.radius = radius;
|
|
|
|
}
|
|
|
|
|
2013-08-25 16:32:01 +00:00
|
|
|
public static boolean isInProtectedArea(Location location)
|
2012-11-03 19:03:38 +00:00
|
|
|
{
|
2013-08-25 16:32:01 +00:00
|
|
|
for (Map.Entry<String, TFM_ProtectedArea> protectedArea : TFM_ProtectedArea.protectedAreas.entrySet())
|
2012-11-03 19:03:38 +00:00
|
|
|
{
|
2013-08-25 16:32:01 +00:00
|
|
|
Location protectedAreaCenter = SerializableLocation.returnLocation(protectedArea.getValue().center);
|
|
|
|
if (protectedAreaCenter != null)
|
2012-11-03 19:03:38 +00:00
|
|
|
{
|
2013-08-25 16:32:01 +00:00
|
|
|
if (location.getWorld() == protectedAreaCenter.getWorld())
|
2012-11-03 19:03:38 +00:00
|
|
|
{
|
2013-08-25 16:32:01 +00:00
|
|
|
double protectedAreaRadius = protectedArea.getValue().radius;
|
2012-11-03 19:03:38 +00:00
|
|
|
|
2013-08-25 16:32:01 +00:00
|
|
|
if (location.distanceSquared(protectedAreaCenter) <= (protectedAreaRadius * protectedAreaRadius))
|
2012-11-03 19:03:38 +00:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-09-04 19:17:22 +00:00
|
|
|
public static boolean isInProtectedArea(Vector min, Vector max, String worldName)
|
|
|
|
{
|
|
|
|
for (Map.Entry<String, TFM_ProtectedArea> protectedArea : TFM_ProtectedArea.protectedAreas.entrySet())
|
|
|
|
{
|
|
|
|
Location protectedAreaCenter = SerializableLocation.returnLocation(protectedArea.getValue().center);
|
|
|
|
if (protectedAreaCenter != null)
|
|
|
|
{
|
|
|
|
if (worldName.equals(protectedAreaCenter.getWorld().getName()))
|
|
|
|
{
|
|
|
|
double sphereRadius = protectedArea.getValue().radius;
|
|
|
|
Vector sphereCenter = protectedAreaCenter.toVector();
|
|
|
|
if (cubeIntersectsSphere(min, max, sphereCenter, sphereRadius))
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static 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 static double square(double v)
|
|
|
|
{
|
|
|
|
return v * v;
|
|
|
|
}
|
|
|
|
|
2013-08-25 16:32:01 +00:00
|
|
|
public static void addProtectedArea(String label, Location location, double radius)
|
2012-11-03 19:03:38 +00:00
|
|
|
{
|
2013-08-25 16:32:01 +00:00
|
|
|
TFM_ProtectedArea.protectedAreas.put(label.toLowerCase(), new TFM_ProtectedArea(location, radius));
|
2012-11-03 19:03:38 +00:00
|
|
|
saveProtectedAreas();
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void removeProtectedArea(String label)
|
|
|
|
{
|
|
|
|
TFM_ProtectedArea.protectedAreas.remove(label.toLowerCase());
|
|
|
|
saveProtectedAreas();
|
|
|
|
}
|
2013-09-03 20:49:48 +00:00
|
|
|
|
2012-11-03 19:03:38 +00:00
|
|
|
public static void clearProtectedAreas()
|
2013-08-28 19:40:14 +00:00
|
|
|
{
|
|
|
|
clearProtectedAreas(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void clearProtectedAreas(boolean hard)
|
2012-11-03 19:03:38 +00:00
|
|
|
{
|
|
|
|
TFM_ProtectedArea.protectedAreas.clear();
|
2013-09-03 20:49:48 +00:00
|
|
|
|
2013-08-28 19:40:14 +00:00
|
|
|
if (!hard)
|
|
|
|
{
|
|
|
|
autoAddSpawnpoints();
|
|
|
|
}
|
2013-09-03 20:49:48 +00:00
|
|
|
|
2012-11-03 19:03:38 +00:00
|
|
|
saveProtectedAreas();
|
|
|
|
}
|
|
|
|
|
|
|
|
public static Set<String> getProtectedAreaLabels()
|
|
|
|
{
|
|
|
|
return TFM_ProtectedArea.protectedAreas.keySet();
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void saveProtectedAreas()
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
FileOutputStream fos = new FileOutputStream(new File(TotalFreedomMod.plugin.getDataFolder(), TotalFreedomMod.PROTECTED_AREA_FILE));
|
|
|
|
ObjectOutputStream oos = new ObjectOutputStream(fos);
|
|
|
|
oos.writeObject(TFM_ProtectedArea.protectedAreas);
|
|
|
|
oos.close();
|
|
|
|
fos.close();
|
|
|
|
}
|
2013-08-14 13:28:19 +00:00
|
|
|
catch (Exception ex)
|
2012-11-03 19:03:38 +00:00
|
|
|
{
|
2013-08-14 13:28:19 +00:00
|
|
|
TFM_Log.severe(ex);
|
2012-11-03 19:03:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-28 15:26:08 +00:00
|
|
|
@SuppressWarnings("unchecked")
|
2012-11-03 19:03:38 +00:00
|
|
|
public static void loadProtectedAreas()
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2013-08-25 16:32:01 +00:00
|
|
|
File input = new File(TotalFreedomMod.plugin.getDataFolder(), TotalFreedomMod.PROTECTED_AREA_FILE);
|
|
|
|
if (input.exists())
|
2012-11-03 19:03:38 +00:00
|
|
|
{
|
2013-08-25 16:32:01 +00:00
|
|
|
FileInputStream fis = new FileInputStream(input);
|
2012-11-03 19:03:38 +00:00
|
|
|
ObjectInputStream ois = new ObjectInputStream(fis);
|
|
|
|
TFM_ProtectedArea.protectedAreas = (HashMap<String, TFM_ProtectedArea>) ois.readObject();
|
|
|
|
ois.close();
|
|
|
|
fis.close();
|
|
|
|
}
|
|
|
|
}
|
2013-08-14 13:28:19 +00:00
|
|
|
catch (Exception ex)
|
2012-11-03 19:03:38 +00:00
|
|
|
{
|
2013-08-25 16:32:01 +00:00
|
|
|
File input = new File(TotalFreedomMod.plugin.getDataFolder(), TotalFreedomMod.PROTECTED_AREA_FILE);
|
|
|
|
input.delete();
|
2012-11-03 19:03:38 +00:00
|
|
|
|
2013-08-14 13:28:19 +00:00
|
|
|
TFM_Log.severe(ex);
|
2012-11-03 19:03:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void autoAddSpawnpoints()
|
|
|
|
{
|
2013-08-18 19:27:17 +00:00
|
|
|
if (TFM_ConfigEntry.AUTO_PROTECT_SPAWNPOINTS.getBoolean())
|
2012-11-03 19:03:38 +00:00
|
|
|
{
|
|
|
|
for (World world : Bukkit.getWorlds())
|
|
|
|
{
|
2013-08-18 19:52:32 +00:00
|
|
|
TFM_ProtectedArea.addProtectedArea("spawn_" + world.getName(), world.getSpawnLocation(), TFM_ConfigEntry.AUTO_PROTECT_RADIUS.getDouble());
|
2012-11-03 19:03:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|