From 411f597cadb573e020033e0d3dfa1e03eb9a9335 Mon Sep 17 00:00:00 2001 From: Olof Larsson Date: Wed, 7 Dec 2011 19:15:23 +0100 Subject: [PATCH] Added a few utils and fixed a few warnings --- src/com/massivecraft/core/Lang.java | 11 +++ src/com/massivecraft/core/lib/gson2/Gson.java | 8 +- .../GsonToMiniGsonTypeAdapterFactory.java | 3 +- .../massivecraft/core/util/ClassLoadHack.java | 50 ++++++++++ src/com/massivecraft/core/util/PermUtil.java | 68 ++++++++++++++ src/com/massivecraft/core/util/SmokeUtil.java | 91 +++++++++++++++++++ 6 files changed, 227 insertions(+), 4 deletions(-) create mode 100755 src/com/massivecraft/core/Lang.java create mode 100755 src/com/massivecraft/core/util/ClassLoadHack.java create mode 100755 src/com/massivecraft/core/util/PermUtil.java create mode 100755 src/com/massivecraft/core/util/SmokeUtil.java diff --git a/src/com/massivecraft/core/Lang.java b/src/com/massivecraft/core/Lang.java new file mode 100755 index 00000000..0fa2b2c8 --- /dev/null +++ b/src/com/massivecraft/core/Lang.java @@ -0,0 +1,11 @@ +package com.massivecraft.core; + +public class Lang +{ + public static final String permForbidden = "You don't have permission to %s."; + public static final String permDoThat = "do that"; + + public static final String commandSenderMustBePlayer = "This command can only be used by ingame players."; + public static final String commandToFewArgs = "To few arguments. Use like this:"; + public static final String commandToManyArgs = "Strange argument \"

%s\". Use the command like this:"; +} diff --git a/src/com/massivecraft/core/lib/gson2/Gson.java b/src/com/massivecraft/core/lib/gson2/Gson.java index b7bf37cc..dd6b7068 100755 --- a/src/com/massivecraft/core/lib/gson2/Gson.java +++ b/src/com/massivecraft/core/lib/gson2/Gson.java @@ -400,7 +400,7 @@ public final class Gson { * @return Json representation of {@code src} * @since 1.4 */ - @SuppressWarnings({"unchecked", "rawtypes"}) // the caller is required to make src and typeOfSrc consistent + // the caller is required to make src and typeOfSrc consistent public JsonElement toJsonTree(Object src, Type typeOfSrc) { JsonElementWriter writer = new JsonElementWriter(); toJson(src, typeOfSrc, writer); @@ -682,7 +682,8 @@ public final class Gson { * @throws JsonSyntaxException if json is not a valid representation for an object of type * @since 1.2 */ - public T fromJson(Reader json, Type typeOfT) throws JsonIOException, JsonSyntaxException { + @SuppressWarnings("unchecked") +public T fromJson(Reader json, Type typeOfT) throws JsonIOException, JsonSyntaxException { JsonReader jsonReader = new JsonReader(json); T object = (T) fromJson(jsonReader, typeOfT); assertFullConsumption(object, jsonReader); @@ -777,7 +778,8 @@ public final class Gson { * @throws JsonSyntaxException if json is not a valid representation for an object of type typeOfT * @since 1.3 */ - public T fromJson(JsonElement json, Type typeOfT) throws JsonSyntaxException { + @SuppressWarnings("unchecked") +public T fromJson(JsonElement json, Type typeOfT) throws JsonSyntaxException { if (json == null) { return null; } diff --git a/src/com/massivecraft/core/lib/gson2/GsonToMiniGsonTypeAdapterFactory.java b/src/com/massivecraft/core/lib/gson2/GsonToMiniGsonTypeAdapterFactory.java index 33f73ea6..568dea41 100755 --- a/src/com/massivecraft/core/lib/gson2/GsonToMiniGsonTypeAdapterFactory.java +++ b/src/com/massivecraft/core/lib/gson2/GsonToMiniGsonTypeAdapterFactory.java @@ -39,7 +39,8 @@ final class GsonToMiniGsonTypeAdapterFactory implements TypeAdapter.Factory { this.deserializers = deserializers; this.deserializationContext = new JsonDeserializationContext() { - public T deserialize(JsonElement json, Type typeOfT) throws JsonParseException { + @SuppressWarnings("unchecked") + public T deserialize(JsonElement json, Type typeOfT) throws JsonParseException { return (T) gson.fromJson(json, typeOfT); } }; diff --git a/src/com/massivecraft/core/util/ClassLoadHack.java b/src/com/massivecraft/core/util/ClassLoadHack.java new file mode 100755 index 00000000..d7e402f6 --- /dev/null +++ b/src/com/massivecraft/core/util/ClassLoadHack.java @@ -0,0 +1,50 @@ +package com.massivecraft.core.util; + +import java.io.File; +import java.lang.reflect.Method; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLClassLoader; + +public class ClassLoadHack +{ + private static URLClassLoader sysloader = (URLClassLoader)ClassLoader.getSystemClassLoader(); + + public static boolean load(String filename) + { + return load(new File(filename)); + } + + public static boolean load(File file) + { + try + { + return load(file.toURI().toURL()); + } + catch (MalformedURLException 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) + { + return false; + } + } +} \ No newline at end of file diff --git a/src/com/massivecraft/core/util/PermUtil.java b/src/com/massivecraft/core/util/PermUtil.java new file mode 100755 index 00000000..44a1c5fd --- /dev/null +++ b/src/com/massivecraft/core/util/PermUtil.java @@ -0,0 +1,68 @@ +package com.massivecraft.core.util; + +import java.util.Map; +import java.util.Map.Entry; + +import org.bukkit.Bukkit; +import org.bukkit.command.CommandSender; +import org.bukkit.permissions.Permission; + +import com.massivecraft.core.Lang; + +public class PermUtil +{ + public static String getPermissionDescription (String perm) + { + if (perm == null) return Lang.permDoThat; + Permission permission = Bukkit.getPluginManager().getPermission(perm); + return getPermissionDescription(permission); + } + + public static String getPermissionDescription (Permission perm) + { + if (perm == null) return Lang.permDoThat; + String desc = perm.getDescription(); + if (desc == null || desc.length() == 0) return Lang.permDoThat; + return desc; + } + + public String getForbiddenMessage(String perm) + { + return null; + // TODO: Decouple the text util. + //return p.txt.parse(Lang.permForbidden, getPermissionDescription(perm)); + } + + public boolean has (CommandSender me, String perm) + { + if (me == null) return false; + return me.hasPermission(perm); + } + + public boolean has (CommandSender me, String perm, boolean verbose) + { + if (has(me, perm)) + { + return true; + } + else if (verbose && me != null) + { + me.sendMessage(this.getForbiddenMessage(perm)); + } + return false; + } + + public T pickFirstVal(CommandSender me, Map perm2val) + { + if (perm2val == null) return null; + T ret = null; + + for ( Entry entry : perm2val.entrySet()) + { + ret = entry.getValue(); + if (has(me, entry.getKey())) break; + } + + return ret; + } +} diff --git a/src/com/massivecraft/core/util/SmokeUtil.java b/src/com/massivecraft/core/util/SmokeUtil.java new file mode 100755 index 00000000..5e59db1a --- /dev/null +++ b/src/com/massivecraft/core/util/SmokeUtil.java @@ -0,0 +1,91 @@ +package com.massivecraft.core.util; + +import java.util.Collection; +import java.util.Random; + +import org.bukkit.Effect; +import org.bukkit.Location; + +// http://mc.kev009.com/Protocol +// ----------------------------- +// Smoke Directions +// ----------------------------- +// Direction ID Direction +// 0 South - East +// 1 South +// 2 South - West +// 3 East +// 4 (Up or middle ?) +// 5 West +// 6 North - East +// 7 North +// 8 North - West +//----------------------------- + +public class SmokeUtil +{ + public static Random random = new Random(); + + // -------------------------------------------- // + // Spawn once + // -------------------------------------------- // + + // Single ======== + public static void spawnSingle(Location location, int direction) + { + if (location == null) return; + location.getWorld().playEffect(location, Effect.SMOKE, direction); + } + + public static void spawnSingle(Location location) + { + spawnSingle(location, 4); + } + + public static void spawnSingleRandom(Location location) + { + spawnSingle(location, random.nextInt(9)); + } + + // Simple Cloud ======== + public static void spawnCloudSimple(Location location) + { + for (int i = 0; i <= 8; i++) + { + spawnSingle(location, i); + } + } + + public static void spawnCloudSimple(Collection locations) + { + for (Location location : locations) + { + spawnCloudSimple(location); + } + } + + // Random Cloud ======== + public static void spawnCloudRandom(Location location, float thickness) + { + int singles = (int) Math.floor(thickness*9); + for (int i = 0; i < singles; i++) + { + spawnSingleRandom(location); + } + } + + public static void spawnCloudRandom(Collection locations, float thickness) + { + for (Location location : locations) + { + spawnCloudRandom(location, thickness); + } + } + + // -------------------------------------------- // + // Attach continuous effects to or locations + // -------------------------------------------- // + + // TODO + +}