Skip to content

Commit

Permalink
stop integrated server if connecting to remote
Browse files Browse the repository at this point in the history
  • Loading branch information
noskill committed Nov 13, 2024
1 parent 5afe919 commit 042772b
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 8 deletions.
12 changes: 9 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -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


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
13 changes: 9 additions & 4 deletions src/main/java/io/singularitynet/Server/ServerStateMachine.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -77,6 +79,7 @@ public class ServerStateMachine extends StateMachine implements IVereyaMessageLi
private ArrayList<String> userConnectionWatchList = new ArrayList<String>();
private ArrayList<String> userTurnSchedule = new ArrayList<String>();
public Map<String, MobEntity> controllableEntities = new HashMap();
protected Set<MobEntity> mobsInGame;

/** Called to initialise a state machine for a specific Mission request.<br>
* Most likely caused by the client creating an integrated server.
Expand All @@ -93,6 +96,7 @@ public ServerStateMachine(ServerState initialState, MissionInit minit, Minecraft
LOGGER.debug("ServerStateMachine: " + this + " server " + server);

this.server = new WeakReference<MinecraftServer>(server);
this.mobsInGame = new HashSet<MobEntity>();
// 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);
Expand All @@ -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);
}
}

Expand All @@ -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)
{
Expand All @@ -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:
Expand Down Expand Up @@ -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));
}

Expand Down Expand Up @@ -1032,7 +1038,6 @@ private void initialiseEnderInventory(PlayerEntity player, AgentStart.EnderBoxIn
}
}


//---------------------------------------------------------------------------------------------------------
/** Mission running state.
*/
Expand Down

0 comments on commit 042772b

Please sign in to comment.