locale files, working

This commit is contained in:
2024-12-06 18:40:53 +01:00
parent f5ed0b1c2c
commit ca190ffbdd
7 changed files with 84 additions and 47 deletions
+5
View File
@@ -86,5 +86,10 @@
<version>6.1</version> <version>6.1</version>
<scope>compile</scope> <scope>compile</scope>
</dependency> </dependency>
<dependency>
<groupId>org.reflections</groupId>
<artifactId>reflections</artifactId>
<version>0.10.2</version>
</dependency>
</dependencies> </dependencies>
</project> </project>
@@ -10,18 +10,28 @@ import java.util.Map;
public class BELocale { public class BELocale {
private static final Map<Locale, BELocale> locales = new HashMap<>(); private static final Map<Locale, BELocale> 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) { 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) { protected static void registerLocale(Locale locale, BELocale blockEditLocale) {
locales.put(locale, blockEditLocale); locales.put(locale, blockEditLocale);
System.out.println("Registered locale " + locale.getISO3Country() + " " + locale.getISO3Language());
} }
public static BELocale getLocale(@NotNull Locale locale) { public static BELocale getLocale(@NotNull Locale locale) {
@@ -5,10 +5,14 @@ import com.google.gson.JsonObject;
import com.google.gson.stream.JsonReader; import com.google.gson.stream.JsonReader;
import me.youhavetrouble.blockedit.wands.SelectionWand; import me.youhavetrouble.blockedit.wands.SelectionWand;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
import org.reflections.Reflections;
import org.reflections.scanners.Scanners;
import java.io.*; import java.io.*;
import java.util.List; import java.net.URL;
import java.util.Locale; import java.util.*;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
public final class BlockEdit extends JavaPlugin { public final class BlockEdit extends JavaPlugin {
@@ -51,37 +55,33 @@ public final class BlockEdit extends JavaPlugin {
} }
private boolean initLocales() { private boolean initLocales() {
List<String> localeFiles; Reflections reflections = new Reflections("locale", Scanners.Resources);
try (InputStream in = BlockEdit.class.getClassLoader().getResourceAsStream("locale"); Set<String> fileNames = reflections.getResources(Pattern.compile("([a-zA-Z]{1,3}_[a-zA-Z]{1,3})(\\.json)"));
BufferedReader br = new BufferedReader(new InputStreamReader(in))) {
localeFiles = br.lines().toList();
} catch (IOException e) {
plugin.getSLF4JLogger().error("Error loading locale files", e);
return false;
}
Gson gson = new Gson(); Gson gson = new Gson();
for (String fileName : localeFiles) { for (String fileName : fileNames) {
Locale locale; Locale locale;
try { try {
String localeString = fileName.replace(".json", ""); String localeString = fileName
String[] split = localeString.split("_"); .replace(".json", "")
if (split.length == 1) { .replace("locale/", "");
locale = Locale.of(split[0]); locale = Locale.of(localeString);
} else {
locale = Locale.of(split[0], split[1]);
}
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
plugin.getSLF4JLogger().error("Invalid locale file name: {}", fileName); plugin.getSLF4JLogger().error("Invalid locale file name: {}", fileName);
continue; continue;
} }
try (InputStream fileStream = BlockEdit.class.getClassLoader().getResourceAsStream("locale/" + fileName); String resourcePath = "/" + fileName;
JsonReader reader = new JsonReader(new InputStreamReader(fileStream))) { 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); JsonObject json = gson.fromJson(reader, JsonObject.class);
BELocale beLocale = new BELocale(json); BELocale beLocale = new BELocale(json);
BELocale.registerLocale(locale, beLocale); BELocale.registerLocale(locale, beLocale);
} catch (IOException e) { } catch (IOException e) {
plugin.getSLF4JLogger().error("Error reading locale file: {}", fileName, e); plugin.getSLF4JLogger().error("Error reading locale file: {}", resourcePath, e);
} }
} }
return true; return true;
@@ -17,6 +17,7 @@ import me.youhavetrouble.blockedit.operations.SetOperation;
import me.youhavetrouble.blockedit.util.Selection; import me.youhavetrouble.blockedit.util.Selection;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor; import net.kyori.adventure.text.format.NamedTextColor;
import org.bukkit.Location;
import org.bukkit.block.BlockState; import org.bukkit.block.BlockState;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
@@ -80,7 +81,6 @@ public class BlockEditCommands {
); );
}); });
} }
private static LiteralCommandNode<CommandSourceStack> wandCommand() { private static LiteralCommandNode<CommandSourceStack> wandCommand() {
@@ -106,7 +106,8 @@ public class BlockEditCommands {
String wandId = ctx.getArgument("wand_id", String.class); String wandId = ctx.getArgument("wand_id", String.class);
ItemStack wand = BlockEditAPI.getWandsHandler().getWand(wandId); ItemStack wand = BlockEditAPI.getWandsHandler().getWand(wandId);
if (wand == null) { 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; return Command.SINGLE_SUCCESS;
} }
player.getInventory().addItem(wand); player.getInventory().addItem(wand);
@@ -133,9 +134,9 @@ public class BlockEditCommands {
BEPlayer bePlayer = BEPlayer.getByPlayer(player); BEPlayer bePlayer = BEPlayer.getByPlayer(player);
try { try {
bePlayer.setClipboardFromSelection(); bePlayer.setClipboardFromSelection();
player.sendMessage(Component.text("Copied selection to clipboard")); player.sendMessage(Component.text(BELocale.getLocale(player.locale()).copiedSelectionToClipboard, NamedTextColor.GRAY));
} catch (IllegalStateException e) { } 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; return Command.SINGLE_SUCCESS;
}) })
@@ -151,7 +152,7 @@ public class BlockEditCommands {
.executes(ctx -> { .executes(ctx -> {
Player player = (Player) ctx.getSource().getSender(); Player player = (Player) ctx.getSource().getSender();
BEPlayer.getByPlayer(player).resetSelection(); 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; return Command.SINGLE_SUCCESS;
}) })
.build(); .build();
@@ -165,8 +166,10 @@ public class BlockEditCommands {
}) })
.executes(ctx -> { .executes(ctx -> {
Player player = (Player) ctx.getSource().getSender(); Player player = (Player) ctx.getSource().getSender();
BEPlayer.getByPlayer(player).setSelectionPoint1(player.getLocation().toBlockLocation()); Location location = player.getLocation().toBlockLocation();
player.sendMessage(Component.text("First point set at your location")); 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; return Command.SINGLE_SUCCESS;
}) })
.build(); .build();
@@ -180,8 +183,10 @@ public class BlockEditCommands {
}) })
.executes(ctx -> { .executes(ctx -> {
Player player = (Player) ctx.getSource().getSender(); Player player = (Player) ctx.getSource().getSender();
BEPlayer.getByPlayer(player).setSelectionPoint2(player.getLocation().toBlockLocation()); Location location = player.getLocation().toBlockLocation();
player.sendMessage(Component.text("Second point set at your location")); 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; return Command.SINGLE_SUCCESS;
}) })
.build(); .build();
@@ -207,7 +212,7 @@ public class BlockEditCommands {
Selection selection = Selection.fromClipboard(absoluteBlocks.keySet(), player.getWorld()); Selection selection = Selection.fromClipboard(absoluteBlocks.keySet(), player.getWorld());
BlockEditAPI.runOperation(selection, 1, new PasteOperation(absoluteBlocks)); 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; return Command.SINGLE_SUCCESS;
}) })
@@ -227,7 +232,7 @@ public class BlockEditCommands {
double angle = ctx.getArgument("angle", Double.class); double angle = ctx.getArgument("angle", Double.class);
BEPlayer bePlayer = BEPlayer.getByPlayer(player); BEPlayer bePlayer = BEPlayer.getByPlayer(player);
bePlayer.getClipboard().rotate(angle); 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; return Command.SINGLE_SUCCESS;
}) })
) )
@@ -248,7 +253,7 @@ public class BlockEditCommands {
BlockState blockState = ctx.getArgument("block", BlockState.class); BlockState blockState = ctx.getArgument("block", BlockState.class);
Selection selection = bePlayer.getSelection(); Selection selection = bePlayer.getSelection();
BlockEditAPI.runOperation(selection, 1, new SetOperation(blockState)); 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; return Command.SINGLE_SUCCESS;
}) })
) )
@@ -261,7 +266,7 @@ public class BlockEditCommands {
int chunksPerTick = ctx.getArgument("chunks_per_tick", Integer.class); int chunksPerTick = ctx.getArgument("chunks_per_tick", Integer.class);
Selection selection = bePlayer.getSelection(); Selection selection = bePlayer.getSelection();
BlockEditAPI.runOperation(selection, chunksPerTick, new SetOperation(blockState)); 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; return Command.SINGLE_SUCCESS;
})) }))
.build(); .build();
@@ -285,7 +290,7 @@ public class BlockEditCommands {
BlockState replaceWith = ctx.getArgument("replace_with", BlockState.class); BlockState replaceWith = ctx.getArgument("replace_with", BlockState.class);
Selection selection = bePlayer.getSelection(); Selection selection = bePlayer.getSelection();
BlockEditAPI.runOperation(selection, 1, new ReplaceOperation(toReplace, replaceWith)); 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; return Command.SINGLE_SUCCESS;
}) })
) )
@@ -299,7 +304,7 @@ public class BlockEditCommands {
int chunksPerTick = ctx.getArgument("chunks_per_tick", Integer.class); int chunksPerTick = ctx.getArgument("chunks_per_tick", Integer.class);
Selection selection = bePlayer.getSelection(); Selection selection = bePlayer.getSelection();
BlockEditAPI.runOperation(selection, chunksPerTick, new ReplaceOperation(toReplace, replaceWith)); 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; return Command.SINGLE_SUCCESS;
}) })
) )
@@ -1,10 +1,13 @@
package me.youhavetrouble.blockedit.wands; package me.youhavetrouble.blockedit.wands;
import me.youhavetrouble.blockedit.BELocale;
import me.youhavetrouble.blockedit.BEPlayer; import me.youhavetrouble.blockedit.BEPlayer;
import me.youhavetrouble.blockedit.api.BlockEditAPI; import me.youhavetrouble.blockedit.api.BlockEditAPI;
import me.youhavetrouble.blockedit.api.BlockEditWand; import me.youhavetrouble.blockedit.api.BlockEditWand;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.text.format.TextDecoration; import net.kyori.adventure.text.format.TextDecoration;
import org.bukkit.Location;
import org.bukkit.block.Block; import org.bukkit.block.Block;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
@@ -13,6 +16,7 @@ import org.bukkit.event.Listener;
import org.bukkit.event.block.Action; import org.bukkit.event.block.Action;
import org.bukkit.event.player.PlayerInteractEvent; import org.bukkit.event.player.PlayerInteractEvent;
@SuppressWarnings("UnstableApiUsage")
public class SelectionWand implements Listener, BlockEditWand { public class SelectionWand implements Listener, BlockEditWand {
@Override @Override
@@ -47,14 +51,18 @@ public class SelectionWand implements Listener, BlockEditWand {
Action action = event.getAction(); Action action = event.getAction();
if (action.equals(Action.LEFT_CLICK_BLOCK)) { if (action.equals(Action.LEFT_CLICK_BLOCK)) {
event.setCancelled(true); event.setCancelled(true);
BEPlayer.getByPlayer(player).setSelectionPoint1(block.getLocation()); Location location = block.getLocation();
player.sendMessage(Component.text("First point set")); 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; return;
} }
if (action.equals(Action.RIGHT_CLICK_BLOCK)) { if (action.equals(Action.RIGHT_CLICK_BLOCK)) {
event.setCancelled(true); event.setCancelled(true);
BEPlayer.getByPlayer(player).setSelectionPoint2(block.getLocation()); Location location = block.getLocation();
player.sendMessage(Component.text("Second point set")); 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; return;
} }
} }
+12
View File
@@ -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..."
}
-3
View File
@@ -1,3 +0,0 @@
{
"could_not_find_wand_by_id": "Could not find wand with id %s"
}