From 8f96cffb973d15f026f30bfc18a5de1aae174c6b Mon Sep 17 00:00:00 2001 From: XyperCode Date: Fri, 13 Dec 2024 08:47:37 +0100 Subject: [PATCH] :sparkles: More events! --- build.gradle | 22 +++++++ .../java/dev/ultreon/mods/xinexlib/Env.java | 6 ++ .../ultreon/mods/xinexlib/EnvExecutor.java | 51 ++++++++++++++++ .../client/event/LocalPlayerEvent.java | 8 +++ .../client/event/LocalPlayerJoinEvent.java | 17 ++++++ .../client/event/LocalPlayerQuitEvent.java | 16 +++++ .../event/player/ServerPlayerEvent.java | 6 ++ .../event/server/ServerLevelSaveEvent.java | 59 +++++++++++++++++++ .../event/server/ServerPlayerQuitEvent.java | 36 +++++++++++ .../mixin/MixinClientPacketListener.java | 28 +++++++++ .../mods/xinexlib/mixin/MixinConnection.java | 30 ++++++++++ .../mods/xinexlib/mixin/MixinPlayerList.java | 5 ++ .../mods/xinexlib/mixin/MixinServerLevel.java | 12 ++++ .../mods/xinexlib/platform/Services.java | 5 ++ .../platform/services/IPlatformHelper.java | 3 + .../src/main/resources/xinexlib.mixins.json | 2 + .../platform/FabricPlatformHelper.java | 9 +++ forge/build.gradle | 2 +- .../platform/ForgePlatformHelper.java | 10 ++++ .../platform/NeoForgePlatformHelper.java | 10 ++++ settings.gradle | 2 +- 21 files changed, 337 insertions(+), 2 deletions(-) create mode 100644 common/src/main/java/dev/ultreon/mods/xinexlib/Env.java create mode 100644 common/src/main/java/dev/ultreon/mods/xinexlib/EnvExecutor.java create mode 100644 common/src/main/java/dev/ultreon/mods/xinexlib/client/event/LocalPlayerEvent.java create mode 100644 common/src/main/java/dev/ultreon/mods/xinexlib/client/event/LocalPlayerJoinEvent.java create mode 100644 common/src/main/java/dev/ultreon/mods/xinexlib/client/event/LocalPlayerQuitEvent.java create mode 100644 common/src/main/java/dev/ultreon/mods/xinexlib/event/server/ServerLevelSaveEvent.java create mode 100644 common/src/main/java/dev/ultreon/mods/xinexlib/event/server/ServerPlayerQuitEvent.java create mode 100644 common/src/main/java/dev/ultreon/mods/xinexlib/mixin/MixinClientPacketListener.java create mode 100644 common/src/main/java/dev/ultreon/mods/xinexlib/mixin/MixinConnection.java diff --git a/build.gradle b/build.gradle index 9cc49be..9fb73a9 100644 --- a/build.gradle +++ b/build.gradle @@ -20,3 +20,25 @@ tasks.register("javadoc", Copy) { from(project(":common").javadoc.outputs) into(file("build/docs/javadoc")) } + +tasks.register("jar", Copy) { + dependsOn "common:build" + dependsOn "forge:build" + dependsOn "neoforge:build" + dependsOn "fabric:build" + + doFirst { + mkdir file("build/libs") + } + + from(project(":common").jar.outputs) + from(project(":forge").jar.outputs) + from(project(":neoforge").jar.outputs) + from(project(":fabric").jar.outputs) + into(file("build/libs")) +} + +tasks.register("build") { + dependsOn "javadoc" + dependsOn "jar" +} diff --git a/common/src/main/java/dev/ultreon/mods/xinexlib/Env.java b/common/src/main/java/dev/ultreon/mods/xinexlib/Env.java new file mode 100644 index 0000000..0f255d5 --- /dev/null +++ b/common/src/main/java/dev/ultreon/mods/xinexlib/Env.java @@ -0,0 +1,6 @@ +package dev.ultreon.mods.xinexlib; + +public enum Env { + CLIENT, + SERVER +} diff --git a/common/src/main/java/dev/ultreon/mods/xinexlib/EnvExecutor.java b/common/src/main/java/dev/ultreon/mods/xinexlib/EnvExecutor.java new file mode 100644 index 0000000..04b10cd --- /dev/null +++ b/common/src/main/java/dev/ultreon/mods/xinexlib/EnvExecutor.java @@ -0,0 +1,51 @@ +package dev.ultreon.mods.xinexlib; + +import dev.ultreon.mods.xinexlib.platform.Services; + +import java.util.concurrent.Callable; +import java.util.function.Supplier; + +public class EnvExecutor { + public static void runInEnv(Env env, Supplier runnable) { + if (Services.getEnv() == env) runnable.get().run(); + } + + public static void runInEnv(Env env, Supplier runnable, Runnable fallback) { + if (Services.getEnv() == env) runnable.get().run(); + else fallback.run(); + } + + public static void runInEnvSpecific(Supplier client, Supplier server) { + if (Services.getEnv() == Env.CLIENT) client.get().run(); + else server.get().run(); + } + + public static T getInEnv(Env env, Supplier> supplier, Supplier fallback) { + if (Services.getEnv() == env) return supplier.get().get(); + return fallback.get(); + } + + public static T getInEnv(Env env, Supplier> supplier) { + return getInEnv(env, supplier, () -> null); + } + + public static T getInEnvSpecific(Supplier> client, Supplier> server) { + if (Services.getEnv() == Env.CLIENT) return client.get().get(); + return server.get().get(); + } + + public static T callInEnv(Env env, Supplier> supplier) throws Exception { + if (Services.getEnv() == env) return supplier.get().call(); + return null; + } + + public static T callInEnv(Env env, Supplier> supplier, Callable fallback) throws Exception { + if (Services.getEnv() == env) return supplier.get().call(); + return fallback.call(); + } + + public static T callInEnvSpecific(Supplier> client, Supplier> server) throws Exception { + if (Services.getEnv() == Env.CLIENT) return client.get().call(); + return server.get().call(); + } +} diff --git a/common/src/main/java/dev/ultreon/mods/xinexlib/client/event/LocalPlayerEvent.java b/common/src/main/java/dev/ultreon/mods/xinexlib/client/event/LocalPlayerEvent.java new file mode 100644 index 0000000..81b08dc --- /dev/null +++ b/common/src/main/java/dev/ultreon/mods/xinexlib/client/event/LocalPlayerEvent.java @@ -0,0 +1,8 @@ +package dev.ultreon.mods.xinexlib.client.event; + +import dev.ultreon.mods.xinexlib.event.player.PlayerEvent; +import net.minecraft.client.player.LocalPlayer; + +public interface LocalPlayerEvent extends PlayerEvent { + LocalPlayer getPlayer(); +} diff --git a/common/src/main/java/dev/ultreon/mods/xinexlib/client/event/LocalPlayerJoinEvent.java b/common/src/main/java/dev/ultreon/mods/xinexlib/client/event/LocalPlayerJoinEvent.java new file mode 100644 index 0000000..ca72449 --- /dev/null +++ b/common/src/main/java/dev/ultreon/mods/xinexlib/client/event/LocalPlayerJoinEvent.java @@ -0,0 +1,17 @@ +package dev.ultreon.mods.xinexlib.client.event; + +import net.minecraft.client.player.LocalPlayer; +import net.minecraft.world.entity.player.Player; + +public class LocalPlayerJoinEvent implements LocalPlayerEvent { + private final LocalPlayer localPlayer; + + public LocalPlayerJoinEvent(LocalPlayer localPlayer) { + this.localPlayer = localPlayer; + } + + @Override + public LocalPlayer getPlayer() { + return localPlayer; + } +} diff --git a/common/src/main/java/dev/ultreon/mods/xinexlib/client/event/LocalPlayerQuitEvent.java b/common/src/main/java/dev/ultreon/mods/xinexlib/client/event/LocalPlayerQuitEvent.java new file mode 100644 index 0000000..a059194 --- /dev/null +++ b/common/src/main/java/dev/ultreon/mods/xinexlib/client/event/LocalPlayerQuitEvent.java @@ -0,0 +1,16 @@ +package dev.ultreon.mods.xinexlib.client.event; + +import net.minecraft.client.player.LocalPlayer; + +public class LocalPlayerQuitEvent implements LocalPlayerEvent { + private final LocalPlayer localPlayer; + + public LocalPlayerQuitEvent(LocalPlayer localPlayer) { + this.localPlayer = localPlayer; + } + + @Override + public LocalPlayer getPlayer() { + return localPlayer; + } +} diff --git a/common/src/main/java/dev/ultreon/mods/xinexlib/event/player/ServerPlayerEvent.java b/common/src/main/java/dev/ultreon/mods/xinexlib/event/player/ServerPlayerEvent.java index 13e1cda..fa09ea1 100644 --- a/common/src/main/java/dev/ultreon/mods/xinexlib/event/player/ServerPlayerEvent.java +++ b/common/src/main/java/dev/ultreon/mods/xinexlib/event/player/ServerPlayerEvent.java @@ -1,6 +1,7 @@ package dev.ultreon.mods.xinexlib.event.player; import dev.ultreon.mods.xinexlib.event.entity.EntityEvent; +import net.minecraft.server.MinecraftServer; import net.minecraft.server.level.ServerPlayer; import net.minecraft.world.entity.Entity; import net.minecraft.world.entity.player.Player; @@ -8,4 +9,9 @@ public interface ServerPlayerEvent extends PlayerEvent, ServerEvent { @Override ServerPlayer getPlayer(); + + @Override + default MinecraftServer getServer() { + return getPlayer().getServer(); + } } diff --git a/common/src/main/java/dev/ultreon/mods/xinexlib/event/server/ServerLevelSaveEvent.java b/common/src/main/java/dev/ultreon/mods/xinexlib/event/server/ServerLevelSaveEvent.java new file mode 100644 index 0000000..bbf368e --- /dev/null +++ b/common/src/main/java/dev/ultreon/mods/xinexlib/event/server/ServerLevelSaveEvent.java @@ -0,0 +1,59 @@ +package dev.ultreon.mods.xinexlib.event.server; + +import dev.ultreon.mods.xinexlib.event.level.ServerLevelEvent; +import net.minecraft.server.level.ServerLevel; +import net.minecraft.util.ProgressListener; + +import java.util.Objects; + +public class ServerLevelSaveEvent implements ServerLevelEvent { + private final ServerLevel serverLevel; + private final ProgressListener progress; + private final boolean flush; + private final boolean skipSave; + + public ServerLevelSaveEvent(ServerLevel serverLevel, ProgressListener progress, boolean flush, boolean skipSave) { + this.serverLevel = serverLevel; + this.progress = progress; + this.flush = flush; + this.skipSave = skipSave; + } + + @Override + public ServerLevel getServerLevel() { + return serverLevel; + } + + public ProgressListener getProgress() { + return progress; + } + + public boolean isFlush() { + return flush; + } + + public boolean isSkipSave() { + return skipSave; + } + + @Override + public boolean equals(Object o) { + if (!(o instanceof ServerLevelSaveEvent that)) return false; + return flush == that.flush && skipSave == that.skipSave && Objects.equals(serverLevel, that.serverLevel) && Objects.equals(progress, that.progress); + } + + @Override + public int hashCode() { + return Objects.hash(serverLevel, progress, flush, skipSave); + } + + @Override + public String toString() { + return "ServerLevelSaveEvent{" + + "serverLevel=" + serverLevel + + ", progress=" + progress + + ", flush=" + flush + + ", skipSave=" + skipSave + + '}'; + } +} diff --git a/common/src/main/java/dev/ultreon/mods/xinexlib/event/server/ServerPlayerQuitEvent.java b/common/src/main/java/dev/ultreon/mods/xinexlib/event/server/ServerPlayerQuitEvent.java new file mode 100644 index 0000000..02414c2 --- /dev/null +++ b/common/src/main/java/dev/ultreon/mods/xinexlib/event/server/ServerPlayerQuitEvent.java @@ -0,0 +1,36 @@ +package dev.ultreon.mods.xinexlib.event.server; + +import dev.ultreon.mods.xinexlib.event.player.ServerPlayerEvent; +import net.minecraft.server.level.ServerPlayer; + +import java.util.Objects; + +public class ServerPlayerQuitEvent implements ServerPlayerEvent { + private final ServerPlayer player; + + public ServerPlayerQuitEvent(ServerPlayer player) { + this.player = player; + } + + public ServerPlayer getPlayer() { + return player; + } + + @Override + public boolean equals(Object o) { + if (o == null || getClass() != o.getClass()) return false; + ServerPlayerQuitEvent that = (ServerPlayerQuitEvent) o; + return Objects.equals(player, that.player); + } + + @Override + public int hashCode() { + return Objects.hashCode(player); + } + + @Override + public String toString() { + return "ServerPlayerQuitEvent{" + + "player=" + player + '}'; + } +} diff --git a/common/src/main/java/dev/ultreon/mods/xinexlib/mixin/MixinClientPacketListener.java b/common/src/main/java/dev/ultreon/mods/xinexlib/mixin/MixinClientPacketListener.java new file mode 100644 index 0000000..a0e69a5 --- /dev/null +++ b/common/src/main/java/dev/ultreon/mods/xinexlib/mixin/MixinClientPacketListener.java @@ -0,0 +1,28 @@ +package dev.ultreon.mods.xinexlib.mixin; + +import com.mojang.blaze3d.platform.Window; +import dev.ultreon.mods.xinexlib.client.event.ClientRenderTickEvent; +import dev.ultreon.mods.xinexlib.client.event.ClientStartedEvent; +import dev.ultreon.mods.xinexlib.client.event.ClientStoppingEvent; +import dev.ultreon.mods.xinexlib.client.event.LocalPlayerJoinEvent; +import dev.ultreon.mods.xinexlib.event.system.EventSystem; +import net.minecraft.client.Minecraft; +import net.minecraft.client.multiplayer.ClientPacketListener; +import org.lwjgl.glfw.GLFW; +import org.spongepowered.asm.mixin.Final; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Shadow; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; + +/// @author XyperCode +/// @since 0.1.0 (December 10, 2024) +@Mixin(ClientPacketListener.class) +public abstract class MixinClientPacketListener { + + @Inject(at = @At("RETURN"), method = "handleLogin") + private void runTick$return(CallbackInfo ci) { + EventSystem.MAIN.publish(new LocalPlayerJoinEvent(Minecraft.getInstance().player)); + } +} diff --git a/common/src/main/java/dev/ultreon/mods/xinexlib/mixin/MixinConnection.java b/common/src/main/java/dev/ultreon/mods/xinexlib/mixin/MixinConnection.java new file mode 100644 index 0000000..8b04a2b --- /dev/null +++ b/common/src/main/java/dev/ultreon/mods/xinexlib/mixin/MixinConnection.java @@ -0,0 +1,30 @@ +package dev.ultreon.mods.xinexlib.mixin; + +import dev.ultreon.mods.xinexlib.Env; +import dev.ultreon.mods.xinexlib.EnvExecutor; +import dev.ultreon.mods.xinexlib.client.event.LocalPlayerJoinEvent; +import dev.ultreon.mods.xinexlib.client.event.LocalPlayerQuitEvent; +import dev.ultreon.mods.xinexlib.event.system.EventSystem; +import net.minecraft.client.Minecraft; +import net.minecraft.client.multiplayer.ClientPacketListener; +import net.minecraft.network.Connection; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; + +/// @author XyperCode +/// @since 0.1.0 (December 10, 2024) +@Mixin(Connection.class) +public abstract class MixinConnection { + + @Inject(at = @At("HEAD"), method = "channelInactive") + private void channelInactive(CallbackInfo ci) { + EnvExecutor.runInEnv(Env.CLIENT, () -> () -> EventSystem.MAIN.publish(new LocalPlayerQuitEvent(Minecraft.getInstance().player))); + } + + @Inject(at = @At("HEAD"), method = "disconnect(Lnet/minecraft/network/DisconnectionDetails;)V") + private void runTick$return(CallbackInfo ci) { + EnvExecutor.runInEnv(Env.CLIENT, () -> () -> EventSystem.MAIN.publish(new LocalPlayerQuitEvent(Minecraft.getInstance().player))); + } +} diff --git a/common/src/main/java/dev/ultreon/mods/xinexlib/mixin/MixinPlayerList.java b/common/src/main/java/dev/ultreon/mods/xinexlib/mixin/MixinPlayerList.java index 2ae2b38..ff4a5ec 100644 --- a/common/src/main/java/dev/ultreon/mods/xinexlib/mixin/MixinPlayerList.java +++ b/common/src/main/java/dev/ultreon/mods/xinexlib/mixin/MixinPlayerList.java @@ -56,6 +56,11 @@ private void handlePlayerLoggedIn(Connection pConnection, ServerPlayer pPlayer, )); } + @Inject(method = "remove", at = @At(value = "HEAD")) + private void handlePlayerLoggedIn(ServerPlayer pPlayer, CallbackInfo ci) { + EventSystem.MAIN.publish(new ServerPlayerQuitEvent(pPlayer)); + } + @Inject(method = "canPlayerLogin", at = @At(value = "HEAD"), cancellable = true) private void handleLoginVerifying(SocketAddress pSocketAddress, GameProfile pGameProfile, CallbackInfoReturnable cir) { ServerPlayerVerifyLoginEvent event = EventSystem.MAIN.publish(new ServerPlayerVerifyLoginEvent( diff --git a/common/src/main/java/dev/ultreon/mods/xinexlib/mixin/MixinServerLevel.java b/common/src/main/java/dev/ultreon/mods/xinexlib/mixin/MixinServerLevel.java index f2b042e..6d46d4e 100644 --- a/common/src/main/java/dev/ultreon/mods/xinexlib/mixin/MixinServerLevel.java +++ b/common/src/main/java/dev/ultreon/mods/xinexlib/mixin/MixinServerLevel.java @@ -1,15 +1,22 @@ package dev.ultreon.mods.xinexlib.mixin; +import dev.ultreon.mods.xinexlib.client.event.ClientStoppingEvent; import dev.ultreon.mods.xinexlib.event.entity.EntitySpawnEvent; +import dev.ultreon.mods.xinexlib.event.server.ServerLevelSaveEvent; import dev.ultreon.mods.xinexlib.event.system.EventSystem; import net.minecraft.server.level.ServerLevel; +import net.minecraft.util.ProgressListener; import net.minecraft.world.entity.Entity; import net.minecraft.world.level.Level; +import org.lwjgl.glfw.GLFW; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; +import static java.awt.SystemColor.window; + @Mixin(ServerLevel.class) public class MixinServerLevel { @Inject(method = "addFreshEntity", at = @At("HEAD"), cancellable = true) @@ -29,4 +36,9 @@ private void addExistingEntity(Entity pEntity, CallbackInfoReturnable c cir.setReturnValue(false); } } + + @Inject(at = @At("HEAD"), method = "save") + private void save(ProgressListener pProgress, boolean pFlush, boolean pSkipSave, CallbackInfo ci) { + EventSystem.MAIN.publish(new ServerLevelSaveEvent((ServerLevel) (Object) this, pProgress, pFlush, pSkipSave)); + } } diff --git a/common/src/main/java/dev/ultreon/mods/xinexlib/platform/Services.java b/common/src/main/java/dev/ultreon/mods/xinexlib/platform/Services.java index 0c8cc9e..ca4fa3b 100644 --- a/common/src/main/java/dev/ultreon/mods/xinexlib/platform/Services.java +++ b/common/src/main/java/dev/ultreon/mods/xinexlib/platform/Services.java @@ -1,6 +1,7 @@ package dev.ultreon.mods.xinexlib.platform; import dev.ultreon.mods.xinexlib.Constants; +import dev.ultreon.mods.xinexlib.Env; import dev.ultreon.mods.xinexlib.ModPlatform; import dev.ultreon.mods.xinexlib.components.IComponentManager; import dev.ultreon.mods.xinexlib.platform.services.IPlatformHelper; @@ -65,4 +66,8 @@ public static IComponentManager getComponentManager(String modId) { public static ICreativeModeTabBuilder creativeTabBuilder() { return PLATFORM.creativeTabBuilder(); } + + public static Env getEnv() { + return PLATFORM.getEnv(); + } } diff --git a/common/src/main/java/dev/ultreon/mods/xinexlib/platform/services/IPlatformHelper.java b/common/src/main/java/dev/ultreon/mods/xinexlib/platform/services/IPlatformHelper.java index 3c31140..1fe0493 100644 --- a/common/src/main/java/dev/ultreon/mods/xinexlib/platform/services/IPlatformHelper.java +++ b/common/src/main/java/dev/ultreon/mods/xinexlib/platform/services/IPlatformHelper.java @@ -1,5 +1,6 @@ package dev.ultreon.mods.xinexlib.platform.services; +import dev.ultreon.mods.xinexlib.Env; import dev.ultreon.mods.xinexlib.ModPlatform; import dev.ultreon.mods.xinexlib.components.ComponentManager; import dev.ultreon.mods.xinexlib.components.IComponentManager; @@ -47,4 +48,6 @@ default IComponentManager getComponentManager(String modId) { } ICreativeModeTabBuilder creativeTabBuilder(); + + Env getEnv(); } diff --git a/common/src/main/resources/xinexlib.mixins.json b/common/src/main/resources/xinexlib.mixins.json index 2794c10..c3bd89a 100644 --- a/common/src/main/resources/xinexlib.mixins.json +++ b/common/src/main/resources/xinexlib.mixins.json @@ -6,6 +6,7 @@ "compatibilityLevel": "JAVA_18", "mixins": [ "MixinBlockItem", + "MixinConnection", "MixinEntity", "MixinLevel", "MixinLivingEntity", @@ -13,6 +14,7 @@ "MixinServerLevel" ], "client": [ + "MixinClientPacketListener", "MixinMinecraft" ], "server": [], diff --git a/fabric/src/main/java/dev/ultreon/mods/xinexlib/platform/FabricPlatformHelper.java b/fabric/src/main/java/dev/ultreon/mods/xinexlib/platform/FabricPlatformHelper.java index 2a39fc7..15622b5 100644 --- a/fabric/src/main/java/dev/ultreon/mods/xinexlib/platform/FabricPlatformHelper.java +++ b/fabric/src/main/java/dev/ultreon/mods/xinexlib/platform/FabricPlatformHelper.java @@ -1,5 +1,6 @@ package dev.ultreon.mods.xinexlib.platform; +import dev.ultreon.mods.xinexlib.Env; import dev.ultreon.mods.xinexlib.ModPlatform; import dev.ultreon.mods.xinexlib.platform.services.IPlatformHelper; import dev.ultreon.mods.xinexlib.platform.services.IRegistrarManager; @@ -32,4 +33,12 @@ public IRegistrarManager getRegistrarManager(String modId) { public ICreativeModeTabBuilder creativeTabBuilder() { return new FabricCreativeTabBuilder(); } + + @Override + public Env getEnv() { + return switch (FabricLoader.getInstance().getEnvironmentType()) { + case CLIENT -> Env.CLIENT; + case SERVER -> Env.SERVER; + }; + } } diff --git a/forge/build.gradle b/forge/build.gradle index d625da1..693632d 100644 --- a/forge/build.gradle +++ b/forge/build.gradle @@ -4,7 +4,7 @@ plugins { id 'org.spongepowered.mixin' version '0.7-SNAPSHOT' } base { - archivesName = "${mod_name}-forge-${minecraft_version}" + archivesName = "${rootProject.name}-forge-${minecraft_version}" } mixin { config("${mod_id}.mixins.json") diff --git a/forge/src/main/java/dev/ultreon/mods/xinexlib/platform/ForgePlatformHelper.java b/forge/src/main/java/dev/ultreon/mods/xinexlib/platform/ForgePlatformHelper.java index b1de353..1c83c27 100644 --- a/forge/src/main/java/dev/ultreon/mods/xinexlib/platform/ForgePlatformHelper.java +++ b/forge/src/main/java/dev/ultreon/mods/xinexlib/platform/ForgePlatformHelper.java @@ -1,10 +1,12 @@ package dev.ultreon.mods.xinexlib.platform; +import dev.ultreon.mods.xinexlib.Env; import dev.ultreon.mods.xinexlib.ModPlatform; import dev.ultreon.mods.xinexlib.platform.services.IPlatformHelper; import dev.ultreon.mods.xinexlib.platform.services.IRegistrarManager; import dev.ultreon.mods.xinexlib.tabs.ICreativeModeTabBuilder; import net.minecraftforge.fml.ModList; +import net.minecraftforge.fml.loading.FMLEnvironment; import net.minecraftforge.fml.loading.FMLLoader; public class ForgePlatformHelper implements IPlatformHelper { @@ -33,4 +35,12 @@ public IRegistrarManager getRegistrarManager(String modId) { public ICreativeModeTabBuilder creativeTabBuilder() { return new ForgeCreativeTabBuilder(); } + + @Override + public Env getEnv() { + return switch (FMLEnvironment.dist) { + case CLIENT -> Env.CLIENT; + case DEDICATED_SERVER -> Env.SERVER; + }; + } } diff --git a/neoforge/src/main/java/dev/ultreon/mods/xinexlib/platform/NeoForgePlatformHelper.java b/neoforge/src/main/java/dev/ultreon/mods/xinexlib/platform/NeoForgePlatformHelper.java index 1bf4b78..e4e9561 100644 --- a/neoforge/src/main/java/dev/ultreon/mods/xinexlib/platform/NeoForgePlatformHelper.java +++ b/neoforge/src/main/java/dev/ultreon/mods/xinexlib/platform/NeoForgePlatformHelper.java @@ -1,11 +1,13 @@ package dev.ultreon.mods.xinexlib.platform; +import dev.ultreon.mods.xinexlib.Env; import dev.ultreon.mods.xinexlib.ModPlatform; import dev.ultreon.mods.xinexlib.platform.services.IPlatformHelper; import dev.ultreon.mods.xinexlib.platform.services.IRegistrarManager; import dev.ultreon.mods.xinexlib.tabs.ICreativeModeTabBuilder; import net.neoforged.bus.api.IEventBus; import net.neoforged.fml.ModList; +import net.neoforged.fml.loading.FMLEnvironment; import net.neoforged.fml.loading.FMLLoader; import java.util.HashMap; @@ -42,6 +44,14 @@ public ICreativeModeTabBuilder creativeTabBuilder() { return new NeoForgeCreativeTabBuilder(); } + @Override + public Env getEnv() { + return switch (FMLEnvironment.dist) { + case CLIENT -> Env.CLIENT; + case DEDICATED_SERVER -> Env.SERVER; + }; + } + public void registerMod(String modId, IEventBus modEventBus) { this.registrars.put(modId, new NeoForgeRegistrarManager(modId, modEventBus)); } diff --git a/settings.gradle b/settings.gradle index 664ba4b..0da8a3b 100644 --- a/settings.gradle +++ b/settings.gradle @@ -44,7 +44,7 @@ plugins { } // This should match the folder name of the project, or else IDEA may complain (see https://youtrack.jetbrains.com/issue/IDEA-317606) -rootProject.name = 'XinexLib' +rootProject.name = 'xinexlib' include('common') include('fabric') include('neoforge')