From 24270e7929b08976fcf8d3473599876aea4e3315 Mon Sep 17 00:00:00 2001 From: Olof Larsson Date: Wed, 10 Apr 2013 10:53:53 +0200 Subject: [PATCH] Geting rid of persist and prepare ConfServer for SimpleConfig usage. --- src/com/massivecraft/factions/ConfServer.java | 13 +- src/com/massivecraft/factions/Factions.java | 8 +- .../massivecraft/factions/cmd/CmdConfig.java | 400 ------------------ .../massivecraft/factions/cmd/CmdHome.java | 2 +- .../massivecraft/factions/cmd/CmdReload.java | 11 +- .../massivecraft/factions/cmd/CmdSaveAll.java | 2 - .../massivecraft/factions/cmd/FCmdRoot.java | 2 - .../massivecraft/factions/zcore/MPlugin.java | 9 +- .../factions/zcore/util/SmokeUtil.java | 91 ---- 9 files changed, 12 insertions(+), 526 deletions(-) delete mode 100644 src/com/massivecraft/factions/cmd/CmdConfig.java delete mode 100644 src/com/massivecraft/factions/zcore/util/SmokeUtil.java diff --git a/src/com/massivecraft/factions/ConfServer.java b/src/com/massivecraft/factions/ConfServer.java index c7bdea9a..1ae5b83d 100644 --- a/src/com/massivecraft/factions/ConfServer.java +++ b/src/com/massivecraft/factions/ConfServer.java @@ -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 diff --git a/src/com/massivecraft/factions/Factions.java b/src/com/massivecraft/factions/Factions.java index da7dcb41..71d058cb 100644 --- a/src/com/massivecraft/factions/Factions.java +++ b/src/com/massivecraft/factions/Factions.java @@ -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 diff --git a/src/com/massivecraft/factions/cmd/CmdConfig.java b/src/com/massivecraft/factions/cmd/CmdConfig.java deleted file mode 100644 index 23271eb6..00000000 --- a/src/com/massivecraft/factions/cmd/CmdConfig.java +++ /dev/null @@ -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 properFieldNames = new HashMap(); - - 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("No configuration setting \"%s\" 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 - 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 matSet = (Set)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 - else if (innerType == String.class) - { - @SuppressWarnings("unchecked") - Set stringSet = (Set)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 - 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 map = (Map)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> - 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> map = (Map>)target.get(null); - - Set relSet = map.get(newPerm); - if (relSet == null) - relSet = new HashSet(); - - // 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); - } - -} diff --git a/src/com/massivecraft/factions/cmd/CmdHome.java b/src/com/massivecraft/factions/cmd/CmdHome.java index ca08cc1c..c26414c9 100644 --- a/src/com/massivecraft/factions/cmd/CmdHome.java +++ b/src/com/massivecraft/factions/cmd/CmdHome.java @@ -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 diff --git a/src/com/massivecraft/factions/cmd/CmdReload.java b/src/com/massivecraft/factions/cmd/CmdReload.java index 8c3d8c32..4fb50b2e 100644 --- a/src/com/massivecraft/factions/cmd/CmdReload.java +++ b/src/com/massivecraft/factions/cmd/CmdReload.java @@ -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("Invalid file specified. Valid files: all, conf, board, factions, players"); + msg("Invalid file specified. Valid files: all, board, factions, players"); return; } diff --git a/src/com/massivecraft/factions/cmd/CmdSaveAll.java b/src/com/massivecraft/factions/cmd/CmdSaveAll.java index 0d9c4b2f..f0f42783 100644 --- a/src/com/massivecraft/factions/cmd/CmdSaveAll.java +++ b/src/com/massivecraft/factions/cmd/CmdSaveAll.java @@ -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("Factions saved to disk!"); } diff --git a/src/com/massivecraft/factions/cmd/FCmdRoot.java b/src/com/massivecraft/factions/cmd/FCmdRoot.java index dedbf23c..0afc2a54 100644 --- a/src/com/massivecraft/factions/cmd/FCmdRoot.java +++ b/src/com/massivecraft/factions/cmd/FCmdRoot.java @@ -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); } diff --git a/src/com/massivecraft/factions/zcore/MPlugin.java b/src/com/massivecraft/factions/zcore/MPlugin.java index 2007f527..0d1667bd 100644 --- a/src/com/massivecraft/factions/zcore/MPlugin.java +++ b/src/com/massivecraft/factions/zcore/MPlugin.java @@ -8,17 +8,13 @@ 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; public abstract class MPlugin extends JavaPlugin -{ - // Some utils - public Persist persist; - +{ // Persist related public Gson gson; private Integer saveTask = null; @@ -41,9 +37,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; diff --git a/src/com/massivecraft/factions/zcore/util/SmokeUtil.java b/src/com/massivecraft/factions/zcore/util/SmokeUtil.java deleted file mode 100644 index 05f718ef..00000000 --- a/src/com/massivecraft/factions/zcore/util/SmokeUtil.java +++ /dev/null @@ -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 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 locations, float thickness) - { - for (Location location : locations) - { - spawnCloudRandom(location, thickness); - } - } - - // -------------------------------------------- // - // Attach continuous effects to or locations - // -------------------------------------------- // - - // TODO - -}