From 05d8e01bee55f16017312a8cbcc046d0459346ed Mon Sep 17 00:00:00 2001 From: haykam821 <24855774+haykam821@users.noreply.github.com> Date: Wed, 1 Jul 2020 17:02:46 -0400 Subject: [PATCH] Add cstacksize command --- .../clientcommands/ClientCommands.java | 1 + .../command/StackSizeCommand.java | 61 +++++++++++++++++++ .../assets/clientcommands/lang/en_us.json | 5 ++ 3 files changed, 67 insertions(+) create mode 100644 src/main/java/net/earthcomputer/clientcommands/command/StackSizeCommand.java diff --git a/src/main/java/net/earthcomputer/clientcommands/ClientCommands.java b/src/main/java/net/earthcomputer/clientcommands/ClientCommands.java index 034299425..cbd1394a6 100644 --- a/src/main/java/net/earthcomputer/clientcommands/ClientCommands.java +++ b/src/main/java/net/earthcomputer/clientcommands/ClientCommands.java @@ -43,6 +43,7 @@ public static void registerCommands(CommandDispatcher dispa GlowCommand.register(dispatcher); GetDataCommand.register(dispatcher); ScriptCommand.register(dispatcher); + StackSizeCommand.register(dispatcher); CrackRNGCommand.register(dispatcher); diff --git a/src/main/java/net/earthcomputer/clientcommands/command/StackSizeCommand.java b/src/main/java/net/earthcomputer/clientcommands/command/StackSizeCommand.java new file mode 100644 index 000000000..828cafd65 --- /dev/null +++ b/src/main/java/net/earthcomputer/clientcommands/command/StackSizeCommand.java @@ -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 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); + } + +} diff --git a/src/main/resources/assets/clientcommands/lang/en_us.json b/src/main/resources/assets/clientcommands/lang/en_us.json index a7b962355..e7ff80e53 100644 --- a/src/main/resources/assets/clientcommands/lang/en_us.json +++ b/src/main/resources/assets/clientcommands/lang/en_us.json @@ -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",