Added a few utils and fixed a few warnings

This commit is contained in:
Olof Larsson 2011-12-07 19:15:23 +01:00
parent ecaa3759dc
commit 411f597cad
6 changed files with 227 additions and 4 deletions

View File

@ -0,0 +1,11 @@
package com.massivecraft.core;
public class Lang
{
public static final String permForbidden = "<b>You don't have permission to %s.";
public static final String permDoThat = "do that";
public static final String commandSenderMustBePlayer = "<b>This command can only be used by ingame players.";
public static final String commandToFewArgs = "<b>To few arguments. <i>Use like this:";
public static final String commandToManyArgs = "<b>Strange argument \"<p>%s<b>\". <i>Use the command like this:";
}

View File

@ -400,7 +400,7 @@ public final class Gson {
* @return Json representation of {@code src} * @return Json representation of {@code src}
* @since 1.4 * @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) { public JsonElement toJsonTree(Object src, Type typeOfSrc) {
JsonElementWriter writer = new JsonElementWriter(); JsonElementWriter writer = new JsonElementWriter();
toJson(src, typeOfSrc, writer); toJson(src, typeOfSrc, writer);
@ -682,6 +682,7 @@ public final class Gson {
* @throws JsonSyntaxException if json is not a valid representation for an object of type * @throws JsonSyntaxException if json is not a valid representation for an object of type
* @since 1.2 * @since 1.2
*/ */
@SuppressWarnings("unchecked")
public <T> T fromJson(Reader json, Type typeOfT) throws JsonIOException, JsonSyntaxException { public <T> T fromJson(Reader json, Type typeOfT) throws JsonIOException, JsonSyntaxException {
JsonReader jsonReader = new JsonReader(json); JsonReader jsonReader = new JsonReader(json);
T object = (T) fromJson(jsonReader, typeOfT); T object = (T) fromJson(jsonReader, typeOfT);
@ -777,6 +778,7 @@ public final class Gson {
* @throws JsonSyntaxException if json is not a valid representation for an object of type typeOfT * @throws JsonSyntaxException if json is not a valid representation for an object of type typeOfT
* @since 1.3 * @since 1.3
*/ */
@SuppressWarnings("unchecked")
public <T> T fromJson(JsonElement json, Type typeOfT) throws JsonSyntaxException { public <T> T fromJson(JsonElement json, Type typeOfT) throws JsonSyntaxException {
if (json == null) { if (json == null) {
return null; return null;

View File

@ -39,6 +39,7 @@ final class GsonToMiniGsonTypeAdapterFactory implements TypeAdapter.Factory {
this.deserializers = deserializers; this.deserializers = deserializers;
this.deserializationContext = new JsonDeserializationContext() { this.deserializationContext = new JsonDeserializationContext() {
@SuppressWarnings("unchecked")
public <T> T deserialize(JsonElement json, Type typeOfT) throws JsonParseException { public <T> T deserialize(JsonElement json, Type typeOfT) throws JsonParseException {
return (T) gson.fromJson(json, typeOfT); return (T) gson.fromJson(json, typeOfT);
} }

View File

@ -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;
}
}
}

View File

@ -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> T pickFirstVal(CommandSender me, Map<String, T> perm2val)
{
if (perm2val == null) return null;
T ret = null;
for ( Entry<String, T> entry : perm2val.entrySet())
{
ret = entry.getValue();
if (has(me, entry.getKey())) break;
}
return ret;
}
}

View File

@ -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<Location> 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<Location> locations, float thickness)
{
for (Location location : locations)
{
spawnCloudRandom(location, thickness);
}
}
// -------------------------------------------- //
// Attach continuous effects to or locations
// -------------------------------------------- //
// TODO
}