diff --git a/src/main/java/me/tychsen/enchantgui/EnchantGUIPlugin.java b/src/main/java/me/tychsen/enchantgui/EnchantGUIPlugin.java index fcc7989..f92c466 100644 --- a/src/main/java/me/tychsen/enchantgui/EnchantGUIPlugin.java +++ b/src/main/java/me/tychsen/enchantgui/EnchantGUIPlugin.java @@ -76,7 +76,7 @@ public void onDisable() { } public void onReload() { - config.reloadConfig(); + config.reload(); hookPlayerPoints(); } diff --git a/src/main/java/me/tychsen/enchantgui/commands/ShopCommand.java b/src/main/java/me/tychsen/enchantgui/commands/ShopCommand.java index f76e24e..5e796d4 100644 --- a/src/main/java/me/tychsen/enchantgui/commands/ShopCommand.java +++ b/src/main/java/me/tychsen/enchantgui/commands/ShopCommand.java @@ -30,7 +30,7 @@ public void onGui(final Player player) { @Subcommand("toggle") @CommandPermission(EShopPermissionSys.TOGGLE) public void onToggle(final Player player) { - if (!EnchantGUIPlugin.getInstance().getMainConfig().getBoolean("right-click-enchanting-table")) { + if (EnchantGUIPlugin.getInstance().getMainConfig().isNotRightClickEnchantingTable()) { tell(player, lm.getLanguageString("disabled-feature")); return; } diff --git a/src/main/java/me/tychsen/enchantgui/config/EShopConfig.java b/src/main/java/me/tychsen/enchantgui/config/EShopConfig.java index 6ba1a39..e9ec0cd 100644 --- a/src/main/java/me/tychsen/enchantgui/config/EShopConfig.java +++ b/src/main/java/me/tychsen/enchantgui/config/EShopConfig.java @@ -1,7 +1,6 @@ package me.tychsen.enchantgui.config; import com.github.sarhatabaot.kraken.core.chat.ChatUtil; -import com.github.sarhatabaot.kraken.core.config.ConfigFile; import dev.dejvokep.boostedyaml.YamlDocument; import dev.dejvokep.boostedyaml.settings.loader.LoaderSettings; import me.tychsen.enchantgui.EnchantGUIPlugin; @@ -13,25 +12,27 @@ import java.io.File; import java.io.IOException; +import java.util.List; import java.util.Map; -public class EShopConfig extends ConfigFile { - private YamlDocument newConfig; +public class EShopConfig { + private YamlDocument config; + private final EnchantGUIPlugin plugin; private static PaymentStrategy economy; public EShopConfig() { - super(EnchantGUIPlugin.getInstance(), "", "config.yml", ""); - saveDefaultConfig(); - - + this.plugin = EnchantGUIPlugin.getInstance(); + this.createAndLoad(); } public void createAndLoad() { try { - this.newConfig = YamlDocument.create(new File(plugin.getDataFolder(), "config.yml"), plugin.getResource("config.yml"), + this.config = YamlDocument.create( + new File(plugin.getDataFolder(), "config.yml"), plugin.getResource("config.yml"), LoaderSettings.builder() .setAutoUpdate(true) - .build()); + .build() + ); } catch (IOException e) { this.plugin.getLogger().severe("Failed to load config.yml"); } @@ -39,7 +40,7 @@ public void createAndLoad() { public void reload() { try { - this.newConfig.reload(); + this.config.reload(); } catch (IOException e) { this.plugin.getLogger().severe("Failed to reload config.yml"); } @@ -60,9 +61,9 @@ public double getPrice(@NotNull Enchantment enchantment, int level) { return config.getDouble(path); } - @SuppressWarnings("BooleanMethodIsAlwaysInverted") - public boolean getBoolean(String path) { - return config.getBoolean(path); + + public boolean isNotRightClickEnchantingTable() { + return !config.getBoolean("right-click-enchanting-table"); } public String getMenuName() { @@ -83,8 +84,8 @@ public boolean getShowPerItem() { } public void reloadConfig(@NotNull CommandSender sender) { - reloadConfig(); - ChatUtil.sendMessage(sender, EnchantGUIPlugin.getInstance().getLm().getPrefix() + " " + EnchantGUIPlugin.getInstance().getLm().getLanguageString("config-reloaded")); + reload(); + ChatUtil.sendMessage(sender, EnchantGUIPlugin.getInstance().getLm().getPrefix() + " " + EnchantGUIPlugin.getInstance().getLm().getLanguageString("config-reloaded")); } @@ -92,7 +93,7 @@ public void reloadConfig(@NotNull CommandSender sender) { String path = enchantment.getKey().toString().toLowerCase(); path = path.split(":")[1]; EnchantGUIPlugin.debug(path); - Map enchantMap = config.getConfigurationSection(path).getValues(false); + Map enchantMap = config.getSection(path).getStringRouteMappedValues(false); String[] enchantLevels = new String[enchantMap.size()]; var position = 0; @@ -117,7 +118,7 @@ public PaymentStrategy getPaymentStrategy() { case "disable" -> setEconomy(new NullPayment()); default -> { final Material possibleMaterial = checkMaterialCurrency(); - if(possibleMaterial == Material.AIR) { + if (possibleMaterial == Material.AIR) { setEconomy(new NullPayment()); } else { setEconomy(new MaterialPayment(possibleMaterial)); @@ -131,7 +132,7 @@ public PaymentStrategy getPaymentStrategy() { private Material checkMaterialCurrency() { final String paymentType = getPaymentType(); - if(paymentType.startsWith("material")) { + if (paymentType.startsWith("material")) { final String possibleMaterial = paymentType.split(":")[1]; return Material.matchMaterial(possibleMaterial.toUpperCase()); } diff --git a/src/main/java/me/tychsen/enchantgui/config/EShopConfig.java~ b/src/main/java/me/tychsen/enchantgui/config/EShopConfig.java~ index 0bc1498..ee52965 100644 --- a/src/main/java/me/tychsen/enchantgui/config/EShopConfig.java~ +++ b/src/main/java/me/tychsen/enchantgui/config/EShopConfig.java~ @@ -1,7 +1,6 @@ package me.tychsen.enchantgui.config; import com.github.sarhatabaot.kraken.core.chat.ChatUtil; -import com.github.sarhatabaot.kraken.core.config.ConfigFile; import dev.dejvokep.boostedyaml.YamlDocument; import dev.dejvokep.boostedyaml.settings.loader.LoaderSettings; import me.tychsen.enchantgui.EnchantGUIPlugin; @@ -12,20 +11,39 @@ import org.bukkit.enchantments.Enchantment; import org.jetbrains.annotations.NotNull; import java.io.File; +import java.io.IOException; +import java.util.List; import java.util.Map; -public class EShopConfig extends ConfigFile { - private YamlDocument newConfig; +public class EShopConfig { + private YamlDocument config; + private final EnchantGUIPlugin plugin; private static PaymentStrategy economy; public EShopConfig() { - super(EnchantGUIPlugin.getInstance(), "", "config.yml", ""); - saveDefaultConfig(); + this.plugin = EnchantGUIPlugin.getInstance(); + this.createAndLoad(); + } + + public void createAndLoad() { + try { + this.config = YamlDocument.create( + new File(plugin.getDataFolder(), "config.yml"), plugin.getResource("config.yml"), + LoaderSettings.builder() + .setAutoUpdate(true) + .build() + ); + } catch (IOException e) { + this.plugin.getLogger().severe("Failed to load config.yml"); + } + } - newConfig = YamlDocument.create(new File(plugin.getDataFolder(), "config.yml"), plugin.getResource("config.yml"), - LoaderSettings.builder() - .setAutoUpdate(true) - .build()); + public void reload() { + try { + this.config.reload(); + } catch (IOException e) { + this.plugin.getLogger().severe("Failed to reload config.yml"); + } } @@ -43,9 +61,9 @@ public class EShopConfig extends ConfigFile { return config.getDouble(path); } - @SuppressWarnings("BooleanMethodIsAlwaysInverted") - public boolean getBoolean(String path) { - return config.getBoolean(path); + + public boolean isNotRightClickEnchantingTable() { + return !config.getBoolean("right-click-enchanting-table"); } public String getMenuName() { @@ -66,8 +84,8 @@ public class EShopConfig extends ConfigFile { } public void reloadConfig(@NotNull CommandSender sender) { - reloadConfig(); - ChatUtil.sendMessage(sender, EnchantGUIPlugin.getInstance().getLm().getPrefix() + " " + EnchantGUIPlugin.getInstance().getLm().getLanguageString("config-reloaded")); + reload(); + ChatUtil.sendMessage(sender, EnchantGUIPlugin.getInstance().getLm().getPrefix() + " " + EnchantGUIPlugin.getInstance().getLm().getLanguageString("config-reloaded")); } @@ -75,7 +93,7 @@ public class EShopConfig extends ConfigFile { String path = enchantment.getKey().toString().toLowerCase(); path = path.split(":")[1]; EnchantGUIPlugin.debug(path); - Map enchantMap = config.getConfigurationSection(path).getValues(false); + Map enchantMap = (Map) config.getMapList(path).get(0); String[] enchantLevels = new String[enchantMap.size()]; var position = 0; @@ -97,9 +115,10 @@ public class EShopConfig extends ConfigFile { case "money" -> setEconomy(new VaultPayment()); case "xp" -> setEconomy(new XPPayment()); case "playerpoints" -> setEconomy(new PlayerPointsPayment()); + case "disable" -> setEconomy(new NullPayment()); default -> { final Material possibleMaterial = checkMaterialCurrency(); - if(possibleMaterial == Material.AIR) { + if (possibleMaterial == Material.AIR) { setEconomy(new NullPayment()); } else { setEconomy(new MaterialPayment(possibleMaterial)); @@ -113,7 +132,7 @@ public class EShopConfig extends ConfigFile { private Material checkMaterialCurrency() { final String paymentType = getPaymentType(); - if(paymentType.startsWith("material")) { + if (paymentType.startsWith("material")) { final String possibleMaterial = paymentType.split(":")[1]; return Material.matchMaterial(possibleMaterial.toUpperCase()); } diff --git a/src/main/java/me/tychsen/enchantgui/event/EventManager.java b/src/main/java/me/tychsen/enchantgui/event/EventManager.java index 0919bf9..90dec5d 100644 --- a/src/main/java/me/tychsen/enchantgui/event/EventManager.java +++ b/src/main/java/me/tychsen/enchantgui/event/EventManager.java @@ -14,13 +14,15 @@ public class EventManager implements Listener { @EventHandler public void onPlayerInteractEvent(PlayerInteractEvent e) { - if (!EnchantGUIPlugin.getInstance().getMainConfig().getBoolean("right-click-enchanting-table")) { + if (EnchantGUIPlugin.getInstance().getMainConfig().isNotRightClickEnchantingTable()) { return; } - if (EnchantGUIPlugin.getInstance().getToggleRightClickPlayers().contains(e.getPlayer().getUniqueId())) + if (EnchantGUIPlugin.getInstance().getToggleRightClickPlayers().contains(e.getPlayer().getUniqueId())) { return; - if (!e.getPlayer().hasPermission(EShopPermissionSys.ENCHANTING_TABLE)) + } + if (!e.getPlayer().hasPermission(EShopPermissionSys.ENCHANTING_TABLE)) { return; + } if (e.getAction() == Action.RIGHT_CLICK_BLOCK && e.getClickedBlock().getType() == Material.ENCHANTING_TABLE) { e.setCancelled(true); diff --git a/src/main/java/me/tychsen/enchantgui/event/EventManager.java~ b/src/main/java/me/tychsen/enchantgui/event/EventManager.java~ deleted file mode 100644 index 40981cc..0000000 --- a/src/main/java/me/tychsen/enchantgui/event/EventManager.java~ +++ /dev/null @@ -1,42 +0,0 @@ -package me.tychsen.enchantgui.event; - -import me.tychsen.enchantgui.EnchantGUIPlugin; -import me.tychsen.enchantgui.permissions.EShopPermissionSys; -import org.bukkit.Material; -import org.bukkit.event.EventHandler; -import org.bukkit.event.Listener; -import org.bukkit.event.block.Action; -import org.bukkit.event.player.PlayerInteractEvent; -import org.jetbrains.annotations.NotNull; - - -public class EventManager implements Listener { - - @EventHandler - public void onPlayerInteractEvent(PlayerInteractEvent e) { - if (!EnchantGUIPlugin.getInstance().getMainConfig().getBoolean("right-click-enchanting-table")) { - return; - } - if (EnchantGUIPlugin.getInstance().getToggleRightClickPlayers().contains(e.getPlayer().getUniqueId())) - return; - if (!e.getPlayer().hasPermission(EShopPermissionSys.ENCHANTING_TABLE)) - return; - - if (e.getAction() == Action.RIGHT_CLICK_BLOCK && e.getClickedBlock().getType() == Material.ENCHANTING_TABLE) { - e.setCancelled(true); - handlePlayerInteractEvent(e); - } - } - - - private void handlePlayerInteractEvent(@NotNull PlayerInteractEvent event) { - if (event.getPlayer().hasPermission(EShopPermissionSys.ENCHANTING_TABLE)) { - if (EnchantGUIPlugin.getToggleRightClickPlayers().contains(event.getPlayer().getUniqueId())) { - return; - } - - EnchantGUIPlugin.getInstance().getShopMenu().showMainMenu(event.getPlayer()); - } - } - -}