Make /locate command support modded structures

This commit is contained in:
tterrag 2019-09-07 01:09:42 -04:00
parent 55fe7c470f
commit fc5573333d
4 changed files with 113 additions and 1 deletions

View File

@ -0,0 +1,17 @@
--- a/net/minecraft/command/impl/LocateCommand.java
+++ b/net/minecraft/command/impl/LocateCommand.java
@@ -20,6 +20,14 @@
public static void func_198528_a(CommandDispatcher<CommandSource> p_198528_0_) {
p_198528_0_.register(Commands.func_197057_a("locate").requires((p_198533_0_) -> {
return p_198533_0_.func_197034_c(2);
+ }).then(Commands.func_197056_a("structure_type", net.minecraftforge.common.command.StructureArgument.structure())
+ .suggests(net.minecraftforge.common.command.StructureArgument.suggestions())
+ .executes(ctx -> {
+ return func_198534_a(ctx.getSource(), ctx.getArgument("structure_type", net.minecraft.util.ResourceLocation.class).toString().replace("minecraft:", ""));
+ })));
+ if (true) return; // Forge: replace this code with above extensible version
+ p_198528_0_.register(Commands.func_197057_a("locate").requires((p_198533_0_) -> {
+ return p_198533_0_.func_197034_c(2);
}).then(Commands.func_197057_a("Pillager_Outpost").executes((p_198530_0_) -> {
return func_198534_a(p_198530_0_.getSource(), "Pillager_Outpost");
})).then(Commands.func_197057_a("Mineshaft").executes((p_198535_0_) -> {

View File

@ -0,0 +1,93 @@
package net.minecraftforge.common.command;
import com.google.common.collect.Streams;
import com.mojang.brigadier.StringReader;
import com.mojang.brigadier.arguments.ArgumentType;
import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import com.mojang.brigadier.exceptions.DynamicCommandExceptionType;
import com.mojang.brigadier.exceptions.SimpleCommandExceptionType;
import com.mojang.brigadier.suggestion.SuggestionProvider;
import net.minecraft.command.CommandSource;
import net.minecraft.command.ISuggestionProvider;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.ResourceLocationException;
import net.minecraft.util.text.TranslationTextComponent;
import net.minecraft.world.gen.feature.structure.Structure;
import net.minecraftforge.registries.GameData;
import java.util.Arrays;
import java.util.Collection;
import java.util.Locale;
public class StructureArgument implements ArgumentType<ResourceLocation>
{
private static final Collection<String> EXAMPLES = Arrays.asList("minecraft:village", "Mansion");
public static final DynamicCommandExceptionType STRUCTURE_UNKNOWN_TYPE = new DynamicCommandExceptionType((p_211367_0_) ->
{
return new TranslationTextComponent("structure.notFound", p_211367_0_);
});
public static StructureArgument structure()
{
return new StructureArgument();
}
public static SuggestionProvider<CommandSource> suggestions()
{
return (ctx, sb) -> ISuggestionProvider.suggest(Streams.concat(
GameData.getStructureMap().values().stream().map(Structure::getStructureName),
GameData.getStructureFeatures().keySet().stream().map(ResourceLocation::toString)), sb);
}
public static ResourceLocation getStructureId(CommandContext<CommandSource> context, String name) throws CommandSyntaxException
{
return checkIfStructureExists(context.getArgument(name, ResourceLocation.class));
}
private static ResourceLocation checkIfStructureExists(ResourceLocation id) throws CommandSyntaxException
{
if (id.getNamespace().equals("minecraft"))
{
// Special case vanilla hardcoded names, for vanilla compat
Structure<?> structure = GameData.getStructureMap().values().stream()
.filter(s -> s.getStructureName().equalsIgnoreCase(id.getPath()))
.findFirst()
.orElse(null);
if (structure != null)
return id;
}
GameData.getStructureFeatures().getValue(id).orElseThrow(() -> STRUCTURE_UNKNOWN_TYPE.create(id));
return id;
}
private static final SimpleCommandExceptionType INVALID_EXCEPTION = new SimpleCommandExceptionType(new TranslationTextComponent("argument.id.invalid"));
public ResourceLocation parse(StringReader reader) throws CommandSyntaxException
{
// Logic taken from ResourceLocation.read, but made case-insensitive
int cursor = reader.getCursor();
try
{
while (reader.canRead() && ResourceLocation.isValidPathCharacter(Character.toLowerCase(reader.peek())))
{
reader.skip();
}
String s = reader.getString().substring(cursor, reader.getCursor());
ResourceLocation rl = new ResourceLocation(s.toLowerCase(Locale.ROOT));
return checkIfStructureExists(rl);
}
catch (ResourceLocationException e)
{
reader.setCursor(cursor);
throw INVALID_EXCEPTION.createWithContext(reader);
}
}
public Collection<String> getExamples()
{
return EXAMPLES;
}
}

View File

@ -198,7 +198,7 @@ public class GameData
// Worldgen
makeRegistry(WORLD_CARVERS, WorldCarver.class).disableSaving().disableSync().create();
makeRegistry(SURFACE_BUILDERS, SurfaceBuilder.class).disableSaving().disableSync().create();
makeRegistry(FEATURES, Feature.class).addCallback(FeatureCallbacks.INSTANCE).disableSaving().disableSync().create();
makeRegistry(FEATURES, Feature.class).addCallback(FeatureCallbacks.INSTANCE).disableSaving().create();
makeRegistry(DECORATORS, Placement.class).disableSaving().disableSync().create();
makeRegistry(BIOME_PROVIDER_TYPES, BiomeProviderType.class).disableSaving().disableSync().create();
makeRegistry(CHUNK_GENERATOR_TYPES, ChunkGeneratorType.class).disableSaving().disableSync().create();

View File

@ -72,6 +72,8 @@
"fml.messages.version.restriction.bounded.lowerexclusive":"above {0}, and {1} or below",
"fml.messages.version.restriction.bounded.upperexclusive":"{0} or above, and below {1}",
"structure.notFound": "Unknown structure: %s",
"commands.forge.dimensions.list": "Currently registered dimensions by type:",
"commands.forge.entity.list.invalid": "Invalid filter, does not match any entities. Use /forge entity list for a proper list",
"commands.forge.entity.list.invalidworld": "Could not load world for dimension {0}. Please select a valid dimension.",