|
| 1 | +package net.earthcomputer.clientcommands.command; |
| 2 | + |
| 3 | +import static com.mojang.brigadier.arguments.IntegerArgumentType.*; |
| 4 | +import static net.earthcomputer.clientcommands.command.ClientCommandManager.*; |
| 5 | +import static net.minecraft.server.command.CommandManager.*; |
| 6 | + |
| 7 | +import com.mojang.brigadier.CommandDispatcher; |
| 8 | +import com.mojang.brigadier.exceptions.*; |
| 9 | + |
| 10 | +import net.minecraft.client.MinecraftClient; |
| 11 | +import net.minecraft.client.network.ClientPlayerEntity; |
| 12 | +import net.minecraft.client.options.HotbarStorage; |
| 13 | +import net.minecraft.client.options.HotbarStorageEntry; |
| 14 | +import net.minecraft.entity.player.PlayerInventory; |
| 15 | +import net.minecraft.item.ItemStack; |
| 16 | +import net.minecraft.server.command.ServerCommandSource; |
| 17 | +import net.minecraft.text.Text; |
| 18 | +import net.minecraft.text.TranslatableText; |
| 19 | + |
| 20 | +public class HotbarCommand { |
| 21 | + |
| 22 | + private static final SimpleCommandExceptionType NOT_CREATIVE_EXCEPTION = new SimpleCommandExceptionType(new TranslatableText("commands.chotbar.notCreative")); |
| 23 | + |
| 24 | + public static void register(CommandDispatcher<ServerCommandSource> dispatcher) { |
| 25 | + addClientSideCommand("chotbar"); |
| 26 | + |
| 27 | + dispatcher.register(literal("chotbar") |
| 28 | + .then(literal("save") |
| 29 | + // Intentionally one-indexed to match keybindings and creative inventory display |
| 30 | + .then(argument("index", integer(1, 9)) |
| 31 | + .executes(ctx -> save(ctx.getSource(), getInteger(ctx, "index"))))) |
| 32 | + .then(literal("restore") |
| 33 | + .then(argument("index", integer(1, 9)) |
| 34 | + .executes(ctx -> restore(ctx.getSource(), getInteger(ctx, "index")))))); |
| 35 | + } |
| 36 | + |
| 37 | + private static int save(ServerCommandSource source, int index) throws CommandSyntaxException { |
| 38 | + MinecraftClient client = MinecraftClient.getInstance(); |
| 39 | + |
| 40 | + HotbarStorage storage = client.getCreativeHotbarStorage(); |
| 41 | + HotbarStorageEntry entry = storage.getSavedHotbar(index - 1); |
| 42 | + |
| 43 | + for (int slot = 0; slot < PlayerInventory.getHotbarSize(); slot++) { |
| 44 | + entry.set(slot, client.player.inventory.getStack(slot).copy()); |
| 45 | + } |
| 46 | + storage.save(); |
| 47 | + |
| 48 | + Text loadKey = client.options.keyLoadToolbarActivator.getBoundKeyLocalizedText(); |
| 49 | + Text hotbarKey = client.options.keysHotbar[index - 1].getBoundKeyLocalizedText(); |
| 50 | + |
| 51 | + sendFeedback(new TranslatableText("inventory.hotbarSaved", loadKey, hotbarKey)); |
| 52 | + return 0; |
| 53 | + } |
| 54 | + |
| 55 | + private static int restore(ServerCommandSource source, int index) throws CommandSyntaxException { |
| 56 | + MinecraftClient client = MinecraftClient.getInstance(); |
| 57 | + |
| 58 | + ClientPlayerEntity player = client.player; |
| 59 | + if (!player.abilities.creativeMode) { |
| 60 | + throw NOT_CREATIVE_EXCEPTION.create(); |
| 61 | + } |
| 62 | + |
| 63 | + HotbarStorage storage = client.getCreativeHotbarStorage(); |
| 64 | + HotbarStorageEntry entry = storage.getSavedHotbar(index - 1); |
| 65 | + |
| 66 | + for (int slot = 0; slot < PlayerInventory.getHotbarSize(); slot++) { |
| 67 | + ItemStack stack = entry.get(slot).copy(); |
| 68 | + |
| 69 | + player.inventory.setStack(slot, stack); |
| 70 | + client.interactionManager.clickCreativeStack(stack, 36 + slot); |
| 71 | + } |
| 72 | + |
| 73 | + player.playerScreenHandler.sendContentUpdates(); |
| 74 | + |
| 75 | + sendFeedback(new TranslatableText("commands.chotbar.restoredHotbar", index)); |
| 76 | + return 0; |
| 77 | + } |
| 78 | + |
| 79 | +} |
0 commit comments