How to create custom events?

Hi, I am programming Sponge plugins for my server, and I need to know how to create a custom event. I’ve read the SpongeDocs, but it didn’t tell me much. I plan on making a

HubJoinEvent

And I do not want to extend the AbstractEvent class. I would be similar to PlayerJoinEvent, but I don’t know how to work that out. I want the event to have custom methods such as setWelcomeMessage(Message msg), but I am not too good at some Java concepts. Could someone please guide me? The Sponge documentation is terrible for now. I learned hardly any concepts from it, and I want to learn more about Sponge. I have trouble reading JavaDocs and I need someone’s help.

System.out.print(“Thanks, \nJeff”) //Says thank you message

1 Like

In a nutshell, Subscribe to a relevant listener.

When the terms are met from that listener that would constitute HubJoinEvent, dispatch the new event (grabbing the information you need) to this custom event.

The docs explain it pretty well, actually.

2 Likes

Well that’s rude. I’d consider it a bit more of a problem with you since, apparently, you also:

A lot of hard work has gone into getting those docs up and running, and they look damn good, imo.

4 Likes

I’m confused by your question what are you trying to accomplish with a custom event? Why do you need one? What exactly is a

This is not really all the descriptive…

If your having issues with java concepts then I would suggest you check out these links to help you out with basic java concepts

  1. Java Tutorials by Oracle
  2. Derek Banas’s Java Tutorials
  3. Google

My suggestion would be to learn those java concepts instead of blaming the documentation(which is damn good IMHO even if it is incomplete). They tell you how to make the custom events. Also I can’t get over this and I know I asked it before but… What are you trying to do? I know you trying to make a HubJoinEvent are you defining a Hub region that player will go into? Are trying to send a message to the player when they join a hub server? If it’s a hub server you could just use the player join event.

2 Likes

I suggest you to implement AbstractEvent. There is no reason for not doing it.
To create a custom event, you need a class that implements the Event interface.

//Event without extending AbstractEvent (not recommended)
public class HubJoinEvent implements Event {

    // This is a 1:1 copy of AbstractEvent
    private final CallbackList callbacks = new CallbackList();

    @Override
    public CallbackList getCallbacks() {
        return this.callbacks;
    }
    // End of AbstractEvent content

    private final Player player;
    private String welcomeMessage;

    //Constructor
    public HubJoinEvent(Player player, String welcomeMessage) {
        this.player = player; 
        this.welcomeMessage = welcomeMessage;
    }

   public Player getPlayer() {
        return this.player;
   }

   public String getWelcomeMessage() {
        return this.welcomeMessage;
   }

   public void setWelcomeMessage(String welcomeMessage) {
        this.welcomeMessage = welcomeMessage;
   }
}

Or this one:

//Event extending AbstractEvent (recommended)
public class HubJoinEvent extends AbstractEvent {

    private final Player player;
    private String welcomeMessage;

    //Constructor
    public HubJoinEvent(Player player, String welcomeMessage) {
        this.player = player; 
        this.welcomeMessage = welcomeMessage;
    }

   public Player getPlayer() {
        return this.player;
   }

   public String getWelcomeMessage() {
        return this.welcomeMessage;
   }

   public void setWelcomeMessage(String welcomeMessage) {
        this.welcomeMessage = welcomeMessage;
   }
}

Now you can create an instance of the event and post it using the event manager:

game.getEventManager().post(new HubJoinEvent(myPlayer, "Hey!"));

That’s it. To subscribe to this event, use

@Subscribe
public void onHubJoin(HubJoinEvent event) { ... }

Wrong. He wants to “post” a custom event using the event manager.

Custom events are useful if you have multiple “modules” in your plugin, or when you have multiple plugins interacting with each other.

5 Likes

https://docs.spongepowered.org/en/plugins/event-bus.html#creating-custom-events

1 Like

If you don’t ‘post’ this custom event from a listener which meets the new event’s conditions, how does the server know when to fire it? Magic?

Your conditions can easily fall outside the event system, and be related to IO :slight_smile:

1 Like

“By why do you need one?” I was asking about about his usecase :stuck_out_tongue_winking_eye: There are plenty of reasons to make one. I just don’t have the faintest idea as to why he needs/wants one :alien: <----alien.

-________- <-----Whale

That’s also entirely true. ;D

However, “HubJoinEvent” doesn’t sound like it would be one of those.

