Skip to content

Event Listener

Kronnox edited this page Jun 5, 2018 · 6 revisions

Creating an EventHandler

To declare a method as an EventHandler the method need the [EventHandler] attribute. The method must contain the respective event as a parameter and cannot have any other parameters besides that. Valid events are the existing Asphalt events or other custom events. Example method:

[EventHandler]
public void OnPlayerMessage(PlayerSendMessageEvent evt)
{
    ...
}

Event Priorities

[EventHandler(EventPriority.Normal)]

You can set the priority of your event to Lowest, Low, Normal, High, Highest and Monitor. Events are called from Lowest to Monitor, so that a mod with higher priority can still change the actions of the mods with lower priority. The Monitor priority should only be used for logging purposes and shouldn't perform any game changing actions at all. If you don't need your EventHandler called before or after the events of specific other mods, just use the default priority Normal.

Ignore Cancellation

You may want to understand how Cancellable Events work first.

[EventHandler(RunIfCancelled = true)]

If RunIfCancelled is set to true the event still gets called (and the other way around) if another EventHandler cancelled the event beforehand. The default calue is true.

Example listener class

public class TestEventHandlers
{
    [EventHandler(EventPriority.Normal, RunIfEventCancelled = false)]   //default values
    public void OnPlayerMessage(PlayerSendMessageEvent evt)
    {
        Console.WriteLine(evt.Message.Text);
    }

    [EventHandler(EventPriority.High)]
    public void OnPlayerMessage2(PlayerSendMessageEvent evt)
    {
        ...
    }
}

Un-/Registering an Event-Handler

To register the created EventHandlers, you need to register the entire class containing the method you want to register.

EventManager.RegisterListener(new TestEventHandlers());

If the events should be active all the time, it is recommended to register them in the initialization of an IServerPlugin or an AsphaltMod. Unregistering EventHandlers is just as easy. Same as the registration, you have to unregister all methods in the respective class together.

EventManager.UnregisterListener(somePreviouslyRegisteredClass);

Currently available Asphalt events

  • InventoryChangeSelectedSlotEvent
  • InventoryMoveItemEvent
  • PlayerBuyEvent
  • PlayerClaimPropertyEvent
  • PlayerCompleteContractEvent
  • PlayerCraftEvent
  • PlayerEatEvent
  • PlayerGainSkillEvent
  • PlayerGetElectedEvent
  • PlayerHarvestEvent
  • PlayerInteractEvent
  • PlayerLoginEvent
  • PlayerLogoutEvent
  • PlayerPayTaxEvent
  • PlayerPickUpEvent
  • PlayerPlaceEvent
  • PlayerProposeVoteEvent
  • PlayerReceiveGovernmentFundsEvent
  • PlayerRunForElectionEvent
  • PlayerSellEvent
  • PlayerSendMessageEvent
  • PlayerTeleportEvent
  • PlayerUnlearnSkillEvent
  • PlayerVoteEvent
  • RpcInvokeEvent
  • WorldPolluteEvent
  • WorldObjectDestroyedEvent
  • WorldObjectEnabledChangedEvent
  • WorldObjectNameChangedEvent
  • WorldObjectOperatingChangedEvent