Geting rid of persist and prepare ConfServer for SimpleConfig usage.

This commit is contained in:
Olof Larsson 2013-04-10 10:53:53 +02:00
parent 262ec248f5
commit 24270e7929
9 changed files with 12 additions and 526 deletions

View File

@ -5,8 +5,9 @@ import java.util.*;
import org.bukkit.*;
import org.bukkit.entity.EntityType;
import com.massivecraft.mcore.SimpleConfig;
public class ConfServer
public class ConfServer extends SimpleConfig
{
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
@ -14,15 +15,7 @@ public class ConfServer
private static transient ConfServer i = new ConfServer();
public static ConfServer get() { return i; }
public static void load()
{
Factions.get().persist.loadOrSaveDefault(i, ConfServer.class, "conf");
}
public static void save()
{
Factions.get().persist.save(i);
}
public ConfServer() { super(Factions.get()); }
// -------------------------------------------- //
// FIELDS

View File

@ -30,6 +30,7 @@ import com.massivecraft.factions.zcore.MPlugin;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import com.massivecraft.mcore.xlib.gson.GsonBuilder;
@ -75,10 +76,13 @@ public class Factions extends MPlugin
public void onEnable()
{
if ( ! preEnable()) return;
// Load Server Config
ConfServer.get().load();
this.loadSuccessful = false;
// Load Conf from disk
ConfServer.load();
FPlayerColl.i.loadFromDisc();
FactionColl.i.loadFromDisc();
Board.load();
@ -151,7 +155,6 @@ public class Factions extends MPlugin
if (this.loadSuccessful)
{
Board.save();
ConfServer.save();
}
EssentialsFeatures.unhookChat();
if (AutoLeaveTask != null)
@ -201,7 +204,6 @@ public class Factions extends MPlugin
public void postAutoSave()
{
Board.save();
ConfServer.save();
}
@Override

View File

@ -1,400 +0,0 @@
package com.massivecraft.factions.cmd;
import java.lang.reflect.Field;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.Set;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import com.massivecraft.factions.ConfServer;
import com.massivecraft.factions.FFlag;
import com.massivecraft.factions.FPerm;
import com.massivecraft.factions.Factions;
import com.massivecraft.factions.Perm;
import com.massivecraft.factions.Rel;
import com.massivecraft.factions.integration.SpoutFeatures;
public class CmdConfig extends FCommand
{
private static HashMap<String, String> properFieldNames = new HashMap<String, String>();
public CmdConfig()
{
super();
this.aliases.add("config");
this.requiredArgs.add("setting");
this.requiredArgs.add("value");
this.errorOnToManyArgs = false;
this.permission = Perm.CONFIG.node;
this.disableOnLock = true;
senderMustBePlayer = false;
senderMustBeMember = false;
senderMustBeOfficer = false;
senderMustBeLeader = false;
}
@Override
public void perform()
{
// store a lookup map of lowercase field names paired with proper capitalization field names
// that way, if the person using this command messes up the capitalization, we can fix that
if (properFieldNames.isEmpty())
{
Field[] fields = ConfServer.class.getDeclaredFields();
for(int i = 0; i < fields.length; i++)
{
properFieldNames.put(fields[i].getName().toLowerCase(), fields[i].getName());
}
}
String field = this.argAsString(0).toLowerCase();
if (field.startsWith("\"") && field.endsWith("\""))
{
field = field.substring(1, field.length() - 1);
}
String fieldName = properFieldNames.get(field);
if (fieldName == null || fieldName.isEmpty())
{
msg("<b>No configuration setting \"<h>%s<b>\" exists.", field);
return;
}
String success = "";
String value = args.get(1);
for(int i = 2; i < args.size(); i++)
{
value += ' ' + args.get(i);
}
try
{
Field target = ConfServer.class.getField(fieldName);
// boolean
if (target.getType() == boolean.class)
{
boolean targetValue = this.strAsBool(value);
target.setBoolean(null, targetValue);
if (targetValue)
{
success = "\""+fieldName+"\" option set to true (enabled).";
}
else
{
success = "\""+fieldName+"\" option set to false (disabled).";
}
}
// int
else if (target.getType() == int.class)
{
try
{
int intVal = Integer.parseInt(value);
target.setInt(null, intVal);
success = "\""+fieldName+"\" option set to "+intVal+".";
}
catch(NumberFormatException ex)
{
sendMessage("Cannot set \""+fieldName+"\": integer (whole number) value required.");
return;
}
}
// double
else if (target.getType() == double.class)
{
try
{
double doubleVal = Double.parseDouble(value);
target.setDouble(null, doubleVal);
success = "\""+fieldName+"\" option set to "+doubleVal+".";
}
catch(NumberFormatException ex)
{
sendMessage("Cannot set \""+fieldName+"\": double (numeric) value required.");
return;
}
}
// float
else if (target.getType() == float.class)
{
try
{
float floatVal = Float.parseFloat(value);
target.setFloat(null, floatVal);
success = "\""+fieldName+"\" option set to "+floatVal+".";
}
catch(NumberFormatException ex)
{
sendMessage("Cannot set \""+fieldName+"\": float (numeric) value required.");
return;
}
}
// String
else if (target.getType() == String.class)
{
target.set(null, value);
success = "\""+fieldName+"\" option set to \""+value+"\".";
}
// ChatColor
else if (target.getType() == ChatColor.class)
{
ChatColor newColor = null;
try
{
newColor = ChatColor.valueOf(value.toUpperCase());
}
catch (IllegalArgumentException ex)
{
}
if (newColor == null)
{
sendMessage("Cannot set \""+fieldName+"\": \""+value.toUpperCase()+"\" is not a valid color.");
return;
}
target.set(null, newColor);
success = "\""+fieldName+"\" color option set to \""+value.toUpperCase()+"\".";
}
// Set<?> or other parameterized collection
else if (target.getGenericType() instanceof ParameterizedType)
{
ParameterizedType targSet = (ParameterizedType)target.getGenericType();
Type innerType = targSet.getActualTypeArguments()[0];
// Set<?>
if (targSet.getRawType() == Set.class)
{
// Set<Material>
if (innerType == Material.class)
{
Material newMat = null;
try
{
newMat = Material.valueOf(value.toUpperCase());
}
catch (IllegalArgumentException ex)
{
}
if (newMat == null)
{
sendMessage("Cannot change \""+fieldName+"\" set: \""+value.toUpperCase()+"\" is not a valid material.");
return;
}
@SuppressWarnings("unchecked")
Set<Material> matSet = (Set<Material>)target.get(null);
// Material already present, so remove it
if (matSet.contains(newMat))
{
matSet.remove(newMat);
target.set(null, matSet);
success = "\""+fieldName+"\" set: Material \""+value.toUpperCase()+"\" removed.";
}
// Material not present yet, add it
else
{
matSet.add(newMat);
target.set(null, matSet);
success = "\""+fieldName+"\" set: Material \""+value.toUpperCase()+"\" added.";
}
}
// Set<String>
else if (innerType == String.class)
{
@SuppressWarnings("unchecked")
Set<String> stringSet = (Set<String>)target.get(null);
// String already present, so remove it
if (stringSet.contains(value))
{
stringSet.remove(value);
success = "\""+fieldName+"\" set: \""+value+"\" removed.";
}
// String not present yet, add it
else
{
stringSet.add(value);
success = "\""+fieldName+"\" set: \""+value+"\" added.";
}
target.set(null, stringSet);
}
// Set of unknown type
else
{
sendMessage("\""+fieldName+"\" is not a data type set which can be modified with this command.");
return;
}
}
// Map<?, ?>
else if (targSet.getRawType() == Map.class)
{
if (args.size() < 3)
{
sendMessage("Cannot change \""+fieldName+"\" map: not enough arguments passed.");
return;
}
Type innerType2 = targSet.getActualTypeArguments()[1];
String value1 = args.get(1);
String value2 = value.substring(value1.length() + 1);
// Map<FFlag, Boolean>
if (innerType == FFlag.class && innerType2 == Boolean.class)
{
value1 = value1.toUpperCase();
FFlag newFlag = null;
try
{
newFlag = FFlag.valueOf(value1);
}
catch (IllegalArgumentException ex) {}
if (newFlag == null)
{
sendMessage("Cannot change \""+fieldName+"\" map: \""+value1+"\" is not a valid FFlag.");
return;
}
@SuppressWarnings("unchecked")
Map<FFlag, Boolean> map = (Map<FFlag, Boolean>)target.get(null);
Boolean targetValue = this.strAsBool(value2);
map.put(newFlag, targetValue);
target.set(null, map);
if (targetValue)
success = "\""+fieldName+"\" flag \""+value1+"\" set to true (enabled).";
else
success = "\""+fieldName+"\" flag \""+value1+"\" set to false (disabled).";
}
// Map<FPerm, Set<Rel>>
else if (innerType == FPerm.class && innerType2 instanceof ParameterizedType)
{
if (((ParameterizedType)innerType2).getRawType() != Set.class)
{
sendMessage("\""+fieldName+"\" is not a data type map which can be modified with this command, due to the inner collection type.");
return;
}
value1 = value1.toUpperCase();
value2 = value2.toUpperCase();
FPerm newPerm = null;
Rel newRel = null;
try
{
newPerm = FPerm.valueOf(value1);
newRel = Rel.valueOf(value2);
}
catch (IllegalArgumentException ex) {}
if (newPerm == null)
{
sendMessage("Cannot change \""+fieldName+"\" map: \""+value1+"\" is not a valid FPerm.");
return;
}
if (newRel == null)
{
sendMessage("Cannot change \""+fieldName+"\" map: \""+value2+"\" is not a valid Rel.");
return;
}
@SuppressWarnings("unchecked")
Map<FPerm, Set<Rel>> map = (Map<FPerm, Set<Rel>>)target.get(null);
Set<Rel> relSet = map.get(newPerm);
if (relSet == null)
relSet = new HashSet<Rel>();
// Rel already present, so remove it
if (relSet.contains(newRel))
{
relSet.remove(newRel);
success = "\""+fieldName+"\" permission \""+value1+"\": relation \""+value2+"\" removed.";
}
// Rel not present yet, add it
else
{
relSet.add(newRel);
success = "\""+fieldName+"\" permission \""+value1+"\": relation \""+value2+"\" added.";
}
map.put(newPerm, relSet);
target.set(null, map);
}
// Map of unknown type
else
{
sendMessage("\""+fieldName+"\" is not a data type map which can be modified with this command.");
return;
}
}
// not a Set or Map?
else
{
sendMessage("\""+fieldName+"\" is not a data collection type which can be modified with this command.");
return;
}
}
// unknown type
else
{
sendMessage("\""+fieldName+"\" is not a data type which can be modified with this command.");
return;
}
}
catch (NoSuchFieldException ex)
{
sendMessage("Configuration setting \""+fieldName+"\" couldn't be matched, though it should be... please report this error.");
return;
}
catch (IllegalAccessException ex)
{
sendMessage("Error setting configuration setting \""+fieldName+"\" to \""+value+"\".");
return;
}
if (!success.isEmpty())
{
sendMessage(success);
if (sender instanceof Player)
{
Factions.get().log(success + " Command was run by "+fme.getName()+".");
}
}
// save change to disk
ConfServer.save();
// in case some Spout related setting was changed
SpoutFeatures.updateTitle(null, null);
//SpoutFeatures.updateCape(null);
}
}

View File

@ -17,7 +17,7 @@ import com.massivecraft.factions.Faction;
import com.massivecraft.factions.Perm;
import com.massivecraft.factions.Rel;
import com.massivecraft.factions.integration.EssentialsFeatures;
import com.massivecraft.factions.zcore.util.SmokeUtil;
import com.massivecraft.mcore.util.SmokeUtil;
public class CmdHome extends FCommand

View File

@ -1,7 +1,6 @@
package com.massivecraft.factions.cmd;
import com.massivecraft.factions.Board;
import com.massivecraft.factions.ConfServer;
import com.massivecraft.factions.FPlayerColl;
import com.massivecraft.factions.FactionColl;
import com.massivecraft.factions.Factions;
@ -35,12 +34,7 @@ public class CmdReload extends FCommand
String fileName;
if (file.startsWith("c"))
{
ConfServer.load();
fileName = "conf.json";
}
else if (file.startsWith("b"))
if (file.startsWith("b"))
{
Board.load();
fileName = "board.json";
@ -58,7 +52,6 @@ public class CmdReload extends FCommand
else if (file.startsWith("a"))
{
fileName = "all";
ConfServer.load();
FPlayerColl.i.loadFromDisc();
FactionColl.i.loadFromDisc();
Board.load();
@ -66,7 +59,7 @@ public class CmdReload extends FCommand
else
{
Factions.get().log("RELOAD CANCELLED - SPECIFIED FILE INVALID");
msg("<b>Invalid file specified. <i>Valid files: all, conf, board, factions, players");
msg("<b>Invalid file specified. <i>Valid files: all, board, factions, players");
return;
}

View File

@ -1,7 +1,6 @@
package com.massivecraft.factions.cmd;
import com.massivecraft.factions.Board;
import com.massivecraft.factions.ConfServer;
import com.massivecraft.factions.FPlayerColl;
import com.massivecraft.factions.FactionColl;
import com.massivecraft.factions.Perm;
@ -33,7 +32,6 @@ public class CmdSaveAll extends FCommand
FPlayerColl.i.saveToDisc();
FactionColl.i.saveToDisc();
Board.save();
ConfServer.save();
msg("<i>Factions saved to disk!");
}

View File

@ -14,7 +14,6 @@ public class FCmdRoot extends FCommand
public CmdAdmin cmdBypass = new CmdAdmin();
public CmdCape cmdCape = new CmdCape();
public CmdClaim cmdClaim = new CmdClaim();
public CmdConfig cmdConfig = new CmdConfig();
public CmdCreate cmdCreate = new CmdCreate();
public CmdDeinvite cmdDeinvite = new CmdDeinvite();
public CmdDemote cmdDemote = new CmdDemote();
@ -110,7 +109,6 @@ public class FCmdRoot extends FCommand
this.addSubCommand(this.cmdPromote);
this.addSubCommand(this.cmdLock);
this.addSubCommand(this.cmdReload);
this.addSubCommand(this.cmdConfig);
this.addSubCommand(this.cmdSaveAll);
this.addSubCommand(this.cmdVersion);
}

View File

@ -8,7 +8,6 @@ import org.bukkit.plugin.java.JavaPlugin;
import com.massivecraft.factions.zcore.persist.EM;
import com.massivecraft.factions.zcore.persist.SaveTask;
import com.massivecraft.factions.zcore.util.Persist;
import com.massivecraft.mcore.util.Txt;
import com.massivecraft.mcore.xlib.gson.Gson;
import com.massivecraft.mcore.xlib.gson.GsonBuilder;
@ -16,9 +15,6 @@ import com.massivecraft.mcore.xlib.gson.GsonBuilder;
public abstract class MPlugin extends JavaPlugin
{
// Some utils
public Persist persist;
// Persist related
public Gson gson;
private Integer saveTask = null;
@ -42,9 +38,6 @@ public abstract class MPlugin extends JavaPlugin
// Ensure basefolder exists!
this.getDataFolder().mkdirs();
// Create Utility Instances
this.persist = new Persist(this);
// GSON 2.1 is now embedded in CraftBukkit, used by the auto-updater: https://github.com/Bukkit/CraftBukkit/commit/0ed1d1fdbb1e0bc09a70bc7bfdf40c1de8411665
// if ( ! lib.require("gson.jar", "http://search.maven.org/remotecontent?filepath=com/google/code/gson/gson/2.1/gson-2.1.jar")) return false;
this.gson = this.getGsonBuilder().create();

View File

@ -1,91 +0,0 @@
package com.massivecraft.factions.zcore.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.clone(), 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.clone());
}
}
public static void spawnCloudRandom(Collection<Location> locations, float thickness)
{
for (Location location : locations)
{
spawnCloudRandom(location, thickness);
}
}
// -------------------------------------------- //
// Attach continuous effects to or locations
// -------------------------------------------- //
// TODO
}