Advertisement
Stiepen

Example Session Plugin - Kibibyte

Mar 10th, 2013
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.98 KB | None | 0 0
  1. // this code might or might not work as it is and is just a concept
  2.  
  3. class TimerPlugin implements IPlugin {
  4.  
  5.     Timer timer;
  6.     BotAccess botAccess;
  7.  
  8.     @Override
  9.     public void onLoad(BotAccess botAccess) {
  10.         this.botAccess = botAccess;
  11.         botAccess.commandManager.registerHandler(Commands.class)
  12.         timer = new Timer(
  13.             botAccess.config.get("ssh", "sessionTimeout", 60000 /* 1 minute by default */, "The Timeout for a session").getInt(),
  14.             botAccess.eventBus);
  15.         botAccess.eventBus.register(this);
  16.     }
  17.  
  18.     @Subscribe
  19.     public void onRehash(RehashEvent event) {
  20.         timer.stop(); // make sure to stop timer first
  21.         timer = new Timer(
  22.             botAccess.config.get("ssh", "sessionTimeout", 60000 /* 1 minute by default */, "The Timeout for a session").getInt(),
  23.             botAccess.eventBus);
  24.     }
  25.  
  26.     @Subscribe
  27.     public void onSessionExpiery(TimerEvent event) {
  28.         if (event.timer != timer) return;
  29.         // Code to destroy session here
  30.     }
  31.  
  32.     @Subscribe
  33.     public void onTimerInterrupt(TimerInterruptedEvent event) {
  34.         if (event.timer != timer) return;
  35.         botAccess.eventBus.postLocal(new TimerEvent(event.timer)); //postLocal() makes it not dispatch events to other eventBusses
  36.     }
  37.  
  38.     private class Commands extends CommandsHandler {
  39.         @Command(name = "mcc", usage = "", help = "" /*[OPTIONAL], permission = "cmd.ssh.mcc" */)
  40.         public void mcc() {
  41.             timer.run(); // starts the timer, or if its already running resets the time
  42.         }
  43.  
  44.         @Command(name = "destroysession", usage = "", help = "destroys a session")
  45.         public void killsession() {
  46.             timer.stop();
  47.         }
  48.     }
  49.  
  50.     @Override
  51.     public void onUnload() {
  52.         // No need to stop timer here because the plugin will always be disabled before unloading it
  53.     }
  54.  
  55.     @Subscribe
  56.     public void onDisable(DisableEvent event) {
  57.         timer.stop();
  58.     }
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement