Skip to content

Commit

Permalink
Events
Browse files Browse the repository at this point in the history
  • Loading branch information
Mark7625 committed Oct 11, 2023
1 parent 8a03231 commit e3e4134
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 11 deletions.
4 changes: 3 additions & 1 deletion ElvargServer/game/src/main/java/com/elvarg/Server.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
import com.elvarg.net.NetworkBuilder;
import com.elvarg.net.NetworkConstants;
import com.elvarg.plugin.event.EventManager;
import com.elvarg.plugin.event.ServerBootEvent;
import com.elvarg.plugin.event.impl.ServerBootEvent;
import com.elvarg.plugin.event.impl.ServerStartedEvent;
import com.elvarg.util.ShutdownHook;
import com.elvarg.util.flood.Flooder;

Expand Down Expand Up @@ -61,6 +62,7 @@ public static void main(String[] args) {
EventManager.INSTANCE.postAndWait(new ServerBootEvent());
new NetworkBuilder().initialize(NetworkConstants.GAME_PORT);
logger.info(GameConstants.NAME + " is now online!");
EventManager.INSTANCE.post(new ServerStartedEvent());
} catch (Exception e) {
logger.log(Level.SEVERE, "An error occurred while binding the Bootstrap!", e);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.elvarg.util;

import com.elvarg.game.World;
import com.elvarg.plugin.event.EventManager;
import com.elvarg.plugin.event.impl.ServerStoppedEvent;

import java.util.logging.Logger;

Expand All @@ -13,6 +15,7 @@ public class ShutdownHook extends Thread {

@Override
public void run() {
EventManager.INSTANCE.post(new ServerStoppedEvent());
logger.info("The shutdown hook is processing all required actions...");
World.savePlayers();
logger.info("The shudown hook actions have been completed, shutting the server down...");
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.elvarg.plugin.event.impl

import com.elvarg.plugin.event.Event
import org.joda.time.DateTime

/*
* Called when the server is started but this waits for all events to be finshed before moving on
*/

class ServerBootEvent(val time : DateTime = DateTime.now()) : Event
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.elvarg.plugin.event.impl

import com.elvarg.plugin.event.Event
import org.joda.time.DateTime


/*
* Called when the server is started
*/

class ServerStartedEvent (val time : DateTime = DateTime.now(),val pid : Long = ProcessHandle.current().pid()) : Event
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.elvarg.plugin.event.impl

import com.elvarg.plugin.event.Event
import org.joda.time.DateTime



/*
* Called when the server is stopped by an update or a command
*/
class ServerStoppedEvent(val time : DateTime = DateTime.now()) : Event
14 changes: 10 additions & 4 deletions ElvargServer/plugin/src/main/kotlin/TestEvents.plugin.kts
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
package com.elvarg.plugin.test

import com.elvarg.plugin.event.EventListener.Companion.on
import com.elvarg.plugin.event.ServerBootEvent
import com.elvarg.plugin.event.impl.ServerBootEvent
import com.elvarg.plugin.event.impl.ServerStartedEvent
import com.elvarg.plugin.event.impl.ServerStoppedEvent

on<ServerBootEvent> {
then { Beep.hey() }
on<ServerStartedEvent> {
then { println("PID: $pid") }
}

on<ServerStoppedEvent> {

then { println("Server Stopped") }
}

0 comments on commit e3e4134

Please sign in to comment.