Skip to content

Commit

Permalink
MCEvent#call now return their specific type
Browse files Browse the repository at this point in the history
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
  • Loading branch information
xDec0de committed Jul 7, 2024
1 parent e2f79fc commit 0406baf
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -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.
*
Expand All @@ -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;
}
}
9 changes: 7 additions & 2 deletions src/main/java/net/codersky/mcutils/events/MCEvent.java
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
}

0 comments on commit 0406baf

Please sign in to comment.