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
+159 -88

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.
* @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
* @see PlayerHomeUpdateEvent
*/
@@ -25,6 +26,7 @@ public class PlayerChangeHomeDescriptionEvent extends PlayerHomeUpdateEvent {
/**
* Returns the old description of the home
*
* @return the old description String
*/
public String getOldDescription() {
@@ -33,6 +35,7 @@ public class PlayerChangeHomeDescriptionEvent extends PlayerHomeUpdateEvent {
/**
* Returns the new description being set to the home
*
* @return the new description String
*/
public String getNewDescription() {
@@ -12,8 +12,9 @@ public class PlayerChangeWarpDescriptionEvent extends PlayerWarpUpdateEvent {
/**
* 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
* @see PlayerWarpUpdateEvent
*/
@@ -25,6 +26,7 @@ public class PlayerChangeWarpDescriptionEvent extends PlayerWarpUpdateEvent {
/**
* Returns the old description of the home
*
* @return the old description String
*/
public String getOldDescription() {
@@ -33,8 +35,10 @@ public class PlayerChangeWarpDescriptionEvent extends PlayerWarpUpdateEvent {
/**
* Returns the new description being set to the home
*
* @return the new description String
*/
public String getNewDescription() {
return newDescription;
}}
}
}
@@ -9,8 +9,9 @@ import org.bukkit.entity.Player;
public class PlayerDeleteHomeEvent extends PlayerHomeUpdateEvent {
/**
* An event, fired when a player deletes a home
*
* @param player The Player who is deleting the home
* @param home The Home being deleted
* @param home The Home being deleted
* @see PlayerHomeUpdateEvent
*/
public PlayerDeleteHomeEvent(Player player, Home home) {
@@ -9,8 +9,9 @@ import org.bukkit.entity.Player;
public class PlayerDeleteWarpEvent extends PlayerWarpUpdateEvent {
/**
* An event, fired when a player deletes a warp
*
* @param player The Player who is deleting the warp
* @param warp The Warp being deleted
* @param warp The Warp being deleted
* @see PlayerWarpUpdateEvent
*/
public PlayerDeleteWarpEvent(Player player, Warp warp) {
@@ -5,6 +5,7 @@ import org.bukkit.entity.Player;
import org.bukkit.event.Cancellable;
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;
import org.bukkit.event.player.PlayerEvent;
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.
* Also fires when it is deleted
*/
public class PlayerHomeUpdateEvent extends Event implements Cancellable {
private final Player player;
public class PlayerHomeUpdateEvent extends PlayerEvent implements Cancellable {
private final Home home;
private static final HandlerList HANDLER_LIST = new HandlerList();
private boolean isCancelled;
@@ -27,7 +27,7 @@ public class PlayerHomeUpdateEvent extends Event implements Cancellable {
* @param home The Home being changed
*/
public PlayerHomeUpdateEvent(Player player, Home home) {
this.player = player;
super(player);
this.home = home;
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
*/
public Player getPlayer() {
return player;
}
/**
* Get the Player involved in this event
*
* @return the Home being updated
* @return the {@link Home}
*/
public Home getHome() {
return home;
@@ -9,11 +9,12 @@ import org.bukkit.entity.Player;
public class PlayerMakeHomePrivateEvent extends PlayerHomeUpdateEvent {
/**
* An event, fired when a player makes a 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
*/
public PlayerMakeHomePrivateEvent(Player player, Home home) {
super(player, home);
}
}
}
@@ -9,8 +9,9 @@ import org.bukkit.entity.Player;
public class PlayerMakeHomePublicEvent extends PlayerHomeUpdateEvent {
/**
* An event, fired when a player makes a 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
*/
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
* @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
* @see PlayerWarpUpdateEvent
*/
@@ -26,6 +27,7 @@ public class PlayerRelocateHomeEvent extends PlayerHomeUpdateEvent {
/**
* Returns the teleportation position of the Home before it is updated
*
* @return the pre-update teleportation position
*/
public TeleportationPoint getOldTeleportationPoint() {
@@ -34,6 +36,7 @@ public class PlayerRelocateHomeEvent extends PlayerHomeUpdateEvent {
/**
* Returns the updated teleportation position of the Home
*
* @return the updated teleportation position
*/
public TeleportationPoint getNewTeleportationPoint() {
@@ -13,8 +13,9 @@ public class PlayerRelocateWarpEvent extends PlayerWarpUpdateEvent {
/**
* 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
* @see PlayerWarpUpdateEvent
*/
@@ -26,6 +27,7 @@ public class PlayerRelocateWarpEvent extends PlayerWarpUpdateEvent {
/**
* Returns the teleportation position of the Warp before it is updated
*
* @return the pre-update teleportation position
*/
public TeleportationPoint getOldTeleportationPoint() {
@@ -34,6 +36,7 @@ public class PlayerRelocateWarpEvent extends PlayerWarpUpdateEvent {
/**
* Returns the updated teleportation position of the Warp
*
* @return the updated teleportation position
*/
public TeleportationPoint getNewTeleportationPoint() {
@@ -12,8 +12,9 @@ public class PlayerRenameHomeEvent extends PlayerHomeUpdateEvent {
/**
* 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
* @see PlayerHomeUpdateEvent
*/
@@ -25,6 +26,7 @@ public class PlayerRenameHomeEvent extends PlayerHomeUpdateEvent {
/**
* Get the name of the Home pre-update
*
* @return the name of the Home before it is updated
*/
public String getOldName() {
@@ -33,6 +35,7 @@ public class PlayerRenameHomeEvent extends PlayerHomeUpdateEvent {
/**
* Get the changed name of the Home
*
* @return the new name of the Home
*/
public String getNewName() {
@@ -12,8 +12,9 @@ public class PlayerRenameWarpEvent extends PlayerWarpUpdateEvent {
/**
* 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
* @see PlayerWarpUpdateEvent
*/
@@ -25,6 +26,7 @@ public class PlayerRenameWarpEvent extends PlayerWarpUpdateEvent {
/**
* Get the name of the Warp pre-update
*
* @return the name of the Warp before it is updated
*/
public String getOldName() {
@@ -33,8 +35,10 @@ public class PlayerRenameWarpEvent extends PlayerWarpUpdateEvent {
/**
* Get the changed name of the Warp
*
* @return the new name of the Warp
*/
public String getNewName() {
return newName;
}}
}
}
@@ -5,26 +5,26 @@ import org.bukkit.entity.Player;
import org.bukkit.event.Cancellable;
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;
import org.bukkit.event.player.PlayerEvent;
import org.jetbrains.annotations.NotNull;
/**
* 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 static final HandlerList HANDLER_LIST = new HandlerList();
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 home the Home being set
* @param player the {@link Player} setting the Home
* @param home the {@link Home} being set
*/
public PlayerSetHomeEvent(Player player, Home home) {
this.player = player;
super(player);
this.home = home;
this.isCancelled = false;
}
@@ -38,19 +38,10 @@ public class PlayerSetHomeEvent extends Event implements Cancellable {
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
*
* @return the Home being set
* @return the {@link Home}
*/
public Home getHome() {
return home;
@@ -5,14 +5,14 @@ import org.bukkit.entity.Player;
import org.bukkit.event.Cancellable;
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;
import org.bukkit.event.player.PlayerEvent;
import org.jetbrains.annotations.NotNull;
/**
* 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 static final HandlerList HANDLER_LIST = new HandlerList();
private boolean isCancelled;
@@ -20,11 +20,11 @@ public class PlayerSetWarpEvent extends Event implements Cancellable {
/**
* An event, fired when a Player sets a Warp
*
* @param player the Player setting the warp
* @param warp the Warp being set
* @param player the {@link Player} setting the warp
* @param warp the {@link Warp} being set
*/
public PlayerSetWarpEvent(Player player, Warp warp) {
this.player = player;
super(player);
this.warp = warp;
this.isCancelled = false;
}
@@ -38,19 +38,10 @@ public class PlayerSetWarpEvent extends Event implements Cancellable {
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
*
* @return the Warp that is being set
* @return the {@link Warp}
*/
public Warp getWarp() {
return warp;
@@ -5,6 +5,7 @@ import org.bukkit.entity.Player;
import org.bukkit.event.Cancellable;
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;
import org.bukkit.event.player.PlayerEvent;
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.
* 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 static final HandlerList HANDLER_LIST = new HandlerList();
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.
* 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
*/
public PlayerWarpUpdateEvent(Player player, Warp warp) {
this.player = player;
super(player);
this.warp = warp;
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
*/
public Player getPlayer() {
return player;
}
/**
* Get the Warp being updated
*
* @return the Warp being updated
* @return the {@link Warp}
*/
public Warp getWarp() {
return warp;
@@ -5,6 +5,7 @@ import com.google.common.io.ByteStreams;
import io.papermc.lib.PaperLib;
import me.william278.huskhomes2.HuskHomes;
import me.william278.huskhomes2.MessageManager;
import me.william278.huskhomes2.api.events.PlayerPreTeleportEvent;
import me.william278.huskhomes2.data.DataManager;
import me.william278.huskhomes2.data.message.CrossServerMessageHandler;
import me.william278.huskhomes2.data.message.Message;
@@ -26,17 +27,44 @@ public class TeleportManager {
private static TeleportationPoint spawnLocation;
public static void teleportPlayer(Player player, String targetPlayer, Connection connection) throws SQLException {
DataManager.setPlayerLastPosition(player, new TeleportationPoint(player.getLocation(),
HuskHomes.getSettings().getServerID()), connection);
setPlayerDestinationFromTargetPlayer(player, targetPlayer, connection);
// Fires the PreTeleportEvent; returns true if the event is cancelled
private static boolean isEventCancelled(Player player, TeleportationPoint targetPoint) {
PlayerPreTeleportEvent preTeleportEvent = new PlayerPreTeleportEvent(player, targetPoint);
Bukkit.getPluginManager().callEvent(preTeleportEvent);
return preTeleportEvent.isCancelled();
}
public static void teleportPlayer(Player player, TeleportationPoint point, Connection connection) throws SQLException {
DataManager.setPlayerLastPosition(player, new TeleportationPoint(player.getLocation(),
HuskHomes.getSettings().getServerID()), connection);
DataManager.setPlayerDestinationLocation(player, point, connection);
teleportPlayer(player);
public static void teleportPlayer(Player player, String targetPlayer, 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);
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