-
-
Notifications
You must be signed in to change notification settings - Fork 116
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #128 from haykam821/stack-size
Add cstacksize command
- Loading branch information
Showing
3 changed files
with
67 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
src/main/java/net/earthcomputer/clientcommands/command/StackSizeCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters