Skip to content

Commit

Permalink
initial support for connection to remote server
Browse files Browse the repository at this point in the history
  • Loading branch information
noskill committed Nov 1, 2024
1 parent 508f248 commit 3765108
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 22 deletions.
72 changes: 52 additions & 20 deletions src/main/java/io/singularitynet/Client/ClientStateMachine.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,12 @@
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking;
import net.fabricmc.fabric.api.client.rendering.v1.WorldRenderContext;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.screen.GameMenuScreen;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.client.gui.screen.multiplayer.ConnectScreen;
import net.minecraft.client.network.ClientPlayerEntity;
import net.minecraft.client.network.ServerAddress;
import net.minecraft.client.network.ServerInfo;
import net.minecraft.client.world.ClientWorld;
import net.minecraft.entity.Entity;
import net.minecraft.entity.mob.MobEntity;
Expand Down Expand Up @@ -550,6 +555,7 @@ public boolean onCommand(String command, String ipFrom, DataOutputStream dos)
{
LOGGER.info("Received from " + ipFrom + ":" +
command.substring(0, Math.min(command.length(), 1024)));

boolean keepProcessing = false;

// Possible commands:
Expand Down Expand Up @@ -849,6 +855,7 @@ private void checkForMissionCommand() throws Exception
{
missionInit.getClientAgentConnection().setAgentIPAddress(comip.ipAddress);
LOGGER.info("Mission received: " + missionInit.getMission().getAbout().getSummary());
LOGGER.debug(missionMessage);
csMachine.currentMissionInit = missionInit;
ClientStateMachine.this.createMissionControlSocket();
// Move on to next state:
Expand Down Expand Up @@ -1178,6 +1185,16 @@ protected void execute() {

boolean needsNewWorld = worldGenerator != null && worldGenerator.shouldCreateWorld(currentMissionInit(), genOptions);
boolean worldCurrentlyExists = world != null;
MinecraftServerConnection serverCon = currentMissionInit().getMinecraftServerConnection();
LOGGER.debug("checking for server connection in mission init: ", serverCon);
if (serverCon != null && serverCon.getAddress() != null && serverCon.getPort() != 0) {
LOGGER.debug("server connection info is provided " + serverCon.toString() +
" assume world already exists");
needsNewWorld = false;
worldCurrentlyExists = true;
episodeHasCompleted(ClientState.WAITING_FOR_SERVER_READY);
return;
}
List<AgentSection> agents = currentMissionInit().getMission().getAgentSection();
String agentName = agents.get(currentMissionInit().getClientRole()).getName();
if (worldCurrentlyExists) {
Expand All @@ -1203,10 +1220,10 @@ protected void execute() {
LOGGER.debug("needsNewWorld && worldCurrentlyExists");
episodeHasCompleted(ClientState.PAUSING_OLD_SERVER);
} else {
// We want a new world, and there is currently nothing running,
// so jump to world creation:
LOGGER.debug("needsNewWorld && not worldCurrentlyExists");
episodeHasCompleted(ClientState.CREATING_NEW_WORLD);
// We want a new world, and there is currently nothing running,
// so jump to world creation:
LOGGER.debug("needsNewWorld && not worldCurrentlyExists");
episodeHasCompleted(ClientState.CREATING_NEW_WORLD);
}
} else { // not needNewWorld
LOGGER.debug("not need new world");
Expand All @@ -1217,7 +1234,7 @@ protected void execute() {
as.getAgentStart().setPlacement(null);

ClientPlayerEntity player = MinecraftClient.getInstance().player;
if (player.isDead()) player.requestRespawn();
if (player != null && player.isDead()) player.requestRespawn();
/*
if (ClientStateMachine.this.serverHandlers == null) {
// We need to use the server's MissionHandlers here:
Expand All @@ -1234,6 +1251,7 @@ protected void execute() {
// boolean isConnectedToRealm = MinecraftClient.getInstance().isConnectedToRealms();
boolean isConnectedToLocal = MinecraftClient.getInstance().isConnectedToLocalServer();
boolean isIntegratedServerRunning = MinecraftClient.getInstance().isIntegratedServerRunning();
boolean isConnectedToRemote = !isIntegratedServerRunning && player != null;
// LOGGER.debug("isConnectedToRealm: " + isConnectedToRealm);
LOGGER.debug("isConnectedToLocal: " + isConnectedToLocal);
LOGGER.debug("isIntegratedServerRunning: " + isIntegratedServerRunning);
Expand All @@ -1260,18 +1278,7 @@ public void run() {
// Skip all the map loading stuff and go straight to waiting for the server:
episodeHasCompleted(ClientState.WAITING_FOR_SERVER_READY);
} else {
LOGGER.debug("sending mission to remote Server");
HashMap<String, String> map = new HashMap<String, String>();
// convert mission init with jaxb serializer
try {
String xmlData = SchemaHelper.serialiseObject(currentMissionInit(), MissionInit.class);
map.put("MissionInit", xmlData);
} catch (JAXBException e) {
episodeHasCompletedWithErrors(ClientState.ERROR_NO_WORLD, "exception while converting mission init to xml" + e.getMessage());
}
// send mission init to server
ClientPlayNetworking.send(new MessagePayload(new VereyaMessage(VereyaMessageType.CLIENT_MISSION_INIT, 0, map)));
episodeHasCompleted(ClientState.WAITING_FOR_SERVER_READY);
throw new RuntimeException("unexpected condition");
}
} else { // not needNewWorld and no world: error
// Mission has requested no new world, but there is no current world to play in - this is an error:
Expand All @@ -1294,6 +1301,7 @@ public class WaitingForServerEpisode extends ErrorAwareEpisode implements IVerey
boolean waitingForChunk = false;
boolean waitingForPlayer = true;
private boolean chunkReady = false;
private boolean sendToRemote = false;

protected WaitingForServerEpisode(ClientStateMachine machine)
{
Expand Down Expand Up @@ -1414,8 +1422,7 @@ public void onClientTick(MinecraftClient client)
}

@Override
protected void execute() throws Exception
{
protected void execute() throws Exception {
totalTicks = 0;

// Minecraft.getMinecraft().displayGuiScreen(null); // Clear any menu screen that might confuse things.
Expand All @@ -1424,7 +1431,18 @@ protected void execute() throws Exception
//if (agents == null || agents.size() <= currentMissionInit().getClientRole())
// throw new Exception("No agent section for us!"); // TODO
this.agentName = agents.get(currentMissionInit().getClientRole()).getName();
/*
MinecraftServerConnection serverCon = currentMissionInit().getMinecraftServerConnection();
PlayerEntity player = MinecraftClient.getInstance().player;
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);
Screen parentScreen = new GameMenuScreen(true);
ServerInfo srvInfo = new ServerInfo("local", srv.getAddress(), ServerInfo.ServerType.LAN);
ConnectScreen.connect(parentScreen, MinecraftClient.getInstance(), srv, srvInfo, true, null);
this.sendToRemote = true;
}
/*
if (agents.size() > 1 && currentMissionInit().getClientRole() != 0)
{
// Multi-agent mission, we should be joining a server.
Expand All @@ -1451,6 +1469,20 @@ protected void execute() throws Exception

protected void handleLan()
{
if (this.sendToRemote) {
LOGGER.debug("sending mission to remote Server");
HashMap<String, String> map = new HashMap<String, String>();
// convert mission init with jaxb serializer
try {
String xmlData = SchemaHelper.serialiseObject(currentMissionInit(), MissionInit.class);
map.put("MissionInit", xmlData);
} catch (JAXBException e) {
episodeHasCompletedWithErrors(ClientState.ERROR_NO_WORLD, "exception while converting mission init to xml" + e.getMessage());
}
// send mission init to server
ClientPlayNetworking.send(new MessagePayload(new VereyaMessage(VereyaMessageType.CLIENT_MISSION_INIT, 0, map)));
sendToRemote = false;
}
// Get our name from the Mission:
/*List<AgentSection> agents = currentMissionInit().getMission().getAgentSection();
this.agentName = agents.get(currentMissionInit().getClientRole()).getName();
Expand Down
7 changes: 5 additions & 2 deletions src/main/java/io/singularitynet/Server/VereyaModServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,11 @@ public void onInitialize() {
SidesMessageHandler.client2server.onMessage(payload, context);
});
ServerLifecycleEvents.SERVER_STARTED.register((MinecraftServer server) -> {

LOGGER.info("Server started");
String msg = "integrated ";
if (server.isDedicated()){
msg = "dedicated ";
}
LOGGER.info(msg + "Server started");
if (stateMachine == null ) {
this.stateMachine = new ServerStateMachine(ServerState.WAITING_FOR_MOD_READY, null, server);
} else {
Expand Down

0 comments on commit 3765108

Please sign in to comment.