Default gson, one and some derp

This commit is contained in:
Olof Larsson 2011-12-12 21:29:32 +01:00
parent 8bce24bb61
commit 65538d0789
16 changed files with 300 additions and 40 deletions

View File

@ -6,6 +6,7 @@ import java.util.Arrays;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Random;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
@ -17,9 +18,10 @@ import org.bukkit.plugin.java.JavaPlugin;
import com.massivecraft.mcore1.cmd.Cmd; import com.massivecraft.mcore1.cmd.Cmd;
import com.massivecraft.mcore1.lib.gson.GsonBuilder; import com.massivecraft.mcore1.lib.gson.GsonBuilder;
import com.massivecraft.mcore1.perm.Perm; import com.massivecraft.mcore1.persist.One;
import com.massivecraft.mcore1.persist.Persist; import com.massivecraft.mcore1.persist.Persist;
import com.massivecraft.mcore1.text.Txt; import com.massivecraft.mcore1.text.Txt;
import com.massivecraft.mcore1.util.Perm;
public class MCore extends JavaPlugin public class MCore extends JavaPlugin
{ {
@ -92,10 +94,26 @@ public class MCore extends JavaPlugin
permInstances.put(owner, new Perm(getTxt(owner))); permInstances.put(owner, new Perm(getTxt(owner)));
} }
// -------------------------------------------- //
// ONE
// -------------------------------------------- //
private static Map<MPlugin, One> oneInstances = new HashMap<MPlugin, One>();
public static Map<MPlugin, One> getOneInstances() { return oneInstances; }
public static One getOne(MPlugin owner) { return oneInstances.get(owner); }
public static void removeOne(MPlugin owner) { oneInstances.remove(owner); }
public static void createOne(MPlugin owner)
{
if (oneInstances.containsKey(owner)) return;
createTxt(owner);
oneInstances.put(owner, new One(owner));
}
// -------------------------------------------- // // -------------------------------------------- //
// DERP // DERP
// -------------------------------------------- // // -------------------------------------------- //
public static Random random = new Random();
public MCore() public MCore()
{ {
this.serverListener = new MCoreServerListener(this); this.serverListener = new MCoreServerListener(this);
@ -123,7 +141,7 @@ public class MCore extends JavaPlugin
Bukkit.getPluginManager().registerEvent(Type.SERVER_COMMAND, this.serverListener, Event.Priority.Lowest, this); Bukkit.getPluginManager().registerEvent(Type.SERVER_COMMAND, this.serverListener, Event.Priority.Lowest, this);
} }
public GsonBuilder getGsonBuilder() public static GsonBuilder getGsonBuilder()
{ {
return new GsonBuilder() return new GsonBuilder()
.setPrettyPrinting() .setPrettyPrinting()

View File

@ -1,4 +1,4 @@
package com.massivecraft.mcore1.plugin; package com.massivecraft.mcore1;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
@ -8,11 +8,13 @@ import org.bukkit.event.Event;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
import com.massivecraft.mcore1.MCore;
import com.massivecraft.mcore1.cmd.Cmd; import com.massivecraft.mcore1.cmd.Cmd;
import com.massivecraft.mcore1.perm.Perm; import com.massivecraft.mcore1.lib.gson.Gson;
import com.massivecraft.mcore1.lib.gson.GsonBuilder;
import com.massivecraft.mcore1.persist.One;
import com.massivecraft.mcore1.persist.Persist; import com.massivecraft.mcore1.persist.Persist;
import com.massivecraft.mcore1.text.Txt; import com.massivecraft.mcore1.text.Txt;
import com.massivecraft.mcore1.util.Perm;
public abstract class MPlugin extends JavaPlugin public abstract class MPlugin extends JavaPlugin
@ -20,9 +22,13 @@ public abstract class MPlugin extends JavaPlugin
// Tools // Tools
public Cmd cmd; public Cmd cmd;
public Persist persist; public Persist persist;
public One one;
public Txt txt; public Txt txt;
public Perm perm; public Perm perm;
// Gson
public Gson gson;
// -------------------------------------------- // // -------------------------------------------- //
// ENABLE // ENABLE
// -------------------------------------------- // // -------------------------------------------- //
@ -37,15 +43,20 @@ public abstract class MPlugin extends JavaPlugin
// Ensure the base folder exists // Ensure the base folder exists
this.getDataFolder().mkdirs(); this.getDataFolder().mkdirs();
// Create Gson
this.gson = this.getGsonBuilder().create();
// Create Tools // Create Tools
MCore.createCmd(this); MCore.createCmd(this);
MCore.createPersist(this); MCore.createPersist(this);
MCore.createOne(this);
MCore.createTxt(this); MCore.createTxt(this);
MCore.createPerm(this); MCore.createPerm(this);
// Assign tool pointers // Assign tool pointers
this.cmd = MCore.getCmd(this); this.cmd = MCore.getCmd(this);
this.persist = MCore.getPersist(this); this.persist = MCore.getPersist(this);
this.one = MCore.getOne(this);
this.txt = MCore.getTxt(this); this.txt = MCore.getTxt(this);
this.perm = MCore.getPerm(this); this.perm = MCore.getPerm(this);
@ -65,18 +76,29 @@ public abstract class MPlugin extends JavaPlugin
{ {
MCore.getPersist(this).saveAll(); MCore.getPersist(this).saveAll();
MCore.removePersist(this); MCore.removePersist(this);
MCore.removeOne(this);
MCore.removeCmd(this); MCore.removeCmd(this);
MCore.removePerm(this); MCore.removePerm(this);
MCore.removeTxt(this); MCore.removeTxt(this);
this.cmd = null; this.cmd = null;
this.persist = null; this.persist = null;
this.one = null;
this.txt = null; this.txt = null;
this.perm = null; this.perm = null;
log("Disabled"); log("Disabled");
} }
// -------------------------------------------- //
// GSON
// -------------------------------------------- //
public GsonBuilder getGsonBuilder()
{
return MCore.getGsonBuilder();
}
// -------------------------------------------- // // -------------------------------------------- //
// CONVENIENCE // CONVENIENCE
// -------------------------------------------- // // -------------------------------------------- //

View File

@ -8,15 +8,15 @@ import org.bukkit.entity.Player;
import com.massivecraft.mcore1.Lang; import com.massivecraft.mcore1.Lang;
import com.massivecraft.mcore1.MCore; import com.massivecraft.mcore1.MCore;
import com.massivecraft.mcore1.MPlugin;
import com.massivecraft.mcore1.cmd.arg.IArgHandler; import com.massivecraft.mcore1.cmd.arg.IArgHandler;
import com.massivecraft.mcore1.cmd.req.IReq; import com.massivecraft.mcore1.cmd.req.IReq;
import com.massivecraft.mcore1.persist.IClassManager; import com.massivecraft.mcore1.persist.IClassManager;
import com.massivecraft.mcore1.persist.Persist; import com.massivecraft.mcore1.persist.Persist;
import com.massivecraft.mcore1.plugin.MPlugin;
public abstract class MCommand public abstract class MCommand
{ {
public abstract MPlugin getPlugin(); public abstract MPlugin p();
// -------------------------------------------- // // -------------------------------------------- //
// COMMAND BEHAVIOR // COMMAND BEHAVIOR
@ -39,6 +39,8 @@ public abstract class MCommand
protected List<String> aliases; protected List<String> aliases;
public List<String> getAliases() { return this.aliases; } public List<String> getAliases() { return this.aliases; }
public void setAliases(List<String> val) { this.aliases = val; } public void setAliases(List<String> val) { this.aliases = val; }
public void addAliases(String... aliases) { this.aliases.addAll(Arrays.asList(aliases)); }
public void addAliases(List<String> aliases) { this.aliases.addAll(aliases); }
// FIELD: requiredArgs // FIELD: requiredArgs
// These args must always be sent // These args must always be sent
@ -76,7 +78,7 @@ public abstract class MCommand
{ {
if (this.desc == null) if (this.desc == null)
{ {
String pdesc = getPlugin().perm.getPermissionDescription(this.descPermission); String pdesc = p().perm.getPermissionDescription(this.descPermission);
if (pdesc != null) if (pdesc != null)
{ {
return pdesc; return pdesc;
@ -261,7 +263,7 @@ public abstract class MCommand
{ {
// Get the to many string slice // Get the to many string slice
List<String> theToMany = args.subList(this.requiredArgs.size() + this.optionalArgs.size(), args.size()); List<String> theToMany = args.subList(this.requiredArgs.size() + this.optionalArgs.size(), args.size());
msg(Lang.commandToManyArgs, getPlugin().txt.implode(theToMany, " ")); msg(Lang.commandToManyArgs, p().txt.implode(theToMany, " "));
sender.sendMessage(this.getUseageTemplate()); sender.sendMessage(this.getUseageTemplate());
} }
return false; return false;
@ -280,16 +282,16 @@ public abstract class MCommand
public String getUseageTemplate(List<MCommand> commandChain, boolean addDesc) public String getUseageTemplate(List<MCommand> commandChain, boolean addDesc)
{ {
StringBuilder ret = new StringBuilder(); StringBuilder ret = new StringBuilder();
ret.append(getPlugin().txt.getDesign().getColorCommand()); ret.append(p().txt.getDesign().getColorCommand());
ret.append('/'); ret.append('/');
for (MCommand mc : commandChain) for (MCommand mc : commandChain)
{ {
ret.append(getPlugin().txt.implode(mc.aliases, ",")); ret.append(p().txt.implode(mc.aliases, ","));
ret.append(' '); ret.append(' ');
} }
ret.append(getPlugin().txt.implode(this.aliases, ",")); ret.append(p().txt.implode(this.aliases, ","));
List<String> args = new ArrayList<String>(); List<String> args = new ArrayList<String>();
@ -314,15 +316,15 @@ public abstract class MCommand
if (args.size() > 0) if (args.size() > 0)
{ {
ret.append(getPlugin().txt.getDesign().getColorParameter()); ret.append(p().txt.getDesign().getColorParameter());
ret.append(' '); ret.append(' ');
ret.append(getPlugin().txt.implode(args, " ")); ret.append(p().txt.implode(args, " "));
} }
if (addDesc) if (addDesc)
{ {
ret.append(' '); ret.append(' ');
ret.append(getPlugin().txt.getDesign().getColorInfo()); ret.append(p().txt.getDesign().getColorInfo());
ret.append(this.getDesc()); ret.append(this.getDesc());
} }
@ -345,12 +347,12 @@ public abstract class MCommand
public void msg(String str, Object... args) public void msg(String str, Object... args)
{ {
sender.sendMessage(getPlugin().txt.parse(str, args)); sender.sendMessage(p().txt.parse(str, args));
} }
public void msg(String str) public void msg(String str)
{ {
sender.sendMessage(getPlugin().txt.parse(str)); sender.sendMessage(p().txt.parse(str));
} }
public void sendMessage(String msg) public void sendMessage(String msg)
@ -391,8 +393,8 @@ public abstract class MCommand
{ {
return defaultNotSet; return defaultNotSet;
} }
IArgHandler<T> handler = getPlugin().cmd.getArgHandler(clazz); IArgHandler<T> handler = p().cmd.getArgHandler(clazz);
T ret = handler.parse(this.arg(idx), style, this.sender, getPlugin()); T ret = handler.parse(this.arg(idx), style, this.sender, p());
if (ret == null) if (ret == null)
{ {
this.msg(handler.getError()); this.msg(handler.getError());
@ -411,6 +413,11 @@ public abstract class MCommand
return this.argAs(idx, clazz, null, defaultNotSet, null); return this.argAs(idx, clazz, null, defaultNotSet, null);
} }
public <T> T argAs(int idx, Class<T> clazz, String style)
{
return this.argAs(idx, clazz, style, null, null);
}
public <T> T argAs(int idx, Class<T> clazz) public <T> T argAs(int idx, Class<T> clazz)
{ {
return this.argAs(idx, clazz, null, null); return this.argAs(idx, clazz, null, null);

View File

@ -2,7 +2,7 @@ package com.massivecraft.mcore1.cmd.arg;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import com.massivecraft.mcore1.plugin.MPlugin; import com.massivecraft.mcore1.MPlugin;
public abstract class AHBase<T> implements IArgHandler<T> public abstract class AHBase<T> implements IArgHandler<T>
{ {

View File

@ -6,7 +6,7 @@ import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import com.massivecraft.mcore1.plugin.MPlugin; import com.massivecraft.mcore1.MPlugin;
public class AHPlayer extends AHBase<Player> public class AHPlayer extends AHBase<Player>
{ {

View File

@ -2,8 +2,8 @@ package com.massivecraft.mcore1.cmd.arg;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import com.massivecraft.mcore1.MPlugin;
import com.massivecraft.mcore1.persist.IClassManager; import com.massivecraft.mcore1.persist.IClassManager;
import com.massivecraft.mcore1.plugin.MPlugin;
public abstract class AHPlayerWrapper<T> extends AHBase<T> public abstract class AHPlayerWrapper<T> extends AHBase<T>
{ {

View File

@ -2,7 +2,7 @@ package com.massivecraft.mcore1.cmd.arg;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import com.massivecraft.mcore1.plugin.MPlugin; import com.massivecraft.mcore1.MPlugin;
public abstract class AHPrimitive<T> extends AHBase<T> public abstract class AHPrimitive<T> extends AHBase<T>
{ {

View File

@ -2,7 +2,7 @@ package com.massivecraft.mcore1.cmd.arg;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import com.massivecraft.mcore1.plugin.MPlugin; import com.massivecraft.mcore1.MPlugin;
public interface IArgHandler<T> public interface IArgHandler<T>
{ {

View File

@ -24,7 +24,7 @@ public class ReqHasPerm implements IReq
@Override @Override
public String createErrorMessage(CommandSender sender, MCommand command) public String createErrorMessage(CommandSender sender, MCommand command)
{ {
return command.getPlugin().perm.getForbiddenMessage(this.perm); return command.p().perm.getForbiddenMessage(this.perm);
} }
} }

View File

@ -19,4 +19,10 @@ public class ReqIsPlayer implements IReq
{ {
return Lang.commandSenderMustBePlayer; return Lang.commandSenderMustBePlayer;
} }
protected static ReqIsPlayer instance= new ReqIsPlayer();
public static ReqIsPlayer getInstance()
{
return instance;
}
} }

View File

@ -67,6 +67,11 @@ public interface IClassManager<T>
public T getBestMatch(Object oid); public T getBestMatch(Object oid);
// Get all // Get all
public Collection<T> getAllLoaded();
public Collection<T> getAllLoaded(Predictate<T> where);
public Collection<T> getAllLoaded(Predictate<T> where, Comparator<T> orderby);
public Collection<T> getAllLoaded(Predictate<T> where, Comparator<T> orderby, Integer limit);
public Collection<T> getAllLoaded(Predictate<T> where, Comparator<T> orderby, Integer limit, Integer offset);
public Collection<T> getAll(); public Collection<T> getAll();
public Collection<T> getAll(Predictate<T> where); public Collection<T> getAll(Predictate<T> where);
public Collection<T> getAll(Predictate<T> where, Comparator<T> orderby); public Collection<T> getAll(Predictate<T> where, Comparator<T> orderby);

View File

@ -0,0 +1,156 @@
package com.massivecraft.mcore1.persist;
import java.io.File;
import java.lang.reflect.Type;
import java.util.logging.Level;
import com.massivecraft.mcore1.MPlugin;
import com.massivecraft.mcore1.util.DiscUtil;
// TODO: Give better name and place to differenciate from the entity-orm-ish system in "com.massivecraft.core.persist".
public class One {
private MPlugin p;
public One(MPlugin p)
{
this.p = p;
}
// ------------------------------------------------------------ //
// GET NAME - What should we call this type of object?
// ------------------------------------------------------------ //
public static String getName(Class<?> clazz)
{
return clazz.getSimpleName().toLowerCase();
}
public static String getName(Object o)
{
return getName(o.getClass());
}
public static String getName(Type type)
{
return getName(type.getClass());
}
// ------------------------------------------------------------ //
// GET FILE - In which file would we like to store this object?
// ------------------------------------------------------------ //
public File getFile(String name)
{
return new File(p.getDataFolder(), name+".json");
}
public File getFile(Class<?> clazz)
{
return getFile(getName(clazz));
}
public File getFile(Object obj)
{
return getFile(getName(obj));
}
public File getFile(Type type)
{
return getFile(getName(type));
}
// NICE WRAPPERS
public <T> T loadOrSaveDefault(T def, Class<T> clazz)
{
return loadOrSaveDefault(def, clazz, getFile(clazz));
}
public <T> T loadOrSaveDefault(T def, Class<T> clazz, String name)
{
return loadOrSaveDefault(def, clazz, getFile(name));
}
public <T> T loadOrSaveDefault(T def, Class<T> clazz, File file)
{
if ( ! file.exists())
{
p.log("Creating default: "+file);
this.save(def, file);
return def;
}
T loaded = this.load(clazz, file);
if (loaded == null)
{
p.log(Level.WARNING, "Using default as I failed to load: "+file);
return def;
}
return loaded;
}
// SAVE
public boolean save(Object instance)
{
return save(instance, getFile(instance));
}
public boolean save(Object instance, String name)
{
return save(instance, getFile(name));
}
public boolean save(Object instance, File file)
{
return DiscUtil.writeCatch(file, p.gson.toJson(instance));
}
// LOAD BY CLASS
public <T> T load(Class<T> clazz)
{
return load(clazz, getFile(clazz));
}
public <T> T load(Class<T> clazz, String name)
{
return load(clazz, getFile(name));
}
public <T> T load(Class<T> clazz, File file)
{
String content = DiscUtil.readCatch(file);
if (content == null)
{
return null;
}
T instance = p.gson.fromJson(content, clazz);
return instance;
}
// LOAD BY TYPE
@SuppressWarnings("unchecked")
public <T> T load(Type typeOfT, String name)
{
return (T) load(typeOfT, getFile(name));
}
@SuppressWarnings("unchecked")
public <T> T load(Type typeOfT, File file)
{
String content = DiscUtil.readCatch(file);
if (content == null) {
return null;
}
return (T) p.gson.fromJson(content, typeOfT);
}
}

View File

@ -1,17 +1,13 @@
package com.massivecraft.mcore1.persist; package com.massivecraft.mcore1.persist;
import java.util.List;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
public abstract class PlayerEntity extends Entity<PlayerEntity> public abstract class PlayerEntity<T extends PlayerEntity<T>> extends Entity<T>
{ {
@Override
protected PlayerEntity getThis()
{
return this;
}
public Player getPlayer() public Player getPlayer()
{ {
return Bukkit.getPlayer(this.getId()); return Bukkit.getPlayer(this.getId());
@ -27,4 +23,24 @@ public abstract class PlayerEntity extends Entity<PlayerEntity>
return ! isOnline(); return ! isOnline();
} }
// -------------------------------------------- //
// Message Sending Helpers
// -------------------------------------------- //
public void sendMessage(String msg)
{
Player player = this.getPlayer();
if (player == null) return;
player.sendMessage(msg);
}
public void sendMessage(List<String> msgs)
{
Player player = this.getPlayer();
if (player == null) return;
for(String msg : msgs)
{
player.sendMessage(msg);
}
}
} }

View File

@ -408,6 +408,36 @@ public abstract class GsonClassManager<T> implements IClassManager<T>
return this.get(oid, this.getIsCreative()); return this.get(oid, this.getIsCreative());
} }
@Override
public Collection<T> getAllLoaded()
{
return entities;
}
@Override
public Collection<T> getAllLoaded(Predictate<T> where)
{
return Persist.uglySQL(this.getAllLoaded(), where, null, null, null);
}
@Override
public Collection<T> getAllLoaded(Predictate<T> where, Comparator<T> orderby)
{
return Persist.uglySQL(this.getAllLoaded(), where, orderby, null, null);
}
@Override
public Collection<T> getAllLoaded(Predictate<T> where, Comparator<T> orderby, Integer limit)
{
return Persist.uglySQL(this.getAllLoaded(), where, orderby, limit, null);
}
@Override
public Collection<T> getAllLoaded(Predictate<T> where, Comparator<T> orderby, Integer limit, Integer offset)
{
return Persist.uglySQL(this.getAllLoaded(), where, orderby, limit, offset);
}
@Override @Override
public Collection<T> getAll() public Collection<T> getAll()
{ {
@ -418,25 +448,25 @@ public abstract class GsonClassManager<T> implements IClassManager<T>
@Override @Override
public Collection<T> getAll(Predictate<T> where) public Collection<T> getAll(Predictate<T> where)
{ {
return Persist.uglySQL(this.entities, where, null, null, null); return Persist.uglySQL(this.getAll(), where, null, null, null);
} }
@Override @Override
public Collection<T> getAll(Predictate<T> where, Comparator<T> orderby) public Collection<T> getAll(Predictate<T> where, Comparator<T> orderby)
{ {
return Persist.uglySQL(this.entities, where, orderby, null, null); return Persist.uglySQL(this.getAll(), where, orderby, null, null);
} }
@Override @Override
public Collection<T> getAll(Predictate<T> where, Comparator<T> orderby, Integer limit) public Collection<T> getAll(Predictate<T> where, Comparator<T> orderby, Integer limit)
{ {
return Persist.uglySQL(this.entities, where, orderby, limit, null); return Persist.uglySQL(this.getAll(), where, orderby, limit, null);
} }
@Override @Override
public Collection<T> getAll(Predictate<T> where, Comparator<T> orderby, Integer limit, Integer offset) public Collection<T> getAll(Predictate<T> where, Comparator<T> orderby, Integer limit, Integer offset)
{ {
return Persist.uglySQL(this.entities, where, orderby, limit, offset); return Persist.uglySQL(this.getAll(), where, orderby, limit, offset);
} }
@Override @Override

View File

@ -11,7 +11,7 @@ import com.massivecraft.mcore1.Predictate;
import com.massivecraft.mcore1.lib.gson.Gson; import com.massivecraft.mcore1.lib.gson.Gson;
import com.massivecraft.mcore1.persist.PlayerEntity; import com.massivecraft.mcore1.persist.PlayerEntity;
public abstract class GsonPlayerEntityManager<T extends PlayerEntity> extends GsonClassManager<T> public abstract class GsonPlayerEntityManager<T extends PlayerEntity<T>> extends GsonClassManager<T>
{ {
public GsonPlayerEntityManager(Gson gson, File folder, boolean creative, boolean lazy) public GsonPlayerEntityManager(Gson gson, File folder, boolean creative, boolean lazy)
{ {
@ -43,7 +43,7 @@ public abstract class GsonPlayerEntityManager<T extends PlayerEntity> extends Gs
public Collection<T> getAllOnline() public Collection<T> getAllOnline()
{ {
return this.getAll(new Predictate<T>() return this.getAllLoaded(new Predictate<T>()
{ {
public boolean apply(T entity) public boolean apply(T entity)
{ {

View File

@ -1,4 +1,4 @@
package com.massivecraft.mcore1.perm; package com.massivecraft.mcore1.util;
import java.util.Map; import java.util.Map;
import java.util.Map.Entry; import java.util.Map.Entry;