From 9efdd886c5b876d58e36744e9442c5bc492b526e Mon Sep 17 00:00:00 2001 From: Jesse Boyd Date: Wed, 25 Dec 2019 02:34:20 +0000 Subject: [PATCH] Fix translate args --- .../worldedit/bukkit/BukkitBlockRegistry.java | 1 - .../sk89q/worldedit/bukkit/BukkitPlayer.java | 4 +- .../adapter/impl/FAWE_Spigot_v1_13_R2.java | 14 ++-- .../adapter/impl/FAWE_Spigot_v1_14_R4.java | 4 +- .../adapter/impl/FAWE_Spigot_v1_15_R1.java | 6 +- .../java/com/boydti/fawe/config/Caption.java | 69 ++++++++++++------- .../java/com/boydti/fawe/util/MainUtil.java | 2 +- .../worldedit/command/BiomeCommands.java | 2 +- .../worldedit/command/HistorySubCommands.java | 4 +- .../worldedit/command/SchematicCommands.java | 4 +- .../util/formatting/WorldEditText.java | 56 ++++++++++++++- .../util/translation/TranslationManager.java | 24 ++++--- .../src/main/resources/lang/strings.json | 2 +- 13 files changed, 139 insertions(+), 53 deletions(-) diff --git a/worldedit-bukkit/src/main/java/com/sk89q/worldedit/bukkit/BukkitBlockRegistry.java b/worldedit-bukkit/src/main/java/com/sk89q/worldedit/bukkit/BukkitBlockRegistry.java index d290fb96e..d69f66933 100644 --- a/worldedit-bukkit/src/main/java/com/sk89q/worldedit/bukkit/BukkitBlockRegistry.java +++ b/worldedit-bukkit/src/main/java/com/sk89q/worldedit/bukkit/BukkitBlockRegistry.java @@ -50,7 +50,6 @@ public class BukkitBlockRegistry extends BundledBlockRegistry { } Material mat = BukkitAdapter.adapt(blockType); if (mat == null) { - if (blockType == BlockTypes.__RESERVED__) return new PassthroughBlockMaterial(super.getMaterial(BlockTypes.AIR)); return new PassthroughBlockMaterial(null); } if (materialMap == null) { diff --git a/worldedit-bukkit/src/main/java/com/sk89q/worldedit/bukkit/BukkitPlayer.java b/worldedit-bukkit/src/main/java/com/sk89q/worldedit/bukkit/BukkitPlayer.java index e25697834..a109c2780 100644 --- a/worldedit-bukkit/src/main/java/com/sk89q/worldedit/bukkit/BukkitPlayer.java +++ b/worldedit-bukkit/src/main/java/com/sk89q/worldedit/bukkit/BukkitPlayer.java @@ -170,9 +170,7 @@ public class BukkitPlayer extends AbstractPlayerActor { @Override public void print(Component component) { - Component prefix = TranslatableComponent.of("fawe.prefix"); - component = TextComponent.builder().append(prefix).append(component).build(); - component = Caption.color(component, getLocale()); + component = Caption.color(TranslatableComponent.of("prefix", component), getLocale()); TextAdapter.sendComponent(player, component); } diff --git a/worldedit-bukkit/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/FAWE_Spigot_v1_13_R2.java b/worldedit-bukkit/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/FAWE_Spigot_v1_13_R2.java index 94c00217b..93c990cb9 100644 --- a/worldedit-bukkit/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/FAWE_Spigot_v1_13_R2.java +++ b/worldedit-bukkit/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/FAWE_Spigot_v1_13_R2.java @@ -60,6 +60,7 @@ import com.sk89q.worldedit.world.registry.BlockMaterial; import net.minecraft.server.v1_13_R2.BiomeBase; import net.minecraft.server.v1_13_R2.Block; import net.minecraft.server.v1_13_R2.BlockPosition; +import net.minecraft.server.v1_13_R2.Blocks; import net.minecraft.server.v1_13_R2.Chunk; import net.minecraft.server.v1_13_R2.ChunkProviderServer; import net.minecraft.server.v1_13_R2.ChunkSection; @@ -148,13 +149,13 @@ public final class FAWE_Spigot_v1_13_R2 extends CachedBukkitAdapter implements I @Override public BlockMaterial getMaterial(BlockType blockType) { Block block = getBlock(blockType); - return block != null ? new BlockMaterial_1_13(block) : null; + return block != null ? new BlockMaterial_1_13(block) : new BlockMaterial_1_13(Blocks.AIR); } @Override public BlockMaterial getMaterial(BlockState state) { IBlockData bs = ((CraftBlockData) Bukkit.createBlockData(state.getAsString())).getState(); - return bs != null ? new BlockMaterial_1_13(bs.getBlock(), bs) : null; + return bs != null ? new BlockMaterial_1_13(bs.getBlock(), bs) : new BlockMaterial_1_13(Blocks.AIR); } public Block getBlock(BlockType blockType) { @@ -314,8 +315,13 @@ public final class FAWE_Spigot_v1_13_R2 extends CachedBukkitAdapter implements I @Override public BlockData adapt(BlockStateHolder state) { - BlockMaterial_1_13 material = (BlockMaterial_1_13) state.getMaterial(); - return material.getCraftBlockData(); + try { + BlockMaterial_1_13 material = (BlockMaterial_1_13) state.getMaterial(); + return material.getCraftBlockData(); + } catch (ClassCastException ignore) { + System.out.println(state.getAsString() + " cast"); + throw ignore; + } } @Override diff --git a/worldedit-bukkit/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/FAWE_Spigot_v1_14_R4.java b/worldedit-bukkit/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/FAWE_Spigot_v1_14_R4.java index b714521e3..b689395bb 100644 --- a/worldedit-bukkit/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/FAWE_Spigot_v1_14_R4.java +++ b/worldedit-bukkit/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/FAWE_Spigot_v1_14_R4.java @@ -66,6 +66,7 @@ import com.sk89q.worldedit.world.registry.BlockMaterial; import net.minecraft.server.v1_14_R1.BiomeBase; import net.minecraft.server.v1_14_R1.Block; import net.minecraft.server.v1_14_R1.BlockPosition; +import net.minecraft.server.v1_14_R1.Blocks; import net.minecraft.server.v1_14_R1.Chunk; import net.minecraft.server.v1_14_R1.ChunkCoordIntPair; import net.minecraft.server.v1_14_R1.ChunkProviderServer; @@ -149,7 +150,8 @@ public final class FAWE_Spigot_v1_14_R4 extends CachedBukkitAdapter implements I @Override public BlockMaterial getMaterial(BlockType blockType) { - return new BlockMaterial_1_14(getBlock(blockType)); + Block block = getBlock(blockType); + return new BlockMaterial_1_14(block); } @Override diff --git a/worldedit-bukkit/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/FAWE_Spigot_v1_15_R1.java b/worldedit-bukkit/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/FAWE_Spigot_v1_15_R1.java index 0692a63a0..1d5ef9242 100644 --- a/worldedit-bukkit/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/FAWE_Spigot_v1_15_R1.java +++ b/worldedit-bukkit/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/FAWE_Spigot_v1_15_R1.java @@ -27,6 +27,7 @@ import com.boydti.fawe.FaweCache; import com.boydti.fawe.beta.IChunkGet; import com.boydti.fawe.beta.implementation.packet.ChunkPacket; import com.boydti.fawe.beta.implementation.queue.SingleThreadQueueExtent; +import com.boydti.fawe.bukkit.adapter.mc1_14.BlockMaterial_1_14; import com.boydti.fawe.bukkit.adapter.mc1_15.BlockMaterial_1_15; import com.boydti.fawe.bukkit.adapter.mc1_15.BukkitAdapter_1_15; import com.boydti.fawe.bukkit.adapter.mc1_15.BukkitGetBlocks_1_15; @@ -68,9 +69,11 @@ import java.util.concurrent.Future; import java.util.function.Supplier; import java.util.stream.Stream; import javax.annotation.Nullable; + import net.minecraft.server.v1_15_R1.BiomeBase; import net.minecraft.server.v1_15_R1.Block; import net.minecraft.server.v1_15_R1.BlockPosition; +import net.minecraft.server.v1_15_R1.Blocks; import net.minecraft.server.v1_15_R1.Chunk; import net.minecraft.server.v1_15_R1.ChunkCoordIntPair; import net.minecraft.server.v1_15_R1.ChunkProviderServer; @@ -139,7 +142,8 @@ public final class FAWE_Spigot_v1_15_R1 extends CachedBukkitAdapter implements I @Override public BlockMaterial getMaterial(BlockType blockType) { - return new BlockMaterial_1_15(getBlock(blockType)); + Block block = getBlock(blockType); + return new BlockMaterial_1_15(block); } @Override diff --git a/worldedit-core/src/main/java/com/boydti/fawe/config/Caption.java b/worldedit-core/src/main/java/com/boydti/fawe/config/Caption.java index d1d7fbdb4..3ab290bff 100644 --- a/worldedit-core/src/main/java/com/boydti/fawe/config/Caption.java +++ b/worldedit-core/src/main/java/com/boydti/fawe/config/Caption.java @@ -37,46 +37,67 @@ public class Caption { return color(WorldEditText.format(component, locale)); } - public static Component color(Component parent) { - if (parent instanceof TextComponent) { - TextComponent text = (TextComponent) parent; - String content = text.content(); - if (content.indexOf('&') != -1) { - Component legacy = LegacyComponentSerializer.legacy().deserialize(content, '&'); - legacy = legacy.style(parent.style()); - if (!parent.children().isEmpty()) { - parent = TextComponent.builder().append(legacy).append(parent.children()).build(); - } else { - parent = legacy; - } + private static Component color(TextComponent text) { + String content = text.content(); + if (content.indexOf('&') != -1) { + TextComponent legacy = LegacyComponentSerializer.legacy().deserialize(content, '&'); + legacy = (TextComponent) legacy.style(text.style()); + if (!text.children().isEmpty()) { + text = TextComponent.builder().append(legacy).append(text.children()).build(); + } else { + text = legacy; } } - TextColor lastColor = parent.color(); - List children = parent.children(); - if (!children.isEmpty()) { - for (int i = 0; i < children.size(); i++) { - Component original = children.get(i); + return text; + } + + private static List color(Component input, List components) { + TextColor lastColor = input.color(); + if (!components.isEmpty()) { + for (int i = 0; i < components.size(); i++) { + Component original = components.get(i); Component child = original; if (child.color() == null && lastColor != null) { child = child.color(lastColor); } child = color(child); if (original != child) { - if (!(children instanceof ArrayList)) { - children = new ArrayList<>(children); + if (!(components instanceof ArrayList)) { + components = new ArrayList<>(components); } - children.set(i, child); + components.set(i, child); } if (child.color() != null) { lastColor = child.color(); } } - if (children instanceof ArrayList) { - parent = parent.children(children); + } + return components; + } + + public static Component color(Component parent) { + if (parent instanceof TextComponent) { + parent = color((TextComponent) parent); + } + TextColor lastColor = parent.color(); + List children = parent.children(); + if (children != (children = color(parent, children))) { + parent = parent.children(children); + } + if (parent instanceof TranslatableComponent) { + TranslatableComponent tc = (TranslatableComponent) parent; + List args = tc.args(); + if (args != (args = color(parent, args))) { + parent = tc.args(args); } } - if (parent.color() == null && lastColor != null) { - parent = parent.color(lastColor); + if (parent.color() == null) { + if (!children.isEmpty()) { + lastColor = children.get(children.size() - 1).color(); + } + if (lastColor != null) { + parent = parent.color(lastColor); + } } return parent; } diff --git a/worldedit-core/src/main/java/com/boydti/fawe/util/MainUtil.java b/worldedit-core/src/main/java/com/boydti/fawe/util/MainUtil.java index 1609913ae..b2bba4e51 100644 --- a/worldedit-core/src/main/java/com/boydti/fawe/util/MainUtil.java +++ b/worldedit-core/src/main/java/com/boydti/fawe/util/MainUtil.java @@ -835,7 +835,7 @@ public class MainUtil { long age = now - file.lastModified(); if (age > timeDiff) { pool.submit(file::delete); - Component msg = WorldEditText.format(TranslatableComponent.of("worldedit.schematic.delete.deleted"), Locale.ROOT); + Component msg = TranslatableComponent.of("worldedit.schematic.delete.deleted"); if (printDebug) Fawe.debug(msg); } }); diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/command/BiomeCommands.java b/worldedit-core/src/main/java/com/sk89q/worldedit/command/BiomeCommands.java index 90bfa057b..c8210d556 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/command/BiomeCommands.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/command/BiomeCommands.java @@ -80,7 +80,7 @@ public class BiomeCommands { @Command( name = "biomelist", - aliases = { "biomels" }, + aliases = { "biomels", "/biomelist", "/listbiomes" }, desc = "Gets all biomes available." ) @CommandPermissions("worldedit.biome.list") diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/command/HistorySubCommands.java b/worldedit-core/src/main/java/com/sk89q/worldedit/command/HistorySubCommands.java index d26413221..b9fa1743b 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/command/HistorySubCommands.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/command/HistorySubCommands.java @@ -227,7 +227,7 @@ public class HistorySubCommands { int size = edit.size(); - String pageCommand = arguments.get().replaceAll("-p [0-9]+", "").trim(); + String pageCommand = "/" + arguments.get().replaceAll("-p [0-9]+", "").trim(); List> list = null; Reference>> cached = player.getMeta(pageCommand); if (cached != null) { @@ -290,7 +290,7 @@ public class HistorySubCommands { checkCommandArgument(timeDiff > 0, "Time must be >= 0"); Location origin = player.getLocation(); - String pageCommand = arguments.get().replaceAll("-p [0-9]+", "").trim(); + String pageCommand = "/" + arguments.get().replaceAll("-p [0-9]+", "").trim(); List> list = null; Reference>> cached = player.getMeta(pageCommand); diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/command/SchematicCommands.java b/worldedit-core/src/main/java/com/sk89q/worldedit/command/SchematicCommands.java index 84300f4b8..8e3aa6ea8 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/command/SchematicCommands.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/command/SchematicCommands.java @@ -552,11 +552,11 @@ public class SchematicCommands { if (oldFirst && newFirst) { throw new StopExecutionException(TextComponent.of("Cannot sort by oldest and newest.")); } - String pageCommand = arguments.get(); + String pageCommand = "/" + arguments.get(); LocalConfiguration config = worldEdit.getConfiguration(); File dir = worldEdit.getWorkingDirectoryFile(config.saveDir); - String schemCmd = "/schematic"; + String schemCmd = "//schematic"; String loadSingle = schemCmd + " load"; String loadMulti = schemCmd + " loadall"; String unload = schemCmd + " unload"; diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/util/formatting/WorldEditText.java b/worldedit-core/src/main/java/com/sk89q/worldedit/util/formatting/WorldEditText.java index e448926fc..58b37f3a4 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/util/formatting/WorldEditText.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/util/formatting/WorldEditText.java @@ -19,23 +19,77 @@ package com.sk89q.worldedit.util.formatting; +import com.boydti.fawe.util.StringMan; +import com.google.common.collect.ImmutableList; import com.sk89q.worldedit.WorldEdit; import com.sk89q.worldedit.util.formatting.text.Component; +import com.sk89q.worldedit.util.formatting.text.TextComponent; +import com.sk89q.worldedit.util.formatting.text.TranslatableComponent; +import org.enginehub.piston.config.Config; import org.enginehub.piston.config.ConfigHolder; import org.enginehub.piston.config.TextConfig; import org.enginehub.piston.util.TextHelper; +import java.lang.reflect.Method; +import java.util.Arrays; +import java.util.Iterator; +import java.util.List; import java.util.Locale; public class WorldEditText { public static final ConfigHolder CONFIG_HOLDER = ConfigHolder.create(); + private static final Method METHOD_APPLY; static { CONFIG_HOLDER.getConfig(TextConfig.commandPrefix()).setValue("/"); + try { + METHOD_APPLY = Config.class.getDeclaredMethod("apply", List.class); + METHOD_APPLY.setAccessible(true); + } catch (NoSuchMethodException e) { + e.printStackTrace(); + throw new RuntimeException(e); + } } public static Component format(Component component, Locale locale) { - return WorldEdit.getInstance().getTranslationManager().convertText(CONFIG_HOLDER.replace(component), locale); + return CONFIG_HOLDER.replace(WorldEdit.getInstance().getTranslationManager().convertText(recursiveReplace(component), locale)); + } + + private static Component recursiveReplace(Component input) { + if (input instanceof TranslatableComponent) { + TranslatableComponent tc = (TranslatableComponent)input; + List args = tc.args(); + if (args != (args = replaceChildren(args))) { + input = tc = tc.args(args); + } + if (CONFIG_HOLDER.getConfigs().containsKey(tc.key())) { + Config config = CONFIG_HOLDER.getConfigs().get(tc.key()); + try { + return (Component) METHOD_APPLY.invoke(config, replaceChildren(tc.args())); + } catch (Throwable e) { + e.printStackTrace(); + } + } + } + List original = input.children(); + List replacement = replaceChildren(original); + return original == replacement ? input : input.children(replacement); + } + + private static List replaceChildren(List input) { + if (input.isEmpty()) { + return input; + } + ImmutableList.Builder copy = ImmutableList.builder(); + boolean modified = false; + for (Component component : input) { + Component replacement = recursiveReplace(component); + if (replacement != component) { + modified = true; + } + copy.add(replacement); + } + return modified ? copy.build() : input; } public static String reduceToText(Component component, Locale locale) { diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/util/translation/TranslationManager.java b/worldedit-core/src/main/java/com/sk89q/worldedit/util/translation/TranslationManager.java index 656430038..46a582736 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/util/translation/TranslationManager.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/util/translation/TranslationManager.java @@ -19,7 +19,6 @@ package com.sk89q.worldedit.util.translation; -import com.google.common.collect.Maps; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import com.google.gson.reflect.TypeToken; @@ -28,15 +27,20 @@ import com.sk89q.worldedit.util.formatting.text.Component; import com.sk89q.worldedit.util.formatting.text.renderer.FriendlyComponentRenderer; import com.sk89q.worldedit.util.io.ResourceLoader; -import java.io.*; +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; import java.lang.reflect.Type; import java.text.MessageFormat; -import java.util.*; +import java.util.HashSet; +import java.util.Locale; +import java.util.Map; +import java.util.Optional; +import java.util.Set; import java.util.concurrent.ConcurrentHashMap; -import static java.util.stream.Collectors.toMap; - - /** * Handles translations for the plugin. * @@ -72,10 +76,8 @@ public class TranslationManager { } private Map filterTranslations(Map translations) { - return translations.entrySet().stream() - .filter(e -> !e.getValue().isEmpty()) - .map(e -> Maps.immutableEntry(e.getKey(), e.getValue().replace("'", "''"))) - .collect(toMap(Map.Entry::getKey, Map.Entry::getValue)); + translations.entrySet().removeIf(entry -> entry.getValue().isEmpty()); + return translations; } private Map parseTranslationFile(InputStream inputStream) { @@ -154,4 +156,4 @@ public class TranslationManager { public Locale getDefaultLocale() { return defaultLocale; } -} +} \ No newline at end of file diff --git a/worldedit-core/src/main/resources/lang/strings.json b/worldedit-core/src/main/resources/lang/strings.json index 33045094c..deb5d268a 100644 --- a/worldedit-core/src/main/resources/lang/strings.json +++ b/worldedit-core/src/main/resources/lang/strings.json @@ -1,5 +1,5 @@ { - "fawe.prefix": "&8(&4&lFAWE&8)&7 ", + "prefix": "&8(&4&lFAWE&8)&7 {0}", "fawe.error": "&c{0}", "fawe.info": "&7{0}", "fawe.debug": "&3{0}",