diff --git a/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/EcoEnchantsPlugin.java b/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/EcoEnchantsPlugin.java index 0f85d9f7..72007462 100644 --- a/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/EcoEnchantsPlugin.java +++ b/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/EcoEnchantsPlugin.java @@ -8,10 +8,10 @@ import com.willfp.eco.core.integrations.IntegrationLoader; import com.willfp.eco.util.TelekinesisUtils; import com.willfp.ecoenchants.command.CommandEcoEnchants; import com.willfp.ecoenchants.command.CommandEnchantinfo; -import com.willfp.ecoenchants.config.DataYml; import com.willfp.ecoenchants.config.RarityYml; import com.willfp.ecoenchants.config.TargetYml; import com.willfp.ecoenchants.config.VanillaEnchantsYml; +import com.willfp.ecoenchants.data.SaveHandler; import com.willfp.ecoenchants.data.storage.DataHandler; import com.willfp.ecoenchants.data.storage.MySQLDataHandler; import com.willfp.ecoenchants.data.storage.YamlDataHandler; @@ -35,7 +35,6 @@ import org.bukkit.event.HandlerList; import org.bukkit.event.Listener; import org.jetbrains.annotations.Nullable; -import java.io.IOException; import java.util.Arrays; import java.util.List; @@ -59,12 +58,6 @@ public class EcoEnchantsPlugin extends EcoPlugin { @Getter private final TargetYml targetYml; - /** - * Data.yml. - */ - @Getter - private final DataYml dataYml; - /** * VanillaEnchants.yml. */ @@ -86,7 +79,6 @@ public class EcoEnchantsPlugin extends EcoPlugin { rarityYml = new RarityYml(this); targetYml = new TargetYml(this); - dataYml = new DataYml(this); vanillaEnchantsYml = new VanillaEnchantsYml(this); dataHandler = this.getConfigYml().getBool("mysql.enabled") ? new MySQLDataHandler(this) : new YamlDataHandler(this); @@ -101,11 +93,7 @@ public class EcoEnchantsPlugin extends EcoPlugin { @Override protected void handleDisable() { - try { - this.dataYml.save(); - } catch (IOException e) { - e.printStackTrace(); - } + SaveHandler.Companion.save(this); for (World world : Bukkit.getServer().getWorlds()) { world.getPopulators().removeIf(blockPopulator -> blockPopulator instanceof LootPopulator); } @@ -131,6 +119,9 @@ public class EcoEnchantsPlugin extends EcoPlugin { enchant.clearCachedRequirements(); } }, 300, 300); + + SaveHandler.Companion.save(this); + this.getScheduler().runTimer(new SaveHandler.Runnable(this), 20000, 20000); } @Override diff --git a/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/command/CommandReload.java b/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/command/CommandReload.java index bd4e1d3e..3f019600 100644 --- a/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/command/CommandReload.java +++ b/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/command/CommandReload.java @@ -5,8 +5,6 @@ import com.willfp.eco.core.command.impl.Subcommand; import com.willfp.ecoenchants.EcoEnchantsPlugin; import org.jetbrains.annotations.NotNull; -import java.io.IOException; - public class CommandReload extends Subcommand { /** * Instantiate a new command handler. @@ -20,11 +18,6 @@ public class CommandReload extends Subcommand { @Override public CommandHandler getHandler() { return (sender, args) -> { - try { - ((EcoEnchantsPlugin) this.getPlugin()).getDataYml().save(); - } catch (IOException e) { - e.printStackTrace(); - } this.getPlugin().reload(); sender.sendMessage(this.getPlugin().getLangYml().getMessage("reloaded")); }; diff --git a/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/command/CommandToggleDescriptions.java b/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/command/CommandToggleDescriptions.java index 20f8fe7a..ef0900fd 100644 --- a/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/command/CommandToggleDescriptions.java +++ b/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/command/CommandToggleDescriptions.java @@ -3,6 +3,7 @@ package com.willfp.ecoenchants.command; import com.willfp.eco.core.command.CommandHandler; import com.willfp.eco.core.command.impl.Subcommand; import com.willfp.ecoenchants.EcoEnchantsPlugin; +import com.willfp.ecoenchants.data.storage.PlayerProfile; import org.bukkit.entity.Player; import org.jetbrains.annotations.NotNull; @@ -19,13 +20,20 @@ public class CommandToggleDescriptions extends Subcommand { @Override public CommandHandler getHandler() { return (sender, args) -> { - if (!((EcoEnchantsPlugin) this.getPlugin()).getDisplayModule().getOptions().getDescriptionOptions().isEnabled()){ + if (!((EcoEnchantsPlugin) this.getPlugin()).getDisplayModule().getOptions().getDescriptionOptions().isEnabled()) { sender.sendMessage(this.getPlugin().getLangYml().getMessage("descriptions-disabled")); return; } Player player = (Player) sender; - ((EcoEnchantsPlugin) this.getPlugin()).getDataYml().toggleDescriptions(player); - player.sendMessage(this.getPlugin().getLangYml().getMessage("descriptions-enabled."+((EcoEnchantsPlugin) this.getPlugin()).getDataYml().isDescriptionEnabled(player))); + PlayerProfile profile = PlayerProfile.getProfile(player); + boolean currentStatus = profile.read("descriptions", true); + currentStatus = !currentStatus; + profile.write("descriptions", currentStatus); + if (currentStatus) { + player.sendMessage(this.getPlugin().getLangYml().getMessage("enabled-descriptions")); + } else { + player.sendMessage(this.getPlugin().getLangYml().getMessage("disabled-descriptions")); + } }; } } diff --git a/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/config/DataYml.java b/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/config/DataYml.java deleted file mode 100644 index e3448199..00000000 --- a/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/config/DataYml.java +++ /dev/null @@ -1,49 +0,0 @@ -package com.willfp.ecoenchants.config; - -import com.willfp.eco.core.EcoPlugin; -import com.willfp.eco.core.config.yaml.YamlBaseConfig; -import org.bukkit.entity.Player; -import org.jetbrains.annotations.NotNull; - -public class DataYml extends YamlBaseConfig { - - /** - * Instantiate data.yml. - * - * @param plugin Instance of EcoEnchants. - */ - public DataYml(@NotNull final EcoPlugin plugin) { - super("data", false, plugin); - } - - /** - * Get descriptions state (enabled/disabled) for the given player. - * - * @param player A player to get the descriptions state of. - * @return Descriptions state for the given player. - */ - public boolean isDescriptionEnabled(@NotNull final Player player) { - if (this.getBoolOrNull(player.getUniqueId() + ".describe") == null) return true; - return this.getBool(player.getUniqueId() + ".describe"); - } - - /** - * Set descriptions state (enabled/disabled) for the given player. - * - * @param player A player to set the given state for. - * @param enabled The state to set for the given player. - */ - public void setDescriptionEnabled(@NotNull final Player player, final boolean enabled) { - this.set(player.getUniqueId() + ".describe", enabled); - } - - /** - * Toggle descriptions state (enabled->disabled | disabled->enabled) for the given player. - * - * @param player A player to toggle the state for. - */ - public void toggleDescriptions(@NotNull final Player player) { - setDescriptionEnabled(player, !isDescriptionEnabled(player)); - } - -} diff --git a/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/display/EnchantDisplay.java b/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/display/EnchantDisplay.java index 7bcf244a..7d20534e 100644 --- a/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/display/EnchantDisplay.java +++ b/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/display/EnchantDisplay.java @@ -131,7 +131,7 @@ public class EnchantDisplay extends DisplayModule { if (!options.getDescriptionOptions().isShowingAtBottom()) { if (enchantments.size() <= options.getDescriptionOptions().getThreshold() && options.getDescriptionOptions().isEnabled() - && options.getDescriptionOptions().playerEnabled(player) + && options.getDescriptionOptions().enabledForPlayer(player) ) { lore.addAll(EnchantmentCache.getEntry(enchantment).getDescription(level)); } @@ -164,7 +164,7 @@ public class EnchantDisplay extends DisplayModule { if (options.getDescriptionOptions().isShowingAtBottom()) { if (enchantments.size() <= options.getDescriptionOptions().getThreshold() && options.getDescriptionOptions().isEnabled() - && options.getDescriptionOptions().playerEnabled(player) + && options.getDescriptionOptions().enabledForPlayer(player) ) { for (Map.Entry entry : enchantments.entrySet()) { lore.addAll(EnchantmentCache.getEntry(entry.getKey()).getDescription(entry.getValue())); diff --git a/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/display/options/DescriptionOptions.java b/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/display/options/DescriptionOptions.java index ca79f43e..d6c7815b 100644 --- a/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/display/options/DescriptionOptions.java +++ b/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/display/options/DescriptionOptions.java @@ -2,7 +2,7 @@ package com.willfp.ecoenchants.display.options; import com.willfp.eco.core.EcoPlugin; import com.willfp.eco.core.PluginDependent; -import com.willfp.ecoenchants.EcoEnchantsPlugin; +import com.willfp.ecoenchants.data.storage.PlayerProfile; import lombok.Getter; import org.bukkit.entity.Player; import org.jetbrains.annotations.NotNull; @@ -53,13 +53,15 @@ public class DescriptionOptions extends PluginDependent { } /** - * Get description state for a player + * Get if descriptions are enabled for a player. * - * @param player - a player to get the state for. + * @param player The player. */ - public boolean playerEnabled(@Nullable final Player player) { - if (player == null) return true; - return ((EcoEnchantsPlugin) this.getPlugin()).getDataYml().isDescriptionEnabled(player); - } + public boolean enabledForPlayer(@Nullable final Player player) { + if (player == null) { + return true; + } + return PlayerProfile.getProfile(player).read("descriptions", true); + } } diff --git a/eco-core/core-plugin/src/main/resources/lang.yml b/eco-core/core-plugin/src/main/resources/lang.yml index 38f4025b..765dce40 100644 --- a/eco-core/core-plugin/src/main/resources/lang.yml +++ b/eco-core/core-plugin/src/main/resources/lang.yml @@ -31,9 +31,8 @@ messages: downloaded-locale: "Locale downloaded! Reload config to enact changes." invalid-locale: "&cYou must supply a valid locale! Check the wiki for more information." specify-locale-subcommand: "&cYou must specify whether to export or download a locale!" - descriptions-enabled: - true: "&fYou have successfully &aenabled &fenchantment descriptions!" - false: "&fYou have successfully &cdisabled &fenchantment descriptions!" + enabled-descriptions: "&fYou have successfully &aenabled &fenchantment descriptions!" + disabled-descriptions: "&fYou have successfully &cdisabled &fenchantment descriptions!" descriptions-disabled: "&cEnchantment descriptions are disabled on this server." no-targets: "&cCannot be applied" diff --git a/eco-core/core-plugin/src/main/resources/plugin.yml b/eco-core/core-plugin/src/main/resources/plugin.yml index 123619df..99227c99 100644 --- a/eco-core/core-plugin/src/main/resources/plugin.yml +++ b/eco-core/core-plugin/src/main/resources/plugin.yml @@ -66,6 +66,7 @@ permissions: ecoenchants.command.giverandombook: true ecoenchants.command.locale.*: true ecoenchants.command.ecoenchants: true + ecoenchants.command.toggledescriptions: true ecoenchants.command.locale.*: description: Allows managing locale features default: op @@ -110,6 +111,9 @@ permissions: ecoenchants.command.locale.export: description: Allows the use of /ecoenchants locale export to export locales default: op + ecoenchants.command.toggledescriptions: + description: Allows the use of /ecoenchants toggledescriptions + default: true ecoenchants.command.ecoenchants: description: Allows the use of /ecoenchants default: true