RecipeCommand .java

net.minecraft.server.commands.RecipeCommand (class, 93 lines): members: ERROR_TAKE_FAILED, LEARNABLE_RECIPE, RESPONSE_TAKE, ERROR_GIVE_FAILED, register.

net.minecraft.server.commandsclass93 linesserver4.3 KB

API Surface (1)

void register(final CommandDispatcher<CommandSourceStack> dispatcher)

Fields & Constants (5)

ERROR_GIVE_FAILED
ERROR_TAKE_FAILED
LEARNABLE_RECIPE
RESPONSE_GIVE
RESPONSE_TAKE

String literals (10)

"commands.recipe.give.failed"
"commands.recipe.take.failed"
"commands.recipe.give.success.single"
"commands.recipe.give.success.multiple"
"commands.recipe.take.success.single"
"commands.recipe.take.success.multiple"
"recipe"
"give"
"targets"
"take"

Full Source

RecipeCommand.java93 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 java.util.Collection;
7
import java.util.Collections;
8
import java.util.function.Predicate;
9
import net.minecraft.commands.CommandSourceStack;
10
import net.minecraft.commands.Commands;
11
import net.minecraft.commands.arguments.EntityArgument;
12
import net.minecraft.commands.arguments.ResourceKeyArgument;
13
import net.minecraft.core.registries.Registries;
14
import net.minecraft.network.chat.Component;
15
import net.minecraft.server.level.ServerPlayer;
16
import net.minecraft.world.item.crafting.Recipe;
17
import net.minecraft.world.item.crafting.RecipeHolder;
18
19
public class RecipeCommand {
20
	private static final SimpleCommandExceptionType ERROR_GIVE_FAILED = new SimpleCommandExceptionType(Component.translatable("commands.recipe.give.failed"));
21
	private static final SimpleCommandExceptionType ERROR_TAKE_FAILED = new SimpleCommandExceptionType(Component.translatable("commands.recipe.take.failed"));
22
	private static final Predicate<Recipe<?>> LEARNABLE_RECIPE = r -> !r.isSpecial();
23
	private static final CommandResponseTracker.Messages<ServerPlayer> RESPONSE_GIVE = CommandResponseTracker.messages(
24
		ERROR_GIVE_FAILED,
25
		(player, totalValue) -> Component.translatable("commands.recipe.give.success.single", totalValue, player.getDisplayName()),
26
		(playerCount, totalValue) -> Component.translatable("commands.recipe.give.success.multiple", totalValue, playerCount)
27
	);
28
	private static final CommandResponseTracker.Messages<ServerPlayer> RESPONSE_TAKE = CommandResponseTracker.messages(
29
		ERROR_TAKE_FAILED::create,
30
		(player, totalValue) -> Component.translatable("commands.recipe.take.success.single", totalValue, player.getDisplayName()),
31
		(playerCount, totalValue) -> Component.translatable("commands.recipe.take.success.multiple", totalValue, playerCount)
32
	);
33
34
	public static void register(final CommandDispatcher<CommandSourceStack> dispatcher) {
35
		dispatcher.register(
36
			Commands.literal("recipe")
37
				.requires(Commands.hasPermission(Commands.LEVEL_GAMEMASTERS))
38
				.then(
39
					Commands.literal("give")
40
						.then(
41
							Commands.argument("targets", EntityArgument.players())
42
								.then(
43
									Commands.argument("recipe", ResourceKeyArgument.key(Registries.RECIPE, LEARNABLE_RECIPE))
44
										.executes(c -> giveRecipes(c.getSource(), EntityArgument.getPlayers(c, "targets"), Collections.singleton(ResourceKeyArgument.getRecipe(c, "recipe"))))
45
								)
46
								.then(
47
									Commands.literal("*")
48
										.executes(
49
											c -> giveRecipes(c.getSource(), EntityArgument.getPlayers(c, "targets"), c.getSource().getServer().getRecipeManager().getLearnableRecipes())
50
										)
51
								)
52
						)
53
				)
54
				.then(
55
					Commands.literal("take")
56
						.then(
57
							Commands.argument("targets", EntityArgument.players())
58
								.then(
59
									Commands.argument("recipe", ResourceKeyArgument.key(Registries.RECIPE, LEARNABLE_RECIPE))
60
										.executes(c -> takeRecipes(c.getSource(), EntityArgument.getPlayers(c, "targets"), Collections.singleton(ResourceKeyArgument.getRecipe(c, "recipe"))))
61
								)
62
								.then(
63
									Commands.literal("*")
64
										.executes(
65
											c -> takeRecipes(c.getSource(), EntityArgument.getPlayers(c, "targets"), c.getSource().getServer().getRecipeManager().getLearnableRecipes())
66
										)
67
								)
68
						)
69
				)
70
		);
71
	}
72
73
	private static int giveRecipes(final CommandSourceStack source, final Collection<ServerPlayer> players, final Collection<RecipeHolder<?>> recipes) throws CommandSyntaxException {
74
		CommandResponseTracker<ServerPlayer> response = CommandResponseTracker.create();
75
76
		for (ServerPlayer player : players) {
77
			response.track(player, player.awardRecipes(recipes));
78
		}
79
80
		return response.sendFeedback(source, true, RESPONSE_GIVE);
81
	}
82
83
	private static int takeRecipes(final CommandSourceStack source, final Collection<ServerPlayer> players, final Collection<RecipeHolder<?>> recipes) throws CommandSyntaxException {
84
		CommandResponseTracker<ServerPlayer> response = CommandResponseTracker.create();
85
86
		for (ServerPlayer player : players) {
87
			response.track(player, player.resetRecipes(recipes));
88
		}
89
90
		return response.sendFeedback(source, true, RESPONSE_TAKE);
91
	}
92
}
93

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