From 9a663c59a2feeb17eec03a168d4dcab21e81eb0b Mon Sep 17 00:00:00 2001 From: Ruben Taelman Date: Fri, 20 Sep 2024 17:48:59 +0200 Subject: [PATCH] Allow commands to be registered in common base mod --- .../cyclops/cyclopscore/command/CommandIgnite.java | 0 .../cyclops/cyclopscore/helper/ModBaseCommon.java | 10 ++++++++++ .../resources/assets/cyclopscore/lang/en_us.json | 2 +- .../org/cyclops/cyclopscore/CyclopsCoreFabric.java | 14 ++++++++++++++ .../cyclops/cyclopscore/init/ModBaseFabric.java | 10 ++++++++++ .../org/cyclops/cyclopscore/CyclopsCoreForge.java | 14 ++++++++++++++ .../org/cyclops/cyclopscore/init/ModBaseForge.java | 7 +++++++ .../java/org/cyclops/cyclopscore/init/ModBase.java | 2 +- 8 files changed, 57 insertions(+), 2 deletions(-) rename {loader-neoforge => loader-common}/src/main/java/org/cyclops/cyclopscore/command/CommandIgnite.java (100%) diff --git a/loader-neoforge/src/main/java/org/cyclops/cyclopscore/command/CommandIgnite.java b/loader-common/src/main/java/org/cyclops/cyclopscore/command/CommandIgnite.java similarity index 100% rename from loader-neoforge/src/main/java/org/cyclops/cyclopscore/command/CommandIgnite.java rename to loader-common/src/main/java/org/cyclops/cyclopscore/command/CommandIgnite.java diff --git a/loader-common/src/main/java/org/cyclops/cyclopscore/helper/ModBaseCommon.java b/loader-common/src/main/java/org/cyclops/cyclopscore/helper/ModBaseCommon.java index 0cfdf8591f..37025dc053 100644 --- a/loader-common/src/main/java/org/cyclops/cyclopscore/helper/ModBaseCommon.java +++ b/loader-common/src/main/java/org/cyclops/cyclopscore/helper/ModBaseCommon.java @@ -2,6 +2,10 @@ import com.google.common.collect.Lists; import com.google.common.collect.Maps; +import com.mojang.brigadier.builder.LiteralArgumentBuilder; +import net.minecraft.commands.CommandBuildContext; +import net.minecraft.commands.CommandSourceStack; +import net.minecraft.commands.Commands; import net.minecraft.network.chat.Component; import net.minecraft.world.item.CreativeModeTab; import net.minecraft.world.item.ItemStack; @@ -88,6 +92,12 @@ protected void loadModCompats(ModCompatLoader modCompatLoader) { } + protected LiteralArgumentBuilder constructBaseCommand(Commands.CommandSelection selection, CommandBuildContext context) { + LiteralArgumentBuilder root = Commands.literal(this.getModId()); + + return root; + } + @Nullable @Override public CreativeModeTab getDefaultCreativeTab() { diff --git a/loader-common/src/main/resources/assets/cyclopscore/lang/en_us.json b/loader-common/src/main/resources/assets/cyclopscore/lang/en_us.json index ac1146769f..645e1779ae 100644 --- a/loader-common/src/main/resources/assets/cyclopscore/lang/en_us.json +++ b/loader-common/src/main/resources/assets/cyclopscore/lang/en_us.json @@ -35,7 +35,7 @@ "chat.cyclopscore.command.invalidNewValue": "Invalid new value", "chat.cyclopscore.command.updatedValue": "Updated %s to: %s", "chat.cyclopscore.command.invalidPlayer": "The player '%s' was not found", - "chat.cyclopscore.command.ignitedPlayer": "The player '%s' was lit on fire for %s seconds", + "chat.cyclopscore.command.ignitedPlayer": "The player '%s' was lit on fire for %s ticks", "chat.cyclopscore.command.noConfigsFound": "There were no config values found that can be set", "_comment": "Multiblock", diff --git a/loader-fabric/src/main/java/org/cyclops/cyclopscore/CyclopsCoreFabric.java b/loader-fabric/src/main/java/org/cyclops/cyclopscore/CyclopsCoreFabric.java index 519b79a2ad..fa9e5d5987 100644 --- a/loader-fabric/src/main/java/org/cyclops/cyclopscore/CyclopsCoreFabric.java +++ b/loader-fabric/src/main/java/org/cyclops/cyclopscore/CyclopsCoreFabric.java @@ -1,12 +1,17 @@ package org.cyclops.cyclopscore; +import com.mojang.brigadier.builder.LiteralArgumentBuilder; import net.fabricmc.api.ModInitializer; +import net.minecraft.commands.CommandBuildContext; +import net.minecraft.commands.CommandSourceStack; +import net.minecraft.commands.Commands; import org.cyclops.cyclopscore.advancement.criterion.GuiContainerOpenTriggerConfig; import org.cyclops.cyclopscore.advancement.criterion.GuiContainerOpenTriggerEventHooksFabric; import org.cyclops.cyclopscore.advancement.criterion.ItemCraftedTriggerConfig; import org.cyclops.cyclopscore.advancement.criterion.ItemCraftedTriggerTriggerEventHooksFabric; import org.cyclops.cyclopscore.advancement.criterion.ModItemObtainedTriggerConfig; import org.cyclops.cyclopscore.advancement.criterion.ModItemObtainedTriggerEventHooksFabric; +import org.cyclops.cyclopscore.command.CommandIgnite; import org.cyclops.cyclopscore.component.DataComponentCapacityConfig; import org.cyclops.cyclopscore.component.DataComponentEnergyStorageConfig; import org.cyclops.cyclopscore.config.ConfigHandlerCommon; @@ -54,6 +59,15 @@ protected ICommonProxyCommon constructCommonProxy() { return new CommonProxyFabric(); } + @Override + protected LiteralArgumentBuilder constructBaseCommand(Commands.CommandSelection selection, CommandBuildContext context) { + LiteralArgumentBuilder root = super.constructBaseCommand(selection, context); + + root.then(CommandIgnite.make()); + + return root; + } + @Override protected void onConfigsRegister(ConfigHandlerCommon configHandler) { super.onConfigsRegister(configHandler); diff --git a/loader-fabric/src/main/java/org/cyclops/cyclopscore/init/ModBaseFabric.java b/loader-fabric/src/main/java/org/cyclops/cyclopscore/init/ModBaseFabric.java index 3c422f34af..5337bc8e9f 100644 --- a/loader-fabric/src/main/java/org/cyclops/cyclopscore/init/ModBaseFabric.java +++ b/loader-fabric/src/main/java/org/cyclops/cyclopscore/init/ModBaseFabric.java @@ -1,6 +1,11 @@ package org.cyclops.cyclopscore.init; +import com.mojang.brigadier.CommandDispatcher; import net.fabricmc.api.ModInitializer; +import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback; +import net.minecraft.commands.CommandBuildContext; +import net.minecraft.commands.CommandSourceStack; +import net.minecraft.commands.Commands; import org.cyclops.cyclopscore.config.ConfigHandlerFabric; import org.cyclops.cyclopscore.helper.IModHelpersFabric; import org.cyclops.cyclopscore.helper.ModBaseCommon; @@ -49,6 +54,7 @@ public void onInitialize() { getConfigHandler().loadSetup(); getConfigHandler().loadForgeRegistries(); getConfigHandler().loadForgeRegistriesFilled(); + CommandRegistrationCallback.EVENT.register(this::onRegisterCommands); // Register proxy things ICommonProxyCommon proxy = getProxy(); @@ -87,6 +93,10 @@ protected void loadModCompats(ModCompatLoader modCompatLoader) { modCompatLoader.addModCompat(new ModCompatForgeConfig()); } + protected void onRegisterCommands(CommandDispatcher dispatcher, CommandBuildContext registryAccess, Commands.CommandSelection environment) { + dispatcher.register(constructBaseCommand(environment, registryAccess)); + } + /** * Get the mod by id. * @param modId The mod id. diff --git a/loader-forge/src/main/java/org/cyclops/cyclopscore/CyclopsCoreForge.java b/loader-forge/src/main/java/org/cyclops/cyclopscore/CyclopsCoreForge.java index 8e1545c9a4..d817b8c875 100644 --- a/loader-forge/src/main/java/org/cyclops/cyclopscore/CyclopsCoreForge.java +++ b/loader-forge/src/main/java/org/cyclops/cyclopscore/CyclopsCoreForge.java @@ -1,5 +1,9 @@ package org.cyclops.cyclopscore; +import com.mojang.brigadier.builder.LiteralArgumentBuilder; +import net.minecraft.commands.CommandBuildContext; +import net.minecraft.commands.CommandSourceStack; +import net.minecraft.commands.Commands; import net.minecraftforge.fml.common.Mod; import net.minecraftforge.fml.event.lifecycle.FMLCommonSetupEvent; import org.cyclops.cyclopscore.advancement.criterion.GuiContainerOpenTriggerConfig; @@ -8,6 +12,7 @@ import org.cyclops.cyclopscore.advancement.criterion.ItemCraftedTriggerTriggerEventHooksForge; import org.cyclops.cyclopscore.advancement.criterion.ModItemObtainedTriggerConfig; import org.cyclops.cyclopscore.advancement.criterion.ModItemObtainedTriggerEventHooksForge; +import org.cyclops.cyclopscore.command.CommandIgnite; import org.cyclops.cyclopscore.component.DataComponentCapacityConfig; import org.cyclops.cyclopscore.component.DataComponentEnergyStorageConfig; import org.cyclops.cyclopscore.config.ConfigHandlerCommon; @@ -57,6 +62,15 @@ protected ICommonProxyCommon constructCommonProxy() { return new CommonProxyForge(); } + @Override + protected LiteralArgumentBuilder constructBaseCommand(Commands.CommandSelection selection, CommandBuildContext context) { + LiteralArgumentBuilder root = super.constructBaseCommand(selection, context); + + root.then(CommandIgnite.make()); + + return root; + } + @Override protected void onConfigsRegister(ConfigHandlerCommon configHandler) { super.onConfigsRegister(configHandler); diff --git a/loader-forge/src/main/java/org/cyclops/cyclopscore/init/ModBaseForge.java b/loader-forge/src/main/java/org/cyclops/cyclopscore/init/ModBaseForge.java index 4b753e7bf7..1d7b1f1c32 100644 --- a/loader-forge/src/main/java/org/cyclops/cyclopscore/init/ModBaseForge.java +++ b/loader-forge/src/main/java/org/cyclops/cyclopscore/init/ModBaseForge.java @@ -1,6 +1,8 @@ package org.cyclops.cyclopscore.init; import com.google.common.collect.Lists; +import net.minecraftforge.common.MinecraftForge; +import net.minecraftforge.event.RegisterCommandsEvent; import net.minecraftforge.eventbus.api.EventPriority; import net.minecraftforge.eventbus.api.IEventBus; import net.minecraftforge.fml.event.lifecycle.FMLClientSetupEvent; @@ -48,6 +50,7 @@ public ModBaseForge(String modId, Consumer instanceSetter) { if (getModHelpers().getMinecraftHelpers().isClientSide()) { getModEventBus().addListener(this::setupClient); } + MinecraftForge.EVENT_BUS.addListener(this::onRegisterCommands); // Initialize config handler this.onConfigsRegister(getConfigHandler()); @@ -140,6 +143,10 @@ private void beforeRegistriedFilled(RegisterEvent event) { } } + protected void onRegisterCommands(RegisterCommandsEvent event) { + event.getDispatcher().register(constructBaseCommand(event.getCommandSelection(), event.getBuildContext())); + } + /** * Get the mod by id. * @param modId The mod id. diff --git a/loader-neoforge/src/main/java/org/cyclops/cyclopscore/init/ModBase.java b/loader-neoforge/src/main/java/org/cyclops/cyclopscore/init/ModBase.java index 9900b9f44b..86c996df6c 100644 --- a/loader-neoforge/src/main/java/org/cyclops/cyclopscore/init/ModBase.java +++ b/loader-neoforge/src/main/java/org/cyclops/cyclopscore/init/ModBase.java @@ -197,7 +197,7 @@ protected IMCHandler constructIMCHandler() { } protected LiteralArgumentBuilder constructBaseCommand(Commands.CommandSelection selection, CommandBuildContext context) { - LiteralArgumentBuilder root = Commands.literal(this.getModId()); + LiteralArgumentBuilder root = super.constructBaseCommand(selection, context); root.then(CommandConfig.make(this)); root.then(CommandVersion.make(this));