File size: 8,134 Bytes
d46f4a3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package net.minecraft.server.commands;

import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.arguments.FloatArgumentType;
import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import com.mojang.brigadier.exceptions.SimpleCommandExceptionType;
import net.minecraft.commands.CommandBuildContext;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.commands.Commands;
import net.minecraft.commands.arguments.EntityArgument;
import net.minecraft.commands.arguments.ResourceArgument;
import net.minecraft.commands.arguments.coordinates.Vec3Argument;
import net.minecraft.core.registries.Registries;
import net.minecraft.network.chat.Component;
import net.minecraft.world.damagesource.DamageSource;
import net.minecraft.world.entity.Entity;

public class DamageCommand {
    private static final SimpleCommandExceptionType ERROR_INVULNERABLE = new SimpleCommandExceptionType(Component.translatable("commands.damage.invulnerable"));

    public static void register(CommandDispatcher<CommandSourceStack> p_270226_, CommandBuildContext p_270136_) {
        p_270226_.register(
            Commands.literal("damage")
                .requires(p_270872_ -> p_270872_.hasPermission(2))
                .then(
                    Commands.argument("target", EntityArgument.entity())
                        .then(
                            Commands.argument("amount", FloatArgumentType.floatArg(0.0F))
                                .executes(
                                    p_374870_ -> damage(
                                            p_374870_.getSource(),
                                            EntityArgument.getEntity(p_374870_, "target"),
                                            FloatArgumentType.getFloat(p_374870_, "amount"),
                                            p_374870_.getSource().getLevel().damageSources().generic()
                                        )
                                )
                                .then(
                                    Commands.argument("damageType", ResourceArgument.resource(p_270136_, Registries.DAMAGE_TYPE))
                                        .executes(
                                            p_270840_ -> damage(
                                                    p_270840_.getSource(),
                                                    EntityArgument.getEntity(p_270840_, "target"),
                                                    FloatArgumentType.getFloat(p_270840_, "amount"),
                                                    new DamageSource(ResourceArgument.getResource(p_270840_, "damageType", Registries.DAMAGE_TYPE))
                                                )
                                        )
                                        .then(
                                            Commands.literal("at")
                                                .then(
                                                    Commands.argument("location", Vec3Argument.vec3())
                                                        .executes(
                                                            p_270444_ -> damage(
                                                                    p_270444_.getSource(),
                                                                    EntityArgument.getEntity(p_270444_, "target"),
                                                                    FloatArgumentType.getFloat(p_270444_, "amount"),
                                                                    new DamageSource(
                                                                        ResourceArgument.getResource(p_270444_, "damageType", Registries.DAMAGE_TYPE),
                                                                        Vec3Argument.getVec3(p_270444_, "location")
                                                                    )
                                                                )
                                                        )
                                                )
                                        )
                                        .then(
                                            Commands.literal("by")
                                                .then(
                                                    Commands.argument("entity", EntityArgument.entity())
                                                        .executes(
                                                            p_270329_ -> damage(
                                                                    p_270329_.getSource(),
                                                                    EntityArgument.getEntity(p_270329_, "target"),
                                                                    FloatArgumentType.getFloat(p_270329_, "amount"),
                                                                    new DamageSource(
                                                                        ResourceArgument.getResource(p_270329_, "damageType", Registries.DAMAGE_TYPE),
                                                                        EntityArgument.getEntity(p_270329_, "entity")
                                                                    )
                                                                )
                                                        )
                                                        .then(
                                                            Commands.literal("from")
                                                                .then(
                                                                    Commands.argument("cause", EntityArgument.entity())
                                                                        .executes(
                                                                            p_270848_ -> damage(
                                                                                    p_270848_.getSource(),
                                                                                    EntityArgument.getEntity(p_270848_, "target"),
                                                                                    FloatArgumentType.getFloat(p_270848_, "amount"),
                                                                                    new DamageSource(
                                                                                        ResourceArgument.getResource(
                                                                                            p_270848_, "damageType", Registries.DAMAGE_TYPE
                                                                                        ),
                                                                                        EntityArgument.getEntity(p_270848_, "entity"),
                                                                                        EntityArgument.getEntity(p_270848_, "cause")
                                                                                    )
                                                                                )
                                                                        )
                                                                )
                                                        )
                                                )
                                        )
                                )
                        )
                )
        );
    }

    private static int damage(CommandSourceStack p_270409_, Entity p_270496_, float p_270836_, DamageSource p_270727_) throws CommandSyntaxException {
        if (p_270496_.hurtServer(p_270409_.getLevel(), p_270727_, p_270836_)) {
            p_270409_.sendSuccess(() -> Component.translatable("commands.damage.success", p_270836_, p_270496_.getDisplayName()), true);
            return 1;
        } else {
            throw ERROR_INVULNERABLE.create();
        }
    }
}