Skip to content
Open
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
13 changes: 0 additions & 13 deletions src/main/java/dev/anvilcraft/rg/RollingGate.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,12 @@
import net.neoforged.bus.api.SubscribeEvent;
import net.neoforged.fml.ModContainer;
import net.neoforged.fml.common.Mod;
import net.neoforged.fml.loading.FMLEnvironment;
import net.neoforged.neoforge.common.NeoForge;
import net.neoforged.neoforge.event.RegisterCommandsEvent;
import net.neoforged.neoforge.event.server.ServerStartingEvent;
import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

@Mod(RollingGate.MODID)
public class RollingGate {
public static final String MODID = "rolling_gate";
Expand All @@ -33,15 +29,6 @@ public RollingGate(@NotNull IEventBus modEventBus, @SuppressWarnings("unused") @
NeoForge.EVENT_BUS.addListener(RollingGateNetwork::onPlayerLoggedOut);
NeoForge.EVENT_BUS.addListener(RollingGateNetwork::onServerStopped);
SERVER_RULE_MANAGER.compileContent();
if (FMLEnvironment.dist.isClient()) {
Class<?> clientClass = RollingGate.class.getClassLoader().loadClass("dev.anvilcraft.rg.client.RollingGateClient");
try {
Method setup = clientClass.getMethod("onClientSetup", ModContainer.class);
setup.invoke(null, modContainer);
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
RollingGate.LOGGER.error(e.getMessage(), e);
}
}
}

@SubscribeEvent
Expand Down
20 changes: 20 additions & 0 deletions src/main/java/dev/anvilcraft/rg/client/RollingGateClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import net.neoforged.neoforge.common.ModConfigSpec;
import net.neoforged.neoforge.common.NeoForge;
import net.neoforged.neoforge.network.PacketDistributor;
import net.neoforged.neoforge.event.server.ServerStoppedEvent;
import net.neoforged.neoforge.server.ServerLifecycleHooks;
import org.jetbrains.annotations.NotNull;

import java.util.Map;
Expand All @@ -41,6 +43,7 @@ public RollingGateClient(@NotNull IEventBus eventBus, @NotNull ModContainer modC
NeoForge.EVENT_BUS.addListener(RollingGateClient::onLoaded);
NeoForge.EVENT_BUS.addListener(RollingGateClient::onLoggingIn);
NeoForge.EVENT_BUS.addListener(RollingGateClient::onLoggingOut);
NeoForge.EVENT_BUS.addListener(RollingGateClient::onIntegratedServerStopped);
}

@SubscribeEvent
Expand All @@ -57,9 +60,26 @@ public static void onLoggingIn(final @NotNull ClientPlayerNetworkEvent.LoggingIn
}

public static void onLoggingOut(final @NotNull ClientPlayerNetworkEvent.LoggingOut event) {
var connection = event.getConnection();
var server = ServerLifecycleHooks.getCurrentServer();
boolean integrated = (connection != null && connection.isMemoryConnection())
|| (server != null && !server.isDedicatedServer());
// Server rules are static fields shared with the integrated server. It
// still needs them when saving during its later shutdown event.
if (integrated) return;
RollingGateClient.CLIENT_SERVER_RULE_MANAGER.resetRulesToDefault();
}

public static void onIntegratedServerStopped(final @NotNull ServerStoppedEvent event) {
if (event.getServer().isDedicatedServer()) return;
net.minecraft.client.Minecraft.getInstance().execute(() -> {
// Do not overwrite rules already applied by a newly connected server.
if (net.minecraft.client.Minecraft.getInstance().getConnection() == null) {
CLIENT_SERVER_RULE_MANAGER.resetRulesToDefault();
}
});
}

@SubscribeEvent
public <T> void onLoad(final @NotNull RGValidatorNotPassedEvent<T> event) {
RollingGateClient.CLIENT_RULE_MANAGER.reInit();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@ public static void onRuleChange(@NotNull RGRuleChangeEvent.Client<?> event) {
@SubscribeEvent
public static void onWindowResizableChange(@NotNull RGRuleChangeEvent.Client<Boolean> event) {
if (!"windowResizable".equals(event.getRule().name())) return;
if (event.getNewValue()) {
GLFW.glfwSetWindowAttrib(Minecraft.getInstance().getWindow().getWindow(), GLFW.GLFW_RESIZABLE, GLFW.GLFW_TRUE);
} else {
GLFW.glfwSetWindowAttrib(Minecraft.getInstance().getWindow().getWindow(), GLFW.GLFW_RESIZABLE, GLFW.GLFW_FALSE);
}
Minecraft minecraft = Minecraft.getInstance();
boolean resizable = event.getNewValue();
// Config loading can post this event on a mod-loading worker. GLFW window
// operations must run on Minecraft's client thread, particularly on macOS.
minecraft.execute(() -> GLFW.glfwSetWindowAttrib(minecraft.getWindow().getWindow(),
GLFW.GLFW_RESIZABLE, resizable ? GLFW.GLFW_TRUE : GLFW.GLFW_FALSE));
}
}