split server

This commit is contained in:
Sen 2025-05-03 18:01:17 +02:00
parent b0dc368ef7
commit 3ab017888b
25 changed files with 458 additions and 482 deletions

View file

@ -1,7 +1,10 @@
package game;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.util.ArrayDeque;
import java.util.ArrayList;
@ -64,6 +67,7 @@ import game.packet.SPacketTimeUpdate;
import game.packet.SPacketWorld;
import game.potion.PotionEffect;
import game.util.ExtMath;
import game.util.Tuple;
import game.util.Util;
import game.world.BlockPos;
import game.world.PortalType;
@ -108,11 +112,9 @@ public final class Server implements IThreadListener {
private final CommandEnvironment scriptEnv = new CommandEnvironment(this);
private final boolean debug;
private final boolean ipcpipe;
private final String token;
private WorldServer space;
private ChannelFuture endpoint;
private String localUser;
private boolean running = true;
private boolean stopped;
@ -137,24 +139,23 @@ public final class Server implements IThreadListener {
public static void main(String[] args) {
Util.checkOs();
Registry.setup("Server thread");
boolean debug = Boolean.parseBoolean(System.getProperty("server.debug", "false"));
boolean ipc = debug || Boolean.parseBoolean(System.getProperty("server.pipe", "false"));;
String token = System.getProperty("server.token", null);
boolean debug = System.getProperty("server.debug", null) != null;
boolean ipc = debug || System.getProperty("server.pipe", null) != null;
int port = Integer.parseInt(System.getProperty("server.port", "" + Config.PORT));
int distance = Integer.parseInt(System.getProperty("server.distance", "" + 0));
final Server server = new Server(debug, ipc, token);
final Server server = new Server(debug, ipc);
Registry.addShutdownHook(new Runnable() {
public void run() {
server.stopServer();
}
});
server.run(port, distance);
server.run(port);
Region.killIO();
Log.flushLog();
}
private Server(boolean debug, boolean ipc, String token) {
private Server(boolean debug, boolean ipc) {
this.debug = debug;
this.ipcpipe = ipc;
this.token = token;
}
public CommandEnvironment getScriptEnvironment() {
@ -177,13 +178,9 @@ public final class Server implements IThreadListener {
return list;
}
public void setLocalUser(String user) {
this.localUser = user;
}
public void saveWorldInfo() {
if(!this.debug) {
Region.saveWorldInfo(null, this.space.getDayTime(), this.localUser);
Region.saveWorldInfo(null, this.space.getDayTime());
WorldServer.saveWarps(this.warps);
}
}
@ -254,14 +251,12 @@ public final class Server implements IThreadListener {
}
}
public void run(int port, int distance) {
public void run(int port) {
long time = System.currentTimeMillis();
Log.JNI.info("Starte Server Version " + Config.VERSION);
if(!this.debug) {
this.setMessage("Welt wird erstellt und geladen");
FolderInfo info = Region.loadWorldInfo(null);
if(distance > 0)
Config.set("viewDistance", "" + distance, null);
// if(dtime == -1L) // {
// dtime = World.START_TIME;
//// Config.set("spawnDim", "1", null);
@ -300,8 +295,6 @@ public final class Server implements IThreadListener {
Config.set("weatherChanges", "false", null);
Config.set("mobSpawning", "false", null);
Config.set("spawnRadius", "0", null);
if(distance > 0)
Config.set("viewDistance", "" + distance, null);
this.worlds.add(this.space = new WorldServer(this, World.START_TIME,
Space.INSTANCE, true));
this.dimensions.put(this.space.dimension.getDimensionId(), this.space);
@ -317,6 +310,32 @@ public final class Server implements IThreadListener {
if(port >= 0)
this.bind(port);
}
Thread con = new Thread(new Runnable() {
private final BufferedReader reader = new BufferedReader(new InputStreamReader(new BufferedInputStream(System.in)));
public void run() {
while(true) {
String line;
try {
line = this.reader.readLine();
}
catch(IOException e) {
line = null;
}
if(line == null)
break;
if(line.startsWith("#")) {
line = line.substring(1);
Tuple<String, String> data = Util.getKeyValue(line);
if(data.first.equals("end"))
Server.this.shutdown();
continue;
}
}
}
}, "Server console handler");
con.setDaemon(true);
con.start();
Log.JNI.info("Server gestartet in " + String.format("%.1f", (double)(System.currentTimeMillis() - time) / 1000.0) + " Sekunden");
while(this.running) {
this.currentTime = System.nanoTime() / 1000L;
@ -365,12 +384,18 @@ public final class Server implements IThreadListener {
}
}
public void preload(WorldServer world, int bx, int bz) {
public void preload(WorldServer world, int bx, int bz, Player callback) {
int done = 0;
int total = Config.distance * 2 + 1;
total *= total;
this.setMessage("Landschaft wird generiert");
this.startProgress(false, total);
if(callback != null) {
callback.displayLoading("Lade Welt ...");
callback.sendTask("Landschaft wird generiert", total);
}
else {
this.setMessage("Landschaft wird generiert");
this.startProgress(false, total);
}
// WorldServer world = this.getWorld(Config.spawnDim);
// world = world == null ? this.space : world;
bx = bx >> 4;
@ -380,17 +405,25 @@ public final class Server implements IThreadListener {
for(int z = -Config.distance; z <= Config.distance; z++) {
long time = System.currentTimeMillis();
if(time - last >= 10L) {
this.setProgress(done);
if(time - last > 1000L) {
Log.JNI.info("Bereite Spawnbereich vor" + ": " + (done * 100 / total) + "%");
last = time;
if(callback != null) {
callback.sendProgress(done);
}
else {
this.setProgress(done);
if(time - last > 1000L) {
Log.JNI.info("Bereite Spawnbereich vor" + ": " + (done * 100 / total) + "%");
last = time;
}
}
}
++done;
world.loadChunk(bx + x, bz + z);
}
}
this.setProgress(-1);
if(callback != null)
callback.sendProgress(-1);
else
this.setProgress(-1);
}
// public void resetProgress() {
@ -476,6 +509,7 @@ public final class Server implements IThreadListener {
if(this.perfTimer == 100) {
this.perfTimer = 0;
}
this.sendPipeIPC("tick", this.getLastTick());
}
public WorldServer getSpace() {
@ -642,7 +676,7 @@ public final class Server implements IThreadListener {
conn.readFromNBT(tag);
if(Config.playerLimit > 0 && this.players.size() >= Config.playerLimit && !conn.isAdmin())
return String.format("Der Server ist voll (%d/%d)!", this.players.size(), Config.playerLimit);
if(!connection.isLocalChannel() && Config.auth) {
if(/* !connection.isLocalChannel() && */ Config.auth) {
if(conn.getPassword() == null) {
if(!Config.register)
return "Anmeldung neuer Accounts ist auf diesem Server deaktiviert (Whitelisted)";
@ -688,8 +722,8 @@ public final class Server implements IThreadListener {
+ player.getId() + " auf Level " + world.dimension.getDimensionId() + ": "
+ String.format("%.2f %.2f %.2f", player.posX, player.posY, player.posZ) + " verbunden (" + (tag == null ? "Charakter-Editor" : "'" + player.getCommandName() + "'") + ")");
if(Config.preloadLocal && conn.isLocal() && this.players.size() == 1)
this.preload(world, (int)player.posX, (int)player.posZ);
if(Config.preload && /* conn.isLocal() && */ this.players.size() == 1)
this.preload(world, (int)player.posX, (int)player.posZ, conn);
conn.sendPacket(new SPacketJoinGame(player.getId(), world.dimension, EntityRegistry.getEntityID(player), tag == null));
conn.sendPacket(new SPacketHeldItemChange(player.inventory.currentItem));
@ -1040,8 +1074,7 @@ public final class Server implements IThreadListener {
private void unsetLanEndpoint() {
for(Player conn : Lists.newArrayList(this.players)) {
if(!conn.isLocal())
conn.disconnect();
conn.disconnect();
}
this.terminateEndpoint();
}
@ -1137,15 +1170,15 @@ public final class Server implements IThreadListener {
}
public void shutdown() {
Futures.getUnchecked(this.schedule(new Runnable() {
public void run() {
for(Player conn : Lists.newArrayList(Server.this.players)) { // = Server.this.getPlayer(Server.this.owner);
// if(conn != null)
if(conn.isLocal())
Server.this.removePlayer(conn);
}
}
}));
// Futures.getUnchecked(this.schedule(new Runnable() {
// public void run() {
// for(Player conn : Lists.newArrayList(Server.this.players)) { // = Server.this.getPlayer(Server.this.owner);
// // if(conn != null)
// if(conn.isLocal())
// Server.this.removePlayer(conn);
// }
// }
// }));
this.running = false;
}
@ -1155,7 +1188,9 @@ public final class Server implements IThreadListener {
}
private void sendPipeIPC(String key, Object value) {
System.out.println("#" + key + (value == null ? "" : " " + String.valueOf(value)));
System.out.flush();
if(this.ipcpipe) {
System.out.println("#" + key + (value == null ? "" : " " + String.valueOf(value)));
System.out.flush();
}
}
}