Commit 7bd1f7fcb9c104020a50606cd30964e2bca49fca

Authored by Mumfrey
1 parent 3810e896

add ShutdownListener

java/client/com/mumfrey/liteloader/client/api/LiteLoaderCoreAPIClient.java
@@ -125,10 +125,13 @@ public class LiteLoaderCoreAPIClient extends LiteLoaderCoreAPI @@ -125,10 +125,13 @@ public class LiteLoaderCoreAPIClient extends LiteLoaderCoreAPI
125 @Override 125 @Override
126 public List<Observer> getObservers() 126 public List<Observer> getObservers()
127 { 127 {
  128 + ObjectFactory<?, ?> objectFactory = this.getObjectFactory();
  129 +
128 return ImmutableList.<Observer>of 130 return ImmutableList.<Observer>of
129 ( 131 (
130 new ResourceObserver(), 132 new ResourceObserver(),
131 - this.getObjectFactory().getPanelManager() 133 + objectFactory.getPanelManager(),
  134 + objectFactory.getEventBroker()
132 ); 135 );
133 } 136 }
134 137
java/common/com/mumfrey/liteloader/ShutdownListener.java 0 → 100644
  1 +package com.mumfrey.liteloader;
  2 +
  3 +/**
  4 + * Interface for mods that want to receive an event when the game is shutting down due to a user request. They do
  5 + * not receive the callback when the VM is terminating for other reasons, use a regular VM shutdownhook for that.
  6 + *
  7 + * @author Adam Mummery-Smith
  8 + */
  9 +public interface ShutdownListener extends LiteMod
  10 +{
  11 + public abstract void onShutDown();
  12 +}
java/common/com/mumfrey/liteloader/core/LiteLoaderEventBroker.java
@@ -35,8 +35,10 @@ import com.mumfrey.liteloader.ServerCommandProvider; @@ -35,8 +35,10 @@ import com.mumfrey.liteloader.ServerCommandProvider;
35 import com.mumfrey.liteloader.ServerPlayerListener; 35 import com.mumfrey.liteloader.ServerPlayerListener;
36 import com.mumfrey.liteloader.ServerPluginChannelListener; 36 import com.mumfrey.liteloader.ServerPluginChannelListener;
37 import com.mumfrey.liteloader.ServerTickable; 37 import com.mumfrey.liteloader.ServerTickable;
  38 +import com.mumfrey.liteloader.ShutdownListener;
38 import com.mumfrey.liteloader.api.InterfaceProvider; 39 import com.mumfrey.liteloader.api.InterfaceProvider;
39 import com.mumfrey.liteloader.api.Listener; 40 import com.mumfrey.liteloader.api.Listener;
  41 +import com.mumfrey.liteloader.api.ShutdownObserver;
