Skip to content

HeathLoganCampbell/NiftyEvents

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 

Repository files navigation

NiftyEvents

A small event management api in java, this supports event driven code :)

Include

We just use JitPack

Maven

    <repositories>
        <repository>
            <id>jitpack.io</id>
            <url>https://jitpack.io</url>
        </repository>
    </repositories>

    <dependencies>
        <dependency>
            <groupId>com.github.HeathLoganCampbell</groupId>
            <artifactId>NiftyEvents</artifactId>
            <version>74179c6261</version>
        </dependency>
    </dependencies>

Example

Lets say we are a large food chain that offers fish and chips, We have multiple stores all over the country. So all the stores want easy access to their stores infomation and others, so they can tell what is selling well but some stores only want to track some things.

if we didn't use event driven code in this case, everything can quickly get out of hand as everyone adds in there line of code

But now, any store can just add a function with @EventListener on it and their code will be triggered

public class ShopListener
{
    @EventListener
    public void onPurchase(PurchaseEvent event)
    {
        System.out.println("PurchaseEvent] Shop Name: " + event.getShopName());
    }

    @EventListener
    public void onShop(ShopEvent event)
    {
        System.out.println("ShopEvent] Shop Name: " + event.getShopName());
    }
}
public class Main
{
    public static void main(String[] args) {
        System.out.println("Setting up...");
        EventManager eventManager = new EventManager(); //this is the hub for our events, everything passes through this
        eventManager.registerEventListeners(new ShopListener());//register a lister (only required once)
        eventManager.triggerEvents(new PurchaseEvent("Heath's Fish and Chips", "2 Chips", 1.00));
    }
}
//Create your own event
//Just extend Event, and boom you're done
import games.bevs.niftyevents.Event;

public class ShopEvent extends Event
{
    private String shopName;

    public ShopEvent(String shopName)
    {
        this.shopName = shopName;
    }

    public String getShopName()
    {
        return shopName;
    }

    public void setShopName(String shopName)
    {
        this.shopName = shopName;
    }
}
//you can even extend your own events to build ontop of them
public class PurchaseEvent extends ShopEvent
{
    private String itemName;
    private double amount;

    public PurchaseEvent(String shopName, String itemName, double amount)
    {
        super(shopName);
        this.itemName = itemName;
        this.amount = amount;
    }

    public String getItemName()
    {
        return itemName;
    }

    public void setItemName(String itemName)
    {
        this.itemName = itemName;
    }

    public double getAmount()
    {
        return amount;
    }

    public void setAmount(double amount)
    {
        this.amount = amount;
    }
}

Output

Setting up...
PurchaseEvent] Shop Name: Heath's Fish and Chips
ShopEvent] Shop Name: Heath's Fish and Chips

CheatSheet

@EventListener - on the top of your function with the only  paramter being the event you want to be the trigger
EventManager.registerEventListeners - Register all the functions in the class with the @EventListener annotion
EventManager.triggerEvents - call all listener classes that are registered to that event

About

💻A small silly event driven api in java

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages