Reformat before I go crazy.

This commit is contained in:
drtshock
2014-04-04 13:55:21 -05:00
parent 0c3ea377dd
commit 814e439705
124 changed files with 11302 additions and 13164 deletions

View File

@@ -1,97 +1,89 @@
package com.massivecraft.factions.util;
import java.util.*;
import org.bukkit.ChatColor;
public class AsciiCompass
{
public enum Point
{
N('N'),
NE('/'),
E('E'),
SE('\\'),
S('S'),
SW('/'),
W('W'),
NW('\\');
public final char asciiChar;
private Point(final char asciiChar)
{
this.asciiChar = asciiChar;
}
@Override
public String toString()
{
return String.valueOf(this.asciiChar);
}
public String toString(boolean isActive, ChatColor colorActive, String colorDefault)
{
return (isActive ? colorActive : colorDefault)+String.valueOf(this.asciiChar);
}
}
public static AsciiCompass.Point getCompassPointForDirection(double inDegrees)
{
double degrees = (inDegrees - 180) % 360 ;
if (degrees < 0)
degrees += 360;
if (0 <= degrees && degrees < 22.5)
return AsciiCompass.Point.N;
else if (22.5 <= degrees && degrees < 67.5)
return AsciiCompass.Point.NE;
else if (67.5 <= degrees && degrees < 112.5)
return AsciiCompass.Point.E;
else if (112.5 <= degrees && degrees < 157.5)
return AsciiCompass.Point.SE;
else if (157.5 <= degrees && degrees < 202.5)
return AsciiCompass.Point.S;
else if (202.5 <= degrees && degrees < 247.5)
return AsciiCompass.Point.SW;
else if (247.5 <= degrees && degrees < 292.5)
return AsciiCompass.Point.W;
else if (292.5 <= degrees && degrees < 337.5)
return AsciiCompass.Point.NW;
else if (337.5 <= degrees && degrees < 360.0)
return AsciiCompass.Point.N;
else
return null;
}
public static ArrayList<String> getAsciiCompass(Point point, ChatColor colorActive, String colorDefault)
{
ArrayList<String> ret = new ArrayList<String>();
String row;
row = "";
row += Point.NW.toString(Point.NW == point, colorActive, colorDefault);
row += Point.N.toString(Point.N == point, colorActive, colorDefault);
row += Point.NE.toString(Point.NE == point, colorActive, colorDefault);
ret.add(row);
row = "";
row += Point.W.toString(Point.W == point, colorActive, colorDefault);
row += colorDefault+"+";
row += Point.E.toString(Point.E == point, colorActive, colorDefault);
ret.add(row);
row = "";
row += Point.SW.toString(Point.SW == point, colorActive, colorDefault);
row += Point.S.toString(Point.S == point, colorActive, colorDefault);
row += Point.SE.toString(Point.SE == point, colorActive, colorDefault);
ret.add(row);
import java.util.ArrayList;
return ret;
}
public static ArrayList<String> getAsciiCompass(double inDegrees, ChatColor colorActive, String colorDefault)
{
return getAsciiCompass(getCompassPointForDirection(inDegrees), colorActive, colorDefault);
}
public class AsciiCompass {
public enum Point {
N('N'),
NE('/'),
E('E'),
SE('\\'),
S('S'),
SW('/'),
W('W'),
NW('\\');
public final char asciiChar;
private Point(final char asciiChar) {
this.asciiChar = asciiChar;
}
@Override
public String toString() {
return String.valueOf(this.asciiChar);
}
public String toString(boolean isActive, ChatColor colorActive, String colorDefault) {
return (isActive ? colorActive : colorDefault) + String.valueOf(this.asciiChar);
}
}
public static AsciiCompass.Point getCompassPointForDirection(double inDegrees) {
double degrees = (inDegrees - 180) % 360;
if (degrees < 0)
degrees += 360;
if (0 <= degrees && degrees < 22.5)
return AsciiCompass.Point.N;
else if (22.5 <= degrees && degrees < 67.5)
return AsciiCompass.Point.NE;
else if (67.5 <= degrees && degrees < 112.5)
return AsciiCompass.Point.E;
else if (112.5 <= degrees && degrees < 157.5)
return AsciiCompass.Point.SE;
else if (157.5 <= degrees && degrees < 202.5)
return AsciiCompass.Point.S;
else if (202.5 <= degrees && degrees < 247.5)
return AsciiCompass.Point.SW;
else if (247.5 <= degrees && degrees < 292.5)
return AsciiCompass.Point.W;
else if (292.5 <= degrees && degrees < 337.5)
return AsciiCompass.Point.NW;
else if (337.5 <= degrees && degrees < 360.0)
return AsciiCompass.Point.N;
else
return null;
}
public static ArrayList<String> getAsciiCompass(Point point, ChatColor colorActive, String colorDefault) {
ArrayList<String> ret = new ArrayList<String>();
String row;
row = "";
row += Point.NW.toString(Point.NW == point, colorActive, colorDefault);
row += Point.N.toString(Point.N == point, colorActive, colorDefault);
row += Point.NE.toString(Point.NE == point, colorActive, colorDefault);
ret.add(row);
row = "";
row += Point.W.toString(Point.W == point, colorActive, colorDefault);
row += colorDefault + "+";
row += Point.E.toString(Point.E == point, colorActive, colorDefault);
ret.add(row);
row = "";
row += Point.SW.toString(Point.SW == point, colorActive, colorDefault);
row += Point.S.toString(Point.S == point, colorActive, colorDefault);
row += Point.SE.toString(Point.SE == point, colorActive, colorDefault);
ret.add(row);
return ret;
}
public static ArrayList<String> getAsciiCompass(double inDegrees, ChatColor colorActive, String colorDefault) {
return getAsciiCompass(getCompassPointForDirection(inDegrees), colorActive, colorDefault);
}
}

View File

@@ -1,94 +1,79 @@
package com.massivecraft.factions.util;
import com.massivecraft.factions.*;
import com.massivecraft.factions.struct.Role;
import org.bukkit.scheduler.BukkitRunnable;
import java.util.ArrayList;
import java.util.ListIterator;
import org.bukkit.scheduler.BukkitRunnable;
public class AutoLeaveProcessTask extends BukkitRunnable {
private transient boolean readyToGo = false;
private transient boolean finished = false;
private transient ArrayList<FPlayer> fplayers;
private transient ListIterator<FPlayer> iterator;
private transient double toleranceMillis;
import com.massivecraft.factions.Conf;
import com.massivecraft.factions.Faction;
import com.massivecraft.factions.FPlayer;
import com.massivecraft.factions.FPlayers;
import com.massivecraft.factions.P;
import com.massivecraft.factions.struct.Role;
public AutoLeaveProcessTask() {
fplayers = new ArrayList<FPlayer>(FPlayers.i.get());
this.iterator = fplayers.listIterator();
this.toleranceMillis = Conf.autoLeaveAfterDaysOfInactivity * 24 * 60 * 60 * 1000;
this.readyToGo = true;
this.finished = false;
}
public class AutoLeaveProcessTask extends BukkitRunnable
{
private transient boolean readyToGo = false;
private transient boolean finished = false;
private transient ArrayList<FPlayer> fplayers;
private transient ListIterator<FPlayer> iterator;
private transient double toleranceMillis;
public void run() {
if (Conf.autoLeaveAfterDaysOfInactivity <= 0.0 || Conf.autoLeaveRoutineMaxMillisecondsPerTick <= 0.0) {
this.stop();
return;
}
public AutoLeaveProcessTask()
{
fplayers = new ArrayList<FPlayer>(FPlayers.i.get());
this.iterator = fplayers.listIterator();
this.toleranceMillis = Conf.autoLeaveAfterDaysOfInactivity * 24 * 60 * 60 * 1000;
this.readyToGo = true;
this.finished = false;
}
if (!readyToGo) return;
// this is set so it only does one iteration at a time, no matter how frequently the timer fires
readyToGo = false;
// and this is tracked to keep one iteration from dragging on too long and possibly choking the system if there are a very large number of players to go through
long loopStartTime = System.currentTimeMillis();
public void run()
{
if (Conf.autoLeaveAfterDaysOfInactivity <= 0.0 || Conf.autoLeaveRoutineMaxMillisecondsPerTick <= 0.0)
{
this.stop();
return;
}
while (iterator.hasNext()) {
long now = System.currentTimeMillis();
if ( ! readyToGo) return;
// this is set so it only does one iteration at a time, no matter how frequently the timer fires
readyToGo = false;
// and this is tracked to keep one iteration from dragging on too long and possibly choking the system if there are a very large number of players to go through
long loopStartTime = System.currentTimeMillis();
// if this iteration has been running for maximum time, stop to take a breather until next tick
if (now > loopStartTime + Conf.autoLeaveRoutineMaxMillisecondsPerTick) {
readyToGo = true;
return;
}
while(iterator.hasNext())
{
long now = System.currentTimeMillis();
FPlayer fplayer = iterator.next();
if (fplayer.isOffline() && now - fplayer.getLastLoginTime() > toleranceMillis) {
if (Conf.logFactionLeave || Conf.logFactionKick)
P.p.log("Player " + fplayer.getName() + " was auto-removed due to inactivity.");
// if this iteration has been running for maximum time, stop to take a breather until next tick
if (now > loopStartTime + Conf.autoLeaveRoutineMaxMillisecondsPerTick)
{
readyToGo = true;
return;
}
// if player is faction admin, sort out the faction since he's going away
if (fplayer.getRole() == Role.ADMIN) {
Faction faction = fplayer.getFaction();
if (faction != null)
fplayer.getFaction().promoteNewLeader();
}
FPlayer fplayer = iterator.next();
if (fplayer.isOffline() && now - fplayer.getLastLoginTime() > toleranceMillis)
{
if (Conf.logFactionLeave || Conf.logFactionKick)
P.p.log("Player "+fplayer.getName()+" was auto-removed due to inactivity.");
fplayer.leave(false);
iterator.remove(); // go ahead and remove this list's link to the FPlayer object
fplayer.detach();
}
}
// if player is faction admin, sort out the faction since he's going away
if (fplayer.getRole() == Role.ADMIN)
{
Faction faction = fplayer.getFaction();
if (faction != null)
fplayer.getFaction().promoteNewLeader();
}
// looks like we've finished
this.stop();
}
fplayer.leave(false);
iterator.remove(); // go ahead and remove this list's link to the FPlayer object
fplayer.detach();
}
}
// we're done, shut down
public void stop() {
readyToGo = false;
finished = true;
// looks like we've finished
this.stop();
}
this.cancel();
}
// we're done, shut down
public void stop()
{
readyToGo = false;
finished = true;
this.cancel();
}
public boolean isFinished()
{
return finished;
}
public boolean isFinished() {
return finished;
}
}

View File

@@ -3,26 +3,23 @@ package com.massivecraft.factions.util;
import com.massivecraft.factions.Conf;
import com.massivecraft.factions.P;
public class AutoLeaveTask implements Runnable
{
private static AutoLeaveProcessTask task;
double rate;
public class AutoLeaveTask implements Runnable {
private static AutoLeaveProcessTask task;
double rate;
public AutoLeaveTask()
{
this.rate = Conf.autoLeaveRoutineRunsEveryXMinutes;
}
public AutoLeaveTask() {
this.rate = Conf.autoLeaveRoutineRunsEveryXMinutes;
}
public synchronized void run()
{
if (task != null && ! task.isFinished())
return;
public synchronized void run() {
if (task != null && !task.isFinished())
return;
task = new AutoLeaveProcessTask();
task.runTaskTimer(P.p, 1, 1);
task = new AutoLeaveProcessTask();
task.runTaskTimer(P.p, 1, 1);
// maybe setting has been changed? if so, restart this task at new rate
if (this.rate != Conf.autoLeaveRoutineRunsEveryXMinutes)
P.p.startAutoLeaveTask(true);
}
// maybe setting has been changed? if so, restart this task at new rate
if (this.rate != Conf.autoLeaveRoutineRunsEveryXMinutes)
P.p.startAutoLeaveTask(true);
}
}

View File

@@ -9,99 +9,86 @@ import org.bukkit.World;
* yet when an object of this class is created, only when the Location is first accessed.
*/
public class LazyLocation
{
private Location location = null;
private String worldName;
private double x;
private double y;
private double z;
private float pitch;
private float yaw;
public class LazyLocation {
private Location location = null;
private String worldName;
private double x;
private double y;
private double z;
private float pitch;
private float yaw;
public LazyLocation(Location loc)
{
setLocation(loc);
}
public LazyLocation(Location loc) {
setLocation(loc);
}
public LazyLocation(final String worldName, final double x, final double y, final double z)
{
this(worldName, x, y, z, 0, 0);
}
public LazyLocation(final String worldName, final double x, final double y, final double z) {
this(worldName, x, y, z, 0, 0);
}
public LazyLocation(final String worldName, final double x, final double y, final double z, final float yaw, final float pitch)
{
this.worldName = worldName;
this.x = x;
this.y = y;
this.z = z;
this.yaw = yaw;
this.pitch = pitch;
}
public LazyLocation(final String worldName, final double x, final double y, final double z, final float yaw, final float pitch) {
this.worldName = worldName;
this.x = x;
this.y = y;
this.z = z;
this.yaw = yaw;
this.pitch = pitch;
}
// This returns the actual Location
public final Location getLocation()
{
// make sure Location is initialized before returning it
initLocation();
return location;
}
// This returns the actual Location
public final Location getLocation() {
// make sure Location is initialized before returning it
initLocation();
return location;
}
// change the Location
public final void setLocation(Location loc)
{
this.location = loc;
this.worldName = loc.getWorld().getName();
this.x = loc.getX();
this.y = loc.getY();
this.z = loc.getZ();
this.yaw = loc.getYaw();
this.pitch = loc.getPitch();
}
// change the Location
public final void setLocation(Location loc) {
this.location = loc;
this.worldName = loc.getWorld().getName();
this.x = loc.getX();
this.y = loc.getY();
this.z = loc.getZ();
this.yaw = loc.getYaw();
this.pitch = loc.getPitch();
}
// This initializes the Location
private void initLocation()
{
// if location is already initialized, simply return
if (location != null) return;
// This initializes the Location
private void initLocation() {
// if location is already initialized, simply return
if (location != null) return;
// get World; hopefully it's initialized at this point
World world = Bukkit.getWorld(worldName);
if (world == null) return;
// get World; hopefully it's initialized at this point
World world = Bukkit.getWorld(worldName);
if (world == null) return;
// store the Location for future calls, and pass it on
location = new Location(world, x, y, z, yaw, pitch);
}
// store the Location for future calls, and pass it on
location = new Location(world, x, y, z, yaw, pitch);
}
public final String getWorldName()
{
return worldName;
}
public final String getWorldName() {
return worldName;
}
public final double getX()
{
return x;
}
public final double getX() {
return x;
}
public final double getY()
{
return y;
}
public final double getY() {
return y;
}
public final double getZ()
{
return z;
}
public final double getZ() {
return z;
}
public final double getPitch()
{
return pitch;
}
public final double getPitch() {
return pitch;
}
public final double getYaw()
{
return yaw;
}
public final double getYaw() {
return yaw;
}
}

View File

@@ -1,128 +1,104 @@
package com.massivecraft.factions.util;
import com.massivecraft.factions.FLocation;
import com.massivecraft.factions.P;
import org.bukkit.craftbukkit.libs.com.google.gson.*;
import java.lang.reflect.Type;
import java.util.concurrent.ConcurrentHashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.logging.Level;
import java.util.Map.Entry;
import org.bukkit.craftbukkit.libs.com.google.gson.JsonArray;
import org.bukkit.craftbukkit.libs.com.google.gson.JsonDeserializationContext;
import org.bukkit.craftbukkit.libs.com.google.gson.JsonDeserializer;
import org.bukkit.craftbukkit.libs.com.google.gson.JsonElement;
import org.bukkit.craftbukkit.libs.com.google.gson.JsonObject;
import org.bukkit.craftbukkit.libs.com.google.gson.JsonParseException;
import org.bukkit.craftbukkit.libs.com.google.gson.JsonPrimitive;
import org.bukkit.craftbukkit.libs.com.google.gson.JsonSerializationContext;
import org.bukkit.craftbukkit.libs.com.google.gson.JsonSerializer;
import com.massivecraft.factions.FLocation;
import com.massivecraft.factions.P;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.logging.Level;
public class MapFLocToStringSetTypeAdapter implements JsonDeserializer<Map<FLocation, Set<String>>>, JsonSerializer<Map<FLocation, Set<String>>>
{
public class MapFLocToStringSetTypeAdapter implements JsonDeserializer<Map<FLocation, Set<String>>>, JsonSerializer<Map<FLocation, Set<String>>> {
@Override
public Map<FLocation, Set<String>> deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException
{
try {
JsonObject obj = json.getAsJsonObject();
if (obj == null)
{
return null;
}
@Override
public Map<FLocation, Set<String>> deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
try {
JsonObject obj = json.getAsJsonObject();
if (obj == null) {
return null;
}
Map<FLocation, Set<String>> locationMap = new ConcurrentHashMap<FLocation, Set<String>>();
Set<String> nameSet;
Iterator<JsonElement> iter;
String worldName;
String[] coords;
int x, z;
Map<FLocation, Set<String>> locationMap = new ConcurrentHashMap<FLocation, Set<String>>();
Set<String> nameSet;
Iterator<JsonElement> iter;
String worldName;
String[] coords;
int x, z;
for (Entry<String, JsonElement> entry : obj.entrySet())
{
worldName = entry.getKey();
for (Entry<String, JsonElement> entry2 : entry.getValue().getAsJsonObject().entrySet())
{
coords = entry2.getKey().trim().split("[,\\s]+");
x = Integer.parseInt(coords[0]);
z = Integer.parseInt(coords[1]);
for (Entry<String, JsonElement> entry : obj.entrySet()) {
worldName = entry.getKey();
for (Entry<String, JsonElement> entry2 : entry.getValue().getAsJsonObject().entrySet()) {
coords = entry2.getKey().trim().split("[,\\s]+");
x = Integer.parseInt(coords[0]);
z = Integer.parseInt(coords[1]);
nameSet = new HashSet<String>();
iter = entry2.getValue().getAsJsonArray().iterator();
while (iter.hasNext())
{
nameSet.add(iter.next().getAsString());
}
nameSet = new HashSet<String>();
iter = entry2.getValue().getAsJsonArray().iterator();
while (iter.hasNext()) {
nameSet.add(iter.next().getAsString());
}
locationMap.put(new FLocation(worldName, x, z), nameSet);
}
}
locationMap.put(new FLocation(worldName, x, z), nameSet);
}
}
return locationMap;
return locationMap;
}
catch (Exception ex)
{
ex.printStackTrace();
P.p.log(Level.WARNING, "Error encountered while deserializing a Map of FLocations to String Sets.");
return null;
}
}
} catch (Exception ex) {
ex.printStackTrace();
P.p.log(Level.WARNING, "Error encountered while deserializing a Map of FLocations to String Sets.");
return null;
}
}
@Override
public JsonElement serialize(Map<FLocation, Set<String>> src, Type typeOfSrc, JsonSerializationContext context)
{
JsonObject obj = new JsonObject();
@Override
public JsonElement serialize(Map<FLocation, Set<String>> src, Type typeOfSrc, JsonSerializationContext context) {
JsonObject obj = new JsonObject();
try {
if (src != null)
{
FLocation loc;
String locWorld;
Set<String> nameSet;
Iterator<String> iter;
JsonArray nameArray;
JsonPrimitive nameElement;
try {
if (src != null) {
FLocation loc;
String locWorld;
Set<String> nameSet;
Iterator<String> iter;
JsonArray nameArray;
JsonPrimitive nameElement;
for (Entry<FLocation, Set<String>> entry : src.entrySet())
{
loc = entry.getKey();
locWorld = loc.getWorldName();
nameSet = entry.getValue();
for (Entry<FLocation, Set<String>> entry : src.entrySet()) {
loc = entry.getKey();
locWorld = loc.getWorldName();
nameSet = entry.getValue();
if (nameSet == null || nameSet.isEmpty())
{
continue;
}
if (nameSet == null || nameSet.isEmpty()) {
continue;
}
nameArray = new JsonArray();
iter = nameSet.iterator();
while (iter.hasNext())
{
nameElement = new JsonPrimitive(iter.next());
nameArray.add(nameElement);
}
nameArray = new JsonArray();
iter = nameSet.iterator();
while (iter.hasNext()) {
nameElement = new JsonPrimitive(iter.next());
nameArray.add(nameElement);
}
if ( ! obj.has(locWorld))
{
obj.add(locWorld, new JsonObject());
}
if (!obj.has(locWorld)) {
obj.add(locWorld, new JsonObject());
}
obj.get(locWorld).getAsJsonObject().add(loc.getCoordString(), nameArray);
}
}
return obj;
obj.get(locWorld).getAsJsonObject().add(loc.getCoordString(), nameArray);
}
}
return obj;
}
catch (Exception ex)
{
ex.printStackTrace();
P.p.log(Level.WARNING, "Error encountered while serializing a Map of FLocations to String Sets.");
return obj;
}
}
} catch (Exception ex) {
ex.printStackTrace();
P.p.log(Level.WARNING, "Error encountered while serializing a Map of FLocations to String Sets.");
return obj;
}
}
}

View File

@@ -1,69 +1,63 @@
package com.massivecraft.factions.util;
import org.bukkit.ChatColor;
import org.bukkit.entity.Creature;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import java.util.Arrays;
import java.util.HashSet;
import org.bukkit.ChatColor;
import org.bukkit.entity.Creature;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Entity;
public class MiscUtil {
public static EntityType creatureTypeFromEntity(Entity entity) {
if (!(entity instanceof Creature)) {
return null;
}
String name = entity.getClass().getSimpleName();
name = name.substring(5); // Remove "Craft"
return EntityType.fromName(name);
}
// Inclusive range
public static long[] range(long start, long end) {
long[] values = new long[(int) Math.abs(end - start) + 1];
if (end < start) {
long oldstart = start;
start = end;
end = oldstart;
}
for (long i = start; i <= end; i++) {
values[(int) (i - start)] = i;
}
return values;
}
/// TODO create tag whitelist!!
public static HashSet<String> substanceChars = new HashSet<String>(Arrays.asList(new String[]{
"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H",
"I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z",
"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r",
"s", "t", "u", "v", "w", "x", "y", "z"
}));
public static String getComparisonString(String str) {
String ret = "";
str = ChatColor.stripColor(str);
str = str.toLowerCase();
for (char c : str.toCharArray()) {
if (substanceChars.contains(String.valueOf(c))) {
ret += c;
}
}
return ret.toLowerCase();
}
public class MiscUtil
{
public static EntityType creatureTypeFromEntity(Entity entity)
{
if ( ! (entity instanceof Creature))
{
return null;
}
String name = entity.getClass().getSimpleName();
name = name.substring(5); // Remove "Craft"
return EntityType.fromName(name);
}
// Inclusive range
public static long[] range(long start, long end) {
long[] values = new long[(int) Math.abs(end - start) + 1];
if (end < start) {
long oldstart = start;
start = end;
end = oldstart;
}
for (long i = start; i <= end; i++) {
values[(int) (i - start)] = i;
}
return values;
}
/// TODO create tag whitelist!!
public static HashSet<String> substanceChars = new HashSet<String>(Arrays.asList(new String []{
"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H",
"I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z",
"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r",
"s", "t", "u", "v", "w", "x", "y", "z"
}));
public static String getComparisonString(String str)
{
String ret = "";
str = ChatColor.stripColor(str);
str = str.toLowerCase();
for (char c : str.toCharArray())
{
if (substanceChars.contains(String.valueOf(c)))
{
ret += c;
}
}
return ret.toLowerCase();
}
}

View File

@@ -1,73 +1,58 @@
package com.massivecraft.factions.util;
import com.massivecraft.factions.P;
import org.bukkit.craftbukkit.libs.com.google.gson.*;
import java.lang.reflect.Type;
import java.util.logging.Level;
import com.massivecraft.factions.P;
import org.bukkit.craftbukkit.libs.com.google.gson.JsonDeserializationContext;
import org.bukkit.craftbukkit.libs.com.google.gson.JsonDeserializer;
import org.bukkit.craftbukkit.libs.com.google.gson.JsonElement;
import org.bukkit.craftbukkit.libs.com.google.gson.JsonObject;
import org.bukkit.craftbukkit.libs.com.google.gson.JsonParseException;
import org.bukkit.craftbukkit.libs.com.google.gson.JsonSerializationContext;
import org.bukkit.craftbukkit.libs.com.google.gson.JsonSerializer;
public class MyLocationTypeAdapter implements JsonDeserializer<LazyLocation>, JsonSerializer<LazyLocation> {
private static final String WORLD = "world";
private static final String X = "x";
private static final String Y = "y";
private static final String Z = "z";
private static final String YAW = "yaw";
private static final String PITCH = "pitch";
@Override
public LazyLocation deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
try {
JsonObject obj = json.getAsJsonObject();
public class MyLocationTypeAdapter implements JsonDeserializer<LazyLocation>, JsonSerializer<LazyLocation>
{
private static final String WORLD = "world";
private static final String X = "x";
private static final String Y = "y";
private static final String Z = "z";
private static final String YAW = "yaw";
private static final String PITCH = "pitch";
@Override
public LazyLocation deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException
{
try
{
JsonObject obj = json.getAsJsonObject();
String worldName = obj.get(WORLD).getAsString();
double x = obj.get(X).getAsDouble();
double y = obj.get(Y).getAsDouble();
double z = obj.get(Z).getAsDouble();
float yaw = obj.get(YAW).getAsFloat();
float pitch = obj.get(PITCH).getAsFloat();
String worldName = obj.get(WORLD).getAsString();
double x = obj.get(X).getAsDouble();
double y = obj.get(Y).getAsDouble();
double z = obj.get(Z).getAsDouble();
float yaw = obj.get(YAW).getAsFloat();
float pitch = obj.get(PITCH).getAsFloat();
return new LazyLocation(worldName, x, y, z, yaw, pitch);
return new LazyLocation(worldName, x, y, z, yaw, pitch);
} catch (Exception ex) {
ex.printStackTrace();
P.p.log(Level.WARNING, "Error encountered while deserializing a LazyLocation.");
return null;
}
}
}
catch (Exception ex)
{
ex.printStackTrace();
P.p.log(Level.WARNING, "Error encountered while deserializing a LazyLocation.");
return null;
}
}
@Override
public JsonElement serialize(LazyLocation src, Type typeOfSrc, JsonSerializationContext context) {
JsonObject obj = new JsonObject();
@Override
public JsonElement serialize(LazyLocation src, Type typeOfSrc, JsonSerializationContext context) {
JsonObject obj = new JsonObject();
try {
obj.addProperty(WORLD, src.getWorldName());
obj.addProperty(X, src.getX());
obj.addProperty(Y, src.getY());
obj.addProperty(Z, src.getZ());
obj.addProperty(YAW, src.getYaw());
obj.addProperty(PITCH, src.getPitch());
try
{
obj.addProperty(WORLD, src.getWorldName());
obj.addProperty(X, src.getX());
obj.addProperty(Y, src.getY());
obj.addProperty(Z, src.getZ());
obj.addProperty(YAW, src.getYaw());
obj.addProperty(PITCH, src.getPitch());
return obj;
}
catch (Exception ex)
{
ex.printStackTrace();
P.p.log(Level.WARNING, "Error encountered while serializing a LazyLocation.");
return obj;
}
}
return obj;
} catch (Exception ex) {
ex.printStackTrace();
P.p.log(Level.WARNING, "Error encountered while serializing a LazyLocation.");
return obj;
}
}
}

View File

@@ -1,140 +1,110 @@
package com.massivecraft.factions.util;
import org.bukkit.ChatColor;
import com.massivecraft.factions.Conf;
import com.massivecraft.factions.FPlayer;
import com.massivecraft.factions.Faction;
import com.massivecraft.factions.iface.RelationParticipator;
import com.massivecraft.factions.struct.Relation;
import com.massivecraft.factions.zcore.util.TextUtil;
import org.bukkit.ChatColor;
public class RelationUtil
{
public static String describeThatToMe(RelationParticipator that, RelationParticipator me, boolean ucfirst)
{
String ret = "";
public class RelationUtil {
public static String describeThatToMe(RelationParticipator that, RelationParticipator me, boolean ucfirst) {
String ret = "";
Faction thatFaction = getFaction(that);
if (thatFaction == null) return "ERROR"; // ERROR
Faction thatFaction = getFaction(that);
if (thatFaction == null) return "ERROR"; // ERROR
Faction myFaction = getFaction(me);
Faction myFaction = getFaction(me);
// if (myFaction == null) return that.describeTo(null); // no relation, but can show basic name or tag
if (that instanceof Faction)
{
if (me instanceof FPlayer && myFaction == thatFaction)
{
ret = "your faction";
}
else
{
ret = thatFaction.getTag();
}
}
else if (that instanceof FPlayer)
{
FPlayer fplayerthat = (FPlayer) that;
if (that == me)
{
ret = "you";
}
else if (thatFaction == myFaction)
{
ret = fplayerthat.getNameAndTitle();
}
else
{
ret = fplayerthat.getNameAndTag();
}
}
if (that instanceof Faction) {
if (me instanceof FPlayer && myFaction == thatFaction) {
ret = "your faction";
} else {
ret = thatFaction.getTag();
}
} else if (that instanceof FPlayer) {
FPlayer fplayerthat = (FPlayer) that;
if (that == me) {
ret = "you";
} else if (thatFaction == myFaction) {
ret = fplayerthat.getNameAndTitle();
} else {
ret = fplayerthat.getNameAndTag();
}
}
if (ucfirst)
{
ret = TextUtil.upperCaseFirst(ret);
}
if (ucfirst) {
ret = TextUtil.upperCaseFirst(ret);
}
return "" + getColorOfThatToMe(that, me) + ret;
}
return "" + getColorOfThatToMe(that, me) + ret;
}
public static String describeThatToMe(RelationParticipator that, RelationParticipator me)
{
return describeThatToMe(that, me, false);
}
public static String describeThatToMe(RelationParticipator that, RelationParticipator me) {
return describeThatToMe(that, me, false);
}
public static Relation getRelationTo(RelationParticipator me, RelationParticipator that)
{
return getRelationTo(that, me, false);
}
public static Relation getRelationTo(RelationParticipator me, RelationParticipator that) {
return getRelationTo(that, me, false);
}
public static Relation getRelationTo(RelationParticipator me, RelationParticipator that, boolean ignorePeaceful)
{
Faction fthat = getFaction(that);
if (fthat == null) return Relation.NEUTRAL; // ERROR
public static Relation getRelationTo(RelationParticipator me, RelationParticipator that, boolean ignorePeaceful) {
Faction fthat = getFaction(that);
if (fthat == null) return Relation.NEUTRAL; // ERROR
Faction fme = getFaction(me);
if (fme == null) return Relation.NEUTRAL; // ERROR
Faction fme = getFaction(me);
if (fme == null) return Relation.NEUTRAL; // ERROR
if (!fthat.isNormal() || !fme.isNormal())
{
return Relation.NEUTRAL;
}
if (!fthat.isNormal() || !fme.isNormal()) {
return Relation.NEUTRAL;
}
if (fthat.equals(fme))
{
return Relation.MEMBER;
}
if (fthat.equals(fme)) {
return Relation.MEMBER;
}
if (!ignorePeaceful && (fme.isPeaceful() || fthat.isPeaceful()))
{
return Relation.NEUTRAL;
}
if (!ignorePeaceful && (fme.isPeaceful() || fthat.isPeaceful())) {
return Relation.NEUTRAL;
}
if (fme.getRelationWish(fthat).value >= fthat.getRelationWish(fme).value)
{
return fthat.getRelationWish(fme);
}
if (fme.getRelationWish(fthat).value >= fthat.getRelationWish(fme).value) {
return fthat.getRelationWish(fme);
}
return fme.getRelationWish(fthat);
}
return fme.getRelationWish(fthat);
}
public static Faction getFaction(RelationParticipator rp)
{
if (rp instanceof Faction)
{
return (Faction) rp;
}
public static Faction getFaction(RelationParticipator rp) {
if (rp instanceof Faction) {
return (Faction) rp;
}
if (rp instanceof FPlayer)
{
return ((FPlayer) rp).getFaction();
}
if (rp instanceof FPlayer) {
return ((FPlayer) rp).getFaction();
}
// ERROR
return null;
}
// ERROR
return null;
}
public static ChatColor getColorOfThatToMe(RelationParticipator that, RelationParticipator me)
{
Faction thatFaction = getFaction(that);
if (thatFaction != null)
{
if (thatFaction.isPeaceful() && thatFaction != getFaction(me))
{
return Conf.colorPeaceful;
}
if (thatFaction.isSafeZone() && thatFaction != getFaction(me))
{
return Conf.colorPeaceful;
}
if (thatFaction.isWarZone() && thatFaction != getFaction(me))
{
return Conf.colorWar;
}
}
return getRelationTo(that, me).getColor();
}
public static ChatColor getColorOfThatToMe(RelationParticipator that, RelationParticipator me) {
Faction thatFaction = getFaction(that);
if (thatFaction != null) {
if (thatFaction.isPeaceful() && thatFaction != getFaction(me)) {
return Conf.colorPeaceful;
}
if (thatFaction.isSafeZone() && thatFaction != getFaction(me)) {
return Conf.colorPeaceful;
}
if (thatFaction.isWarZone() && thatFaction != getFaction(me)) {
return Conf.colorWar;
}
}
return getRelationTo(that, me).getColor();
}
}

View File

@@ -1,13 +1,12 @@
package com.massivecraft.factions.util;
import java.util.logging.Level;
import com.massivecraft.factions.FLocation;
import com.massivecraft.factions.P;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.World;
import com.massivecraft.factions.FLocation;
import com.massivecraft.factions.P;
import java.util.logging.Level;
/*
@@ -23,78 +22,74 @@ import com.massivecraft.factions.P;
* [7][<][<][<][<][<][<][7]
*/
public abstract class SpiralTask implements Runnable
{
// general task-related reference data
private transient World world = null;
private transient boolean readyToGo = false;
private transient int taskID = -1;
private transient int limit = 0;
public abstract class SpiralTask implements Runnable {
// general task-related reference data
private transient World world = null;
private transient boolean readyToGo = false;
private transient int taskID = -1;
private transient int limit = 0;
// values for the spiral pattern routine
private transient int x = 0;
private transient int z = 0;
private transient boolean isZLeg = false;
private transient boolean isNeg = false;
private transient int length = -1;
private transient int current = 0;
// values for the spiral pattern routine
private transient int x = 0;
private transient int z = 0;
private transient boolean isZLeg = false;
private transient boolean isNeg = false;
private transient int length = -1;
private transient int current = 0;
@SuppressWarnings("LeakingThisInConstructor")
public SpiralTask(FLocation fLocation, int radius)
{
// limit is determined based on spiral leg length for given radius; see insideRadius()
this.limit = (radius - 1) * 2;
@SuppressWarnings("LeakingThisInConstructor")
public SpiralTask(FLocation fLocation, int radius) {
// limit is determined based on spiral leg length for given radius; see insideRadius()
this.limit = (radius - 1) * 2;
this.world = Bukkit.getWorld(fLocation.getWorldName());
if (this.world == null)
{
P.p.log(Level.WARNING, "[SpiralTask] A valid world must be specified!");
this.stop();
return;
}
this.world = Bukkit.getWorld(fLocation.getWorldName());
if (this.world == null) {
P.p.log(Level.WARNING, "[SpiralTask] A valid world must be specified!");
this.stop();
return;
}
this.x = (int)fLocation.getX();
this.z = (int)fLocation.getZ();
this.x = (int) fLocation.getX();
this.z = (int) fLocation.getZ();
this.readyToGo = true;
this.readyToGo = true;
// get this party started
this.setTaskID(Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(P.p, this, 2, 2));
}
// get this party started
this.setTaskID(Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(P.p, this, 2, 2));
}
/*
* This is where the necessary work is done; you'll need to override this method with whatever you want
* done at each chunk in the spiral pattern.
* Return false if the entire task needs to be aborted, otherwise return true to continue.
*/
public abstract boolean work();
/*
* This is where the necessary work is done; you'll need to override this method with whatever you want
* done at each chunk in the spiral pattern.
* Return false if the entire task needs to be aborted, otherwise return true to continue.
*/
public abstract boolean work();
/*
* Returns an FLocation pointing at the current chunk X and Z values.
*/
public final FLocation currentFLocation()
{
return new FLocation(world.getName(), x, z);
}
/*
* Returns a Location pointing at the current chunk X and Z values.
* note that the Location is at the corner of the chunk, not the center.
*/
public final Location currentLocation()
{
return new Location(world, FLocation.chunkToBlock(x), 65.0, FLocation.chunkToBlock(z));
}
/*
* Returns current chunk X and Z values.
*/
public final int getX()
{
return x;
}
public final int getZ()
{
return z;
}
/*
* Returns an FLocation pointing at the current chunk X and Z values.
*/
public final FLocation currentFLocation() {
return new FLocation(world.getName(), x, z);
}
/*
* Returns a Location pointing at the current chunk X and Z values.
* note that the Location is at the corner of the chunk, not the center.
*/
public final Location currentLocation() {
return new Location(world, FLocation.chunkToBlock(x), 65.0, FLocation.chunkToBlock(z));
}
/*
* Returns current chunk X and Z values.
*/
public final int getX() {
return x;
}
public final int getZ() {
return z;
}
@@ -102,112 +97,98 @@ public abstract class SpiralTask implements Runnable
* Below are the guts of the class, which you normally wouldn't need to mess with.
*/
public final void setTaskID(int ID)
{
if (ID == -1)
this.stop();
taskID = ID;
}
public final void setTaskID(int ID) {
if (ID == -1)
this.stop();
taskID = ID;
}
public final void run()
{
if (!this.valid() || !readyToGo) return;
public final void run() {
if (!this.valid() || !readyToGo) return;
// this is set so it only does one iteration at a time, no matter how frequently the timer fires
readyToGo = false;
// this is set so it only does one iteration at a time, no matter how frequently the timer fires
readyToGo = false;
// make sure we're still inside the specified radius
if ( ! this.insideRadius()) return;
// make sure we're still inside the specified radius
if (!this.insideRadius()) return;
// track this to keep one iteration from dragging on too long and possibly choking the system
long loopStartTime = now();
// track this to keep one iteration from dragging on too long and possibly choking the system
long loopStartTime = now();
// keep going until the task has been running for 20ms or more, then stop to take a breather
while (now() < loopStartTime + 20)
{
// run the primary task on the current X/Z coordinates
if ( ! this.work())
{
this.finish();
return;
}
// keep going until the task has been running for 20ms or more, then stop to take a breather
while (now() < loopStartTime + 20) {
// run the primary task on the current X/Z coordinates
if (!this.work()) {
this.finish();
return;
}
// move on to next chunk in spiral
if ( ! this.moveToNext())
return;
}
// move on to next chunk in spiral
if (!this.moveToNext())
return;
}
// ready for the next iteration to run
readyToGo = true;
}
// ready for the next iteration to run
readyToGo = true;
}
// step through chunks in spiral pattern from center; returns false if we're done, otherwise returns true
public final boolean moveToNext()
{
if ( ! this.valid()) return false;
// step through chunks in spiral pattern from center; returns false if we're done, otherwise returns true
public final boolean moveToNext() {
if (!this.valid()) return false;
// make sure we don't need to turn down the next leg of the spiral
if (current < length)
{
current++;
// make sure we don't need to turn down the next leg of the spiral
if (current < length) {
current++;
// if we're outside the radius, we're done
if ( ! this.insideRadius()) return false;
}
else
{ // one leg/side of the spiral down...
current = 0;
isZLeg ^= true;
// every second leg (between X and Z legs, negative or positive), length increases
if (isZLeg)
{
isNeg ^= true;
length++;
}
}
// if we're outside the radius, we're done
if (!this.insideRadius()) return false;
} else { // one leg/side of the spiral down...
current = 0;
isZLeg ^= true;
// every second leg (between X and Z legs, negative or positive), length increases
if (isZLeg) {
isNeg ^= true;
length++;
}
}
// move one chunk further in the appropriate direction
if (isZLeg)
z += (isNeg) ? -1 : 1;
else
x += (isNeg) ? -1 : 1;
// move one chunk further in the appropriate direction
if (isZLeg)
z += (isNeg) ? -1 : 1;
else
x += (isNeg) ? -1 : 1;
return true;
}
return true;
}
public final boolean insideRadius()
{
boolean inside = current < limit;
if (!inside)
this.finish();
return inside;
}
public final boolean insideRadius() {
boolean inside = current < limit;
if (!inside)
this.finish();
return inside;
}
// for successful completion
public void finish()
{
// for successful completion
public void finish() {
// P.p.log("SpiralTask successfully completed!");
this.stop();
}
this.stop();
}
// we're done, whether finished or cancelled
public final void stop()
{
if (!this.valid()) return;
// we're done, whether finished or cancelled
public final void stop() {
if (!this.valid()) return;
readyToGo = false;
Bukkit.getServer().getScheduler().cancelTask(taskID);
taskID = -1;
}
readyToGo = false;
Bukkit.getServer().getScheduler().cancelTask(taskID);
taskID = -1;
}
// is this task still valid/workable?
public final boolean valid()
{
return taskID != -1;
}
// is this task still valid/workable?
public final boolean valid() {
return taskID != -1;
}
private static long now()
{
return System.currentTimeMillis();
}
private static long now() {
return System.currentTimeMillis();
}
}