File size: 5,236 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
package net.minecraft.server.commands;

import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.arguments.IntegerArgumentType;
import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import com.mojang.brigadier.exceptions.DynamicCommandExceptionType;
import java.util.Collection;
import java.util.Collections;
import java.util.function.Predicate;
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.item.ItemPredicateArgument;
import net.minecraft.network.chat.Component;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.item.ItemStack;

public class ClearInventoryCommands {
    private static final DynamicCommandExceptionType ERROR_SINGLE = new DynamicCommandExceptionType(
        p_308637_ -> Component.translatableEscape("clear.failed.single", p_308637_)
    );
    private static final DynamicCommandExceptionType ERROR_MULTIPLE = new DynamicCommandExceptionType(
        p_308634_ -> Component.translatableEscape("clear.failed.multiple", p_308634_)
    );

    public static void register(CommandDispatcher<CommandSourceStack> p_214421_, CommandBuildContext p_214422_) {
        p_214421_.register(
            Commands.literal("clear")
                .requires(p_136704_ -> p_136704_.hasPermission(2))
                .executes(p_326228_ -> clearUnlimited(p_326228_.getSource(), Collections.singleton(p_326228_.getSource().getPlayerOrException()), p_180029_ -> true))
                .then(
                    Commands.argument("targets", EntityArgument.players())
                        .executes(p_326232_ -> clearUnlimited(p_326232_.getSource(), EntityArgument.getPlayers(p_326232_, "targets"), p_180027_ -> true))
                        .then(
                            Commands.argument("item", ItemPredicateArgument.itemPredicate(p_214422_))
                                .executes(
                                    p_326233_ -> clearUnlimited(
                                            p_326233_.getSource(),
                                            EntityArgument.getPlayers(p_326233_, "targets"),
                                            ItemPredicateArgument.getItemPredicate(p_326233_, "item")
                                        )
                                )
                                .then(
                                    Commands.argument("maxCount", IntegerArgumentType.integer(0))
                                        .executes(
                                            p_326231_ -> clearInventory(
                                                    p_326231_.getSource(),
                                                    EntityArgument.getPlayers(p_326231_, "targets"),
                                                    ItemPredicateArgument.getItemPredicate(p_326231_, "item"),
                                                    IntegerArgumentType.getInteger(p_326231_, "maxCount")
                                                )
                                        )
                                )
                        )
                )
        );
    }

    private static int clearUnlimited(CommandSourceStack p_333436_, Collection<ServerPlayer> p_334305_, Predicate<ItemStack> p_336088_) throws CommandSyntaxException {
        return clearInventory(p_333436_, p_334305_, p_336088_, -1);
    }

    private static int clearInventory(CommandSourceStack p_136706_, Collection<ServerPlayer> p_136707_, Predicate<ItemStack> p_136708_, int p_136709_) throws CommandSyntaxException {
        int i = 0;

        for (ServerPlayer serverplayer : p_136707_) {
            i += serverplayer.getInventory().clearOrCountMatchingItems(p_136708_, p_136709_, serverplayer.inventoryMenu.getCraftSlots());
            serverplayer.containerMenu.broadcastChanges();
            serverplayer.inventoryMenu.slotsChanged(serverplayer.getInventory());
        }

        if (i == 0) {
            if (p_136707_.size() == 1) {
                throw ERROR_SINGLE.create(p_136707_.iterator().next().getName());
            } else {
                throw ERROR_MULTIPLE.create(p_136707_.size());
            }
        } else {
            int j = i;
            if (p_136709_ == 0) {
                if (p_136707_.size() == 1) {
                    p_136706_.sendSuccess(() -> Component.translatable("commands.clear.test.single", j, p_136707_.iterator().next().getDisplayName()), true);
                } else {
                    p_136706_.sendSuccess(() -> Component.translatable("commands.clear.test.multiple", j, p_136707_.size()), true);
                }
            } else if (p_136707_.size() == 1) {
                p_136706_.sendSuccess(() -> Component.translatable("commands.clear.success.single", j, p_136707_.iterator().next().getDisplayName()), true);
            } else {
                p_136706_.sendSuccess(() -> Component.translatable("commands.clear.success.multiple", j, p_136707_.size()), true);
            }

            return i;
        }
    }
}