Make registries Keyed.

Dieser Commit ist enthalten in:
wizjany 2019-05-01 19:59:02 -04:00
Ursprung 5781b4cd76
Commit 5e81dd1c4c
18 geänderte Dateien mit 84 neuen und 23 gelöschten Zeilen

Datei anzeigen

@ -20,6 +20,7 @@
package com.sk89q.worldedit.command.argument; package com.sk89q.worldedit.command.argument;
import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList;
import com.sk89q.worldedit.registry.Keyed;
import com.sk89q.worldedit.registry.Registry; import com.sk89q.worldedit.registry.Registry;
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.TextComponent;
@ -46,7 +47,7 @@ import java.util.List;
import static org.enginehub.piston.converter.SuggestionHelper.limitByPrefix; import static org.enginehub.piston.converter.SuggestionHelper.limitByPrefix;
public class RegistryConverter<V> implements ArgumentConverter<V> { public final class RegistryConverter<V extends Keyed> implements ArgumentConverter<V> {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public static void register(CommandManager commandManager) { public static void register(CommandManager commandManager) {
@ -62,18 +63,18 @@ public class RegistryConverter<V> implements ArgumentConverter<V> {
GameMode.class, GameMode.class,
WeatherType.class WeatherType.class
).stream() ).stream()
.map(c -> (Class<Object>) c) .map(c -> (Class<Keyed>) c)
.forEach(registryType -> .forEach(registryType ->
commandManager.registerConverter(Key.of(registryType), from(registryType)) commandManager.registerConverter(Key.of(registryType), from(registryType))
); );
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
private static <V> RegistryConverter<V> from(Class<V> registryType) { private static <V extends Keyed> RegistryConverter<V> from(Class<Keyed> registryType) {
try { try {
Field registryField = registryType.getDeclaredField("REGISTRY"); Field registryField = registryType.getDeclaredField("REGISTRY");
Registry<V> registry = (Registry<V>) registryField.get(null); Registry<V> registry = (Registry<V>) registryField.get(null);
return new RegistryConverter<>(registryType, registry); return new RegistryConverter<>(registry);
} catch (NoSuchFieldException e) { } catch (NoSuchFieldException e) {
throw new IllegalArgumentException("Not a registry-backed type: " + registryType.getName()); throw new IllegalArgumentException("Not a registry-backed type: " + registryType.getName());
} catch (IllegalAccessException e) { } catch (IllegalAccessException e) {
@ -84,7 +85,7 @@ public class RegistryConverter<V> implements ArgumentConverter<V> {
private final Registry<V> registry; private final Registry<V> registry;
private final TextComponent choices; private final TextComponent choices;
private RegistryConverter(Class<V> clazz, Registry<V> registry) { private RegistryConverter(Registry<V> registry) {
this.registry = registry; this.registry = registry;
this.choices = TextComponent.of("any " + registry.getName()); this.choices = TextComponent.of("any " + registry.getName());
} }

Datei anzeigen

@ -22,7 +22,7 @@ package com.sk89q.worldedit.registry;
import java.util.HashSet; import java.util.HashSet;
import java.util.Set; import java.util.Set;
public abstract class Category<T> { public abstract class Category<T extends Keyed> {
private final Set<T> set = new HashSet<>(); private final Set<T> set = new HashSet<>();
protected final String id; protected final String id;
private boolean empty = true; private boolean empty = true;

Datei anzeigen

@ -0,0 +1,32 @@
/*
* WorldEdit, a Minecraft world manipulation toolkit
* Copyright (C) sk89q <http://www.sk89q.com>
* 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 <http://www.gnu.org/licenses/>.
*/
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();
}

Datei anzeigen

@ -24,7 +24,7 @@ import static java.util.Objects.requireNonNull;
import javax.annotation.Nullable; import javax.annotation.Nullable;
public final class NamespacedRegistry<V> extends Registry<V> { public final class NamespacedRegistry<V extends Keyed> extends Registry<V> {
private static final String MINECRAFT_NAMESPACE = "minecraft"; private static final String MINECRAFT_NAMESPACE = "minecraft";
private final String defaultNamespace; private final String defaultNamespace;
@ -37,10 +37,13 @@ public final class NamespacedRegistry<V> extends Registry<V> {
this.defaultNamespace = defaultNamespace; this.defaultNamespace = defaultNamespace;
} }
public @Nullable V get(final String key) { @Nullable
@Override
public V get(final String key) {
return super.get(this.orDefaultNamespace(key)); return super.get(this.orDefaultNamespace(key));
} }
@Override
public V register(final String key, final V value) { public V register(final String key, final V value) {
requireNonNull(key, "key"); requireNonNull(key, "key");
checkState(key.indexOf(':') > -1, "key is not namespaced"); checkState(key.indexOf(':') > -1, "key is not namespaced");

Datei anzeigen

@ -32,7 +32,7 @@ import java.util.Set;
import javax.annotation.Nullable; import javax.annotation.Nullable;
public class Registry<V> implements Iterable<V> { public class Registry<V extends Keyed> implements Iterable<V> {
private final Map<String, V> map = new HashMap<>(); private final Map<String, V> map = new HashMap<>();
private final String name; private final String name;
@ -44,7 +44,8 @@ public class Registry<V> implements Iterable<V> {
return name; 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"); checkState(key.equals(key.toLowerCase(Locale.ROOT)), "key must be lowercase");
return this.map.get(key); return this.map.get(key);
} }

Datei anzeigen

@ -34,6 +34,7 @@ public abstract class PaginationBox extends MessageBox {
private String pageCommand; private String pageCommand;
private int componentsPerPage = IDEAL_ROWS_FOR_PLAYER; private int componentsPerPage = IDEAL_ROWS_FOR_PLAYER;
private int currentPage = -1;
/** /**
* Creates a Paginated component * Creates a Paginated component
@ -57,6 +58,10 @@ public abstract class PaginationBox extends MessageBox {
this.componentsPerPage = 20; this.componentsPerPage = 20;
} }
protected final int getCurrentPage() {
return currentPage;
}
/** /**
* Creates a Paginated component * Creates a Paginated component
* *
@ -80,6 +85,7 @@ public abstract class PaginationBox extends MessageBox {
if (page < 1 || page > pageCount) { if (page < 1 || page > pageCount) {
throw new InvalidComponentException("Invalid page number."); throw new InvalidComponentException("Invalid page number.");
} }
currentPage = page;
final int lastComp = Math.min(page * componentsPerPage, getComponentsSize()); final int lastComp = Math.min(page * componentsPerPage, getComponentsSize());
for (int i = (page - 1) * componentsPerPage; i < lastComp; i++) { for (int i = (page - 1) * componentsPerPage; i < lastComp; i++) {
getContents().append(getComponent(i)); getContents().append(getComponent(i));

Datei anzeigen

@ -19,12 +19,13 @@
package com.sk89q.worldedit.world.biome; package com.sk89q.worldedit.world.biome;
import com.sk89q.worldedit.registry.Keyed;
import com.sk89q.worldedit.registry.NamespacedRegistry; import com.sk89q.worldedit.registry.NamespacedRegistry;
/** /**
* All the types of biomes in the game. * All the types of biomes in the game.
*/ */
public class BiomeType { public class BiomeType implements Keyed {
public static final NamespacedRegistry<BiomeType> REGISTRY = new NamespacedRegistry<>("biome type"); public static final NamespacedRegistry<BiomeType> REGISTRY = new NamespacedRegistry<>("biome type");
@ -39,6 +40,7 @@ public class BiomeType {
* *
* @return The id * @return The id
*/ */
@Override
public String getId() { public String getId() {
return this.id; return this.id;
} }

Datei anzeigen

@ -22,6 +22,7 @@ package com.sk89q.worldedit.world.block;
import com.sk89q.worldedit.WorldEdit; import com.sk89q.worldedit.WorldEdit;
import com.sk89q.worldedit.extension.platform.Capability; import com.sk89q.worldedit.extension.platform.Capability;
import com.sk89q.worldedit.registry.Category; import com.sk89q.worldedit.registry.Category;
import com.sk89q.worldedit.registry.Keyed;
import com.sk89q.worldedit.registry.NamespacedRegistry; import com.sk89q.worldedit.registry.NamespacedRegistry;
import java.util.Set; import java.util.Set;
@ -30,7 +31,7 @@ import java.util.Set;
* A category of blocks. This is due to the splitting up of * A category of blocks. This is due to the splitting up of
* blocks such as wool into separate ids. * blocks such as wool into separate ids.
*/ */
public class BlockCategory extends Category<BlockType> { public class BlockCategory extends Category<BlockType> implements Keyed {
public static final NamespacedRegistry<BlockCategory> REGISTRY = new NamespacedRegistry<>("block tag"); public static final NamespacedRegistry<BlockCategory> REGISTRY = new NamespacedRegistry<>("block tag");

Datei anzeigen

@ -25,6 +25,7 @@ import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableMap;
import com.sk89q.worldedit.WorldEdit; import com.sk89q.worldedit.WorldEdit;
import com.sk89q.worldedit.extension.platform.Capability; import com.sk89q.worldedit.extension.platform.Capability;
import com.sk89q.worldedit.registry.Keyed;
import com.sk89q.worldedit.registry.NamespacedRegistry; import com.sk89q.worldedit.registry.NamespacedRegistry;
import com.sk89q.worldedit.registry.state.Property; import com.sk89q.worldedit.registry.state.Property;
import com.sk89q.worldedit.world.item.ItemType; import com.sk89q.worldedit.world.item.ItemType;
@ -41,7 +42,7 @@ import java.util.function.Supplier;
import javax.annotation.Nullable; import javax.annotation.Nullable;
public class BlockType { public class BlockType implements Keyed {
public static final NamespacedRegistry<BlockType> REGISTRY = new NamespacedRegistry<>("block type"); public static final NamespacedRegistry<BlockType> REGISTRY = new NamespacedRegistry<>("block type");
@ -91,6 +92,7 @@ public class BlockType {
* *
* @return The id * @return The id
*/ */
@Override
public String getId() { public String getId() {
return this.id; return this.id;
} }

Datei anzeigen

@ -19,9 +19,10 @@
package com.sk89q.worldedit.world.entity; package com.sk89q.worldedit.world.entity;
import com.sk89q.worldedit.registry.Keyed;
import com.sk89q.worldedit.registry.NamespacedRegistry; import com.sk89q.worldedit.registry.NamespacedRegistry;
public class EntityType { public class EntityType implements Keyed {
public static final NamespacedRegistry<EntityType> REGISTRY = new NamespacedRegistry<>("entity type"); public static final NamespacedRegistry<EntityType> REGISTRY = new NamespacedRegistry<>("entity type");
@ -35,6 +36,7 @@ public class EntityType {
this.id = id; this.id = id;
} }
@Override
public String getId() { public String getId() {
return this.id; return this.id;
} }

Datei anzeigen

@ -20,6 +20,7 @@
package com.sk89q.worldedit.world.fluid; package com.sk89q.worldedit.world.fluid;
import com.sk89q.worldedit.registry.Category; import com.sk89q.worldedit.registry.Category;
import com.sk89q.worldedit.registry.Keyed;
import com.sk89q.worldedit.registry.NamespacedRegistry; import com.sk89q.worldedit.registry.NamespacedRegistry;
import java.util.Collections; import java.util.Collections;
@ -29,7 +30,7 @@ import java.util.Set;
* A category of fluids. This is due to the splitting up of * A category of fluids. This is due to the splitting up of
* blocks such as wool into separate ids. * blocks such as wool into separate ids.
*/ */
public class FluidCategory extends Category<FluidType> { public class FluidCategory extends Category<FluidType> implements Keyed {
public static final NamespacedRegistry<FluidCategory> REGISTRY = new NamespacedRegistry<>("fluid tag"); public static final NamespacedRegistry<FluidCategory> REGISTRY = new NamespacedRegistry<>("fluid tag");

Datei anzeigen

@ -19,13 +19,14 @@
package com.sk89q.worldedit.world.fluid; package com.sk89q.worldedit.world.fluid;
import com.sk89q.worldedit.registry.Keyed;
import com.sk89q.worldedit.registry.NamespacedRegistry; import com.sk89q.worldedit.registry.NamespacedRegistry;
/** /**
* Minecraft now has a 'fluid' system. This is a * Minecraft now has a 'fluid' system. This is a
* stub class to represent what it may be in the future. * stub class to represent what it may be in the future.
*/ */
public class FluidType { public class FluidType implements Keyed {
public static final NamespacedRegistry<FluidType> REGISTRY = new NamespacedRegistry<>("fluid type"); public static final NamespacedRegistry<FluidType> REGISTRY = new NamespacedRegistry<>("fluid type");
@ -40,6 +41,7 @@ public class FluidType {
* *
* @return The id * @return The id
*/ */
@Override
public String getId() { public String getId() {
return this.id; return this.id;
} }

Datei anzeigen

@ -19,9 +19,10 @@
package com.sk89q.worldedit.world.gamemode; package com.sk89q.worldedit.world.gamemode;
import com.sk89q.worldedit.registry.Keyed;
import com.sk89q.worldedit.registry.Registry; import com.sk89q.worldedit.registry.Registry;
public class GameMode { public class GameMode implements Keyed {
public static final Registry<GameMode> REGISTRY = new Registry<>("game mode"); public static final Registry<GameMode> REGISTRY = new Registry<>("game mode");
@ -31,6 +32,7 @@ public class GameMode {
this.id = id; this.id = id;
} }
@Override
public String getId() { public String getId() {
return this.id; return this.id;
} }

Datei anzeigen

@ -23,7 +23,6 @@ import javax.annotation.Nullable;
public class GameModes { public class GameModes {
public static final GameMode NOT_SET = register("");
public static final GameMode SURVIVAL = register("survival"); public static final GameMode SURVIVAL = register("survival");
public static final GameMode CREATIVE = register("creative"); public static final GameMode CREATIVE = register("creative");
public static final GameMode ADVENTURE = register("adventure"); public static final GameMode ADVENTURE = register("adventure");
@ -40,7 +39,8 @@ public class GameModes {
return GameMode.REGISTRY.register(gameMode.getId(), gameMode); 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); return GameMode.REGISTRY.get(id);
} }

Datei anzeigen

@ -23,6 +23,7 @@ import com.sk89q.worldedit.WorldEdit;
import com.sk89q.worldedit.blocks.BaseItem; import com.sk89q.worldedit.blocks.BaseItem;
import com.sk89q.worldedit.extension.platform.Capability; import com.sk89q.worldedit.extension.platform.Capability;
import com.sk89q.worldedit.registry.Category; import com.sk89q.worldedit.registry.Category;
import com.sk89q.worldedit.registry.Keyed;
import com.sk89q.worldedit.registry.NamespacedRegistry; import com.sk89q.worldedit.registry.NamespacedRegistry;
import java.util.Set; import java.util.Set;
@ -31,7 +32,7 @@ import java.util.Set;
* A category of items. This is due to the splitting up of * A category of items. This is due to the splitting up of
* items such as wool into separate ids. * items such as wool into separate ids.
*/ */
public class ItemCategory extends Category<ItemType> { public class ItemCategory extends Category<ItemType> implements Keyed {
public static final NamespacedRegistry<ItemCategory> REGISTRY = new NamespacedRegistry<>("item tag"); public static final NamespacedRegistry<ItemCategory> REGISTRY = new NamespacedRegistry<>("item tag");

Datei anzeigen

@ -21,13 +21,14 @@ package com.sk89q.worldedit.world.item;
import com.sk89q.worldedit.WorldEdit; import com.sk89q.worldedit.WorldEdit;
import com.sk89q.worldedit.extension.platform.Capability; import com.sk89q.worldedit.extension.platform.Capability;
import com.sk89q.worldedit.registry.Keyed;
import com.sk89q.worldedit.registry.NamespacedRegistry; import com.sk89q.worldedit.registry.NamespacedRegistry;
import com.sk89q.worldedit.world.block.BlockType; import com.sk89q.worldedit.world.block.BlockType;
import com.sk89q.worldedit.world.block.BlockTypes; import com.sk89q.worldedit.world.block.BlockTypes;
import javax.annotation.Nullable; import javax.annotation.Nullable;
public class ItemType { public class ItemType implements Keyed {
public static final NamespacedRegistry<ItemType> REGISTRY = new NamespacedRegistry<>("item type"); public static final NamespacedRegistry<ItemType> REGISTRY = new NamespacedRegistry<>("item type");
@ -41,6 +42,7 @@ public class ItemType {
this.id = id; this.id = id;
} }
@Override
public String getId() { public String getId() {
return this.id; return this.id;
} }

Datei anzeigen

@ -20,13 +20,14 @@
package com.sk89q.worldedit.world.registry; package com.sk89q.worldedit.world.registry;
import com.sk89q.worldedit.registry.Category; import com.sk89q.worldedit.registry.Category;
import com.sk89q.worldedit.registry.Keyed;
import java.util.Set; import java.util.Set;
/** /**
* A registry of categories. Minecraft internally calls these 'Tags'. * A registry of categories. Minecraft internally calls these 'Tags'.
*/ */
public interface CategoryRegistry<T> { public interface CategoryRegistry<T extends Keyed> {
/** /**
* Gets a set of values with a given category. * Gets a set of values with a given category.

Datei anzeigen

@ -19,9 +19,10 @@
package com.sk89q.worldedit.world.weather; package com.sk89q.worldedit.world.weather;
import com.sk89q.worldedit.registry.Keyed;
import com.sk89q.worldedit.registry.Registry; import com.sk89q.worldedit.registry.Registry;
public class WeatherType { public class WeatherType implements Keyed {
public static final Registry<WeatherType> REGISTRY = new Registry<>("weather type"); public static final Registry<WeatherType> REGISTRY = new Registry<>("weather type");
@ -31,6 +32,7 @@ public class WeatherType {
this.id = id; this.id = id;
} }
@Override
public String getId() { public String getId() {
return this.id; return this.id;
} }