Skip to content

Commit

Permalink
Allow commands to be registered in common base mod
Browse files Browse the repository at this point in the history
  • Loading branch information
rubensworks committed Sep 20, 2024
1 parent 4189c9d commit 9a663c5
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -88,6 +92,12 @@ protected void loadModCompats(ModCompatLoader modCompatLoader) {

}

protected LiteralArgumentBuilder<CommandSourceStack> constructBaseCommand(Commands.CommandSelection selection, CommandBuildContext context) {
LiteralArgumentBuilder<CommandSourceStack> root = Commands.literal(this.getModId());

return root;
}

@Nullable
@Override
public CreativeModeTab getDefaultCreativeTab() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -54,6 +59,15 @@ protected ICommonProxyCommon constructCommonProxy() {
return new CommonProxyFabric();
}

@Override
protected LiteralArgumentBuilder<CommandSourceStack> constructBaseCommand(Commands.CommandSelection selection, CommandBuildContext context) {
LiteralArgumentBuilder<CommandSourceStack> root = super.constructBaseCommand(selection, context);

root.then(CommandIgnite.make());

return root;
}

@Override
protected void onConfigsRegister(ConfigHandlerCommon configHandler) {
super.onConfigsRegister(configHandler);
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -49,6 +54,7 @@ public void onInitialize() {
getConfigHandler().loadSetup();
getConfigHandler().loadForgeRegistries();
getConfigHandler().loadForgeRegistriesFilled();
CommandRegistrationCallback.EVENT.register(this::onRegisterCommands);

// Register proxy things
ICommonProxyCommon proxy = getProxy();
Expand Down Expand Up @@ -87,6 +93,10 @@ protected void loadModCompats(ModCompatLoader modCompatLoader) {
modCompatLoader.addModCompat(new ModCompatForgeConfig());
}

protected void onRegisterCommands(CommandDispatcher<CommandSourceStack> dispatcher, CommandBuildContext registryAccess, Commands.CommandSelection environment) {
dispatcher.register(constructBaseCommand(environment, registryAccess));
}

/**
* Get the mod by id.
* @param modId The mod id.
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -57,6 +62,15 @@ protected ICommonProxyCommon constructCommonProxy() {
return new CommonProxyForge();
}

@Override
protected LiteralArgumentBuilder<CommandSourceStack> constructBaseCommand(Commands.CommandSelection selection, CommandBuildContext context) {
LiteralArgumentBuilder<CommandSourceStack> root = super.constructBaseCommand(selection, context);

root.then(CommandIgnite.make());

return root;
}

@Override
protected void onConfigsRegister(ConfigHandlerCommon configHandler) {
super.onConfigsRegister(configHandler);
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -48,6 +50,7 @@ public ModBaseForge(String modId, Consumer<T> instanceSetter) {
if (getModHelpers().getMinecraftHelpers().isClientSide()) {
getModEventBus().addListener(this::setupClient);
}
MinecraftForge.EVENT_BUS.addListener(this::onRegisterCommands);

// Initialize config handler
this.onConfigsRegister(getConfigHandler());
Expand Down Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ protected IMCHandler constructIMCHandler() {
}

protected LiteralArgumentBuilder<CommandSourceStack> constructBaseCommand(Commands.CommandSelection selection, CommandBuildContext context) {
LiteralArgumentBuilder<CommandSourceStack> root = Commands.literal(this.getModId());
LiteralArgumentBuilder<CommandSourceStack> root = super.constructBaseCommand(selection, context);

root.then(CommandConfig.make(this));
root.then(CommandVersion.make(this));
Expand Down

0 comments on commit 9a663c5

Please sign in to comment.