DamageCommand .java

net.minecraft.server.commands.DamageCommand (class): members: ERROR_INVULNERABLE, register. strings: "commands.damage.invulnerable", "damageType", "cause".

net.minecraft.server.commandsclass105 linesserver4.1 KB

API Surface (1)

void register(final CommandDispatcher<CommandSourceStack> dispatcher, final CommandBuildContext context)

Fields & Constants (1)

ERROR_INVULNERABLE

String literals (10)

"commands.damage.invulnerable"
"damage"
"target"
"amount"
"damageType"
"location"
"entity"
"from"
"cause"
"commands.damage.success"

Full Source

DamageCommand.java105 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.server.commands;
2
3
import com.mojang.brigadier.CommandDispatcher;
4
import com.mojang.brigadier.arguments.FloatArgumentType;
5
import com.mojang.brigadier.exceptions.CommandSyntaxException;
6
import com.mojang.brigadier.exceptions.SimpleCommandExceptionType;
7
import net.minecraft.commands.CommandBuildContext;
8
import net.minecraft.commands.CommandSourceStack;
9
import net.minecraft.commands.Commands;
10
import net.minecraft.commands.arguments.EntityArgument;
11
import net.minecraft.commands.arguments.ResourceArgument;
12
import net.minecraft.commands.arguments.coordinates.Vec3Argument;
13
import net.minecraft.core.registries.Registries;
14
import net.minecraft.network.chat.Component;
15
import net.minecraft.world.damagesource.DamageSource;
16
import net.minecraft.world.entity.Entity;
17
18
public class DamageCommand {
19
	private static final SimpleCommandExceptionType ERROR_INVULNERABLE = new SimpleCommandExceptionType(Component.translatable("commands.damage.invulnerable"));
20
21
	public static void register(final CommandDispatcher<CommandSourceStack> dispatcher, final CommandBuildContext context) {
22
		dispatcher.register(
23
			Commands.literal("damage")
24
				.requires(Commands.hasPermission(Commands.LEVEL_GAMEMASTERS))
25
				.then(
26
					Commands.argument("target", EntityArgument.entity())
27
						.then(
28
							Commands.argument("amount", FloatArgumentType.floatArg(0.0F))
29
								.executes(
30
									c -> damage(
31
										c.getSource(), EntityArgument.getEntity(c, "target"), FloatArgumentType.getFloat(c, "amount"), c.getSource().getLevel().damageSources().generic()
32
									)
33
								)
34
								.then(
35
									Commands.argument("damageType", ResourceArgument.resource(context, Registries.DAMAGE_TYPE))
36
										.executes(
37
											c -> damage(
38
												c.getSource(),
39
												EntityArgument.getEntity(c, "target"),
40
												FloatArgumentType.getFloat(c, "amount"),
41
												new DamageSource(ResourceArgument.getResource(c, "damageType", Registries.DAMAGE_TYPE))
42
											)
43
										)
44
										.then(
45
											Commands.literal("at")
46
												.then(
47
													Commands.argument("location", Vec3Argument.vec3())
48
														.executes(
49
															c -> damage(
50
																c.getSource(),
51
																EntityArgument.getEntity(c, "target"),
52
																FloatArgumentType.getFloat(c, "amount"),
53
																new DamageSource(ResourceArgument.getResource(c, "damageType", Registries.DAMAGE_TYPE), Vec3Argument.getVec3(c, "location"))
54
															)
55
														)
56
												)
57
										)
58
										.then(
59
											Commands.literal("by")
60
												.then(
61
													Commands.argument("entity", EntityArgument.entity())
62
														.executes(
63
															c -> damage(
64
																c.getSource(),
65
																EntityArgument.getEntity(c, "target"),
66
																FloatArgumentType.getFloat(c, "amount"),
67
																new DamageSource(ResourceArgument.getResource(c, "damageType", Registries.DAMAGE_TYPE), EntityArgument.getEntity(c, "entity"))
68
															)
69
														)
70
														.then(
71
															Commands.literal("from")
72
																.then(
73
																	Commands.argument("cause", EntityArgument.entity())
74
																		.executes(
75
																			c -> damage(
76
																				c.getSource(),
77
																				EntityArgument.getEntity(c, "target"),
78
																				FloatArgumentType.getFloat(c, "amount"),
79
																				new DamageSource(
80
																					ResourceArgument.getResource(c, "damageType", Registries.DAMAGE_TYPE),
81
																					EntityArgument.getEntity(c, "entity"),
82
																					EntityArgument.getEntity(c, "cause")
83
																				)
84
																			)
85
																		)
86
																)
87
														)
88
												)
89
										)
90
								)
91
						)
92
				)
93
		);
94
	}
95
96
	private static int damage(final CommandSourceStack stack, final Entity target, final float amount, final DamageSource source) throws CommandSyntaxException {
97
		if (target.hurtServer(stack.getLevel(), source, amount)) {
98
			stack.sendSuccess(() -> Component.translatable("commands.damage.success", amount, target.getDisplayName()), true);
99
			return 1;
100
		} else {
101
			throw ERROR_INVULNERABLE.create();
102
		}
103
	}
104
}
105

Decompiled Minecraft 26.3 source of DamageCommand (net/minecraft/server/commands/DamageCommand.java) with Mojang official mappings, stored verbatim on the site.