From 0406bafc87fa07dd1c402912ec0cd815fb3023b6 Mon Sep 17 00:00:00 2001 From: xDec0de_ Date: Sun, 7 Jul 2024 22:36:32 +0200 Subject: [PATCH] MCEvent#call now return their specific type This is mostly done so on cancellable events you can just do a one-liner such as event.call().isCancelled(), which makes the call method actually useful instead of being just a shortcut --- .../mcutils/events/CancellableMCEvent.java | 23 ++++++++++++++++++- .../net/codersky/mcutils/events/MCEvent.java | 9 ++++++-- .../player/CancellableMCPlayerEvent.java | 22 ++++++++++++++++++ .../mcutils/events/player/MCPlayerEvent.java | 20 ++++++++++++++++ 4 files changed, 71 insertions(+), 3 deletions(-) diff --git a/src/main/java/net/codersky/mcutils/events/CancellableMCEvent.java b/src/main/java/net/codersky/mcutils/events/CancellableMCEvent.java index 7325c6e..48497e9 100644 --- a/src/main/java/net/codersky/mcutils/events/CancellableMCEvent.java +++ b/src/main/java/net/codersky/mcutils/events/CancellableMCEvent.java @@ -1,10 +1,16 @@ package net.codersky.mcutils.events; +import net.codersky.mcutils.events.player.MCPlayerEvent; +import org.bukkit.Bukkit; import org.bukkit.event.Cancellable; +import org.bukkit.event.Event; +import org.bukkit.plugin.PluginManager; + +import javax.annotation.Nonnull; /** * A simple type of {@link MCEvent} that - * implements the {@link Cancellable} interface + * implements the {@link Cancellable} interface, * so you don't have to manually do it, see * {@link MCEvent} for more details. * @@ -25,4 +31,19 @@ public boolean isCancelled() { public void setCancelled(boolean cancel) { this.cancelled = cancel; } + + /** + * Calls this {@link CancellableMCEvent}, a shortcut to + * {@link PluginManager#callEvent(Event)} that returns this + * {@link CancellableMCEvent} to be used further instead of {@code void}. + * + * @return This {@link CancellableMCEvent}. + * + * @since MCUtils 1.0.0 + */ + @Nonnull + public CancellableMCEvent call() { + Bukkit.getPluginManager().callEvent(this); + return this; + } } diff --git a/src/main/java/net/codersky/mcutils/events/MCEvent.java b/src/main/java/net/codersky/mcutils/events/MCEvent.java index ea35e34..17c6742 100644 --- a/src/main/java/net/codersky/mcutils/events/MCEvent.java +++ b/src/main/java/net/codersky/mcutils/events/MCEvent.java @@ -1,9 +1,12 @@ package net.codersky.mcutils.events; +import net.codersky.mcutils.events.player.MCPlayerEvent; import org.bukkit.Bukkit; import org.bukkit.event.Event; import org.bukkit.plugin.PluginManager; +import javax.annotation.Nonnull; + /** * A very simple {@link Event} class that provides a {@link #call()} * method to to create and call an {@link Event} in just one line. @@ -67,11 +70,13 @@ public MCEvent(boolean isAsync) { } /** - * Calls this {@link MCEvent}, a shortcut of - * {@link PluginManager#callEvent(Event)}. + * Calls this {@link MCEvent}, a shortcut to + * {@link PluginManager#callEvent(Event)} that returns this + * {@link MCEvent} to be used further instead of {@code void}. * * @return This {@link MCEvent}. */ + @Nonnull public MCEvent call() { Bukkit.getPluginManager().callEvent(this); return this; diff --git a/src/main/java/net/codersky/mcutils/events/player/CancellableMCPlayerEvent.java b/src/main/java/net/codersky/mcutils/events/player/CancellableMCPlayerEvent.java index 70af03b..bd8578b 100644 --- a/src/main/java/net/codersky/mcutils/events/player/CancellableMCPlayerEvent.java +++ b/src/main/java/net/codersky/mcutils/events/player/CancellableMCPlayerEvent.java @@ -1,9 +1,15 @@ package net.codersky.mcutils.events.player; +import net.codersky.mcutils.events.CancellableMCEvent; +import org.bukkit.Bukkit; import org.bukkit.entity.Player; import org.bukkit.event.Cancellable; import net.codersky.mcutils.events.MCEvent; +import org.bukkit.event.Event; +import org.bukkit.plugin.PluginManager; + +import javax.annotation.Nonnull; /** * A simple type of {@link MCPlayerEvent} that @@ -41,4 +47,20 @@ public boolean isCancelled() { public void setCancelled(boolean cancel) { this.cancelled = cancel; } + + /** + * Calls this {@link CancellableMCPlayerEvent}, a shortcut to + * {@link PluginManager#callEvent(Event)} that returns this + * {@link CancellableMCPlayerEvent} to be used further instead of {@code void}. + * + * @return This {@link CancellableMCPlayerEvent}. + * + * @since MCUtils 1.0.0 + */ + @Nonnull + @Override + public CancellableMCPlayerEvent call() { + Bukkit.getPluginManager().callEvent(this); + return this; + } } diff --git a/src/main/java/net/codersky/mcutils/events/player/MCPlayerEvent.java b/src/main/java/net/codersky/mcutils/events/player/MCPlayerEvent.java index 3a9b14f..d977b05 100644 --- a/src/main/java/net/codersky/mcutils/events/player/MCPlayerEvent.java +++ b/src/main/java/net/codersky/mcutils/events/player/MCPlayerEvent.java @@ -4,9 +4,13 @@ import javax.annotation.Nonnull; +import net.codersky.mcutils.events.CancellableMCEvent; +import org.bukkit.Bukkit; import org.bukkit.entity.Player; import net.codersky.mcutils.events.MCEvent; +import org.bukkit.event.Event; +import org.bukkit.plugin.PluginManager; /** * A simple type of {@link MCEvent} that @@ -62,4 +66,20 @@ public MCPlayerEvent(Player who, boolean async) { public Player getPlayer() { return player; } + + /** + * Calls this {@link MCPlayerEvent}, a shortcut to + * {@link PluginManager#callEvent(Event)} that returns this + * {@link MCPlayerEvent} to be used further instead of {@code void}. + * + * @return This {@link MCPlayerEvent}. + * + * @since MCUtils 1.0.0 + */ + @Nonnull + @Override + public MCPlayerEvent call() { + Bukkit.getPluginManager().callEvent(this); + return this; + } }