From 301e7252b04f40f57c44528e1d5ba49f119fd64c Mon Sep 17 00:00:00 2001 From: Paint_Ninja Date: Fri, 14 Jul 2023 07:06:37 -0700 Subject: [PATCH] Add option to advertise dedicated servers to LAN. (#9502) --- build.gradle | 8 +++- .../client/server/LanServerPinger.java.patch | 4 +- .../dedicated/DedicatedServer.java.patch | 38 ++++++++++++++++++- .../minecraftforge/common/ForgeConfig.java | 7 ++++ src/main/resources/forge.sas | 4 +- 5 files changed, 55 insertions(+), 6 deletions(-) diff --git a/build.gradle b/build.gradle index bd5d1462aa..ca75173d36 100644 --- a/build.gradle +++ b/build.gradle @@ -27,8 +27,8 @@ plugins { id 'net.minecraftforge.gradleutils' version '2.+' id 'eclipse' id 'de.undercouch.download' version '5.1.3' - id 'net.minecraftforge.gradle.patcher' version '[5.1.50,5.2.0)' apply false - id 'net.minecraftforge.gradle.mcp' version '[5.1.50,5.2.0)' apply false + id 'net.minecraftforge.gradle.patcher' version '[5.1.77,5.2.0)' apply false + id 'net.minecraftforge.gradle.mcp' version '[5.1.77,5.2.0)' apply false } Util.init() //Init all our extension methods! @@ -884,6 +884,10 @@ project(':forge') { apply plugin: 'net.minecraftforge.gradle.patcher' apply plugin: 'org.cadixdev.licenser' + applyPatches { + level 'WARNING' + } + sourceSets { main { java { diff --git a/patches/minecraft/net/minecraft/client/server/LanServerPinger.java.patch b/patches/minecraft/net/minecraft/client/server/LanServerPinger.java.patch index e89de40562..ed1cb082ba 100644 --- a/patches/minecraft/net/minecraft/client/server/LanServerPinger.java.patch +++ b/patches/minecraft/net/minecraft/client/server/LanServerPinger.java.patch @@ -1,6 +1,6 @@ --- a/net/minecraft/client/server/LanServerPinger.java +++ b/net/minecraft/client/server/LanServerPinger.java -@@ -16,7 +_,7 @@ +@@ -13,7 +_,7 @@ public class LanServerPinger extends Thread { private static final AtomicInteger f_120101_ = new AtomicInteger(0); private static final Logger f_120102_ = LogUtils.getLogger(); @@ -9,7 +9,7 @@ public static final int f_174975_ = 4445; private static final long f_174976_ = 1500L; private final String f_120103_; -@@ -39,7 +_,7 @@ +@@ -36,7 +_,7 @@ while(!this.isInterrupted() && this.f_120105_) { try { diff --git a/patches/minecraft/net/minecraft/server/dedicated/DedicatedServer.java.patch b/patches/minecraft/net/minecraft/server/dedicated/DedicatedServer.java.patch index fd883412b0..b1c34abbfa 100644 --- a/patches/minecraft/net/minecraft/server/dedicated/DedicatedServer.java.patch +++ b/patches/minecraft/net/minecraft/server/dedicated/DedicatedServer.java.patch @@ -1,5 +1,14 @@ --- a/net/minecraft/server/dedicated/DedicatedServer.java +++ b/net/minecraft/server/dedicated/DedicatedServer.java +@@ -68,6 +_,8 @@ + private MinecraftServerGui f_139605_; + @Nullable + private final TextFilterClient f_139606_; ++ @Nullable ++ private net.minecraft.client.server.LanServerPinger dediLanPinger; + + public DedicatedServer(Thread p_214789_, LevelStorageSource.LevelStorageAccess p_214790_, PackRepository p_214791_, WorldStem p_214792_, DedicatedServerSettings p_214793_, DataFixer p_214794_, Services p_214795_, ChunkProgressListenerFactory p_214796_) { + super(p_214789_, p_214790_, p_214791_, p_214792_, Proxy.NO_PROXY, p_214794_, p_214795_, p_214796_); @@ -79,6 +_,7 @@ public boolean m_7038_() throws IOException { Thread thread = new Thread("Server console handler") { @@ -22,7 +31,7 @@ if (dedicatedserverproperties.f_139744_ != null) { this.m_129900_().m_46170_(GameRules.f_46153_).m_46246_(dedicatedserverproperties.f_139744_, this); } -@@ -183,12 +_,13 @@ +@@ -183,12 +_,18 @@ thread1.start(); } @@ -33,7 +42,34 @@ } - return true; ++ if (net.minecraftforge.common.ForgeConfig.SERVER.advertiseDedicatedServerToLan.get()) { ++ this.dediLanPinger = new net.minecraft.client.server.LanServerPinger(this.m_129916_(), String.valueOf(this.m_7448_())); ++ this.dediLanPinger.start(); ++ } ++ + return net.minecraftforge.server.ServerLifecycleHooks.handleServerStarting(this); } } +@@ -263,6 +_,10 @@ + this.f_139601_.m_7530_(); + } + ++ if (this.dediLanPinger != null) { ++ this.dediLanPinger.interrupt(); ++ this.dediLanPinger = null; ++ } + } + + public void m_5703_(BooleanSupplier p_139661_) { +@@ -492,6 +_,10 @@ + + public void m_7041_() { + super.m_7041_(); ++ if (this.dediLanPinger != null) { ++ this.dediLanPinger.interrupt(); ++ this.dediLanPinger = null; ++ } + Util.m_137580_(); + SkullBlockEntity.m_196704_(); + } diff --git a/src/main/java/net/minecraftforge/common/ForgeConfig.java b/src/main/java/net/minecraftforge/common/ForgeConfig.java index a02b69260c..ee776d20d7 100644 --- a/src/main/java/net/minecraftforge/common/ForgeConfig.java +++ b/src/main/java/net/minecraftforge/common/ForgeConfig.java @@ -30,6 +30,8 @@ public class ForgeConfig { public final ConfigValue permissionHandler; + public final BooleanValue advertiseDedicatedServerToLan; + Server(ForgeConfigSpec.Builder builder) { builder.comment("Server configuration settings") .push("server"); @@ -69,6 +71,11 @@ public class ForgeConfig { .translation("forge.configgui.permissionHandler") .define("permissionHandler", "forge:default_handler"); + advertiseDedicatedServerToLan = builder + .comment("Set this to true to enable advertising the dedicated server to local LAN clients so that it shows up in the Multiplayer screen automatically.") + .translation("forge.configgui.advertiseDedicatedServerToLan") + .define("advertiseDedicatedServerToLan", true); + builder.pop(); } } diff --git a/src/main/resources/forge.sas b/src/main/resources/forge.sas index e9a3db032a..14b42c20a5 100644 --- a/src/main/resources/forge.sas +++ b/src/main/resources/forge.sas @@ -4,4 +4,6 @@ # checkAndFixSAS will populate all overrides as nessasary. Prefixing added lines with \t # checkAndFixSAS also supports names using . so simplest way to find the correct line to add is to get the AT line from the bot # and remove the access modifier. -#================================================================================================================================== \ No newline at end of file +# You need to run the setup task again after running checkAndFixSAS for genPatches to pick up the changes. +#================================================================================================================================== +net/minecraft/client/server/LanServerPinger \ No newline at end of file