| 1 | package net.minecraft.server.commands;
|
| 2 |
|
| 3 | import com.mojang.brigadier.CommandDispatcher;
|
| 4 | import com.mojang.brigadier.arguments.IntegerArgumentType;
|
| 5 | import com.mojang.brigadier.exceptions.CommandSyntaxException;
|
| 6 | import com.mojang.brigadier.exceptions.Dynamic2CommandExceptionType;
|
| 7 | import com.mojang.brigadier.exceptions.DynamicCommandExceptionType;
|
| 8 | import com.mojang.brigadier.exceptions.SimpleCommandExceptionType;
|
| 9 | import java.util.Collection;
|
| 10 | import net.minecraft.commands.CommandBuildContext;
|
| 11 | import net.minecraft.commands.CommandSourceStack;
|
| 12 | import net.minecraft.commands.Commands;
|
| 13 | import net.minecraft.commands.arguments.EntityArgument;
|
| 14 | import net.minecraft.commands.arguments.ResourceArgument;
|
| 15 | import net.minecraft.core.Holder;
|
| 16 | import net.minecraft.core.registries.Registries;
|
| 17 | import net.minecraft.network.chat.Component;
|
| 18 | import net.minecraft.world.entity.Entity;
|
| 19 | import net.minecraft.world.entity.LivingEntity;
|
| 20 | import net.minecraft.world.item.ItemStack;
|
| 21 | import net.minecraft.world.item.enchantment.Enchantment;
|
| 22 | import net.minecraft.world.item.enchantment.EnchantmentHelper;
|
| 23 |
|
| 24 | public class EnchantCommand {
|
| 25 | private static final DynamicCommandExceptionType ERROR_NOT_LIVING_ENTITY = new DynamicCommandExceptionType(
|
| 26 | target -> Component.translatableEscape("commands.enchant.failed.entity", target)
|
| 27 | );
|
| 28 | private static final DynamicCommandExceptionType ERROR_NO_ITEM = new DynamicCommandExceptionType(
|
| 29 | target -> Component.translatableEscape("commands.enchant.failed.itemless", target)
|
| 30 | );
|
| 31 | private static final DynamicCommandExceptionType ERROR_INCOMPATIBLE = new DynamicCommandExceptionType(
|
| 32 | item -> Component.translatableEscape("commands.enchant.failed.incompatible", item)
|
| 33 | );
|
| 34 | private static final Dynamic2CommandExceptionType ERROR_LEVEL_TOO_HIGH = new Dynamic2CommandExceptionType(
|
| 35 | (level, max) -> Component.translatableEscape("commands.enchant.failed.level", level, max)
|
| 36 | );
|
| 37 | private static final SimpleCommandExceptionType ERROR_NOTHING_HAPPENED = new SimpleCommandExceptionType(Component.translatable("commands.enchant.failed"));
|
| 38 | private static final CommandResponseTracker.MessagesWithArgs<Entity, Holder<Enchantment>, Integer> RESPONSE_ENCHANT = CommandResponseTracker.messages(
|
| 39 | ERROR_NOTHING_HAPPENED,
|
| 40 | (entity, var1, enchantment, level) -> Component.translatable(
|
| 41 | "commands.enchant.success.single", Enchantment.getFullname(enchantment, level), entity.getDisplayName()
|
| 42 | ),
|
| 43 | (entityCount, var1, enchantment, level) -> Component.translatable(
|
| 44 | "commands.enchant.success.multiple", Enchantment.getFullname(enchantment, level), entityCount
|
| 45 | )
|
| 46 | );
|
| 47 |
|
| 48 | public static void register(final CommandDispatcher<CommandSourceStack> dispatcher, final CommandBuildContext context) {
|
| 49 | dispatcher.register(
|
| 50 | Commands.literal("enchant")
|
| 51 | .requires(Commands.hasPermission(Commands.LEVEL_GAMEMASTERS))
|
| 52 | .then(
|
| 53 | Commands.argument("targets", EntityArgument.entities())
|
| 54 | .then(
|
| 55 | Commands.argument("enchantment", ResourceArgument.resource(context, Registries.ENCHANTMENT))
|
| 56 | .executes(c -> enchant(c.getSource(), EntityArgument.getEntities(c, "targets"), ResourceArgument.getEnchantment(c, "enchantment"), 1))
|
| 57 | .then(
|
| 58 | Commands.argument("level", IntegerArgumentType.integer(0))
|
| 59 | .executes(
|
| 60 | c -> enchant(
|
| 61 | c.getSource(),
|
| 62 | EntityArgument.getEntities(c, "targets"),
|
| 63 | ResourceArgument.getEnchantment(c, "enchantment"),
|
| 64 | IntegerArgumentType.getInteger(c, "level")
|
| 65 | )
|
| 66 | )
|
| 67 | )
|
| 68 | )
|
| 69 | )
|
| 70 | );
|
| 71 | }
|
| 72 |
|
| 73 | private static int enchant(
|
| 74 | final CommandSourceStack source, final Collection<? extends Entity> targets, final Holder<Enchantment> enchantmentHolder, final int level
|
| 75 | ) throws CommandSyntaxException {
|
| 76 | Enchantment enchantment = enchantmentHolder.value();
|
| 77 | if (level > enchantment.getMaxLevel()) {
|
| 78 | throw ERROR_LEVEL_TOO_HIGH.create(level, enchantment.getMaxLevel());
|
| 79 | }
|
| 80 |
|
| 81 | CommandResponseTracker<Entity> tracker = CommandResponseTracker.create();
|
| 82 |
|
| 83 | for (Entity entity : targets) {
|
| 84 | if (entity instanceof LivingEntity target) {
|
| 85 | ItemStack item = target.getMainHandItem();
|
| 86 | if (!item.isEmpty()) {
|
| 87 | if (enchantment.canEnchant(item)
|
| 88 | && EnchantmentHelper.isEnchantmentCompatible(EnchantmentHelper.getEnchantmentsForCrafting(item).keySet(), enchantmentHolder)) {
|
| 89 | item.enchant(enchantmentHolder, level);
|
| 90 | tracker.track(entity);
|
| 91 | } else if (targets.size() == 1) {
|
| 92 | throw ERROR_INCOMPATIBLE.create(item.getHoverName().getString());
|
| 93 | }
|
| 94 | } else if (targets.size() == 1) {
|
| 95 | throw ERROR_NO_ITEM.create(target.getDisplayName().getString());
|
| 96 | }
|
| 97 | } else if (targets.size() == 1) {
|
| 98 | throw ERROR_NOT_LIVING_ENTITY.create(entity.getDisplayName().getString());
|
| 99 | }
|
| 100 | }
|
| 101 |
|
| 102 | return tracker.sendFeedback(source, true, RESPONSE_ENCHANT, enchantmentHolder, level);
|
| 103 | }
|
| 104 | }
|
| 105 |
|