In progress: Using MassiveCraftCore and Allman indentation style and minor refactoring.
This commit is contained in:
@ -1,33 +0,0 @@
|
||||
package com.massivecraft.factions.util;
|
||||
|
||||
import java.io.*;
|
||||
|
||||
/**
|
||||
* Harddisc related methods such as read and write.
|
||||
*/
|
||||
public class DiscUtil {
|
||||
/**
|
||||
* Convenience function for writing a string to a file.
|
||||
*/
|
||||
public static void write(File file, String content) throws IOException {
|
||||
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file, false), "UTF8"));
|
||||
out.write(content);
|
||||
out.close();
|
||||
}
|
||||
|
||||
/**
|
||||
* Convenience function for reading a file as a string.
|
||||
*/
|
||||
public static String read(File file) throws IOException {
|
||||
BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));
|
||||
String ret = new String(new byte[0], "UTF-8");
|
||||
|
||||
String line;
|
||||
while ((line = in.readLine()) != null) {
|
||||
ret += line;
|
||||
}
|
||||
|
||||
in.close();
|
||||
return ret;
|
||||
}
|
||||
}
|
@ -1,18 +0,0 @@
|
||||
package com.massivecraft.factions.util;
|
||||
|
||||
import org.bukkit.entity.Creature;
|
||||
import org.bukkit.entity.CreatureType;
|
||||
import org.bukkit.entity.Entity;
|
||||
|
||||
public class EntityUtil {
|
||||
public static CreatureType creatureTypeFromEntity(Entity entity) {
|
||||
if ( ! (entity instanceof Creature)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
String name = entity.getClass().getSimpleName();
|
||||
name = name.substring(5); // Remove "Craft"
|
||||
|
||||
return CreatureType.fromName(name);
|
||||
}
|
||||
}
|
@ -1,59 +0,0 @@
|
||||
package com.massivecraft.factions.util;
|
||||
|
||||
import java.io.File;
|
||||
import java.lang.reflect.Method;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
import java.net.URLClassLoader;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
public class JarLoader {
|
||||
|
||||
private static URLClassLoader sysloader = (URLClassLoader)ClassLoader.getSystemClassLoader();
|
||||
|
||||
public static boolean load(String filename) {
|
||||
return load(new File(filename));
|
||||
}
|
||||
|
||||
public static boolean load(File file) {
|
||||
if ( ! file.exists()) {
|
||||
log("This file does not exist: " + file);
|
||||
return false;
|
||||
}
|
||||
|
||||
try {
|
||||
return load(file.toURI().toURL());
|
||||
} catch (MalformedURLException e) {
|
||||
log("The url for \""+file+"\" was malformed." + e);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean load(URL url) {
|
||||
// If the file already is loaded we can skip it
|
||||
for (URL otherUrl : sysloader.getURLs()) {
|
||||
if (otherUrl.sameFile(url)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
Method addURLMethod = URLClassLoader.class.getDeclaredMethod("addURL", new Class[]{ URL.class });
|
||||
addURLMethod.setAccessible(true);
|
||||
addURLMethod.invoke(sysloader, new Object[]{ url });
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
log("Failed to load \""+url+"\":" + e);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// -------------------------------------------- //
|
||||
// Logger
|
||||
// -------------------------------------------- //
|
||||
private static void log(Object o) {
|
||||
Logger.getLogger("Minecraft").log(Level.SEVERE, "[JAR-LOADER] " + o);
|
||||
}
|
||||
|
||||
}
|
@ -19,16 +19,19 @@ import com.google.gson.JsonPrimitive;
|
||||
import com.google.gson.JsonSerializationContext;
|
||||
import com.google.gson.JsonSerializer;
|
||||
import com.massivecraft.factions.FLocation;
|
||||
import com.massivecraft.factions.Factions;
|
||||
import com.massivecraft.factions.P;
|
||||
|
||||
|
||||
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 {
|
||||
public Map<FLocation, Set<String>> deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException
|
||||
{
|
||||
try {
|
||||
JsonObject obj = json.getAsJsonObject();
|
||||
if (obj == null) {
|
||||
if (obj == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
@ -39,16 +42,19 @@ public class MapFLocToStringSetTypeAdapter implements JsonDeserializer<Map<FLoca
|
||||
String[] coords;
|
||||
int x, z;
|
||||
|
||||
for (Entry<String, JsonElement> entry : obj.entrySet()) {
|
||||
for (Entry<String, JsonElement> entry : obj.entrySet())
|
||||
{
|
||||
worldName = entry.getKey();
|
||||
for (Entry<String, JsonElement> entry2 : entry.getValue().getAsJsonObject().entrySet()) {
|
||||
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()) {
|
||||
while (iter.hasNext())
|
||||
{
|
||||
nameSet.add(iter.next().getAsString());
|
||||
}
|
||||
|
||||
@ -58,19 +64,23 @@ public class MapFLocToStringSetTypeAdapter implements JsonDeserializer<Map<FLoca
|
||||
|
||||
return locationMap;
|
||||
|
||||
} catch (Exception ex) {
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
ex.printStackTrace();
|
||||
Factions.log(Level.WARNING, "Error encountered while deserializing a Map of FLocations to String Sets.");
|
||||
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) {
|
||||
public JsonElement serialize(Map<FLocation, Set<String>> src, Type typeOfSrc, JsonSerializationContext context)
|
||||
{
|
||||
JsonObject obj = new JsonObject();
|
||||
|
||||
try {
|
||||
if (src != null) {
|
||||
if (src != null)
|
||||
{
|
||||
FLocation loc;
|
||||
String locWorld;
|
||||
Set<String> nameSet;
|
||||
@ -78,23 +88,27 @@ public class MapFLocToStringSetTypeAdapter implements JsonDeserializer<Map<FLoca
|
||||
JsonArray nameArray;
|
||||
JsonPrimitive nameElement;
|
||||
|
||||
for (Entry<FLocation, Set<String>> entry : src.entrySet()) {
|
||||
for (Entry<FLocation, Set<String>> entry : src.entrySet())
|
||||
{
|
||||
loc = entry.getKey();
|
||||
locWorld = loc.getWorldName();
|
||||
nameSet = entry.getValue();
|
||||
|
||||
if (nameSet == null || nameSet.isEmpty()) {
|
||||
if (nameSet == null || nameSet.isEmpty())
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
nameArray = new JsonArray();
|
||||
iter = nameSet.iterator();
|
||||
while (iter.hasNext()) {
|
||||
while (iter.hasNext())
|
||||
{
|
||||
nameElement = new JsonPrimitive(iter.next());
|
||||
nameArray.add(nameElement);
|
||||
}
|
||||
|
||||
if ( ! obj.has(locWorld)) {
|
||||
if ( ! obj.has(locWorld))
|
||||
{
|
||||
obj.add(locWorld, new JsonObject());
|
||||
}
|
||||
|
||||
@ -103,9 +117,11 @@ public class MapFLocToStringSetTypeAdapter implements JsonDeserializer<Map<FLoca
|
||||
}
|
||||
return obj;
|
||||
|
||||
} catch (Exception ex) {
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
ex.printStackTrace();
|
||||
Factions.log(Level.WARNING, "Error encountered while serializing a Map of FLocations to String Sets.");
|
||||
P.p.log(Level.WARNING, "Error encountered while serializing a Map of FLocations to String Sets.");
|
||||
return obj;
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +1,27 @@
|
||||
package com.massivecraft.factions.util;
|
||||
|
||||
public class MiscUtil {
|
||||
import java.util.Arrays;
|
||||
import java.util.HashSet;
|
||||
|
||||
import org.bukkit.entity.Creature;
|
||||
import org.bukkit.entity.CreatureType;
|
||||
import org.bukkit.entity.Entity;
|
||||
|
||||
public class MiscUtil
|
||||
{
|
||||
|
||||
public static CreatureType creatureTypeFromEntity(Entity entity)
|
||||
{
|
||||
if ( ! (entity instanceof Creature))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
String name = entity.getClass().getSimpleName();
|
||||
name = name.substring(5); // Remove "Craft"
|
||||
|
||||
return CreatureType.fromName(name);
|
||||
}
|
||||
|
||||
// Inclusive range
|
||||
public static long[] range(long start, long end) {
|
||||
@ -12,8 +33,6 @@ public class MiscUtil {
|
||||
end = oldstart;
|
||||
}
|
||||
|
||||
|
||||
|
||||
for (long i = start; i <= end; i++) {
|
||||
values[(int) (i - start)] = i;
|
||||
}
|
||||
@ -21,5 +40,27 @@ public class MiscUtil {
|
||||
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 = "";
|
||||
|
||||
for (char c : str.toCharArray())
|
||||
{
|
||||
if (substanceChars.contains(String.valueOf(c)))
|
||||
{
|
||||
ret += c;
|
||||
}
|
||||
}
|
||||
return ret.toLowerCase();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
@ -13,10 +13,11 @@ import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonParseException;
|
||||
import com.google.gson.JsonSerializationContext;
|
||||
import com.google.gson.JsonSerializer;
|
||||
import com.massivecraft.factions.Factions;
|
||||
import com.massivecraft.factions.P;
|
||||
|
||||
|
||||
public class MyLocationTypeAdapter implements JsonDeserializer<Location>, JsonSerializer<Location> {
|
||||
public class MyLocationTypeAdapter implements JsonDeserializer<Location>, JsonSerializer<Location>
|
||||
{
|
||||
private static final String WORLD = "world";
|
||||
private static final String X = "x";
|
||||
private static final String Y = "y";
|
||||
@ -25,14 +26,16 @@ public class MyLocationTypeAdapter implements JsonDeserializer<Location>, JsonSe
|
||||
private static final String PITCH = "pitch";
|
||||
|
||||
@Override
|
||||
public Location deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
|
||||
try {
|
||||
public Location deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException
|
||||
{
|
||||
try
|
||||
{
|
||||
JsonObject obj = json.getAsJsonObject();
|
||||
|
||||
String worldname = obj.get(WORLD).getAsString();
|
||||
World world = Factions.instance.getServer().getWorld(worldname);
|
||||
World world = P.p.getServer().getWorld(worldname);
|
||||
if (world == null) {
|
||||
Factions.log(Level.WARNING, "Stored location's world \"" + worldname + "\" not found on server; dropping the location.");
|
||||
P.p.log(Level.WARNING, "Stored location's world \"" + worldname + "\" not found on server; dropping the location.");
|
||||
return null;
|
||||
}
|
||||
|
||||
@ -44,9 +47,11 @@ public class MyLocationTypeAdapter implements JsonDeserializer<Location>, JsonSe
|
||||
|
||||
return new Location(world, x, y, z, yaw, pitch);
|
||||
|
||||
} catch (Exception ex) {
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
ex.printStackTrace();
|
||||
Factions.log(Level.WARNING, "Error encountered while deserializing a location.");
|
||||
P.p.log(Level.WARNING, "Error encountered while deserializing a location.");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@ -55,10 +60,11 @@ public class MyLocationTypeAdapter implements JsonDeserializer<Location>, JsonSe
|
||||
public JsonElement serialize(Location src, Type typeOfSrc, JsonSerializationContext context) {
|
||||
JsonObject obj = new JsonObject();
|
||||
|
||||
try {
|
||||
try
|
||||
{
|
||||
if (src.getWorld() == null)
|
||||
{
|
||||
Factions.log(Level.WARNING, "Passed location's world was not found on the server. Dropping the location.");
|
||||
P.p.log(Level.WARNING, "Passed location's world was not found on the server. Dropping the location.");
|
||||
return obj;
|
||||
}
|
||||
|
||||
@ -71,9 +77,11 @@ public class MyLocationTypeAdapter implements JsonDeserializer<Location>, JsonSe
|
||||
|
||||
return obj;
|
||||
|
||||
} catch (Exception ex) {
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
ex.printStackTrace();
|
||||
Factions.log(Level.WARNING, "Error encountered while serializing a location.");
|
||||
P.p.log(Level.WARNING, "Error encountered while serializing a location.");
|
||||
return obj;
|
||||
}
|
||||
}
|
||||
|
@ -1,88 +0,0 @@
|
||||
package com.massivecraft.factions.util;
|
||||
import java.util.*;
|
||||
|
||||
import org.bukkit.Material;
|
||||
|
||||
import com.massivecraft.factions.Conf;
|
||||
|
||||
|
||||
public class TextUtil {
|
||||
public static String titleize(String str) {
|
||||
String line = Conf.colorChrome+repeat("_", 60);
|
||||
String center = ".[ " + Conf.colorSystem + str + Conf.colorChrome + " ].";
|
||||
int pivot = line.length() / 2;
|
||||
int eatLeft = center.length() / 2;
|
||||
int eatRight = center.length() - eatLeft;
|
||||
|
||||
if (eatLeft < pivot)
|
||||
return line.substring(0, pivot - eatLeft) + center + line.substring(pivot + eatRight);
|
||||
else
|
||||
return center;
|
||||
}
|
||||
|
||||
public static String repeat(String s, int times) {
|
||||
if (times <= 0) return "";
|
||||
else return s + repeat(s, times-1);
|
||||
}
|
||||
|
||||
public static ArrayList<String> split(String str) {
|
||||
return new ArrayList<String>(Arrays.asList(str.trim().split("\\s+")));
|
||||
}
|
||||
|
||||
public static String implode(List<String> list, String glue) {
|
||||
String ret = "";
|
||||
for (int i=0; i<list.size(); i++) {
|
||||
if (i!=0) {
|
||||
ret += glue;
|
||||
}
|
||||
ret += list.get(i);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
public static String implode(List<String> list) {
|
||||
return implode(list, " ");
|
||||
}
|
||||
|
||||
/*public static String commandHelp(List<String> aliases, String param, String desc) {
|
||||
ArrayList<String> parts = new ArrayList<String>();
|
||||
parts.add(Conf.colorCommand+Conf.aliasBase.get(0));
|
||||
parts.add(TextUtil.implode(aliases, ", "));
|
||||
if (param.length() > 0) {
|
||||
parts.add(Conf.colorParameter+param);
|
||||
}
|
||||
if (desc.length() > 0) {
|
||||
parts.add(Conf.colorSystem+desc);
|
||||
}
|
||||
//Log.debug(TextUtil.implode(parts, " "));
|
||||
return TextUtil.implode(parts, " ");
|
||||
}*/
|
||||
|
||||
public static String getMaterialName(Material material) {
|
||||
String ret = material.toString();
|
||||
ret = ret.replace('_', ' ');
|
||||
ret = ret.toLowerCase();
|
||||
return ret.substring(0, 1).toUpperCase()+ret.substring(1);
|
||||
}
|
||||
|
||||
/// 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 = "";
|
||||
|
||||
for (char c : str.toCharArray()) {
|
||||
if (substanceChars.contains(String.valueOf(c))) {
|
||||
ret += c;
|
||||
}
|
||||
}
|
||||
|
||||
return ret.toLowerCase();
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user