Jump to content
Team Avolition


Popular Content

Showing content with the highest reputation since 07/24/19 in all areas

  1. 2 points
    You make an abstract Event class from which all your events descend. You make an EventManager class that has a subscribe method that allows you to specify a method which should be called if an event of a particular type fires, and a call method that calls each of those subscribed event handlers whenever an event occurs. Then you make a class for each event type you need (ex. EntitySpawned, PlayerDied) Create a method to handle each event you care about and use EventManager.subscribe(EntitySpawned, handle_entity_spawned) So something like the following. I don't work in Java anymore, so you'll have to figure out the particular syntax yourself, I'm kinda fuzzy on Java 8 features. I left comments so you have some idea of what to Google. public class Event {} public class PlayerKilled extends Event { // fields, constructor... } // Java 8 function interface - each event handler probably needs an anonymous class? I don't know public interface EventFunction { void call(Event event); } public class EventManager { // a mapping of class types inheriting from Event to a list of function handlers for that event type // ex. PlayerKilled -> call(PlayerKilled event) private HashMap<Class<Event>, ArrayList<EventFunction>> handlers = new HashMap(); public void subscribe(Class<Event> event_type, EventFunction func) { // if this is the first event for this type we need to create the ArrayList they're stored in first if(!handlers.containsKey(event_type)) { handlers.put(event_type, new ArrayList<EventFunction>()); } handlers.get(event_type).add(func); } public void call(Event event) { // call all event handlers using a Java 8 lambda expression (Array.forEach lambda) handlers.get(event.class).forEach(handler -> { handler.call(event); }); } } For any further help consider joining the aVo Discord and asking in the #developers channel (check the thread in the Random section, it's stickied). EDIT: I don't know the exact syntax for the subscribe method, but you want a "SAM type". See these links- https://ocpjava.wordpress.com/2016/04/05/java-8-functional-interfaces-sam/ https://stackoverflow.com/questions/17913409/what-is-a-sam-type-in-java Something like whatever.subscribe(PlayerKilled.class, (event) -> { // do stuff with event });
  2. 1 point
  3. -1 points
    does this mean new reliant is programmed in Kotlin?
This leaderboard is set to New York/GMT-04:00