Skip to content

Commit

Permalink
Ensure mod does not run at all on clients
Browse files Browse the repository at this point in the history
Probably not a necessary check, but I feel like I should be explicit.
  • Loading branch information
Gunni committed Jan 1, 2024
1 parent 1947e22 commit cfbdabd
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 17 deletions.
4 changes: 2 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ yarn_mappings=1.20.4+build.3
loader_version=0.15.3

# Mod Properties
mod_version=1.0.0
mod_version=1.0.1
maven_group=is.meh.minecraft.lan_announcer
archives_base_name=lanannouncer

# Dependencies
fabric_version=0.92.0+1.20.4
fabric_version=0.92.0+1.20.4
26 changes: 12 additions & 14 deletions src/main/java/is/meh/minecraft/lan_announcer/LANAnnouncer.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents;
import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.server.MinecraftServer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -21,9 +22,16 @@ public class LANAnnouncer implements ModInitializer {

@Override
public void onInitialize() {
LOGGER.debug("onInitialize");
ServerLifecycleEvents.SERVER_STARTED.register(this::onServerStarted);
ServerLifecycleEvents.SERVER_STOPPING.register(this::onServerStopping);
if (FabricLoader.getInstance().getEnvironmentType() == net.fabricmc.api.EnvType.SERVER) {
ServerLifecycleEvents.SERVER_STARTED.register(this::onServerStarted);
ServerLifecycleEvents.SERVER_STOPPING.register(this::onServerStopping);
} else {
LOGGER.warn(
"This mod is only intended for dedicated multiplayer" +
"servers, not clients. Clients can start their own built-in" +
"LAN broadcast by using `Open to LAN`"
);
}
}

// Prevent conflicts with the packet payload
Expand All @@ -32,32 +40,22 @@ private String sanitizeMOTD(String motd) {
}

private void onServerStarted(MinecraftServer server) {
LOGGER.info("onServerStarted");
String motd = sanitizeMOTD(server.getServerMotd());
int server_port = server.getServerPort();

String payload = "[MOTD]" + motd + "[/MOTD][AD]" + server_port + "[/AD]";
LOGGER.info("payload: {}", payload);

byte[] message = payload.getBytes();
byte[] message = ("[MOTD]" + motd + "[/MOTD][AD]" + server_port + "[/AD]").getBytes();

// Initialize and start the IPv4 and IPv6 announcers
ipv4Announcer = new ServerAnnouncer("224.0.2.60", message);
ipv4Announcer.startAnnouncing();

ipv6Announcer = new ServerAnnouncer("ff75:230::60", message);
ipv6Announcer.startAnnouncing();

LOGGER.debug("started");
}

private void onServerStopping(MinecraftServer server) {
LOGGER.info("onServerStopping");

ipv4Announcer.stopAnnouncing();
ipv6Announcer.stopAnnouncing();

LOGGER.debug("stopped...");
}

static class ServerAnnouncer {
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/fabric.mod.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
},
"license": "CC0-1.0",
"icon": "assets/lanannouncer/icon.png",
"environment": "*",
"environment": "server",
"entrypoints": {
"main": [
"is.meh.minecraft.lan_announcer.LANAnnouncer"
Expand Down

0 comments on commit cfbdabd

Please sign in to comment.