ClearInventoryCommands .java

net.minecraft.server.commands.ClearInventoryCommands (class, 90 lines): members: ERROR_DISPATCH, ERROR_MULTIPLE, RESPONSE_TEST, ERROR_SINGLE, predicate.

net.minecraft.server.commandsclass90 linesserver4.4 KB

API Surface (1)

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

Fields & Constants (6)

ERROR_SINGLE
ERROR_MULTIPLE
RESPONSE_TEST
RESPONSE_CLEAR
ERROR_DISPATCH
predicate

String literals (10)

"clear.failed.single"
"clear.failed.multiple"
"commands.clear.test.single"
"commands.clear.test.multiple"
"commands.clear.success.single"
"commands.clear.success.multiple"
"clear"
"targets"
"item"
"maxCount"

Full Source

ClearInventoryCommands.java90 lines · Minecraft 26.3 · verbatim
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.exceptions.CommandSyntaxException;
6
import com.mojang.brigadier.exceptions.DynamicCommandExceptionType;
7
import java.util.Collection;
8
import java.util.Collections;
9
import java.util.function.Predicate;
10
import net.minecraft.commands.CommandBuildContext;
11
import net.minecraft.commands.CommandSourceStack;
12
import net.minecraft.commands.Commands;
13
import net.minecraft.commands.arguments.EntityArgument;
14
import net.minecraft.commands.arguments.item.ItemPredicateArgument;
15
import net.minecraft.network.chat.Component;
16
import net.minecraft.server.level.ServerPlayer;
17
import net.minecraft.world.item.ItemStack;
18
19
public class ClearInventoryCommands {
20
	private static final DynamicCommandExceptionType ERROR_SINGLE = new DynamicCommandExceptionType(
21
		name -> Component.translatableEscape("clear.failed.single", name)
22
	);
23
	private static final DynamicCommandExceptionType ERROR_MULTIPLE = new DynamicCommandExceptionType(
24
		count -> Component.translatableEscape("clear.failed.multiple", count)
25
	);
26
	private static final CommandResponseTracker.Messages<ServerPlayer> RESPONSE_TEST = CommandResponseTracker.messages(
27
		(player, totalValue) -> Component.translatable("commands.clear.test.single", totalValue, player.getDisplayName()),
28
		(playerCount, totalValue) -> Component.translatable("commands.clear.test.multiple", totalValue, playerCount)
29
	);
30
	private static final CommandResponseTracker.Messages<ServerPlayer> RESPONSE_CLEAR = CommandResponseTracker.messages(
31
		(player, totalValue) -> Component.translatable("commands.clear.success.single", totalValue, player.getDisplayName()),
32
		(playerCount, totalValue) -> Component.translatable("commands.clear.success.multiple", totalValue, playerCount)
33
	);
34
	private static final CommandResponseTracker.Dispatch<CommandSyntaxException, ServerPlayer> ERROR_DISPATCH = new CommandResponseTracker.Dispatch<>(
35
		(player, var1) -> ERROR_SINGLE.create(player.getDisplayName()), (playerCount, var1) -> ERROR_MULTIPLE.create(playerCount)
36
	);
37
38
	public static void register(final CommandDispatcher<CommandSourceStack> dispatcher, final CommandBuildContext context) {
39
		dispatcher.register(
40
			Commands.literal("clear")
41
				.requires(Commands.hasPermission(Commands.LEVEL_GAMEMASTERS))
42
				.executes(c -> clearUnlimited(c.getSource(), Collections.singleton(c.getSource().getPlayerOrException()), var0x -> true))
43
				.then(
44
					Commands.argument("targets", EntityArgument.players())
45
						.executes(c -> clearUnlimited(c.getSource(), EntityArgument.getPlayers(c, "targets"), var0x -> true))
46
						.then(
47
							Commands.argument("item", ItemPredicateArgument.itemPredicate(context))
48
								.executes(c -> clearUnlimited(c.getSource(), EntityArgument.getPlayers(c, "targets"), ItemPredicateArgument.getItemPredicate(c, "item")))
49
								.then(
50
									Commands.argument("maxCount", IntegerArgumentType.integer(0))
51
										.executes(
52
											c -> clearInventory(
53
												c.getSource(),
54
												EntityArgument.getPlayers(c, "targets"),
55
												ItemPredicateArgument.getItemPredicate(c, "item"),
56
												IntegerArgumentType.getInteger(c, "maxCount")
57
											)
58
										)
59
								)
60
						)
61
				)
62
		);
63
	}
64
65
	private static int clearUnlimited(final CommandSourceStack source, final Collection<ServerPlayer> players, final Predicate<ItemStack> predicate) throws CommandSyntaxException {
66
		return clearInventory(source, players, predicate, -1);
67
	}
68
69
	private static int clearInventory(
70
		final CommandSourceStack source, final Collection<ServerPlayer> players, final Predicate<ItemStack> predicate, final int maxCount
71
	) throws CommandSyntaxException {
72
		CommandResponseTracker<ServerPlayer> tracker = CommandResponseTracker.create();
73
		boolean countingOnly = maxCount == 0;
74
75
		for (ServerPlayer player : players) {
76
			tracker.track(player, player.getInventory().clearOrCountMatchingItems(predicate, countingOnly, maxCount, player.inventoryMenu.getCraftSlots()));
77
			if (!countingOnly) {
78
				player.containerMenu.broadcastChanges();
79
				player.inventoryMenu.slotsChanged(player.getInventory());
80
			}
81
		}
82
83
		if (tracker.totalValue() == 0) {
84
			throw (CommandSyntaxException)tracker.dispatch(CommandResponseTracker.ElementType.ANY, ERROR_DISPATCH);
85
		} else {
86
			return tracker.sendFeedback(source, true, CommandResponseTracker.ElementType.NON_ZERO, maxCount == 0 ? RESPONSE_TEST : RESPONSE_CLEAR);
87
		}
88
	}
89
}
90

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