From 5e81dd1c4ccb5853a34d89b2b030b2745b8b37cc Mon Sep 17 00:00:00 2001 From: wizjany Date: Wed, 1 May 2019 19:59:02 -0400 Subject: [PATCH] Make registries Keyed. --- .../command/argument/RegistryConverter.java | 11 ++++--- .../sk89q/worldedit/registry/Category.java | 2 +- .../com/sk89q/worldedit/registry/Keyed.java | 32 +++++++++++++++++++ .../registry/NamespacedRegistry.java | 7 ++-- .../sk89q/worldedit/registry/Registry.java | 5 +-- .../formatting/component/PaginationBox.java | 6 ++++ .../worldedit/world/biome/BiomeType.java | 4 ++- .../worldedit/world/block/BlockCategory.java | 3 +- .../worldedit/world/block/BlockType.java | 4 ++- .../worldedit/world/entity/EntityType.java | 4 ++- .../worldedit/world/fluid/FluidCategory.java | 3 +- .../worldedit/world/fluid/FluidType.java | 4 ++- .../worldedit/world/gamemode/GameMode.java | 4 ++- .../worldedit/world/gamemode/GameModes.java | 4 +-- .../worldedit/world/item/ItemCategory.java | 3 +- .../sk89q/worldedit/world/item/ItemType.java | 4 ++- .../world/registry/CategoryRegistry.java | 3 +- .../worldedit/world/weather/WeatherType.java | 4 ++- 18 files changed, 84 insertions(+), 23 deletions(-) create mode 100644 worldedit-core/src/main/java/com/sk89q/worldedit/registry/Keyed.java diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/command/argument/RegistryConverter.java b/worldedit-core/src/main/java/com/sk89q/worldedit/command/argument/RegistryConverter.java index 7bafb86e9..a37c5b174 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/command/argument/RegistryConverter.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/command/argument/RegistryConverter.java @@ -20,6 +20,7 @@ package com.sk89q.worldedit.command.argument; import com.google.common.collect.ImmutableList; +import com.sk89q.worldedit.registry.Keyed; import com.sk89q.worldedit.registry.Registry; import com.sk89q.worldedit.util.formatting.text.Component; import com.sk89q.worldedit.util.formatting.text.TextComponent; @@ -46,7 +47,7 @@ import java.util.List; import static org.enginehub.piston.converter.SuggestionHelper.limitByPrefix; -public class RegistryConverter implements ArgumentConverter { +public final class RegistryConverter implements ArgumentConverter { @SuppressWarnings("unchecked") public static void register(CommandManager commandManager) { @@ -62,18 +63,18 @@ public class RegistryConverter implements ArgumentConverter { GameMode.class, WeatherType.class ).stream() - .map(c -> (Class) c) + .map(c -> (Class) c) .forEach(registryType -> commandManager.registerConverter(Key.of(registryType), from(registryType)) ); } @SuppressWarnings("unchecked") - private static RegistryConverter from(Class registryType) { + private static RegistryConverter from(Class registryType) { try { Field registryField = registryType.getDeclaredField("REGISTRY"); Registry registry = (Registry) registryField.get(null); - return new RegistryConverter<>(registryType, registry); + return new RegistryConverter<>(registry); } catch (NoSuchFieldException e) { throw new IllegalArgumentException("Not a registry-backed type: " + registryType.getName()); } catch (IllegalAccessException e) { @@ -84,7 +85,7 @@ public class RegistryConverter implements ArgumentConverter { private final Registry registry; private final TextComponent choices; - private RegistryConverter(Class clazz, Registry registry) { + private RegistryConverter(Registry registry) { this.registry = registry; this.choices = TextComponent.of("any " + registry.getName()); } diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/registry/Category.java b/worldedit-core/src/main/java/com/sk89q/worldedit/registry/Category.java index 0f7f256a8..e3c0941d8 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/registry/Category.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/registry/Category.java @@ -22,7 +22,7 @@ package com.sk89q.worldedit.registry; import java.util.HashSet; import java.util.Set; -public abstract class Category { +public abstract class Category { private final Set set = new HashSet<>(); protected final String id; private boolean empty = true; diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/registry/Keyed.java b/worldedit-core/src/main/java/com/sk89q/worldedit/registry/Keyed.java new file mode 100644 index 000000000..5351ed36b --- /dev/null +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/registry/Keyed.java @@ -0,0 +1,32 @@ +/* + * WorldEdit, a Minecraft world manipulation toolkit + * Copyright (C) sk89q + * Copyright (C) WorldEdit team and contributors + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as published by the + * Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License + * for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see . + */ + + +package com.sk89q.worldedit.registry; + +/** + * Represents an objects that can be added to a registry and referenced by an id which is unique within its registry. + */ +public interface Keyed { + /** + * The id of this object in the registry. Must be unique, and lowercase. Certain registries (e.g Namespaced ones) may have additional restrictions. + * @return an id + */ + String getId(); +} 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 d3b151f6e..4519bbc88 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 @@ -24,7 +24,7 @@ import static java.util.Objects.requireNonNull; 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 String defaultNamespace; @@ -37,10 +37,13 @@ public final class NamespacedRegistry extends Registry { this.defaultNamespace = defaultNamespace; } - public @Nullable V get(final String key) { + @Nullable + @Override + public V get(final String key) { return super.get(this.orDefaultNamespace(key)); } + @Override public V register(final String key, final V value) { requireNonNull(key, "key"); checkState(key.indexOf(':') > -1, "key is not namespaced"); diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/registry/Registry.java b/worldedit-core/src/main/java/com/sk89q/worldedit/registry/Registry.java index 8d8bf46c4..fcd673eb4 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/registry/Registry.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/registry/Registry.java @@ -32,7 +32,7 @@ import java.util.Set; import javax.annotation.Nullable; -public class Registry implements Iterable { +public class Registry implements Iterable { private final Map map = new HashMap<>(); private final String name; @@ -44,7 +44,8 @@ public class Registry implements Iterable { return name; } - public @Nullable V get(final String key) { + @Nullable + public V get(final String key) { checkState(key.equals(key.toLowerCase(Locale.ROOT)), "key must be lowercase"); return this.map.get(key); } 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 bba74a4db..6d33d1a5f 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 @@ -34,6 +34,7 @@ public abstract class PaginationBox extends MessageBox { private String pageCommand; private int componentsPerPage = IDEAL_ROWS_FOR_PLAYER; + private int currentPage = -1; /** * Creates a Paginated component @@ -57,6 +58,10 @@ public abstract class PaginationBox extends MessageBox { this.componentsPerPage = 20; } + protected final int getCurrentPage() { + return currentPage; + } + /** * Creates a Paginated component * @@ -80,6 +85,7 @@ public abstract class PaginationBox extends MessageBox { if (page < 1 || page > pageCount) { throw new InvalidComponentException("Invalid page number."); } + currentPage = page; final int lastComp = Math.min(page * componentsPerPage, getComponentsSize()); for (int i = (page - 1) * componentsPerPage; i < lastComp; i++) { getContents().append(getComponent(i)); diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/world/biome/BiomeType.java b/worldedit-core/src/main/java/com/sk89q/worldedit/world/biome/BiomeType.java index 7dc155253..fe9203c70 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/world/biome/BiomeType.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/world/biome/BiomeType.java @@ -19,12 +19,13 @@ package com.sk89q.worldedit.world.biome; +import com.sk89q.worldedit.registry.Keyed; import com.sk89q.worldedit.registry.NamespacedRegistry; /** * All the types of biomes in the game. */ -public class BiomeType { +public class BiomeType implements Keyed { public static final NamespacedRegistry REGISTRY = new NamespacedRegistry<>("biome type"); @@ -39,6 +40,7 @@ public class BiomeType { * * @return The id */ + @Override public String getId() { return this.id; } diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/world/block/BlockCategory.java b/worldedit-core/src/main/java/com/sk89q/worldedit/world/block/BlockCategory.java index 161b1c304..19a1cbc82 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/world/block/BlockCategory.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/world/block/BlockCategory.java @@ -22,6 +22,7 @@ package com.sk89q.worldedit.world.block; import com.sk89q.worldedit.WorldEdit; import com.sk89q.worldedit.extension.platform.Capability; import com.sk89q.worldedit.registry.Category; +import com.sk89q.worldedit.registry.Keyed; import com.sk89q.worldedit.registry.NamespacedRegistry; import java.util.Set; @@ -30,7 +31,7 @@ import java.util.Set; * A category of blocks. This is due to the splitting up of * blocks such as wool into separate ids. */ -public class BlockCategory extends Category { +public class BlockCategory extends Category implements Keyed { public static final NamespacedRegistry REGISTRY = new NamespacedRegistry<>("block tag"); 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 633f00a3f..e86ee7e83 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 @@ -25,6 +25,7 @@ import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; import com.sk89q.worldedit.WorldEdit; import com.sk89q.worldedit.extension.platform.Capability; +import com.sk89q.worldedit.registry.Keyed; import com.sk89q.worldedit.registry.NamespacedRegistry; import com.sk89q.worldedit.registry.state.Property; import com.sk89q.worldedit.world.item.ItemType; @@ -41,7 +42,7 @@ import java.util.function.Supplier; import javax.annotation.Nullable; -public class BlockType { +public class BlockType implements Keyed { public static final NamespacedRegistry REGISTRY = new NamespacedRegistry<>("block type"); @@ -91,6 +92,7 @@ public class BlockType { * * @return The id */ + @Override public String getId() { return this.id; } diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/world/entity/EntityType.java b/worldedit-core/src/main/java/com/sk89q/worldedit/world/entity/EntityType.java index 71779e953..245bfb2e5 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/world/entity/EntityType.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/world/entity/EntityType.java @@ -19,9 +19,10 @@ package com.sk89q.worldedit.world.entity; +import com.sk89q.worldedit.registry.Keyed; import com.sk89q.worldedit.registry.NamespacedRegistry; -public class EntityType { +public class EntityType implements Keyed { public static final NamespacedRegistry REGISTRY = new NamespacedRegistry<>("entity type"); @@ -35,6 +36,7 @@ public class EntityType { this.id = id; } + @Override public String getId() { return this.id; } diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/world/fluid/FluidCategory.java b/worldedit-core/src/main/java/com/sk89q/worldedit/world/fluid/FluidCategory.java index 52685cb56..721c22aad 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/world/fluid/FluidCategory.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/world/fluid/FluidCategory.java @@ -20,6 +20,7 @@ package com.sk89q.worldedit.world.fluid; import com.sk89q.worldedit.registry.Category; +import com.sk89q.worldedit.registry.Keyed; import com.sk89q.worldedit.registry.NamespacedRegistry; import java.util.Collections; @@ -29,7 +30,7 @@ import java.util.Set; * A category of fluids. This is due to the splitting up of * blocks such as wool into separate ids. */ -public class FluidCategory extends Category { +public class FluidCategory extends Category implements Keyed { public static final NamespacedRegistry REGISTRY = new NamespacedRegistry<>("fluid tag"); diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/world/fluid/FluidType.java b/worldedit-core/src/main/java/com/sk89q/worldedit/world/fluid/FluidType.java index 5b94015e1..b5e137ee6 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/world/fluid/FluidType.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/world/fluid/FluidType.java @@ -19,13 +19,14 @@ package com.sk89q.worldedit.world.fluid; +import com.sk89q.worldedit.registry.Keyed; import com.sk89q.worldedit.registry.NamespacedRegistry; /** * Minecraft now has a 'fluid' system. This is a * stub class to represent what it may be in the future. */ -public class FluidType { +public class FluidType implements Keyed { public static final NamespacedRegistry REGISTRY = new NamespacedRegistry<>("fluid type"); @@ -40,6 +41,7 @@ public class FluidType { * * @return The id */ + @Override public String getId() { return this.id; } diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/world/gamemode/GameMode.java b/worldedit-core/src/main/java/com/sk89q/worldedit/world/gamemode/GameMode.java index 9cfd4b111..b64d960de 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/world/gamemode/GameMode.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/world/gamemode/GameMode.java @@ -19,9 +19,10 @@ package com.sk89q.worldedit.world.gamemode; +import com.sk89q.worldedit.registry.Keyed; import com.sk89q.worldedit.registry.Registry; -public class GameMode { +public class GameMode implements Keyed { public static final Registry REGISTRY = new Registry<>("game mode"); @@ -31,6 +32,7 @@ public class GameMode { this.id = id; } + @Override public String getId() { return this.id; } diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/world/gamemode/GameModes.java b/worldedit-core/src/main/java/com/sk89q/worldedit/world/gamemode/GameModes.java index ab5b9fc69..0e5673221 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/world/gamemode/GameModes.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/world/gamemode/GameModes.java @@ -23,7 +23,6 @@ import javax.annotation.Nullable; public class GameModes { - public static final GameMode NOT_SET = register(""); public static final GameMode SURVIVAL = register("survival"); public static final GameMode CREATIVE = register("creative"); public static final GameMode ADVENTURE = register("adventure"); @@ -40,7 +39,8 @@ public class GameModes { return GameMode.REGISTRY.register(gameMode.getId(), gameMode); } - public static @Nullable GameMode get(final String id) { + @Nullable + public static GameMode get(final String id) { return GameMode.REGISTRY.get(id); } diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/world/item/ItemCategory.java b/worldedit-core/src/main/java/com/sk89q/worldedit/world/item/ItemCategory.java index c5efdb4b9..8858dc9f9 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/world/item/ItemCategory.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/world/item/ItemCategory.java @@ -23,6 +23,7 @@ import com.sk89q.worldedit.WorldEdit; import com.sk89q.worldedit.blocks.BaseItem; import com.sk89q.worldedit.extension.platform.Capability; import com.sk89q.worldedit.registry.Category; +import com.sk89q.worldedit.registry.Keyed; import com.sk89q.worldedit.registry.NamespacedRegistry; import java.util.Set; @@ -31,7 +32,7 @@ import java.util.Set; * A category of items. This is due to the splitting up of * items such as wool into separate ids. */ -public class ItemCategory extends Category { +public class ItemCategory extends Category implements Keyed { public static final NamespacedRegistry REGISTRY = new NamespacedRegistry<>("item tag"); diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/world/item/ItemType.java b/worldedit-core/src/main/java/com/sk89q/worldedit/world/item/ItemType.java index ab151729f..6654d5dec 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/world/item/ItemType.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/world/item/ItemType.java @@ -21,13 +21,14 @@ package com.sk89q.worldedit.world.item; import com.sk89q.worldedit.WorldEdit; import com.sk89q.worldedit.extension.platform.Capability; +import com.sk89q.worldedit.registry.Keyed; import com.sk89q.worldedit.registry.NamespacedRegistry; import com.sk89q.worldedit.world.block.BlockType; import com.sk89q.worldedit.world.block.BlockTypes; import javax.annotation.Nullable; -public class ItemType { +public class ItemType implements Keyed { public static final NamespacedRegistry REGISTRY = new NamespacedRegistry<>("item type"); @@ -41,6 +42,7 @@ public class ItemType { this.id = id; } + @Override public String getId() { return this.id; } diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/world/registry/CategoryRegistry.java b/worldedit-core/src/main/java/com/sk89q/worldedit/world/registry/CategoryRegistry.java index 496893821..0de14a17c 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/world/registry/CategoryRegistry.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/world/registry/CategoryRegistry.java @@ -20,13 +20,14 @@ package com.sk89q.worldedit.world.registry; import com.sk89q.worldedit.registry.Category; +import com.sk89q.worldedit.registry.Keyed; import java.util.Set; /** * A registry of categories. Minecraft internally calls these 'Tags'. */ -public interface CategoryRegistry { +public interface CategoryRegistry { /** * Gets a set of values with a given category. diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/world/weather/WeatherType.java b/worldedit-core/src/main/java/com/sk89q/worldedit/world/weather/WeatherType.java index c44392df0..43f689d8c 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/world/weather/WeatherType.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/world/weather/WeatherType.java @@ -19,9 +19,10 @@ package com.sk89q.worldedit.world.weather; +import com.sk89q.worldedit.registry.Keyed; import com.sk89q.worldedit.registry.Registry; -public class WeatherType { +public class WeatherType implements Keyed { public static final Registry REGISTRY = new Registry<>("weather type"); @@ -31,6 +32,7 @@ public class WeatherType { this.id = id; } + @Override public String getId() { return this.id; }