From 0ef3fafca3781f1670bcd395fdfa61660fbb8924 Mon Sep 17 00:00:00 2001 From: MayaqqDev Date: Sat, 5 Oct 2024 19:44:42 +0200 Subject: [PATCH] I tried :( Didn't get client working at a point where it actually makes things easier... :sob: --- .../common/event/EventType.java | 19 +++++++++++++++++++ .../events/CommandRegistrationEvent.java | 15 +++++++++++++++ .../fabric/ResourcefulLibFabric.java | 15 +++++++++++++++ fabric/src/main/resources/fabric.mod.json | 2 +- .../neoforge/ResourcefulLibNeoForge.java | 7 +++++++ 5 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 common/src/main/java/com/teamresourceful/resourcefullib/common/event/EventType.java create mode 100644 common/src/main/java/com/teamresourceful/resourcefullib/common/event/events/CommandRegistrationEvent.java create mode 100644 fabric/src/main/java/com/teamresourceful/resourcefullib/fabric/ResourcefulLibFabric.java diff --git a/common/src/main/java/com/teamresourceful/resourcefullib/common/event/EventType.java b/common/src/main/java/com/teamresourceful/resourcefullib/common/event/EventType.java new file mode 100644 index 0000000..4aefdcd --- /dev/null +++ b/common/src/main/java/com/teamresourceful/resourcefullib/common/event/EventType.java @@ -0,0 +1,19 @@ +package com.teamresourceful.resourcefullib.common.event; + +import java.util.ArrayList; +import java.util.List; +import java.util.function.Consumer; + +public class EventType { + private final List> listeners = new ArrayList<>(); + + public void register(Consumer listener) { + listeners.add(listener); + } + + public void post(T event) { + for (Consumer listener : listeners) { + listener.accept(event); + } + } +} diff --git a/common/src/main/java/com/teamresourceful/resourcefullib/common/event/events/CommandRegistrationEvent.java b/common/src/main/java/com/teamresourceful/resourcefullib/common/event/events/CommandRegistrationEvent.java new file mode 100644 index 0000000..2f8ecd3 --- /dev/null +++ b/common/src/main/java/com/teamresourceful/resourcefullib/common/event/events/CommandRegistrationEvent.java @@ -0,0 +1,15 @@ +package com.teamresourceful.resourcefullib.common.event.events; + +import com.mojang.brigadier.CommandDispatcher; +import com.teamresourceful.resourcefullib.common.event.EventType; +import net.minecraft.commands.CommandBuildContext; +import net.minecraft.commands.CommandSourceStack; +import net.minecraft.commands.Commands; + +/** + * Just a simple event to abstract the registration between platforms, simply hook into this event on common setup + * Please use @see this guide to implement your commands properly + */ +public record CommandRegistrationEvent(CommandDispatcher dispatcher, CommandBuildContext context, Commands.CommandSelection selection) { + public static EventType EVENT = new EventType<>(); +} diff --git a/fabric/src/main/java/com/teamresourceful/resourcefullib/fabric/ResourcefulLibFabric.java b/fabric/src/main/java/com/teamresourceful/resourcefullib/fabric/ResourcefulLibFabric.java new file mode 100644 index 0000000..64d37af --- /dev/null +++ b/fabric/src/main/java/com/teamresourceful/resourcefullib/fabric/ResourcefulLibFabric.java @@ -0,0 +1,15 @@ +package com.teamresourceful.resourcefullib.fabric; + +import com.teamresourceful.resourcefullib.ResourcefulLib; +import com.teamresourceful.resourcefullib.common.event.events.CommandRegistrationEvent; +import net.fabricmc.api.ModInitializer; +import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback; + +public class ResourcefulLibFabric implements ModInitializer { + @Override + public void onInitialize() { + ResourcefulLib.init(); + + CommandRegistrationCallback.EVENT.register((dispatcher, context, selection) -> CommandRegistrationEvent.EVENT.post(new CommandRegistrationEvent(dispatcher, context, selection))); + } +} diff --git a/fabric/src/main/resources/fabric.mod.json b/fabric/src/main/resources/fabric.mod.json index dc85bd0..da729e5 100644 --- a/fabric/src/main/resources/fabric.mod.json +++ b/fabric/src/main/resources/fabric.mod.json @@ -18,7 +18,7 @@ "com.teamresourceful.resourcefullib.fabric.ResourcefulLibFabricClient" ], "main": [ - "com.teamresourceful.resourcefullib.ResourcefulLib::init" + "com.teamresourceful.resourcefullib.fabric.ResourcefulLibFabric" ], "server": [ "com.teamresourceful.resourcefullib.fabric.ResourcefulLibFabricServer" diff --git a/neoforge/src/main/java/com/teamresourceful/resourcefullib/neoforge/ResourcefulLibNeoForge.java b/neoforge/src/main/java/com/teamresourceful/resourcefullib/neoforge/ResourcefulLibNeoForge.java index 0ea1cb6..180aab6 100644 --- a/neoforge/src/main/java/com/teamresourceful/resourcefullib/neoforge/ResourcefulLibNeoForge.java +++ b/neoforge/src/main/java/com/teamresourceful/resourcefullib/neoforge/ResourcefulLibNeoForge.java @@ -2,10 +2,12 @@ import com.teamresourceful.resourcefullib.ResourcefulLib; import com.teamresourceful.resourcefullib.common.ApiProxy; +import com.teamresourceful.resourcefullib.common.event.events.CommandRegistrationEvent; import com.teamresourceful.resourcefullib.common.network.neoforge.NeoForgeNetworking; import net.neoforged.bus.api.IEventBus; import net.neoforged.fml.common.Mod; import net.neoforged.fml.loading.FMLLoader; +import net.neoforged.neoforge.event.RegisterCommandsEvent; import net.neoforged.neoforge.network.event.RegisterPayloadHandlersEvent; @Mod(ResourcefulLib.MOD_ID) @@ -21,6 +23,11 @@ public ResourcefulLibNeoForge(IEventBus bus) { } bus.addListener(ResourcefulLibNeoForge::onNetworkSetup); + bus.addListener(ResourcefulLibNeoForge::onCommandRegister); + } + + public static void onCommandRegister(RegisterCommandsEvent event) { + CommandRegistrationEvent.EVENT.post(new CommandRegistrationEvent(event.getDispatcher(), event.getBuildContext(), event.getCommandSelection())); } public static void onNetworkSetup(RegisterPayloadHandlersEvent event) {