Implement PreTeleportEvent, adjust API events to extend PlayerEvent

This commit is contained in:
William committed 2021-11-23 00:00:41 +00:00
1 parent 914c4fabe1
commit cfa5c45845
16 files changed
+158 -87

No files matched your search

@@ -12,8 +12,9 @@ public class PlayerChangeHomeDescriptionEvent extends PlayerHomeUpdateEvent {
/** /**
* An event, fired when a player changes the description of a home. * An event, fired when a player changes the description of a home.
* @param player The Player who is changing the home's description *
* @param home The Home being changed * @param player The Player who is changing the home's description
* @param home The Home being changed
* @param newDescription The new description being set * @param newDescription The new description being set
* @see PlayerHomeUpdateEvent * @see PlayerHomeUpdateEvent
*/ */
@@ -25,6 +26,7 @@ public class PlayerChangeHomeDescriptionEvent extends PlayerHomeUpdateEvent {
/** /**
* Returns the old description of the home * Returns the old description of the home
*
* @return the old description String * @return the old description String
*/ */
public String getOldDescription() { public String getOldDescription() {
@@ -33,6 +35,7 @@ public class PlayerChangeHomeDescriptionEvent extends PlayerHomeUpdateEvent {
/** /**
* Returns the new description being set to the home * Returns the new description being set to the home
*
* @return the new description String * @return the new description String
*/ */
public String getNewDescription() { public String getNewDescription() {
@@ -12,8 +12,9 @@ public class PlayerChangeWarpDescriptionEvent extends PlayerWarpUpdateEvent {
/** /**
* An event, fired when a player changes the description of a warp. * An event, fired when a player changes the description of a warp.
* @param player The Player who is changing the warp's description *
* @param warp The Warp being changed * @param player The Player who is changing the warp's description
* @param warp The Warp being changed
* @param newDescription The new description being set * @param newDescription The new description being set
* @see PlayerWarpUpdateEvent * @see PlayerWarpUpdateEvent
*/ */
@@ -25,6 +26,7 @@ public class PlayerChangeWarpDescriptionEvent extends PlayerWarpUpdateEvent {
/** /**
* Returns the old description of the home * Returns the old description of the home
*
* @return the old description String * @return the old description String
*/ */
public String getOldDescription() { public String getOldDescription() {
@@ -33,8 +35,10 @@ public class PlayerChangeWarpDescriptionEvent extends PlayerWarpUpdateEvent {
/** /**
* Returns the new description being set to the home * Returns the new description being set to the home
*
* @return the new description String * @return the new description String
*/ */
public String getNewDescription() { public String getNewDescription() {
return newDescription; return newDescription;
}} }
}
@@ -9,8 +9,9 @@ import org.bukkit.entity.Player;
public class PlayerDeleteHomeEvent extends PlayerHomeUpdateEvent { public class PlayerDeleteHomeEvent extends PlayerHomeUpdateEvent {
/** /**
* An event, fired when a player deletes a home * An event, fired when a player deletes a home
*
* @param player The Player who is deleting the home * @param player The Player who is deleting the home
* @param home The Home being deleted * @param home The Home being deleted
* @see PlayerHomeUpdateEvent * @see PlayerHomeUpdateEvent
*/ */
public PlayerDeleteHomeEvent(Player player, Home home) { public PlayerDeleteHomeEvent(Player player, Home home) {
@@ -9,8 +9,9 @@ import org.bukkit.entity.Player;
public class PlayerDeleteWarpEvent extends PlayerWarpUpdateEvent { public class PlayerDeleteWarpEvent extends PlayerWarpUpdateEvent {
/** /**
* An event, fired when a player deletes a warp * An event, fired when a player deletes a warp
*
* @param player The Player who is deleting the warp * @param player The Player who is deleting the warp
* @param warp The Warp being deleted * @param warp The Warp being deleted
* @see PlayerWarpUpdateEvent * @see PlayerWarpUpdateEvent
*/ */
public PlayerDeleteWarpEvent(Player player, Warp warp) { public PlayerDeleteWarpEvent(Player player, Warp warp) {
@@ -5,6 +5,7 @@ import org.bukkit.entity.Player;
import org.bukkit.event.Cancellable; import org.bukkit.event.Cancellable;
import org.bukkit.event.Event; import org.bukkit.event.Event;
import org.bukkit.event.HandlerList; import org.bukkit.event.HandlerList;
import org.bukkit.event.player.PlayerEvent;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
/** /**
@@ -12,8 +13,7 @@ import org.jetbrains.annotations.NotNull;
* Fires when a player renames a Home, updates its' location, changes its' name, changes whether or not it is public/private. * Fires when a player renames a Home, updates its' location, changes its' name, changes whether or not it is public/private.
* Also fires when it is deleted * Also fires when it is deleted
*/ */
public class PlayerHomeUpdateEvent extends Event implements Cancellable { public class PlayerHomeUpdateEvent extends PlayerEvent implements Cancellable {
private final Player player;
private final Home home; private final Home home;
private static final HandlerList HANDLER_LIST = new HandlerList(); private static final HandlerList HANDLER_LIST = new HandlerList();
private boolean isCancelled; private boolean isCancelled;
@@ -27,7 +27,7 @@ public class PlayerHomeUpdateEvent extends Event implements Cancellable {
* @param home The Home being changed * @param home The Home being changed
*/ */
public PlayerHomeUpdateEvent(Player player, Home home) { public PlayerHomeUpdateEvent(Player player, Home home) {
this.player = player; super(player);
this.home = home; this.home = home;
this.isCancelled = false; this.isCancelled = false;
} }
@@ -42,18 +42,9 @@ public class PlayerHomeUpdateEvent extends Event implements Cancellable {
} }
/** /**
* Get the Player involved in this event * Gets the {@link Home} being updated
* *
* @return the Player who updated the home * @return the {@link Home}
*/
public Player getPlayer() {
return player;
}
/**
* Get the Player involved in this event
*
* @return the Home being updated
*/ */
public Home getHome() { public Home getHome() {
return home; return home;
@@ -9,8 +9,9 @@ import org.bukkit.entity.Player;
public class PlayerMakeHomePrivateEvent extends PlayerHomeUpdateEvent { public class PlayerMakeHomePrivateEvent extends PlayerHomeUpdateEvent {
/** /**
* An event, fired when a player makes a home private * An event, fired when a player makes a home private
*
* @param player The Player who is making the Home private * @param player The Player who is making the Home private
* @param home The Home being changed * @param home The Home being changed
* @see PlayerHomeUpdateEvent * @see PlayerHomeUpdateEvent
*/ */
public PlayerMakeHomePrivateEvent(Player player, Home home) { public PlayerMakeHomePrivateEvent(Player player, Home home) {
@@ -9,8 +9,9 @@ import org.bukkit.entity.Player;
public class PlayerMakeHomePublicEvent extends PlayerHomeUpdateEvent { public class PlayerMakeHomePublicEvent extends PlayerHomeUpdateEvent {
/** /**
* An event, fired when a player makes a home public * An event, fired when a player makes a home public
*
* @param player The Player who is making the Home public * @param player The Player who is making the Home public
* @param home The Home being changed * @param home The Home being changed
* @see PlayerHomeUpdateEvent * @see PlayerHomeUpdateEvent
*/ */
public PlayerMakeHomePublicEvent(Player player, Home home) { public PlayerMakeHomePublicEvent(Player player, Home home) {
@@ -0,0 +1,55 @@
package me.william278.huskhomes2.api.events;
import me.william278.huskhomes2.teleport.points.TeleportationPoint;
import org.bukkit.entity.Player;
import org.bukkit.event.Cancellable;
import org.bukkit.event.HandlerList;
import org.bukkit.event.player.PlayerEvent;
import org.jetbrains.annotations.NotNull;
public class PlayerPreTeleportEvent extends PlayerEvent implements Cancellable {
private final TeleportationPoint targetPoint;
private static final HandlerList HANDLER_LIST = new HandlerList();
private boolean isCancelled;
/**
* An event, fired before a player is teleported by HuskHomes for any reason
*
* @param player The Player who is updating the Home
* @param targetPoint The Player's teleport destination
*/
public PlayerPreTeleportEvent(Player player, TeleportationPoint targetPoint) {
super(player);
this.targetPoint = targetPoint;
this.isCancelled = false;
}
@Override
public @NotNull HandlerList getHandlers() {
return HANDLER_LIST;
}
public static HandlerList getHandlerList() {
return HANDLER_LIST;
}
/**
* Get the Player's teleportation destination {@link TeleportationPoint}
*
* @return the {@link TeleportationPoint}
*/
public TeleportationPoint getTargetPoint() {
return targetPoint;
}
@Override
public boolean isCancelled() {
return isCancelled;
}
@Override
public void setCancelled(boolean cancel) {
isCancelled = cancel;
}
}
@@ -13,8 +13,9 @@ public class PlayerRelocateHomeEvent extends PlayerHomeUpdateEvent {
/** /**
* An event, fired when a player relocates a home * An event, fired when a player relocates a home
* @param player The Player who is relocating the Home *
* @param home The Home being moved * @param player The Player who is relocating the Home
* @param home The Home being moved
* @param newTeleportationPoint The new teleportation position of the Home * @param newTeleportationPoint The new teleportation position of the Home
* @see PlayerWarpUpdateEvent * @see PlayerWarpUpdateEvent
*/ */
@@ -26,6 +27,7 @@ public class PlayerRelocateHomeEvent extends PlayerHomeUpdateEvent {
/** /**
* Returns the teleportation position of the Home before it is updated * Returns the teleportation position of the Home before it is updated
*
* @return the pre-update teleportation position * @return the pre-update teleportation position
*/ */
public TeleportationPoint getOldTeleportationPoint() { public TeleportationPoint getOldTeleportationPoint() {
@@ -34,6 +36,7 @@ public class PlayerRelocateHomeEvent extends PlayerHomeUpdateEvent {
/** /**
* Returns the updated teleportation position of the Home * Returns the updated teleportation position of the Home
*
* @return the updated teleportation position * @return the updated teleportation position
*/ */
public TeleportationPoint getNewTeleportationPoint() { public TeleportationPoint getNewTeleportationPoint() {
@@ -13,8 +13,9 @@ public class PlayerRelocateWarpEvent extends PlayerWarpUpdateEvent {
/** /**
* An event, fired when a player relocates a warp * An event, fired when a player relocates a warp
* @param player The Player who is relocating the Warp *
* @param warp The Warp being moved * @param player The Player who is relocating the Warp
* @param warp The Warp being moved
* @param newTeleportationPoint The new teleportation position of the Warp * @param newTeleportationPoint The new teleportation position of the Warp
* @see PlayerWarpUpdateEvent * @see PlayerWarpUpdateEvent
*/ */
@@ -26,6 +27,7 @@ public class PlayerRelocateWarpEvent extends PlayerWarpUpdateEvent {
/** /**
* Returns the teleportation position of the Warp before it is updated * Returns the teleportation position of the Warp before it is updated
*
* @return the pre-update teleportation position * @return the pre-update teleportation position
*/ */
public TeleportationPoint getOldTeleportationPoint() { public TeleportationPoint getOldTeleportationPoint() {
@@ -34,6 +36,7 @@ public class PlayerRelocateWarpEvent extends PlayerWarpUpdateEvent {
/** /**
* Returns the updated teleportation position of the Warp * Returns the updated teleportation position of the Warp
*
* @return the updated teleportation position * @return the updated teleportation position
*/ */
public TeleportationPoint getNewTeleportationPoint() { public TeleportationPoint getNewTeleportationPoint() {
@@ -12,8 +12,9 @@ public class PlayerRenameHomeEvent extends PlayerHomeUpdateEvent {
/** /**
* An event, fired when a player changes the name of a home * An event, fired when a player changes the name of a home
* @param player The Player who is changing the Home's name *
* @param home The Home being changed * @param player The Player who is changing the Home's name
* @param home The Home being changed
* @param newName The new name of the home * @param newName The new name of the home
* @see PlayerHomeUpdateEvent * @see PlayerHomeUpdateEvent
*/ */
@@ -25,6 +26,7 @@ public class PlayerRenameHomeEvent extends PlayerHomeUpdateEvent {
/** /**
* Get the name of the Home pre-update * Get the name of the Home pre-update
*
* @return the name of the Home before it is updated * @return the name of the Home before it is updated
*/ */
public String getOldName() { public String getOldName() {
@@ -33,6 +35,7 @@ public class PlayerRenameHomeEvent extends PlayerHomeUpdateEvent {
/** /**
* Get the changed name of the Home * Get the changed name of the Home
*
* @return the new name of the Home * @return the new name of the Home
*/ */
public String getNewName() { public String getNewName() {
@@ -12,8 +12,9 @@ public class PlayerRenameWarpEvent extends PlayerWarpUpdateEvent {
/** /**
* An event, fired when a player changes the name of a warp * An event, fired when a player changes the name of a warp
* @param player The Player who is changing the Warp's name *
* @param warp The Warp being changed * @param player The Player who is changing the Warp's name
* @param warp The Warp being changed
* @param newName The new name of the warp * @param newName The new name of the warp
* @see PlayerWarpUpdateEvent * @see PlayerWarpUpdateEvent
*/ */
@@ -25,6 +26,7 @@ public class PlayerRenameWarpEvent extends PlayerWarpUpdateEvent {
/** /**
* Get the name of the Warp pre-update * Get the name of the Warp pre-update
*
* @return the name of the Warp before it is updated * @return the name of the Warp before it is updated
*/ */
public String getOldName() { public String getOldName() {
@@ -33,8 +35,10 @@ public class PlayerRenameWarpEvent extends PlayerWarpUpdateEvent {
/** /**
* Get the changed name of the Warp * Get the changed name of the Warp
*
* @return the new name of the Warp * @return the new name of the Warp
*/ */
public String getNewName() { public String getNewName() {
return newName; return newName;
}} }
}
@@ -5,26 +5,26 @@ import org.bukkit.entity.Player;
import org.bukkit.event.Cancellable; import org.bukkit.event.Cancellable;
import org.bukkit.event.Event; import org.bukkit.event.Event;
import org.bukkit.event.HandlerList; import org.bukkit.event.HandlerList;
import org.bukkit.event.player.PlayerEvent;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
/** /**
* An event, fired when a player sets a new home * An event, fired when a player sets a new home
*/ */
public class PlayerSetHomeEvent extends Event implements Cancellable { public class PlayerSetHomeEvent extends PlayerEvent implements Cancellable {
private final Player player;
private final Home home; private final Home home;
private static final HandlerList HANDLER_LIST = new HandlerList(); private static final HandlerList HANDLER_LIST = new HandlerList();
private boolean isCancelled; private boolean isCancelled;
/** /**
* An event, fired when a player sets a Home * An event, fired when a player sets a {@link Home}
* *
* @param player the Player setting the Home * @param player the {@link Player} setting the Home
* @param home the Home being set * @param home the {@link Home} being set
*/ */
public PlayerSetHomeEvent(Player player, Home home) { public PlayerSetHomeEvent(Player player, Home home) {
this.player = player; super(player);
this.home = home; this.home = home;
this.isCancelled = false; this.isCancelled = false;
} }
@@ -38,19 +38,10 @@ public class PlayerSetHomeEvent extends Event implements Cancellable {
return HANDLER_LIST; return HANDLER_LIST;
} }
/**
* Get the player involved in this event
*
* @return the Player who is setting the home
*/
public Player getPlayer() {
return player;
}
/** /**
* Get the Home being set * Get the Home being set
* *
* @return the Home being set * @return the {@link Home}
*/ */
public Home getHome() { public Home getHome() {
return home; return home;
@@ -5,14 +5,14 @@ import org.bukkit.entity.Player;
import org.bukkit.event.Cancellable; import org.bukkit.event.Cancellable;
import org.bukkit.event.Event; import org.bukkit.event.Event;
import org.bukkit.event.HandlerList; import org.bukkit.event.HandlerList;
import org.bukkit.event.player.PlayerEvent;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
/** /**
* An event, fired when a player sets a new warp * An event, fired when a player sets a new warp
*/ */
public class PlayerSetWarpEvent extends Event implements Cancellable { public class PlayerSetWarpEvent extends PlayerEvent implements Cancellable {
private final Player player;
private final Warp warp; private final Warp warp;
private static final HandlerList HANDLER_LIST = new HandlerList(); private static final HandlerList HANDLER_LIST = new HandlerList();
private boolean isCancelled; private boolean isCancelled;
@@ -20,11 +20,11 @@ public class PlayerSetWarpEvent extends Event implements Cancellable {
/** /**
* An event, fired when a Player sets a Warp * An event, fired when a Player sets a Warp
* *
* @param player the Player setting the warp * @param player the {@link Player} setting the warp
* @param warp the Warp being set * @param warp the {@link Warp} being set
*/ */
public PlayerSetWarpEvent(Player player, Warp warp) { public PlayerSetWarpEvent(Player player, Warp warp) {
this.player = player; super(player);
this.warp = warp; this.warp = warp;
this.isCancelled = false; this.isCancelled = false;
} }
@@ -38,19 +38,10 @@ public class PlayerSetWarpEvent extends Event implements Cancellable {
return HANDLER_LIST; return HANDLER_LIST;
} }
/**
* Get the Player involved in this event
*
* @return the Player who is setting the warp
*/
public Player getPlayer() {
return player;
}
/** /**
* Get the Warp being set * Get the Warp being set
* *
* @return the Warp that is being set * @return the {@link Warp}
*/ */
public Warp getWarp() { public Warp getWarp() {
return warp; return warp;
@@ -5,6 +5,7 @@ import org.bukkit.entity.Player;
import org.bukkit.event.Cancellable; import org.bukkit.event.Cancellable;
import org.bukkit.event.Event; import org.bukkit.event.Event;
import org.bukkit.event.HandlerList; import org.bukkit.event.HandlerList;
import org.bukkit.event.player.PlayerEvent;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
/** /**
@@ -12,15 +13,14 @@ import org.jetbrains.annotations.NotNull;
* Fires when a player renames a Warp, updates its' location or changes its' name. * Fires when a player renames a Warp, updates its' location or changes its' name.
* Also fires when a warp is deleted * Also fires when a warp is deleted
*/ */
public class PlayerWarpUpdateEvent extends Event implements Cancellable { public class PlayerWarpUpdateEvent extends PlayerEvent implements Cancellable {
private final Player player;
private final Warp warp; private final Warp warp;
private static final HandlerList HANDLER_LIST = new HandlerList(); private static final HandlerList HANDLER_LIST = new HandlerList();
private boolean isCancelled; private boolean isCancelled;
/** /**
* An event, fired when a Warp is updated. * An event, fired when a {@link Warp} is updated.
* Fires when a player renames a Warp, updates its' location, changes its' name or when it is deleted. * Fires when a player renames a Warp, updates its' location, changes its' name or when it is deleted.
* Does not fire when a player sets a Warp. * Does not fire when a player sets a Warp.
* *
@@ -28,7 +28,7 @@ public class PlayerWarpUpdateEvent extends Event implements Cancellable {
* @param warp The Warp being changed * @param warp The Warp being changed
*/ */
public PlayerWarpUpdateEvent(Player player, Warp warp) { public PlayerWarpUpdateEvent(Player player, Warp warp) {
this.player = player; super(player);
this.warp = warp; this.warp = warp;
this.isCancelled = false; this.isCancelled = false;
} }
@@ -43,18 +43,9 @@ public class PlayerWarpUpdateEvent extends Event implements Cancellable {
} }
/** /**
* Get the Player involved in this event * Get the {@link Warp} being updated
* *
* @return the Player who updated the Warp * @return the {@link Warp}
*/
public Player getPlayer() {
return player;
}
/**
* Get the Warp being updated
*
* @return the Warp being updated
*/ */
public Warp getWarp() { public Warp getWarp() {
return warp; return warp;
@@ -5,6 +5,7 @@ import com.google.common.io.ByteStreams;
import io.papermc.lib.PaperLib; import io.papermc.lib.PaperLib;
import me.william278.huskhomes2.HuskHomes; import me.william278.huskhomes2.HuskHomes;
import me.william278.huskhomes2.MessageManager; import me.william278.huskhomes2.MessageManager;
import me.william278.huskhomes2.api.events.PlayerPreTeleportEvent;
import me.william278.huskhomes2.data.DataManager; import me.william278.huskhomes2.data.DataManager;
import me.william278.huskhomes2.data.message.CrossServerMessageHandler; import me.william278.huskhomes2.data.message.CrossServerMessageHandler;
import me.william278.huskhomes2.data.message.Message; import me.william278.huskhomes2.data.message.Message;
@@ -26,17 +27,44 @@ public class TeleportManager {
private static TeleportationPoint spawnLocation; private static TeleportationPoint spawnLocation;
public static void teleportPlayer(Player player, String targetPlayer, Connection connection) throws SQLException { // Fires the PreTeleportEvent; returns true if the event is cancelled
DataManager.setPlayerLastPosition(player, new TeleportationPoint(player.getLocation(), private static boolean isEventCancelled(Player player, TeleportationPoint targetPoint) {
HuskHomes.getSettings().getServerID()), connection); PlayerPreTeleportEvent preTeleportEvent = new PlayerPreTeleportEvent(player, targetPoint);
setPlayerDestinationFromTargetPlayer(player, targetPlayer, connection); Bukkit.getPluginManager().callEvent(preTeleportEvent);
return preTeleportEvent.isCancelled();
} }
public static void teleportPlayer(Player player, TeleportationPoint point, Connection connection) throws SQLException { public static void teleportPlayer(Player player, String targetPlayer, Connection connection) {
DataManager.setPlayerLastPosition(player, new TeleportationPoint(player.getLocation(), final TeleportationPoint destination = new TeleportationPoint(player.getLocation(),
HuskHomes.getSettings().getServerID()), connection); HuskHomes.getSettings().getServerID());
DataManager.setPlayerDestinationLocation(player, point, connection); Bukkit.getScheduler().runTask(plugin, () -> {
teleportPlayer(player); if (isEventCancelled(player, destination)) return;
Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> {
try {
DataManager.setPlayerLastPosition(player, destination, connection);
setPlayerDestinationFromTargetPlayer(player, targetPlayer, connection);
} catch (SQLException e) {
plugin.getLogger().log(Level.SEVERE, "An SQL exception occurred teleporting a player.", e);
}
});
});
}
public static void teleportPlayer(Player player, TeleportationPoint point, Connection connection) {
final TeleportationPoint destination = new TeleportationPoint(player.getLocation(),
HuskHomes.getSettings().getServerID());
Bukkit.getScheduler().runTask(plugin, () -> {
if (isEventCancelled(player, destination)) return;
Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> {
try {
DataManager.setPlayerLastPosition(player, destination, connection);
DataManager.setPlayerDestinationLocation(player, point, connection);
teleportPlayer(player);
} catch (SQLException e) {
plugin.getLogger().log(Level.SEVERE, "An SQL exception occurred teleporting a player.", e);
}
});
});
} }
// Move a player to a different server in the bungee network // Move a player to a different server in the bungee network