Skip to content

Commit eb9897e

Browse files
authored
Add the chotbar command (#228)
1 parent 36bc68a commit eb9897e

File tree

3 files changed

+83
-0
lines changed

3 files changed

+83
-0
lines changed

src/main/java/net/earthcomputer/clientcommands/ClientCommands.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ public static void registerCommands(CommandDispatcher<ServerCommandSource> dispa
5656
CPlaySoundCommand.register(dispatcher);
5757
CStopSoundCommand.register(dispatcher);
5858
FovCommand.register(dispatcher);
59+
HotbarCommand.register(dispatcher);
5960

6061
CrackRNGCommand.register(dispatcher);
6162

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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+
}

src/main/resources/assets/clientcommands/lang/en_us.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,9 @@
7676
"commands.cglow.entity.success": "Glowing %d entities",
7777
"commands.cglow.area.success": "Glowing %d area(s)",
7878

79+
"commands.chotbar.notCreative": "Player must be in creative mode to restore item hotbars",
80+
"commands.chotbar.restoredHotbar": "Restored item hotbar %d",
81+
7982
"commands.cplaysound.success": "Played sound %s to self",
8083

8184
"commands.crelog.failed": "Failed to relog",

0 commit comments

Comments
 (0)