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