From 1c0ffb1b738c9f5fb8a7f66b7133911f966ca799 Mon Sep 17 00:00:00 2001 From: joe Date: Sun, 21 Apr 2024 17:22:05 +0100 Subject: [PATCH] Rename citemgroup to ccreativetab --- .../clientcommands/ClientCommands.java | 4 +- ...upCommand.java => CreativeTabCommand.java} | 179 ++++++++++-------- .../assets/clientcommands/lang/en_us.json | 26 +-- 3 files changed, 111 insertions(+), 98 deletions(-) rename src/main/java/net/earthcomputer/clientcommands/command/{ItemGroupCommand.java => CreativeTabCommand.java} (61%) diff --git a/src/main/java/net/earthcomputer/clientcommands/ClientCommands.java b/src/main/java/net/earthcomputer/clientcommands/ClientCommands.java index 0eaa6a47e..7082aad41 100644 --- a/src/main/java/net/earthcomputer/clientcommands/ClientCommands.java +++ b/src/main/java/net/earthcomputer/clientcommands/ClientCommands.java @@ -85,7 +85,7 @@ public void onInitializeClient() { new ModConfigBuilder("clientcommands", Configs.class).build(); - ItemGroupCommand.registerItemGroups(); + CreativeTabCommand.registerItemGroups(); MappingsHelper.load(); } @@ -128,6 +128,7 @@ public static void registerCommands(CommandDispatcher CParticleCommand.register(dispatcher); CPlaySoundCommand.register(dispatcher); CrackRNGCommand.register(dispatcher); + CreativeTabCommand.register(dispatcher, context); CStopSoundCommand.register(dispatcher); CTeleportCommand.register(dispatcher); CTellRawCommand.register(dispatcher); @@ -143,7 +144,6 @@ public static void registerCommands(CommandDispatcher GhostBlockCommand.register(dispatcher, context); GlowCommand.register(dispatcher); HotbarCommand.register(dispatcher); - ItemGroupCommand.register(dispatcher, context); KitCommand.register(dispatcher); ListenCommand.register(dispatcher); LookCommand.register(dispatcher); diff --git a/src/main/java/net/earthcomputer/clientcommands/command/ItemGroupCommand.java b/src/main/java/net/earthcomputer/clientcommands/command/CreativeTabCommand.java similarity index 61% rename from src/main/java/net/earthcomputer/clientcommands/command/ItemGroupCommand.java rename to src/main/java/net/earthcomputer/clientcommands/command/CreativeTabCommand.java index ac012e900..8aa1b592e 100644 --- a/src/main/java/net/earthcomputer/clientcommands/command/ItemGroupCommand.java +++ b/src/main/java/net/earthcomputer/clientcommands/command/CreativeTabCommand.java @@ -31,6 +31,8 @@ import java.io.File; import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.NoSuchFileException; import java.nio.file.Path; import java.util.HashMap; import java.util.Map; @@ -42,67 +44,67 @@ import static net.fabricmc.fabric.api.client.command.v2.ClientCommandManager.*; import static net.minecraft.commands.SharedSuggestionProvider.*; -public class ItemGroupCommand { +public class CreativeTabCommand { private static final Logger LOGGER = LogUtils.getLogger(); - private static final DynamicCommandExceptionType NOT_FOUND_EXCEPTION = new DynamicCommandExceptionType(arg -> Component.translatable("commands.citemgroup.notFound", arg)); - private static final DynamicCommandExceptionType OUT_OF_BOUNDS_EXCEPTION = new DynamicCommandExceptionType(arg -> Component.translatable("commands.citemgroup.outOfBounds", arg)); + private static final DynamicCommandExceptionType NOT_FOUND_EXCEPTION = new DynamicCommandExceptionType(arg -> Component.translatable("commands.ccreativetab.notFound", arg)); + private static final DynamicCommandExceptionType OUT_OF_BOUNDS_EXCEPTION = new DynamicCommandExceptionType(arg -> Component.translatable("commands.ccreativetab.outOfBounds", arg)); - private static final SimpleCommandExceptionType SAVE_FAILED_EXCEPTION = new SimpleCommandExceptionType(Component.translatable("commands.citemgroup.saveFile.failed")); - private static final DynamicCommandExceptionType ILLEGAL_CHARACTER_EXCEPTION = new DynamicCommandExceptionType(arg -> Component.translatable("commands.citemgroup.addGroup.illegalCharacter", arg)); - private static final DynamicCommandExceptionType ALREADY_EXISTS_EXCEPTION = new DynamicCommandExceptionType(arg -> Component.translatable("commands.citemgroup.addGroup.alreadyExists", arg)); + private static final SimpleCommandExceptionType SAVE_FAILED_EXCEPTION = new SimpleCommandExceptionType(Component.translatable("commands.ccreativetab.saveFile.failed")); + private static final DynamicCommandExceptionType ILLEGAL_CHARACTER_EXCEPTION = new DynamicCommandExceptionType(arg -> Component.translatable("commands.ccreativetab.addTab.illegalCharacter", arg)); + private static final DynamicCommandExceptionType ALREADY_EXISTS_EXCEPTION = new DynamicCommandExceptionType(arg -> Component.translatable("commands.ccreativetab.addTab.alreadyExists", arg)); private static final Path configPath = FabricLoader.getInstance().getConfigDir().resolve("clientcommands"); - private static final Map groups = new HashMap<>(); + private static final Map tabs = new HashMap<>(); public static void registerItemGroups() { try { loadFile(); } catch (IOException e) { - LOGGER.error("Could not load groups file, hence /citemgroup will not work!", e); + LOGGER.error("Could not load groups file, hence /ccreativetab will not work!", e); } - groups.forEach((key, group) -> group.registerItemGroup(key)); + tabs.forEach((key, tab) -> tab.registerItemGroup(key)); } public static void register(CommandDispatcher dispatcher, CommandBuildContext context) { - dispatcher.register(literal("citemgroup") + dispatcher.register(literal("ccreativetab") .then(literal("modify") - .then(argument("group", string()) - .suggests((ctx, builder) -> suggest(groups.keySet(), builder)) + .then(argument("tab", string()) + .suggests((ctx, builder) -> suggest(tabs.keySet(), builder)) .then(literal("add") .then(argument("itemstack", itemStack(context)) .then(argument("count", integer(1)) - .executes(ctx -> addStack(ctx.getSource(), getString(ctx, "group"), getCItemStackArgument(ctx, "itemstack").createItemStack(getInteger(ctx, "count"), false)))) - .executes(ctx -> addStack(ctx.getSource(), getString(ctx, "group"), getCItemStackArgument(ctx, "itemstack").createItemStack(1, false))))) + .executes(ctx -> addStack(ctx.getSource(), getString(ctx, "tab"), getCItemStackArgument(ctx, "itemstack").createItemStack(getInteger(ctx, "count"), false)))) + .executes(ctx -> addStack(ctx.getSource(), getString(ctx, "tab"), getCItemStackArgument(ctx, "itemstack").createItemStack(1, false))))) .then(literal("remove") .then(argument("index", integer(0)) - .executes(ctx -> removeStack(ctx.getSource(), getString(ctx, "group"), getInteger(ctx, "index"))))) + .executes(ctx -> removeStack(ctx.getSource(), getString(ctx, "tab"), getInteger(ctx, "index"))))) .then(literal("set") .then(argument("index", integer(0)) .then(argument("itemstack", itemStack(context)) .then(argument("count", integer(1)) - .executes(ctx -> setStack(ctx.getSource(), getString(ctx, "group"), getInteger(ctx, "index"), getCItemStackArgument(ctx, "itemstack").createItemStack(getInteger(ctx, "count"), false)))) - .executes(ctx -> setStack(ctx.getSource(), getString(ctx, "group"), getInteger(ctx, "index"), getCItemStackArgument(ctx, "itemstack").createItemStack(1, false)))))) + .executes(ctx -> setStack(ctx.getSource(), getString(ctx, "tab"), getInteger(ctx, "index"), getCItemStackArgument(ctx, "itemstack").createItemStack(getInteger(ctx, "count"), false)))) + .executes(ctx -> setStack(ctx.getSource(), getString(ctx, "tab"), getInteger(ctx, "index"), getCItemStackArgument(ctx, "itemstack").createItemStack(1, false)))))) .then(literal("icon") .then(argument("icon", itemStack(context)) - .executes(ctx -> changeIcon(ctx.getSource(), getString(ctx, "group"), getCItemStackArgument(ctx, "icon").createItemStack(1, false))))) + .executes(ctx -> changeIcon(ctx.getSource(), getString(ctx, "tab"), getCItemStackArgument(ctx, "icon").createItemStack(1, false))))) .then(literal("rename") .then(argument("new", string()) - .executes(ctx -> renameGroup(ctx.getSource(), getString(ctx, "group"), getString(ctx, "new"))))))) + .executes(ctx -> renameTab(ctx.getSource(), getString(ctx, "tab"), getString(ctx, "new"))))))) .then(literal("add") - .then(argument("group", string()) + .then(argument("tab", string()) .then(argument("icon", itemStack(context)) - .executes(ctx -> addGroup(ctx.getSource(), getString(ctx, "group"), getCItemStackArgument(ctx, "icon").createItemStack(1, false)))))) + .executes(ctx -> addTab(ctx.getSource(), getString(ctx, "tab"), getCItemStackArgument(ctx, "icon").createItemStack(1, false)))))) .then(literal("remove") - .then(argument("group", string()) - .suggests((ctx, builder) -> suggest(groups.keySet(), builder)) - .executes(ctx -> removeGroup(ctx.getSource(), getString(ctx, "group")))))); + .then(argument("tab", string()) + .suggests((ctx, builder) -> suggest(tabs.keySet(), builder)) + .executes(ctx -> removeTab(ctx.getSource(), getString(ctx, "tab")))))); } - private static int addGroup(FabricClientCommandSource source, String name, ItemStack icon) throws CommandSyntaxException { - if (groups.containsKey(name)) { + private static int addTab(FabricClientCommandSource source, String name, ItemStack icon) throws CommandSyntaxException { + if (tabs.containsKey(name)) { throw ALREADY_EXISTS_EXCEPTION.create(name); } @@ -111,96 +113,96 @@ private static int addGroup(FabricClientCommandSource source, String name, ItemS throw ILLEGAL_CHARACTER_EXCEPTION.create(name); } - groups.put(name, new Group(icon, new ListTag())); + tabs.put(name, new Tab(icon, new ListTag())); saveFile(); - source.sendFeedback(Component.translatable("commands.citemgroup.addGroup.success", name)); + source.sendFeedback(Component.translatable("commands.ccreativetab.addTab.success", name)); ClientCommandHelper.sendRequiresRestart(); return Command.SINGLE_SUCCESS; } - private static int removeGroup(FabricClientCommandSource source, String name) throws CommandSyntaxException { - if (!groups.containsKey(name)) { + private static int removeTab(FabricClientCommandSource source, String name) throws CommandSyntaxException { + if (!tabs.containsKey(name)) { throw NOT_FOUND_EXCEPTION.create(name); } - groups.remove(name); + tabs.remove(name); saveFile(); - source.sendFeedback(Component.translatable("commands.citemgroup.removeGroup.success", name)); + source.sendFeedback(Component.translatable("commands.ccreativetab.removeTab.success", name)); ClientCommandHelper.sendRequiresRestart(); return Command.SINGLE_SUCCESS; } private static int addStack(FabricClientCommandSource source, String name, ItemStack itemStack) throws CommandSyntaxException { - if (!groups.containsKey(name)) { + if (!tabs.containsKey(name)) { throw NOT_FOUND_EXCEPTION.create(name); } - Group group = groups.get(name); - ListTag items = group.items(); + Tab tab = tabs.get(name); + ListTag items = tab.items(); items.add(itemStack.save(new CompoundTag())); saveFile(); - source.sendFeedback(Component.translatable("commands.citemgroup.addStack.success", itemStack.getItem().getDescription(), name)); + source.sendFeedback(Component.translatable("commands.ccreativetab.addStack.success", itemStack.getItem().getDescription(), name)); ClientCommandHelper.sendRequiresRestart(); return Command.SINGLE_SUCCESS; } private static int removeStack(FabricClientCommandSource source, String name, int index) throws CommandSyntaxException { - if (!groups.containsKey(name)) { + if (!tabs.containsKey(name)) { throw NOT_FOUND_EXCEPTION.create(name); } - Group group = groups.get(name); - ListTag items = group.items(); + Tab tab = tabs.get(name); + ListTag items = tab.items(); if (index < 0 || index >= items.size()) { throw OUT_OF_BOUNDS_EXCEPTION.create(index); } items.remove(index); saveFile(); - source.sendFeedback(Component.translatable("commands.citemgroup.removeStack.success", name, index)); + source.sendFeedback(Component.translatable("commands.ccreativetab.removeStack.success", name, index)); ClientCommandHelper.sendRequiresRestart(); return Command.SINGLE_SUCCESS; } private static int setStack(FabricClientCommandSource source, String name, int index, ItemStack itemStack) throws CommandSyntaxException { - if (!groups.containsKey(name)) { + if (!tabs.containsKey(name)) { throw NOT_FOUND_EXCEPTION.create(name); } - Group group = groups.get(name); - ListTag items = group.items(); + Tab tab = tabs.get(name); + ListTag items = tab.items(); if ((index < 0) || (index >= items.size())) { throw OUT_OF_BOUNDS_EXCEPTION.create(index); } items.set(index, itemStack.save(new CompoundTag())); saveFile(); - source.sendFeedback(Component.translatable("commands.citemgroup.setStack.success", name, index, itemStack.getItem().getDescription())); + source.sendFeedback(Component.translatable("commands.ccreativetab.setStack.success", name, index, itemStack.getItem().getDescription())); ClientCommandHelper.sendRequiresRestart(); return Command.SINGLE_SUCCESS; } private static int changeIcon(FabricClientCommandSource source, String name, ItemStack icon) throws CommandSyntaxException { - if (!groups.containsKey(name)) { + if (!tabs.containsKey(name)) { throw NOT_FOUND_EXCEPTION.create(name); } - Group group = groups.get(name); - ListTag items = group.items(); - ItemStack old = group.icon(); + Tab tab = tabs.get(name); + ListTag items = tab.items(); + ItemStack old = tab.icon(); - groups.put(name, new Group(icon, items)); + tabs.put(name, new Tab(icon, items)); saveFile(); - source.sendFeedback(Component.translatable("commands.citemgroup.changeIcon.success", name, old.getItem().getDescription(), icon.getItem().getDescription())); + source.sendFeedback(Component.translatable("commands.ccreativetab.changeIcon.success", name, old.getItem().getDescription(), icon.getItem().getDescription())); ClientCommandHelper.sendRequiresRestart(); return Command.SINGLE_SUCCESS; } - private static int renameGroup(FabricClientCommandSource source, String name, String _new) throws CommandSyntaxException { - if (!groups.containsKey(name)) { + private static int renameTab(FabricClientCommandSource source, String name, String _new) throws CommandSyntaxException { + if (!tabs.containsKey(name)) { throw NOT_FOUND_EXCEPTION.create(name); } @@ -208,11 +210,11 @@ private static int renameGroup(FabricClientCommandSource source, String name, St if (identifier == null) { throw ILLEGAL_CHARACTER_EXCEPTION.create(_new); } - Group group = groups.remove(name); - groups.put(_new, group); + Tab tab = tabs.remove(name); + tabs.put(_new, tab); saveFile(); - source.sendFeedback(Component.translatable("commands.citemgroup.renameGroup.success", name, _new)); + source.sendFeedback(Component.translatable("commands.ccreativetab.renameTab.success", name, _new)); ClientCommandHelper.sendRequiresRestart(); return Command.SINGLE_SUCCESS; } @@ -221,18 +223,18 @@ private static void saveFile() throws CommandSyntaxException { try { CompoundTag rootTag = new CompoundTag(); CompoundTag compoundTag = new CompoundTag(); - groups.forEach((key, value) -> { - CompoundTag group = new CompoundTag(); - group.put("icon", value.icon().save(new CompoundTag())); - group.put("items", value.items()); - compoundTag.put(key, group); + tabs.forEach((key, value) -> { + CompoundTag tab = new CompoundTag(); + tab.put("icon", value.icon().save(new CompoundTag())); + tab.put("items", value.items()); + compoundTag.put(key, tab); }); rootTag.putInt("DataVersion", SharedConstants.getCurrentVersion().getDataVersion().getVersion()); - rootTag.put("Groups", compoundTag); - Path newFile = File.createTempFile("groups", ".dat", configPath.toFile()).toPath(); + rootTag.put("CreativeTabs", compoundTag); + Path newFile = File.createTempFile("creative_tabs", ".dat", configPath.toFile()).toPath(); NbtIo.write(rootTag, newFile); - Path backupFile = configPath.resolve("groups.dat_old"); - Path currentFile = configPath.resolve("groups.dat"); + Path backupFile = configPath.resolve("creative_tabs.dat_old"); + Path currentFile = configPath.resolve("creative_tabs.dat"); Util.safeReplaceFile(currentFile, newFile, backupFile); } catch (IOException e) { e.printStackTrace(); @@ -241,47 +243,58 @@ private static void saveFile() throws CommandSyntaxException { } private static void loadFile() throws IOException { - groups.clear(); - CompoundTag rootTag = NbtIo.read(configPath.resolve("groups.dat")); + tabs.clear(); + CompoundTag rootTag = NbtIo.read(configPath.resolve("creative_tabs.dat")); if (rootTag == null) { - return; + try { + Files.move(configPath.resolve("groups.dat"), configPath.resolve("creative_tabs.dat")); + } catch (NoSuchFileException e) { + return; + } + rootTag = NbtIo.read(configPath.resolve("creative_tabs.dat")); + if (rootTag == null) { + return; + } } final int currentVersion = SharedConstants.getCurrentVersion().getDataVersion().getVersion(); final int fileVersion = rootTag.getInt("DataVersion"); - CompoundTag compoundTag = rootTag.getCompound("Groups"); + CompoundTag compoundTag = rootTag.getCompound("CreativeTabs"); + if (compoundTag.isEmpty()) { + compoundTag = rootTag.getCompound("Groups"); + } DataFixer dataFixer = Minecraft.getInstance().getFixerUpper(); if (fileVersion >= currentVersion) { - compoundTag.getAllKeys().forEach(key -> { + for (String key : compoundTag.getAllKeys()) { if (ResourceLocation.tryParse("clientcommands:" + key) == null) { - LOGGER.warn("Skipping item group with invalid name {}", key); + LOGGER.warn("Skipping creative tab with invalid name {}", key); return; } - CompoundTag group = compoundTag.getCompound(key); - ItemStack icon = singleItemFromNbt(group.getCompound("icon")); - ListTag items = group.getList("items", Tag.TAG_COMPOUND); - groups.put(key, new Group(icon, items)); - }); + CompoundTag tab = compoundTag.getCompound(key); + ItemStack icon = singleItemFromNbt(tab.getCompound("icon")); + ListTag items = tab.getList("items", Tag.TAG_COMPOUND); + tabs.put(key, new Tab(icon, items)); + } } else { - compoundTag.getAllKeys().forEach(key -> { + for (String key : compoundTag.getAllKeys()) { if (ResourceLocation.tryParse("clientcommands:" + key) == null) { - LOGGER.warn("Skipping item group with invalid name {}", key); + LOGGER.warn("Skipping creative tab with invalid name {}", key); return; } - CompoundTag group = compoundTag.getCompound(key); - Dynamic oldStackDynamic = new Dynamic<>(NbtOps.INSTANCE, group.getCompound("icon")); + CompoundTag tab = compoundTag.getCompound(key); + Dynamic oldStackDynamic = new Dynamic<>(NbtOps.INSTANCE, tab.getCompound("icon")); Dynamic newStackDynamic = dataFixer.update(References.ITEM_STACK, oldStackDynamic, fileVersion, currentVersion); ItemStack icon = singleItemFromNbt((CompoundTag) newStackDynamic.getValue()); ListTag updatedListTag = new ListTag(); - group.getList("items", Tag.TAG_COMPOUND).forEach(tag -> { + tab.getList("items", Tag.TAG_COMPOUND).forEach(tag -> { Dynamic oldTagDynamic = new Dynamic<>(NbtOps.INSTANCE, tag); Dynamic newTagDynamic = dataFixer.update(References.ITEM_STACK, oldTagDynamic, fileVersion, currentVersion); updatedListTag.add(newTagDynamic.getValue()); }); - groups.put(key, new Group(icon, updatedListTag)); - }); + tabs.put(key, new Tab(icon, updatedListTag)); + } } } @@ -293,7 +306,7 @@ private static ItemStack singleItemFromNbt(CompoundTag nbt) { return stack; } - private record Group(ItemStack icon, ListTag items) { + private record Tab(ItemStack icon, ListTag items) { void registerItemGroup(String key) { Registry.register(BuiltInRegistries.CREATIVE_MODE_TAB, new ResourceLocation("clientcommands", key), FabricItemGroup.builder() .title(Component.literal(key)) diff --git a/src/main/resources/assets/clientcommands/lang/en_us.json b/src/main/resources/assets/clientcommands/lang/en_us.json index c0dbf225e..22483b85b 100644 --- a/src/main/resources/assets/clientcommands/lang/en_us.json +++ b/src/main/resources/assets/clientcommands/lang/en_us.json @@ -44,6 +44,19 @@ "commands.ccrackrng.starting": "Cracking player seed", "commands.ccrackrng.success": "Player RNG cracked: %d", + "commands.ccreativetab.addStack.success": "Successfully added %s to \"%s\"", + "commands.ccreativetab.addTab.alreadyExists": "Creative tab \"%s\" already exists", + "commands.ccreativetab.addTab.illegalCharacter": "Creative tab can only contain [a-z0-9/._-] characters", + "commands.ccreativetab.addTab.success": "Successfully added creative tab \"%s\"", + "commands.ccreativetab.changeIcon.success": "Successfully changed the icon of \"%s\" from %s to %s", + "commands.ccreativetab.notFound": "Creative tab \"%s\" not found", + "commands.ccreativetab.outOfBounds": "Index %d is out of bounds", + "commands.ccreativetab.removeStack.success": "Successfully removed stack from \"%s\" at index %d", + "commands.ccreativetab.removeTab.success": "Successfully removed creative tab \"%s\"", + "commands.ccreativetab.renameTab.success": "Successfully renamed creative tab \"%s\" to \"%s\"", + "commands.ccreativetab.saveFile.failed": "Could not save creative tabs file", + "commands.ccreativetab.setStack.success": "Successfully set the stack from \"%s\" at index %d to %s", + "commands.cenchant.expectedWithWithoutExactly": "Expected \"with\"/\"without\"/\"exactly\"", "commands.cenchant.failed": "It's impossible or would take too long to get those enchantments", "commands.cenchant.help.uncrackedPlayerSeed": "Help: you have not fully cracked the player seed", @@ -118,19 +131,6 @@ "commands.chotbar.notCreative": "Player must be in creative mode to restore item hotbars", "commands.chotbar.restoredHotbar": "Restored item hotbar %d", - "commands.citemgroup.notFound": "Item group \"%s\" not found", - "commands.citemgroup.outOfBounds": "Index %d is out of bounds", - "commands.citemgroup.saveFile.failed": "Could not save item groups file", - "commands.citemgroup.addGroup.success": "Successfully added item group \"%s\"", - "commands.citemgroup.addGroup.illegalCharacter": "Item groups can only contain [a-z0-9/._-] characters", - "commands.citemgroup.addGroup.alreadyExists": "Item group \"%s\" already exists", - "commands.citemgroup.removeGroup.success": "Successfully removed item group \"%s\"", - "commands.citemgroup.addStack.success": "Successfully added %s to \"%s\"", - "commands.citemgroup.removeStack.success": "Successfully removed stack from \"%s\" at index %d", - "commands.citemgroup.setStack.success": "Successfully set the stack from \"%s\" at index %d to %s", - "commands.citemgroup.changeIcon.success": "Successfully changed the icon of \"%s\" from %s to %s", - "commands.citemgroup.renameGroup.success": "Successfully renamed item group \"%s\" to \"%s\"", - "commands.ckit.notFound": "Kit \"%s\" not found", "commands.ckit.saveFile.failed": "Could not save kits file", "commands.ckit.load.success": "Successfully gave kit \"%s\" to self",