Skip to content
cyklon73 edited this page Apr 11, 2024 · 2 revisions

JEvent

Example

public class MyEvent extends Event {
	private String name;
	
	public MyEvent(String name) {
		this.name = name;
	}
	
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
}

public class MyListener {

	@EventHandler //marks the Method as EventHandler method
	//the parameter is the Event the handler should listen to
	public void onEvent(MyEvent event) {
		System.out.println("Event name: " + event.getName());
		//change the event data
		event.setName("Edited " + event.getName());
	}

}

public class Main {
	public static void main(String[] args) {
		//get the default Event manager
		EventManager manager = JEvent.getDefaultManager();

		//register the listener
		manager.registerListener(MyListener.class);
		//or
		manager.registerListener(new MyListener());

		//initialize Event
		MyEvent event = new MyEvent("MyEventName");
		
		System.out.println(event.getName()); //"MyEventName"
		
		//call Event
		manager.callEvent(event);
		//or
		event.callEvent(); //only possible on the default manager

		System.out.println(event.getName()); //"Edited MyEventName"
	}
}
Clone this wiki locally