Skip to content

Commit

Permalink
added teleport protection
Browse files Browse the repository at this point in the history
  • Loading branch information
YouHaveTrouble committed Mar 8, 2021
1 parent 938b10a commit 3be97b5
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/main/java/eu/endermite/togglepvp/config/ConfigCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.");

Expand All @@ -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!");
Expand Down
Original file line number Diff line number Diff line change
@@ -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());
}

}
10 changes: 10 additions & 0 deletions src/main/java/eu/endermite/togglepvp/players/PlayerData.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down Expand Up @@ -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;
}
}
10 changes: 9 additions & 1 deletion src/main/java/eu/endermite/togglepvp/players/PlayerManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down Expand Up @@ -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();
}

}

0 comments on commit 3be97b5

Please sign in to comment.