Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a //registry command to search registries #2584

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import com.sk89q.worldedit.internal.anvil.ChunkDeleter;
import com.sk89q.worldedit.internal.command.CommandUtil;
import com.sk89q.worldedit.internal.util.LogManagerCompat;
import com.sk89q.worldedit.registry.Registries;
import com.sk89q.worldedit.registry.state.Property;
import com.sk89q.worldedit.util.lifecycle.Lifecycled;
import com.sk89q.worldedit.util.lifecycle.SimpleLifecycled;
Expand Down Expand Up @@ -250,14 +251,16 @@ private void initializeRegistries() {
EntityType.REGISTRY.register(key, new EntityType(key));
});

// ... :|
GameModes.get("");
WeatherTypes.get("");

// Registries only available via NMS
BukkitImplAdapter adapter = getBukkitImplAdapter();
if (adapter != null) {
adapter.initializeRegistries();
}

// ... :|
GameModes.get("");
WeatherTypes.get("");
Registries.get("");
}

private void setupTags() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@
import com.sk89q.worldedit.internal.command.CommandUtil;
import com.sk89q.worldedit.internal.cui.ServerCUIHandler;
import com.sk89q.worldedit.math.BlockVector3;
import com.sk89q.worldedit.registry.Keyed;
import com.sk89q.worldedit.registry.Registry;
import com.sk89q.worldedit.session.Placement;
import com.sk89q.worldedit.session.PlacementType;
import com.sk89q.worldedit.util.SideEffect;
Expand All @@ -49,8 +51,12 @@
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.formatting.text.event.ClickEvent;
import com.sk89q.worldedit.util.formatting.text.event.HoverEvent;
import com.sk89q.worldedit.util.formatting.text.format.TextColor;
import com.sk89q.worldedit.world.World;
import com.sk89q.worldedit.world.biome.BiomeType;
import com.sk89q.worldedit.world.block.BlockType;
import com.sk89q.worldedit.world.item.ItemType;
import org.enginehub.piston.CommandManager;
import org.enginehub.piston.CommandManagerService;
Expand Down Expand Up @@ -518,4 +524,61 @@ public Component call() throws Exception {
.create(page);
}
}

@Command(
name = "/registry",
desc = "Search through the given registry"
)
@CommandPermissions("worldedit.registry")
public void registry(Actor actor,
@Arg(desc = "The registry to search through")
Registry<?> registry,
@ArgFlag(name = 'p', desc = "Page of results to return", def = "1")
int page,
@Arg(desc = "Search query", variable = true, def = "")
List<String> query) {
String search = String.join(" ", query);

WorldEditAsyncCommandBuilder.createAndSendMessage(actor, new RegistrySearcher(registry, search, page),
TranslatableComponent.of("worldedit.registry.searching"));
}

