Files
src
build.gradle
gradle.properties
settings.gradle
Code
8 342
src/client/java/com/aniketplaysz/apsessential/ApsEssentialClient.java
package com.aniketplaysz.apsessential;
import com.aniketplaysz.apsessential.mixin.IntegratedServerAccessor;
import com.aniketplaysz.apsessential.mixin.ServerNetworkIoAccessor;
import io.netty.channel.ChannelFuture;
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.fabric.api.client.command.v2.ClientCommandManager;
import net.fabricmc.fabric.api.client.command.v2.ClientCommandRegistrationCallback;
import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource;
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents;
import net.minecraft.client.MinecraftClient;
import net.minecraft.server.integrated.IntegratedServer;
import net.minecraft.server.ServerNetworkIo;
import net.minecraft.client.network.LanServerPinger;
import net.minecraft.text.ClickEvent;
import net.minecraft.text.Text;
import net.minecraft.util.Formatting;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.ThreadLocalRandom;
public class ApsEssentialClient implements ClientModInitializer {
private static int countdownTicks = -1;
@Override
public void onInitializeClient() {
ClientCommandRegistrationCallback.EVENT.register((dispatcher, registryAccess) ->
dispatcher.register(ClientCommandManager.literal("assen")
.then(ClientCommandManager.literal("enable")
.executes(context -> enable(context.getSource())))
.then(ClientCommandManager.literal("disable")
.executes(context -> disable(context.getSource())))
.then(ClientCommandManager.literal("info")
.executes(context -> info(context.getSource())))
.executes(context -> {
context.getSource().sendFeedback(Text.literal("Usage: /assen <enable|disable|info>"));
return 1;
})));
ClientTickEvents.END_CLIENT_TICK.register(ApsEssentialClient::tickCountdown);
}
private static int enable(FabricClientCommandSource source) {
MinecraftClient client = source.getClient();
IntegratedServer server = client.getServer();
if (server == null) {
source.sendError(Text.literal("ApsEssential can only host a singleplayer world."));
return 0;
}
if (server.isRemote()) {
source.sendError(Text.literal("LAN hosting is already online on port " + server.getServerPort() + "."));
return 0;
}
if (countdownTicks >= 0) {
source.sendError(Text.literal("The hosting countdown is already running."));
return 0;
}
countdownTicks = 100;
showCountdown(client, 5);
source.sendFeedback(Text.literal("ApsEssential hosting countdown started."));
return 1;
}
private static int disable(FabricClientCommandSource source) {
MinecraftClient client = source.getClient();
IntegratedServer server = client.getServer();
if (countdownTicks >= 0) {
countdownTicks = -1;
client.inGameHud.clearTitle();
source.sendFeedback(Text.literal("Hosting countdown cancelled."));
return 1;
}
if (server == null || !server.isRemote()) {
source.sendError(Text.literal("LAN hosting is not online."));
return 0;
}
if (!closeLanListener(server)) {
source.sendError(Text.literal("Could not safely close the LAN listener; the world was left running."));
return 0;
}
source.sendFeedback(Text.literal("LAN hosting stopped. Your singleplayer world is still running."));
return 1;
}
private static int info(FabricClientCommandSource source) {
MinecraftClient client = source.getClient();
IntegratedServer server = client.getServer();
if (server == null) {
source.sendFeedback(Text.literal("Status: not in a singleplayer world."));
} else if (countdownTicks >= 0) {
source.sendFeedback(Text.literal("Status: hosting countdown in progress."));
} else if (server.isRemote()) {
sendLanStatus(client, server.getServerPort());
} else {
source.sendFeedback(Text.literal("Status: world is local only; LAN hosting is offline."));
}
return 1;
}
private static void tickCountdown(MinecraftClient client) {
if (countdownTicks < 0) {
return;
}
if (client.getServer() == null || client.player == null) {
countdownTicks = -1;
client.inGameHud.clearTitle();
return;
}
countdownTicks--;
if (countdownTicks > 0 && countdownTicks % 20 == 0) {
showCountdown(client, countdownTicks / 20);
} else if (countdownTicks == 0) {
countdownTicks = -1;
startLanHosting(client);
}
}
private static void showCountdown(MinecraftClient client, int seconds) {
client.inGameHud.setTitle(Text.literal("Your World Is Being Turned Into a Server In..."));
client.inGameHud.setSubtitle(Text.literal(Integer.toString(seconds)));
client.inGameHud.setTitleTicks(0, 22, 0);
}
private static void startLanHosting(MinecraftClient client) {
IntegratedServer server = client.getServer();
if (server == null || client.player == null) {
return;
}
var gameMode = client.interactionManager == null ? null : client.interactionManager.getCurrentGameMode();
boolean cheatsAllowed = server.getPlayerManager().areCheatsAllowed();
int port = ThreadLocalRandom.current().nextInt(1024, 65536);
if (!server.openToLan(gameMode, cheatsAllowed, port)) {
client.player.sendMessage(Text.literal("ApsEssential could not open a LAN port. Try enabling hosting again."), false);
return;
}
sendLanStatus(client, server.getServerPort());
client.player.sendMessage(Text.literal("No public relay is configured. This listener is reachable only where your network allows it."), false);
}
private static void sendLanStatus(MinecraftClient client, int port) {
if (client.player == null) {
return;
}
Text copyPort = Text.literal("LAN port: " + port + " (click to copy)")
.formatted(Formatting.GREEN)
.styled(style -> style.withClickEvent(new ClickEvent.CopyToClipboard(Integer.toString(port))));
client.player.sendMessage(Text.literal("ApsEssential status: ONLINE"), false);
client.player.sendMessage(copyPort, false);
client.player.sendMessage(Text.literal("This is a LAN port, not a public Internet address."), false);
}
private static boolean closeLanListener(IntegratedServer server) {
int port = server.getServerPort();
ServerNetworkIo networkIo = server.getNetworkIo();
List<ChannelFuture> channels = ((ServerNetworkIoAccessor) networkIo).apsessential$getChannels();
boolean listenerClosed = false;
synchronized (channels) {
Iterator<ChannelFuture> iterator = channels.iterator();
while (iterator.hasNext()) {
ChannelFuture future = iterator.next();
SocketAddress localAddress = future.channel().localAddress();
if (localAddress instanceof InetSocketAddress address && address.getPort() == port) {
iterator.remove();
future.channel().close();
listenerClosed = true;
break;
}
}
}
if (!listenerClosed) {
return false;
}
IntegratedServerAccessor accessor = (IntegratedServerAccessor) server;
LanServerPinger pinger = accessor.apsessential$getLanPinger();
if (pinger != null) {
pinger.interrupt();
accessor.apsessential$setLanPinger(null);
}
accessor.apsessential$setLanPort(-1);
return true;
}
}