Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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<T> {
private final List<Consumer<T>> listeners = new ArrayList<>();

public void register(Consumer<T> listener) {
listeners.add(listener);
}

public void post(T event) {
for (Consumer<T> listener : listeners) {
listener.accept(event);
}
}
}
Original file line number Diff line number Diff line change
@@ -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 <a href="https://gist.github.com/falkreon/f58bb91e45ba558bc7fd827e81c6cb45">this guide</a> to implement your commands properly
*/
public record CommandRegistrationEvent(CommandDispatcher<CommandSourceStack> dispatcher, CommandBuildContext context, Commands.CommandSelection selection) {
public static EventType<CommandRegistrationEvent> EVENT = new EventType<>();
}
Original file line number Diff line number Diff line change
@@ -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)));
}
}
2 changes: 1 addition & 1 deletion fabric/src/main/resources/fabric.mod.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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) {
Expand Down