| 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.builder.LiteralArgumentBuilder;
|
| 6 | import com.mojang.brigadier.exceptions.CommandSyntaxException;
|
| 7 | import com.mojang.brigadier.exceptions.SimpleCommandExceptionType;
|
| 8 | import java.util.Collection;
|
| 9 | import java.util.List;
|
| 10 | import net.minecraft.commands.CommandSourceStack;
|
| 11 | import net.minecraft.commands.Commands;
|
| 12 | import net.minecraft.commands.arguments.EntityArgument;
|
| 13 | import net.minecraft.commands.arguments.SwingAnimationArgument;
|
| 14 | import net.minecraft.commands.arguments.TimeArgument;
|
| 15 | import net.minecraft.network.chat.Component;
|
| 16 | import net.minecraft.world.InteractionHand;
|
| 17 | import net.minecraft.world.entity.Entity;
|
| 18 | import net.minecraft.world.entity.LivingEntity;
|
| 19 | import net.minecraft.world.item.SwingAnimationType;
|
| 20 | import net.minecraft.world.item.component.SwingAnimation;
|
| 21 |
|
| 22 | public class SwingCommand {
|
| 23 | private static final SimpleCommandExceptionType ERROR_NO_LIVING_ENTITY = new SimpleCommandExceptionType(
|
| 24 | Component.translatable("commands.swing.failed.notliving")
|
| 25 | );
|
| 26 | private static final CommandResponseTracker.Messages<LivingEntity> RESPONSE_SWING = CommandResponseTracker.messages(
|
| 27 | ERROR_NO_LIVING_ENTITY,
|
| 28 | (entity, var1) -> Component.translatable("commands.swing.success.single", entity.getDisplayName()),
|
| 29 | (entityCount, var1) -> Component.translatable("commands.swing.success.multiple", entityCount)
|
| 30 | );
|
| 31 |
|
| 32 | public static void register(final CommandDispatcher<CommandSourceStack> dispatcher) {
|
| 33 | dispatcher.register(
|
| 34 | Commands.literal("swing")
|
| 35 | .requires(Commands.hasPermission(Commands.LEVEL_GAMEMASTERS))
|
| 36 | .executes(c -> swing(c.getSource(), List.of(c.getSource().getEntityOrException()), InteractionHand.MAIN_HAND))
|
| 37 | .then(
|
| 38 | Commands.argument("targets", EntityArgument.entities())
|
| 39 | .executes(c -> swing(c.getSource(), EntityArgument.getEntities(c, "targets"), InteractionHand.MAIN_HAND))
|
| 40 | .then(handSwing("mainhand", InteractionHand.MAIN_HAND))
|
| 41 | .then(handSwing("offhand", InteractionHand.OFF_HAND))
|
| 42 | )
|
| 43 | );
|
| 44 | }
|
| 45 |
|
| 46 | private static LiteralArgumentBuilder<CommandSourceStack> handSwing(final String name, final InteractionHand hand) {
|
| 47 | return Commands.literal(name)
|
| 48 | .executes(c -> swing(c.getSource(), EntityArgument.getEntities(c, "targets"), hand))
|
| 49 | .then(
|
| 50 | Commands.argument("animation", SwingAnimationArgument.swingAnimationType())
|
| 51 | .executes(
|
| 52 | c -> swing(
|
| 53 | c.getSource(),
|
| 54 | EntityArgument.getEntities(c, "targets"),
|
| 55 | hand,
|
| 56 | SwingAnimationArgument.getSwingAnimationType(c, "animation"),
|
| 57 | SwingAnimation.DEFAULT.duration()
|
| 58 | )
|
| 59 | )
|
| 60 | .then(
|
| 61 | Commands.argument("duration", TimeArgument.time(1))
|
| 62 | .executes(
|
| 63 | c -> swing(
|
| 64 | c.getSource(),
|
| 65 | EntityArgument.getEntities(c, "targets"),
|
| 66 | hand,
|
| 67 | SwingAnimationArgument.getSwingAnimationType(c, "animation"),
|
| 68 | IntegerArgumentType.getInteger(c, "duration")
|
| 69 | )
|
| 70 | )
|
| 71 | )
|
| 72 | );
|
| 73 | }
|
| 74 |
|
| 75 | private static int swing(final CommandSourceStack source, final Collection<? extends Entity> targets, final InteractionHand hand) throws CommandSyntaxException {
|
| 76 | return swing(source, targets, hand, SwingAnimation.DEFAULT.type(), SwingAnimation.DEFAULT.duration());
|
| 77 | }
|
| 78 |
|
| 79 | private static int swing(
|
| 80 | final CommandSourceStack source,
|
| 81 | final Collection<? extends Entity> targets,
|
| 82 | final InteractionHand hand,
|
| 83 | final SwingAnimationType animationType,
|
| 84 | final int duration
|
| 85 | ) throws CommandSyntaxException {
|
| 86 | CommandResponseTracker<LivingEntity> tracker = CommandResponseTracker.create();
|
| 87 | SwingAnimation animation = new SwingAnimation(animationType, duration);
|
| 88 |
|
| 89 | for (Entity entity : targets) {
|
| 90 | if (entity instanceof LivingEntity livingEntity) {
|
| 91 | livingEntity.swing(hand, animation, true);
|
| 92 | tracker.track(livingEntity);
|
| 93 | }
|
| 94 | }
|
| 95 |
|
| 96 | return tracker.sendFeedback(source, true, RESPONSE_SWING);
|
| 97 | }
|
| 98 | }
|
| 99 |
|