Skip to content

Commit ffe3708

Browse files
Enhance DropCommand to support item quantity (#5664)
1 parent 8e0cb5f commit ffe3708

File tree

2 files changed

+36
-7
lines changed

2 files changed

+36
-7
lines changed

src/main/java/meteordevelopment/meteorclient/commands/commands/DropCommand.java

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55

66
package meteordevelopment.meteorclient.commands.commands;
77

8+
import com.mojang.brigadier.arguments.IntegerArgumentType;
89
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
10+
import com.mojang.brigadier.context.CommandContext;
911
import com.mojang.brigadier.exceptions.CommandSyntaxException;
1012
import com.mojang.brigadier.exceptions.SimpleCommandExceptionType;
1113
import meteordevelopment.meteorclient.commands.Command;
@@ -67,17 +69,38 @@ public void build(LiteralArgumentBuilder<CommandSource> builder) {
6769
})));
6870

6971
// Specific item
70-
builder.then(argument("item", ItemStackArgumentType.itemStack(REGISTRY_ACCESS)).executes(context -> drop(player -> {
71-
ItemStack stack = ItemStackArgumentType.getItemStackArgument(context, "item").createStack(1, false);
72+
builder.then(argument("item", ItemStackArgumentType.itemStack(REGISTRY_ACCESS))
73+
.executes(context -> drop(player -> {
74+
dropItem(player, context, Integer.MAX_VALUE);
75+
}))
76+
.then(argument("amount", IntegerArgumentType.integer(1))
77+
.executes(context -> drop(player -> {
78+
int amount = IntegerArgumentType.getInteger(context, "amount");
79+
dropItem(player, context, amount);
80+
})))
81+
);
82+
}
7283

73-
if (stack == null || stack.getItem() == Items.AIR) throw NO_SUCH_ITEM.create();
84+
private void dropItem(ClientPlayerEntity player, CommandContext<CommandSource> context, int amount) throws CommandSyntaxException {
85+
ItemStack stack = ItemStackArgumentType.getItemStackArgument(context, "item").createStack(1, false);
86+
if (stack == null || stack.getItem() == Items.AIR) throw NO_SUCH_ITEM.create();
7487

75-
for (int i = 0; i < player.getInventory().size(); i++) {
76-
if (stack.getItem() == player.getInventory().getStack(i).getItem()) {
77-
InvUtils.drop().slot(i);
88+
for (int i = 0; i < player.getInventory().size() && amount > 0; i++) {
89+
ItemStack invStack = player.getInventory().getStack(i);
90+
if (invStack.isEmpty() || stack.getItem() != invStack.getItem()) continue;
91+
92+
int dropCount = Math.min(amount, invStack.getCount());
93+
94+
if (dropCount == invStack.getCount()) {
95+
InvUtils.drop().slot(i);
96+
} else {
97+
for (int j = 0; j < dropCount; j++) {
98+
InvUtils.dropOne().slot(i);
7899
}
79100
}
80-
})));
101+
102+
amount -= dropCount;
103+
}
81104
}
82105

83106
private int drop(PlayerConsumer consumer) throws CommandSyntaxException {

src/main/java/meteordevelopment/meteorclient/utils/player/InvUtils.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,12 @@ public static Action drop() {
207207
return ACTION;
208208
}
209209

210+
public static Action dropOne() {
211+
ACTION.type = SlotActionType.THROW;
212+
ACTION.data = 0;
213+
return ACTION;
214+
}
215+
210216
public static void dropHand() {
211217
if (!mc.player.currentScreenHandler.getCursorStack().isEmpty()) mc.interactionManager.clickSlot(mc.player.currentScreenHandler.syncId, ScreenHandler.EMPTY_SPACE_SLOT_INDEX, 0, SlotActionType.PICKUP, mc.player);
212218
}

0 commit comments

Comments
 (0)