diff --git a/EssentialsSpawn/src/main/java/com/earth2me/essentials/spawn/Commandspawn.java b/EssentialsSpawn/src/main/java/com/earth2me/essentials/spawn/Commandspawn.java index 02781551d..9150f7964 100644 --- a/EssentialsSpawn/src/main/java/com/earth2me/essentials/spawn/Commandspawn.java +++ b/EssentialsSpawn/src/main/java/com/earth2me/essentials/spawn/Commandspawn.java @@ -7,6 +7,7 @@ import com.earth2me.essentials.User; import com.earth2me.essentials.commands.EssentialsCommand; import com.earth2me.essentials.commands.NoChargeException; import com.earth2me.essentials.commands.NotEnoughArgumentsException; +import net.essentialsx.api.v2.events.UserTeleportSpawnEvent; import org.bukkit.Location; import org.bukkit.Server; import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause; @@ -38,7 +39,7 @@ public class Commandspawn extends EssentialsCommand { }); respawn(user.getSource(), user, otherUser, charge, commandLabel, future); } else { - respawn(user.getSource(), user, user, charge, commandLabel, new CompletableFuture<>()); + respawn(user.getSource(), null, user, charge, commandLabel, new CompletableFuture<>()); } throw new NoChargeException(); @@ -77,6 +78,11 @@ public class Commandspawn extends EssentialsCommand { showError(sender.getSender(), e, commandLabel); return false; }); + final UserTeleportSpawnEvent spawnEvent = new UserTeleportSpawnEvent(teleportee, teleportOwner, teleportee.getGroup(), spawn); + ess.getServer().getPluginManager().callEvent(spawnEvent); + if (spawnEvent.isCancelled()) { + return; + } if (teleportOwner == null) { teleportee.getAsyncTeleport().now(spawn, false, TeleportCause.COMMAND, future); return; diff --git a/EssentialsSpawn/src/main/java/net/essentialsx/api/v2/events/UserTeleportSpawnEvent.java b/EssentialsSpawn/src/main/java/net/essentialsx/api/v2/events/UserTeleportSpawnEvent.java new file mode 100644 index 000000000..9505b40e3 --- /dev/null +++ b/EssentialsSpawn/src/main/java/net/essentialsx/api/v2/events/UserTeleportSpawnEvent.java @@ -0,0 +1,76 @@ +package net.essentialsx.api.v2.events; + +import net.ess3.api.IUser; +import org.bukkit.Location; +import org.bukkit.event.Cancellable; +import org.bukkit.event.Event; +import org.bukkit.event.HandlerList; + +/** + * Called before a user is teleported to spawn via the /spawn command. + */ +public class UserTeleportSpawnEvent extends Event implements Cancellable { + private static final HandlerList handlers = new HandlerList(); + + private final IUser user; + private final IUser controller; + private final String spawnGroup; + private final Location target; + private boolean cancelled = false; + + public UserTeleportSpawnEvent(final IUser user, final IUser controller, final String spawnGroup, final Location target) { + this.user = user; + this.controller = controller; + this.spawnGroup = spawnGroup; + this.target = target; + } + + /** + * @return The user who is being teleported to spawn. + */ + public IUser getUser() { + return user; + } + + /** + * @return The user who caused teleport to spawn or null if there is none + */ + public IUser getController() { + return controller; + } + + /** + * The {@link #getUser() user's} group used to determine their spawn location. + * @return The user's group. + */ + public String getSpawnGroup() { + return spawnGroup; + } + + /** + * The spawn location of the {@link #getUser() user's} {@link #getSpawnGroup() group}. + * @return The spawn location of the user's group. + */ + public Location getSpawnLocation() { + return target; + } + + @Override + public boolean isCancelled() { + return cancelled; + } + + @Override + public void setCancelled(final boolean cancelled) { + this.cancelled = cancelled; + } + + @Override + public HandlerList getHandlers() { + return handlers; + } + + public static HandlerList getHandlerList() { + return handlers; + } +}