minecraft-source/src/net/minecraft/server/commands/GiveCommand.java

67 lines
4.3 KiB
Java

package net.minecraft.server.commands;
import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import net.minecraft.world.entity.item.ItemEntity;
import net.minecraft.world.item.ItemStack;
import java.util.Iterator;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.TranslatableComponent;
import net.minecraft.world.entity.player.Player;
import net.minecraft.sounds.SoundSource;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.server.level.ServerPlayer;
import java.util.Collection;
import net.minecraft.commands.arguments.item.ItemInput;
import com.mojang.brigadier.arguments.IntegerArgumentType;
import net.minecraft.commands.arguments.item.ItemArgument;
import com.mojang.brigadier.builder.RequiredArgumentBuilder;
import com.mojang.brigadier.arguments.ArgumentType;
import net.minecraft.commands.arguments.EntityArgument;
import net.minecraft.commands.Commands;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import net.minecraft.commands.CommandSourceStack;
import com.mojang.brigadier.CommandDispatcher;
public class GiveCommand {
public static void register(final CommandDispatcher<CommandSourceStack> commandDispatcher) {
commandDispatcher.register((LiteralArgumentBuilder)((LiteralArgumentBuilder)Commands.literal("give").requires(cq -> cq.hasPermission(2))).then(Commands.argument("targets", (com.mojang.brigadier.arguments.ArgumentType<Object>)EntityArgument.players()).then(((RequiredArgumentBuilder)Commands.argument("item", (com.mojang.brigadier.arguments.ArgumentType<Object>)ItemArgument.item()).executes(commandContext -> giveItem((CommandSourceStack)commandContext.getSource(), ItemArgument.getItem((com.mojang.brigadier.context.CommandContext<Object>)commandContext, "item"), EntityArgument.getPlayers((CommandContext<CommandSourceStack>)commandContext, "targets"), 1))).then(Commands.argument("count", (com.mojang.brigadier.arguments.ArgumentType<Object>)IntegerArgumentType.integer(1)).executes(commandContext -> giveItem((CommandSourceStack)commandContext.getSource(), ItemArgument.getItem((com.mojang.brigadier.context.CommandContext<Object>)commandContext, "item"), EntityArgument.getPlayers((CommandContext<CommandSourceStack>)commandContext, "targets"), IntegerArgumentType.getInteger(commandContext, "count")))))));
}
private static int giveItem(final CommandSourceStack cq, final ItemInput ek, final Collection<ServerPlayer> collection, final int integer) throws CommandSyntaxException {
for (final ServerPlayer xe6 : collection) {
int integer2 = integer;
while (integer2 > 0) {
final int integer3 = Math.min(ek.getItem().getMaxStackSize(), integer2);
integer2 -= integer3;
final ItemStack bek9 = ek.createItemStack(integer3, false);
final boolean boolean10 = xe6.inventory.add(bek9);
if (!boolean10 || !bek9.isEmpty()) {
final ItemEntity avy11 = xe6.drop(bek9, false);
if (avy11 == null) {
continue;
}
avy11.setNoPickUpDelay();
avy11.setOwner(xe6.getUUID());
}
else {
bek9.setCount(1);
final ItemEntity avy11 = xe6.drop(bek9, false);
if (avy11 != null) {
avy11.makeFakeItem();
}
xe6.level.playSound(null, xe6.getX(), xe6.getY(), xe6.getZ(), SoundEvents.ITEM_PICKUP, SoundSource.PLAYERS, 0.2f, ((xe6.getRandom().nextFloat() - xe6.getRandom().nextFloat()) * 0.7f + 1.0f) * 2.0f);
xe6.inventoryMenu.broadcastChanges();
}
}
}
if (collection.size() == 1) {
cq.sendSuccess(new TranslatableComponent("commands.give.success.single", new Object[] { integer, ek.createItemStack(integer, false).getDisplayName(), collection.iterator().next().getDisplayName() }), true);
}
else {
cq.sendSuccess(new TranslatableComponent("commands.give.success.single", new Object[] { integer, ek.createItemStack(integer, false).getDisplayName(), collection.size() }), true);
}
return collection.size();
}
}