Skip to content

Commit

Permalink
Merge pull request #128 from haykam821/stack-size
Browse files Browse the repository at this point in the history
Add cstacksize command
  • Loading branch information
Earthcomputer authored Jul 2, 2020
2 parents 799cf98 + 05d8e01 commit becc848
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public static void registerCommands(CommandDispatcher<ServerCommandSource> dispa
GlowCommand.register(dispatcher);
GetDataCommand.register(dispatcher);
ScriptCommand.register(dispatcher);
StackSizeCommand.register(dispatcher);

CrackRNGCommand.register(dispatcher);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package net.earthcomputer.clientcommands.command;

import com.mojang.brigadier.CommandDispatcher;

import net.minecraft.client.MinecraftClient;
import net.minecraft.item.ItemStack;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.text.Text;
import net.minecraft.text.TranslatableText;
import net.minecraft.util.Hand;

import static net.minecraft.command.arguments.ItemStackArgumentType.*;
import static com.mojang.brigadier.arguments.IntegerArgumentType.*;
import static net.earthcomputer.clientcommands.command.ClientCommandManager.*;
import static net.minecraft.server.command.CommandManager.*;

public class StackSizeCommand {

public static void register(CommandDispatcher<ServerCommandSource> dispatcher) {
addClientSideCommand("cstacksize");

dispatcher.register(literal("cstacksize")
.then(argument("count", integer(0))
.then(argument("item", itemStack())
.executes(ctx -> {
ItemStack stack = getItemStackArgument(ctx, "item").createStack(1, false);
return getStackSize(ctx.getSource(), stack, getInteger(ctx, "count"));
}))
.executes(ctx -> {
return getStackSize(ctx.getSource(), getInteger(ctx, "count"));
})));
}

public static int getStackSize(ServerCommandSource source, ItemStack stack, int count) {
int stacks = count / stack.getMaxCount();
int remainder = count % stack.getMaxCount();

if (stack.isEmpty()) {
if (remainder == 0) {
sendFeedback(new TranslatableText("commands.cstacksize.success.empty.exact", count, stacks));
} else {
sendFeedback(new TranslatableText("commands.cstacksize.success.empty", count, stacks, remainder));
}
} else {
Text itemText = stack.toHoverableText();
if (remainder == 0) {
sendFeedback(new TranslatableText("commands.cstacksize.success.exact", count, itemText, stacks));
} else {
sendFeedback(new TranslatableText("commands.cstacksize.success", count, itemText, stacks, remainder));
}
}

return 1;
}

public static int getStackSize(ServerCommandSource source, int count) {
ItemStack heldStack = MinecraftClient.getInstance().player.getStackInHand(Hand.MAIN_HAND).copy();
return getStackSize(source, heldStack, count);
}

}
5 changes: 5 additions & 0 deletions src/main/resources/assets/clientcommands/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@
"commands.cscript.reload.success": "Reloaded scripts",
"commands.cscript.run.success": "Script ran successfully",

"commands.cstacksize.success": "%d %s is %d stacks and %d extra",
"commands.cstacksize.success.empty": "%d items is %d stacks and %d extra",
"commands.cstacksize.success.empty.exact": "%d items is exactly %d stacks",
"commands.cstacksize.success.exact": "%d %s is exactly %d stacks",

"commands.ctask.list.noTasks": "No currently executing tasks",
"commands.ctask.list.success": "%d currently executing tasks",
"commands.ctask.stop.noMatch": "No matching tasks",
Expand Down

0 comments on commit becc848

Please sign in to comment.