I made a couple of custom events for my plot plugin:

  • PlotClaimEvent
  • PlotPermissionChangeEvent
  • PlotAbandonEvent
  • PlotRegenerationEvent

A permission plugin can also make use of custom events.

There are many use cases.

And those are valid I’m not saying they arn’t. What I was asking was

Im not trying to say there aren’t good reasons hell I use a PatternInteractionEvent to tell when a pattern has been activated. But I have no idea what he is trying to accomplish, to me it sounds like he’s trying to replicate the PlayerJoinEvent… But that’s me. Maybe I could have worded it better in quote

Thank you for your help, but there is one problem, how do I am HubJoinEvent happen when a player joins. Note that HubJoinEvent is just a example. To create this event, show I do this:

public interface HubJoinEvent extends PlayerJoinEvent {
//Do stuff
}

Or:

public class HubJoinEvent implements PlayerJoinEvent {
//Do stuff
}

I just want this event to happen on startup. Is there a way to do this while extending AbstractEvent? Thank you. The SpongeDocs are okay, but I want to go deeper into Sponge programming.

This is a preview of my current code. Reply to me or email changes to my email, [email protected].


package com.blockenton.server.plugins.hub.events;

import org.spongepowered.api.Game;
import org.spongepowered.api.entity.player.Player;
import org.spongepowered.api.event.AbstractEvent;
import org.spongepowered.api.event.entity.living.player.PlayerJoinEvent;
import org.spongepowered.api.text.message.Message;

public class HubJoinEvent extends AbstractEvent implements PlayerJoinEvent {    

private Player player;

public Message msg;

public HubJoinEvent(Player player, Message msg) {
    this.player = player;
    this.msg = msg;
}

public Player getPlayer() {
    return this.player;
}

public Message getWelcomeMessage() {
    return this.msg;
}

public void setWelcomeMessage(Message msg) {
    this.msg = msg;
}

@Override
public Message getJoinMessage() {
    return this.msg;
}

@Override
public void setJoinMessage(Message joinMessage) {
    this.msg = joinMessage;
}

@Override
public Player getHuman() {
    throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

@Override
public Player getLiving() {
    throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

@Override
public Player getEntity() {
    throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

@Override
public Game getGame() {
    throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

}

I’m pretty sure you put:

game.getEventManager().post(new HubJoinEvent(myPlayer, "Hey!"));

Inside PlayerJoinEvent where you want it to fire.

1 Like

nononono xD

1 Like

Take the tags [code]all that stuff above[/code] you might find it useful.

I just use `some code`

some code

Or [code]```java
public class SomeCode extends Code {
private final List stringList = new ArrayList():
}

```java
public class SomeCode extends Code {
    private final List<String> stringList = new ArrayList<String>():
}

Could some of you guys suggest some changes to my code?

package com.blockenton.server.plugins.hub.events;
   
    import org.spongepowered.api.Game;
    import org.spongepowered.api.entity.player.Player;
    import org.spongepowered.api.event.AbstractEvent;
    import org.spongepowered.api.event.entity.living.player.PlayerJoinEvent;
    import org.spongepowered.api.text.message.Message;

    public class HubJoinEvent extends AbstractEvent implements PlayerJoinEvent {    
    
    private Player player;
    
    public Message msg;
    
    public HubJoinEvent(Player player, Message msg) {
        this.player = player;
        this.msg = msg;
    }
    
    public Player getPlayer() {
        return this.player;
    }
    
    public Message getWelcomeMessage() {
        return this.msg;
    }
    
    public void setWelcomeMessage(Message msg) {
        this.msg = msg;
    }

    @Override
    public Message getJoinMessage() {
        return this.msg;
    }

    @Override
    public void setJoinMessage(Message joinMessage) {
        this.msg = joinMessage;
    }

    @Override
    public Player getHuman() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public Player getLiving() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public Player getEntity() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public Game getGame() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }
}

We can not help you if you don’t describe the usage of this event.

Why do you want to implement a PlayerJoinEvent? That might cause problems because other plugins listening for a PlayerJoinEvent will be called. When you post the event when a player logs in, these plugins will be called twice.

Also, the join message (“xy joined the game”) and the welcome message (“Welcome, xy!”) should be 2 different variables, e.g. joinMsg and welcomeMsg

2 Likes