Fix translate args

This commit is contained in:
Jesse Boyd 2019-12-25 02:34:20 +00:00
parent 50c19f5a1c
commit 9efdd886c5
No known key found for this signature in database
GPG Key ID: 59F1DE6293AF6E1F
13 changed files with 139 additions and 53 deletions

View File

@ -50,7 +50,6 @@ public class BukkitBlockRegistry extends BundledBlockRegistry {
} }
Material mat = BukkitAdapter.adapt(blockType); Material mat = BukkitAdapter.adapt(blockType);
if (mat == null) { if (mat == null) {
if (blockType == BlockTypes.__RESERVED__) return new PassthroughBlockMaterial(super.getMaterial(BlockTypes.AIR));
return new PassthroughBlockMaterial(null); return new PassthroughBlockMaterial(null);
} }
if (materialMap == null) { if (materialMap == null) {

View File

@ -170,9 +170,7 @@ public class BukkitPlayer extends AbstractPlayerActor {
@Override @Override
public void print(Component component) { public void print(Component component) {
Component prefix = TranslatableComponent.of("fawe.prefix"); component = Caption.color(TranslatableComponent.of("prefix", component), getLocale());
component = TextComponent.builder().append(prefix).append(component).build();
component = Caption.color(component, getLocale());
TextAdapter.sendComponent(player, component); TextAdapter.sendComponent(player, component);
} }

View File

@ -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.BiomeBase;
import net.minecraft.server.v1_13_R2.Block; import net.minecraft.server.v1_13_R2.Block;
import net.minecraft.server.v1_13_R2.BlockPosition; 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.Chunk;
import net.minecraft.server.v1_13_R2.ChunkProviderServer; import net.minecraft.server.v1_13_R2.ChunkProviderServer;
import net.minecraft.server.v1_13_R2.ChunkSection; 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 @Override
public BlockMaterial getMaterial(BlockType blockType) { public BlockMaterial getMaterial(BlockType blockType) {
Block block = getBlock(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 @Override
public BlockMaterial getMaterial(BlockState state) { public BlockMaterial getMaterial(BlockState state) {
IBlockData bs = ((CraftBlockData) Bukkit.createBlockData(state.getAsString())).getState(); 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) { public Block getBlock(BlockType blockType) {
@ -314,8 +315,13 @@ public final class FAWE_Spigot_v1_13_R2 extends CachedBukkitAdapter implements I
@Override @Override
public BlockData adapt(BlockStateHolder state) { public BlockData adapt(BlockStateHolder state) {
BlockMaterial_1_13 material = (BlockMaterial_1_13) state.getMaterial(); try {
return material.getCraftBlockData(); BlockMaterial_1_13 material = (BlockMaterial_1_13) state.getMaterial();
return material.getCraftBlockData();
} catch (ClassCastException ignore) {
System.out.println(state.getAsString() + " cast");
throw ignore;
}
} }
@Override @Override

View File

@ -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.BiomeBase;
import net.minecraft.server.v1_14_R1.Block; import net.minecraft.server.v1_14_R1.Block;
import net.minecraft.server.v1_14_R1.BlockPosition; 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.Chunk;
import net.minecraft.server.v1_14_R1.ChunkCoordIntPair; import net.minecraft.server.v1_14_R1.ChunkCoordIntPair;
import net.minecraft.server.v1_14_R1.ChunkProviderServer; 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 @Override
public BlockMaterial getMaterial(BlockType blockType) { public BlockMaterial getMaterial(BlockType blockType) {
return new BlockMaterial_1_14(getBlock(blockType)); Block block = getBlock(blockType);
return new BlockMaterial_1_14(block);
} }
@Override @Override

View File

@ -27,6 +27,7 @@ import com.boydti.fawe.FaweCache;
import com.boydti.fawe.beta.IChunkGet; import com.boydti.fawe.beta.IChunkGet;
import com.boydti.fawe.beta.implementation.packet.ChunkPacket; import com.boydti.fawe.beta.implementation.packet.ChunkPacket;
import com.boydti.fawe.beta.implementation.queue.SingleThreadQueueExtent; 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.BlockMaterial_1_15;
import com.boydti.fawe.bukkit.adapter.mc1_15.BukkitAdapter_1_15; import com.boydti.fawe.bukkit.adapter.mc1_15.BukkitAdapter_1_15;
import com.boydti.fawe.bukkit.adapter.mc1_15.BukkitGetBlocks_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.function.Supplier;
import java.util.stream.Stream; import java.util.stream.Stream;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import net.minecraft.server.v1_15_R1.BiomeBase; import net.minecraft.server.v1_15_R1.BiomeBase;
import net.minecraft.server.v1_15_R1.Block; import net.minecraft.server.v1_15_R1.Block;
import net.minecraft.server.v1_15_R1.BlockPosition; 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.Chunk;
import net.minecraft.server.v1_15_R1.ChunkCoordIntPair; import net.minecraft.server.v1_15_R1.ChunkCoordIntPair;
import net.minecraft.server.v1_15_R1.ChunkProviderServer; 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 @Override
public BlockMaterial getMaterial(BlockType blockType) { public BlockMaterial getMaterial(BlockType blockType) {
return new BlockMaterial_1_15(getBlock(blockType)); Block block = getBlock(blockType);
return new BlockMaterial_1_15(block);
} }
@Override @Override

View File

@ -37,46 +37,67 @@ public class Caption {
return color(WorldEditText.format(component, locale)); return color(WorldEditText.format(component, locale));
} }
public static Component color(Component parent) { private static Component color(TextComponent text) {
if (parent instanceof TextComponent) { String content = text.content();
TextComponent text = (TextComponent) parent; if (content.indexOf('&') != -1) {
String content = text.content(); TextComponent legacy = LegacyComponentSerializer.legacy().deserialize(content, '&');
if (content.indexOf('&') != -1) { legacy = (TextComponent) legacy.style(text.style());
Component legacy = LegacyComponentSerializer.legacy().deserialize(content, '&'); if (!text.children().isEmpty()) {
legacy = legacy.style(parent.style()); text = TextComponent.builder().append(legacy).append(text.children()).build();
if (!parent.children().isEmpty()) { } else {
parent = TextComponent.builder().append(legacy).append(parent.children()).build(); text = legacy;
} else {
parent = legacy;
}
} }
} }
TextColor lastColor = parent.color(); return text;
List<Component> children = parent.children(); }
if (!children.isEmpty()) {
for (int i = 0; i < children.size(); i++) { private static List<Component> color(Component input, List<Component> components) {
Component original = children.get(i); TextColor lastColor = input.color();
if (!components.isEmpty()) {
for (int i = 0; i < components.size(); i++) {
Component original = components.get(i);
Component child = original; Component child = original;
if (child.color() == null && lastColor != null) { if (child.color() == null && lastColor != null) {
child = child.color(lastColor); child = child.color(lastColor);
} }
child = color(child); child = color(child);
if (original != child) { if (original != child) {
if (!(children instanceof ArrayList)) { if (!(components instanceof ArrayList)) {
children = new ArrayList<>(children); components = new ArrayList<>(components);
} }
children.set(i, child); components.set(i, child);
} }
if (child.color() != null) { if (child.color() != null) {
lastColor = child.color(); 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<Component> children = parent.children();
if (children != (children = color(parent, children))) {
parent = parent.children(children);
}
if (parent instanceof TranslatableComponent) {
TranslatableComponent tc = (TranslatableComponent) parent;
List<Component> args = tc.args();
if (args != (args = color(parent, args))) {
parent = tc.args(args);
} }
} }
if (parent.color() == null && lastColor != null) { if (parent.color() == null) {
parent = parent.color(lastColor); if (!children.isEmpty()) {
lastColor = children.get(children.size() - 1).color();
}
if (lastColor != null) {
parent = parent.color(lastColor);
}
} }
return parent; return parent;
} }

View File

@ -835,7 +835,7 @@ public class MainUtil {
long age = now - file.lastModified(); long age = now - file.lastModified();
if (age > timeDiff) { if (age > timeDiff) {
pool.submit(file::delete); 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); if (printDebug) Fawe.debug(msg);
} }
}); });

View File

@ -80,7 +80,7 @@ public class BiomeCommands {
@Command( @Command(
name = "biomelist", name = "biomelist",
aliases = { "biomels" }, aliases = { "biomels", "/biomelist", "/listbiomes" },
desc = "Gets all biomes available." desc = "Gets all biomes available."
) )
@CommandPermissions("worldedit.biome.list") @CommandPermissions("worldedit.biome.list")

View File

@ -227,7 +227,7 @@ public class HistorySubCommands {
int size = edit.size(); int size = edit.size();
String pageCommand = arguments.get().replaceAll("-p [0-9]+", "").trim(); String pageCommand = "/" + arguments.get().replaceAll("-p [0-9]+", "").trim();
List<Countable<BlockState>> list = null; List<Countable<BlockState>> list = null;
Reference<List<Countable<BlockState>>> cached = player.getMeta(pageCommand); Reference<List<Countable<BlockState>>> cached = player.getMeta(pageCommand);
if (cached != null) { if (cached != null) {
@ -290,7 +290,7 @@ public class HistorySubCommands {
checkCommandArgument(timeDiff > 0, "Time must be >= 0"); checkCommandArgument(timeDiff > 0, "Time must be >= 0");
Location origin = player.getLocation(); Location origin = player.getLocation();
String pageCommand = arguments.get().replaceAll("-p [0-9]+", "").trim(); String pageCommand = "/" + arguments.get().replaceAll("-p [0-9]+", "").trim();
List<Supplier<RollbackOptimizedHistory>> list = null; List<Supplier<RollbackOptimizedHistory>> list = null;
Reference<List<Supplier<RollbackOptimizedHistory>>> cached = player.getMeta(pageCommand); Reference<List<Supplier<RollbackOptimizedHistory>>> cached = player.getMeta(pageCommand);

View File

@ -552,11 +552,11 @@ public class SchematicCommands {
if (oldFirst && newFirst) { if (oldFirst && newFirst) {
throw new StopExecutionException(TextComponent.of("Cannot sort by oldest and newest.")); throw new StopExecutionException(TextComponent.of("Cannot sort by oldest and newest."));
} }
String pageCommand = arguments.get(); String pageCommand = "/" + arguments.get();
LocalConfiguration config = worldEdit.getConfiguration(); LocalConfiguration config = worldEdit.getConfiguration();
File dir = worldEdit.getWorkingDirectoryFile(config.saveDir); File dir = worldEdit.getWorkingDirectoryFile(config.saveDir);
String schemCmd = "/schematic"; String schemCmd = "//schematic";
String loadSingle = schemCmd + " load"; String loadSingle = schemCmd + " load";
String loadMulti = schemCmd + " loadall"; String loadMulti = schemCmd + " loadall";
String unload = schemCmd + " unload"; String unload = schemCmd + " unload";

View File

@ -19,23 +19,77 @@
package com.sk89q.worldedit.util.formatting; 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.WorldEdit;
import com.sk89q.worldedit.util.formatting.text.Component; 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.ConfigHolder;
import org.enginehub.piston.config.TextConfig; import org.enginehub.piston.config.TextConfig;
import org.enginehub.piston.util.TextHelper; 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; import java.util.Locale;
public class WorldEditText { public class WorldEditText {
public static final ConfigHolder CONFIG_HOLDER = ConfigHolder.create(); public static final ConfigHolder CONFIG_HOLDER = ConfigHolder.create();
private static final Method METHOD_APPLY;
static { static {
CONFIG_HOLDER.getConfig(TextConfig.commandPrefix()).setValue("/"); 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) { 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<Component> 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<Component> original = input.children();
List<Component> replacement = replaceChildren(original);
return original == replacement ? input : input.children(replacement);
}
private static List<Component> replaceChildren(List<Component> input) {
if (input.isEmpty()) {
return input;
}
ImmutableList.Builder<Component> 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) { public static String reduceToText(Component component, Locale locale) {

View File

@ -19,7 +19,6 @@
package com.sk89q.worldedit.util.translation; package com.sk89q.worldedit.util.translation;
import com.google.common.collect.Maps;
import com.google.gson.Gson; import com.google.gson.Gson;
import com.google.gson.GsonBuilder; import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken; 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.formatting.text.renderer.FriendlyComponentRenderer;
import com.sk89q.worldedit.util.io.ResourceLoader; 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.lang.reflect.Type;
import java.text.MessageFormat; 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 java.util.concurrent.ConcurrentHashMap;
import static java.util.stream.Collectors.toMap;
/** /**
* Handles translations for the plugin. * Handles translations for the plugin.
* *
@ -72,10 +76,8 @@ public class TranslationManager {
} }
private Map<String, String> filterTranslations(Map<String, String> translations) { private Map<String, String> filterTranslations(Map<String, String> translations) {
return translations.entrySet().stream() translations.entrySet().removeIf(entry -> entry.getValue().isEmpty());
.filter(e -> !e.getValue().isEmpty()) return translations;
.map(e -> Maps.immutableEntry(e.getKey(), e.getValue().replace("'", "''")))
.collect(toMap(Map.Entry::getKey, Map.Entry::getValue));
} }
private Map<String, String> parseTranslationFile(InputStream inputStream) { private Map<String, String> parseTranslationFile(InputStream inputStream) {
@ -154,4 +156,4 @@ public class TranslationManager {
public Locale getDefaultLocale() { public Locale getDefaultLocale() {
return defaultLocale; return defaultLocale;
} }
} }

View File

@ -1,5 +1,5 @@
{ {
"fawe.prefix": "&8(&4&lFAWE&8)&7 ", "prefix": "&8(&4&lFAWE&8)&7 {0}",
"fawe.error": "&c{0}", "fawe.error": "&c{0}",
"fawe.info": "&7{0}", "fawe.info": "&7{0}",
"fawe.debug": "&3{0}", "fawe.debug": "&3{0}",