How to create custom events?

@boformer HubJoinEvent is just a example. I want this event to trigger when a player joins the server. When a player joins the server, an announcement will be made to all the players. The player who triggered the event will also recieve a custom welcome message such as:

===
Welcome to the Blockenton City Server

– Type /rules for rules
– Use the compass to select a game

…Why do you need a custom event for that?

Just Subscribe to PlayerJoinEvent.

4 Likes

This. It’s extremely pointless to make custom events unless you actually have a valid reason to make them. In this case this isn’t a valid reason and it would be much easier to just used the internal events.

1 Like

Like I said, HubJoinEvent is complete useless and pointless, because I could just @Subscribe to PlayerJoinEvent. But if I were to create hub jumppads, and create a HubJumppadEvent, how do I run the event when a player enters a jumppad?

For future reference, ask for help on what you really want.

Now, assuming that the jump-pad is, say, a gold block.
(Excuse my in-browser pseudo code)

@Subscribe
public void onPlayerMove(PlayerMoveEvent event) {
    Location loc = event.getPlayer().getLocation();
    Block block = event.getPlayer().getWorld().getBlock(loc.getX(), loc.getY() - 1, loc.getZ());
    if (block == BlockType.GOLD_BLOCK) {
        game.getEventManager().post(new HubJumppadEvent(event.getPlayer()); // Plus, any other values this event may need.
    }
}

@FerusGrim, I looked at your code snippet and saw the line
game.getEventManager().post(new HubJumppadEvent(event.getPlayer());

I did not understand that line of code. What is game? Is it a reference?

Oh yeah, the SpongeDocs are okay, but I want to become more advanced at the Sponge Api. I think the SpongeDocs are aiming at beginners.

game.method() is the Sponge equivalent of Bukkit.method().
You can get it during the ServerStartingEvent with event.getGame().

Then use https://spongepowered.github.io/SpongeAPI/

It’s easier to, in the class that annotates @Plugin, to do this:

@Inject private Game game;

@Inject will automagically pass Sponge’s Game instance to it, when the Plugin is initialized.

2 Likes

I am currently inspecting JavaDocs. The amount of inheritance the confusing, but I will manage. Just a question - What does the @Inject, @Subscribe annotations even do? The SpongeDocs doesn’t seem the purpose of @Inject.

@Inject injects for example a Logger or in this case a Game automatically at the right time for you. I only know these both usecases.
@Subscribe is described somewhere in the docs.

@frogocomics @RandomByte

@Inject is used for quite a few injections. :smile:
Logger, PluginContainer, File (ConfigDir), File (DefaultConfig), ServiceManager, etc.

EDIT: @Subscribe just tells Sponge which events you’re going to be listening to when you register the class as an Event handler. Note, that the class which utilizes @Plugin is automatically registered as such.

EDIT2: If you’re familiar with Bukkit’s @EventHandler, you’ll realize that @Subscribe is a fairly similar concept.

@FerusGrim, the code you suggested,


@Subscribe
public void onPlayerMove(PlayerMoveEvent event) {
Location loc = event.getPlayer().getLocation();
Block block = event.getPlayer().getWorld().getBlock(loc.getX(), loc.getY() - 1, loc.getZ());
if (block == BlockType.GOLD_BLOCK) {
game.getEventManager().post(new HubJumppadEvent(event.getPlayer()); // Plus, any other values this event may need.
}
}

Does not work!

Well, why don’t you fix it, @frogocomics?

2 Likes

@boformer I don’t know how to! Don’t be mean!

We could spoon-feed (give you the code) you, but you won’t learn anything.

Study the code. Study java.

3 Likes

C’mon man. Really.

EDIT: The majority of this post has been you blaming others for your lack of knowledge in the platform you’re attempting to work within.

Take @DotDash’s advice and

1 Like

Study hard, one must. Learn not you will should you ignore this. [/yoda] :trollface:

4 Likes

(post withdrawn by author, will be automatically deleted in 24 hours unless flagged)

1 Like

If you’re not good at something, you practice. And in this case its called studying.

2 Likes