diff --git a/build.gradle b/build.gradle index 81c91ec..79643f2 100644 --- a/build.gradle +++ b/build.gradle @@ -1,52 +1,51 @@ plugins { - id 'com.github.johnrengelman.shadow' version '7.1.0' - id "io.freefair.lombok" version "6.3.0" + id 'com.github.johnrengelman.shadow' version '7.1.2' + id 'io.freefair.lombok' version '6.3.0' id 'java-library' id 'org.sonarqube' version '3.3' } group = 'me.tychsen' -version = '1.6.1' - -sourceCompatibility = targetCompatibility = 16 +version = '1.7.0-SNAPSHOT' repositories { mavenCentral() maven { - name= "aikar-acf" - url = "https://repo.aikar.co/content/groups/aikar/" } + name= 'aikar-acf' + url = 'https://repo.aikar.co/content/groups/aikar/' } maven { - name = "spigot-repo" - url = "https://hub.spigotmc.org/nexus/content/repositories/snapshots/" + name = 'spigot-repo' + url = 'https://hub.spigotmc.org/nexus/content/repositories/snapshots/' } maven { - name = "vault-repo" - url = "https://nexus.hc.to/content/repositories/pub_releases" + name = 'vault-repo' + url = 'https://nexus.hc.to/content/repositories/pub_releases' } maven { - name = "bungeecord-chat" - url = "https://oss.sonatype.org/content/groups/public/" + name = 'bungeecord-chat' + url = 'https://oss.sonatype.org/content/groups/public/' } maven { - name = "codemc-repo" - url = "https://repo.codemc.org/repository/maven-public" + name = 'codemc-repo' + url = 'https://repo.codemc.org/repository/maven-public' } maven { - name = "jitpack" - url = "https://jitpack.io" + name = 'jitpack' + url = 'https://jitpack.io' } maven { - name = "playerpoints" + name = 'playerpoints' url = 'https://repo.rosewooddev.io/repository/public/' } } dependencies { - compileOnly "org.spigotmc:spigot-api:1.17-R0.1-SNAPSHOT" - compileOnly "com.github.MilkBowl:VaultAPI:1.7.1" + compileOnly 'org.spigotmc:spigot-api:1.17-R0.1-SNAPSHOT' + compileOnly 'com.github.MilkBowl:VaultAPI:1.7.1' compileOnly 'org.black_ixx:playerpoints:3.2.3' + api 'com.github.sarhatabaot:krakencore:1.1.3' api 'de.tr7zw:item-nbt-api:2.9.2' - api "co.aikar:acf-paper:0.5.1-SNAPSHOT" + api 'co.aikar:acf-paper:0.5.1-SNAPSHOT' api 'org.bstats:bstats-bukkit:3.0.0' api group: 'org.jetbrains', name: 'annotations', version: '23.0.0' } @@ -55,6 +54,7 @@ rootProject.tasks.named("processResources") { duplicatesStrategy = DuplicatesStrategy.INCLUDE } + processResources { def props = [version: version] inputs.properties props @@ -70,17 +70,24 @@ shadowJar { relocate 'co.aikar.commands', 'me.tychsen.enchantgui.acf' relocate 'co.aikar.locales', 'me.tychsen.enchantgui.locales' relocate 'de.tr7zw', 'me.tychsen.enchantgui.nbt' + relocate 'com.github.sarhatabaot.kraken', 'me.tychsen.enchantgui.kraken' } wrapper { - gradleVersion = "7.4.2" + gradleVersion = '7.4.2' } sonarqube { properties { - property "sonar.projectKey", "sarhatabaot_EnchantGUI" - property "sonar.organization", "sarhatabaot-github" - property "sonar.host.url", "https://sonarcloud.io" + property 'sonar.projectKey', 'sarhatabaot_EnchantGUI' + property 'sonar.organization', 'sarhatabaot-github' + property 'sonar.host.url', 'https://sonarcloud.io' + } +} + +java { + toolchain { + languageVersion.set(JavaLanguageVersion.of(16)) } } diff --git a/settings.gradle b/settings.gradle index 299f63f..cfce86a 100644 --- a/settings.gradle +++ b/settings.gradle @@ -1,2 +1 @@ -rootProject.name = 'EnchantGUI' - +rootProject.name = 'EnchantGUI' \ No newline at end of file diff --git a/src/main/java/me/tychsen/enchantgui/ChatUtil.java b/src/main/java/me/tychsen/enchantgui/ChatUtil.java deleted file mode 100644 index 92d4243..0000000 --- a/src/main/java/me/tychsen/enchantgui/ChatUtil.java +++ /dev/null @@ -1,21 +0,0 @@ -package me.tychsen.enchantgui; - -import org.bukkit.ChatColor; -import org.bukkit.command.CommandSender; -import org.jetbrains.annotations.Contract; -import org.jetbrains.annotations.NotNull; - -/** - * @author kangarko - */ -public class ChatUtil { - public static void tell(@NotNull CommandSender toWhom, String message) { - toWhom.sendMessage(colorize(message)); - } - - @Contract("_ -> new") - public static @NotNull String colorize(String message) { - return ChatColor.translateAlternateColorCodes('&', message); - } - -} diff --git a/src/main/java/me/tychsen/enchantgui/Main.java b/src/main/java/me/tychsen/enchantgui/Main.java index 77e0ea7..6385a8e 100644 --- a/src/main/java/me/tychsen/enchantgui/Main.java +++ b/src/main/java/me/tychsen/enchantgui/Main.java @@ -7,6 +7,7 @@ import me.tychsen.enchantgui.commands.ShopCommand; import me.tychsen.enchantgui.config.EShopConfig; import me.tychsen.enchantgui.event.EventManager; +import me.tychsen.enchantgui.localization.LocalizationManager; import me.tychsen.enchantgui.menu.DefaultMenuSystem; import me.tychsen.enchantgui.menu.MenuSystem; import net.milkbowl.vault.economy.Economy; @@ -16,6 +17,7 @@ import org.bukkit.Bukkit; import org.bukkit.plugin.RegisteredServiceProvider; import org.bukkit.plugin.java.JavaPlugin; +import org.jetbrains.annotations.NotNull; import java.util.HashSet; import java.util.Set; @@ -31,15 +33,19 @@ public class Main extends JavaPlugin { @Getter @Setter (AccessLevel.PRIVATE) private static Set toggleRightClickPlayers = new HashSet<>(); + private EShopConfig config; + private LocalizationManager lm; + @Getter private PlayerPointsAPI ppApi; @Override public void onEnable() { setInstance(this); + config = new EShopConfig(); + lm = new LocalizationManager(); hookVaultEconomy(); - // Generate config.yml if there is none - saveDefaultConfig(); + // Register event manager setMenuSystem(new DefaultMenuSystem()); @@ -55,7 +61,7 @@ public void onEnable() { new Metrics(this, 3871); getLogger().info(() -> getName() + " " + getDescription().getVersion() + " enabled!"); - getLogger().info(() -> getName() + " " + getDescription().getVersion() + " using: "+ EShopConfig.getPaymentStrategy().name()); + getLogger().info(() -> getName() + " " + getDescription().getVersion() + " using: "+ Main.getInstance().getMainConfig().getPaymentStrategy().name()); } @Override @@ -68,17 +74,19 @@ public void onDisable() { } public void onReload(){ + config.reloadConfig(); + hookVaultEconomy(); hookPlayerPoints(); } public static void debug(String msg) { - if (EShopConfig.getDebug()) + if (Main.getInstance().getMainConfig().getDebug()) Main.getInstance().getLogger().warning(() -> String.format("DEBUG %s", msg)); } private void hookVaultEconomy() { - if(!EShopConfig.getPaymentType().equalsIgnoreCase("money")) { + if(!Main.getInstance().getMainConfig().getPaymentType().equalsIgnoreCase("money")) { return; } if (getServer().getPluginManager().getPlugin("Vault") == null) { @@ -94,7 +102,7 @@ private void hookVaultEconomy() { } private void hookPlayerPoints() { - if(!EShopConfig.getPaymentType().equalsIgnoreCase("playerpoints")) { + if(!config.getPaymentType().equalsIgnoreCase("playerpoints")) { return; } if (Bukkit.getPluginManager().isPluginEnabled("PlayerPoints")) { @@ -102,6 +110,14 @@ private void hookPlayerPoints() { } } + @NotNull + public EShopConfig getMainConfig() { + return config; + } + + public LocalizationManager getLm() { + return lm; + } } \ No newline at end of file diff --git a/src/main/java/me/tychsen/enchantgui/commands/ShopCommand.java b/src/main/java/me/tychsen/enchantgui/commands/ShopCommand.java index c0c75cd..c84ec54 100644 --- a/src/main/java/me/tychsen/enchantgui/commands/ShopCommand.java +++ b/src/main/java/me/tychsen/enchantgui/commands/ShopCommand.java @@ -2,14 +2,12 @@ import co.aikar.commands.BaseCommand; import co.aikar.commands.annotation.*; +import com.github.sarhatabaot.kraken.core.chat.ChatUtil; import me.tychsen.enchantgui.Main; -import me.tychsen.enchantgui.config.EShopConfig; -import me.tychsen.enchantgui.config.EShopEnchants; -import me.tychsen.enchantgui.config.EShopShop; +import me.tychsen.enchantgui.config.Enchants; import me.tychsen.enchantgui.localization.LocalizationManager; import me.tychsen.enchantgui.menu.DefaultMenuSystem; import me.tychsen.enchantgui.menu.MenuSystem; -import me.tychsen.enchantgui.ChatUtil; import me.tychsen.enchantgui.permissions.EShopPermissionSys; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; @@ -21,10 +19,10 @@ public class ShopCommand extends BaseCommand { private final String prefix; private final MenuSystem menuSystem; - private final LocalizationManager lm = LocalizationManager.getInstance(); + private final LocalizationManager lm = Main.getInstance().getLm(); public ShopCommand() { - this.prefix = lm.getString("prefix"); + this.prefix = lm.getLanguageString("prefix"); this.menuSystem = Main.getMenuSystem(); } @@ -37,18 +35,18 @@ public void onGui(final Player player){ @Subcommand("toggle") @CommandPermission(EShopPermissionSys.TOGGLE) public void onToggle(final Player player){ - if(!EShopConfig.getBoolean("right-click-enchanting-table")){ - tell(player,lm.getString("disabled-feature")); + if(!Main.getInstance().getMainConfig().getBoolean("right-click-enchanting-table")){ + tell(player,lm.getLanguageString("disabled-feature")); return; } if(Main.getToggleRightClickPlayers().contains(player.getUniqueId())){ Main.getToggleRightClickPlayers().remove(player.getUniqueId()); - tell(player,lm.getString("toggle-on")); + tell(player,lm.getLanguageString("toggle-on")); } else { Main.getToggleRightClickPlayers().add(player.getUniqueId()); - tell(player,lm.getString("toggle-off")); + tell(player,lm.getLanguageString("toggle-off")); } } @@ -56,16 +54,13 @@ public void onToggle(final Player player){ @Subcommand("reload") @CommandPermission(EShopPermissionSys.RELOAD) public void onReload(final CommandSender player){ - EShopConfig.reloadConfig(player); - LocalizationManager.getInstance().reload(player); - Main.setMenuSystem(new DefaultMenuSystem()); - Main.getMenuSystem().getMenuGenerator().setShopEnchants(new EShopEnchants()); - EShopShop.getInstance().reload(player); Main.getInstance().onReload(); - Main.getInstance().getLogger().info(getName() + " " + Main.getInstance().getDescription().getVersion() + " using: "+ EShopConfig.getPaymentStrategy().name()); + Main.getInstance().getLm().reload(player); + Main.getMenuSystem().reload(); + Main.getInstance().getLogger().info(() -> "%s %s using: %s".formatted(getName(),Main.getInstance().getDescription().getVersion(),Main.getInstance().getMainConfig().getPaymentStrategy().name())); } private void tell(final Player player, final String message){ - ChatUtil.tell(player,String.format("%s %s",prefix,message)); + ChatUtil.sendMessage(player,String.format("%s %s",prefix,message)); } } diff --git a/src/main/java/me/tychsen/enchantgui/config/AConfig.java b/src/main/java/me/tychsen/enchantgui/config/AConfig.java deleted file mode 100644 index 362bbad..0000000 --- a/src/main/java/me/tychsen/enchantgui/config/AConfig.java +++ /dev/null @@ -1,84 +0,0 @@ -package me.tychsen.enchantgui.config; - -import lombok.AccessLevel; -import lombok.Getter; -import lombok.Setter; -import org.bukkit.configuration.file.FileConfiguration; -import org.bukkit.configuration.file.YamlConfiguration; -import org.bukkit.plugin.java.JavaPlugin; - -import java.io.File; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.io.Reader; -import java.nio.charset.StandardCharsets; - -/** - * @author sarhatabaot - */ -public abstract class AConfig { - @Setter - @Getter (AccessLevel.PROTECTED) - private String fileName; - private File configFile; - private final JavaPlugin plugin; - private FileConfiguration config; - - /** - * - * @param fileName file name including the type. - * @param plugin plugin instance. - */ - public AConfig(final String fileName, final JavaPlugin plugin) { - this.fileName = fileName; - this.plugin = plugin; - } - - /** - * Reloads the file - */ - protected void reloadConfig(){ - if (configFile == null || !configFile.getName().equalsIgnoreCase(getFileName())) { - configFile = new File(plugin.getDataFolder(), getFileName()); - } - config = YamlConfiguration.loadConfiguration(configFile); - - // Look for defaults in the jar - Reader defaultConfigStream; - InputStream defaultConfigInputStream = plugin.getResource(getFileName()); - if (defaultConfigInputStream != null) { - defaultConfigStream = new InputStreamReader(defaultConfigInputStream, StandardCharsets.UTF_8); - YamlConfiguration defaultConfig = YamlConfiguration.loadConfiguration(defaultConfigStream); - config.setDefaults(defaultConfig); - } - - plugin.getLogger().info(getFileName()+" reloaded."); - } - - - /** - * Saves the default configuration from the jar. - */ - protected void saveDefaultConfiguration(){ - if (configFile == null) { - configFile = new File(plugin.getDataFolder(), getFileName()); - } - if (!configFile.exists()) { - plugin.saveResource(getFileName(), false); - } - } - - /** - * @return FileConfiguration config. - */ - protected FileConfiguration getConfig() { - if (config == null) { - reloadConfig(); - } - return config; - } - - public abstract String getString(String path); - - public abstract String getString(String path, String def); -} diff --git a/src/main/java/me/tychsen/enchantgui/config/EShopConfig.java b/src/main/java/me/tychsen/enchantgui/config/EShopConfig.java index b3ab6f0..1f7b070 100644 --- a/src/main/java/me/tychsen/enchantgui/config/EShopConfig.java +++ b/src/main/java/me/tychsen/enchantgui/config/EShopConfig.java @@ -1,86 +1,73 @@ package me.tychsen.enchantgui.config; -import lombok.Getter; +import com.github.sarhatabaot.kraken.core.chat.ChatUtil; +import com.github.sarhatabaot.kraken.core.config.ConfigFile; import lombok.Setter; -import me.tychsen.enchantgui.economy.PlayerPointsPayment; -import me.tychsen.enchantgui.economy.VaultPayment; +import me.tychsen.enchantgui.Main; import me.tychsen.enchantgui.economy.NullPayment; import me.tychsen.enchantgui.economy.PaymentStrategy; +import me.tychsen.enchantgui.economy.PlayerPointsPayment; +import me.tychsen.enchantgui.economy.VaultPayment; import me.tychsen.enchantgui.economy.XPPayment; -import me.tychsen.enchantgui.localization.LocalizationManager; -import me.tychsen.enchantgui.menu.DefaultMenuSystem; -import me.tychsen.enchantgui.Main; -import me.tychsen.enchantgui.ChatUtil; import org.bukkit.command.CommandSender; -import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.enchantments.Enchantment; import org.jetbrains.annotations.NotNull; import java.util.Map; -public class EShopConfig { - @Setter - @Getter - private static EShopConfig instance; - @Setter - private static FileConfiguration config; +public class EShopConfig extends ConfigFile
{ + @Setter private static PaymentStrategy economy; public EShopConfig() { - setInstance(this); - setConfig(Main.getInstance().getConfig()); + super(Main.getInstance(), "", "config.yml", ""); + saveDefaultConfig(); } - public static boolean getIgnoreItemType(){ + public boolean getIgnoreItemType() { return config.getBoolean("ignore-itemtype"); } - public static boolean getDebug(){ + public boolean getDebug() { return config.getBoolean("debug"); } - public static double getPrice(@NotNull Enchantment enchantment, int level) { + public double getPrice(@NotNull Enchantment enchantment, int level) { String path = enchantment.getKey().toString().toLowerCase() + ".level" + level; path = path.split(":")[1]; return config.getDouble(path); } - public static boolean getBoolean(String path){ + public boolean getBoolean(String path) { return config.getBoolean(path); } - public static String getMenuName() { + public String getMenuName() { var path = "menu-name"; - if (config.contains(path) && config.isSet(path) && config.isString(path)) { - if (config.getString(path).length() > 32) { - return config.getString(path).substring(0, 32); - } else { - return config.getString(path); - } - } else { - return "EnchantGUI"; + final String defaultName = "EnchantGUI"; + + if (config.getString(path, defaultName).length() > 32) { + return config.getString(path, defaultName).substring(0, 32); } + + return config.getString(path, defaultName); + + } - public static boolean getShowPerItem(){ + public boolean getShowPerItem() { return config.getBoolean("show-per-item"); } - public static void reloadConfig(@NotNull CommandSender sender) { - LocalizationManager lm = LocalizationManager.getInstance(); - if (sender.isOp() || sender.hasPermission("eshop.admin")) { - Main.getInstance().reloadConfig(); - setConfig(Main.getInstance().getConfig()); - economy = null; - ChatUtil.tell(sender,DefaultMenuSystem.PREFIX + lm.getString("config-reloaded")); - } else { - ChatUtil.tell(sender,DefaultMenuSystem.PREFIX + lm.getString("no-permission")); - } + public void reloadConfig(@NotNull CommandSender sender) { + reloadConfig(); + ChatUtil.sendMessage(sender, Main.getInstance().getLm().getPrefix() + " " + Main.getInstance().getLm().getLanguageString("config-reloaded")); } - public static String @NotNull [] getEnchantLevels(@NotNull Enchantment enchantment) { + + public @NotNull String[] getEnchantLevels(@NotNull Enchantment enchantment) { String path = enchantment.getKey().toString().toLowerCase(); path = path.split(":")[1]; Main.debug(path); @@ -96,7 +83,7 @@ public static void reloadConfig(@NotNull CommandSender sender) { return enchantLevels; } - public static String getEconomyCurrency(){ + public String getEconomyCurrency() { return switch (getPaymentType().toLowerCase()) { case "money" -> Main.getEconomy().currencyNameSingular(); case "xp" -> "levels"; @@ -104,11 +91,11 @@ public static String getEconomyCurrency(){ }; } - public static String getPaymentType() { - return config.getString(config.getString("payment-currency", "xp")); + public String getPaymentType() { + return config.getString("payment-currency", "xp"); } - public static PaymentStrategy getPaymentStrategy() { + public PaymentStrategy getPaymentStrategy() { if (economy == null) { switch (getPaymentType().toLowerCase()) { case "money" -> setEconomy(new VaultPayment()); @@ -120,8 +107,8 @@ public static PaymentStrategy getPaymentStrategy() { return economy; } - public static String getLanguage() { - return config.getString("language","en"); + public String getLanguage() { + return config.getString("language", "en"); } } diff --git a/src/main/java/me/tychsen/enchantgui/config/EShopShop.java b/src/main/java/me/tychsen/enchantgui/config/EShopShop.java deleted file mode 100644 index a20897a..0000000 --- a/src/main/java/me/tychsen/enchantgui/config/EShopShop.java +++ /dev/null @@ -1,40 +0,0 @@ -package me.tychsen.enchantgui.config; - -import me.tychsen.enchantgui.Main; -import me.tychsen.enchantgui.localization.LocalizationManager; -import org.bukkit.command.CommandSender; -import org.jetbrains.annotations.NotNull; - -/** - * @author sarhatabaot - */ -public class EShopShop extends AConfig { - private static EShopShop instance; - - private EShopShop() { - super("languages/"+EShopConfig.getLanguage()+"/shop.yml", Main.getInstance()); - saveDefaultConfiguration(); - } - - public void reload(@NotNull CommandSender sender) { - reloadConfig(); - sender.sendMessage(LocalizationManager.getInstance().getString("prefix") + " " + LocalizationManager.getInstance().getString("shop-reloaded")); - } - - @Override - public String getString(String path, String def) { - return getConfig().getString(path, def); - } - - @Override - public String getString(final String path) { - return getConfig().getString(path); - } - - public static EShopShop getInstance() { - if (instance == null) { - instance = new EShopShop(); - } - return instance; - } -} diff --git a/src/main/java/me/tychsen/enchantgui/config/EShopEnchants.java b/src/main/java/me/tychsen/enchantgui/config/Enchants.java similarity index 59% rename from src/main/java/me/tychsen/enchantgui/config/EShopEnchants.java rename to src/main/java/me/tychsen/enchantgui/config/Enchants.java index 4c65fec..0d5022b 100644 --- a/src/main/java/me/tychsen/enchantgui/config/EShopEnchants.java +++ b/src/main/java/me/tychsen/enchantgui/config/Enchants.java @@ -1,5 +1,6 @@ package me.tychsen.enchantgui.config; +import me.tychsen.enchantgui.Main; import me.tychsen.enchantgui.localization.LocalizationManager; import org.bukkit.Material; import org.bukkit.enchantments.Enchantment; @@ -11,12 +12,11 @@ import java.util.ArrayList; import java.util.List; -public class EShopEnchants { - private final List enchantList = new ArrayList<>(); - private final EShopShop shop; +public class Enchants { + private List enchantList; - public EShopEnchants() { - shop = EShopShop.getInstance(); + public Enchants() { + enchantList = new ArrayList<>(); createEnchantList(); } @@ -24,8 +24,13 @@ public List getEnchantList() { return enchantList; } + public void reload() { + enchantList = new ArrayList<>(); + createEnchantList(); + } + private Material matchEnchant(@NotNull String enchantment) { - return Material.matchMaterial(shop.getString("shop.enchants." + enchantment)); + return Material.matchMaterial(Main.getInstance().getLm().getActiveShopFile().getString("shop.enchants." + enchantment)); } /** @@ -33,45 +38,45 @@ private Material matchEnchant(@NotNull String enchantment) { * Material and a display name. */ private void createEnchantList() { - LocalizationManager lm = LocalizationManager.getInstance(); - addItem(Enchantment.ARROW_DAMAGE, matchEnchant("power"), lm.getString("enchant.power")); - addItem(Enchantment.ARROW_FIRE, matchEnchant("flame"), lm.getString("enchant.flame")); - addItem(Enchantment.ARROW_INFINITE, matchEnchant("infinity"), lm.getString("enchant.infinity")); - addItem(Enchantment.ARROW_KNOCKBACK, matchEnchant("punch"), lm.getString("enchant.punch")); - addItem(Enchantment.DAMAGE_ALL, matchEnchant("sharpness"), lm.getString("enchant.sharpness")); - addItem(Enchantment.DAMAGE_ARTHROPODS, matchEnchant("bane_of_arthropods"), lm.getString("enchant.bane_of_arthropods")); - addItem(Enchantment.DAMAGE_UNDEAD, matchEnchant("smite"), lm.getString("enchant.smite")); - addItem(Enchantment.DEPTH_STRIDER, matchEnchant("depth_strider"), lm.getString("enchant.depth_strider")); - addItem(Enchantment.DIG_SPEED, matchEnchant("efficiency"), lm.getString("enchant.efficiency")); - addItem(Enchantment.DURABILITY, matchEnchant("unbreaking"), lm.getString("enchant.unbreaking")); - addItem(Enchantment.FIRE_ASPECT, matchEnchant("fire_aspect"), lm.getString("enchant.fire_aspect")); - addItem(Enchantment.KNOCKBACK, Material.DIAMOND_SWORD, lm.getString("enchant.knockback")); - addItem(Enchantment.LOOT_BONUS_BLOCKS, matchEnchant("fortune"), lm.getString("enchant.fortune")); - addItem(Enchantment.LOOT_BONUS_MOBS, matchEnchant("looting"), lm.getString("enchant.looting")); - addItem(Enchantment.LUCK, matchEnchant("luck_of_the_sea"), lm.getString("enchant.luck_of_the_sea")); - addItem(Enchantment.LURE, matchEnchant("lure"), lm.getString("enchant.lure")); - addItem(Enchantment.OXYGEN, matchEnchant("respiration"), lm.getString("enchant.respiration")); - addItem(Enchantment.PROTECTION_ENVIRONMENTAL, matchEnchant("protection"), lm.getString("enchant.protection")); - addItem(Enchantment.PROTECTION_EXPLOSIONS, matchEnchant("blast_protection"), lm.getString("enchant.blast_protection")); - addItem(Enchantment.PROTECTION_FALL, matchEnchant("feather_falling"), lm.getString("enchant.feather_falling")); - addItem(Enchantment.PROTECTION_FIRE, matchEnchant("fire_protection"), lm.getString("enchant.fire_protection")); - addItem(Enchantment.PROTECTION_PROJECTILE, matchEnchant("projectile_protection"), lm.getString("enchant.projectile_protection")); - addItem(Enchantment.SILK_TOUCH, matchEnchant("silk_touch"), lm.getString("enchant.silk_touch")); - addItem(Enchantment.THORNS, matchEnchant("thorns"), lm.getString("enchant.thorns")); - addItem(Enchantment.WATER_WORKER, matchEnchant("aqua_affinity"), lm.getString("enchant.aqua_affinity")); - addItem(Enchantment.FROST_WALKER, matchEnchant("frost_walker"), lm.getString("enchant.frost_walker")); - addItem(Enchantment.MENDING, matchEnchant("mending"), lm.getString("enchant.mending")); - addItem(Enchantment.SWEEPING_EDGE, matchEnchant("sweeping"), lm.getString("enchant.sweeping")); - addItem(Enchantment.CHANNELING, matchEnchant("channeling"), lm.getString("enchant.channeling")); - addItem(Enchantment.IMPALING, matchEnchant("impaling"), lm.getString("enchant.impaling")); - addItem(Enchantment.LOYALTY, matchEnchant("loyalty"), lm.getString("enchant.loyalty")); - addItem(Enchantment.RIPTIDE, matchEnchant("riptide"), lm.getString("enchant.riptide")); + LocalizationManager lm = Main.getInstance().getLm(); + addItem(Enchantment.ARROW_DAMAGE, matchEnchant("power"), lm.getLanguageString("enchant.power")); + addItem(Enchantment.ARROW_FIRE, matchEnchant("flame"), lm.getLanguageString("enchant.flame")); + addItem(Enchantment.ARROW_INFINITE, matchEnchant("infinity"), lm.getLanguageString("enchant.infinity")); + addItem(Enchantment.ARROW_KNOCKBACK, matchEnchant("punch"), lm.getLanguageString("enchant.punch")); + addItem(Enchantment.DAMAGE_ALL, matchEnchant("sharpness"), lm.getLanguageString("enchant.sharpness")); + addItem(Enchantment.DAMAGE_ARTHROPODS, matchEnchant("bane_of_arthropods"), lm.getLanguageString("enchant.bane_of_arthropods")); + addItem(Enchantment.DAMAGE_UNDEAD, matchEnchant("smite"), lm.getLanguageString("enchant.smite")); + addItem(Enchantment.DEPTH_STRIDER, matchEnchant("depth_strider"), lm.getLanguageString("enchant.depth_strider")); + addItem(Enchantment.DIG_SPEED, matchEnchant("efficiency"), lm.getLanguageString("enchant.efficiency")); + addItem(Enchantment.DURABILITY, matchEnchant("unbreaking"), lm.getLanguageString("enchant.unbreaking")); + addItem(Enchantment.FIRE_ASPECT, matchEnchant("fire_aspect"), lm.getLanguageString("enchant.fire_aspect")); + addItem(Enchantment.KNOCKBACK, Material.DIAMOND_SWORD, lm.getLanguageString("enchant.knockback")); + addItem(Enchantment.LOOT_BONUS_BLOCKS, matchEnchant("fortune"), lm.getLanguageString("enchant.fortune")); + addItem(Enchantment.LOOT_BONUS_MOBS, matchEnchant("looting"), lm.getLanguageString("enchant.looting")); + addItem(Enchantment.LUCK, matchEnchant("luck_of_the_sea"), lm.getLanguageString("enchant.luck_of_the_sea")); + addItem(Enchantment.LURE, matchEnchant("lure"), lm.getLanguageString("enchant.lure")); + addItem(Enchantment.OXYGEN, matchEnchant("respiration"), lm.getLanguageString("enchant.respiration")); + addItem(Enchantment.PROTECTION_ENVIRONMENTAL, matchEnchant("protection"), lm.getLanguageString("enchant.protection")); + addItem(Enchantment.PROTECTION_EXPLOSIONS, matchEnchant("blast_protection"), lm.getLanguageString("enchant.blast_protection")); + addItem(Enchantment.PROTECTION_FALL, matchEnchant("feather_falling"), lm.getLanguageString("enchant.feather_falling")); + addItem(Enchantment.PROTECTION_FIRE, matchEnchant("fire_protection"), lm.getLanguageString("enchant.fire_protection")); + addItem(Enchantment.PROTECTION_PROJECTILE, matchEnchant("projectile_protection"), lm.getLanguageString("enchant.projectile_protection")); + addItem(Enchantment.SILK_TOUCH, matchEnchant("silk_touch"), lm.getLanguageString("enchant.silk_touch")); + addItem(Enchantment.THORNS, matchEnchant("thorns"), lm.getLanguageString("enchant.thorns")); + addItem(Enchantment.WATER_WORKER, matchEnchant("aqua_affinity"), lm.getLanguageString("enchant.aqua_affinity")); + addItem(Enchantment.FROST_WALKER, matchEnchant("frost_walker"), lm.getLanguageString("enchant.frost_walker")); + addItem(Enchantment.MENDING, matchEnchant("mending"), lm.getLanguageString("enchant.mending")); + addItem(Enchantment.SWEEPING_EDGE, matchEnchant("sweeping"), lm.getLanguageString("enchant.sweeping")); + addItem(Enchantment.CHANNELING, matchEnchant("channeling"), lm.getLanguageString("enchant.channeling")); + addItem(Enchantment.IMPALING, matchEnchant("impaling"), lm.getLanguageString("enchant.impaling")); + addItem(Enchantment.LOYALTY, matchEnchant("loyalty"), lm.getLanguageString("enchant.loyalty")); + addItem(Enchantment.RIPTIDE, matchEnchant("riptide"), lm.getLanguageString("enchant.riptide")); - addItem(Enchantment.PIERCING, matchEnchant("piercing"), lm.getString("enchant.piercing")); - addItem(Enchantment.MULTISHOT, matchEnchant("multishot"), lm.getString("enchant.multishot")); - addItem(Enchantment.QUICK_CHARGE, matchEnchant("quick_charge"), lm.getString("enchant.quick_charge")); + addItem(Enchantment.PIERCING, matchEnchant("piercing"), lm.getLanguageString("enchant.piercing")); + addItem(Enchantment.MULTISHOT, matchEnchant("multishot"), lm.getLanguageString("enchant.multishot")); + addItem(Enchantment.QUICK_CHARGE, matchEnchant("quick_charge"), lm.getLanguageString("enchant.quick_charge")); - addItem(Enchantment.SOUL_SPEED, matchEnchant("soul_speed"), lm.getString("enchant.soul_speed")); + addItem(Enchantment.SOUL_SPEED, matchEnchant("soul_speed"), lm.getLanguageString("enchant.soul_speed")); } /** diff --git a/src/main/java/me/tychsen/enchantgui/event/EventManager.java b/src/main/java/me/tychsen/enchantgui/event/EventManager.java index 6f182c6..48518b2 100644 --- a/src/main/java/me/tychsen/enchantgui/event/EventManager.java +++ b/src/main/java/me/tychsen/enchantgui/event/EventManager.java @@ -1,8 +1,8 @@ package me.tychsen.enchantgui.event; -import me.tychsen.enchantgui.config.EShopConfig; import me.tychsen.enchantgui.Main; import me.tychsen.enchantgui.menu.MenuSystem; +import me.tychsen.enchantgui.permissions.EShopPermissionSys; import org.bukkit.Material; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; @@ -35,7 +35,8 @@ public void onInventoryClickEvent(@NotNull InventoryClickEvent e) { } catch (IllegalStateException exception){ inventoryName = ""; } - String configInventoryName = EShopConfig.getMenuName().toLowerCase(); + //todo, this could cause problems if our inventory name is not unique + String configInventoryName = Main.getInstance().getMainConfig().getMenuName().toLowerCase(); boolean correctEvent = inventoryName.startsWith(configInventoryName); if (correctEvent) { @@ -52,12 +53,12 @@ public void onInventoryClickEvent(@NotNull InventoryClickEvent e) { @EventHandler public void onPlayerInteractEvent(PlayerInteractEvent e) { - if (!EShopConfig.getBoolean("right-click-enchanting-table")) { + if (!Main.getInstance().getMainConfig().getBoolean("right-click-enchanting-table")) { return; } if (Main.getToggleRightClickPlayers().contains(e.getPlayer().getUniqueId())) return; - if(!e.getPlayer().hasPermission("eshop.enchantingtable")) + if(!e.getPlayer().hasPermission(EShopPermissionSys.ENCHANTING_TABLE)) return; if (e.getAction() == Action.RIGHT_CLICK_BLOCK && e.getClickedBlock().getType() == Material.ENCHANTING_TABLE) { @@ -76,7 +77,7 @@ private void handleInventoryClickEvent(@NotNull InventoryClickEvent event) { } private void handlePlayerInteractEvent(@NotNull PlayerInteractEvent event) { - if (event.getPlayer().hasPermission("eshop.enchantingtable")) { + if (event.getPlayer().hasPermission(EShopPermissionSys.ENCHANTING_TABLE)) { if (Main.getToggleRightClickPlayers().contains(event.getPlayer().getUniqueId())) { return; } diff --git a/src/main/java/me/tychsen/enchantgui/localization/LocalLanguageConfigFile.java b/src/main/java/me/tychsen/enchantgui/localization/LocalLanguageConfigFile.java new file mode 100644 index 0000000..e80b22d --- /dev/null +++ b/src/main/java/me/tychsen/enchantgui/localization/LocalLanguageConfigFile.java @@ -0,0 +1,19 @@ +package me.tychsen.enchantgui.localization; + + +/** + * @author sarhatabaot + */ +public class LocalLanguageConfigFile extends LocalizedConfigFile { + public LocalLanguageConfigFile(final String lang) { + super(lang, "localization.yml"); + } + + public String getPrefix() { + return getConfig().getString("prefix","EnchantGUI"); + } + + public String getShopReloaded() { + return getConfig().getString("shop-reloaded", "Shop file has been reloaded!"); + } +} diff --git a/src/main/java/me/tychsen/enchantgui/localization/LocalizationManager.java b/src/main/java/me/tychsen/enchantgui/localization/LocalizationManager.java index 79ec9d0..9d23b64 100644 --- a/src/main/java/me/tychsen/enchantgui/localization/LocalizationManager.java +++ b/src/main/java/me/tychsen/enchantgui/localization/LocalizationManager.java @@ -1,47 +1,82 @@ package me.tychsen.enchantgui.localization; -import me.tychsen.enchantgui.ChatUtil; +import com.github.sarhatabaot.kraken.core.chat.ChatUtil; import me.tychsen.enchantgui.Main; -import me.tychsen.enchantgui.config.AConfig; -import me.tychsen.enchantgui.config.EShopConfig; import org.bukkit.command.CommandSender; +import org.jetbrains.annotations.Contract; +import org.jetbrains.annotations.Unmodifiable; -public class LocalizationManager extends AConfig { - private static LocalizationManager instance; +import java.io.File; +import java.util.HashMap; +import java.util.List; +import java.util.Map; - private LocalizationManager() { - super("localization/" + EShopConfig.getLanguage() + "/localization.yml", Main.getInstance()); - saveDefaultConfiguration(); - } +public class LocalizationManager { + private final Map> languages; + + public LocalizationManager() { + languages = new HashMap<>(); + for(String lang: getSupportedLanguages()) { + languages.putIfAbsent(lang,new HashMap<>()); + languages.get(lang).put("localization", new LocalLanguageConfigFile(lang)); + languages.get(lang).put("shop", new ShopLanguageConfigFile(lang)); + } + final File languagesFolder = new File(Main.getInstance().getDataFolder(),"languages"); + if(!languagesFolder.exists()) { + boolean made = languagesFolder.mkdirs(); + Main.getInstance().getLogger().info(() -> "Created folder: languages= %b".formatted(made)); + } + for(Map.Entry> entry: languages.entrySet()) { + final File langFolder = new File(languagesFolder,entry.getKey()); + if(!langFolder.exists()) { + boolean made = langFolder.mkdirs(); + Main.getInstance().getLogger().info(() -> "Created folder: %s= %b".formatted(entry.getKey(),made)); + } + for(Map.Entry localizedConfigFileEntry: entry.getValue().entrySet()) { + localizedConfigFileEntry.getValue().saveDefaultConfig(); + } + + } - @Override - public String getString(String path) { - return ChatUtil.colorize(getConfig().getString(path)); } - @Override - public String getString(final String path, final String def) { - return ChatUtil.colorize(getConfig().getString(path,def)); + public String getLanguageString(String path) { + return ChatUtil.color(getActiveLanguageFile().getConfig().getString(path)); } public void reload(CommandSender sender) { - Main.debug(EShopConfig.getLanguage()); - setFileName("localization/" + EShopConfig.getLanguage() + "/localization.yml"); - saveDefaultConfiguration(); - reloadConfig(); - ChatUtil.tell(sender, instance.getString("prefix") + " " + instance.getString("localization-reloaded")); + for(Map.Entry> entry: languages.entrySet()){ + //reload the files (if you changed your local setup) + entry.getValue().forEach((key, value) -> value.reloadConfig()); + } + ChatUtil.sendMessage(sender, getPrefix() + " " + getActiveLanguageFile().getConfig().getString("localization-reloaded")); + } + + + + public LocalLanguageConfigFile getActiveLanguageFile() { + return (LocalLanguageConfigFile) languages.get(Main.getInstance().getMainConfig().getLanguage()).get("localization"); } - /** - * Get singleton instance of LocalizationManager. - * - * @return LocalizationManager instance as singleton. - */ - public static LocalizationManager getInstance() { - if (instance == null) - instance = new LocalizationManager(); + public ShopLanguageConfigFile getActiveShopFile() { + return (ShopLanguageConfigFile) languages.get(Main.getInstance().getMainConfig().getLanguage()).get("shop"); + } - return instance; + @Contract(pure = true) + //TODO We need to get this automatically... Perhaps a json file we can update using crowdin or something + private @Unmodifiable List getSupportedLanguages() { + return List.of("en","he","pt-br"); } + + public String getPrefix() { + return ChatUtil.color(getActiveLanguageFile().getPrefix()); + } + + public String getShopReloaded() { + return getActiveLanguageFile().getShopReloaded(); + } + + + } diff --git a/src/main/java/me/tychsen/enchantgui/localization/LocalizedConfigFile.java b/src/main/java/me/tychsen/enchantgui/localization/LocalizedConfigFile.java new file mode 100644 index 0000000..ff82c6f --- /dev/null +++ b/src/main/java/me/tychsen/enchantgui/localization/LocalizedConfigFile.java @@ -0,0 +1,23 @@ +package me.tychsen.enchantgui.localization; + +import com.github.sarhatabaot.kraken.core.config.ConfigFile; +import me.tychsen.enchantgui.Main; + +import java.io.File; + +/** + * @author sarhatabaot + */ +public class LocalizedConfigFile extends ConfigFile
{ + public LocalizedConfigFile(final String lang, final String fileName) { + super(Main.getInstance(), "languages"+ File.separator+lang+File.separator, fileName, "languages"+File.separator+lang); + } + + public String getString(final String path) { + return getConfig().getString(path); + } + + public String getString(final String path, final String def) { + return getConfig().getString(path,def); + } +} diff --git a/src/main/java/me/tychsen/enchantgui/localization/ShopLanguageConfigFile.java b/src/main/java/me/tychsen/enchantgui/localization/ShopLanguageConfigFile.java new file mode 100644 index 0000000..db3afe1 --- /dev/null +++ b/src/main/java/me/tychsen/enchantgui/localization/ShopLanguageConfigFile.java @@ -0,0 +1,10 @@ +package me.tychsen.enchantgui.localization; + +/** + * @author sarhatabaot + */ +public class ShopLanguageConfigFile extends LocalizedConfigFile { + public ShopLanguageConfigFile(final String lang) { + super(lang, "shop.yml"); + } +} diff --git a/src/main/java/me/tychsen/enchantgui/menu/DefaultMenuGenerator.java b/src/main/java/me/tychsen/enchantgui/menu/DefaultMenuGenerator.java index 92ab592..126ea20 100644 --- a/src/main/java/me/tychsen/enchantgui/menu/DefaultMenuGenerator.java +++ b/src/main/java/me/tychsen/enchantgui/menu/DefaultMenuGenerator.java @@ -1,11 +1,10 @@ package me.tychsen.enchantgui.menu; +import com.github.sarhatabaot.kraken.core.chat.ChatUtil; import de.tr7zw.changeme.nbtapi.NBTItem; -import me.tychsen.enchantgui.ChatUtil; import me.tychsen.enchantgui.Main; import me.tychsen.enchantgui.NbtUtils; -import me.tychsen.enchantgui.config.EShopEnchants; -import me.tychsen.enchantgui.config.EShopShop; +import me.tychsen.enchantgui.config.Enchants; import me.tychsen.enchantgui.economy.NullPayment; import me.tychsen.enchantgui.permissions.EShopPermissionSys; import org.bukkit.Material; @@ -21,26 +20,27 @@ import java.util.List; import java.util.Map; -import static me.tychsen.enchantgui.config.EShopConfig.*; - public class DefaultMenuGenerator implements MenuGenerator { private final int inventorySize; - private EShopEnchants enchants; - private final EShopPermissionSys permSys; - public DefaultMenuGenerator(int inventorySize, EShopPermissionSys permSys) { + private Enchants enchants; + + public DefaultMenuGenerator(int inventorySize) { this.inventorySize = inventorySize; - this.enchants = new EShopEnchants(); - this.permSys = permSys; + this.enchants = new Enchants(); } public Inventory mainMenu(@NotNull Player p) { - Inventory inv = p.getServer().createInventory(p, inventorySize, getMenuName()); + Inventory inv = p.getServer().createInventory(p, inventorySize, Main.getInstance().getMainConfig().getMenuName()); generateMainMenu(p, inv); return inv; } + public void reload() { + this.enchants.reload(); + } + public Inventory enchantMenu(Player p, ItemStack item, Map playerLevels) { return generateEnchantMenu(p, item, playerLevels); } @@ -60,8 +60,8 @@ private void generateMainMenu(Player p, Inventory inv) { List itemList = new ArrayList<>(); for (ItemStack item : enchantList) { - if (permSys.hasEnchantPermission(p, item)) { - if (getShowPerItem()) { + if (EShopPermissionSys.hasEnchantPermission(p, item)) { + if (Main.getInstance().getMainConfig().getShowPerItem()) { itemList = showPerItem(itemList, item, p); } else { itemList.add(item); @@ -73,14 +73,14 @@ private void generateMainMenu(Player p, Inventory inv) { @NotNull private String format(int type, String name) { - String string = EShopShop.getInstance().getString("shop." + name); - return MessageFormat.format(ChatUtil.colorize(string), type); + String string = Main.getInstance().getLm().getActiveShopFile().getString("shop."+name); + return MessageFormat.format(ChatUtil.color(string), type); } @NotNull private String format(double type, String name) { - String string = EShopShop.getInstance().getString("shop." + name); - return MessageFormat.format(ChatUtil.colorize(string), type); + String string = Main.getInstance().getLm().getActiveShopFile().getString("shop."+name); + return MessageFormat.format(ChatUtil.color(string), type); } private @NotNull ItemStack generateItemWithMeta(@NotNull ItemStack item, int level, Enchantment enchantment) { @@ -89,8 +89,8 @@ private String format(double type, String name) { List lore = new ArrayList<>(); lore.add(format(level, "level")); - double price = getPrice(enchantment, level); - if (!(getPaymentStrategy() instanceof NullPayment)) { + double price = Main.getInstance().getMainConfig().getPrice(enchantment, level); + if (!(Main.getInstance().getMainConfig().getPaymentStrategy() instanceof NullPayment)) { lore.add(format(price, "price")); } @@ -98,26 +98,26 @@ private String format(double type, String name) { tempItem.setItemMeta(meta); NBTItem nbtItem = new NBTItem(tempItem); nbtItem.setInteger(NbtUtils.LEVEL,level); - if (!(getPaymentStrategy() instanceof NullPayment)) { + if (!(Main.getInstance().getMainConfig().getPaymentStrategy() instanceof NullPayment)) { nbtItem.setDouble(NbtUtils.PRICE,price); } return nbtItem.getItem(); } private @NotNull Inventory generateEnchantMenu(@NotNull Player player, @NotNull ItemStack item, Map playerLevels) { - Inventory inv = player.getServer().createInventory(player, inventorySize, getMenuName()); + Inventory inv = player.getServer().createInventory(player, inventorySize, Main.getInstance().getMainConfig().getMenuName()); Enchantment enchantment = item.getEnchantments().keySet().toArray(new Enchantment[1])[0]; List itemList = new ArrayList<>(); // Generate the correct items for the player. // Based on permissions or OP status. - String[] enchantLevels = getEnchantLevels(enchantment); + String[] enchantLevels = Main.getInstance().getMainConfig().getEnchantLevels(enchantment); List levels = new ArrayList<>(); for (String enchantLevel : enchantLevels) { enchantLevel = enchantLevel.substring(5); int level = Integer.parseInt(enchantLevel); - if (permSys.hasEnchantPermission(player, enchantment, level)) { + if (EShopPermissionSys.hasEnchantPermission(player, enchantment, level)) { //TODO: Upgrade option, pass the original item as an object and compare the enchantments. Make sure to account for negative price. itemList.add(generateItemWithMeta(item, level, enchantment)); @@ -137,14 +137,14 @@ private String format(double type, String name) { } private @NotNull ItemStack generateBackItem() { - Material material = Material.matchMaterial(EShopShop.getInstance().getString("shop.back-item.material", "EMERALD")); + Material material = Material.matchMaterial(Main.getInstance().getLm().getActiveShopFile().getString("shop.back-item.material", "EMERALD")); if(material == null) material = Material.EMERALD; ItemStack backItem = new ItemStack(material); ItemMeta meta = backItem.getItemMeta(); - meta.setDisplayName(EShopShop.getInstance().getString("shop.back-item.display-name","Go back")); + meta.setDisplayName(Main.getInstance().getLm().getActiveShopFile().getString("shop.back-item.display-name","Go back")); backItem.setItemMeta(meta); NBTItem nbtItem = new NBTItem(backItem); nbtItem.setBoolean(NbtUtils.BACK_BUTTON,true); @@ -152,12 +152,12 @@ private String format(double type, String name) { } @Override - public EShopEnchants getShopEnchants() { + public Enchants getShopEnchants() { return enchants; } @Override - public void setShopEnchants(final EShopEnchants enchants) { + public void setShopEnchants(final Enchants enchants) { this.enchants = enchants; } } diff --git a/src/main/java/me/tychsen/enchantgui/menu/DefaultMenuSystem.java b/src/main/java/me/tychsen/enchantgui/menu/DefaultMenuSystem.java index 5e19c72..d6e6776 100644 --- a/src/main/java/me/tychsen/enchantgui/menu/DefaultMenuSystem.java +++ b/src/main/java/me/tychsen/enchantgui/menu/DefaultMenuSystem.java @@ -1,11 +1,9 @@ package me.tychsen.enchantgui.menu; +import com.github.sarhatabaot.kraken.core.chat.ChatUtil; import de.tr7zw.changeme.nbtapi.NBTItem; -import me.tychsen.enchantgui.ChatUtil; import me.tychsen.enchantgui.Main; import me.tychsen.enchantgui.NbtUtils; -import me.tychsen.enchantgui.config.EShopConfig; -import me.tychsen.enchantgui.config.EShopShop; import me.tychsen.enchantgui.economy.PaymentStrategy; import me.tychsen.enchantgui.localization.LocalizationManager; import me.tychsen.enchantgui.permissions.EShopPermissionSys; @@ -20,36 +18,34 @@ import java.util.HashMap; import java.util.Map; -import static me.tychsen.enchantgui.config.EShopConfig.getIgnoreItemType; -import static me.tychsen.enchantgui.config.EShopConfig.getPrice; public class DefaultMenuSystem implements MenuSystem { - public static String PREFIX; + private final String prefix; private final Map playerLevels; - private final EShopPermissionSys permsys; - private final MenuGenerator generator; public DefaultMenuSystem() { playerLevels = new HashMap<>(); - permsys = new EShopPermissionSys(); - new EShopConfig(); - generator = new DefaultMenuGenerator(36, permsys); - DefaultMenuSystem.PREFIX = LocalizationManager.getInstance().getString("prefix") + " "; + generator = new DefaultMenuGenerator(36); + prefix = Main.getInstance().getLm().getPrefix() + " "; + } + + public void reload() { + generator.reload(); } private void tell(Player player, String message) { - ChatUtil.tell(player, PREFIX + message); + ChatUtil.sendMessage(player, prefix + message); } @Override public void showMainMenu(@NotNull Player player) { - LocalizationManager lm = LocalizationManager.getInstance(); + LocalizationManager lm = Main.getInstance().getLm(); playerLevels.remove(player.getName()); - if (!permsys.hasUsePermission(player)) { - tell(player, lm.getString("no-permission")); + if (!EShopPermissionSys.hasUsePermission(player)) { + tell(player, lm.getLanguageString("no-permission")); return; } @@ -81,35 +77,35 @@ public MenuGenerator getMenuGenerator() { } private void purchaseEnchant(@NotNull Player player, @NotNull InventoryClickEvent event) { - LocalizationManager lm = LocalizationManager.getInstance(); + LocalizationManager lm = Main.getInstance().getLm(); ItemStack item = event.getCurrentItem(); Enchantment enchantment = item.getEnchantments().keySet().toArray(new Enchantment[1])[0]; ItemStack playerHand = player.getInventory().getItemInMainHand(); int level = Integer.parseInt(playerLevels.get(player.getName())[event.getSlot()]); - int price = (int) getPrice(enchantment, level); + int price = (int) Main.getInstance().getMainConfig().getPrice(enchantment, level); Main.debug("Slot: " + event.getSlot() + " Level: " + level); if (playerHand.getType() == Material.AIR) { - tell(player, lm.getString("cant-enchant")); + tell(player, lm.getLanguageString("cant-enchant")); player.closeInventory(); return; } - if (enchantment.canEnchantItem(playerHand) || getIgnoreItemType()) { - PaymentStrategy payment = EShopConfig.getPaymentStrategy(); + if (enchantment.canEnchantItem(playerHand) || Main.getInstance().getMainConfig().getIgnoreItemType()) { + PaymentStrategy payment = Main.getInstance().getMainConfig().getPaymentStrategy(); if (payment.withdraw(player, price)) { enchantItem(playerHand, enchantment, level); - String message = String.format("%s &d%s %d &ffor &6%d%s", lm.getString("item-enchanted"), item.getItemMeta().getDisplayName(), level, price, EShopConfig.getEconomyCurrency()); + String message = String.format("%s &d%s %d &ffor &6%d%s", lm.getLanguageString("item-enchanted"), item.getItemMeta().getDisplayName(), level, price, Main.getInstance().getMainConfig().getEconomyCurrency()); tell(player, message); player.closeInventory(); } else { - tell(player, lm.getString("insufficient-funds")); + tell(player, lm.getLanguageString("insufficient-funds")); } } else { - tell(player, lm.getString("item-cant-be-enchanted")); + tell(player, lm.getLanguageString("item-cant-be-enchanted")); } } diff --git a/src/main/java/me/tychsen/enchantgui/menu/MenuGenerator.java b/src/main/java/me/tychsen/enchantgui/menu/MenuGenerator.java index 0ed805f..77d1fda 100644 --- a/src/main/java/me/tychsen/enchantgui/menu/MenuGenerator.java +++ b/src/main/java/me/tychsen/enchantgui/menu/MenuGenerator.java @@ -1,6 +1,6 @@ package me.tychsen.enchantgui.menu; -import me.tychsen.enchantgui.config.EShopEnchants; +import me.tychsen.enchantgui.config.Enchants; import org.bukkit.entity.Player; import org.bukkit.inventory.Inventory; import org.bukkit.inventory.ItemStack; @@ -10,6 +10,7 @@ public interface MenuGenerator { Inventory mainMenu(Player p); Inventory enchantMenu(Player p, ItemStack item, Map playerLevels); - EShopEnchants getShopEnchants(); - void setShopEnchants(EShopEnchants enchants); + Enchants getShopEnchants(); + void setShopEnchants(Enchants enchants); + void reload(); } diff --git a/src/main/java/me/tychsen/enchantgui/menu/MenuSystem.java b/src/main/java/me/tychsen/enchantgui/menu/MenuSystem.java index 1cff4cc..c16f416 100644 --- a/src/main/java/me/tychsen/enchantgui/menu/MenuSystem.java +++ b/src/main/java/me/tychsen/enchantgui/menu/MenuSystem.java @@ -10,4 +10,5 @@ public interface MenuSystem { void showMainMenu(Player p); void handleMenuClick(Player p, InventoryClickEvent event); MenuGenerator getMenuGenerator(); + void reload(); } diff --git a/src/main/java/me/tychsen/enchantgui/permissions/EShopPermissionSys.java b/src/main/java/me/tychsen/enchantgui/permissions/EShopPermissionSys.java index f0becb8..df6a566 100644 --- a/src/main/java/me/tychsen/enchantgui/permissions/EShopPermissionSys.java +++ b/src/main/java/me/tychsen/enchantgui/permissions/EShopPermissionSys.java @@ -4,6 +4,7 @@ import org.bukkit.enchantments.Enchantment; import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; +import org.jetbrains.annotations.Contract; import org.jetbrains.annotations.NotNull; import java.io.Serial; @@ -11,41 +12,48 @@ public class EShopPermissionSys { public static final String USE = "eshop.use"; + public static final String ENCHANTING_TABLE = "eshop.enchantingtable"; public static final String TOGGLE = "eshop.enchantingtable.toggle"; public static final String RELOAD = "eshop.reload"; private static final String BASE = "eshop.enchants."; - public boolean hasEnchantPermission(final @NotNull Player player, final ItemStack item) { + + public static boolean hasEnchantPermission(final @NotNull Player player, final ItemStack item) { if (player.isOp()) return true; Map enchants = item.getEnchantments(); - if (enchants.size() > 1) throw new TooManyEnchantmentsException("Item has more than one enchant!"); - - Enchantment ench = enchants.keySet().toArray(new Enchantment[1])[0]; - String name = (ench.getKey().toString().toLowerCase()).split(":")[1]; - String perm = BASE + name; - + if (enchants.size() > 1) { + throw new TooManyEnchantmentsException("Item has more than one enchant!"); + //This should never happen. + } + + Enchantment enchantment = enchants.keySet().toArray(new Enchantment[1])[0]; + String name = (enchantment.getKey().toString().toLowerCase()).split(":")[1]; + String perm = getEnchantmentPermission(name); return player.hasPermission(perm) || player.hasPermission(BASE + "all"); } - public boolean hasEnchantPermission(final @NotNull Player player, final Enchantment enchantment, int level) { + public static boolean hasEnchantPermission(final @NotNull Player player, final Enchantment enchantment, int level) { if (player.isOp()) return true; String enchantmentName = (enchantment.getKey().toString().toLowerCase()).split(":")[1]; - String perm = BASE + enchantmentName + "." + level; + String perm = getEnchantmentLevelPermission(enchantmentName,level); return player.hasPermission(perm) || player.hasPermission(BASE + enchantmentName + ".all") || player.hasPermission(BASE + "all"); } - public boolean hasUsePermission(@NotNull Player player) { + public static boolean hasUsePermission(@NotNull Player player) { if (player.isOp()) return true; - return player.hasPermission(EShopPermissionSys.USE); + return player.hasPermission(EShopPermissionSys.USE); } - @RequiredArgsConstructor - private static final class TooManyEnchantmentsException extends RuntimeException { - @Serial - private static final long serialVersionUID = 1L; + @Contract(pure = true) + private static @NotNull String getEnchantmentPermission(final String name) { + return BASE + name; + } - private final String tellMessage; + @Contract(pure = true) + private static @NotNull String getEnchantmentLevelPermission(final String name, int level) { + return getEnchantmentPermission(name) + "."+level; } + } diff --git a/src/main/java/me/tychsen/enchantgui/permissions/TooManyEnchantmentsException.java b/src/main/java/me/tychsen/enchantgui/permissions/TooManyEnchantmentsException.java new file mode 100644 index 0000000..45741f1 --- /dev/null +++ b/src/main/java/me/tychsen/enchantgui/permissions/TooManyEnchantmentsException.java @@ -0,0 +1,15 @@ +package me.tychsen.enchantgui.permissions; + +import java.io.Serial; + +/** + * @author sarhatabaot + */ +public class TooManyEnchantmentsException extends RuntimeException{ + @Serial + private static final long serialVersionUID = 1L; + + public TooManyEnchantmentsException(final String message) { + super(message); + } +} diff --git a/src/main/resources/languages/en/localization.yml b/src/main/resources/languages/en/localization.yml index 7682de7..b99b5bb 100644 --- a/src/main/resources/languages/en/localization.yml +++ b/src/main/resources/languages/en/localization.yml @@ -43,6 +43,7 @@ no-permission: "&cSorry, you do not have permission for this." config-reloaded: "Configuration file has been reloaded!" localization-reloaded: "Localization file has been reloaded!" shop-reloaded: "Shop file has been reloaded!" +localized-file-reloaded: "The %s files have been reloaded!" cant-enchant: "You can't enchant that!" item-enchanted: "Your item was enchanted with" insufficient-funds: "Insufficient funds."