private static class RegistrySearcher implements Callable<Component> {
private final Registry<?> registry;
private final String search;
private final int page;

RegistrySearcher(Registry<?> registry, String search, int page) {
this.registry = registry;
this.search = search;
this.page = page;
}

@Override
public Component call() throws Exception {
String command = "//registry " + registry.id() + " -p %page% " + search;
Map<String, Component> results = new TreeMap<>();
String idMatch = search.replace(' ', '_');
for (Keyed searchType : registry) {
final String id = searchType.id();
if (id.contains(idMatch)) {
var builder = TextComponent.builder()
.append(searchType.id())
.clickEvent(ClickEvent.copyToClipboard(searchType.id()));
switch (searchType) {
case ItemType itemType -> builder.hoverEvent(HoverEvent.showText(itemType.getRichName()));
case BlockType blockType -> builder.hoverEvent(HoverEvent.showText(blockType.getRichName()));
case BiomeType biomeType -> builder.hoverEvent(HoverEvent.showText(biomeType.getRichName()));
default -> {
}
}
results.put(id, builder.build());
}
}
List<Component> list = new ArrayList<>(results.values());
String title = search.isBlank() ? "Registry contents" : "Search results for '" + search + "'";
return PaginationBox.fromComponents(title, command, list)
.create(page);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package com.sk89q.worldedit.command.argument;

import com.google.common.collect.ImmutableList;
import com.google.common.reflect.TypeToken;
import com.sk89q.worldedit.command.util.SuggestionHelper;
import com.sk89q.worldedit.registry.Keyed;
import com.sk89q.worldedit.registry.Registry;
Expand Down Expand Up @@ -48,7 +49,6 @@
import java.lang.reflect.Field;
import java.util.List;
import java.util.Locale;
import java.util.stream.Collectors;

public final class RegistryConverter<V extends Keyed> implements ArgumentConverter<V> {

Expand All @@ -73,6 +73,9 @@ public static void register(CommandManager commandManager) {
.forEach(registryType ->
commandManager.registerConverter(Key.of(registryType), from(registryType))
);

// This must be separate as it has a generic type
commandManager.registerConverter(Key.of(new TypeToken<>() {}), new RegistryConverter<>(Registry.REGISTRY));
}

@SuppressWarnings("unchecked")
Expand Down Expand Up @@ -112,6 +115,6 @@ public ConversionResult<V> convert(String argument, InjectedValueAccess injected

@Override
public List<String> getSuggestions(String input, InjectedValueAccess context) {
return SuggestionHelper.getRegistrySuggestions(registry, input).collect(Collectors.toList());
return SuggestionHelper.getRegistrySuggestions(registry, input).toList();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,23 +32,36 @@ public final class NamespacedRegistry<V extends Keyed> extends Registry<V> {
private final Set<String> knownNamespaces = new HashSet<>();
private final String defaultNamespace;

@Deprecated
public NamespacedRegistry(final String name) {
this(name, MINECRAFT_NAMESPACE);
}

@Deprecated
public NamespacedRegistry(final String name, final boolean checkInitialized) {
this(name, MINECRAFT_NAMESPACE, checkInitialized);
}

@Deprecated
public NamespacedRegistry(final String name, final String defaultNamespace) {
this(name, defaultNamespace, false);
}

@Deprecated
public NamespacedRegistry(final String name, final String defaultNamespace, final boolean checkInitialized) {
super(name, checkInitialized);
this.defaultNamespace = defaultNamespace;
}

public NamespacedRegistry(final String name, final String id, final String defaultNamespace) {
this(name, id, defaultNamespace, false);
}

public NamespacedRegistry(final String name, final String id, final String defaultNamespace, final boolean checkInitialized) {
super(name, id, checkInitialized);
this.defaultNamespace = defaultNamespace;
}

@Nullable
@Override
public V get(final String key) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* 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 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package com.sk89q.worldedit.registry;

import com.sk89q.worldedit.world.biome.BiomeCategory;
import com.sk89q.worldedit.world.biome.BiomeType;
import com.sk89q.worldedit.world.block.BlockCategory;
import com.sk89q.worldedit.world.block.BlockType;
import com.sk89q.worldedit.world.entity.EntityType;
import com.sk89q.worldedit.world.fluid.FluidCategory;
import com.sk89q.worldedit.world.fluid.FluidType;
import com.sk89q.worldedit.world.gamemode.GameMode;
import com.sk89q.worldedit.world.generation.ConfiguredFeatureType;
import com.sk89q.worldedit.world.generation.StructureType;
import com.sk89q.worldedit.world.item.ItemCategory;
import com.sk89q.worldedit.world.item.ItemType;
import com.sk89q.worldedit.world.weather.WeatherType;

import javax.annotation.Nullable;

public class Registries {
public static final Registry<BlockType> BLOCK_TYPE = addRegistry(BlockType.REGISTRY);
public static final Registry<BlockCategory> BLOCK_CATEGORY = addRegistry(BlockCategory.REGISTRY);
public static final Registry<ItemType> ITEM_TYPE = addRegistry(ItemType.REGISTRY);
public static final Registry<ItemCategory> ITEM_CATEGORY = addRegistry(ItemCategory.REGISTRY);
public static final Registry<GameMode> GAME_MODE = addRegistry(GameMode.REGISTRY);
public static final Registry<WeatherType> WEATHER_TYPE = addRegistry(WeatherType.REGISTRY);
public static final Registry<BiomeType> BIOME_TYPE = addRegistry(BiomeType.REGISTRY);
public static final Registry<BiomeCategory> BIOME_CATEGORY = addRegistry(BiomeCategory.REGISTRY);
public static final Registry<EntityType> ENTITY_TYPE = addRegistry(EntityType.REGISTRY);
public static final Registry<FluidType> FLUID_TYPE = addRegistry(FluidType.REGISTRY);
public static final Registry<FluidCategory> FLUID_CATEGORY = addRegistry(FluidCategory.REGISTRY);
public static final Registry<ConfiguredFeatureType> CONFIGURED_FEATURE_TYPE = addRegistry(ConfiguredFeatureType.REGISTRY);
public static final Registry<StructureType> STRUCTURE_TYPE = addRegistry(StructureType.REGISTRY);

private static <T extends Keyed> Registry<T> addRegistry(Registry<T> registry) {
Registry.REGISTRY.register(registry.id(), registry);
return registry;
}

@Nullable
public static Registry<?> get(final String id) {
return Registry.REGISTRY.get(id);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,24 +33,73 @@
import static com.google.common.base.Preconditions.checkState;
import static java.util.Objects.requireNonNull;

public class Registry<V extends Keyed> implements Iterable<V> {
public class Registry<V extends Keyed> implements Iterable<V>, Keyed {
public static final Registry<Registry<?>> REGISTRY = new Registry<>("registry", "registry");

private final Map<String, V> map = new HashMap<>();
private final String name;
private final String id;
private final boolean checkInitialized;

private static String nameToId(String name) {
return name.toLowerCase(Locale.ROOT).replace(' ', '_');
}

/**
* Creates a new Registry.
*
* @param name The name of the registry
* @deprecated Use {@link #Registry(String, String)} instead to provide an ID
*/
@Deprecated
public Registry(final String name) {
this(name, false);
}

/**
* Creates a new Registry.
*
* @param name The name of the registry
* @param checkInitialized Whether to check if WorldEdit is initialized
* @deprecated Use {@link #Registry(String, String, boolean)} instead to provide an ID
*/
@Deprecated
public Registry(final String name, final boolean checkInitialized) {
this(name, nameToId(name), checkInitialized);
}

/**
* Creates a new Registry.
*
* @param name The name of the registry
* @param id The ID of the registry
*/
public Registry(final String name, final String id) {
this(name, id, false);
}

/**
* Creates a new Registry.
*
* @param name The name of the registry
* @param id The ID of the registry
* @param checkInitialized Whether to check if WorldEdit is initialized
*/
public Registry(final String name, final String id, final boolean checkInitialized) {
this.name = name;
this.id = id;
this.checkInitialized = checkInitialized;
}

public String getName() {
return name;
}

@Override
public String id() {
return this.id;
}

@Nullable
public V get(final String key) {
checkState(key.equals(key.toLowerCase(Locale.ROOT)), "key must be lowercase: %s", key);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
*/
public class BiomeCategory extends Category<BiomeType> implements Keyed {

public static final NamespacedRegistry<BiomeCategory> REGISTRY = new NamespacedRegistry<>("biome tag", true);
public static final NamespacedRegistry<BiomeCategory> REGISTRY = new NamespacedRegistry<>("biome tag", "biome_tag", "minecraft", true);

public BiomeCategory(final String id) {
super(id, Set::of);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,13 @@

package com.sk89q.worldedit.world.biome;

import com.sk89q.worldedit.WorldEdit;
import com.sk89q.worldedit.extension.platform.Capability;
import com.sk89q.worldedit.function.pattern.BiomePattern;
import com.sk89q.worldedit.math.BlockVector3;
import com.sk89q.worldedit.registry.Keyed;
import com.sk89q.worldedit.registry.NamespacedRegistry;
import com.sk89q.worldedit.util.formatting.text.Component;

/**
* All the types of biomes in the game.
Expand All @@ -31,7 +34,7 @@
*/
public record BiomeType(String id) implements Keyed, BiomePattern {

public static final NamespacedRegistry<BiomeType> REGISTRY = new NamespacedRegistry<>("biome type", true);
public static final NamespacedRegistry<BiomeType> REGISTRY = new NamespacedRegistry<>("biome type", "biome_type", "minecraft", true);

@Override
public String toString() {
Expand All @@ -42,4 +45,9 @@ public String toString() {
public BiomeType applyBiome(BlockVector3 position) {
return this;
}

public Component getRichName() {
return WorldEdit.getInstance().getPlatformManager().queryCapability(Capability.GAME_HOOKS)
.getRegistries().getBiomeRegistry().getRichName(this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
*/
public class BlockCategory extends Category<BlockType> implements Keyed {

public static final NamespacedRegistry<BlockCategory> REGISTRY = new NamespacedRegistry<>("block tag", true);
public static final NamespacedRegistry<BlockCategory> REGISTRY = new NamespacedRegistry<>("block tag", "block_tag", "minecraft", true);

public BlockCategory(final String id) {
super(id, () -> WorldEdit.getInstance().getPlatformManager()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@

public class BlockType implements Keyed {

public static final NamespacedRegistry<BlockType> REGISTRY = new NamespacedRegistry<>("block type", true);
public static final NamespacedRegistry<BlockType> REGISTRY = new NamespacedRegistry<>("block type", "block_type", "minecraft", true);

private final String id;
private final Function<BlockState, BlockState> values;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import com.sk89q.worldedit.registry.NamespacedRegistry;

public record EntityType(String id) implements Keyed {
public static final NamespacedRegistry<EntityType> REGISTRY = new NamespacedRegistry<>("entity type", true);
public static final NamespacedRegistry<EntityType> REGISTRY = new NamespacedRegistry<>("entity type", "entity_type", "minecraft", true);

public EntityType {
// If it has no namespace, assume minecraft.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
*/
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", "fluid_tag", "minecraft");

public FluidCategory(final String id) {
// TODO Make this work.
Expand Down
Loading