From f224810f453bae72d1000d6f4dc10f542923cf2e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20F?= Date: Mon, 20 Jun 2022 18:25:08 +0100 Subject: [PATCH] Refractoring code --- .../java/com/tomff/beesplus/BeesPlus.java | 18 +++-- .../tomff/beesplus/core/UpdateChecker.java | 3 +- .../core/items/CustomItemManager.java | 1 + .../java/com/tomff/beesplus/gui/BeeInfo.java | 78 ++++++++++--------- ...onWrapper.java => LocalizationLoader.java} | 6 +- 5 files changed, 58 insertions(+), 48 deletions(-) rename src/main/java/com/tomff/beesplus/localization/{LocalizationWrapper.java => LocalizationLoader.java} (79%) diff --git a/src/main/java/com/tomff/beesplus/BeesPlus.java b/src/main/java/com/tomff/beesplus/BeesPlus.java index b94482a..009b24c 100644 --- a/src/main/java/com/tomff/beesplus/BeesPlus.java +++ b/src/main/java/com/tomff/beesplus/BeesPlus.java @@ -12,7 +12,7 @@ import com.tomff.beesplus.handlers.DamageHandler; import com.tomff.beesplus.handlers.RightClickHandler; import com.tomff.beesplus.items.*; import com.tomff.beesplus.localization.Localization; -import com.tomff.beesplus.localization.LocalizationWrapper; +import com.tomff.beesplus.localization.LocalizationLoader; import org.bstats.bukkit.Metrics; import org.bukkit.configuration.InvalidConfigurationException; import org.bukkit.configuration.file.YamlConfiguration; @@ -23,6 +23,9 @@ import java.util.Locale; public class BeesPlus extends JavaPlugin { + private static final int RESOURCE_ID = 77224; + private static final int PLUGIN_ID = 7065; + private GuiViewTracker guiViewTracker; private CustomItemManager customItemManager; @@ -45,8 +48,11 @@ public class BeesPlus extends JavaPlugin { registerItems(); setupMetrics(); + setupUpdateChecker(); + } - new UpdateChecker(this, 77224).getVersion(version -> { + private void setupUpdateChecker() { + new UpdateChecker(this, RESOURCE_ID).getVersion(version -> { if (!this.getDescription().getVersion().equalsIgnoreCase(version)) { getLogger().info("A new update is available: BeesPlus " + version); } @@ -54,7 +60,7 @@ public class BeesPlus extends JavaPlugin { } private void setupMetrics() { - Metrics metrics = new Metrics(this, 7065); + Metrics metrics = new Metrics(this, PLUGIN_ID); metrics.addCustomChart(new Metrics.SimplePie("language_used", () -> getConfig().getString("locale", Locale.ENGLISH.toLanguageTag()))); @@ -67,10 +73,10 @@ public class BeesPlus extends JavaPlugin { private boolean loadLocale() { String locale = getConfig().getString("locale", Locale.ENGLISH.toLanguageTag()); - LocalizationWrapper localizationWrapper = new LocalizationWrapper(this, "locale"); + LocalizationLoader localizationLoader = new LocalizationLoader(this, "locale"); try { - YamlConfiguration localeYamlFile = localizationWrapper.getLocale(locale); + YamlConfiguration localeYamlFile = localizationLoader.load(locale); Localization.load(localeYamlFile); } catch (IOException e) { getLogger().severe("Invalid locale! Please choose a valid locale."); @@ -133,7 +139,7 @@ public class BeesPlus extends JavaPlugin { boolean isProtectionSuitEnabled = getConfig().getBoolean("beeprotectionsuit.enabled", true); - if(isProtectionSuitEnabled) { + if (isProtectionSuitEnabled) { customItemManager.registerCustomItem("protection_boots", new BeeProtectionBoots()); customItemManager.registerCustomItem("protection_leggings", new BeeProtectionLeggings()); customItemManager.registerCustomItem("protection_chestplate", new BeeProtectionChestplate()); diff --git a/src/main/java/com/tomff/beesplus/core/UpdateChecker.java b/src/main/java/com/tomff/beesplus/core/UpdateChecker.java index 84ec7cc..0c918f4 100644 --- a/src/main/java/com/tomff/beesplus/core/UpdateChecker.java +++ b/src/main/java/com/tomff/beesplus/core/UpdateChecker.java @@ -21,7 +21,8 @@ public class UpdateChecker { public void getVersion(final Consumer consumer) { Bukkit.getScheduler().runTaskAsynchronously(beesPlus, () -> { - try (InputStream inputStream = new URL("https://api.spigotmc.org/legacy/update.php?resource=" + this.resourceId).openStream(); Scanner scanner = new Scanner(inputStream)) { + try (InputStream inputStream = new URL("https://api.spigotmc.org/legacy/update.php?resource=" + this.resourceId).openStream(); + Scanner scanner = new Scanner(inputStream)) { if (scanner.hasNext()) { consumer.accept(scanner.next()); } diff --git a/src/main/java/com/tomff/beesplus/core/items/CustomItemManager.java b/src/main/java/com/tomff/beesplus/core/items/CustomItemManager.java index d1a8bec..1638d98 100644 --- a/src/main/java/com/tomff/beesplus/core/items/CustomItemManager.java +++ b/src/main/java/com/tomff/beesplus/core/items/CustomItemManager.java @@ -27,6 +27,7 @@ public class CustomItemManager { NamespacedKey namespacedKey = new NamespacedKey(beesPlus, id); ShapedRecipe recipe = new ShapedRecipe(namespacedKey, customItem.getResult()); + recipe.shape(customItem.getRecipe()); customItem.getIngredients().forEach(recipe::setIngredient); diff --git a/src/main/java/com/tomff/beesplus/gui/BeeInfo.java b/src/main/java/com/tomff/beesplus/gui/BeeInfo.java index 3d768b9..3079773 100644 --- a/src/main/java/com/tomff/beesplus/gui/BeeInfo.java +++ b/src/main/java/com/tomff/beesplus/gui/BeeInfo.java @@ -6,6 +6,7 @@ import com.tomff.beesplus.core.items.ItemBuilder; import com.tomff.beesplus.localization.Localization; import org.bukkit.*; import org.bukkit.entity.Bee; +import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; public class BeeInfo extends Gui { @@ -26,6 +27,44 @@ public class BeeInfo extends Gui { return Localization.get(Localization.BEE_INFO_GUI_TITLE); } + public void rideBee(Player player) { + double distance = player.getLocation().distance(bee.getLocation()); + + if (!player.hasPermission("beesplus.bee.ride")) { + player.playSound(player.getLocation(), Sound.BLOCK_NOTE_BLOCK_PLING, 2, 2); + Localization.sendMessage(player, Localization.BEE_INFO_GUI_RIDE_NO_PERMISSION); + + return; + } + + if (bee.getAnger() > 0) { + player.playSound(player.getLocation(), Sound.BLOCK_NOTE_BLOCK_PLING, 2, 2); + Localization.sendMessage(player, Localization.BEE_INFO_GUI_RIDE_ANGRY); + + return; + } + + if (distance <= 6) { + if (bee.getPassengers().size() >= 1) { + Localization.sendMessage(player, Localization.BEE_INFO_GUI_RIDE_ALREADY); + return; + } + + player.closeInventory(); + + bee.addPassenger(player); + + String title = Localization.get(Localization.RIDE_BEE_TITLE, player.getName()); + String subtitle = Localization.get(Localization.RIDE_BEE_SUBTITLE, player.getName()); + + player.playSound(player.getLocation(), Sound.ENTITY_ENDER_DRAGON_FLAP, 10, 1); + player.sendTitle(title, subtitle, 10, 25, 10); + } else { + player.playSound(player.getLocation(), Sound.BLOCK_NOTE_BLOCK_PLING, 2, 2); + Localization.sendMessage(player, Localization.BEE_INFO_GUI_RIDE_TOO_FAR); + } + } + @Override public void buildIcons() { ItemStack age = new ItemBuilder(Material.OAK_SIGN) @@ -64,44 +103,7 @@ public class BeeInfo extends Gui { .setName(Localization.get(Localization.BEE_INFO_GUI_RIDE)) .build(); - Icon mountIcon = new Icon(mount, (player) -> { - double distance = player.getLocation().distance(bee.getLocation()); - - if (!player.hasPermission("beesplus.bee.ride")) { - player.playSound(player.getLocation(), Sound.BLOCK_NOTE_BLOCK_PLING, 2, 2); - Localization.sendMessage(player, Localization.BEE_INFO_GUI_RIDE_NO_PERMISSION); - - return; - } - - if (bee.getAnger() > 0) { - player.playSound(player.getLocation(), Sound.BLOCK_NOTE_BLOCK_PLING, 2, 2); - Localization.sendMessage(player, Localization.BEE_INFO_GUI_RIDE_ANGRY); - - return; - } - - if (distance <= 6) { - - if (bee.getPassengers().size() >= 1) { - Localization.sendMessage(player, Localization.BEE_INFO_GUI_RIDE_ALREADY); - return; - } - - player.closeInventory(); - - bee.addPassenger(player); - - String title = Localization.get(Localization.RIDE_BEE_TITLE, player.getName()); - String subtitle = Localization.get(Localization.RIDE_BEE_SUBTITLE, player.getName()); - - player.playSound(player.getLocation(), Sound.ENTITY_ENDER_DRAGON_FLAP, 10, 1); - player.sendTitle(title, subtitle, 10, 25, 10); - } else { - player.playSound(player.getLocation(), Sound.BLOCK_NOTE_BLOCK_PLING, 2, 2); - Localization.sendMessage(player, Localization.BEE_INFO_GUI_RIDE_TOO_FAR); - } - }); + Icon mountIcon = new Icon(mount, this::rideBee); setIcon(mountIcon, 15); ItemStack stung = new ItemBuilder(Material.IRON_SWORD) diff --git a/src/main/java/com/tomff/beesplus/localization/LocalizationWrapper.java b/src/main/java/com/tomff/beesplus/localization/LocalizationLoader.java similarity index 79% rename from src/main/java/com/tomff/beesplus/localization/LocalizationWrapper.java rename to src/main/java/com/tomff/beesplus/localization/LocalizationLoader.java index 55dd4bb..9ba1f3e 100644 --- a/src/main/java/com/tomff/beesplus/localization/LocalizationWrapper.java +++ b/src/main/java/com/tomff/beesplus/localization/LocalizationLoader.java @@ -8,17 +8,17 @@ import java.io.File; import java.io.IOException; import java.nio.file.Paths; -public class LocalizationWrapper { +public class LocalizationLoader { private final BeesPlus beesPlus; private final String basePath; - public LocalizationWrapper(BeesPlus beesPlus, String path) { + public LocalizationLoader(BeesPlus beesPlus, String path) { this.beesPlus = beesPlus; this.basePath = path; } - public YamlConfiguration getLocale(String locale) throws IOException, InvalidConfigurationException { + public YamlConfiguration load(String locale) throws IOException, InvalidConfigurationException { String path = Paths.get(basePath, locale + ".yml").toString(); File localeFile = new File(beesPlus.getDataFolder(), path);