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;
|
|
|
|
|
|
|
|
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-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();
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void clearProtectedAreas()
|
|
|
|
{
|
|
|
|
TFM_ProtectedArea.protectedAreas.clear();
|
|
|
|
autoAddSpawnpoints();
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|