81 lines
4.5 KiB
Java
81 lines
4.5 KiB
Java
/*
|
|
* Decompiled with CFR 0.152.
|
|
*
|
|
* Could not load the following classes:
|
|
* com.mojang.brigadier.CommandDispatcher
|
|
* com.mojang.brigadier.arguments.IntegerArgumentType
|
|
* com.mojang.brigadier.builder.LiteralArgumentBuilder
|
|
* com.mojang.brigadier.builder.RequiredArgumentBuilder
|
|
* com.mojang.brigadier.context.CommandContext
|
|
* com.mojang.brigadier.exceptions.CommandSyntaxException
|
|
*/
|
|
package net.minecraft.server.commands;
|
|
|
|
import com.mojang.brigadier.CommandDispatcher;
|
|
import com.mojang.brigadier.arguments.IntegerArgumentType;
|
|
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
|
|
import com.mojang.brigadier.builder.RequiredArgumentBuilder;
|
|
import com.mojang.brigadier.context.CommandContext;
|
|
import com.mojang.brigadier.exceptions.CommandSyntaxException;
|
|
import java.util.Collection;
|
|
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.ItemArgument;
|
|
import net.minecraft.commands.arguments.item.ItemInput;
|
|
import net.minecraft.network.chat.Component;
|
|
import net.minecraft.server.level.ServerPlayer;
|
|
import net.minecraft.sounds.SoundEvents;
|
|
import net.minecraft.sounds.SoundSource;
|
|
import net.minecraft.world.entity.item.ItemEntity;
|
|
import net.minecraft.world.item.ItemStack;
|
|
|
|
public class GiveCommand {
|
|
public static final int MAX_ALLOWED_ITEMSTACKS = 100;
|
|
|
|
public static void register(CommandDispatcher<CommandSourceStack> dispatcher, CommandBuildContext context) {
|
|
dispatcher.register((LiteralArgumentBuilder)((LiteralArgumentBuilder)Commands.literal("give").requires(Commands.hasPermission(Commands.LEVEL_GAMEMASTERS))).then(Commands.argument("targets", EntityArgument.players()).then(((RequiredArgumentBuilder)Commands.argument("item", ItemArgument.item(context)).executes(c -> GiveCommand.giveItem((CommandSourceStack)c.getSource(), ItemArgument.getItem(c, "item"), EntityArgument.getPlayers((CommandContext<CommandSourceStack>)c, "targets"), 1))).then(Commands.argument("count", IntegerArgumentType.integer((int)1)).executes(c -> GiveCommand.giveItem((CommandSourceStack)c.getSource(), ItemArgument.getItem(c, "item"), EntityArgument.getPlayers((CommandContext<CommandSourceStack>)c, "targets"), IntegerArgumentType.getInteger((CommandContext)c, (String)"count")))))));
|
|
}
|
|
|
|
private static int giveItem(CommandSourceStack source, ItemInput input, Collection<ServerPlayer> players, int count) throws CommandSyntaxException {
|
|
ItemStack displayItemStack = input.createItemStack(1, false);
|
|
int maxStackSize = displayItemStack.getMaxStackSize();
|
|
int maxAllowedCount = maxStackSize * 100;
|
|
if (count > maxAllowedCount) {
|
|
source.sendFailure(Component.translatable("commands.give.failed.toomanyitems", maxAllowedCount, displayItemStack.getDisplayName()));
|
|
return 0;
|
|
}
|
|
for (ServerPlayer player : players) {
|
|
int remaining = count;
|
|
while (remaining > 0) {
|
|
ItemEntity drop;
|
|
int size = Math.min(maxStackSize, remaining);
|
|
remaining -= size;
|
|
ItemStack itemStack = input.createItemStack(size, false);
|
|
boolean added = player.getInventory().add(itemStack);
|
|
if (!added || !itemStack.isEmpty()) {
|
|
drop = player.drop(itemStack, false);
|
|
if (drop == null) continue;
|
|
drop.setNoPickUpDelay();
|
|
drop.setTarget(player.getUUID());
|
|
continue;
|
|
}
|
|
drop = player.drop(displayItemStack, false);
|
|
if (drop != null) {
|
|
drop.makeFakeItem();
|
|
}
|
|
player.level().playSound(null, player.getX(), player.getY(), player.getZ(), SoundEvents.ITEM_PICKUP, SoundSource.PLAYERS, 0.2f, ((player.getRandom().nextFloat() - player.getRandom().nextFloat()) * 0.7f + 1.0f) * 2.0f);
|
|
player.containerMenu.broadcastChanges();
|
|
}
|
|
}
|
|
if (players.size() == 1) {
|
|
source.sendSuccess(() -> Component.translatable("commands.give.success.single", count, displayItemStack.getDisplayName(), ((ServerPlayer)players.iterator().next()).getDisplayName()), true);
|
|
} else {
|
|
source.sendSuccess(() -> Component.translatable("commands.give.success.single", count, displayItemStack.getDisplayName(), players.size()), true);
|
|
}
|
|
return players.size();
|
|
}
|
|
}
|
|
|