From ca190ffbdd1f1350ed8ed4d0389d4f7a7b585a99 Mon Sep 17 00:00:00 2001 From: YouHaveTrouble Date: Fri, 6 Dec 2024 18:40:53 +0100 Subject: [PATCH] locale files, working --- pom.xml | 5 +++ .../me/youhavetrouble/blockedit/BELocale.java | 18 ++++++-- .../youhavetrouble/blockedit/BlockEdit.java | 42 +++++++++---------- .../blockedit/BlockEditCommands.java | 35 +++++++++------- .../blockedit/wands/SelectionWand.java | 16 +++++-- src/main/resources/locale/en_US.json | 12 ++++++ src/main/resources/locale/en_us.json | 3 -- 7 files changed, 84 insertions(+), 47 deletions(-) create mode 100644 src/main/resources/locale/en_US.json delete mode 100644 src/main/resources/locale/en_us.json diff --git a/pom.xml b/pom.xml index fbdfbe5..1d624a1 100644 --- a/pom.xml +++ b/pom.xml @@ -86,5 +86,10 @@ 6.1 compile + + org.reflections + reflections + 0.10.2 + diff --git a/src/main/java/me/youhavetrouble/blockedit/BELocale.java b/src/main/java/me/youhavetrouble/blockedit/BELocale.java index b58ebac..a4bfe75 100644 --- a/src/main/java/me/youhavetrouble/blockedit/BELocale.java +++ b/src/main/java/me/youhavetrouble/blockedit/BELocale.java @@ -10,18 +10,28 @@ import java.util.Map; public class BELocale { private static final Map locales = new HashMap<>(); - private static final Locale defaultLocale = Locale.ENGLISH; + private static final Locale defaultLocale = Locale.of("en_US"); - public final String COULD_NOT_FIND_WAND_BY_ID; + public final String couldNotFindWandById, selectArea, copiedSelectionToClipboard, selectionReset, firstPositionSet, + secondPositionSet, pastingClipboard, clipboardRotated, settingBlocks, replacingBlocks; protected BELocale(JsonObject json) { - COULD_NOT_FIND_WAND_BY_ID = json.get("could_not_find_wand_by_id").getAsString(); + BlockEdit.getPlugin().getSLF4JLogger().info(json.toString()); + couldNotFindWandById = json.get("could_not_find_wand_by_id").getAsString(); + selectArea = json.get("select_area").getAsString(); + copiedSelectionToClipboard = json.get("copied_selection_to_clipboard").getAsString(); + selectionReset = json.get("selection_reset").getAsString(); + firstPositionSet = json.get("first_position_set").getAsString(); + secondPositionSet = json.get("second_position_set").getAsString(); + pastingClipboard = json.get("pasting_clipboard").getAsString(); + clipboardRotated = json.get("clipboard_rotated").getAsString(); + settingBlocks = json.get("setting_blocks").getAsString(); + replacingBlocks = json.get("replacing_blocks").getAsString(); } protected static void registerLocale(Locale locale, BELocale blockEditLocale) { locales.put(locale, blockEditLocale); - System.out.println("Registered locale " + locale.getISO3Country() + " " + locale.getISO3Language()); } public static BELocale getLocale(@NotNull Locale locale) { diff --git a/src/main/java/me/youhavetrouble/blockedit/BlockEdit.java b/src/main/java/me/youhavetrouble/blockedit/BlockEdit.java index 88fe135..1d7656a 100644 --- a/src/main/java/me/youhavetrouble/blockedit/BlockEdit.java +++ b/src/main/java/me/youhavetrouble/blockedit/BlockEdit.java @@ -5,10 +5,14 @@ import com.google.gson.JsonObject; import com.google.gson.stream.JsonReader; import me.youhavetrouble.blockedit.wands.SelectionWand; import org.bukkit.plugin.java.JavaPlugin; +import org.reflections.Reflections; +import org.reflections.scanners.Scanners; import java.io.*; -import java.util.List; -import java.util.Locale; +import java.net.URL; +import java.util.*; +import java.util.regex.Pattern; +import java.util.stream.Collectors; public final class BlockEdit extends JavaPlugin { @@ -51,37 +55,33 @@ public final class BlockEdit extends JavaPlugin { } private boolean initLocales() { - List localeFiles; - try (InputStream in = BlockEdit.class.getClassLoader().getResourceAsStream("locale"); - BufferedReader br = new BufferedReader(new InputStreamReader(in))) { - localeFiles = br.lines().toList(); - } catch (IOException e) { - plugin.getSLF4JLogger().error("Error loading locale files", e); - return false; - } + Reflections reflections = new Reflections("locale", Scanners.Resources); + Set fileNames = reflections.getResources(Pattern.compile("([a-zA-Z]{1,3}_[a-zA-Z]{1,3})(\\.json)")); Gson gson = new Gson(); - for (String fileName : localeFiles) { + for (String fileName : fileNames) { Locale locale; try { - String localeString = fileName.replace(".json", ""); - String[] split = localeString.split("_"); - if (split.length == 1) { - locale = Locale.of(split[0]); - } else { - locale = Locale.of(split[0], split[1]); - } + String localeString = fileName + .replace(".json", "") + .replace("locale/", ""); + locale = Locale.of(localeString); } catch (IllegalArgumentException e) { plugin.getSLF4JLogger().error("Invalid locale file name: {}", fileName); continue; } - try (InputStream fileStream = BlockEdit.class.getClassLoader().getResourceAsStream("locale/" + fileName); - JsonReader reader = new JsonReader(new InputStreamReader(fileStream))) { + String resourcePath = "/" + fileName; + try (InputStream fileStream = BlockEdit.class.getResourceAsStream(resourcePath)) { + if (fileStream == null) { + plugin.getSLF4JLogger().error("Locale file not found: {}", resourcePath); + continue; + } + JsonReader reader = new JsonReader(new InputStreamReader(fileStream)); JsonObject json = gson.fromJson(reader, JsonObject.class); BELocale beLocale = new BELocale(json); BELocale.registerLocale(locale, beLocale); } catch (IOException e) { - plugin.getSLF4JLogger().error("Error reading locale file: {}", fileName, e); + plugin.getSLF4JLogger().error("Error reading locale file: {}", resourcePath, e); } } return true; diff --git a/src/main/java/me/youhavetrouble/blockedit/BlockEditCommands.java b/src/main/java/me/youhavetrouble/blockedit/BlockEditCommands.java index 5582fad..ec51835 100644 --- a/src/main/java/me/youhavetrouble/blockedit/BlockEditCommands.java +++ b/src/main/java/me/youhavetrouble/blockedit/BlockEditCommands.java @@ -17,6 +17,7 @@ import me.youhavetrouble.blockedit.operations.SetOperation; import me.youhavetrouble.blockedit.util.Selection; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.format.NamedTextColor; +import org.bukkit.Location; import org.bukkit.block.BlockState; import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; @@ -80,7 +81,6 @@ public class BlockEditCommands { ); }); - } private static LiteralCommandNode wandCommand() { @@ -106,7 +106,8 @@ public class BlockEditCommands { String wandId = ctx.getArgument("wand_id", String.class); ItemStack wand = BlockEditAPI.getWandsHandler().getWand(wandId); if (wand == null) { - ctx.getSource().getSender().sendMessage(Component.text("Could not find wand with id %s".formatted(wandId), NamedTextColor.RED)); + BlockEdit.getPlugin().getSLF4JLogger().info(String.valueOf(player.locale())); + ctx.getSource().getSender().sendMessage(Component.text(BELocale.getLocale(player.locale()).couldNotFindWandById.formatted(wandId), NamedTextColor.RED)); return Command.SINGLE_SUCCESS; } player.getInventory().addItem(wand); @@ -133,9 +134,9 @@ public class BlockEditCommands { BEPlayer bePlayer = BEPlayer.getByPlayer(player); try { bePlayer.setClipboardFromSelection(); - player.sendMessage(Component.text("Copied selection to clipboard")); + player.sendMessage(Component.text(BELocale.getLocale(player.locale()).copiedSelectionToClipboard, NamedTextColor.GRAY)); } catch (IllegalStateException e) { - player.sendMessage(Component.text("You need to select an area first")); + player.sendMessage(Component.text(BELocale.getLocale(player.locale()).selectArea, NamedTextColor.RED)); } return Command.SINGLE_SUCCESS; }) @@ -151,7 +152,7 @@ public class BlockEditCommands { .executes(ctx -> { Player player = (Player) ctx.getSource().getSender(); BEPlayer.getByPlayer(player).resetSelection(); - player.sendMessage(Component.text("You have reset your selection")); + player.sendMessage(Component.text(BELocale.getLocale(player.locale()).selectionReset, NamedTextColor.GRAY)); return Command.SINGLE_SUCCESS; }) .build(); @@ -165,8 +166,10 @@ public class BlockEditCommands { }) .executes(ctx -> { Player player = (Player) ctx.getSource().getSender(); - BEPlayer.getByPlayer(player).setSelectionPoint1(player.getLocation().toBlockLocation()); - player.sendMessage(Component.text("First point set at your location")); + Location location = player.getLocation().toBlockLocation(); + BEPlayer.getByPlayer(player).setSelectionPoint1(location); + String locationString = "X: " + location.blockX() + " Y: " + location.blockY() + " Z: " + location.blockZ(); + player.sendMessage(Component.text(BELocale.getLocale(player.locale()).firstPositionSet.formatted(locationString), NamedTextColor.GRAY)); return Command.SINGLE_SUCCESS; }) .build(); @@ -180,8 +183,10 @@ public class BlockEditCommands { }) .executes(ctx -> { Player player = (Player) ctx.getSource().getSender(); - BEPlayer.getByPlayer(player).setSelectionPoint2(player.getLocation().toBlockLocation()); - player.sendMessage(Component.text("Second point set at your location")); + Location location = player.getLocation().toBlockLocation(); + BEPlayer.getByPlayer(player).setSelectionPoint2(location); + String locationString = "X: " + location.blockX() + " Y: " + location.blockY() + " Z: " + location.blockZ(); + player.sendMessage(Component.text(BELocale.getLocale(player.locale()).secondPositionSet.formatted(locationString), NamedTextColor.GRAY)); return Command.SINGLE_SUCCESS; }) .build(); @@ -207,7 +212,7 @@ public class BlockEditCommands { Selection selection = Selection.fromClipboard(absoluteBlocks.keySet(), player.getWorld()); BlockEditAPI.runOperation(selection, 1, new PasteOperation(absoluteBlocks)); - player.sendMessage(Component.text("Pasting clipboard...")); + player.sendMessage(Component.text(BELocale.getLocale(player.locale()).pastingClipboard, NamedTextColor.GRAY)); return Command.SINGLE_SUCCESS; }) @@ -227,7 +232,7 @@ public class BlockEditCommands { double angle = ctx.getArgument("angle", Double.class); BEPlayer bePlayer = BEPlayer.getByPlayer(player); bePlayer.getClipboard().rotate(angle); - player.sendMessage(Component.text("Rotated clipboard by " + angle + " degrees")); + player.sendMessage(Component.text(BELocale.getLocale(player.locale()).clipboardRotated.formatted(angle), NamedTextColor.GRAY)); return Command.SINGLE_SUCCESS; }) ) @@ -248,7 +253,7 @@ public class BlockEditCommands { BlockState blockState = ctx.getArgument("block", BlockState.class); Selection selection = bePlayer.getSelection(); BlockEditAPI.runOperation(selection, 1, new SetOperation(blockState)); - player.sendMessage(Component.text("Setting blocks...")); + player.sendMessage(Component.text(BELocale.getLocale(player.locale()).settingBlocks, NamedTextColor.GRAY)); return Command.SINGLE_SUCCESS; }) ) @@ -261,7 +266,7 @@ public class BlockEditCommands { int chunksPerTick = ctx.getArgument("chunks_per_tick", Integer.class); Selection selection = bePlayer.getSelection(); BlockEditAPI.runOperation(selection, chunksPerTick, new SetOperation(blockState)); - player.sendMessage(Component.text("Setting blocks...")); + player.sendMessage(Component.text(BELocale.getLocale(player.locale()).settingBlocks, NamedTextColor.GRAY)); return Command.SINGLE_SUCCESS; })) .build(); @@ -285,7 +290,7 @@ public class BlockEditCommands { BlockState replaceWith = ctx.getArgument("replace_with", BlockState.class); Selection selection = bePlayer.getSelection(); BlockEditAPI.runOperation(selection, 1, new ReplaceOperation(toReplace, replaceWith)); - player.sendMessage(Component.text("Replacing blocks...")); + player.sendMessage(Component.text(BELocale.getLocale(player.locale()).replacingBlocks, NamedTextColor.GRAY)); return Command.SINGLE_SUCCESS; }) ) @@ -299,7 +304,7 @@ public class BlockEditCommands { int chunksPerTick = ctx.getArgument("chunks_per_tick", Integer.class); Selection selection = bePlayer.getSelection(); BlockEditAPI.runOperation(selection, chunksPerTick, new ReplaceOperation(toReplace, replaceWith)); - player.sendMessage(Component.text("Replacing blocks...")); + player.sendMessage(Component.text(BELocale.getLocale(player.locale()).replacingBlocks, NamedTextColor.GRAY)); return Command.SINGLE_SUCCESS; }) ) diff --git a/src/main/java/me/youhavetrouble/blockedit/wands/SelectionWand.java b/src/main/java/me/youhavetrouble/blockedit/wands/SelectionWand.java index 3d253e6..9d70a99 100644 --- a/src/main/java/me/youhavetrouble/blockedit/wands/SelectionWand.java +++ b/src/main/java/me/youhavetrouble/blockedit/wands/SelectionWand.java @@ -1,10 +1,13 @@ package me.youhavetrouble.blockedit.wands; +import me.youhavetrouble.blockedit.BELocale; import me.youhavetrouble.blockedit.BEPlayer; import me.youhavetrouble.blockedit.api.BlockEditAPI; import me.youhavetrouble.blockedit.api.BlockEditWand; import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.format.NamedTextColor; import net.kyori.adventure.text.format.TextDecoration; +import org.bukkit.Location; import org.bukkit.block.Block; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; @@ -13,6 +16,7 @@ import org.bukkit.event.Listener; import org.bukkit.event.block.Action; import org.bukkit.event.player.PlayerInteractEvent; +@SuppressWarnings("UnstableApiUsage") public class SelectionWand implements Listener, BlockEditWand { @Override @@ -47,14 +51,18 @@ public class SelectionWand implements Listener, BlockEditWand { Action action = event.getAction(); if (action.equals(Action.LEFT_CLICK_BLOCK)) { event.setCancelled(true); - BEPlayer.getByPlayer(player).setSelectionPoint1(block.getLocation()); - player.sendMessage(Component.text("First point set")); + Location location = block.getLocation(); + BEPlayer.getByPlayer(player).setSelectionPoint1(location); + String locationString = "X: " + location.blockX() + " Y: " + location.blockY() + " Z: " + location.blockZ(); + player.sendMessage(Component.text(BELocale.getLocale(player.locale()).firstPositionSet.formatted(locationString), NamedTextColor.GRAY)); return; } if (action.equals(Action.RIGHT_CLICK_BLOCK)) { event.setCancelled(true); - BEPlayer.getByPlayer(player).setSelectionPoint2(block.getLocation()); - player.sendMessage(Component.text("Second point set")); + Location location = block.getLocation(); + BEPlayer.getByPlayer(player).setSelectionPoint2(location); + String locationString = "X: " + location.blockX() + " Y: " + location.blockY() + " Z: " + location.blockZ(); + player.sendMessage(Component.text(BELocale.getLocale(player.locale()).secondPositionSet.formatted(locationString), NamedTextColor.GRAY)); return; } } diff --git a/src/main/resources/locale/en_US.json b/src/main/resources/locale/en_US.json new file mode 100644 index 0000000..24abaf2 --- /dev/null +++ b/src/main/resources/locale/en_US.json @@ -0,0 +1,12 @@ +{ + "could_not_find_wand_by_id": "Could not find wand with id %s", + "select_area": "You need to select an area first", + "copied_selection_to_clipboard": "Copied selection to clipboard", + "selection_reset": "You have reset your selection", + "first_position_set": "First position set to %s", + "second_position_set": "Second position set to %s", + "pasting_clipboard": "Pasting clipboard...", + "clipboard_rotated": "Clipboard rotated %s degrees", + "setting_blocks": "Setting blocks...", + "replacing_blocks": "Replacing blocks..." +} diff --git a/src/main/resources/locale/en_us.json b/src/main/resources/locale/en_us.json deleted file mode 100644 index 778eef7..0000000 --- a/src/main/resources/locale/en_us.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "could_not_find_wand_by_id": "Could not find wand with id %s" -}