Eventos Personalizados

Puede escribir sus propias clases de evento y enviar esos eventos utilizando el método descrito arriba. Una clase de evento debe extender la clase AbstractEvent, de esta manera implementando el interfaz de Event. Dependiendo de la naturaleza exacta del evento, más interfaces deberian ser implementadas, como Cancellable para eventro que puedan ser cancelado por un escucha o interfaces como TargetPlayerEvent aclarando que tipo de objeto se ve afectado por su evento.

Ejemplo: Clase de Evento Personalizada

La siguiente clase describe un evento que indica que un “Jugador” ha entrado en contacto con FLARD y que está a punto de mutar en ese momento en la manera que especifica el evento. Como el evento apunta a un jugador y puede ser cancelado por los oyentes, implementa tanto la interfaz TargetPlayerEvent como la ``Cancellable”.

Debido a que generalmente los eventos personalizados están hechos para ser escuchados por otros plug-ins, documentarlos apropiadamente es su mejor apuesta. Esto incluye una lista de objetos típicamente encontrados en Cause. En el ejemplo posterior, probablemente sea mencionado que la causa raíz es generalmente un objeto de la clase ficticia FLARDSource.

import org.spongepowered.api.entity.living.player.Player;
import org.spongepowered.api.event.Cancellable;
import org.spongepowered.api.event.cause.Cause;
import org.spongepowered.api.event.entity.living.humanoid.player.TargetPlayerEvent;
import org.spongepowered.api.event.impl.AbstractEvent;

public class PlayerMutationEvent extends AbstractEvent implements TargetPlayerEvent, Cancellable {

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

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

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

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

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

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

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

    @Override
    public Player getTargetEntity() {
        return this.victim;
    }

}

Ejemplo: Evento de Fuego Personalizado

import org.spongepowered.api.Sponge;

PlayerMutationEvent event = new PlayerMutationEvent(victim, PlayerMutationEvent.Mutation.ROTTED_SOCKS,
        Cause.source(flardSource).build());
Sponge.getEventManager().post(event);
if (!event.isCancelled()) {
    // Mutation code
}

Tenga en mente de que necesita suplir a una causa -no vacía. Si su evento era “Cancelable”, asegúrese de que no haya sido cancelado antes antes de haber ejecutado la acción descripta por el evento.

Ejemplo: Escuchar Evento Personalizado

import org.spongepowered.api.event.Listener;
import org.spongepowered.api.text.Text;

@Listener
public void onPrivateMessage(PlayerMutationEvent event) {
    if(event.getMutation() == PlayerMutationEvent.Mutation.SPONTANEOUS_COMBUSTION) {
        event.setCancelled(true);
        event.getTargetEntity().sendMessage(Text.of("You can not combust here, this is a non-smoking area!"));
    }
}