The /give
command has a built-in limit on items given (100 stacks) to prevent players from creating too many items and lagging or crashing the server. However, even though the command gives no items and produces a failure message when trying to overcome the aforementioned limit, the command actually technically succeeds, which can break data packs expecting the command’s success
value to be 0.
How to reproduce:
Execute the following command:
/execute store success storage mc-302537:test success_value int 1 run give @s minecraft:stick 6401
Execute the following command:
/data get storage mc-302537:test success_value
→ ❌ The value stored is 1, indicating the command executed by the
run
argument of the first command succeeded.
Expected result:
The /give
command would fail when trying to give more than 100 stacks of an item.
Observed result:
The /give
command succeeds when trying to give more than 100 stacks of an item despite no items being given and a failure message being sent.
Code analysis:
This code analysis uses 1.21.9’s code, decompiled and deobfuscated using McDeob.
In the net.minecraft.server.commands.GiveCommand
class, there is a method called giveItem
, which is called when the command is run. The start of this method is shown below:
private static int giveItem(CommandSourceStack commandsourcestack, ItemInput iteminput, Collection<ServerPlayer> collection, int i) throws CommandSyntaxException {
ItemStack itemstack = iteminput.createItemStack(1, false);
int j = itemstack.getMaxStackSize();
int k = j * 100;
if (i > k) {
commandsourcestack.sendFailure(Component.translatable("commands.give.failed.toomanyitems", k, itemstack.getDisplayName()));
return 0;
} else {
...
When the count
argument of the command is set to a number greater than 100 times the maximum stack size of the item to be given, the command sends a failure message and returns a Brigadier return value. When a command returns a value, it is considered successful, and its success
value is therefore set to 1. Most other commands, when failing, throw a CommandSyntaxException
instead of returning a value, since the game only sets a command’s success
value to 0 when such an exception is thrown.
Comments 0
No comments.