semi-major api changes, javadocs

This commit is contained in:
2022-12-31 16:44:50 +01:00
parent 3c014304ba
commit 5abf99cd6c
8 changed files with 55 additions and 26 deletions
@@ -1,5 +1,7 @@
package me.youhavetrouble.preventstabby.api.event; package me.youhavetrouble.preventstabby.api.event;
import me.youhavetrouble.preventstabby.PreventStabby;
import me.youhavetrouble.preventstabby.players.PlayerData;
import org.bukkit.OfflinePlayer; import org.bukkit.OfflinePlayer;
import org.bukkit.event.Event; import org.bukkit.event.Event;
import org.bukkit.event.HandlerList; import org.bukkit.event.HandlerList;
@@ -25,6 +27,10 @@ public class PlayerTogglePvpEvent extends Event {
return player; return player;
} }
public PlayerData getPlayerData() {
return PreventStabby.getPlugin().getPlayerManager().getPlayer(player.getUniqueId());
}
/** /**
* Returns the state player's pvp state was toggled to. * Returns the state player's pvp state was toggled to.
* @return The state player's pvp state was toggled to. * @return The state player's pvp state was toggled to.
@@ -55,7 +55,7 @@ public class PlacoholderApiHook extends PlaceholderExpansion {
if (!player.isOnline()) { if (!player.isOnline()) {
return legacyComponentSerializer.serialize(PluginMessages.parseMessage(plugin.getConfigCache().getPlaceholder_not_in_combat())); return legacyComponentSerializer.serialize(PluginMessages.parseMessage(plugin.getConfigCache().getPlaceholder_not_in_combat()));
} }
long seconds = plugin.getPlayerManager().getPlayer(uuid).getCombattime() - Instant.now().getEpochSecond(); long seconds = plugin.getPlayerManager().getPlayer(uuid).getCombatTime();
if (seconds > 0) { if (seconds > 0) {
String msg = plugin.getConfigCache().getPlaceholder_combat_time(); String msg = plugin.getConfigCache().getPlaceholder_combat_time();
msg = msg.replaceAll("%time%", String.valueOf(seconds)); msg = msg.replaceAll("%time%", String.valueOf(seconds));
@@ -5,6 +5,9 @@ import me.youhavetrouble.preventstabby.PreventStabby;
import java.time.Instant; import java.time.Instant;
import java.util.UUID; import java.util.UUID;
/**
* PreventStabby player data keeper.<br>
*/
public class PlayerData { public class PlayerData {
private final UUID playerUuid; private final UUID playerUuid;
@@ -18,13 +21,17 @@ public class PlayerData {
this.loginTimestamp = Instant.now().getEpochSecond()-1; this.loginTimestamp = Instant.now().getEpochSecond()-1;
this.teleportTimestamp = Instant.now().getEpochSecond()-1; this.teleportTimestamp = Instant.now().getEpochSecond()-1;
this.inCombat = false; this.inCombat = false;
refreshCachetime(); refreshCacheTime();
} }
public UUID getPlayerUuid() { public UUID getPlayerUuid() {
return playerUuid; return playerUuid;
} }
/**
* Returns true if player has personal pvp enabled, false otherwise.
* @return True if player has personal pvp enabled, false otherwise.
*/
public boolean isPvpEnabled() { public boolean isPvpEnabled() {
return pvpEnabled; return pvpEnabled;
} }
@@ -33,55 +40,75 @@ public class PlayerData {
this.pvpEnabled = pvpEnabled; this.pvpEnabled = pvpEnabled;
} }
public long getCachetime() { protected long getCachetime() {
return cachetime; return cachetime;
} }
public void refreshCachetime() { /**
this.cachetime = Instant.now().getEpochSecond()+ PreventStabby.getPlugin().getConfigCache().getCache_time(); * Lets player cache system know that data should not be removed for the next cache time peroid.<br>
* Use with caution, may cause memory leaks if used inappropriately.
*/
public void refreshCacheTime() {
this.cachetime = Instant.now().getEpochSecond() + PreventStabby.getPlugin().getConfigCache().getCache_time();
} }
public long getCombattime() { /**
return combattime; * Time left until the end of combat in seconds.
* @return Time left until the end of combat in seconds.<br>
* Return of 0 means out of combat or about to be out of combat.
*/
public long getCombatTime() {
return Math.max(combattime - Instant.now().getEpochSecond(), 0);
} }
/**
* Sets timestamp for combat expiry
* @param combattime Timestamp for when combat should expire
*/
public void setCombattime(long combattime) { public void setCombattime(long combattime) {
this.combattime = combattime; this.combattime = combattime;
} }
/**
* Sets player combat time to the interval set in config.
*/
public void refreshCombatTime() { public void refreshCombatTime() {
this.combattime = Instant.now().getEpochSecond()+ PreventStabby.getPlugin().getConfigCache().getCombat_time(); this.combattime = Instant.now().getEpochSecond()+ PreventStabby.getPlugin().getConfigCache().getCombat_time();
} }
public boolean getLastCombatCheck() { protected boolean getLastCombatCheck() {
return lastCombatCheck; return lastCombatCheck;
} }
public void setLastCombatCheck(boolean bool) { protected void setLastCombatCheck(boolean bool) {
lastCombatCheck = bool; lastCombatCheck = bool;
} }
public void setLoginTimestamp(long loginTimestamp) { protected void setLoginTimestamp(long loginTimestamp) {
this.loginTimestamp = loginTimestamp + PreventStabby.getPlugin().getConfigCache().getLogin_protection_time()-1; this.loginTimestamp = loginTimestamp + PreventStabby.getPlugin().getConfigCache().getLogin_protection_time()-1;
} }
public long getLoginTimestamp() { protected long getLoginTimestamp() {
return loginTimestamp; return loginTimestamp;
} }
public void setTeleportTimestamp(long teleportTimestamp) { protected void setTeleportTimestamp(long teleportTimestamp) {
this.teleportTimestamp = teleportTimestamp + PreventStabby.getPlugin().getConfigCache().getTeleport_protection_time()-1; this.teleportTimestamp = teleportTimestamp + PreventStabby.getPlugin().getConfigCache().getTeleport_protection_time()-1;
} }
public long getTeleportTimestamp() { protected long getTeleportTimestamp() {
return teleportTimestamp; return teleportTimestamp;
} }
/**
* Returns player's current combat state.
* @return Player's current combat state.
*/
public boolean isInCombat() { public boolean isInCombat() {
return inCombat; return inCombat;
} }
public void setInCombat(boolean inCombat) { protected void setInCombat(boolean inCombat) {
this.inCombat = inCombat; this.inCombat = inCombat;
} }
} }
@@ -1,7 +1,6 @@
package me.youhavetrouble.preventstabby.listeners.player; package me.youhavetrouble.preventstabby.players;
import me.youhavetrouble.preventstabby.PreventStabby; import me.youhavetrouble.preventstabby.PreventStabby;
import me.youhavetrouble.preventstabby.players.PlayerData;
import me.youhavetrouble.preventstabby.util.PreventStabbyListener; import me.youhavetrouble.preventstabby.util.PreventStabbyListener;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
@@ -1,4 +1,4 @@
package me.youhavetrouble.preventstabby.listeners.player; package me.youhavetrouble.preventstabby.players;
import me.youhavetrouble.preventstabby.PreventStabby; import me.youhavetrouble.preventstabby.PreventStabby;
import me.youhavetrouble.preventstabby.players.PlayerData; import me.youhavetrouble.preventstabby.players.PlayerData;
@@ -34,7 +34,7 @@ public class PlayerJoinAndLeaveListener implements Listener {
}); });
return; return;
} }
playerData.refreshCachetime(); playerData.refreshCacheTime();
playerData.setLoginTimestamp(time); playerData.setLoginTimestamp(time);
} }
@@ -60,7 +60,7 @@ public class PlayerManager {
} }
public void refreshPlayersCacheTime(UUID uuid) { public void refreshPlayersCacheTime(UUID uuid) {
playerList.get(uuid).refreshCachetime(); playerList.get(uuid).refreshCacheTime();
} }
public void refreshPlayersCombatTime(UUID uuid) { public void refreshPlayersCombatTime(UUID uuid) {
@@ -1,8 +1,6 @@
package me.youhavetrouble.preventstabby.listeners.player; package me.youhavetrouble.preventstabby.players;
import me.youhavetrouble.preventstabby.PreventStabby; import me.youhavetrouble.preventstabby.PreventStabby;
import me.youhavetrouble.preventstabby.players.PlayerData;
import me.youhavetrouble.preventstabby.players.SmartCache;
import me.youhavetrouble.preventstabby.util.PreventStabbyListener; import me.youhavetrouble.preventstabby.util.PreventStabbyListener;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
@@ -11,8 +11,7 @@ public class CombatTimer {
public static void refreshPlayersCombatTime(UUID uuid) { public static void refreshPlayersCombatTime(UUID uuid) {
try { try {
long now = Instant.now().getEpochSecond(); long combattime = PreventStabby.getPlugin().getSmartCache().getPlayerData(uuid).getCombatTime();
long combattime = PreventStabby.getPlugin().getSmartCache().getPlayerData(uuid).getCombattime();
Player player = Bukkit.getPlayer(uuid); Player player = Bukkit.getPlayer(uuid);
if (player == null || !player.isOnline()) return; if (player == null || !player.isOnline()) return;
@@ -22,7 +21,7 @@ public class CombatTimer {
if (playerEnterCombatEvent.isCancelled()) return; if (playerEnterCombatEvent.isCancelled()) return;
PreventStabby.getPlugin().getPlayerManager().refreshPlayersCombatTime(uuid); PreventStabby.getPlugin().getPlayerManager().refreshPlayersCombatTime(uuid);
if (combattime <= now) { if (combattime <= 0) {
PluginMessages.sendActionBar(uuid, PreventStabby.getPlugin().getConfigCache().getEntering_combat()); PluginMessages.sendActionBar(uuid, PreventStabby.getPlugin().getConfigCache().getEntering_combat());
} }
}); });
@@ -38,7 +37,7 @@ public class CombatTimer {
public static boolean isInCombat(UUID uuid) { public static boolean isInCombat(UUID uuid) {
try { try {
return PreventStabby.getPlugin().getPlayerManager().getPlayer(uuid).getCombattime() >= Instant.now().getEpochSecond(); return PreventStabby.getPlugin().getPlayerManager().getPlayer(uuid).getCombatTime() >= Instant.now().getEpochSecond();
} catch (Exception e) { } catch (Exception e) {
return false; return false;
} }