-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4a9135f
commit 085b535
Showing
5 changed files
with
283 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
116 changes: 116 additions & 0 deletions
116
src/main/java/net/frozenblock/lib/tag/api/TagKeyArgument.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
package net.frozenblock.lib.tag.api; | ||
|
||
import com.google.gson.JsonObject; | ||
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.suggestion.Suggestions; | ||
import com.mojang.brigadier.suggestion.SuggestionsBuilder; | ||
import net.minecraft.commands.CommandBuildContext; | ||
import net.minecraft.commands.CommandSourceStack; | ||
import net.minecraft.commands.SharedSuggestionProvider; | ||
import net.minecraft.commands.synchronization.ArgumentTypeInfo; | ||
import net.minecraft.core.Registry; | ||
import net.minecraft.network.FriendlyByteBuf; | ||
import net.minecraft.resources.ResourceKey; | ||
import net.minecraft.resources.ResourceLocation; | ||
import net.minecraft.tags.TagKey; | ||
import java.util.Arrays; | ||
import java.util.Collection; | ||
import java.util.Optional; | ||
import java.util.concurrent.CompletableFuture; | ||
|
||
public class TagKeyArgument<T> implements ArgumentType<TagKeyArgument.Result<T>> { | ||
private static final Collection<String> EXAMPLES = Arrays.asList("foo", "foo:bar", "012", "#skeletons", "#minecraft:skeletons"); | ||
final ResourceKey<? extends Registry<T>> registryKey; | ||
|
||
public TagKeyArgument(ResourceKey<? extends Registry<T>> registryKey) { | ||
this.registryKey = registryKey; | ||
} | ||
|
||
public static <T> TagKeyArgument<T> tagKey(ResourceKey<? extends Registry<T>> registryKey) { | ||
return new TagKeyArgument<>(registryKey); | ||
} | ||
|
||
public static <T> TagKeyArgument.Result<T> getTagKey( | ||
CommandContext<CommandSourceStack> context, String argument, ResourceKey<Registry<T>> registryKey, DynamicCommandExceptionType dynamicCommandExceptionType | ||
) throws CommandSyntaxException { | ||
TagKeyArgument.Result<?> result = context.getArgument(argument, TagKeyArgument.Result.class); | ||
Optional<TagKeyArgument.Result<T>> optional = result.cast(registryKey); | ||
return optional.orElseThrow(() -> dynamicCommandExceptionType.create(result)); | ||
} | ||
|
||
public TagKeyArgument.Result<T> parse(StringReader reader) throws CommandSyntaxException { | ||
int cursor = reader.getCursor(); | ||
|
||
try { | ||
reader.skip(); | ||
ResourceLocation resourceLocation = ResourceLocation.read(reader); | ||
return new Result<>(TagKey.create(this.registryKey, resourceLocation)); | ||
} catch (CommandSyntaxException var4) { | ||
reader.setCursor(cursor); | ||
throw var4; | ||
} | ||
} | ||
|
||
@Override | ||
public <S> CompletableFuture<Suggestions> listSuggestions(CommandContext<S> commandContext, SuggestionsBuilder suggestionsBuilder) { | ||
Object var4 = commandContext.getSource(); | ||
return var4 instanceof SharedSuggestionProvider sharedSuggestionProvider | ||
? sharedSuggestionProvider.suggestRegistryElements(this.registryKey, SharedSuggestionProvider.ElementSuggestionType.TAGS, suggestionsBuilder, commandContext) | ||
: suggestionsBuilder.buildFuture(); | ||
} | ||
|
||
@Override | ||
public Collection<String> getExamples() { | ||
return EXAMPLES; | ||
} | ||
|
||
public static class Info<T> implements ArgumentTypeInfo<TagKeyArgument<T>, TagKeyArgument.Info<T>.Template> { | ||
public void serializeToNetwork(TagKeyArgument.Info<T>.Template template, FriendlyByteBuf buffer) { | ||
buffer.writeResourceLocation(template.registryKey.location()); | ||
} | ||
|
||
public TagKeyArgument.Info<T>.Template deserializeFromNetwork(FriendlyByteBuf buffer) { | ||
ResourceLocation resourceLocation = buffer.readResourceLocation(); | ||
return new TagKeyArgument.Info.Template(ResourceKey.createRegistryKey(resourceLocation)); | ||
} | ||
|
||
public void serializeToJson(TagKeyArgument.Info<T>.Template template, JsonObject json) { | ||
json.addProperty("registry", template.registryKey.location().toString()); | ||
} | ||
|
||
public TagKeyArgument.Info<T>.Template unpack(TagKeyArgument<T> argument) { | ||
return new TagKeyArgument.Info.Template(argument.registryKey); | ||
} | ||
|
||
public final class Template implements ArgumentTypeInfo.Template<TagKeyArgument<T>> { | ||
final ResourceKey<? extends Registry<T>> registryKey; | ||
|
||
Template(ResourceKey<? extends Registry<T>> registryKey) { | ||
this.registryKey = registryKey; | ||
} | ||
|
||
public TagKeyArgument<T> instantiate(CommandBuildContext context) { | ||
return new TagKeyArgument<>(this.registryKey); | ||
} | ||
|
||
@Override | ||
public ArgumentTypeInfo<TagKeyArgument<T>, ?> type() { | ||
return TagKeyArgument.Info.this; | ||
} | ||
} | ||
} | ||
|
||
record Result<T>(TagKey<T> key) { | ||
public <E> Optional<TagKeyArgument.Result<E>> cast(ResourceKey<? extends Registry<E>> registryKey) { | ||
return this.key.cast(registryKey).map(Result::new); | ||
} | ||
|
||
public String asPrintable() { | ||
return "#" + this.key.location(); | ||
} | ||
} | ||
} |
144 changes: 144 additions & 0 deletions
144
src/main/java/net/frozenblock/lib/tag/api/TagListCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
package net.frozenblock.lib.tag.api; | ||
|
||
import com.mojang.brigadier.CommandDispatcher; | ||
import com.mojang.brigadier.exceptions.CommandSyntaxException; | ||
import com.mojang.brigadier.exceptions.DynamicCommandExceptionType; | ||
import net.minecraft.commands.CommandSourceStack; | ||
import net.minecraft.commands.Commands; | ||
import net.minecraft.core.Holder; | ||
import net.minecraft.core.HolderSet; | ||
import net.minecraft.core.Registry; | ||
import net.minecraft.core.registries.Registries; | ||
import net.minecraft.network.chat.Component; | ||
import net.minecraft.resources.ResourceKey; | ||
|
||
public class TagListCommand { | ||
private TagListCommand() {} | ||
|
||
private static final DynamicCommandExceptionType ERROR_TAG_INVALID = new DynamicCommandExceptionType( | ||
type -> Component.translatable("commands.frozenlib.taglist.tag.invalid", type) | ||
); | ||
|
||
public static void register(CommandDispatcher<CommandSourceStack> dispatcher) { | ||
dispatcher.register( | ||
Commands.literal("taglist") | ||
.requires(source -> source.hasPermission(2)) | ||
.then( | ||
Commands.literal("biome") | ||
.then( | ||
Commands.argument("biome", TagKeyArgument.tagKey(Registries.BIOME)) | ||
.executes( | ||
context -> list( | ||
context.getSource(), | ||
Registries.BIOME, | ||
TagKeyArgument.getTagKey(context, "biome", Registries.BIOME, ERROR_TAG_INVALID) | ||
) | ||
) | ||
) | ||
) | ||
.then( | ||
Commands.literal("block") | ||
.then( | ||
Commands.argument("block", TagKeyArgument.tagKey(Registries.BLOCK)) | ||
.executes( | ||
context -> list( | ||
context.getSource(), | ||
Registries.BLOCK, | ||
TagKeyArgument.getTagKey(context, "block", Registries.BLOCK, ERROR_TAG_INVALID) | ||
) | ||
) | ||
) | ||
) | ||
.then( | ||
Commands.literal("entity_type") | ||
.then( | ||
Commands.argument("entity_type", TagKeyArgument.tagKey(Registries.ENTITY_TYPE)) | ||
.executes( | ||
context -> list( | ||
context.getSource(), | ||
Registries.ENTITY_TYPE, | ||
TagKeyArgument.getTagKey(context, "entity_type", Registries.ENTITY_TYPE, ERROR_TAG_INVALID) | ||
) | ||
) | ||
) | ||
) | ||
.then( | ||
Commands.literal("fluid") | ||
.then( | ||
Commands.argument("fluid", TagKeyArgument.tagKey(Registries.FLUID)) | ||
.executes( | ||
context -> list( | ||
context.getSource(), | ||
Registries.FLUID, | ||
TagKeyArgument.getTagKey(context, "fluid", Registries.FLUID, ERROR_TAG_INVALID) | ||
) | ||
) | ||
) | ||
) | ||
.then( | ||
Commands.literal("instrument") | ||
.then( | ||
Commands.argument("instrument", TagKeyArgument.tagKey(Registries.INSTRUMENT)) | ||
.executes( | ||
context -> list( | ||
context.getSource(), | ||
Registries.INSTRUMENT, | ||
TagKeyArgument.getTagKey(context, "instrument", Registries.INSTRUMENT, ERROR_TAG_INVALID) | ||
) | ||
) | ||
) | ||
) | ||
.then( | ||
Commands.literal("item") | ||
.then( | ||
Commands.argument("item", TagKeyArgument.tagKey(Registries.ITEM)) | ||
.executes( | ||
context -> list( | ||
context.getSource(), | ||
Registries.ITEM, | ||
TagKeyArgument.getTagKey(context, "item", Registries.ITEM, ERROR_TAG_INVALID) | ||
) | ||
) | ||
) | ||
) | ||
.then( | ||
Commands.literal("structure") | ||
.then( | ||
Commands.argument("structure", TagKeyArgument.tagKey(Registries.STRUCTURE)) | ||
.executes( | ||
context -> list( | ||
context.getSource(), | ||
Registries.STRUCTURE, | ||
TagKeyArgument.getTagKey(context, "structure", Registries.STRUCTURE, ERROR_TAG_INVALID) | ||
) | ||
) | ||
) | ||
) | ||
); | ||
} | ||
|
||
private static <T> int list(CommandSourceStack source, ResourceKey<Registry<T>> registryKey, TagKeyArgument.Result<T> tag) throws CommandSyntaxException { | ||
Registry<T> registry = source.getLevel().registryAccess().registryOrThrow(registryKey); | ||
String printable = tag.asPrintable(); | ||
HolderSet.Named<T> holderSet = registry.getTag(tag.key()).orElseThrow( | ||
() -> ERROR_TAG_INVALID.create(printable) | ||
); | ||
int size = holderSet.size(); | ||
|
||
for (Holder<T> value : holderSet) { | ||
if (holderSet.contains(value)) { | ||
source.sendSuccess( | ||
() -> Component.literal( | ||
value.unwrapKey().orElseThrow().location().toString() | ||
), | ||
true | ||
); | ||
} | ||
} | ||
source.sendSuccess( | ||
() -> Component.translatable("commands.frozenlib.taglist.footer", size, printable), | ||
true | ||
); | ||
return size; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters