From 444eba5d36430edb63143eac03294db9f113a7e1 Mon Sep 17 00:00:00 2001 From: Alexander Meech Date: Tue, 13 Aug 2019 03:05:45 -0400 Subject: [PATCH] Ported the new configuration system to some of the base code, and improved elements of the Preset system --- .../projectkorra/BendingManager.java | 15 +- .../projectkorra/BendingPlayer.java | 15 +- .../projectkorra/projectkorra/PKListener.java | 20 +-- .../projectkorra/command/PresetCommand.java | 7 +- .../configuration/better/ConfigManager.java | 2 +- .../properties/FirePropertiesConfig.java | 3 + .../properties/GeneralPropertiesConfig.java | 4 + .../properties/WaterPropertiesConfig.java | 3 + .../projectkorra/object/Preset.java | 132 ++++++++++-------- 9 files changed, 112 insertions(+), 89 deletions(-) diff --git a/src/com/projectkorra/projectkorra/BendingManager.java b/src/com/projectkorra/projectkorra/BendingManager.java index c4f01ba8..ae26765d 100644 --- a/src/com/projectkorra/projectkorra/BendingManager.java +++ b/src/com/projectkorra/projectkorra/BendingManager.java @@ -3,8 +3,6 @@ package com.projectkorra.projectkorra; import java.util.HashMap; import java.util.UUID; -import co.aikar.timings.lib.MCTiming; - import org.bukkit.Bukkit; import org.bukkit.ChatColor; import org.bukkit.World; @@ -12,6 +10,9 @@ import org.bukkit.entity.Player; import com.projectkorra.projectkorra.ability.CoreAbility; import com.projectkorra.projectkorra.ability.ElementalAbility; +import com.projectkorra.projectkorra.configuration.better.ConfigManager; +import com.projectkorra.projectkorra.configuration.better.configs.properties.FirePropertiesConfig; +import com.projectkorra.projectkorra.configuration.better.configs.properties.WaterPropertiesConfig; import com.projectkorra.projectkorra.earthbending.metal.MetalClips; import com.projectkorra.projectkorra.object.HorizontalVelocityTracker; import com.projectkorra.projectkorra.util.ActionBar; @@ -20,6 +21,8 @@ import com.projectkorra.projectkorra.util.TempArmor; import com.projectkorra.projectkorra.util.TempPotionEffect; import com.projectkorra.projectkorra.waterbending.blood.Bloodbending; +import co.aikar.timings.lib.MCTiming; + public class BendingManager implements Runnable { private static BendingManager instance; @@ -150,19 +153,19 @@ public class BendingManager implements Runnable { } public static String getSunriseMessage() { - return ChatColor.translateAlternateColorCodes('&', ConfigManager.languageConfig.get().getString("Extras.Fire.DayMessage")); + return ChatColor.translateAlternateColorCodes('&', ConfigManager.getConfig(FirePropertiesConfig.class).DayMessage); } public static String getSunsetMessage() { - return ChatColor.translateAlternateColorCodes('&', ConfigManager.languageConfig.get().getString("Extras.Fire.NightMessage")); + return ChatColor.translateAlternateColorCodes('&', ConfigManager.getConfig(FirePropertiesConfig.class).NightMessage); } public static String getMoonriseMessage() { - return ChatColor.translateAlternateColorCodes('&', ConfigManager.languageConfig.get().getString("Extras.Water.NightMessage")); + return ChatColor.translateAlternateColorCodes('&', ConfigManager.getConfig(WaterPropertiesConfig.class).NightMessage); } public static String getMoonsetMessage() { - return ChatColor.translateAlternateColorCodes('&', ConfigManager.languageConfig.get().getString("Extras.Water.DayMessage")); + return ChatColor.translateAlternateColorCodes('&', ConfigManager.getConfig(WaterPropertiesConfig.class).DayMessage); } } diff --git a/src/com/projectkorra/projectkorra/BendingPlayer.java b/src/com/projectkorra/projectkorra/BendingPlayer.java index efbfe995..03c58595 100644 --- a/src/com/projectkorra/projectkorra/BendingPlayer.java +++ b/src/com/projectkorra/projectkorra/BendingPlayer.java @@ -9,15 +9,14 @@ import java.util.Map; import java.util.Map.Entry; import java.util.UUID; import java.util.concurrent.ConcurrentHashMap; +import java.util.stream.Stream; import org.apache.commons.lang3.builder.ToStringBuilder; import org.apache.commons.lang3.builder.ToStringStyle; - import org.bukkit.Bukkit; import org.bukkit.GameMode; import org.bukkit.Location; import org.bukkit.OfflinePlayer; -import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.entity.Player; import com.projectkorra.projectkorra.Element.SubElement; @@ -28,6 +27,8 @@ import com.projectkorra.projectkorra.ability.CoreAbility; import com.projectkorra.projectkorra.ability.util.PassiveManager; import com.projectkorra.projectkorra.avatar.AvatarState; import com.projectkorra.projectkorra.command.Commands; +import com.projectkorra.projectkorra.configuration.better.ConfigManager; +import com.projectkorra.projectkorra.configuration.better.configs.properties.GeneralPropertiesConfig; import com.projectkorra.projectkorra.earthbending.metal.MetalClips; import com.projectkorra.projectkorra.event.PlayerCooldownChangeEvent; import com.projectkorra.projectkorra.event.PlayerCooldownChangeEvent.Result; @@ -245,7 +246,6 @@ public class BendingPlayer { return false; } - final List disabledWorlds = getConfig().getStringList("Properties.DisabledWorlds"); final Location playerLoc = this.player.getLocation(); if (!this.player.isOnline() || this.player.isDead()) { @@ -258,7 +258,7 @@ public class BendingPlayer { return false; } else if (!ignoreBinds && (!ability.getName().equals(this.getBoundAbilityName()))) { return false; - } else if (disabledWorlds != null && disabledWorlds.contains(this.player.getWorld().getName())) { + } else if (Stream.of(ConfigManager.getConfig(GeneralPropertiesConfig.class).DisabledWorlds).anyMatch(this.player.getWorld().getName()::equalsIgnoreCase)) { return false; } else if (Commands.isToggledForAll || !this.isToggled() || !this.isElementToggled(ability.getElement())) { return false; @@ -267,7 +267,7 @@ public class BendingPlayer { } if (!ignoreCooldowns && this.cooldowns.containsKey(this.name)) { - if (this.cooldowns.get(this.name).getCooldown() + getConfig().getLong("Properties.GlobalCooldown") >= System.currentTimeMillis()) { + if (this.cooldowns.get(this.name).getCooldown() + ConfigManager.getConfig(GeneralPropertiesConfig.class).GlobalCooldown >= System.currentTimeMillis()) { return false; } @@ -300,11 +300,10 @@ public class BendingPlayer { return false; // If the passive is disabled. } final Element element = ability.getElement(); - if (Commands.isToggledForAll && ConfigManager.defaultConfig.get().getBoolean("Properties.TogglePassivesWithAllBending")) { + if (Commands.isToggledForAll && ConfigManager.getConfig(GeneralPropertiesConfig.class).TogglePassivesWithAllBending) { return false; } - final List disabledWorlds = getConfig().getStringList("Properties.DisabledWorlds"); if (element == null || this.player == null) { return false; @@ -312,7 +311,7 @@ public class BendingPlayer { return false; } else if (!this.hasElement(element)) { return false; - } else if (disabledWorlds != null && disabledWorlds.contains(this.player.getWorld().getName())) { + } else if (Stream.of(ConfigManager.getConfig(GeneralPropertiesConfig.class).DisabledWorlds).anyMatch(this.player.getWorld().getName()::equalsIgnoreCase)) { return false; } else if (this.player.getGameMode() == GameMode.SPECTATOR) { return false; diff --git a/src/com/projectkorra/projectkorra/PKListener.java b/src/com/projectkorra/projectkorra/PKListener.java index 76054c79..e20f5da2 100644 --- a/src/com/projectkorra/projectkorra/PKListener.java +++ b/src/com/projectkorra/projectkorra/PKListener.java @@ -1198,21 +1198,9 @@ public class PKListener implements Listener { GeneralMethods.removeUnusableAbilities(player.getName()); }, 5); - if (ConfigManager.languageConfig.get().getBoolean("Chat.Branding.JoinMessage.Enabled")) { - Bukkit.getScheduler().runTaskLater(ProjectKorra.plugin, (Runnable) () -> { - ChatColor color = ChatColor.valueOf(ConfigManager.languageConfig.get().getString("Chat.Branding.Color").toUpperCase()); - color = color == null ? ChatColor.GOLD : color; - final String topBorder = ConfigManager.languageConfig.get().getString("Chat.Branding.Borders.TopBorder"); - final String bottomBorder = ConfigManager.languageConfig.get().getString("Chat.Branding.Borders.BottomBorder"); - if (!topBorder.isEmpty()) { - player.sendMessage(ChatColor.translateAlternateColorCodes('&', topBorder)); - } - player.sendMessage(color + "This server is running ProjectKorra version " + ProjectKorra.plugin.getDescription().getVersion() + " for bending! Find out more at http://www.projectkorra.com!"); - if (!bottomBorder.isEmpty()) { - player.sendMessage(ChatColor.translateAlternateColorCodes('&', bottomBorder)); - } - }, 20 * 4); - } + Bukkit.getScheduler().runTaskLater(ProjectKorra.plugin, (Runnable) () -> { + player.sendMessage(ChatColor.GOLD + "This server is running ProjectKorra version " + ProjectKorra.plugin.getDescription().getVersion() + " for bending! Find out more at http://www.projectkorra.com!"); + }, 20 * 4); } @EventHandler @@ -1567,7 +1555,7 @@ public class PKListener implements Listener { final int slot = event.getNewSlot() + 1; GeneralMethods.displayMovePreview(player, slot); - if (!ConfigManager.defaultConfig.get().getBoolean("Properties.BendingPreview")) { + if (!ConfigManager.getConfig(GeneralPropertiesConfig.class).BendingPreview) { final WaterArms waterArms = CoreAbility.getAbility(player, WaterArms.class); if (waterArms != null) { waterArms.displayBoundMsg(event.getNewSlot() + 1); diff --git a/src/com/projectkorra/projectkorra/command/PresetCommand.java b/src/com/projectkorra/projectkorra/command/PresetCommand.java index efa04582..d670b790 100644 --- a/src/com/projectkorra/projectkorra/command/PresetCommand.java +++ b/src/com/projectkorra/projectkorra/command/PresetCommand.java @@ -220,7 +220,12 @@ public class PresetCommand extends PKCommand { if (bPlayer == null) { return; } - final HashMap abilities = (HashMap) bPlayer.getAbilities().clone(); + String[] abilities = new String[9]; + for (int slot = 0; slot < 9; slot++) { + if (bPlayer.getAbilities().containsKey(slot + 1)) { + abilities[slot] = bPlayer.getAbilities().get(slot + 1); + } + } final Preset preset = new Preset(player.getUniqueId(), name, abilities); preset.save(player); diff --git a/src/com/projectkorra/projectkorra/configuration/better/ConfigManager.java b/src/com/projectkorra/projectkorra/configuration/better/ConfigManager.java index 7f767c65..2b37e486 100644 --- a/src/com/projectkorra/projectkorra/configuration/better/ConfigManager.java +++ b/src/com/projectkorra/projectkorra/configuration/better/ConfigManager.java @@ -53,7 +53,7 @@ public class ConfigManager { CONFIG_CACHE.put(clazz, defaultConfig); File file = new File(JavaPlugin.getPlugin(ProjectKorra.class).getDataFolder(), "config"); - file.mkdir(); + file.mkdirs(); for (String parent : defaultConfig.getParents()) { file = new File(file, parent); diff --git a/src/com/projectkorra/projectkorra/configuration/better/configs/properties/FirePropertiesConfig.java b/src/com/projectkorra/projectkorra/configuration/better/configs/properties/FirePropertiesConfig.java index b006d1f8..7989ae22 100644 --- a/src/com/projectkorra/projectkorra/configuration/better/configs/properties/FirePropertiesConfig.java +++ b/src/com/projectkorra/projectkorra/configuration/better/configs/properties/FirePropertiesConfig.java @@ -11,6 +11,9 @@ public class FirePropertiesConfig implements Config { public final long RevertTicks = 0; public final double DayFactor = 0; + public final String DayMessage = ""; + public final String NightMessage = ""; + public final ParticleEffect Particles = ParticleEffect.FLAME; public final boolean PlaySound = true; diff --git a/src/com/projectkorra/projectkorra/configuration/better/configs/properties/GeneralPropertiesConfig.java b/src/com/projectkorra/projectkorra/configuration/better/configs/properties/GeneralPropertiesConfig.java index 90a93a2c..4bd69e22 100644 --- a/src/com/projectkorra/projectkorra/configuration/better/configs/properties/GeneralPropertiesConfig.java +++ b/src/com/projectkorra/projectkorra/configuration/better/configs/properties/GeneralPropertiesConfig.java @@ -26,6 +26,8 @@ public class GeneralPropertiesConfig implements Config { public final long GlobalCooldown = 0; + public final boolean TogglePassivesWithAllBending = true; + public final int MaxPresets = 0; public final boolean ImportEnabled = false; @@ -40,6 +42,8 @@ public class GeneralPropertiesConfig implements Config { public final boolean ApplyHorizontalCollisionBarrierBlockDamage = true; + public final String[] DisabledWorlds = {}; + @Override public String getName() { return "General"; diff --git a/src/com/projectkorra/projectkorra/configuration/better/configs/properties/WaterPropertiesConfig.java b/src/com/projectkorra/projectkorra/configuration/better/configs/properties/WaterPropertiesConfig.java index ee2707b4..a93e8470 100644 --- a/src/com/projectkorra/projectkorra/configuration/better/configs/properties/WaterPropertiesConfig.java +++ b/src/com/projectkorra/projectkorra/configuration/better/configs/properties/WaterPropertiesConfig.java @@ -11,6 +11,9 @@ public class WaterPropertiesConfig implements Config { public final double NightFactor = 0; + public final String DayMessage = ""; + public final String NightMessage = ""; + public final boolean PlaySound = true; public final Sound SoundType = Sound.BLOCK_WATER_AMBIENT; public final float SoundVolume = 0; diff --git a/src/com/projectkorra/projectkorra/object/Preset.java b/src/com/projectkorra/projectkorra/object/Preset.java index f57cf592..1c33cda6 100644 --- a/src/com/projectkorra/projectkorra/object/Preset.java +++ b/src/com/projectkorra/projectkorra/object/Preset.java @@ -1,5 +1,9 @@ package com.projectkorra.projectkorra.object; +import java.io.BufferedReader; +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; @@ -10,31 +14,28 @@ import java.util.Map; import java.util.UUID; import java.util.concurrent.ConcurrentHashMap; -import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.entity.Player; +import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.scheduler.BukkitRunnable; +import com.google.gson.Gson; import com.projectkorra.projectkorra.BendingPlayer; import com.projectkorra.projectkorra.ProjectKorra; import com.projectkorra.projectkorra.ability.CoreAbility; -import com.projectkorra.projectkorra.configuration.ConfigManager; import com.projectkorra.projectkorra.storage.DBConnection; /** - * A savable association of abilities and hotbar slots, stored per player. - * - * @author kingbirdy - * + * A persistent association of abilities and hotbar slots, stored per player. */ +@SuppressWarnings("rawtypes") public class Preset { /** * ConcurrentHashMap that stores a list of every Player's {@link Preset * presets}, keyed to their UUID */ - public static Map> presets = new ConcurrentHashMap>(); - public static FileConfiguration config = ConfigManager.presetConfig.get(); - public static HashMap> externalPresets = new HashMap>(); + public static Map> presets = new ConcurrentHashMap<>(); + public static Map externalPresets = new HashMap<>(); static String loadQuery = "SELECT * FROM pk_presets WHERE uuid = ?"; static String loadNameQuery = "SELECT * FROM pk_presets WHERE uuid = ? AND name = ?"; static String deleteQuery = "DELETE FROM pk_presets WHERE uuid = ? AND name = ?"; @@ -42,9 +43,9 @@ public class Preset { static String updateQuery1 = "UPDATE pk_presets SET slot"; static String updateQuery2 = " = ? WHERE uuid = ? AND name = ?"; - private final UUID uuid; - private final HashMap abilities; - private final String name; + private final transient UUID uuid; + private final transient String[] abilities; + private final transient String name; /** * Creates a new {@link Preset} @@ -54,7 +55,7 @@ public class Preset { * @param abilities A HashMap of the abilities to be saved in the Preset, * keyed to the slot they're bound to */ - public Preset(final UUID uuid, final String name, final HashMap abilities) { + public Preset(final UUID uuid, final String name, final String[] abilities) { this.uuid = uuid; this.name = name; this.abilities = abilities; @@ -63,6 +64,32 @@ public class Preset { } presets.get(uuid).add(this); } + + public static final class ExternalPreset extends Preset { + + private final String Name = null; + private final String[] Abilities = null; + + public ExternalPreset() { + super(null, null, null); + } + + @Override + public String getName() { + return Name; + } + + @Override + public String[] getAbilities() { + return Abilities; + } + + @Override + public void delete() {} + + @Override + public void save(final Player player) {} + } /** * Unload a Player's Presets from those stored in memory. @@ -94,14 +121,11 @@ public class Preset { if (rs.next()) { // Presets exist. int i = 0; do { - final HashMap moves = new HashMap(); - for (int total = 1; total <= 9; total++) { - final String slot = rs.getString("slot" + total); - if (slot != null) { - moves.put(total, slot); - } + String[] abilities = new String[9]; + for (int slot = 0; slot < 9; slot++) { + abilities[slot] = rs.getString("slot" + (slot + 1)); } - new Preset(uuid, rs.getString("name"), moves); + new Preset(uuid, rs.getString("name"), abilities); i++; } while (rs.next()); ProjectKorra.log.info("Loaded " + i + " presets for " + player.getName()); @@ -136,16 +160,19 @@ public class Preset { return false; } - final HashMap abilities = new HashMap<>(preset.abilities); + String[] abilities = preset.getAbilities(); boolean boundAll = true; - for (int i = 1; i <= 9; i++) { - final CoreAbility coreAbil = CoreAbility.getAbility(abilities.get(i)); + HashMap bindings = new HashMap<>(); + for (int i = 0; i < abilities.length; i++) { + final CoreAbility coreAbil = CoreAbility.getAbility(abilities[i]); if (coreAbil != null && !bPlayer.canBind(coreAbil)) { - abilities.remove(i); + abilities[i] = null; boundAll = false; + } else { + bindings.put(i + 1, abilities[i]); } } - bPlayer.setAbilities(abilities); + bPlayer.setAbilities(bindings); return boundAll; } @@ -189,15 +216,20 @@ public class Preset { } public static void loadExternalPresets() { - final HashMap> presets = new HashMap>(); - for (final String name : config.getKeys(false)) { - if (!presets.containsKey(name)) { - if (!config.getStringList(name).isEmpty() && config.getStringList(name).size() <= 9) { - presets.put(name.toLowerCase(), (ArrayList) config.getStringList(name)); + Gson gson = new Gson(); + File directory = new File(JavaPlugin.getPlugin(ProjectKorra.class).getDataFolder(), "presets"); + if (directory.exists() && directory.isDirectory()) { + for (File f : directory.listFiles((parent, name) -> name.endsWith(".json"))) { + try (BufferedReader reader = Files.newBufferedReader(f.toPath())) { + ExternalPreset preset = gson.fromJson(reader, ExternalPreset.class); + externalPresets.put(preset.getName().toLowerCase(), preset); + } catch (IOException e) { + e.printStackTrace(); } } + } else { + directory.mkdirs(); } - externalPresets = presets; } public static boolean externalPresetExists(final String name) { @@ -214,10 +246,10 @@ public class Preset { * * @param player The Player who's Preset should be gotten * @param name The name of the Preset who's contents should be gotten - * @return HashMap of ability names keyed to hotbar slots, if the Preset + * @return String array of ability names indexed by hotbar slots, if the Preset * exists, or null otherwise */ - public static HashMap getPresetContents(final Player player, final String name) { + public static String[] getPresetContents(final Player player, final String name) { if (!presets.containsKey(player.getUniqueId())) { return null; } @@ -230,33 +262,13 @@ public class Preset { } public static boolean bindExternalPreset(final Player player, final String name) { - boolean boundAll = true; - int slot = 0; final BendingPlayer bPlayer = BendingPlayer.getBendingPlayer(player); if (bPlayer == null) { return false; } - final HashMap abilities = new HashMap(); - if (externalPresetExists(name.toLowerCase())) { - for (final String ability : externalPresets.get(name.toLowerCase())) { - slot++; - final CoreAbility coreAbil = CoreAbility.getAbility(ability); - if (coreAbil != null) { - abilities.put(slot, coreAbil.getName()); - } - } - - for (int i = 1; i <= 9; i++) { - final CoreAbility coreAbil = CoreAbility.getAbility(abilities.get(i)); - if (coreAbil != null && !bPlayer.canBind(coreAbil)) { - abilities.remove(i); - boundAll = false; - } - } - bPlayer.setAbilities(abilities); - return boundAll; + return bindPreset(player, externalPresets.get(name.toLowerCase())); } return false; } @@ -284,6 +296,10 @@ public class Preset { public String getName() { return this.name; } + + public String[] getAbilities() { + return this.abilities; + } /** * Saves the Preset to the database. @@ -303,15 +319,17 @@ public class Preset { } catch (final SQLException e) { e.printStackTrace(); } - for (final Integer i : this.abilities.keySet()) { + for (int i = 0; i < abilities.length; i++) { + final String ability = abilities[i]; + final int slot = i + 1; new BukkitRunnable() { PreparedStatement ps; @Override public void run() { try { - this.ps = DBConnection.sql.getConnection().prepareStatement(updateQuery1 + i + updateQuery2); - this.ps.setString(1, Preset.this.abilities.get(i)); + this.ps = DBConnection.sql.getConnection().prepareStatement(updateQuery1 + slot + updateQuery2); + this.ps.setString(1, ability); this.ps.setString(2, Preset.this.uuid.toString()); this.ps.setString(3, Preset.this.name); this.ps.execute();