Archived
0

Added "data import" to allow modules importing data easily

This commit is contained in:
Pepich
2017-05-18 13:27:57 +02:00
parent 33d1e25632
commit c35c7df4c8

View File

@@ -4,6 +4,7 @@ import java.io.File;
import java.io.FilenameFilter; import java.io.FilenameFilter;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.util.UUID;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
@@ -14,6 +15,8 @@ import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerQuitEvent; import org.bukkit.event.player.PlayerQuitEvent;
import org.json.simple.JSONObject; import org.json.simple.JSONObject;
import com.nemez.cmdmgr.Command;
import com.nemez.cmdmgr.CommandManager;
import com.redstoner.annotations.AutoRegisterListener; import com.redstoner.annotations.AutoRegisterListener;
import com.redstoner.annotations.Version; import com.redstoner.annotations.Version;
import com.redstoner.coremods.moduleLoader.ModuleLoader; import com.redstoner.coremods.moduleLoader.ModuleLoader;
@@ -24,7 +27,7 @@ import com.redstoner.modules.CoreModule;
import com.redstoner.modules.Module; import com.redstoner.modules.Module;
@AutoRegisterListener @AutoRegisterListener
@Version(major = 3, minor = 1, revision = 1, compatible = 3) @Version(major = 3, minor = 1, revision = 2, compatible = 3)
public final class DataManager implements CoreModule, Listener public final class DataManager implements CoreModule, Listener
{ {
protected final File dataFolder = new File(Main.plugin.getDataFolder(), "data"); protected final File dataFolder = new File(Main.plugin.getDataFolder(), "data");
@@ -39,6 +42,7 @@ public final class DataManager implements CoreModule, Listener
{ {
loadData_(p.getUniqueId().toString()); loadData_(p.getUniqueId().toString());
} }
CommandManager.registerCommand(getClass().getResourceAsStream("DataManager.cmd"), this, Main.plugin);
} }
@Override @Override
@@ -50,6 +54,21 @@ public final class DataManager implements CoreModule, Listener
} }
} }
@Command(hook = "import_file")
public boolean importFile(CommandSender sender, String file, String module)
{
try
{
JSONObject object = JsonManager.getObject(new File(file));
importObject_(module, object);
}
catch (Exception e)
{
Utils.sendErrorMessage(sender, null, "Could not import data!");
}
return true;
}
@EventHandler @EventHandler
public void onPlayerJoin(PlayerJoinEvent event) public void onPlayerJoin(PlayerJoinEvent event)
{ {
@@ -141,6 +160,9 @@ public final class DataManager implements CoreModule, Listener
JSONObject moduleData = ((JSONObject) ((JSONObject) data.get(id)).get(module)); JSONObject moduleData = ((JSONObject) ((JSONObject) data.get(id)).get(module));
if (moduleData == null) if (moduleData == null)
return null; return null;
if (key == null)
return moduleData;
else
return moduleData.get(key); return moduleData.get(key);
} }
else else
@@ -152,6 +174,9 @@ public final class DataManager implements CoreModule, Listener
JSONObject playerData = JsonManager.getObject(new File(dataFolder, id + ".json")); JSONObject playerData = JsonManager.getObject(new File(dataFolder, id + ".json"));
if (playerData == null) if (playerData == null)
return null; return null;
if (key == null)
return playerData.get(module);
else
return ((JSONObject) playerData.get(module)).get(key); return ((JSONObject) playerData.get(module)).get(key);
} }
@@ -174,7 +199,6 @@ public final class DataManager implements CoreModule, Listener
{} {}
} }
@SuppressWarnings("unchecked")
public void setData_(CommandSender sender, String module, String key, Object value) public void setData_(CommandSender sender, String module, String key, Object value)
{ {
String id; String id;
@@ -182,6 +206,12 @@ public final class DataManager implements CoreModule, Listener
id = ((Player) sender).getUniqueId().toString(); id = ((Player) sender).getUniqueId().toString();
else else
id = "CONSOLE"; id = "CONSOLE";
setData_(id, module, key, value);
}
@SuppressWarnings("unchecked")
protected void setData_(String id, String module, String key, Object value)
{
if (data.containsKey(id)) if (data.containsKey(id))
{ {
JSONObject moduleData = ((JSONObject) ((JSONObject) data.get(id)).get(module)); JSONObject moduleData = ((JSONObject) ((JSONObject) data.get(id)).get(module));
@@ -190,13 +220,58 @@ public final class DataManager implements CoreModule, Listener
moduleData = new JSONObject(); moduleData = new JSONObject();
((JSONObject) data.get(id)).put(module, moduleData); ((JSONObject) data.get(id)).put(module, moduleData);
} }
if (key == null)
setDirectly_(id, module, value);
else
moduleData.put(key, value); moduleData.put(key, value);
save_(sender); save_(id);
} }
else else
loadAndSet(id, module, key, value); loadAndSet(id, module, key, value);
} }
public static void setDirectly(CommandSender sender, Object value)
{
setData(sender, Utils.getCaller("DataManager"), value);
}
public static void setDirectly(CommandSender sender, String module, Object value)
{
try
{
Module mod = ModuleLoader.getModule("DataManager");
Method m = mod.getClass().getDeclaredMethod("setDirectly_", CommandSender.class, String.class,
Object.class);
m.invoke(mod, sender, module, value);
}
catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException
| InvocationTargetException e)
{}
}
public void setDirectly_(CommandSender sender, String module, Object value)
{
String id;
if (sender instanceof Player)
id = ((Player) sender).getUniqueId().toString();
else
id = "CONSOLE";
setDirectly_(id, module, value);
}
@SuppressWarnings("unchecked")
protected void setDirectly_(String id, String module, Object value)
{
if (data.containsKey(id))
{
JSONObject playerdata = (JSONObject) data.get(id);
playerdata.put(module, value);
save_(id);
}
else
loadAndSetDirectly(id, module, value);
}
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
protected void loadAndSet(String id, String module, String key, Object value) protected void loadAndSet(String id, String module, String key, Object value)
{ {
@@ -214,6 +289,17 @@ public final class DataManager implements CoreModule, Listener
JsonManager.save(playerData, dataFile); JsonManager.save(playerData, dataFile);
} }
@SuppressWarnings("unchecked")
protected void loadAndSetDirectly(String id, String module, Object value)
{
File dataFile = new File(dataFolder, id + ".json");
JSONObject playerData = JsonManager.getObject(dataFile);
if (playerData == null)
playerData = new JSONObject();
playerData.put(module, value);
JsonManager.save(playerData, dataFile);
}
public static void removeData(CommandSender sender, String key) public static void removeData(CommandSender sender, String key)
{ {
removeData(sender, Utils.getCaller("DataManager"), key); removeData(sender, Utils.getCaller("DataManager"), key);
@@ -264,6 +350,39 @@ public final class DataManager implements CoreModule, Listener
JsonManager.save(playerData, dataFile); JsonManager.save(playerData, dataFile);
} }
public static void importObject(JSONObject object)
{
importObject(object, Utils.getCaller("DataManager"));
}
public static void importObject(JSONObject object, String module)
{
try
{
Module mod = ModuleLoader.getModule("DataManager");
Method m = mod.getClass().getDeclaredMethod("importObject_", String.class, String.class, String.class);
m.invoke(mod, module, object);
}
catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException
| InvocationTargetException e)
{}
}
protected void importObject_(String module, JSONObject object)
{
for (Object o : object.keySet())
{
String uid = null;
if (o instanceof String)
uid = (String) o;
else if (o instanceof UUID)
uid = ((UUID) o).toString();
if (uid == null)
continue;
setDirectly_(uid, module, object.get(o));
}
}
public static void migrateAll(String oldName) public static void migrateAll(String oldName)
{ {
migrateAll(oldName, Utils.getCaller("DataManager")); migrateAll(oldName, Utils.getCaller("DataManager"));