Jump to content
Team Avolition
FoggyLight

Help Creating an Event Manager

Recommended Posts

FoggyLight    0

So I started making my own client a quite while back, but I ended up using a client base. I quit a long time ago, but now I'm back, and I want to make my client from scratch, ground up, that way I can actually understand how my own client works. Problem is, I'm stuck on making an event manager, since all the the tutorials just give you code to copy/paste, with no explanation of what each part of it actually does. (if there's even anyone still on this site) Can someone possibly give me some direction? At the very least, show me how one of the event managers work, so I'm not limited by someone else's code?

Share this post


Link to post
Sirenfal    4040

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
});

 

  • Upvote 2

Share this post


Link to post
FoggyLight    0

Thanks for the helpful reply, I tried to make it work, but I got stuck on the API for subscribing to the Specific modules.
Anyway, my friend who's experienced in making successful hacked clients told me to drop reflection, and, if I'm entirely honest, I don't know why I'd want reflection, either. I'm going for a simpler approach, since the traditional one seems like unnecessary work.

Also, sorry for the late reply, I got caught up in school, haven't worked on my client for a while. I'm back at it, and in a couple of years, you'll see a client that actually has some Hypixel bypasses 😉

In the meantime, I've got a lot of work ahead of me. Wish me luck!

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×