Jump to content

Event registration checks for correct bus


TheGreyGhost

Recommended Posts

Hi

 

Perhaps it would be helpful to new modders if the Event handler registration would check that you have registered your event handler to the correct bus.  It is rather easy to get them confused (or be unaware that eg the FML has a different bus) and all you can tell is that your event is not called.

 

-TGG

Link to comment
Share on other sites

Perhaps something like

 

public class MinecraftForge
{
    /**
     * The core Forge EventBusses, all events for Forge will be fired on these,
     * you should use this to register all your listeners.
     * This replaces every register*Handler() function in the old version of Forge.
     * TERRAIN_GEN_BUS for terrain gen events
     * ORE_GEN_BUS for ore gen events
     * EVENT_BUS for everything else
     */
    public static final EventBus EVENT_BUS = new EventBus("EVENT_BUS");
    public static final EventBus TERRAIN_GEN_BUS = new EventBus("TERRAIN_GEN_BUS");
    public static final EventBus ORE_GEN_BUS = new EventBus("ORE_GEN_BUS");

 

and

 

public class EventBus
{
    private static int maxID = 0;
    
    private ConcurrentHashMap<Object, ArrayList<IEventListener>> listeners = new ConcurrentHashMap<Object, ArrayList<IEventListener>>();
    private final int busID = maxID++;
    private String busName;

    public EventBus(String init_busName)
    {
        busName = init_busName;
        ListenerList.resize(busID + 1);
    }
    
// ...

     private void register(Class<?> eventType, Object target, Method method)
    {
        try
        {
            Constructor<?> ctr = eventType.getConstructor();
            ctr.setAccessible(true);
            Event event = (Event)ctr.newInstance();
            
            if (! event.whichBus().equals(busName)) {
                            throw new IllegalArgumentException(
                                "Method " + method + " has @ForgeSubscribe annotation, but is being registered on the wrong bus " +
                                "(" + busName + " instead of " + event.whichBus() + ")"
                            );
            }  
// ... etc ... 
       }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }

 

and

 

public class Event
{

//... 
    private static ListenerList listeners = new ListenerList();
    
    public String whichBus() {return "";};
// ...
}

public class BlockEvent extends Event {
//...
    @Override
    public String whichBus() {return "EVENT_BUS";};
//...
}

Link to comment
Share on other sites

But what if people want to fire our events on different busses?

Short answer is no, you just need to register your shit on the correct bus. It's REALLY easy to tell if you use the correct bus. Does your event fire when it should? No? Wrong bus.

I do Forge for free, however the servers to run it arn't free, so anything is appreciated.
Consider supporting the team on Patreon

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.