diff --git a/buildSrc/src/main/kotlin/LibsConfig.kt b/buildSrc/src/main/kotlin/LibsConfig.kt index 172be5d39..a84f4f5d9 100644 --- a/buildSrc/src/main/kotlin/LibsConfig.kt +++ b/buildSrc/src/main/kotlin/LibsConfig.kt @@ -25,6 +25,11 @@ fun Project.applyLibrariesConfiguration() { group = "${rootProject.group}.worldedit-libs" + val relocations = mapOf( + "net.kyori.text" to "com.sk89q.worldedit.util.formatting.text", + "net.kyori.minecraft" to "com.sk89q.worldedit.util.kyori" + ) + tasks.register("jar") { configurations = listOf(project.configurations["shade"]) archiveClassifier.set("") @@ -36,13 +41,15 @@ fun Project.applyLibrariesConfiguration() { exclude(dependency("org.slf4j:slf4j-api")) } - relocate("net.kyori.text", "com.sk89q.worldedit.util.formatting.text") + relocations.forEach { (from, to) -> + relocate(from, to) + } } val altConfigFiles = { artifactType: String -> val deps = configurations["shade"].incoming.dependencies .filterIsInstance() .map { it.copy() } - .map { dependency: ModuleDependency -> + .map { dependency -> dependency.artifact { name = dependency.name type = artifactType @@ -61,13 +68,15 @@ fun Project.applyLibrariesConfiguration() { from({ altConfigFiles("sources") }) - val filePattern = Regex("(.*)net/kyori/text((?:/|$).*)") - val textPattern = Regex("net\\.kyori\\.text") - eachFile { - filter { - it.replaceFirst(textPattern, "com.sk89q.worldedit.util.formatting.text") + relocations.forEach { (from, to) -> + val filePattern = Regex("(.*)${from.replace('.', '/')}((?:/|$).*)") + val textPattern = Regex.fromLiteral(from) + eachFile { + filter { + it.replaceFirst(textPattern, to) + } + path = path.replaceFirst(filePattern, "$1${to.replace('.', '/')}$2") } - path = path.replaceFirst(filePattern, "$1com/sk89q/worldedit/util/formatting/text$2") } archiveClassifier.set("sources") } diff --git a/worldedit-bukkit/src/main/java/com/sk89q/worldedit/bukkit/BukkitBiomeRegistry.java b/worldedit-bukkit/src/main/java/com/sk89q/worldedit/bukkit/BukkitBiomeRegistry.java index d61d833fe..5bccb3283 100644 --- a/worldedit-bukkit/src/main/java/com/sk89q/worldedit/bukkit/BukkitBiomeRegistry.java +++ b/worldedit-bukkit/src/main/java/com/sk89q/worldedit/bukkit/BukkitBiomeRegistry.java @@ -19,6 +19,9 @@ package com.sk89q.worldedit.bukkit; +import com.sk89q.worldedit.util.formatting.text.Component; +import com.sk89q.worldedit.util.formatting.text.TranslatableComponent; +import com.sk89q.worldedit.util.translation.TranslationManager; import com.sk89q.worldedit.world.biome.BiomeData; import com.sk89q.worldedit.world.biome.BiomeType; import com.sk89q.worldedit.world.registry.BiomeRegistry; @@ -34,6 +37,14 @@ class BukkitBiomeRegistry implements BiomeRegistry { BukkitBiomeRegistry() { } + @Override + public Component getRichName(BiomeType biomeType) { + return TranslatableComponent.of( + TranslationManager.makeTranslationKey("biome", biomeType.getId()) + ); + } + + @Deprecated @Nullable @Override public BiomeData getData(BiomeType biome) { 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 6decfd7f2..221980f2d 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 @@ -20,6 +20,7 @@ package com.sk89q.worldedit.bukkit; import com.sk89q.worldedit.bukkit.adapter.BukkitImplAdapter; +import com.sk89q.worldedit.util.formatting.text.Component; import com.sk89q.worldedit.registry.state.Property; import com.sk89q.worldedit.world.block.BlockState; import com.sk89q.worldedit.world.block.BlockType; @@ -39,6 +40,14 @@ public class BukkitBlockRegistry extends BundledBlockRegistry { private BukkitBlockMaterial[] materialMap; + @Override + public Component getRichName(BlockType blockType) { + if (WorldEditPlugin.getInstance().getBukkitImplAdapter() != null) { + return WorldEditPlugin.getInstance().getBukkitImplAdapter().getRichBlockName(blockType); + } + return super.getRichName(blockType); + } + @Nullable @Override public BlockMaterial getMaterial(BlockType blockType) { diff --git a/worldedit-bukkit/src/main/java/com/sk89q/worldedit/bukkit/BukkitItemRegistry.java b/worldedit-bukkit/src/main/java/com/sk89q/worldedit/bukkit/BukkitItemRegistry.java index 2c49a6a5f..b76c06fbd 100644 --- a/worldedit-bukkit/src/main/java/com/sk89q/worldedit/bukkit/BukkitItemRegistry.java +++ b/worldedit-bukkit/src/main/java/com/sk89q/worldedit/bukkit/BukkitItemRegistry.java @@ -19,13 +19,32 @@ package com.sk89q.worldedit.bukkit; +import com.sk89q.worldedit.blocks.BaseItemStack; +import com.sk89q.worldedit.util.formatting.text.Component; +import com.sk89q.worldedit.world.item.ItemType; import com.sk89q.worldedit.world.registry.BundledItemRegistry; import org.bukkit.Material; import java.util.ArrayList; import java.util.Collection; -public class BukkitItemRegistry extends BundledItemRegistry { +class BukkitItemRegistry extends BundledItemRegistry { + @Override + public Component getRichName(ItemType itemType) { + if (WorldEditPlugin.getInstance().getBukkitImplAdapter() != null) { + return WorldEditPlugin.getInstance().getBukkitImplAdapter().getRichItemName(itemType); + } + return super.getRichName(itemType); + } + + @Override + public Component getRichName(BaseItemStack itemStack) { + if (WorldEditPlugin.getInstance().getBukkitImplAdapter() != null) { + return WorldEditPlugin.getInstance().getBukkitImplAdapter().getRichItemName(itemStack); + } + return super.getRichName(itemStack); + } + @Override public Collection values() { ArrayList values = new ArrayList<>(); diff --git a/worldedit-bukkit/src/main/java/com/sk89q/worldedit/bukkit/BukkitRegistries.java b/worldedit-bukkit/src/main/java/com/sk89q/worldedit/bukkit/BukkitRegistries.java index 005526a00..db865281b 100644 --- a/worldedit-bukkit/src/main/java/com/sk89q/worldedit/bukkit/BukkitRegistries.java +++ b/worldedit-bukkit/src/main/java/com/sk89q/worldedit/bukkit/BukkitRegistries.java @@ -22,7 +22,6 @@ package com.sk89q.worldedit.bukkit; import com.sk89q.worldedit.world.registry.BiomeRegistry; import com.sk89q.worldedit.world.registry.BlockCategoryRegistry; import com.sk89q.worldedit.world.registry.BlockRegistry; -import com.sk89q.worldedit.world.registry.BundledItemRegistry; import com.sk89q.worldedit.world.registry.BundledRegistries; import com.sk89q.worldedit.world.registry.EntityRegistry; import com.sk89q.worldedit.world.registry.ItemCategoryRegistry; @@ -35,8 +34,8 @@ class BukkitRegistries extends BundledRegistries { private static final BukkitRegistries INSTANCE = new BukkitRegistries(); private final BlockRegistry blockRegistry = new BukkitBlockRegistry(); - private final ItemRegistry itemRegistry = new BukkitItemRegistry(); private final BiomeRegistry biomeRegistry = new BukkitBiomeRegistry(); + private final ItemRegistry itemRegistry = new BukkitItemRegistry(); private final EntityRegistry entityRegistry = new BukkitEntityRegistry(); private final BlockCategoryRegistry blockCategoryRegistry = new BukkitBlockCategoryRegistry(); private final ItemCategoryRegistry itemCategoryRegistry = new BukkitItemCategoryRegistry(); @@ -57,6 +56,11 @@ class BukkitRegistries extends BundledRegistries { return biomeRegistry; } + @Override + public ItemRegistry getItemRegistry() { + return itemRegistry; + } + @Override public BlockCategoryRegistry getBlockCategoryRegistry() { return blockCategoryRegistry; @@ -81,8 +85,4 @@ class BukkitRegistries extends BundledRegistries { return INSTANCE; } - @Override - public ItemRegistry getItemRegistry() { - return itemRegistry; - } } diff --git a/worldedit-bukkit/src/main/java/com/sk89q/worldedit/bukkit/adapter/BukkitImplAdapter.java b/worldedit-bukkit/src/main/java/com/sk89q/worldedit/bukkit/adapter/BukkitImplAdapter.java index cd3e86499..52286440a 100644 --- a/worldedit-bukkit/src/main/java/com/sk89q/worldedit/bukkit/adapter/BukkitImplAdapter.java +++ b/worldedit-bukkit/src/main/java/com/sk89q/worldedit/bukkit/adapter/BukkitImplAdapter.java @@ -36,12 +36,14 @@ import com.sk89q.worldedit.regions.Region; import com.sk89q.worldedit.registry.state.Property; import com.sk89q.worldedit.util.Direction; import com.sk89q.worldedit.util.SideEffect; +import com.sk89q.worldedit.util.formatting.text.Component; import com.sk89q.worldedit.world.DataFixer; import com.sk89q.worldedit.world.RegenOptions; import com.sk89q.worldedit.world.biome.BiomeType; import com.sk89q.worldedit.world.block.BaseBlock; import com.sk89q.worldedit.world.block.BlockState; import com.sk89q.worldedit.world.block.BlockType; +import com.sk89q.worldedit.world.item.ItemType; import com.sk89q.worldedit.world.registry.BlockMaterial; import org.bukkit.Location; import org.bukkit.World; @@ -52,10 +54,10 @@ import org.bukkit.entity.Entity; import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; +import javax.annotation.Nullable; import java.util.Map; import java.util.OptionalInt; import java.util.Set; -import javax.annotation.Nullable; /** * An interface for adapters of various Bukkit implementations. @@ -127,6 +129,30 @@ public interface BukkitImplAdapter extends IBukkitAdapter { @Nullable Entity createEntity(Location location, BaseEntity state); + /** + * Gets the name for the given block. + * + * @param blockType the block + * @return The name + */ + Component getRichBlockName(BlockType blockType); + + /** + * Gets the name for the given item. + * + * @param itemType the item + * @return The name + */ + Component getRichItemName(ItemType itemType); + + /** + * Gets the name for the given item stack. + * + * @param itemStack the item stack + * @return The name + */ + Component getRichItemName(BaseItemStack itemStack); + /** * Get a map of {@code string -> property}. * diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/WorldEdit.java b/worldedit-core/src/main/java/com/sk89q/worldedit/WorldEdit.java index 25c14e5e4..289c82bb7 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/WorldEdit.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/WorldEdit.java @@ -591,15 +591,17 @@ public final class WorldEdit { Map missingBlocks = editSession.popMissingBlocks(); if (!missingBlocks.isEmpty()) { - StringBuilder str = new StringBuilder(); + TextComponent.Builder str = TextComponent.builder(); str.append("Missing these blocks: "); int size = missingBlocks.size(); int i = 0; for (Map.Entry blockTypeIntegerEntry : missingBlocks.entrySet()) { - str.append((blockTypeIntegerEntry.getKey()).getName()); + str.append((blockTypeIntegerEntry.getKey()).getRichName()); - str.append(" [Amt: ").append(blockTypeIntegerEntry.getValue()).append("]"); + str.append(" [Amt: ") + .append(String.valueOf(blockTypeIntegerEntry.getValue())) + .append("]"); ++i; @@ -608,7 +610,7 @@ public final class WorldEdit { } } - actor.printError(str.toString()); + actor.printError(str.build()); } } diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/blocks/BaseItemStack.java b/worldedit-core/src/main/java/com/sk89q/worldedit/blocks/BaseItemStack.java index 291875342..2f22ebb93 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/blocks/BaseItemStack.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/blocks/BaseItemStack.java @@ -20,6 +20,9 @@ package com.sk89q.worldedit.blocks; import com.sk89q.jnbt.CompoundTag; +import com.sk89q.worldedit.WorldEdit; +import com.sk89q.worldedit.extension.platform.Capability; +import com.sk89q.worldedit.util.formatting.text.Component; import com.sk89q.worldedit.world.item.ItemType; /** @@ -65,7 +68,7 @@ public class BaseItemStack extends BaseItem { /** * Get the number of items in the stack. - * + * * @return the amount */ public int getAmount() { @@ -74,10 +77,15 @@ public class BaseItemStack extends BaseItem { /** * Set the amount of items in the stack. - * + * * @param amount the amount to set */ public void setAmount(int amount) { this.amount = amount; } + + public Component getRichName() { + return WorldEdit.getInstance().getPlatformManager().queryCapability(Capability.GAME_HOOKS) + .getRegistries().getItemRegistry().getRichName(this); + } } 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 c4fc98694..15a9e32e7 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 @@ -49,7 +49,6 @@ import com.sk89q.worldedit.util.formatting.text.TextComponent; import com.sk89q.worldedit.util.formatting.text.TranslatableComponent; import com.sk89q.worldedit.util.formatting.text.event.HoverEvent; import com.sk89q.worldedit.world.World; -import com.sk89q.worldedit.world.biome.BiomeData; import com.sk89q.worldedit.world.biome.BiomeType; import com.sk89q.worldedit.world.registry.BiomeRegistry; import org.enginehub.piston.annotation.Command; @@ -87,23 +86,19 @@ public class BiomeCommands { @ArgFlag(name = 'p', desc = "Page number.", def = "1") int page) { WorldEditAsyncCommandBuilder.createAndSendMessage(actor, () -> { - BiomeRegistry biomeRegistry = - WorldEdit.getInstance().getPlatformManager().queryCapability(Capability.GAME_HOOKS) - .getRegistries().getBiomeRegistry(); + BiomeRegistry biomeRegistry = WorldEdit.getInstance().getPlatformManager() + .queryCapability(Capability.GAME_HOOKS).getRegistries().getBiomeRegistry(); - PaginationBox paginationBox = PaginationBox - .fromStrings("Available Biomes", "/biomelist -p %page%", - BiomeType.REGISTRY.values().stream().map(biomeType -> { - String id = biomeType.getId(); - final BiomeData data = biomeRegistry.getData(biomeType); - if (data != null) { - String name = data.getName(); - return id + " (" + name + ")"; - } else { - return id; - } - }).collect(Collectors.toList())); - return paginationBox.create(page); + PaginationBox paginationBox = PaginationBox.fromComponents("Available Biomes", "/biomelist -p %page%", + BiomeType.REGISTRY.values().stream() + .map(biomeType -> TextComponent.builder() + .append(biomeType.getId()) + .append(" (") + .append(biomeRegistry.getRichName(biomeType)) + .append(")") + .build()) + .collect(Collectors.toList())); + return paginationBox.create(page); }, (Component) null); } @@ -150,14 +145,11 @@ public class BiomeCommands { messageKey = "worldedit.biomeinfo.selection"; } - List components = biomes.stream().map(biome -> { - BiomeData data = biomeRegistry.getData(biome); - if (data != null) { - return TextComponent.of(data.getName()).hoverEvent(HoverEvent.showText(TextComponent.of(biome.getId()))); - } else { - return TextComponent.of(biome.getId()); - } - }).collect(Collectors.toList()); + List components = biomes.stream().map(biome -> + biomeRegistry.getRichName(biome).hoverEvent( + HoverEvent.showText(TextComponent.of(biome.getId())) + ) + ).collect(Collectors.toList()); player.printInfo(TranslatableComponent.of(messageKey, TextUtils.join(components, TextComponent.of(", ")))); } diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/command/SelectionCommands.java b/worldedit-core/src/main/java/com/sk89q/worldedit/command/SelectionCommands.java index 47ce2b41b..2a4a0c0e0 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/command/SelectionCommands.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/command/SelectionCommands.java @@ -715,7 +715,7 @@ public class SelectionCommands { final BlockState state = c.getID(); final BlockType blockType = state.getBlockType(); - TextComponent blockName = TextComponent.of(blockType.getName(), TextColor.LIGHT_PURPLE); + Component blockName = blockType.getRichName().color(TextColor.LIGHT_PURPLE); TextComponent toolTip; if (separateStates && state != blockType.getDefaultState()) { toolTip = TextComponent.of(state.getAsString(), TextColor.GRAY); diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/command/tool/BlockReplacer.java b/worldedit-core/src/main/java/com/sk89q/worldedit/command/tool/BlockReplacer.java index 14479c2a8..5c3aabb9e 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/command/tool/BlockReplacer.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/command/tool/BlockReplacer.java @@ -78,7 +78,7 @@ public class BlockReplacer implements DoubleActionBlockTool { if (targetBlock != null) { pattern = targetBlock; - player.printInfo(TranslatableComponent.of("worldedit.tool.repl.switched", TextComponent.of(targetBlock.getBlockType().getName()))); + player.printInfo(TranslatableComponent.of("worldedit.tool.repl.switched", targetBlock.getBlockType().getRichName())); } return true; diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/command/tool/QueryTool.java b/worldedit-core/src/main/java/com/sk89q/worldedit/command/tool/QueryTool.java index 1f192aea1..ebce5559e 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/command/tool/QueryTool.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/command/tool/QueryTool.java @@ -59,7 +59,7 @@ public class QueryTool implements BlockTool { TextComponent.Builder builder = TextComponent.builder(); builder.append(TextComponent.of("@" + clicked.toVector().toBlockPoint() + ": ", TextColor.BLUE)); - builder.append(TextComponent.of(block.getBlockType().getName(), TextColor.YELLOW)); + builder.append(block.getBlockType().getRichName().color(TextColor.YELLOW)); builder.append(TextComponent.of(" (" + block + ") ", TextColor.GRAY) .hoverEvent(HoverEvent.of(HoverEvent.Action.SHOW_TEXT, TranslatableComponent.of("worldedit.tool.info.blockstate.hover")))); final int internalId = BlockStateIdAccess.getBlockStateId(block.toImmutableState()); diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/internal/command/exception/WorldEditExceptionConverter.java b/worldedit-core/src/main/java/com/sk89q/worldedit/internal/command/exception/WorldEditExceptionConverter.java index 549ea1d59..194a67be0 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/internal/command/exception/WorldEditExceptionConverter.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/internal/command/exception/WorldEditExceptionConverter.java @@ -36,6 +36,7 @@ import com.sk89q.worldedit.command.InsufficientArgumentsException; import com.sk89q.worldedit.command.tool.InvalidToolBindException; import com.sk89q.worldedit.internal.expression.ExpressionException; import com.sk89q.worldedit.regions.RegionOperationException; +import com.sk89q.worldedit.util.formatting.text.Component; import com.sk89q.worldedit.util.formatting.text.TextComponent; import com.sk89q.worldedit.util.io.file.FileSelectionAbortedException; import com.sk89q.worldedit.util.io.file.FilenameResolutionException; @@ -62,7 +63,11 @@ public class WorldEditExceptionConverter extends ExceptionConverterHelper { } private CommandException newCommandException(String message, Throwable cause) { - return new CommandException(TextComponent.of(String.valueOf(message)), cause, ImmutableList.of()); + return newCommandException(TextComponent.of(String.valueOf(message)), cause); + } + + private CommandException newCommandException(Component message, Throwable cause) { + return new CommandException(message, cause, ImmutableList.of()); } @ExceptionMatch @@ -158,7 +163,13 @@ public class WorldEditExceptionConverter extends ExceptionConverterHelper { @ExceptionMatch public void convert(InvalidToolBindException e) throws CommandException { - throw newCommandException("Can't bind tool to " + e.getItemType().getName() + ": " + e.getMessage(), e); + throw newCommandException( + TextComponent.builder("Can't bind tool to ") + .append(e.getItemType().getRichName()) + .append(": " + e.getMessage()) + .build(), + e + ); } @ExceptionMatch diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/util/formatting/component/PaginationBox.java b/worldedit-core/src/main/java/com/sk89q/worldedit/util/formatting/component/PaginationBox.java index 9301a0493..f5f96a190 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/util/formatting/component/PaginationBox.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/util/formatting/component/PaginationBox.java @@ -21,17 +21,19 @@ package com.sk89q.worldedit.util.formatting.component; import com.google.common.base.Function; import com.google.common.collect.Collections2; +import com.google.common.collect.ImmutableList; +import com.sk89q.worldedit.history.changeset.ChangeSet; import com.sk89q.worldedit.util.formatting.text.Component; import com.sk89q.worldedit.util.formatting.text.TextComponent; import com.sk89q.worldedit.util.formatting.text.event.ClickEvent; import com.sk89q.worldedit.util.formatting.text.event.HoverEvent; import com.sk89q.worldedit.util.formatting.text.format.TextColor; +import javax.annotation.Nullable; import java.util.Collection; -import java.util.Iterator; import java.util.List; import java.util.function.Supplier; -import javax.annotation.Nullable; +import java.util.stream.Collectors; public abstract class PaginationBox extends MessageBox { @@ -145,46 +147,27 @@ public abstract class PaginationBox extends MessageBox { return new ListPaginationBox(header, pageCommand, lines); } - public static PaginationBox fromStrings(String header, @Nullable String pageCommand, List lines) { - return fromStrings(header, pageCommand, (Collection) lines); + public static PaginationBox fromStrings(String header, @Nullable String pageCommand, Collection lines) { + return fromComponents(header, pageCommand, lines.stream() + .map(TextComponent::of) + .collect(Collectors.toList())); } - public static class ListPaginationBox extends PaginationBox { - private final Collection lines; - private int iterIndex; - private Iterator iterator; + public static PaginationBox fromComponents(String header, @Nullable String pageCommand, Collection lines) { + return new ListPaginationBox(header, pageCommand, lines); + } - public ListPaginationBox(String header, String pageCommand, List lines) { - this(header, pageCommand, (Collection) lines); - } + private static class ListPaginationBox extends PaginationBox { + private final List lines; - public ListPaginationBox(String header, String pageCommand, Collection lines) { + ListPaginationBox(String header, String pageCommand, Collection lines) { super(header, pageCommand); this.lines = lines; } @Override public Component getComponent(int number) { - Object obj; - if (lines instanceof List) { - obj = ((List) lines).get(number); - } else { - if (iterator == null || iterIndex > number) { - iterator = lines.iterator(); - iterIndex = 0; - } - do { - obj = iterator.next(); - iterIndex++; - } while (iterIndex < number); - } - if (obj instanceof Supplier) { - obj = ((Supplier) obj).get(); - } - if (obj instanceof Component) { - return (Component) obj; - } - return TextComponent.of(obj + ""); + return lines.get(number); } @Override diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/world/biome/BiomeData.java b/worldedit-core/src/main/java/com/sk89q/worldedit/world/biome/BiomeData.java index 4d1a09b91..72307720f 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/world/biome/BiomeData.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/world/biome/BiomeData.java @@ -19,6 +19,8 @@ package com.sk89q.worldedit.world.biome; +import com.sk89q.worldedit.world.registry.BiomeRegistry; + /** * Provides information about a biome. * @@ -32,6 +34,8 @@ public interface BiomeData { * particular convention. * * @return the biome's name + * @deprecated This method does not work on the server. + * Use {@link BiomeRegistry#getRichName(BiomeType)}. */ @Deprecated String getName(); diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/world/block/BlockType.java b/worldedit-core/src/main/java/com/sk89q/worldedit/world/block/BlockType.java index 8ccb2b136..3686d8ed9 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/world/block/BlockType.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/world/block/BlockType.java @@ -33,6 +33,7 @@ import com.sk89q.worldedit.registry.state.AbstractProperty; import com.sk89q.worldedit.registry.state.Property; import com.sk89q.worldedit.registry.state.PropertyKey; import com.sk89q.worldedit.util.concurrency.LazyReference; +import com.sk89q.worldedit.util.formatting.text.Component; import com.sk89q.worldedit.world.item.ItemType; import com.sk89q.worldedit.world.item.ItemTypes; import com.sk89q.worldedit.world.registry.BlockMaterial; @@ -96,6 +97,11 @@ public class BlockType implements Keyed, Pattern { return this.id; } + public Component getRichName() { + return WorldEdit.getInstance().getPlatformManager().queryCapability(Capability.GAME_HOOKS) + .getRegistries().getBlockRegistry().getRichName(this); + } + public String getNamespace() { String id = getId(); int i = id.indexOf(':'); @@ -111,15 +117,11 @@ public class BlockType implements Keyed, Pattern { * Gets the name of this block, or the ID if the name cannot be found. * * @return The name, or ID + * @deprecated The name is now translatable, use {@link #getRichName()}. */ @Deprecated public String getName() { - String name = WorldEdit.getInstance().getPlatformManager().queryCapability(Capability.GAME_HOOKS).getRegistries().getBlockRegistry().getName(this); - if (name == null) { - return getId(); - } else { - return name; - } + return getRichName().toString(); } /* @@ -274,9 +276,7 @@ public class BlockType implements Keyed, Pattern { /** * Gets the legacy ID. Needed for legacy reasons. * - *

* DO NOT USE THIS. - *

* * @return legacy id or 0, if unknown */ @@ -286,11 +286,39 @@ public class BlockType implements Keyed, Pattern { return combinedId == null ? 0 : combinedId; } + /** + * Gets the legacy data. Needed for legacy reasons. + * + * DO NOT USE THIS. + * + * @return legacy data or 0, if unknown + */ @Deprecated public int getLegacyId() { return computeLegacy(0); } + /** + * Gets the legacy data. Needed for legacy reasons. + * + *

+ * DO NOT USE THIS. + *

+ * + * @return legacy data or 0, if unknown + */ + @Deprecated + public int getLegacyData() { + return computeLegacy(1); + } + + private int computeLegacy(int index) { + if (this.legacyCombinedId == null) { + this.legacyCombinedId = LegacyMapper.getInstance().getLegacyCombined(this.getDefaultState()); + } + return index == 0 ? legacyCombinedId >> 4 : legacyCombinedId & 15; + } + /** * The internal index of this type. * @@ -336,25 +364,4 @@ public class BlockType implements Keyed, Pattern { public SingleBlockTypeMask toMask(Extent extent) { return new SingleBlockTypeMask(extent, this); } - - /** - * Gets the legacy data. Needed for legacy reasons. - * - *

- * DO NOT USE THIS. - *

- * - * @return legacy data or 0, if unknown - */ - @Deprecated - public int getLegacyData() { - return computeLegacy(1); - } - - private int computeLegacy(int index) { - if (this.legacyCombinedId == null) { - this.legacyCombinedId = LegacyMapper.getInstance().getLegacyCombined(this.getDefaultState()); - } - return index == 0 ? legacyCombinedId >> 4 : legacyCombinedId & 15; - } } diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/world/registry/BiomeRegistry.java b/worldedit-core/src/main/java/com/sk89q/worldedit/world/registry/BiomeRegistry.java index 05e6b075a..3e18c4777 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/world/registry/BiomeRegistry.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/world/registry/BiomeRegistry.java @@ -19,6 +19,7 @@ package com.sk89q.worldedit.world.registry; +import com.sk89q.worldedit.util.formatting.text.Component; import com.sk89q.worldedit.world.biome.BiomeData; import com.sk89q.worldedit.world.biome.BiomeType; @@ -29,11 +30,21 @@ import javax.annotation.Nullable; */ public interface BiomeRegistry { + /** + * Get the name of the biome, usually as a translatable component. + * + * @param biomeType the biome type + * @return the name of the biome + */ + Component getRichName(BiomeType biomeType); + /** * Get data about a biome. * * @param biome the biome * @return a data object or null if information is not known + * @deprecated This method no longer returns any useful information. + * Use {@link #getRichName(BiomeType)} for the name of the biome. */ @Deprecated @Nullable diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/world/registry/BlockRegistry.java b/worldedit-core/src/main/java/com/sk89q/worldedit/world/registry/BlockRegistry.java index de439dcf9..e7bad3d86 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/world/registry/BlockRegistry.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/world/registry/BlockRegistry.java @@ -20,29 +20,41 @@ package com.sk89q.worldedit.world.registry; import com.sk89q.worldedit.registry.state.Property; +import com.sk89q.worldedit.util.formatting.text.Component; import com.sk89q.worldedit.world.block.BlockState; import com.sk89q.worldedit.world.block.BlockType; +import javax.annotation.Nullable; import java.util.Collection; import java.util.Collections; import java.util.Map; import java.util.OptionalInt; -import javax.annotation.Nullable; /** * Provides information on blocks and provides methods to create them. */ public interface BlockRegistry { + /** + * Gets the name for the given block. + * + * @param blockType the block + * @return The name + */ + Component getRichName(BlockType blockType); + /** * Gets the name for the given block. * * @param blockType the block * @return The name, or null if it's unknown + * @deprecated Names are now translatable, use {@link #getRichName(BlockType)}. */ @Deprecated @Nullable - String getName(BlockType blockType); + default String getName(BlockType blockType) { + return getRichName(blockType).toString(); + } /** * Get the material for the given block. diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/world/registry/BundledBlockRegistry.java b/worldedit-core/src/main/java/com/sk89q/worldedit/world/registry/BundledBlockRegistry.java index 435fb21c7..25e0b999b 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/world/registry/BundledBlockRegistry.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/world/registry/BundledBlockRegistry.java @@ -20,6 +20,10 @@ package com.sk89q.worldedit.world.registry; import com.sk89q.worldedit.registry.state.Property; +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 com.sk89q.worldedit.util.translation.TranslationManager; import com.sk89q.worldedit.world.block.BlockState; import com.sk89q.worldedit.world.block.BlockType; @@ -34,11 +38,19 @@ import javax.annotation.Nullable; */ public class BundledBlockRegistry implements BlockRegistry { - @Nullable @Override - public String getName(BlockType blockType) { + public Component getRichName(BlockType blockType) { BundledBlockData.BlockEntry blockEntry = BundledBlockData.getInstance().findById(blockType.getId()); - return blockEntry != null ? blockEntry.localizedName : null; + if (blockEntry != null) { + // This is more likely to be "right", but not translated + // Some vanilla MC blocks have overrides so we need this name here + // Most platforms should be overriding this anyways, so it likely doesn't matter + // too much! + return TextComponent.of(blockEntry.localizedName); + } + return TranslatableComponent.of( + TranslationManager.makeTranslationKey("block", blockType.getId()) + ); } @Nullable diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/world/registry/ItemRegistry.java b/worldedit-core/src/main/java/com/sk89q/worldedit/world/registry/ItemRegistry.java index 274dbf171..7458da4d9 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/world/registry/ItemRegistry.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/world/registry/ItemRegistry.java @@ -23,9 +23,9 @@ import com.sk89q.worldedit.blocks.BaseItemStack; import com.sk89q.worldedit.util.formatting.text.Component; import com.sk89q.worldedit.world.item.ItemType; +import javax.annotation.Nullable; import java.util.Collection; import java.util.Collections; -import javax.annotation.Nullable; public interface ItemRegistry { diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/world/registry/NullBiomeRegistry.java b/worldedit-core/src/main/java/com/sk89q/worldedit/world/registry/NullBiomeRegistry.java index b26e10fda..7951b25a0 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/world/registry/NullBiomeRegistry.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/world/registry/NullBiomeRegistry.java @@ -19,6 +19,9 @@ package com.sk89q.worldedit.world.registry; +import com.sk89q.worldedit.util.formatting.text.Component; +import com.sk89q.worldedit.util.formatting.text.TranslatableComponent; +import com.sk89q.worldedit.util.translation.TranslationManager; import com.sk89q.worldedit.world.biome.BiomeData; import com.sk89q.worldedit.world.biome.BiomeType; @@ -35,6 +38,14 @@ public class NullBiomeRegistry implements BiomeRegistry { public NullBiomeRegistry() { } + @Override + public Component getRichName(BiomeType biomeType) { + return TranslatableComponent.of( + TranslationManager.makeTranslationKey("biome", biomeType.getId()) + ); + } + + @Deprecated @Nullable @Override public BiomeData getData(BiomeType biome) { diff --git a/worldedit-fabric/src/main/java/com/sk89q/worldedit/fabric/FabricBiomeRegistry.java b/worldedit-fabric/src/main/java/com/sk89q/worldedit/fabric/FabricBiomeRegistry.java index ea2c48849..a005feaf8 100644 --- a/worldedit-fabric/src/main/java/com/sk89q/worldedit/fabric/FabricBiomeRegistry.java +++ b/worldedit-fabric/src/main/java/com/sk89q/worldedit/fabric/FabricBiomeRegistry.java @@ -19,6 +19,8 @@ package com.sk89q.worldedit.fabric; +import com.sk89q.worldedit.util.formatting.text.Component; +import com.sk89q.worldedit.util.formatting.text.TranslatableComponent; import com.sk89q.worldedit.world.biome.BiomeData; import com.sk89q.worldedit.world.biome.BiomeType; import com.sk89q.worldedit.world.registry.BiomeRegistry; @@ -29,6 +31,12 @@ import net.minecraft.world.biome.Biome; */ class FabricBiomeRegistry implements BiomeRegistry { + @Override + public Component getRichName(BiomeType biomeType) { + return TranslatableComponent.of(FabricAdapter.adapt(biomeType).getTranslationKey()); + } + + @Deprecated @Override public BiomeData getData(BiomeType biome) { return new FabricBiomeData(FabricAdapter.adapt(biome)); @@ -37,6 +45,7 @@ class FabricBiomeRegistry implements BiomeRegistry { /** * Cached biome data information. */ + @Deprecated private static class FabricBiomeData implements BiomeData { private final Biome biome; @@ -49,6 +58,7 @@ class FabricBiomeRegistry implements BiomeRegistry { this.biome = biome; } + @SuppressWarnings("deprecation") @Override public String getName() { return biome.getName().asFormattedString(); diff --git a/worldedit-fabric/src/main/java/com/sk89q/worldedit/fabric/FabricBlockRegistry.java b/worldedit-fabric/src/main/java/com/sk89q/worldedit/fabric/FabricBlockRegistry.java index c54a27769..1d9e62eae 100644 --- a/worldedit-fabric/src/main/java/com/sk89q/worldedit/fabric/FabricBlockRegistry.java +++ b/worldedit-fabric/src/main/java/com/sk89q/worldedit/fabric/FabricBlockRegistry.java @@ -20,17 +20,15 @@ package com.sk89q.worldedit.fabric; import com.sk89q.worldedit.registry.state.Property; +import com.sk89q.worldedit.util.formatting.text.Component; +import com.sk89q.worldedit.util.formatting.text.TranslatableComponent; import com.sk89q.worldedit.world.block.BlockState; import com.sk89q.worldedit.world.block.BlockType; import com.sk89q.worldedit.world.registry.BlockMaterial; import com.sk89q.worldedit.world.registry.BundledBlockRegistry; -import net.fabricmc.api.EnvType; -import net.fabricmc.loader.api.FabricLoader; import net.minecraft.block.Block; import net.minecraft.block.Material; -import javax.annotation.Nullable; - import java.util.Collection; import java.util.HashMap; import java.util.Map; @@ -41,15 +39,9 @@ public class FabricBlockRegistry extends BundledBlockRegistry { private Map materialMap = new HashMap<>(); - @Nullable @Override - public String getName(BlockType blockType) { - Block block = FabricAdapter.adapt(blockType); - if (FabricLoader.getInstance().getEnvironmentType() == EnvType.CLIENT) { - return block.getName().asFormattedString(); - } else { - return super.getName(blockType); - } + public Component getRichName(BlockType blockType) { + return TranslatableComponent.of(FabricAdapter.adapt(blockType).getTranslationKey()); } @Override diff --git a/worldedit-fabric/src/main/java/com/sk89q/worldedit/fabric/FabricItemRegistry.java b/worldedit-fabric/src/main/java/com/sk89q/worldedit/fabric/FabricItemRegistry.java index 6f19555c6..8218b932f 100644 --- a/worldedit-fabric/src/main/java/com/sk89q/worldedit/fabric/FabricItemRegistry.java +++ b/worldedit-fabric/src/main/java/com/sk89q/worldedit/fabric/FabricItemRegistry.java @@ -19,24 +19,26 @@ package com.sk89q.worldedit.fabric; +import com.sk89q.worldedit.blocks.BaseItemStack; +import com.sk89q.worldedit.util.formatting.text.Component; +import com.sk89q.worldedit.util.formatting.text.TranslatableComponent; import com.sk89q.worldedit.world.item.ItemType; import com.sk89q.worldedit.world.registry.BundledItemRegistry; -import net.fabricmc.api.EnvType; -import net.fabricmc.loader.api.FabricLoader; -import net.minecraft.client.resource.language.I18n; -import net.minecraft.item.Item; - -import javax.annotation.Nullable; public class FabricItemRegistry extends BundledItemRegistry { - @Nullable @Override - public String getName(ItemType itemType) { - if (FabricLoader.getInstance().getEnvironmentType() == EnvType.CLIENT) { - final Item item = FabricAdapter.adapt(itemType); - return I18n.translate(item.getTranslationKey()); - } - return super.getName(itemType); + public Component getRichName(ItemType itemType) { + return TranslatableComponent.of( + FabricAdapter.adapt(itemType).getTranslationKey() + ); } + + @Override + public Component getRichName(BaseItemStack itemStack) { + return TranslatableComponent.of( + FabricAdapter.adapt(itemStack).getTranslationKey() + ); + } + } diff --git a/worldedit-forge/src/main/java/com/sk89q/worldedit/forge/ForgeBiomeRegistry.java b/worldedit-forge/src/main/java/com/sk89q/worldedit/forge/ForgeBiomeRegistry.java index c8fa3f8da..38b24d78b 100644 --- a/worldedit-forge/src/main/java/com/sk89q/worldedit/forge/ForgeBiomeRegistry.java +++ b/worldedit-forge/src/main/java/com/sk89q/worldedit/forge/ForgeBiomeRegistry.java @@ -19,6 +19,8 @@ package com.sk89q.worldedit.forge; +import com.sk89q.worldedit.util.formatting.text.Component; +import com.sk89q.worldedit.util.formatting.text.TranslatableComponent; import com.sk89q.worldedit.world.biome.BiomeData; import com.sk89q.worldedit.world.biome.BiomeType; import com.sk89q.worldedit.world.registry.BiomeRegistry; @@ -29,6 +31,12 @@ import net.minecraft.world.biome.Biome; */ class ForgeBiomeRegistry implements BiomeRegistry { + @Override + public Component getRichName(BiomeType biomeType) { + return TranslatableComponent.of(ForgeAdapter.adapt(biomeType).getTranslationKey()); + } + + @Deprecated @Override public BiomeData getData(BiomeType biome) { return new ForgeBiomeData(ForgeAdapter.adapt(biome)); @@ -37,6 +45,7 @@ class ForgeBiomeRegistry implements BiomeRegistry { /** * Cached biome data information. */ + @Deprecated private static class ForgeBiomeData implements BiomeData { private final Biome biome; @@ -49,6 +58,7 @@ class ForgeBiomeRegistry implements BiomeRegistry { this.biome = biome; } + @SuppressWarnings("deprecation") @Override public String getName() { return biome.getDisplayName().getString(); diff --git a/worldedit-forge/src/main/java/com/sk89q/worldedit/forge/ForgeBlockRegistry.java b/worldedit-forge/src/main/java/com/sk89q/worldedit/forge/ForgeBlockRegistry.java index a6c3f45b9..cdb67bb57 100644 --- a/worldedit-forge/src/main/java/com/sk89q/worldedit/forge/ForgeBlockRegistry.java +++ b/worldedit-forge/src/main/java/com/sk89q/worldedit/forge/ForgeBlockRegistry.java @@ -20,6 +20,8 @@ package com.sk89q.worldedit.forge; import com.sk89q.worldedit.registry.state.Property; +import com.sk89q.worldedit.util.formatting.text.Component; +import com.sk89q.worldedit.util.formatting.text.TranslatableComponent; import com.sk89q.worldedit.world.block.BlockState; import com.sk89q.worldedit.world.block.BlockType; import com.sk89q.worldedit.world.registry.BlockMaterial; @@ -27,9 +29,7 @@ import com.sk89q.worldedit.world.registry.BundledBlockRegistry; import net.minecraft.block.Block; import net.minecraft.block.material.Material; import net.minecraft.state.IProperty; -import net.minecraftforge.fml.loading.FMLLoader; -import javax.annotation.Nullable; import java.util.Collection; import java.util.HashMap; import java.util.Map; @@ -40,15 +40,9 @@ public class ForgeBlockRegistry extends BundledBlockRegistry { private Map materialMap = new HashMap<>(); - @Nullable @Override - public String getName(BlockType blockType) { - Block block = ForgeAdapter.adapt(blockType); - if (block != null && FMLLoader.getDist().isClient()) { - return block.getNameTextComponent().getFormattedText(); - } else { - return super.getName(blockType); - } + public Component getRichName(BlockType blockType) { + return TranslatableComponent.of(ForgeAdapter.adapt(blockType).getTranslationKey()); } @Override diff --git a/worldedit-forge/src/main/java/com/sk89q/worldedit/forge/ForgeItemRegistry.java b/worldedit-forge/src/main/java/com/sk89q/worldedit/forge/ForgeItemRegistry.java index 8bf23c6cb..873eab3cf 100644 --- a/worldedit-forge/src/main/java/com/sk89q/worldedit/forge/ForgeItemRegistry.java +++ b/worldedit-forge/src/main/java/com/sk89q/worldedit/forge/ForgeItemRegistry.java @@ -19,33 +19,26 @@ package com.sk89q.worldedit.forge; -import com.sk89q.worldedit.blocks.BaseItem; -import com.sk89q.worldedit.world.item.ItemTypes; -import com.sk89q.worldedit.world.registry.ItemRegistry; -import net.minecraft.item.Item; -import net.minecraft.util.ResourceLocation; +import com.sk89q.worldedit.blocks.BaseItemStack; +import com.sk89q.worldedit.util.formatting.text.Component; +import com.sk89q.worldedit.util.formatting.text.TranslatableComponent; import com.sk89q.worldedit.world.item.ItemType; import com.sk89q.worldedit.world.registry.BundledItemRegistry; -import javax.annotation.Nullable; -import net.minecraft.client.resources.I18n; -import net.minecraft.item.Item; -import net.minecraft.util.ResourceLocation; -import net.minecraftforge.fml.loading.FMLLoader; -import net.minecraftforge.registries.RegistryManager; - public class ForgeItemRegistry extends BundledItemRegistry { - @Nullable @Override - public String getName(ItemType itemType) { - if (FMLLoader.getDist().isClient()) { - final Item item = RegistryManager.ACTIVE.getRegistry(Item.class) - .getValue(ResourceLocation.tryCreate(itemType.getId())); - if (item != null) { - return I18n.format(item.getTranslationKey()); - } - } - return super.getName(itemType); + public Component getRichName(ItemType itemType) { + return TranslatableComponent.of( + ForgeAdapter.adapt(itemType).getTranslationKey() + ); } + + @Override + public Component getRichName(BaseItemStack itemStack) { + return TranslatableComponent.of( + ForgeAdapter.adapt(itemStack).getTranslationKey() + ); + } + } diff --git a/worldedit-sponge/src/main/java/com/sk89q/worldedit/sponge/SpongeBiomeRegistry.java b/worldedit-sponge/src/main/java/com/sk89q/worldedit/sponge/SpongeBiomeRegistry.java index dba2a904c..fd012d294 100644 --- a/worldedit-sponge/src/main/java/com/sk89q/worldedit/sponge/SpongeBiomeRegistry.java +++ b/worldedit-sponge/src/main/java/com/sk89q/worldedit/sponge/SpongeBiomeRegistry.java @@ -19,6 +19,9 @@ package com.sk89q.worldedit.sponge; +import com.sk89q.worldedit.util.formatting.text.Component; +import com.sk89q.worldedit.util.formatting.text.TranslatableComponent; +import com.sk89q.worldedit.util.translation.TranslationManager; import com.sk89q.worldedit.world.biome.BiomeData; import com.sk89q.worldedit.world.registry.BiomeRegistry; import org.spongepowered.api.world.biome.BiomeType; @@ -30,12 +33,21 @@ import javax.annotation.Nullable; */ class SpongeBiomeRegistry implements BiomeRegistry { + @Override + public Component getRichName(com.sk89q.worldedit.world.biome.BiomeType biomeType) { + return TranslatableComponent.of( + TranslationManager.makeTranslationKey("biome", biomeType.getId()) + ); + } + + @Deprecated @Nullable @Override public BiomeData getData(com.sk89q.worldedit.world.biome.BiomeType biome) { return new SpongeBiomeData(SpongeAdapter.adapt(biome)); } + @Deprecated private static class SpongeBiomeData implements BiomeData { private final BiomeType biome; @@ -48,6 +60,7 @@ class SpongeBiomeRegistry implements BiomeRegistry { this.biome = biome; } + @SuppressWarnings("deprecation") @Override public String getName() { return biome.getName();