From 1273936d763f0dd04b3481a7a687a924701ff561 Mon Sep 17 00:00:00 2001 From: SeanOMik Date: Mon, 6 Jul 2020 23:31:14 -0500 Subject: [PATCH] Add reload command and fix a small bug. The bug is it not loading systems by players that are not online when the plugin loads, or when the player doesn't join. --- pom.xml | 2 +- .../energeticstorage/EnergeticStorage.java | 12 ++-- .../commands/ESReloadCommand.java | 31 ++++++++++ .../energeticstorage/files/PlayersFile.java | 62 +++++++++++++++++++ .../energeticstorage/utils/Utils.java | 2 +- src/main/resources/plugin.yml | 4 ++ target/classes/config.yml | 2 +- target/classes/plugin.yml | 6 +- target/maven-archiver/pom.properties | 4 +- .../compile/default-compile/inputFiles.lst | 1 + 10 files changed, 114 insertions(+), 12 deletions(-) create mode 100644 src/main/java/net/seanomik/energeticstorage/commands/ESReloadCommand.java diff --git a/pom.xml b/pom.xml index a947258..4011d3b 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ net.seanomik energeticstorage - 0.1-SNAPSHOT + 0.2-SNAPSHOT jar EnergeticStorage diff --git a/src/main/java/net/seanomik/energeticstorage/EnergeticStorage.java b/src/main/java/net/seanomik/energeticstorage/EnergeticStorage.java index 2b5e77f..73576af 100644 --- a/src/main/java/net/seanomik/energeticstorage/EnergeticStorage.java +++ b/src/main/java/net/seanomik/energeticstorage/EnergeticStorage.java @@ -1,6 +1,7 @@ package net.seanomik.energeticstorage; import net.seanomik.energeticstorage.commands.ESGiveCommand; +import net.seanomik.energeticstorage.commands.ESReloadCommand; import net.seanomik.energeticstorage.files.PlayersFile; import net.seanomik.energeticstorage.listeners.BlockBreakListener; import net.seanomik.energeticstorage.listeners.BlockPlaceListener; @@ -28,13 +29,12 @@ public final class EnergeticStorage extends JavaPlugin implements Listener { PlayersFile.getConfig().saveDefaultConfig(); - for (Player player : Bukkit.getOnlinePlayers()) { - cachePlayersSystems(player); - } + Reference.ES_SYSTEMS = PlayersFile.getAllSystems(); } private void registerCommands() { getCommand("esgive").setExecutor(new ESGiveCommand()); + getCommand("esreload").setExecutor(new ESReloadCommand()); } private void registerListener() { @@ -48,17 +48,17 @@ public final class EnergeticStorage extends JavaPlugin implements Listener { getServer().getPluginManager().registerEvents(this, this); } - public void cachePlayersSystems(Player player) { + /*public void cachePlayersSystems(Player player) { if (PlayersFile.doesPlayerHaveSystem(player.getUniqueId())) { Reference.ES_SYSTEMS.put(player.getUniqueId(), PlayersFile.getPlayersSystems(player.getUniqueId())); } - } + }*/ @EventHandler public void onPlayerJoin(PlayerJoinEvent event) { Player player = event.getPlayer(); - cachePlayersSystems(player); + //cachePlayersSystems(player); } @Override diff --git a/src/main/java/net/seanomik/energeticstorage/commands/ESReloadCommand.java b/src/main/java/net/seanomik/energeticstorage/commands/ESReloadCommand.java new file mode 100644 index 0000000..f3221fb --- /dev/null +++ b/src/main/java/net/seanomik/energeticstorage/commands/ESReloadCommand.java @@ -0,0 +1,31 @@ +package net.seanomik.energeticstorage.commands; + +import net.seanomik.energeticstorage.files.ConfigFile; +import net.seanomik.energeticstorage.files.PlayersFile; +import net.seanomik.energeticstorage.utils.ItemConstructor; +import net.seanomik.energeticstorage.utils.PermissionChecks; +import net.seanomik.energeticstorage.utils.Reference; +import org.bukkit.Bukkit; +import org.bukkit.ChatColor; +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.command.TabExecutor; +import org.bukkit.entity.Player; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +public class ESReloadCommand implements CommandExecutor { + @Override + public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { + ConfigFile.getConfig().reloadConfig(); + PlayersFile.getConfig().reloadConfig(); + + // Recache the systems + Reference.ES_SYSTEMS = PlayersFile.getAllSystems(); + + return true; + } +} diff --git a/src/main/java/net/seanomik/energeticstorage/files/PlayersFile.java b/src/main/java/net/seanomik/energeticstorage/files/PlayersFile.java index 99c34b0..dea8fc2 100644 --- a/src/main/java/net/seanomik/energeticstorage/files/PlayersFile.java +++ b/src/main/java/net/seanomik/energeticstorage/files/PlayersFile.java @@ -99,6 +99,68 @@ public class PlayersFile extends YamlConfiguration { return getConfig().contains("players." + uuid + ".systems"); } + public static Map> getAllSystems() { + Map> allSystems = new HashMap<>(); + + for (String playerUUID : getConfig().getConfigurationSection("players").getKeys(false)) { + List playersSystems = new ArrayList<>(); + for (String systemUUID : getConfig().getConfigurationSection("players." + playerUUID + ".systems").getKeys(false)) { + String systemPath = "players." + playerUUID + ".systems." + systemUUID + "."; + List drives = new ArrayList<>(); + + if (getConfig().contains(systemPath + "drives")) { + for (String driveUUID : getConfig().getConfigurationSection(systemPath + "drives").getKeys(false)) { + + Map items = new HashMap(); + if (getConfig().contains(systemPath + "drives." + driveUUID + ".items")) { + try { + JSONParser jsonParser = new JSONParser(); + JSONArray itemJsonArray = (JSONArray) jsonParser.parse(getConfig().getString(systemPath + "drives." + driveUUID + ".items")); + + for (int i = 0; i < itemJsonArray.size(); i++) { + JSONObject itemObject = (JSONObject) itemJsonArray.get(i); + + Map.Entry item = ItemSerialization.deserializeItem((String) itemObject.get("itemYAML")); + + items.put(item.getKey(), item.getValue()); + } + } catch (ParseException | InvalidConfigurationException e) { + e.printStackTrace(); + } + } + + int size = getConfig().getInt(systemPath + "drives." + driveUUID + ".size"); + + drives.add(new ESDrive(size, items)); + } + } + + List trustedUUIDs = new ArrayList<>(); + if (getConfig().contains(systemPath + "trustedUUIDs")) { + try { + JSONArray trustedJson = (JSONArray) new JSONParser().parse(getConfig().getString(systemPath + "trustedUUIDs")); + for (int i = 0; i < trustedJson.size(); i++) { + JSONObject object = (JSONObject) trustedJson.get(i); + + trustedUUIDs.add(UUID.fromString((String) object.get("UUID"))); + } + } catch (ParseException e) { + e.printStackTrace(); + } + } + + boolean isPublic = getConfig().getBoolean(systemPath + "public"); + + Location loc = Utils.convertStringToLocation(getConfig().getString(systemPath + "loc")); + playersSystems.add(new ESSystem(UUID.fromString(playerUUID), UUID.fromString(systemUUID), loc, drives, trustedUUIDs, isPublic)); + } + + allSystems.put(UUID.fromString(playerUUID), playersSystems); + } + + return allSystems; + } + public static List getPlayersSystems(UUID uuid) { List systems = new ArrayList<>(); for (String systemUUID : getConfig().getConfigurationSection("players." + uuid + ".systems").getKeys(false)) { diff --git a/src/main/java/net/seanomik/energeticstorage/utils/Utils.java b/src/main/java/net/seanomik/energeticstorage/utils/Utils.java index 2de46d8..b6b6e20 100644 --- a/src/main/java/net/seanomik/energeticstorage/utils/Utils.java +++ b/src/main/java/net/seanomik/energeticstorage/utils/Utils.java @@ -1,6 +1,5 @@ package net.seanomik.energeticstorage.utils; -import jdk.internal.jline.internal.Nullable; import net.seanomik.energeticstorage.objects.ESSystem; import org.bukkit.Bukkit; import org.bukkit.Location; @@ -9,6 +8,7 @@ import org.bukkit.World; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.ItemMeta; +import javax.annotation.Nullable; import java.util.*; public class Utils { diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index fa8ac2e..89e8bf2 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -8,6 +8,10 @@ commands: aliases: [egive] description: Give a Energetic Storage item. usage: /esgive + esreload: + aliases: [ereload] + description: Reload all Energetic Storage configs. + usage: /esreload permissions: energeticstorage.*: description: All Energetic Storage permissions. diff --git a/target/classes/config.yml b/target/classes/config.yml index b848d38..191249a 100644 --- a/target/classes/config.yml +++ b/target/classes/config.yml @@ -1 +1 @@ -driveMaxTypes: 128 \ No newline at end of file +driveMaxTypes: 128 diff --git a/target/classes/plugin.yml b/target/classes/plugin.yml index df37e86..5288378 100644 --- a/target/classes/plugin.yml +++ b/target/classes/plugin.yml @@ -1,5 +1,5 @@ name: EnergeticStorage -version: 0.1-SNAPSHOT +version: 0.2-SNAPSHOT main: net.seanomik.energeticstorage.EnergeticStorage api-version: 1.15 authors: [SeanOMik] @@ -8,6 +8,10 @@ commands: aliases: [egive] description: Give a Energetic Storage item. usage: /esgive + esreload: + aliases: [ereload] + description: Reload all Energetic Storage configs. + usage: /esreload permissions: energeticstorage.*: description: All Energetic Storage permissions. diff --git a/target/maven-archiver/pom.properties b/target/maven-archiver/pom.properties index 05c2644..8ab2ed9 100644 --- a/target/maven-archiver/pom.properties +++ b/target/maven-archiver/pom.properties @@ -1,5 +1,5 @@ #Generated by Maven -#Tue Apr 07 00:41:39 CDT 2020 +#Mon Jul 06 23:29:34 CDT 2020 groupId=net.seanomik artifactId=energeticstorage -version=0.1-SNAPSHOT +version=0.2-SNAPSHOT diff --git a/target/maven-status/maven-compiler-plugin/compile/default-compile/inputFiles.lst b/target/maven-status/maven-compiler-plugin/compile/default-compile/inputFiles.lst index 56de941..23f6ef1 100644 --- a/target/maven-status/maven-compiler-plugin/compile/default-compile/inputFiles.lst +++ b/target/maven-status/maven-compiler-plugin/compile/default-compile/inputFiles.lst @@ -1,6 +1,7 @@ D:\Code\java\spigotPlugins\myPlugins\EnergeticStorage\src\main\java\net\seanomik\energeticstorage\gui\ESTerminalGUI.java D:\Code\java\spigotPlugins\myPlugins\EnergeticStorage\src\main\java\net\seanomik\energeticstorage\utils\ItemRecipies.java D:\Code\java\spigotPlugins\myPlugins\EnergeticStorage\src\main\java\net\seanomik\energeticstorage\commands\ESGiveCommand.java +D:\Code\java\spigotPlugins\myPlugins\EnergeticStorage\src\main\java\net\seanomik\energeticstorage\commands\ESReloadCommand.java D:\Code\java\spigotPlugins\myPlugins\EnergeticStorage\src\main\java\net\seanomik\energeticstorage\utils\Utils.java D:\Code\java\spigotPlugins\myPlugins\EnergeticStorage\src\main\java\net\seanomik\energeticstorage\gui\ESSystemSecurityGUI.java D:\Code\java\spigotPlugins\myPlugins\EnergeticStorage\src\main\java\net\seanomik\energeticstorage\utils\ItemConstructor.java