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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
|
--- a/net/minecraft/server/commands/GiveCommand.java
+++ b/net/minecraft/server/commands/GiveCommand.java
@@ -2,12 +2,13 @@
import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.arguments.IntegerArgumentType;
-import com.mojang.brigadier.context.CommandContext;
+import com.mojang.brigadier.builder.LiteralArgumentBuilder;
+import com.mojang.brigadier.builder.RequiredArgumentBuilder;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import java.util.Collection;
+import java.util.Iterator;
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;
@@ -16,92 +17,75 @@
import net.minecraft.sounds.SoundEvents;
import net.minecraft.sounds.SoundSource;
import net.minecraft.world.entity.item.ItemEntity;
+import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
public class GiveCommand {
+
public static final int MAX_ALLOWED_ITEMSTACKS = 100;
+ public GiveCommand() {}
+
public static void register(CommandDispatcher<CommandSourceStack> dispatcher, CommandBuildContext context) {
- dispatcher.register(
- Commands.literal("give")
- .requires(source -> source.hasPermission(2))
- .then(
- Commands.argument("targets", EntityArgument.players())
- .then(
- Commands.argument("item", ItemArgument.item(context))
- .executes(
- context1 -> giveItem(
- context1.getSource(), ItemArgument.getItem(context1, "item"), EntityArgument.getPlayers(context1, "targets"), 1
- )
- )
- .then(
- Commands.argument("count", IntegerArgumentType.integer(1))
- .executes(
- context1 -> giveItem(
- context1.getSource(),
- ItemArgument.getItem(context1, "item"),
- EntityArgument.getPlayers(context1, "targets"),
- IntegerArgumentType.getInteger(context1, "count")
- )
- )
- )
- )
- )
- );
+ dispatcher.register((LiteralArgumentBuilder) ((LiteralArgumentBuilder) net.minecraft.commands.Commands.literal("give").requires((commandlistenerwrapper) -> {
+ return commandlistenerwrapper.hasPermission(2);
+ })).then(net.minecraft.commands.Commands.argument("targets", EntityArgument.players()).then(((RequiredArgumentBuilder) net.minecraft.commands.Commands.argument("item", ItemArgument.item(context)).executes((commandcontext) -> {
+ return giveItem((CommandSourceStack) commandcontext.getSource(), ItemArgument.getItem(commandcontext, "item"), EntityArgument.getPlayers(commandcontext, "targets"), 1);
+ })).then(net.minecraft.commands.Commands.argument("count", IntegerArgumentType.integer(1)).executes((commandcontext) -> {
+ return giveItem((CommandSourceStack) commandcontext.getSource(), ItemArgument.getItem(commandcontext, "item"), EntityArgument.getPlayers(commandcontext, "targets"), IntegerArgumentType.getInteger(commandcontext, "count"));
+ })))));
}
private static int giveItem(CommandSourceStack source, ItemInput item, Collection<ServerPlayer> targets, int count) throws CommandSyntaxException {
- int maxStackSize = item.getItem().getMaxStackSize();
- int i = maxStackSize * 100;
- ItemStack itemStack = item.createItemStack(count, false);
- if (count > i) {
- source.sendFailure(Component.translatable("commands.give.failed.toomanyitems", i, itemStack.getDisplayName()));
+ int j = item.getItem().getMaxStackSize();
+ int k = j * 100;
+ ItemStack itemstack = item.createItemStack(count, false);
+
+ if (count > k) {
+ source.sendFailure(Component.translatable("commands.give.failed.toomanyitems", k, itemstack.getDisplayName()));
return 0;
} else {
- for (ServerPlayer serverPlayer : targets) {
- int i1 = count;
+ Iterator iterator = targets.iterator();
- while (i1 > 0) {
- int min = Math.min(maxStackSize, i1);
- i1 -= min;
- ItemStack itemStack1 = item.createItemStack(min, false);
- boolean flag = serverPlayer.getInventory().add(itemStack1);
- if (flag && itemStack1.isEmpty()) {
- itemStack1.setCount(1);
- ItemEntity itemEntity = serverPlayer.drop(itemStack1, false);
- if (itemEntity != null) {
- itemEntity.makeFakeItem();
+ while (iterator.hasNext()) {
+ ServerPlayer entityplayer = (ServerPlayer) iterator.next();
+ int l = count;
+
+ while (l > 0) {
+ int i1 = Math.min(j, l);
+
+ l -= i1;
+ ItemStack itemstack1 = item.createItemStack(i1, false);
+ boolean flag = entityplayer.getInventory().add(itemstack1);
+ ItemEntity entityitem;
+
+ if (flag && itemstack1.isEmpty()) {
+ itemstack1.setCount(1);
+ entityitem = entityplayer.drop(itemstack1, false, false, false); // SPIGOT-2942: Add boolean to call event
+ if (entityitem != null) {
+ entityitem.makeFakeItem();
}
- serverPlayer.level()
- .playSound(
- null,
- serverPlayer.getX(),
- serverPlayer.getY(),
- serverPlayer.getZ(),
- SoundEvents.ITEM_PICKUP,
- SoundSource.PLAYERS,
- 0.2F,
- ((serverPlayer.getRandom().nextFloat() - serverPlayer.getRandom().nextFloat()) * 0.7F + 1.0F) * 2.0F
- );
- serverPlayer.containerMenu.broadcastChanges();
+ entityplayer.level().playSound((Player) null, entityplayer.getX(), entityplayer.getY(), entityplayer.getZ(), SoundEvents.ITEM_PICKUP, SoundSource.PLAYERS, 0.2F, ((entityplayer.getRandom().nextFloat() - entityplayer.getRandom().nextFloat()) * 0.7F + 1.0F) * 2.0F);
+ entityplayer.containerMenu.broadcastChanges();
} else {
- ItemEntity itemEntity = serverPlayer.drop(itemStack1, false);
- if (itemEntity != null) {
- itemEntity.setNoPickUpDelay();
- itemEntity.setTarget(serverPlayer.getUUID());
+ entityitem = entityplayer.drop(itemstack1, false);
+ if (entityitem != null) {
+ entityitem.setNoPickUpDelay();
+ entityitem.setTarget(entityplayer.getUUID());
}
}
}
}
if (targets.size() == 1) {
- source.sendSuccess(
- () -> Component.translatable("commands.give.success.single", count, itemStack.getDisplayName(), targets.iterator().next().getDisplayName()),
- true
- );
+ source.sendSuccess(() -> {
+ return Component.translatable("commands.give.success.single", count, itemstack.getDisplayName(), ((ServerPlayer) targets.iterator().next()).getDisplayName());
+ }, true);
} else {
- source.sendSuccess(() -> Component.translatable("commands.give.success.single", count, itemStack.getDisplayName(), targets.size()), true);
+ source.sendSuccess(() -> {
+ return Component.translatable("commands.give.success.single", count, itemstack.getDisplayName(), targets.size());
+ }, true);
}
return targets.size();
|