Acara Khusus

You can write your own event classes and dispatch those events using the method described above. An event class must extend the Event class. Depending on the exact nature of the event, more interfaces should be implemented, like Cancellable for events that can be cancelled by a listener or interfaces like AffectEntityEvent clarifying what sort of object is affected by your event.

Tip

You can extend AbstractEvent for common methods to be implementated for you

Contoh: adat kelas acara peristiwa

The following class describes an event indicating a ServerPlayer has come in contact with FLARD and is now about to mutate in a way specified by the event. Since the event can be cancelled by listeners, it implements the Cancellable interface.

Karena umumnya acara khusus dimaksudkan untuk didengarkan oleh plugin lain, sangat penting untuk mendokumentasikannya dengan tepat. Ini termasuk daftar objek yang biasanya ditemukan di :javadoc:`Penyebab`. Dalam contoh di bawah ini, mungkin akan disebutkan bahwa akar masalahnya pada umumnya adalah objek kelas SumberFLARD kelas.

import org.spongepowered.api.entity.living.player.server.ServerPlayer;
import org.spongepowered.api.event.Cancellable;
import org.spongepowered.api.event.Cause;
import org.spongepowered.api.event.impl.AbstractEvent;

public class PlayerMutationEvent extends AbstractEvent implements Cancellable {

    public enum Mutation {
        COMPULSIVE_POETRY,
        ROTTED_SOCKS,
        SPONTANEOUS_COMBUSTION;
    };

    private final Cause cause;
    private final ServerPlayer victim;
    private final Mutation mutation;
    private boolean cancelled = false;

    public PlayerMutationEvent(ServerPlayer victim, Mutation mutation, Cause cause) {
        this.victim = victim;
        this.mutation = mutation;
        this.cause = cause;
    }

    public ServerPlayer victim() {
        return this.victim;
    }

    public Mutation mutation() {
        return this.mutation;
    }

    @Override
    public boolean isCancelled() {
        return this.cancelled;
    }

    @Override
    public void setCancelled(boolean cancel) {
        this.cancelled = cancel;
    }

    @Override
    public Cause cause() {
        return this.cause;
    }
}

Contoh: Acara Khusus Kebakaran

import org.spongepowered.api.event.Cause;
import org.spongepowered.api.event.EventContext;
import org.spongepowered.api.event.EventContextKeys;
import org.spongepowered.api.Sponge;

PluginContainer plugin = ...;
EventContext eventContext = EventContext.builder().add(EventContextKeys.PLUGIN, plugin).build();

PlayerMutationEvent event = new PlayerMutationEvent(victim, PlayerMutationEvent.Mutation.ROTTED_SOCKS,
        Cause.of(eventContext, plugin));
Sponge.eventManager().post(event);
if (!event.isCancelled()) {
    // Mutation code
}

Ingatlah bahwa Anda perlu menyediakan penyebab yang tidak kosong. Jika acara Anda `` Bisa dicegah``, pastikan tidak dibatalkan sebelum melakukan tindakan yang dijelaskan oleh acara.

Contoh: Dengarkan Peristiwa Khusus

import net.kyori.adventure.text.Component;
import org.spongepowered.api.event.Listener;

@Listener
public void onPrivateMessage(PlayerMutationEvent event) {
    if (event.mutation() == PlayerMutationEvent.Mutation.SPONTANEOUS_COMBUSTION) {
        event.setCancelled(true);
        event.victim().sendMessage(Component.text("You cannot combust here, this is a non-smoking area!"));
    }
}