Skip to content

Initializing & Registering a Listener

iso2013 edited this page Apr 25, 2018 · 1 revision

Initializing

Due to the way PacketEntityAPI supports shading, you must call a static initializer function in your #onEnable to get an instance. The method accepts a plugin object and a function to run once the API is complete. This function is where you should register listeners and store a copy of the API for later use. For example:

public void onEnable() {
    PacketEntityAPI.initialize(this, api -> api.addListener(new ExampleListener()));
}

Registering a Listener

To register a listener, use the IPacketEntityAPI#addListener(IListener) method. Pass in an instance of your class which overrides the default interface implementations

Here is an example listener:

public class ExampleListener implements IListener {
    @Override
    public ListenerPriority getPriority() { return ListenerPriority.NORMAL; }

    @Override
    public void onEvent(IEntityPacketEvent e) {
        System.out.println("A packet event was fired!");
    }

    @Override
    public EntityType[] getTargets() { return new EntityType[]{ EntityType.SHEEP }; }
}

For more information about the overrideable methods, click here.

If you would like to fire the listener for fake entities (entities which do not actually exist server-side), then override IListener#shouldFireForFake.

If you would like to fire the listener for click events on custom entity hitboxes (note: special cases exist. See Javadocs for IFakeEntity#setHitbox for more information.), then override IListener#requiresCollidable.

HomeJavadocs


Getting Started

Obtaining an API Instance


Fake Entities:

Hitboxes:

  • Creating Hitboxes
  • Setting a Hitbox
  • Hitbox Limitations

Listeners:

  • Registering a Listener

Modifiers:

  • Obtaining Modifiers
  • Modifying an Entity

Packets:

  • Creating Packets
  • Sending Packets
  • Queueing Packets

Clone this wiki locally