40 import com.mumfrey.liteloader.common.GameEngine; 42 import com.mumfrey.liteloader.common.GameEngine;
41 import com.mumfrey.liteloader.common.LoadingProgress; 43 import com.mumfrey.liteloader.common.LoadingProgress;
42 import com.mumfrey.liteloader.core.event.HandlerList; 44 import com.mumfrey.liteloader.core.event.HandlerList;
@@ -54,7 +56,7 @@ import com.mumfrey.liteloader.util.log.LiteLoaderLogger; @@ -54,7 +56,7 @@ import com.mumfrey.liteloader.util.log.LiteLoaderLogger;
54 * @param <TClient> Type of the client runtime, "Minecraft" on client and null on the server 56 * @param <TClient> Type of the client runtime, "Minecraft" on client and null on the server
55 * @param <TServer> Type of the server runtime, "IntegratedServer" on the client, "MinecraftServer" on the server 57 * @param <TServer> Type of the server runtime, "IntegratedServer" on the client, "MinecraftServer" on the server
56 */ 58 */
57 -public abstract class LiteLoaderEventBroker<TClient, TServer extends MinecraftServer> implements InterfaceProvider 59 +public abstract class LiteLoaderEventBroker<TClient, TServer extends MinecraftServer> implements InterfaceProvider, ShutdownObserver
58 { 60 {
59 /** 61 /**
60 * @author Adam Mummery-Smith 62 * @author Adam Mummery-Smith
@@ -152,6 +154,11 @@ public abstract class LiteLoaderEventBroker&lt;TClient, TServer extends MinecraftSe @@ -152,6 +154,11 @@ public abstract class LiteLoaderEventBroker&lt;TClient, TServer extends MinecraftSe
152 private FastIterable<ServerTickable> serverTickListeners = new HandlerList<ServerTickable>(ServerTickable.class); 154 private FastIterable<ServerTickable> serverTickListeners = new HandlerList<ServerTickable>(ServerTickable.class);
153 155
154 /** 156 /**
  157 + * List of mods which want to be notified when the game is shutting down
  158 + */
  159 + private FastIterable<ShutdownListener> shutdownListeners = new HandlerList<ShutdownListener>(ShutdownListener.class);
  160 +
  161 + /**
155 * ctor 162 * ctor
156 * 163 *
157 * @param loader 164 * @param loader
@@ -210,6 +217,7 @@ public abstract class LiteLoaderEventBroker&lt;TClient, TServer extends MinecraftSe @@ -210,6 +217,7 @@ public abstract class LiteLoaderEventBroker&lt;TClient, TServer extends MinecraftSe
210 delegate.registerInterface(PlayerMoveListener.class); 217 delegate.registerInterface(PlayerMoveListener.class);
211 delegate.registerInterface(CommonPluginChannelListener.class); 218 delegate.registerInterface(CommonPluginChannelListener.class);
212 delegate.registerInterface(ServerTickable.class); 219 delegate.registerInterface(ServerTickable.class);
  220 + delegate.registerInterface(ShutdownListener.class);
213 } 221 }
214 222
215 /** 223 /**
@@ -264,6 +272,14 @@ public abstract class LiteLoaderEventBroker&lt;TClient, TServer extends MinecraftSe @@ -264,6 +272,14 @@ public abstract class LiteLoaderEventBroker&lt;TClient, TServer extends MinecraftSe
264 { 272 {
265 this.serverTickListeners.add(serverTickable); 273 this.serverTickListeners.add(serverTickable);
266 } 274 }
  275 +
  276 + /**
  277 + * @param shutdownListener
  278 + */
  279 + public void addShutdownListener(ShutdownListener shutdownListener)
  280 + {
  281 + this.shutdownListeners.add(shutdownListener);
  282 + }
267 283
268 /** 284 /**
269 * @param instance 285 * @param instance
@@ -510,4 +526,20 @@ public abstract class LiteLoaderEventBroker&lt;TClient, TServer extends MinecraftSe @@ -510,4 +526,20 @@ public abstract class LiteLoaderEventBroker&lt;TClient, TServer extends MinecraftSe
510 this.playerStateList.remove(playerState); 526 this.playerStateList.remove(playerState);
511 } 527 }
512 } 528 }
  529 +
  530 + @Override
  531 + public void onShutDown()
  532 + {
  533 + for (ShutdownListener listener : this.shutdownListeners)
  534 + {
  535 + try
  536 + {
  537 + listener.onShutDown();
  538 + }
  539 + catch (Throwable th)
  540 + {
  541 + th.printStackTrace();
  542 + }
  543 + }
  544 + }
513 } 545 }
java/common/com/mumfrey/liteloader/interfaces/FastIterable.java
@@ -7,7 +7,7 @@ package com.mumfrey.liteloader.interfaces; @@ -7,7 +7,7 @@ package com.mumfrey.liteloader.interfaces;
7 * 7 *
8 * @param <T> 8 * @param <T>
9 */ 9 */
10 -public interface FastIterable<T> 10 +public interface FastIterable<T> extends Iterable<T>
11 { 11 {
12 /** 12 /**
13 * Add an entry to the iterable 13 * Add an entry to the iterable