GiveCommand.java
net.minecraft.server.commands.GiveCommand
信息
- 全限定名:net.minecraft.server.commands.GiveCommand
- 类型:public class
- 包:net.minecraft.server.commands
- 源码路径:src/main/java/net/minecraft/server/commands/GiveCommand.java
- 起始行号:L20
- 职责:
TODO
字段/常量
MAX_ALLOWED_ITEMSTACKS- 类型:
int - 修饰符:
public static final - 源码定位:
L21 - 说明:
TODO
- 类型:
内部类/嵌套类型
- 无
构造器
- 无
方法
下面的方法块按源码顺序生成。
public static void register(CommandDispatcher<CommandSourceStack> dispatcher, CommandBuildContext context) @ L23
- 方法名:register
- 源码定位:L23
- 返回类型:void
- 修饰符:public static
参数:
- dispatcher: CommandDispatcher
- context: CommandBuildContext
说明:
TODO
private static int giveItem(CommandSourceStack source, ItemInput input, Collection<ServerPlayer> players, int count) @ L48
- 方法名:giveItem
- 源码定位:L48
- 返回类型:int
- 修饰符:private static
参数:
- source: CommandSourceStack
- input: ItemInput
- players: Collection
- count: int
说明:
TODO
代码
public class GiveCommand {
public static final int MAX_ALLOWED_ITEMSTACKS = 100;
public static void register(CommandDispatcher<CommandSourceStack> dispatcher, CommandBuildContext context) {
dispatcher.register(
Commands.literal("give")
.requires(Commands.hasPermission(Commands.LEVEL_GAMEMASTERS))
.then(
Commands.argument("targets", EntityArgument.players())
.then(
Commands.argument("item", ItemArgument.item(context))
.executes(c -> giveItem(c.getSource(), ItemArgument.getItem(c, "item"), EntityArgument.getPlayers(c, "targets"), 1))
.then(
Commands.argument("count", IntegerArgumentType.integer(1))
.executes(
c -> giveItem(
c.getSource(),
ItemArgument.getItem(c, "item"),
EntityArgument.getPlayers(c, "targets"),
IntegerArgumentType.getInteger(c, "count")
)
)
)
)
)
);
}
private static int giveItem(CommandSourceStack source, ItemInput input, Collection<ServerPlayer> players, int count) throws CommandSyntaxException {
ItemStack prototypeItemStack = input.createItemStack(1);
int maxStackSize = prototypeItemStack.getMaxStackSize();
int maxAllowedCount = maxStackSize * 100;
if (count > maxAllowedCount) {
source.sendFailure(Component.translatable("commands.give.failed.toomanyitems", maxAllowedCount, prototypeItemStack.getDisplayName()));
return 0;
} else {
for (ServerPlayer player : players) {
int remaining = count;
while (remaining > 0) {
int size = Math.min(maxStackSize, remaining);
remaining -= size;
ItemStack copyToDrop = prototypeItemStack.copyWithCount(size);
boolean added = player.getInventory().add(copyToDrop);
if (added && copyToDrop.isEmpty()) {
ItemEntity drop = player.drop(prototypeItemStack.copy(), 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();
} else {
ItemEntity drop = player.drop(copyToDrop, false);
if (drop != null) {
drop.setNoPickUpDelay();
drop.setTarget(player.getUUID());
}
}
}
}
if (players.size() == 1) {
source.sendSuccess(
() -> Component.translatable(
"commands.give.success.single", count, prototypeItemStack.getDisplayName(), players.iterator().next().getDisplayName()
),
true
);
} else {
source.sendSuccess(
() -> Component.translatable("commands.give.success.single", count, prototypeItemStack.getDisplayName(), players.size()), true
);
}
return players.size();
}
}
}引用的其他类
-
- 引用位置:
参数
- 引用位置:
-
- 引用位置:
参数
- 引用位置:
-
- 引用位置:
方法调用 - 关联成员:
Commands.argument(), Commands.hasPermission(), Commands.literal()
- 引用位置:
-
- 引用位置:
方法调用 - 关联成员:
EntityArgument.getPlayers(), EntityArgument.players()
- 引用位置:
-
- 引用位置:
方法调用 - 关联成员:
ItemArgument.getItem(), ItemArgument.item()
- 引用位置:
-
- 引用位置:
参数
- 引用位置:
-
- 引用位置:
方法调用 - 关联成员:
Component.translatable()
- 引用位置:
-
- 引用位置:
参数
- 引用位置: