From 3911a848d433e0cf885dd3e0f2ad4088a5d19510 Mon Sep 17 00:00:00 2001 From: William Date: Thu, 13 Jan 2022 15:33:29 +0000 Subject: [PATCH] Cancel teleports for players who engaged in combat while teleporting --- .../huskhomes2/teleport/TimedTeleport.java | 19 +++++++++++++++++++ src/main/resources/languages/en-gb.yml | 3 ++- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/src/main/java/me/william278/huskhomes2/teleport/TimedTeleport.java b/src/main/java/me/william278/huskhomes2/teleport/TimedTeleport.java index 0a77d2b9..9247bbe4 100644 --- a/src/main/java/me/william278/huskhomes2/teleport/TimedTeleport.java +++ b/src/main/java/me/william278/huskhomes2/teleport/TimedTeleport.java @@ -10,6 +10,8 @@ import me.william278.huskhomes2.teleport.points.TeleportationPoint; import net.md_5.bungee.api.chat.TextComponent; import org.bukkit.Bukkit; import org.bukkit.Location; +import org.bukkit.Statistic; +import org.bukkit.entity.EntityType; import org.bukkit.entity.Player; import org.bukkit.scheduler.BukkitRunnable; @@ -29,6 +31,7 @@ public class TimedTeleport { private final Player player; private final Location initialLocation; private final double initialHealth; + private final int initialPlayerDamageDealt; private final int warmupTime = HuskHomes.getSettings().getTeleportWarmupTime(); @@ -36,6 +39,7 @@ public class TimedTeleport { this.player = player; this.initialLocation = player.getLocation(); this.initialHealth = player.getHealth(); + this.initialPlayerDamageDealt = player.getStatistic(Statistic.DAMAGE_DEALT, EntityType.PLAYER); this.targetType = targetType; this.targetPoint = targetPoint; @@ -47,6 +51,7 @@ public class TimedTeleport { this.player = player; this.initialLocation = player.getLocation(); this.initialHealth = player.getHealth(); + this.initialPlayerDamageDealt = player.getStatistic(Statistic.DAMAGE_DEALT, EntityType.PLAYER); this.targetType = TargetType.RANDOM; MessageManager.sendMessage(player, "teleporting_countdown_start", Integer.toString(this.warmupTime)); @@ -57,6 +62,7 @@ public class TimedTeleport { this.player = player; this.initialLocation = player.getLocation(); this.initialHealth = player.getHealth(); + this.initialPlayerDamageDealt = player.getStatistic(Statistic.DAMAGE_DEALT, EntityType.PLAYER); this.targetType = TargetType.PLAYER; this.targetPlayerName = targetPlayerName; @@ -88,6 +94,13 @@ public class TimedTeleport { sendWarmupMessage(player, "teleporting_action_bar_cancelled"); return; } + if (hasDamagedPlayers(executablePlayer)) { + cancel(); + executablePlayer.playSound(executablePlayer.getLocation(), HuskHomes.getSettings().getTeleportCancelledSound(), 1, 1); + MessageManager.sendMessage(player, "teleporting_cancelled_pvp"); + sendWarmupMessage(player, "teleporting_action_bar_cancelled"); + return; + } i[0] = i[0] - 1; if (i[0] == 0) { cancel(); @@ -190,6 +203,12 @@ public class TimedTeleport { return totalDiff > movementThreshold; } + // This returns if the player has damaged other players + public boolean hasDamagedPlayers(Player p) { + final int playerDamageDealt = p.getStatistic(Statistic.DAMAGE_DEALT, EntityType.PLAYER); + return playerDamageDealt > initialPlayerDamageDealt; + } + public Player getPlayer() { return player; } diff --git a/src/main/resources/languages/en-gb.yml b/src/main/resources/languages/en-gb.yml index 710c96df..ef708c8c 100644 --- a/src/main/resources/languages/en-gb.yml +++ b/src/main/resources/languages/en-gb.yml @@ -119,4 +119,5 @@ error_no_offline_position: '[Error:](#ff3300) [Could not find where %1% last log error_invalid_player: '[Error:](#ff3300) [Please specify the name of a valid player!](#ff7e5e)' error_no_homes_set_other: '[Error:](#ff3300) [%1% hasn''t set any homes!](#ff7e5e)' error_home_invalid_other: '[Error:](#ff3300) [%1% doesn''t have a home set named "%2%"](#ff7e5e)' -error_edit_home_maximum_public_homes: '[Error:](#ff3300) [You can only make a maximum of %1% homes public.](#ff7e5e)' \ No newline at end of file +error_edit_home_maximum_public_homes: '[Error:](#ff3300) [You can only make a maximum of %1% homes public.](#ff7e5e)' +teleporting_cancelled_pvp: '[Teleportation cancelled; you engaged in combat!](#ff7e5e)' \ No newline at end of file