Add UserTeleportSpawnEvent (#4328)

Co-authored-by: Josh Roy <10731363+JRoy@users.noreply.github.com>
This commit is contained in:
Ansandr 2021-07-07 21:52:49 +03:00 committed by GitHub
parent 2858bd3784
commit 3787b80286
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 83 additions and 1 deletions

View file

@ -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;

View file

@ -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;
}
}