| 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.Dynamic2CommandExceptionType;
|
| 7 | import java.util.Collection;
|
| 8 | import net.minecraft.commands.CommandBuildContext;
|
| 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.item.ItemArgument;
|
| 13 | import net.minecraft.commands.arguments.item.ItemInput;
|
| 14 | import net.minecraft.network.chat.Component;
|
| 15 | import net.minecraft.server.level.ServerPlayer;
|
| 16 | import net.minecraft.sounds.SoundEvents;
|
| 17 | import net.minecraft.sounds.SoundSource;
|
| 18 | import net.minecraft.world.entity.item.ItemEntity;
|
| 19 | import net.minecraft.world.item.ItemStack;
|
| 20 |
|
| 21 | public class GiveCommand {
|
| 22 | private static final Dynamic2CommandExceptionType TOO_MANY_ITEMS = new Dynamic2CommandExceptionType(
|
| 23 | (maxCount, item) -> Component.translatableEscape("commands.give.failed.toomanyitems", maxCount, item)
|
| 24 | );
|
| 25 | private static final CommandResponseTracker.MessagesWithArgs<ServerPlayer, Integer, ItemStack> RESPONSE_GIVE = CommandResponseTracker.messages(
|
| 26 | (player, var1, count, prototypeItemStack) -> Component.translatable(
|
| 27 | "commands.give.success.single", count, prototypeItemStack.getDisplayName(), player.getDisplayName()
|
| 28 | ),
|
| 29 | (playerCount, var1, count, prototypeItemStack) -> Component.translatable(
|
| 30 | "commands.give.success.multiple", count, prototypeItemStack.getDisplayName(), playerCount
|
| 31 | )
|
| 32 | );
|
| 33 | public static final int MAX_ALLOWED_ITEMSTACKS = 100;
|
| 34 |
|
| 35 | public static void register(final CommandDispatcher<CommandSourceStack> dispatcher, final CommandBuildContext context) {
|
| 36 | dispatcher.register(
|
| 37 | Commands.literal("give")
|
| 38 | .requires(Commands.hasPermission(Commands.LEVEL_GAMEMASTERS))
|
| 39 | .then(
|
| 40 | Commands.argument("targets", EntityArgument.players())
|
| 41 | .then(
|
| 42 | Commands.argument("item", ItemArgument.item(context))
|
| 43 | .executes(c -> giveItem(c.getSource(), ItemArgument.getItem(c, "item"), EntityArgument.getPlayers(c, "targets"), 1))
|
| 44 | .then(
|
| 45 | Commands.argument("count", IntegerArgumentType.integer(1))
|
| 46 | .executes(
|
| 47 | c -> giveItem(c.getSource(), ItemArgument.getItem(c, "item"), EntityArgument.getPlayers(c, "targets"), IntegerArgumentType.getInteger(c, "count"))
|
| 48 | )
|
| 49 | )
|
| 50 | )
|
| 51 | )
|
| 52 | );
|
| 53 | }
|
| 54 |
|
| 55 | private static int giveItem(final CommandSourceStack source, final ItemInput input, final Collection<ServerPlayer> players, final int count) throws CommandSyntaxException {
|
| 56 | CommandResponseTracker<ServerPlayer> tracker = CommandResponseTracker.create();
|
| 57 | ItemStack prototypeItemStack = input.createItemStack(1);
|
| 58 | int maxStackSize = prototypeItemStack.getMaxStackSize();
|
| 59 | int maxAllowedCount = maxStackSize * 100;
|
| 60 | if (count > maxAllowedCount) {
|
| 61 | throw TOO_MANY_ITEMS.create(maxAllowedCount, prototypeItemStack.getDisplayName());
|
| 62 | }
|
| 63 |
|
| 64 | for (ServerPlayer player : players) {
|
| 65 | int remaining = count;
|
| 66 |
|
| 67 | while (remaining > 0) {
|
| 68 | int size = Math.min(maxStackSize, remaining);
|
| 69 | remaining -= size;
|
| 70 | ItemStack copyToDrop = prototypeItemStack.copyWithCount(size);
|
| 71 | boolean added = player.getInventory().add(copyToDrop);
|
| 72 | if (added && copyToDrop.isEmpty()) {
|
| 73 | ItemEntity drop = player.createItemStackToDrop(prototypeItemStack.copy(), false, false);
|
| 74 | if (drop != null) {
|
| 75 | drop.makeFakeItem();
|
| 76 | drop.level().addFreshEntity(drop);
|
| 77 | }
|
| 78 |
|
| 79 | player.level()
|
| 80 | .playSound(
|
| 81 | null,
|
| 82 | player.getX(),
|
| 83 | player.getY(),
|
| 84 | player.getZ(),
|
| 85 | SoundEvents.ITEM_PICKUP,
|
| 86 | SoundSource.PLAYERS,
|
| 87 | 0.2F,
|
| 88 | ((player.getRandom().nextFloat() - player.getRandom().nextFloat()) * 0.7F + 1.0F) * 2.0F
|
| 89 | );
|
| 90 | player.containerMenu.broadcastChanges();
|
| 91 | } else {
|
| 92 | ItemEntity drop = player.createItemStackToDrop(copyToDrop, false, false);
|
| 93 | if (drop != null) {
|
| 94 | drop.setNoPickUpDelay();
|
| 95 | drop.setTarget(player.getUUID());
|
| 96 | drop.level().addFreshEntity(drop);
|
| 97 | }
|
| 98 | }
|
| 99 | }
|
| 100 |
|
| 101 | tracker.track(player);
|
| 102 | }
|
| 103 |
|
| 104 | return tracker.sendFeedback(source, true, RESPONSE_GIVE, count, prototypeItemStack);
|
| 105 | }
|
| 106 | }
|
| 107 |
|