loading screen

This commit is contained in:
Sen 2025-03-16 23:14:24 +01:00
parent 987ed4059e
commit d4f6d3e4f9
6 changed files with 139 additions and 95 deletions

View file

@ -117,7 +117,7 @@ public final class Server implements Runnable, IThreadListener {
private ChannelFuture endpoint;
private ChannelFuture localEndpoint;
private String localUser;
// private String message;
private String message;
private boolean running = true;
private boolean stopped;
@ -138,7 +138,8 @@ public final class Server implements Runnable, IThreadListener {
private int pingTimer;
private int syncTimer;
private int perfTimer;
// private int progress = -1;
private int progress = -1;
private int total = 0;
Server(File dir) {
// this.owner = owner;
@ -215,36 +216,24 @@ public final class Server implements Runnable, IThreadListener {
if(this.debug)
return;
if(message) {
this.setProgress(true, -1, 0);
this.startProgress(true, this.worlds.size());
}
long last = System.currentTimeMillis();
int done = 0;
this.saveWorldInfo();
for(WorldServer world : this.worlds) {
if(message) {
long time = System.currentTimeMillis();
if(time - last >= 10L) {
if(this.setProgress(true, (done * 100) / this.worlds.size(), (int)(time - last))) {
last = time;
}
}
this.setProgress(done);
}
++done;
world.saveAllChunks();
}
this.setProgress(-1);
}
private boolean setProgress(boolean save, int progress, int delta) {
// this.setProgress(progress);
if(progress == -1) {
Log.JNI.info((save ? "Speichere" : "Generiere und lade") + " Level " + (this.baseDir == null ? "." : this.baseDir.getName()));
return true;
}
else if(!save && delta > 1000) {
Log.JNI.info("Bereite Spawnbereich vor" + ": " + progress + "%");
return true;
}
return false;
private void startProgress(boolean save, int amount) {
this.setTotal(amount);
this.setProgress(0);
Log.JNI.info((save ? "Speichere" : "Generiere und lade") + " Level " + (this.baseDir == null ? "." : this.baseDir.getName()));
}
public void unloadWorld(WorldServer world) {
@ -266,7 +255,7 @@ public final class Server implements Runnable, IThreadListener {
long time = System.currentTimeMillis();
Log.JNI.info("Starte Server Version " + Config.VERSION);
if(!this.debug) {
// this.setMessage("Welt wird erstellt");
this.setMessage("Welt wird erstellt und geladen");
FolderInfo info = Region.loadWorldInfo(this.baseDir);
// if(dtime == -1L) // {
// dtime = World.START_TIME;
@ -312,24 +301,6 @@ public final class Server implements Runnable, IThreadListener {
}
this.setTpsTarget(20.0f);
if(!this.debug) {
// int done = 0;
// this.setMessage("Landschaft wird generiert");
// this.setProgress(false, -1, 0);
// WorldServer world = this.getWorld(Config.spawnDim);
// world = world == null ? this.space : world;
// long last = System.currentTimeMillis();
// for(int x = -192; x <= 192 && this.running; x += 16) {
// for(int z = -192; z <= 192 && this.running; z += 16) {
// long time1 = System.currentTimeMillis();
// if(time1 - last >= 10L) {
// if(this.setProgress(false, done * 100 / 625, (int)(time1 - last))) {
// last = time1;
// }
// }
// ++done;
// world.loadChunk(Config.spawnX + x >> 4, Config.spawnZ + z >> 4);
// }
// }
for(Dimension dim : UniverseRegistry.getDimensions()) {
if(WorldServer.needsLoading(this.baseDir, dim)) {
this.getWorld(dim.getDimensionId()).loadForcedChunks();
@ -380,6 +351,34 @@ public final class Server implements Runnable, IThreadListener {
Log.JNI.info("Server wurde beendet");
}
}
private void preload(Entity entity) {
int done = 0;
int total = Config.distance * 2 + 1;
total *= total;
this.setMessage("Landschaft wird generiert");
this.startProgress(false, total);
WorldServer world = this.getWorld(Config.spawnDim);
world = world == null ? this.space : world;
int bx = (int)entity.posX >> 4;
int bz = (int)entity.posZ >> 4;
long last = System.currentTimeMillis();
for(int x = -Config.distance; x <= Config.distance && this.running; x++) {
for(int z = -Config.distance; z <= Config.distance && this.running; 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;
}
}
++done;
world.loadChunk(bx + x, bz + z);
}
}
this.setProgress(-1);
}
private void tick() {
// boolean pause = this.paused;
@ -539,29 +538,41 @@ public final class Server implements Runnable, IThreadListener {
return this.usermap.get(user.toLowerCase());
}
// public String getMessage() {
// synchronized(this) {
// return this.message;
// }
// }
//
// public int getProgress() {
// synchronized(this) {
// return this.progress;
// }
// }
//
// private void setMessage(String message) {
// synchronized(this) {
// this.message = message;
// }
// }
//
// private void setProgress(int progress) {
// synchronized(this) {
// this.progress = progress;
// }
// }
public String getMessage() {
synchronized(this) {
return this.message;
}
}
public int getProgress() {
synchronized(this) {
return this.progress;
}
}
public int getTotal() {
synchronized(this) {
return this.total;
}
}
private void setMessage(String message) {
synchronized(this) {
this.message = message;
}
}
private void setProgress(int progress) {
synchronized(this) {
this.progress = progress;
}
}
private void setTotal(int total) {
synchronized(this) {
this.total = total;
}
}
public void setVar(String cv, String value) {
this.schedule(new Runnable() {
@ -672,6 +683,9 @@ public final class Server implements Runnable, IThreadListener {
+ player.getId() + " auf Level " + world.dimension.getDimensionId() + ": "
+ String.format("%.2f %.2f %.2f", player.posX, player.posY, player.posZ) + " verbunden");
if(Config.preloadAll || (Config.preloadLocal && conn.isLocal()))
this.preload(player);
conn.sendPacket(new SPacketJoinGame(player.getId(), world.dimension, EntityRegistry.getEntityID(player)));
conn.sendPacket(new SPacketHeldItemChange(player.inventory.currentItem));
conn.sendPacket(new SPacketWorld(WorldServer.clampGravity(),
@ -1102,8 +1116,8 @@ public final class Server implements Runnable, IThreadListener {
void stopServer() {
if(!this.stopped) {
// this.setProgress(-1);
// this.setMessage("Stoppe server");
this.setProgress(-1);
this.setMessage("Stoppe server");
Log.JNI.info("Beende Server");
if(this.started)
for(NetHandlerPlayServer conn : Lists.newArrayList(this.players)) {