From 042772b782b5146a5dce09cfa567921803464a86 Mon Sep 17 00:00:00 2001 From: Anatoly Belikov Date: Wed, 13 Nov 2024 21:23:20 +0300 Subject: [PATCH] stop integrated server if connecting to remote --- build.gradle | 12 +++++++++--- .../singularitynet/Client/ClientStateMachine.java | 6 +++++- .../singularitynet/Server/ServerStateMachine.java | 13 +++++++++---- 3 files changed, 23 insertions(+), 8 deletions(-) diff --git a/build.gradle b/build.gradle index 59a2949..ff72923 100644 --- a/build.gradle +++ b/build.gradle @@ -1,14 +1,20 @@ plugins { + id 'java' + id 'base' id 'fabric-loom' version '1.6-SNAPSHOT' id 'maven-publish' id("com.github.bjornvester.xjc") version "1.6.0" } -sourceCompatibility = JavaVersion.VERSION_21 -targetCompatibility = JavaVersion.VERSION_21 +java { + sourceCompatibility = JavaVersion.VERSION_21 + targetCompatibility = JavaVersion.VERSION_21 +} version = rootProject.file('VERSION').text.trim() -archivesBaseName = project.archives_base_name + '-fabric_' + project.fabric_version +base { + archivesName = project.archives_base_name + '-fabric_' + project.fabric_version +} group = project.maven_group diff --git a/src/main/java/io/singularitynet/Client/ClientStateMachine.java b/src/main/java/io/singularitynet/Client/ClientStateMachine.java index 27c1713..e0bb333 100755 --- a/src/main/java/io/singularitynet/Client/ClientStateMachine.java +++ b/src/main/java/io/singularitynet/Client/ClientStateMachine.java @@ -1190,6 +1190,10 @@ protected void execute() { if (serverCon != null && serverCon.getAddress() != null && serverCon.getPort() != 0) { LOGGER.debug("server connection info is provided " + serverCon.toString() + " assume world already exists"); + if (MinecraftClient.getInstance().isIntegratedServerRunning()){ + LOGGER.debug("stopping integrated server"); + MinecraftClient.getInstance().getServer().stop(true); + } needsNewWorld = false; worldCurrentlyExists = true; episodeHasCompleted(ClientState.WAITING_FOR_SERVER_READY); @@ -1434,7 +1438,7 @@ protected void execute() throws Exception { boolean isConnectedToRemote = player != null && !MinecraftClient.getInstance().isIntegratedServerRunning(); if (!isConnectedToRemote && serverCon != null && serverCon.getAddress() != null && serverCon.getPort() != 0) { ServerAddress srv = new ServerAddress(serverCon.getAddress(), serverCon.getPort()); - LOGGER.debug("connecting to ", srv); + LOGGER.debug("connecting to " + srv.getAddress() + ":" + srv.getPort()); Screen parentScreen = new GameMenuScreen(true); ServerInfo srvInfo = new ServerInfo("local", srv.getAddress(), ServerInfo.ServerType.LAN); ConnectScreen.connect(parentScreen, MinecraftClient.getInstance(), srv, srvInfo, true, null); diff --git a/src/main/java/io/singularitynet/Server/ServerStateMachine.java b/src/main/java/io/singularitynet/Server/ServerStateMachine.java index df00e71..8efcd40 100644 --- a/src/main/java/io/singularitynet/Server/ServerStateMachine.java +++ b/src/main/java/io/singularitynet/Server/ServerStateMachine.java @@ -27,12 +27,14 @@ import jakarta.xml.bind.JAXBElement; import jakarta.xml.bind.JAXBException; +import net.fabricmc.fabric.mixin.event.lifecycle.*; import net.fabricmc.fabric.api.event.lifecycle.v1.ServerEntityEvents; import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents; import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents; import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking; import net.minecraft.entity.Entity; import net.minecraft.entity.EntityType; +import net.minecraft.entity.LivingEntity; import net.minecraft.entity.mob.MobEntity; import net.minecraft.entity.player.PlayerEntity; @@ -77,6 +79,7 @@ public class ServerStateMachine extends StateMachine implements IVereyaMessageLi private ArrayList userConnectionWatchList = new ArrayList(); private ArrayList userTurnSchedule = new ArrayList(); public Map controllableEntities = new HashMap(); + protected Set mobsInGame; /** Called to initialise a state machine for a specific Mission request.
* Most likely caused by the client creating an integrated server. @@ -93,6 +96,7 @@ public ServerStateMachine(ServerState initialState, MissionInit minit, Minecraft LOGGER.debug("ServerStateMachine: " + this + " server " + server); this.server = new WeakReference(server); + this.mobsInGame = new HashSet(); // Register ourself on the event busses, so we can harness the server tick: ServerTickEvents.END_SERVER_TICK.register(this::onServerTick); ServerLifecycleEvents.SERVER_STOPPING.register(this::onServerStopping); @@ -111,6 +115,7 @@ private void onEntityUnload(Entity entity, ServerWorld serverWorld) { LOGGER.debug("removing controlled mob uuid: " + uuid); controllableEntities.remove(uuid); } + if (this.mobsInGame.contains(entity)) this.mobsInGame.remove(entity); } } @@ -122,12 +127,12 @@ private void onEntityLoad(Entity entity, ServerWorld serverWorld) { controllableEntities.put(uuid, mobEntity); LOGGER.debug("sending new controlled mob message to client uuid: " + uuid); sendToAll(new VereyaMessage(VereyaMessageType.SERVER_CONTROLLED_MOB, uuid)); + } else { + this.mobsInGame.add((MobEntity) entity); } } } - - /** Used to prevent spawning in our world.*/ public ActionResult onGetPotentialSpawns(Entity entity, ServerWorld world) { @@ -144,7 +149,6 @@ public ActionResult onGetPotentialSpawns(Entity entity, ServerWorld world) ServerInitialConditions sic = (ss != null) ? ss.getServerInitialConditions() : null; if (sic != null) allowSpawning = (sic.isAllowSpawning() == Boolean.TRUE); - if (allowSpawning && sic.getAllowedMobs() != null && !sic.getAllowedMobs().isEmpty()) { // Spawning is allowed, but restricted to our list: @@ -285,11 +289,13 @@ private void sendToAll(VereyaMessage msg){ return; } for(ServerPlayerEntity player: server.getPlayerManager().getPlayerList()){ + LOGGER.debug("send " + msg.getMessageType().toString() + " to " + player.getName()); ServerPlayNetworking.send(player, new MessagePayload(msg)); } } private void sendToPlayer(VereyaMessage msg, ServerPlayerEntity player){ + LOGGER.debug("send " + msg.getMessageType().toString() + " to " + player.getName()); ServerPlayNetworking.send(player, new MessagePayload(msg)); } @@ -1032,7 +1038,6 @@ private void initialiseEnderInventory(PlayerEntity player, AgentStart.EnderBoxIn } } - //--------------------------------------------------------------------------------------------------------- /** Mission running state. */