Ported the new configuration system to some of the base code, and improved elements of the Preset system

This commit is contained in:
Alexander Meech 2019-08-13 03:05:45 -04:00
parent fd1710ce61
commit 444eba5d36
9 changed files with 112 additions and 89 deletions

View file

@ -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);
}
}

View file

@ -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<String> 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<String> 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;

View file

@ -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);

View file

@ -220,7 +220,12 @@ public class PresetCommand extends PKCommand {
if (bPlayer == null) {
return;
}
final HashMap<Integer, String> abilities = (HashMap<Integer, String>) 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);

View file

@ -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);

View file

@ -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;

View file

@ -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";

View file

@ -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;

View file

@ -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<UUID, List<Preset>> presets = new ConcurrentHashMap<UUID, List<Preset>>();
public static FileConfiguration config = ConfigManager.presetConfig.get();
public static HashMap<String, ArrayList<String>> externalPresets = new HashMap<String, ArrayList<String>>();
public static Map<UUID, List<Preset>> presets = new ConcurrentHashMap<>();
public static Map<String, ExternalPreset> 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<Integer, String> 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<Integer, String> abilities) {
public Preset(final UUID uuid, final String name, final String[] abilities) {
this.uuid = uuid;
this.name = name;
this.abilities = abilities;
@ -64,6 +65,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<Integer, String> moves = new HashMap<Integer, String>();
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<Integer, String> 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<Integer, String> 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<String, ArrayList<String>> presets = new HashMap<String, ArrayList<String>>();
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<String>) 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<Integer, String> 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<Integer, String> abilities = new HashMap<Integer, String>();
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;
}
@ -285,6 +297,10 @@ public class Preset {
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();