From 03c97984a669ef0a715a7c6cad155fa249dd0513 Mon Sep 17 00:00:00 2001 From: sekwah Date: Wed, 24 Jan 2018 10:05:46 +0000 Subject: [PATCH] Started working on portal data saving --- Resources/lang/en_GB.lang | 1 + .../core/AdvancedPortalsCore.java | 34 +++++++++++++---- .../advancedportals/core/CoreListeners.java | 11 ++++++ .../core/api/effect/TestEffect.java | 4 +- .../core/api/effect/WarpEffect.java | 4 +- .../core/api/managers/DestinationManager.java | 15 ++++++++ .../core/api/managers/PortalManager.java | 38 ++++++++++++++----- .../{Portal.java => AdvancedPortal.java} | 2 +- .../subcommands/portal/ReloadSubCommand.java | 4 ++ .../portal/TransUpdateSubCommand.java | 2 +- .../core/data/PlayerLocation.java | 14 +++++++ .../advancedportals/core/util/Config.java | 2 +- .../core/util/DataStorage.java | 11 +++++- .../container/PlayerContainer.java | 4 ++ 14 files changed, 121 insertions(+), 25 deletions(-) rename src/com/sekwah/advancedportals/core/api/portal/{Portal.java => AdvancedPortal.java} (73%) create mode 100644 src/com/sekwah/advancedportals/core/data/PlayerLocation.java diff --git a/Resources/lang/en_GB.lang b/Resources/lang/en_GB.lang index a28e1b2c..c22c5c73 100644 --- a/Resources/lang/en_GB.lang +++ b/Resources/lang/en_GB.lang @@ -31,6 +31,7 @@ command.help.invalidnum= Sorry but \u00A7e%1$s\u00A7c is not a valid page number command.reload.help=Reloads portal data command.reload.detailedhelp=Reloads all portal data from files in the data folder +command.reload.reloaded= All Advanced Portals data reloaded command.version.help=Returns the current version of the plugin diff --git a/src/com/sekwah/advancedportals/core/AdvancedPortalsCore.java b/src/com/sekwah/advancedportals/core/AdvancedPortalsCore.java index 09ffe289..eae40726 100644 --- a/src/com/sekwah/advancedportals/core/AdvancedPortalsCore.java +++ b/src/com/sekwah/advancedportals/core/AdvancedPortalsCore.java @@ -1,6 +1,8 @@ package com.sekwah.advancedportals.core; import com.sekwah.advancedportals.core.api.commands.SubCommand; +import com.sekwah.advancedportals.core.api.managers.DestinationManager; +import com.sekwah.advancedportals.core.api.managers.PortalManager; import com.sekwah.advancedportals.core.commands.CommandWithSubCommands; import com.sekwah.advancedportals.core.commands.subcommands.portal.ReloadSubCommand; import com.sekwah.advancedportals.core.commands.subcommands.portal.TransUpdateSubCommand; @@ -25,6 +27,9 @@ public class AdvancedPortalsCore { private CommandWithSubCommands portalCommand; private CommandWithSubCommands destiCommand; + private PortalManager portalManager; + private DestinationManager destiManager; + public static final String version = "1.0.0"; public static final String lastTranslationUpdate = "1.0.0"; @@ -33,7 +38,9 @@ public class AdvancedPortalsCore { this.infoLogger = infoLogger; this.instance = this; this.commandRegister = commandRegister; - this.coreListeners = new CoreListeners(); + this.coreListeners = new CoreListeners(this); + this.portalManager = new PortalManager(this); + this.destiManager = new DestinationManager(this); this.onEnable(); } @@ -50,6 +57,8 @@ public class AdvancedPortalsCore { this.registerPortalCommand(); this.registerDestinationCommand(); + this.portalManager.loadPortals(); + this.infoLogger.log(Lang.translate("logger.pluginenable")); } @@ -80,28 +89,39 @@ public class AdvancedPortalsCore { * Loads the portal config into the memory and saves from the memory to check in case certain things have changed * (basically if values are missing or whatever) */ - private void loadPortalConfig() { + public void loadPortalConfig() { this.config = this.dataStorage.loadJson(Config.class, "config.json"); this.dataStorage.storeJson(this.config, "config.json"); } + /** + * This cannot be called to disable the plugin, it just performs any saves or anything needed after if they are required + */ public void onDisable() { this.infoLogger.log(Lang.translate("logger.plugindisable")); } - private static AdvancedPortalsCore getInstance() { + public static AdvancedPortalsCore getInstance() { return instance; } - public static DataStorage getDataStorage() { - return instance.dataStorage; + public DataStorage getDataStorage() { + return this.dataStorage; } public static InfoLogger getInfoLogger() { return instance.infoLogger; } - public CoreListeners getCoreListeners() { - return this.coreListeners; + public static CoreListeners getCoreListeners() { + return instance.coreListeners; + } + + public static PortalManager getPortalManager() { + return instance.portalManager; + } + + public static DestinationManager getDestiManager() { + return instance.destiManager; } } diff --git a/src/com/sekwah/advancedportals/core/CoreListeners.java b/src/com/sekwah/advancedportals/core/CoreListeners.java index a40126b4..b2fa6c50 100644 --- a/src/com/sekwah/advancedportals/core/CoreListeners.java +++ b/src/com/sekwah/advancedportals/core/CoreListeners.java @@ -1,10 +1,17 @@ package com.sekwah.advancedportals.core; +import com.sekwah.advancedportals.core.data.PlayerLocation; import com.sekwah.advancedportals.core.util.Lang; import com.sekwah.advancedportals.coreconnector.container.PlayerContainer; public class CoreListeners { + private final AdvancedPortalsCore portalsCore; + + public CoreListeners(AdvancedPortalsCore portalsCore) { + this.portalsCore = portalsCore; + } + public void playerJoin(PlayerContainer player) { if(player.isOp()) { if(!Lang.translate("translatedata.lastchange").equals(AdvancedPortalsCore.lastTranslationUpdate)) { @@ -16,4 +23,8 @@ public class CoreListeners { } } + public void playerMove(PlayerContainer player, PlayerLocation fromLoc, PlayerLocation toLoc) { + + } + } diff --git a/src/com/sekwah/advancedportals/core/api/effect/TestEffect.java b/src/com/sekwah/advancedportals/core/api/effect/TestEffect.java index d5976645..ae3826f1 100644 --- a/src/com/sekwah/advancedportals/core/api/effect/TestEffect.java +++ b/src/com/sekwah/advancedportals/core/api/effect/TestEffect.java @@ -1,6 +1,6 @@ package com.sekwah.advancedportals.core.api.effect; -import com.sekwah.advancedportals.core.api.portal.Portal; +import com.sekwah.advancedportals.core.api.portal.AdvancedPortal; import com.sekwah.advancedportals.core.data.PortalLocation; import com.sekwah.advancedportals.coreconnector.container.PlayerContainer; @@ -10,7 +10,7 @@ import com.sekwah.advancedportals.coreconnector.container.PlayerContainer; public class TestEffect implements WarpEffect { @Override - public void onWarp(PlayerContainer player, PortalLocation loc, Action action, Portal portal) { + public void onWarp(PlayerContainer player, PortalLocation loc, Action action, AdvancedPortal portal) { } diff --git a/src/com/sekwah/advancedportals/core/api/effect/WarpEffect.java b/src/com/sekwah/advancedportals/core/api/effect/WarpEffect.java index 2f3715ca..7dc99895 100644 --- a/src/com/sekwah/advancedportals/core/api/effect/WarpEffect.java +++ b/src/com/sekwah/advancedportals/core/api/effect/WarpEffect.java @@ -1,6 +1,6 @@ package com.sekwah.advancedportals.core.api.effect; -import com.sekwah.advancedportals.core.api.portal.Portal; +import com.sekwah.advancedportals.core.api.portal.AdvancedPortal; import com.sekwah.advancedportals.core.data.PortalLocation; import com.sekwah.advancedportals.coreconnector.container.PlayerContainer; @@ -15,7 +15,7 @@ import com.sekwah.advancedportals.coreconnector.container.PlayerContainer; */ public interface WarpEffect { - void onWarp(PlayerContainer player, PortalLocation loc, Action action, Portal portal); + void onWarp(PlayerContainer player, PortalLocation loc, Action action, AdvancedPortal portal); Type getType(); diff --git a/src/com/sekwah/advancedportals/core/api/managers/DestinationManager.java b/src/com/sekwah/advancedportals/core/api/managers/DestinationManager.java index c60339ad..b59f4151 100644 --- a/src/com/sekwah/advancedportals/core/api/managers/DestinationManager.java +++ b/src/com/sekwah/advancedportals/core/api/managers/DestinationManager.java @@ -1,7 +1,22 @@ package com.sekwah.advancedportals.core.api.managers; +import com.sekwah.advancedportals.core.AdvancedPortalsCore; +import com.sekwah.advancedportals.core.api.destination.Destination; + +import java.util.HashMap; + /** * @author sekwah41 */ public class DestinationManager { + + private final AdvancedPortalsCore portalsCore; + /** + * Contains all the data for the destinations + */ + private HashMap destiHashMap = new HashMap<>(); + + public DestinationManager(AdvancedPortalsCore portalsCore) { + this.portalsCore = portalsCore; + } } diff --git a/src/com/sekwah/advancedportals/core/api/managers/PortalManager.java b/src/com/sekwah/advancedportals/core/api/managers/PortalManager.java index 879975d6..408d18db 100644 --- a/src/com/sekwah/advancedportals/core/api/managers/PortalManager.java +++ b/src/com/sekwah/advancedportals/core/api/managers/PortalManager.java @@ -1,9 +1,13 @@ package com.sekwah.advancedportals.core.api.managers; -import com.sekwah.advancedportals.core.api.portal.Portal; +import com.google.gson.reflect.TypeToken; +import com.sekwah.advancedportals.core.AdvancedPortalsCore; +import com.sekwah.advancedportals.core.api.portal.AdvancedPortal; import com.sekwah.advancedportals.coreconnector.container.PlayerContainer; +import java.lang.reflect.Type; import java.util.HashMap; +import java.util.Map; /** * When a player leaves the server any data stored on them is removed to free memory. @@ -17,7 +21,10 @@ public class PortalManager { */ private static final int COOLDOWN = 0; - private static PortalManager instance = new PortalManager(); + private static PortalManager instance; + + private final AdvancedPortalsCore portalsCore; + /** * Store data of when the player last entered the portal */ @@ -25,10 +32,16 @@ public class PortalManager { /** * Tracks what portal a player has selected */ - private HashMap selectedPortal = new HashMap(); + private HashMap selectedPortal = new HashMap(); - public PortalManager() { - this.loadPortals(); + /** + * Contains all the data for the portals + */ + private HashMap portalHashMap; + + public PortalManager(AdvancedPortalsCore portalsCore) { + this.instance = this; + this.portalsCore = portalsCore; } /** @@ -36,15 +49,20 @@ public class PortalManager { * * @param player */ - public static void playerLeave(PlayerContainer player) { - instance.lastAttempt.remove(player.getUUID()); - instance.selectedPortal.remove(player.getUUID()); + public void playerLeave(PlayerContainer player) { + this.lastAttempt.remove(player.getUUID()); + this.selectedPortal.remove(player.getUUID()); } /** * Load the default data into the portals. */ - private void loadPortals() { - + public void loadPortals() { + Type type = new TypeToken>(){}.getType(); + this.portalHashMap = this.portalsCore.getDataStorage().loadJson(type ,"portals.json"); + if(this.portalHashMap == null) { + this.portalHashMap = new HashMap<>(); + } + this.portalsCore.getDataStorage().storeJson(this.portalHashMap, "config.json"); } } diff --git a/src/com/sekwah/advancedportals/core/api/portal/Portal.java b/src/com/sekwah/advancedportals/core/api/portal/AdvancedPortal.java similarity index 73% rename from src/com/sekwah/advancedportals/core/api/portal/Portal.java rename to src/com/sekwah/advancedportals/core/api/portal/AdvancedPortal.java index 5919a911..c0a2d451 100644 --- a/src/com/sekwah/advancedportals/core/api/portal/Portal.java +++ b/src/com/sekwah/advancedportals/core/api/portal/AdvancedPortal.java @@ -3,5 +3,5 @@ package com.sekwah.advancedportals.core.api.portal; /** * @author sekwah41 */ -public class Portal { +public class AdvancedPortal { } diff --git a/src/com/sekwah/advancedportals/core/commands/subcommands/portal/ReloadSubCommand.java b/src/com/sekwah/advancedportals/core/commands/subcommands/portal/ReloadSubCommand.java index 9e61d5f9..0ebefbc1 100644 --- a/src/com/sekwah/advancedportals/core/commands/subcommands/portal/ReloadSubCommand.java +++ b/src/com/sekwah/advancedportals/core/commands/subcommands/portal/ReloadSubCommand.java @@ -2,6 +2,7 @@ package com.sekwah.advancedportals.core.commands.subcommands.portal; import com.sekwah.advancedportals.core.AdvancedPortalsCore; import com.sekwah.advancedportals.core.api.commands.SubCommand; +import com.sekwah.advancedportals.core.api.portal.AdvancedPortal; import com.sekwah.advancedportals.core.util.Lang; import com.sekwah.advancedportals.coreconnector.container.CommandSenderContainer; @@ -10,6 +11,9 @@ import java.util.List; public class ReloadSubCommand implements SubCommand { @Override public void onCommand(CommandSenderContainer sender, String[] args) { + AdvancedPortalsCore.getInstance().loadPortalConfig(); + AdvancedPortalsCore.getPortalManager().loadPortals(); + sender.sendMessage(Lang.translateColor("messageprefix.positive") + Lang.translateColor("command.reload.reloaded")); } @Override diff --git a/src/com/sekwah/advancedportals/core/commands/subcommands/portal/TransUpdateSubCommand.java b/src/com/sekwah/advancedportals/core/commands/subcommands/portal/TransUpdateSubCommand.java index ba056db7..f45f54bb 100644 --- a/src/com/sekwah/advancedportals/core/commands/subcommands/portal/TransUpdateSubCommand.java +++ b/src/com/sekwah/advancedportals/core/commands/subcommands/portal/TransUpdateSubCommand.java @@ -10,7 +10,7 @@ import java.util.List; public class TransUpdateSubCommand implements SubCommand { @Override public void onCommand(CommandSenderContainer sender, String[] args) { - AdvancedPortalsCore.getDataStorage().copyDefaultFile("lang/en_GB.lang", true); + AdvancedPortalsCore.getInstance().getDataStorage().copyDefaultFile("lang/en_GB.lang", true); sender.sendMessage(Lang.translateColor("messageprefix.positive") + Lang.translateColor("translatedata.replaced")); } diff --git a/src/com/sekwah/advancedportals/core/data/PlayerLocation.java b/src/com/sekwah/advancedportals/core/data/PlayerLocation.java new file mode 100644 index 00000000..88ad306e --- /dev/null +++ b/src/com/sekwah/advancedportals/core/data/PlayerLocation.java @@ -0,0 +1,14 @@ +package com.sekwah.advancedportals.core.data; + +public class PlayerLocation { + + public final double posX; + public final double posY; + public final double posZ; + + public PlayerLocation(double posX, double posY, double posZ) { + this.posX = posX; + this.posY = posY; + this.posZ = posZ; + } +} diff --git a/src/com/sekwah/advancedportals/core/util/Config.java b/src/com/sekwah/advancedportals/core/util/Config.java index 30bd7061..709dff87 100644 --- a/src/com/sekwah/advancedportals/core/util/Config.java +++ b/src/com/sekwah/advancedportals/core/util/Config.java @@ -7,7 +7,7 @@ public class Config { private boolean useOnlySpecialAxe; - private String axeMaterial = "IRON_AXE"; + private String selectorMaterial = "IRON_AXE"; private boolean portalProtection = true; diff --git a/src/com/sekwah/advancedportals/core/util/DataStorage.java b/src/com/sekwah/advancedportals/core/util/DataStorage.java index 9e685c5c..ae3e20b8 100644 --- a/src/com/sekwah/advancedportals/core/util/DataStorage.java +++ b/src/com/sekwah/advancedportals/core/util/DataStorage.java @@ -5,6 +5,7 @@ import com.google.gson.GsonBuilder; import com.sekwah.advancedportals.core.AdvancedPortalsCore; import java.io.*; +import java.lang.reflect.Type; public class DataStorage { @@ -33,8 +34,16 @@ public class DataStorage { } } + public T loadJson(Type dataHolder, String location) { + InputStream jsonResource = this.loadResource(location); + if(jsonResource == null) { + return null; + } + BufferedReader bufReader = new BufferedReader(new InputStreamReader(jsonResource)); + T object = gson.fromJson(bufReader, dataHolder); + return object; + } public T loadJson(Class dataHolder, String location) { - // TODO get json and if file doesnt exist create default class if true InputStream jsonResource = this.loadResource(location); if(jsonResource == null) { try { diff --git a/src/com/sekwah/advancedportals/coreconnector/container/PlayerContainer.java b/src/com/sekwah/advancedportals/coreconnector/container/PlayerContainer.java index cece56d9..3728c385 100644 --- a/src/com/sekwah/advancedportals/coreconnector/container/PlayerContainer.java +++ b/src/com/sekwah/advancedportals/coreconnector/container/PlayerContainer.java @@ -26,4 +26,8 @@ public class PlayerContainer { public boolean isOp() { return player.isOp(); } + + public PlayerLocation getLoc() { + return null; + } }