Skip to content

Commit

Permalink
fix .enchant command
Browse files Browse the repository at this point in the history
  • Loading branch information
RacoonDog committed Oct 14, 2024
1 parent 25004d1 commit 86de47f
Show file tree
Hide file tree
Showing 2 changed files with 129 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
/*
* This file is part of the Meteor Client distribution (https://github.com/MeteorDevelopment/meteor-client).
* Copyright (c) Meteor Development.
*/

package meteordevelopment.meteorclient.commands.arguments;

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.Dynamic2CommandExceptionType;
import com.mojang.brigadier.exceptions.Dynamic3CommandExceptionType;
import com.mojang.brigadier.suggestion.Suggestions;
import com.mojang.brigadier.suggestion.SuggestionsBuilder;
import net.minecraft.client.MinecraftClient;
import net.minecraft.command.CommandSource;
import net.minecraft.enchantment.Enchantment;
import net.minecraft.entity.EntityType;
import net.minecraft.entity.attribute.EntityAttribute;
import net.minecraft.entity.effect.StatusEffect;
import net.minecraft.registry.Registry;
import net.minecraft.registry.RegistryKey;
import net.minecraft.registry.RegistryKeys;
import net.minecraft.registry.entry.RegistryEntry;
import net.minecraft.text.Text;
import net.minecraft.util.Identifier;
import net.minecraft.world.gen.structure.Structure;

import java.util.Arrays;
import java.util.Collection;
import java.util.concurrent.CompletableFuture;

