Better Listeners
-Specific listeners for the start and stop of loading -Better(?) layer deletion after exiting a world -Actual server thread shutdown
This commit is contained in:
parent
94db44e443
commit
6521cb5749
@ -28,6 +28,8 @@ import ru.windcorp.progressia.client.graphics.world.LayerWorld;
|
||||
import ru.windcorp.progressia.client.localization.MutableString;
|
||||
import ru.windcorp.progressia.client.localization.MutableStringLocalized;
|
||||
import ru.windcorp.progressia.common.world.WorldData;
|
||||
import ru.windcorp.progressia.server.ChunkManager.ChunksLoadFinishListener;
|
||||
import ru.windcorp.progressia.server.ChunkManager.ChunksLoadStartListener;
|
||||
import ru.windcorp.progressia.server.ServerState;
|
||||
import ru.windcorp.progressia.test.LayerAbout;
|
||||
import ru.windcorp.progressia.test.LayerTestText;
|
||||
@ -38,8 +40,6 @@ public class ClientState {
|
||||
|
||||
private static Client instance;
|
||||
|
||||
private static Collection<Layer> layers;
|
||||
|
||||
private static boolean firstLoad;
|
||||
private static LayerTestText layer;
|
||||
|
||||
@ -67,14 +67,16 @@ public class ClientState {
|
||||
|
||||
setInstance(client);
|
||||
|
||||
ServerState.getInstance().getChunkManager().register(bl -> {
|
||||
if (!bl && firstLoad)
|
||||
ServerState.getInstance().getChunkManager().register((ChunksLoadStartListener)() -> {
|
||||
if (firstLoad)
|
||||
{
|
||||
MutableString t = new MutableStringLocalized("LayerText.Load");
|
||||
layer = new LayerTestText("Text",() -> {t.update(); return t.get();});
|
||||
GUI.addTopLayer(layer);
|
||||
}
|
||||
else if (bl && firstLoad)
|
||||
}});
|
||||
|
||||
ServerState.getInstance().getChunkManager().register((ChunksLoadFinishListener)() -> {
|
||||
if (firstLoad)
|
||||
{
|
||||
GUI.removeLayer(layer);
|
||||
|
||||
@ -85,11 +87,6 @@ public class ClientState {
|
||||
GUI.addTopLayer(layerUI);
|
||||
GUI.addTopLayer(layerAbout);
|
||||
|
||||
layers = new HashSet<Layer>();
|
||||
layers.add(layerWorld);
|
||||
layers.add(layerUI);
|
||||
layers.add(layerAbout);
|
||||
|
||||
firstLoad = false;
|
||||
}
|
||||
});
|
||||
@ -98,12 +95,12 @@ public class ClientState {
|
||||
|
||||
public static void disconnectFromLocalServer()
|
||||
{
|
||||
for (Layer layer : layers)
|
||||
for (Layer layer : GUI.getLayers())
|
||||
{
|
||||
GUI.removeLayer(layer);
|
||||
}
|
||||
|
||||
ServerState.getInstance().getClientManager();
|
||||
//ServerState.getInstance().getClientManager();
|
||||
}
|
||||
|
||||
private ClientState() {
|
||||
|
@ -86,6 +86,22 @@ public class ChunkManager {
|
||||
void handle(boolean starting);
|
||||
}
|
||||
|
||||
@FunctionalInterface
|
||||
private interface ChunksLoadEventListener
|
||||
{
|
||||
void handle();
|
||||
}
|
||||
|
||||
public interface ChunksLoadStartListener extends ChunksLoadEventListener
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public interface ChunksLoadFinishListener extends ChunksLoadEventListener
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
private final Server server;
|
||||
|
||||
private final ChunkSet loaded;
|
||||
@ -94,6 +110,8 @@ public class ChunkManager {
|
||||
private final ChunkSet toUnload = ChunkSets.newHashSet();
|
||||
|
||||
private Collection<ChunksLoadListener> listeners = Collections.synchronizedCollection( new ArrayList<>());
|
||||
private Collection<ChunksLoadStartListener> startListeners = Collections.synchronizedCollection( new ArrayList<>());
|
||||
private Collection<ChunksLoadFinishListener> finishListeners = Collections.synchronizedCollection( new ArrayList<>());
|
||||
|
||||
// TODO replace with a normal Map managed by some sort of PlayerListener,
|
||||
// weak maps are weak
|
||||
@ -119,6 +137,16 @@ public class ChunkManager {
|
||||
listeners.add(cll);
|
||||
}
|
||||
|
||||
public void register(ChunksLoadStartListener clsl)
|
||||
{
|
||||
startListeners.add(clsl);
|
||||
}
|
||||
|
||||
public void register(ChunksLoadFinishListener clfl)
|
||||
{
|
||||
finishListeners.add(clfl);
|
||||
}
|
||||
|
||||
public void unregisterAll()
|
||||
{
|
||||
listeners.clear();
|
||||
@ -153,8 +181,10 @@ public class ChunkManager {
|
||||
if (toUnload.size()!=0 || toLoad.size()!=0)
|
||||
{
|
||||
LogManager.getLogger().info(String.valueOf(toUnload.size())+" "+String.valueOf( toLoad.size()));
|
||||
listeners.forEach(l -> l.handle(false));
|
||||
}
|
||||
listeners.forEach(l -> l.handle(false));
|
||||
startListeners.forEach(sl -> sl.handle());
|
||||
|
||||
toUnload.forEach(this::unloadChunk);
|
||||
toUnload.clear();
|
||||
toLoad.forEach(this::loadChunk);
|
||||
@ -164,6 +194,7 @@ public class ChunkManager {
|
||||
v.processQueues(p);
|
||||
});
|
||||
listeners.forEach(l -> l.handle(true));
|
||||
finishListeners.forEach(fl -> fl.handle());
|
||||
|
||||
}
|
||||
|
||||
|
@ -79,6 +79,7 @@ public class ServerThread implements Runnable {
|
||||
|
||||
public void stop() {
|
||||
try {
|
||||
executor.shutdown();
|
||||
executor.awaitTermination(10, TimeUnit.MINUTES);
|
||||
} catch (InterruptedException e) {
|
||||
LogManager.getLogger().warn("Received interrupt in ServerThread.stop(), aborting wait");
|
||||
|
@ -33,6 +33,8 @@ import ru.windcorp.progressia.client.graphics.gui.menu.MenuLayer;
|
||||
import ru.windcorp.progressia.client.localization.MutableString;
|
||||
import ru.windcorp.progressia.client.localization.MutableStringLocalized;
|
||||
import ru.windcorp.progressia.server.ChunkManager;
|
||||
import ru.windcorp.progressia.server.ChunkManager.ChunksLoadFinishListener;
|
||||
import ru.windcorp.progressia.server.ChunkManager.ChunksLoadListener;
|
||||
import ru.windcorp.progressia.server.Player;
|
||||
import ru.windcorp.progressia.server.ServerState;
|
||||
|
||||
@ -94,22 +96,25 @@ public class LayerButtonTest extends MenuLayer {
|
||||
|
||||
ChunkManager cm = ServerState.getInstance().getChunkManager();
|
||||
alive = true;
|
||||
cm.register(bl -> {
|
||||
if (bl && alive)
|
||||
cm.register((ChunksLoadFinishListener)() -> {
|
||||
if (alive)
|
||||
{
|
||||
GUI.removeLayer(layer);
|
||||
GUI.addTopLayer(new LayerTitle("Title"));
|
||||
//cm.unregisterAll();
|
||||
alive = false;
|
||||
|
||||
ServerState.getInstance().shutdown("Safe Exit");
|
||||
|
||||
ServerState.setInstance(null);
|
||||
}
|
||||
});
|
||||
|
||||
//ClientState.getInstance();
|
||||
ClientState.setInstance(null);
|
||||
ServerState.setInstance(null);
|
||||
//ServerState.getInstance().getChunkManager().unloadAll();
|
||||
|
||||
//ServerState.getInstance().shutdown("Safe Exit");
|
||||
|
||||
}));
|
||||
|
||||
getContent().takeFocus();
|
||||
|
Reference in New Issue
Block a user