SummonCommand .java

net.minecraft.server.commands.SummonCommand (class): members: ERROR_FAILED_PEACEFUL, ERROR_DUPLICATE_UUID, createEntity, INVALID_POSITION, nbt, register.

net.minecraft.server.commandsclass108 linesserver4.6 KB

API Surface (2)

void register(final CommandDispatcher<CommandSourceStack> dispatcher, final CommandBuildContext context)
Entity createEntity(final CommandSourceStack source, final Holder.Reference<EntityType<?>> type, final Vec3 pos, final CompoundTag nbt, final boolean finalize)

Fields & Constants (5)

ERROR_FAILED
ERROR_FAILED_PEACEFUL
ERROR_DUPLICATE_UUID
INVALID_POSITION
nbt

String literals (7)

"commands.summon.failed"
"commands.summon.failed.peaceful"
"commands.summon.failed.uuid"
"commands.summon.invalidPosition"
"summon"
"entity"
"commands.summon.success"

Full Source

SummonCommand.java108 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.server.commands;
2
3
import com.mojang.brigadier.CommandDispatcher;
4
import com.mojang.brigadier.exceptions.CommandSyntaxException;
5
import com.mojang.brigadier.exceptions.SimpleCommandExceptionType;
6
import net.minecraft.commands.CommandBuildContext;
7
import net.minecraft.commands.CommandSourceStack;
8
import net.minecraft.commands.Commands;
9
import net.minecraft.commands.arguments.CompoundTagArgument;
10
import net.minecraft.commands.arguments.ResourceArgument;
11
import net.minecraft.commands.arguments.coordinates.Vec3Argument;
12
import net.minecraft.commands.synchronization.SuggestionProviders;
13
import net.minecraft.core.BlockPos;
14
import net.minecraft.core.Holder;
15
import net.minecraft.core.registries.Registries;
16
import net.minecraft.nbt.CompoundTag;
17
import net.minecraft.network.chat.Component;
18
import net.minecraft.server.level.ServerLevel;
19
import net.minecraft.world.Difficulty;
20
import net.minecraft.world.entity.Entity;
21
import net.minecraft.world.entity.EntitySpawnReason;
22
import net.minecraft.world.entity.EntitySpawnRequest;
23
import net.minecraft.world.entity.EntityType;
24
import net.minecraft.world.entity.Mob;
25
import net.minecraft.world.level.Level;
26
import net.minecraft.world.phys.Vec3;
27
28
public class SummonCommand {
29
	private static final SimpleCommandExceptionType ERROR_FAILED = new SimpleCommandExceptionType(Component.translatable("commands.summon.failed"));
30
	private static final SimpleCommandExceptionType ERROR_FAILED_PEACEFUL = new SimpleCommandExceptionType(
31
		Component.translatable("commands.summon.failed.peaceful")
32
	);
33
	private static final SimpleCommandExceptionType ERROR_DUPLICATE_UUID = new SimpleCommandExceptionType(Component.translatable("commands.summon.failed.uuid"));
34
	private static final SimpleCommandExceptionType INVALID_POSITION = new SimpleCommandExceptionType(Component.translatable("commands.summon.invalidPosition"));
35
36
	public static void register(final CommandDispatcher<CommandSourceStack> dispatcher, final CommandBuildContext context) {
37
		dispatcher.register(
38
			Commands.literal("summon")
39
				.requires(Commands.hasPermission(Commands.LEVEL_GAMEMASTERS))
40
				.then(
41
					Commands.argument("entity", ResourceArgument.resource(context, Registries.ENTITY_TYPE))
42
						.suggests(SuggestionProviders.cast(SuggestionProviders.SUMMONABLE_ENTITIES))
43
						.executes(c -> spawnEntity(c.getSource(), ResourceArgument.getSummonableEntityType(c, "entity"), c.getSource().getPosition(), new CompoundTag(), true))
44
						.then(
45
							Commands.argument("pos", Vec3Argument.vec3())
46
								.executes(
47
									c -> spawnEntity(c.getSource(), ResourceArgument.getSummonableEntityType(c, "entity"), Vec3Argument.getVec3(c, "pos"), new CompoundTag(), true)
48
								)
49
								.then(
50
									Commands.argument("nbt", CompoundTagArgument.compoundTag())
51
										.executes(
52
											c -> spawnEntity(
53
												c.getSource(),
54
												ResourceArgument.getSummonableEntityType(c, "entity"),
55
												Vec3Argument.getVec3(c, "pos"),
56
												CompoundTagArgument.getCompoundTag(c, "nbt"),
57
												false
58
											)
59
										)
60
								)
61
						)
62
				)
63
		);
64
	}
65
66
	public static Entity createEntity(
67
		final CommandSourceStack source, final Holder.Reference<EntityType<?>> type, final Vec3 pos, final CompoundTag nbt, final boolean finalize
68
	) throws CommandSyntaxException {
69
		BlockPos blockPos = BlockPos.containing(pos);
70
		if (!Level.isInSpawnableBounds(blockPos)) {
71
			throw INVALID_POSITION.create();
72
		}
73
74
		if (source.getLevel().getDifficulty() == Difficulty.PEACEFUL && !type.value().isAllowedInPeaceful()) {
75
			throw ERROR_FAILED_PEACEFUL.create();
76
		}
77
78
		CompoundTag entityTag = nbt.copy();
79
		entityTag.putString("id", type.key().identifier().toString());
80
		ServerLevel level = source.getLevel();
81
		Entity entity = EntityType.loadEntityRecursive(entityTag, level, new EntitySpawnRequest(EntitySpawnReason.COMMAND, false), e -> {
82
			e.snapTo(pos.x, pos.y, pos.z, e.getYRot(), e.getXRot());
83
			return e;
84
		});
85
		if (entity == null) {
86
			throw ERROR_FAILED.create();
87
		}
88
89
		if (finalize && entity instanceof Mob mob) {
90
			mob.finalizeSpawn(source.getLevel(), source.getLevel().getCurrentDifficultyAt(entity.blockPosition()), EntitySpawnReason.COMMAND, null);
91
		}
92
93
		if (!level.tryAddFreshEntityWithPassengers(entity)) {
94
			throw ERROR_DUPLICATE_UUID.create();
95
		} else {
96
			return entity;
97
		}
98
	}
99
100
	private static int spawnEntity(
101
		final CommandSourceStack source, final Holder.Reference<EntityType<?>> type, final Vec3 pos, final CompoundTag nbt, final boolean finalize
102
	) throws CommandSyntaxException {
103
		Entity entity = createEntity(source, type, pos, nbt, finalize);
104
		source.sendSuccess(() -> Component.translatable("commands.summon.success", entity.getDisplayName()), true);
105
		return 1;
106
	}
107
}
108

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