public class RegistryEntryReferenceArgumentType<T> implements ArgumentType<RegistryEntry.Reference<T>> {
private static final RegistryEntryReferenceArgumentType<Enchantment> ENCHANTMENT = new RegistryEntryReferenceArgumentType<>(RegistryKeys.ENCHANTMENT);
private static final RegistryEntryReferenceArgumentType<EntityAttribute> ENTITY_ATTRIBUTE = new RegistryEntryReferenceArgumentType<>(RegistryKeys.ATTRIBUTE);
private static final RegistryEntryReferenceArgumentType<Structure> STRUCTURE = new RegistryEntryReferenceArgumentType<>(RegistryKeys.STRUCTURE);
private static final RegistryEntryReferenceArgumentType<EntityType<?>> ENTITY_TYPE = new RegistryEntryReferenceArgumentType<>(RegistryKeys.ENTITY_TYPE);
private static final RegistryEntryReferenceArgumentType<StatusEffect> STATUS_EFFECT = new RegistryEntryReferenceArgumentType<>(RegistryKeys.STATUS_EFFECT);

private static final Collection<String> EXAMPLES = Arrays.asList("foo", "foo:bar", "012");
public static final Dynamic2CommandExceptionType NOT_FOUND_EXCEPTION = new Dynamic2CommandExceptionType(
(element, type) -> Text.stringifiedTranslatable("argument.resource.not_found", element, type)
);
public static final Dynamic3CommandExceptionType INVALID_TYPE_EXCEPTION = new Dynamic3CommandExceptionType(
(element, type, expectedType) -> Text.stringifiedTranslatable("argument.resource.invalid_type", element, type, expectedType)
);
private final RegistryKey<? extends Registry<T>> registryRef;

private RegistryEntryReferenceArgumentType(RegistryKey<? extends Registry<T>> registryRef) {
this.registryRef = registryRef;
}

public static RegistryEntryReferenceArgumentType<Enchantment> enchantment() {
return ENCHANTMENT;
}

public static RegistryEntryReferenceArgumentType<EntityAttribute> entityAttribute() {
return ENTITY_ATTRIBUTE;
}

public static RegistryEntryReferenceArgumentType<Structure> structure() {
return STRUCTURE;
}

public static RegistryEntryReferenceArgumentType<EntityType<?>> entityType() {
return ENTITY_TYPE;
}

public static RegistryEntryReferenceArgumentType<StatusEffect> statusEffect() {
return STATUS_EFFECT;
}

public static RegistryEntry.Reference<Enchantment> getEnchantment(CommandContext<?> context, String name) throws CommandSyntaxException {
return getRegistryEntry(context, name, RegistryKeys.ENCHANTMENT);
}

public static RegistryEntry.Reference<EntityAttribute> getEntityAttribute(CommandContext<?> context, String name) throws CommandSyntaxException {
return getRegistryEntry(context, name, RegistryKeys.ATTRIBUTE);
}

public static RegistryEntry.Reference<Structure> getStructure(CommandContext<?> context, String name) throws CommandSyntaxException {
return getRegistryEntry(context, name, RegistryKeys.STRUCTURE);
}

public static RegistryEntry.Reference<EntityType<?>> getEntityType(CommandContext<?> context, String name) throws CommandSyntaxException {
return getRegistryEntry(context, name, RegistryKeys.ENTITY_TYPE);
}

public static RegistryEntry.Reference<StatusEffect> getStatusEffect(CommandContext<?> context, String name) throws CommandSyntaxException {
return getRegistryEntry(context, name, RegistryKeys.STATUS_EFFECT);
}

private static <T> RegistryEntry.Reference<T> getRegistryEntry(CommandContext<?> context, String name, RegistryKey<Registry<T>> registryRef) throws CommandSyntaxException {
RegistryEntry.Reference<T> reference = context.getArgument(name, RegistryEntry.Reference.class);
RegistryKey<?> registryKey = reference.registryKey();
if (registryKey.isOf(registryRef)) {
return reference;
} else {
throw INVALID_TYPE_EXCEPTION.create(registryKey.getValue(), registryKey.getRegistry(), registryRef.getValue());
}
}

@Override
public RegistryEntry.Reference<T> parse(StringReader reader) throws CommandSyntaxException {
Identifier identifier = Identifier.fromCommandInput(reader);
RegistryKey<T> registryKey = RegistryKey.of(this.registryRef, identifier);
return MinecraftClient.getInstance().getNetworkHandler().getRegistryManager()
.getWrapperOrThrow(this.registryRef)
.getOptional(registryKey)
.orElseThrow(() -> NOT_FOUND_EXCEPTION.createWithContext(reader, identifier, this.registryRef.getValue()));
}

@Override
public <S> CompletableFuture<Suggestions> listSuggestions(CommandContext<S> context, SuggestionsBuilder builder) {
return CommandSource.suggestIdentifiers(MinecraftClient.getInstance().getNetworkHandler().getRegistryManager().getWrapperOrThrow(this.registryRef).streamKeys().map(RegistryKey::getValue), builder);
}

@Override
public Collection<String> getExamples() {
return EXAMPLES;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import com.mojang.brigadier.exceptions.SimpleCommandExceptionType;
import meteordevelopment.meteorclient.commands.Command;
import meteordevelopment.meteorclient.commands.arguments.RegistryEntryReferenceArgumentType;
import meteordevelopment.meteorclient.utils.Utils;
import net.minecraft.client.gui.screen.ingame.InventoryScreen;
import net.minecraft.command.CommandSource;
import net.minecraft.command.argument.RegistryEntryReferenceArgumentType;
import net.minecraft.enchantment.Enchantment;
import net.minecraft.item.ItemStack;
import net.minecraft.registry.RegistryKeys;
Expand All @@ -33,7 +33,7 @@ public EnchantCommand() {

@Override
public void build(LiteralArgumentBuilder<CommandSource> builder) {
builder.then(literal("one").then(argument("enchantment", RegistryEntryReferenceArgumentType.registryEntry(REGISTRY_ACCESS, RegistryKeys.ENCHANTMENT))
builder.then(literal("one").then(argument("enchantment", RegistryEntryReferenceArgumentType.enchantment())
.then(literal("level").then(argument("level", IntegerArgumentType.integer()).executes(context -> {
one(context, enchantment -> context.getArgument("level", Integer.class));
return SINGLE_SUCCESS;
Expand Down Expand Up @@ -74,9 +74,9 @@ public void build(LiteralArgumentBuilder<CommandSource> builder) {
return SINGLE_SUCCESS;
}));

builder.then(literal("remove").then(argument("enchantment", RegistryEntryReferenceArgumentType.registryEntry(REGISTRY_ACCESS, RegistryKeys.ENCHANTMENT)).executes(context -> {
builder.then(literal("remove").then(argument("enchantment", RegistryEntryReferenceArgumentType.enchantment()).executes(context -> {
ItemStack itemStack = tryGetItemStack();
RegistryEntry.Reference<Enchantment> enchantment = context.getArgument("enchantment", RegistryEntry.Reference.class);
RegistryEntry.Reference<Enchantment> enchantment = RegistryEntryReferenceArgumentType.getEnchantment(context, "enchantment");
Utils.removeEnchantment(itemStack, enchantment.value());

syncItem();
Expand All @@ -87,7 +87,7 @@ public void build(LiteralArgumentBuilder<CommandSource> builder) {
private void one(CommandContext<CommandSource> context, ToIntFunction<Enchantment> level) throws CommandSyntaxException {
ItemStack itemStack = tryGetItemStack();

RegistryEntry.Reference<Enchantment> enchantment = context.getArgument("enchantment", RegistryEntry.Reference.class);
RegistryEntry.Reference<Enchantment> enchantment = RegistryEntryReferenceArgumentType.getEnchantment(context, "enchantment");
Utils.addEnchantment(itemStack, enchantment, level.applyAsInt(enchantment.value()));

syncItem();
Expand All @@ -96,7 +96,7 @@ private void one(CommandContext<CommandSource> context, ToIntFunction<Enchantmen
private void all(boolean onlyPossible, ToIntFunction<Enchantment> level) throws CommandSyntaxException {
ItemStack itemStack = tryGetItemStack();

REGISTRY_ACCESS.getOptionalWrapper(RegistryKeys.ENCHANTMENT).ifPresent(registry -> {
mc.getNetworkHandler().getRegistryManager().getOptionalWrapper(RegistryKeys.ENCHANTMENT).ifPresent(registry -> {
registry.streamEntries().forEach(enchantment -> {
if (!onlyPossible || enchantment.value().isAcceptableItem(itemStack)) {
Utils.addEnchantment(itemStack, enchantment, level.applyAsInt(enchantment.value()));
Expand Down

0 comments on commit 86de47f

Please sign in to comment.