From 1eec4884c855176463a9d4c0ec481a1467ff17b9 Mon Sep 17 00:00:00 2001 From: MattBDev <4009945+MattBDev@users.noreply.github.com> Date: Mon, 19 Aug 2019 14:12:28 -0400 Subject: [PATCH] Attempt at fixing NamespacedRegistry --- .../worldedit/bukkit/WorldEditPlugin.java | 23 ++++++++++++++----- .../registry/NamespacedRegistry.java | 11 ++++++--- .../worldedit/world/block/BlockType.java | 6 +++++ .../worldedit/world/block/BlockTypes.java | 5 ++-- 4 files changed, 33 insertions(+), 12 deletions(-) diff --git a/worldedit-bukkit/src/main/java/com/sk89q/worldedit/bukkit/WorldEditPlugin.java b/worldedit-bukkit/src/main/java/com/sk89q/worldedit/bukkit/WorldEditPlugin.java index 59b0217bf..ba29ee0fd 100644 --- a/worldedit-bukkit/src/main/java/com/sk89q/worldedit/bukkit/WorldEditPlugin.java +++ b/worldedit-bukkit/src/main/java/com/sk89q/worldedit/bukkit/WorldEditPlugin.java @@ -37,16 +37,24 @@ import com.sk89q.worldedit.bukkit.adapter.BukkitImplLoader; import com.sk89q.worldedit.event.platform.CommandEvent; import com.sk89q.worldedit.event.platform.CommandSuggestionEvent; import com.sk89q.worldedit.event.platform.PlatformReadyEvent; +import com.sk89q.worldedit.extension.input.InputParseException; +import com.sk89q.worldedit.extension.input.ParserContext; import com.sk89q.worldedit.extension.platform.Actor; import com.sk89q.worldedit.extension.platform.Capability; import com.sk89q.worldedit.extension.platform.Platform; import com.sk89q.worldedit.extent.inventory.BlockBag; import com.sk89q.worldedit.internal.command.CommandUtil; +import com.sk89q.worldedit.registry.state.Property; import com.sk89q.worldedit.world.biome.BiomeType; import com.sk89q.worldedit.world.block.BlockCategory; +import com.sk89q.worldedit.world.block.BlockState; +import com.sk89q.worldedit.world.block.BlockType; +import com.sk89q.worldedit.world.block.FuzzyBlockState; import com.sk89q.worldedit.world.entity.EntityType; +import com.sk89q.worldedit.world.gamemode.GameModes; import com.sk89q.worldedit.world.item.ItemCategory; import com.sk89q.worldedit.world.item.ItemType; +import com.sk89q.worldedit.world.weather.WeatherTypes; import io.papermc.lib.PaperLib; import org.bukkit.Bukkit; import org.bukkit.Material; @@ -214,7 +222,7 @@ public class WorldEditPlugin extends JavaPlugin { //implements TabCompleter PaperLib.suggestPaper(this); } - public void setupRegistries() { + private void setupRegistries() { // Biome for (Biome biome : Biome.values()) { String lowerCaseBiomeName = biome.name().toLowerCase(Locale.ROOT); @@ -259,6 +267,9 @@ public class WorldEditPlugin extends JavaPlugin { //implements TabCompleter EntityType.REGISTRY.register("minecraft:" + lowerCaseMcId, new EntityType("minecraft:" + lowerCaseMcId)); } } + // ... :| + GameModes.get(""); + WeatherTypes.get(""); } private void setupTags() { @@ -428,7 +439,7 @@ public class WorldEditPlugin extends JavaPlugin { //implements TabCompleter // code of WorldEdit expects it String[] split = new String[args.length + 1]; System.arraycopy(args, 0, split, 1, args.length); - split[0] = "/" + cmd.getName(); + split[0] = "/" + commandLabel; CommandEvent event = new CommandEvent(wrapCommandSender(sender), Joiner.on(" ").join(split)); getWorldEdit().getEventBus().post(event); @@ -443,7 +454,7 @@ public class WorldEditPlugin extends JavaPlugin { //implements TabCompleter // code of WorldEdit expects it String[] split = new String[args.length + 1]; System.arraycopy(args, 0, split, 1, args.length); - split[0] = "/" + cmd.getName(); + split[0] = "/" + commandLabel; String arguments = Joiner.on(" ").join(split); CommandSuggestionEvent event = new CommandSuggestionEvent(wrapCommandSender(sender), arguments); @@ -577,9 +588,9 @@ public class WorldEditPlugin extends JavaPlugin { //implements TabCompleter if (!event.isCommand()) return; String buffer = event.getBuffer(); - final String[] parts = buffer.split(" "); - if (parts.length < 1) return; - final String label = parts[0]; + int firstSpace = buffer.indexOf(' '); + if (firstSpace < 0) return; + final String label = buffer.substring(0, firstSpace); final Optional command = WorldEdit.getInstance().getPlatformManager().getPlatformCommandManager().getCommandManager().getCommand(label); if (!command.isPresent()) return; diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/registry/NamespacedRegistry.java b/worldedit-core/src/main/java/com/sk89q/worldedit/registry/NamespacedRegistry.java index 6c86fdb0c..ea72d9f69 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/registry/NamespacedRegistry.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/registry/NamespacedRegistry.java @@ -29,7 +29,7 @@ import java.util.List; import java.util.Set; import javax.annotation.Nullable; -public final class NamespacedRegistry extends Registry { +public final class NamespacedRegistry extends Registry { private static final String MINECRAFT_NAMESPACE = "minecraft"; private final Set knownNamespaces = new HashSet<>(); private final String defaultNamespace; @@ -60,7 +60,9 @@ public final class NamespacedRegistry extends Re if (existing != null) { throw new UnsupportedOperationException("Replacing existing registrations is not supported"); } - value.setInternalId(lastInternalId++); + if (value instanceof RegistryItem) { + ((RegistryItem)value).setInternalId(lastInternalId++); + } values.add(value); super.register(key, value); if (key.startsWith(defaultNamespace)) { @@ -78,7 +80,10 @@ public final class NamespacedRegistry extends Re } public int getInternalId(V value) { - return value.getInternalId(); + if (value instanceof RegistryItem) { + return ((RegistryItem)value).getInternalId(); + } + return 0; } public int size() { 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 0465bc21d..2b6b1c155 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 @@ -29,6 +29,7 @@ import com.sk89q.worldedit.function.mask.SingleBlockTypeMask; import com.sk89q.worldedit.function.pattern.FawePattern; import com.sk89q.worldedit.math.BlockVector3; import com.sk89q.worldedit.registry.Keyed; +import com.sk89q.worldedit.registry.NamespacedRegistry; import com.sk89q.worldedit.registry.state.AbstractProperty; import com.sk89q.worldedit.registry.state.Property; import com.sk89q.worldedit.registry.state.PropertyKey; @@ -36,15 +37,20 @@ import com.sk89q.worldedit.world.item.ItemType; import com.sk89q.worldedit.world.item.ItemTypes; import com.sk89q.worldedit.world.registry.BlockMaterial; import com.sk89q.worldedit.world.registry.LegacyMapper; + import java.util.Collections; import java.util.List; import java.util.Map; import java.util.Set; +import java.util.function.Function; import java.util.stream.Collectors; import java.util.stream.IntStream; import javax.annotation.Nullable; public class BlockType implements FawePattern, Keyed { + + public static final NamespacedRegistry REGISTRY = new NamespacedRegistry<>("block type"); + private final String id; private final BlockTypes.Settings settings; diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/world/block/BlockTypes.java b/worldedit-core/src/main/java/com/sk89q/worldedit/world/block/BlockTypes.java index 2abc4d052..215355dbe 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/world/block/BlockTypes.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/world/block/BlockTypes.java @@ -954,15 +954,14 @@ public final class BlockTypes { try { Field field = BlockTypes.class.getDeclaredField(enumName); ReflectionUtils.setFailsafeFieldValue(field, null, existing); - } catch (NoSuchFieldException e) { - e.printStackTrace(); - } catch (IllegalAccessException e) { + } catch (NoSuchFieldException | IllegalAccessException e) { e.printStackTrace(); } // register states if (typeName.startsWith("minecraft:")) $REGISTRY.put(typeName.substring(10), existing); $REGISTRY.put(typeName, existing); + BlockType.REGISTRY.register(typeName,existing); String nameSpace = typeName.substring(0, typeName.indexOf(':')); $NAMESPACES.add(nameSpace); return existing;