mirror of
https://github.com/Minestom/Minestom.git
synced 2025-01-04 23:47:59 +01:00
Fix & cleanup OpenToLAN
This commit is contained in:
parent
9ec257ed3d
commit
23c76bc799
@ -59,12 +59,9 @@ public class OpenToLAN {
|
|||||||
*/
|
*/
|
||||||
public static boolean open(@NotNull OpenToLANConfig config) {
|
public static boolean open(@NotNull OpenToLANConfig config) {
|
||||||
Objects.requireNonNull(config, "config");
|
Objects.requireNonNull(config, "config");
|
||||||
|
if (socket != null) return false;
|
||||||
|
|
||||||
if (socket != null) {
|
|
||||||
return false;
|
|
||||||
} else {
|
|
||||||
int port = config.port;
|
int port = config.port;
|
||||||
|
|
||||||
if (port == 0) {
|
if (port == 0) {
|
||||||
try {
|
try {
|
||||||
port = NetworkUtils.getFreePort();
|
port = NetworkUtils.getFreePort();
|
||||||
@ -87,7 +84,6 @@ public class OpenToLAN {
|
|||||||
.schedule();
|
.schedule();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Closes the server to LAN.
|
* Closes the server to LAN.
|
||||||
@ -95,18 +91,14 @@ public class OpenToLAN {
|
|||||||
* @return {@code true} if it was closed, {@code false} if it was already closed
|
* @return {@code true} if it was closed, {@code false} if it was already closed
|
||||||
*/
|
*/
|
||||||
public static boolean close() {
|
public static boolean close() {
|
||||||
if (socket == null) {
|
if (socket == null) return false;
|
||||||
return false;
|
|
||||||
} else {
|
|
||||||
task.cancel();
|
task.cancel();
|
||||||
socket.close();
|
socket.close();
|
||||||
|
|
||||||
task = null;
|
task = null;
|
||||||
socket = null;
|
socket = null;
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Checks if the server is currently opened to LAN.
|
* Checks if the server is currently opened to LAN.
|
||||||
@ -121,7 +113,7 @@ public class OpenToLAN {
|
|||||||
* Performs the ping.
|
* Performs the ping.
|
||||||
*/
|
*/
|
||||||
private static void ping() {
|
private static void ping() {
|
||||||
if (MinecraftServer.getServer().getPort() != 0) {
|
if (!MinecraftServer.getServer().isOpen()) return;
|
||||||
if (packet == null || eventCooldown.isReady(System.currentTimeMillis())) {
|
if (packet == null || eventCooldown.isReady(System.currentTimeMillis())) {
|
||||||
final ServerListPingEvent event = new ServerListPingEvent(OPEN_TO_LAN);
|
final ServerListPingEvent event = new ServerListPingEvent(OPEN_TO_LAN);
|
||||||
EventDispatcher.call(event);
|
EventDispatcher.call(event);
|
||||||
@ -132,12 +124,10 @@ public class OpenToLAN {
|
|||||||
eventCooldown.refreshLastUpdate(System.currentTimeMillis());
|
eventCooldown.refreshLastUpdate(System.currentTimeMillis());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
socket.send(packet);
|
socket.send(packet);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
LOGGER.warn("Could not send Open to LAN packet!", e);
|
LOGGER.warn("Could not send Open to LAN packet!", e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -5,6 +5,7 @@ import org.slf4j.Logger;
|
|||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.net.InetSocketAddress;
|
||||||
import java.net.SocketAddress;
|
import java.net.SocketAddress;
|
||||||
import java.nio.channels.SelectionKey;
|
import java.nio.channels.SelectionKey;
|
||||||
import java.nio.channels.Selector;
|
import java.nio.channels.Selector;
|
||||||
@ -41,6 +42,12 @@ public final class Server {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void start(SocketAddress address) throws IOException {
|
public void start(SocketAddress address) throws IOException {
|
||||||
|
if (address instanceof InetSocketAddress) {
|
||||||
|
InetSocketAddress inetSocketAddress = (InetSocketAddress) address;
|
||||||
|
this.address = inetSocketAddress.getHostString();
|
||||||
|
this.port = inetSocketAddress.getPort();
|
||||||
|
} // TODO unix domain support
|
||||||
|
|
||||||
this.serverSocket = ServerSocketChannel.open();
|
this.serverSocket = ServerSocketChannel.open();
|
||||||
serverSocket.bind(address);
|
serverSocket.bind(address);
|
||||||
serverSocket.configureBlocking(false);
|
serverSocket.configureBlocking(false);
|
||||||
|
Loading…
Reference in New Issue
Block a user