Giter Club home page Giter Club logo

festivalkit's Introduction

asd FestivalKit

Build status

FestivalKit is a small and simple C# eventing library aimed to be familiar to Bukkit developers. This can also be useful in a general purpose plugin architecture where unrelated components need to step on each other in a somewhat orderly fashion. The library is available under MIT License.

Install

FestivalKit is available on NuGet.

Install-Package FestivalKit.dll

Usage

First let's declare an event type.

public class SomethingEvent : IEvent
{
  public string Text { get; set; }
}

Then declare a listener for that type.

A Listener class can have multiple EventHandler methods.

public class SomethingListener : IListener
{
  [EventHandler]
  public void OnSomething(SomethingEvent e)
  {
    Console.WriteLine(e.Text);
  }
}

Finally use a dispatcher to register your listener and dispatch your events.

You usually only need a single dispatcher in your application.

var dispatcher = new EventDispatcher();
dispatcher.Register(listener);

dispatcher.Dispatch(new SomethingEvent() {Text = "Hello World!"});

EventOrder

You can specify an order to a listener method. The order is between 1 and 7, the default is 3. The highest order is executed last.

public class SomethingListener : IListener
{
	[EventHandler(EventOrder.Second)]
	public void OnSomething(SomethingEvent e)
	{
		Console.WriteLine($"This is written first: {e.Text}");
	}
	
	[EventHandler(EventOrder.Fifth)]
	public void OnSomething2(SomethingEvent e)
	{
		Console.WriteLine($"This is written second: {e.Text}");
	}
}

Event Cancellation

Some events within your application may be cancellable by its listeners. For example if an event is fired before broadcasting a chat message in an IRC server, a word filter listener might want to cancel it. In turn an user whitelist listener might permit the message if it is from an admin user and un-cancel it.

To use this facility, simply derive your event class from CancellableEvent.

public class ChatEvent : CancellableEvent
{
	public string Username { get; set; }
	public string Message { get; set; }
}

Then listeners can toggle the Cancelled boolean property of your event.

public class WhitelistListener : IListener
{
	[EventHandler(EventOrder.Sixth)]
	public void OnChat(ChatEvent e)
	{
		if (e.Username == "Minikloon")
			e.Cancelled = false;
	}
}

You can also use EventDispatcher's Dispatch overload for CancellableEvent with this nifty pattern.

var e = new ChatEvent() {Username = "Minikloon", Message = "Yo!"};
if (dispatcher.Dispatch(e))
{
	Server.Broadcast($"{e.Username}: {e.Message}");
}

festivalkit's People

Stargazers

RedPlayer_1 avatar Luis avatar Martín avatar Jono Kemball avatar Ward Boumans avatar KarikuDev avatar  avatar Alex Gurrola avatar Samuel avatar

Watchers

James Cloos avatar Samuel avatar

Forkers

karikudev

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.