From 3be97b57d44d747cf41c8ceb6f2a7df1ccf2673d Mon Sep 17 00:00:00 2001 From: YouHaveTrouble Date: Mon, 8 Mar 2021 13:47:02 +0100 Subject: [PATCH] added teleport protection --- .../togglepvp/config/ConfigCache.java | 4 ++-- .../player/PlayerTeleportListener.java | 23 +++++++++++++++++++ .../togglepvp/players/PlayerData.java | 10 ++++++++ .../togglepvp/players/PlayerManager.java | 10 +++++++- 4 files changed, 44 insertions(+), 3 deletions(-) create mode 100644 src/main/java/eu/endermite/togglepvp/listeners/player/PlayerTeleportListener.java diff --git a/src/main/java/eu/endermite/togglepvp/config/ConfigCache.java b/src/main/java/eu/endermite/togglepvp/config/ConfigCache.java index 3033bd5..c8f3e0f 100644 --- a/src/main/java/eu/endermite/togglepvp/config/ConfigCache.java +++ b/src/main/java/eu/endermite/togglepvp/config/ConfigCache.java @@ -30,7 +30,7 @@ public void loadDefaults() { addDefault("settings.lava_and_fire_stopper.enabled", "true"); addDefault("settings.lava_and_fire_stopper.radius", "2.5"); - addDefault("settings.channeling_enchant_disabled", "false", "Disables channeling (trident enchant) lightning strike. You may want to keep it disabled because players with pvp off can use it to attack players with pvp on"); + addDefault("settings.channeling_enchant_disabled", "false", "Disables channeling (trident enchant) lightning strike.\nYou may want to keep it disabled because players with pvp off can use it to attack players with pvp on"); addDefault("settings.only_owner_can_interact_with_pet", "false", "Makes it so only pet owner can interact with it. Useful if you don't want people renaming other people's pets."); @@ -45,7 +45,7 @@ public void loadDefaults() { addDefault("settings.punish_for_combat_logout.announce", "true"); addDefault("settings.punish_for_combat_logout.message", "&f%player% logged out while in combat. What a loser."); - addDefault("settings.cache_time", "30", "Time (in seconds) to keep player data in memory when players goes offline. This is used for checking if offline players pvp state. Adjust only if you know what you're doing. NEVER set to less than 6."); + addDefault("settings.cache_time", "30", "Time (in seconds) to keep player data in memory when players goes offline.\nThis is used for checking if offline players pvp state.\nAdjust only if you know what you're doing. NEVER set to less than 6."); addDefault("messages.pvp_enabled", "&cYou enabled PvP!"); addDefault("messages.pvp_disabled", "&cYou disabled PvP!"); diff --git a/src/main/java/eu/endermite/togglepvp/listeners/player/PlayerTeleportListener.java b/src/main/java/eu/endermite/togglepvp/listeners/player/PlayerTeleportListener.java new file mode 100644 index 0000000..d1e7f55 --- /dev/null +++ b/src/main/java/eu/endermite/togglepvp/listeners/player/PlayerTeleportListener.java @@ -0,0 +1,23 @@ +package eu.endermite.togglepvp.listeners.player; + +import eu.endermite.togglepvp.TogglePvp; +import eu.endermite.togglepvp.players.PlayerData; +import eu.endermite.togglepvp.players.SmartCache; +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; +import org.bukkit.event.Listener; +import java.time.Instant; + +@eu.endermite.togglepvp.util.Listener +public class PlayerTeleportListener implements Listener { + + @EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true) + public void onPlayerTeleport(org.bukkit.event.player.PlayerTeleportEvent event) { + Player player = event.getPlayer(); + SmartCache smartCache = TogglePvp.getPlugin().getSmartCache(); + PlayerData playerData = smartCache.getPlayerData(player.getUniqueId()); + playerData.setTeleportTimestamp(Instant.now().getEpochSecond()); + } + +} diff --git a/src/main/java/eu/endermite/togglepvp/players/PlayerData.java b/src/main/java/eu/endermite/togglepvp/players/PlayerData.java index a5d1bd6..ed9554b 100644 --- a/src/main/java/eu/endermite/togglepvp/players/PlayerData.java +++ b/src/main/java/eu/endermite/togglepvp/players/PlayerData.java @@ -10,11 +10,13 @@ public class PlayerData { private boolean pvpEnabled; private boolean lastCombatCheck; private long loginTimestamp; + private long teleportTimestamp; public PlayerData(boolean pvpEnabled) { this.pvpEnabled = pvpEnabled; this.combattime = Instant.now().getEpochSecond()-1; this.loginTimestamp = Instant.now().getEpochSecond()-1; + this.teleportTimestamp = Instant.now().getEpochSecond()-1; refreshCachetime(); } @@ -61,4 +63,12 @@ public void setLoginTimestamp(long loginTimestamp) { public long getLoginTimestamp() { return loginTimestamp; } + + public void setTeleportTimestamp(long teleportTimestamp) { + this.teleportTimestamp = teleportTimestamp + TogglePvp.getPlugin().getConfigCache().getTeleport_protection_time(); + } + + public long getTeleportTimestamp() { + return teleportTimestamp; + } } diff --git a/src/main/java/eu/endermite/togglepvp/players/PlayerManager.java b/src/main/java/eu/endermite/togglepvp/players/PlayerManager.java index f663f52..b26c4b5 100644 --- a/src/main/java/eu/endermite/togglepvp/players/PlayerManager.java +++ b/src/main/java/eu/endermite/togglepvp/players/PlayerManager.java @@ -85,12 +85,15 @@ public boolean canDamage(UUID attacker, UUID victim, boolean sendDenyMessage) { public boolean canDamage(UUID attacker, UUID victim, boolean sendDenyMessage, boolean checkVictimSpawnProtection) { - if (hasLoginProtection(attacker)) + if (hasLoginProtection(attacker) || hasTeleportProtection(attacker)) return false; if (checkVictimSpawnProtection && hasLoginProtection(victim)) return false; + if (checkVictimSpawnProtection && hasTeleportProtection(victim)) + return false; + SmartCache smartCache = TogglePvp.getPlugin().getSmartCache(); if (!smartCache.getPlayerData(attacker).isPvpEnabled()) { @@ -123,4 +126,9 @@ public boolean hasLoginProtection(UUID... uuid) { return false; } + public boolean hasTeleportProtection(UUID uuid) { + SmartCache smartCache = TogglePvp.getPlugin().getSmartCache(); + return Instant.now().getEpochSecond() < smartCache.getPlayerData(uuid).getTeleportTimestamp(); + } + }