more api changes and javadocs

This commit is contained in:
2022-12-31 17:09:20 +01:00
parent 5abf99cd6c
commit 61908b8fab
3 changed files with 71 additions and 23 deletions
@@ -31,11 +31,16 @@ public class PlayerData {
/** /**
* Returns true if player has personal pvp enabled, false otherwise. * Returns true if player has personal pvp enabled, false otherwise.
* @return True if player has personal pvp enabled, false otherwise. * @return True if player has personal pvp enabled, false otherwise.
* @see PlayerManager#getPlayerPvPState(UUID)
*/ */
public boolean isPvpEnabled() { public boolean isPvpEnabled() {
return pvpEnabled; return pvpEnabled;
} }
/**
* Sets player's personal pvp state.
* @param pvpEnabled Pvp state to set.
*/
public void setPvpEnabled(boolean pvpEnabled) { public void setPvpEnabled(boolean pvpEnabled) {
this.pvpEnabled = pvpEnabled; this.pvpEnabled = pvpEnabled;
} }
@@ -44,11 +49,7 @@ public class PlayerData {
return cachetime; return cachetime;
} }
/** protected void refreshCacheTime() {
* 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(); this.cachetime = Instant.now().getEpochSecond() + PreventStabby.getPlugin().getConfigCache().getCache_time();
} }
@@ -60,17 +61,14 @@ public class PlayerData {
public long getCombatTime() { public long getCombatTime() {
return Math.max(combattime - Instant.now().getEpochSecond(), 0); return Math.max(combattime - Instant.now().getEpochSecond(), 0);
} }
/** protected void setCombattime(long combattime) {
* Sets timestamp for combat expiry
* @param combattime Timestamp for when combat should expire
*/
public void setCombattime(long combattime) {
this.combattime = combattime; this.combattime = combattime;
} }
/** /**
* Sets player combat time to the interval set in config. * Sets player in combat and sets combat time to the interval set in config.
* @see PlayerManager#refreshPlayersCombatTime(UUID)
*/ */
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();
@@ -59,10 +59,10 @@ public class PlayerManager {
} }
public void refreshPlayersCacheTime(UUID uuid) { /**
playerList.get(uuid).refreshCacheTime(); * Sets player in combat and sets combat time to the interval set in config.
} * @see PlayerData#refreshCombatTime()
*/
public void refreshPlayersCombatTime(UUID uuid) { public void refreshPlayersCombatTime(UUID uuid) {
PlayerData data = playerList.get(uuid); PlayerData data = playerList.get(uuid);
if (data == null) return; if (data == null) return;
@@ -70,25 +70,46 @@ public class PlayerManager {
if (player == null || player.isDead()) return; if (player == null || player.isDead()) return;
data.refreshCombatTime(); data.refreshCombatTime();
data.setInCombat(true); data.setInCombat(true);
} }
/**
* Gets player's PlayerData object. Returns null when player with provided UUID doesn't exist.
* @param uuid Player's UUID.
* @return Player's PlayerData object or null if player doesn't exist.
*/
public PlayerData getPlayer(UUID uuid) { public PlayerData getPlayer(UUID uuid) {
return playerList.get(uuid); return playerList.get(uuid);
} }
public void addPlayer(UUID uuid, PlayerData data) { protected void addPlayer(UUID uuid, PlayerData data) {
playerList.put(uuid, data); playerList.put(uuid, data);
} }
/**
* Returns true if player has personal pvp enabled, false otherwise.
* @param uuid Player's UUID.
* @return True if player has personal pvp enabled, false otherwise.
* @see PlayerData#isPvpEnabled()
*/
public boolean getPlayerPvPState(UUID uuid) { public boolean getPlayerPvPState(UUID uuid) {
return PreventStabby.getPlugin().getSmartCache().getPlayerData(uuid).isPvpEnabled(); return PreventStabby.getPlugin().getSmartCache().getPlayerData(uuid).isPvpEnabled();
} }
/**
* Sets player's personal pvp state.
* @param uuid Player's UUID.
* @param state Pvp state to set.
* @see PlayerData#setPvpEnabled(boolean)
*/
public void setPlayerPvpState(UUID uuid, boolean state) { public void setPlayerPvpState(UUID uuid, boolean state) {
PreventStabby.getPlugin().getSmartCache().getPlayerData(uuid).setPvpEnabled(state); PreventStabby.getPlugin().getSmartCache().getPlayerData(uuid).setPvpEnabled(state);
} }
/**
* Toggles player's personal pvp state.
* @param uuid Player's UUID.
* @return Player's personal pvp state after the change.
*/
public boolean togglePlayerPvpState(UUID uuid) { public boolean togglePlayerPvpState(UUID uuid) {
SmartCache smartCache = PreventStabby.getPlugin().getSmartCache(); SmartCache smartCache = PreventStabby.getPlugin().getSmartCache();
if (smartCache.getPlayerData(uuid).isPvpEnabled()) { if (smartCache.getPlayerData(uuid).isPvpEnabled()) {
@@ -100,10 +121,26 @@ public class PlayerManager {
} }
} }
/**
* Check if attacker can harm the victim. Both of them have to have personal pvp enabled and none of them can have
* any kind of spawn or teleport protection.
* @param attacker Atacker's UUID.
* @param victim Victim's UUID.
* @param sendDenyMessage Should plugin send a message that there was attempt at damaging to both players?
* @return Whenever attacker can harm the victim.
*/
public boolean canDamage(UUID attacker, UUID victim, boolean sendDenyMessage) { public boolean canDamage(UUID attacker, UUID victim, boolean sendDenyMessage) {
return canDamage(attacker, victim, sendDenyMessage, true); return canDamage(attacker, victim, sendDenyMessage, true);
} }
/**
* Check if attacker can harm the victim.
* @param attacker Atacker's UUID.
* @param victim Victim's UUID.
* @param sendDenyMessage Should plugin send a message that there was attempt at damaging to both players?
* @param checkVictimSpawnProtection Should teleport and spawn protections be taken into account?
* @return Whenever attacker can harm the victim.
*/
public boolean canDamage(UUID attacker, UUID victim, boolean sendDenyMessage, boolean checkVictimSpawnProtection) { public boolean canDamage(UUID attacker, UUID victim, boolean sendDenyMessage, boolean checkVictimSpawnProtection) {
if (hasLoginProtection(attacker) || hasTeleportProtection(attacker)) return false; if (hasLoginProtection(attacker) || hasTeleportProtection(attacker)) return false;
@@ -149,8 +186,8 @@ public class PlayerManager {
} }
/** /**
* @param uuid Player UUIDs * @param uuid Player UUIDs.
* @return true if any of the provided UUIDs has spawn protection * @return True if any of the provided UUIDs has spawn protection.
*/ */
public boolean hasLoginProtection(UUID... uuid) { public boolean hasLoginProtection(UUID... uuid) {
SmartCache smartCache = PreventStabby.getPlugin().getSmartCache(); SmartCache smartCache = PreventStabby.getPlugin().getSmartCache();
@@ -161,15 +198,27 @@ public class PlayerManager {
return false; return false;
} }
/**
* @param uuid Player UUID.
* @return True if player tied to the uuid currently has teleport protection.
*/
public boolean hasTeleportProtection(UUID uuid) { public boolean hasTeleportProtection(UUID uuid) {
SmartCache smartCache = PreventStabby.getPlugin().getSmartCache(); SmartCache smartCache = PreventStabby.getPlugin().getSmartCache();
return Instant.now().getEpochSecond() < smartCache.getPlayerData(uuid).getTeleportTimestamp(); return Instant.now().getEpochSecond() < smartCache.getPlayerData(uuid).getTeleportTimestamp();
} }
/**
* Returns current forced pvp state.
* @return Current forced pvp state.
*/
public PvpState getForcedPvpState() { public PvpState getForcedPvpState() {
return pvpForcedState; return pvpForcedState;
} }
/**
* Sets current forced pvp state.
* @param forcedPvpState New forced pvp state.
*/
public void setForcedPvpState(PvpState forcedPvpState) { public void setForcedPvpState(PvpState forcedPvpState) {
this.pvpForcedState = forcedPvpState; this.pvpForcedState = forcedPvpState;
} }
@@ -16,7 +16,7 @@ public class SmartCache {
try { try {
Player player = Bukkit.getPlayer(e.getKey()); Player player = Bukkit.getPlayer(e.getKey());
if (player != null && player.isOnline()) { if (player != null && player.isOnline()) {
PreventStabby.getPlugin().getPlayerManager().refreshPlayersCacheTime(e.getKey()); e.getValue().refreshCacheTime();
} }
} catch (NullPointerException ignored) {} } catch (NullPointerException ignored) {}
} }
@@ -32,8 +32,9 @@ public class SmartCache {
public PlayerData getPlayerData(UUID uuid) { public PlayerData getPlayerData(UUID uuid) {
// Try to get data from cache and refresh it // Try to get data from cache and refresh it
try { try {
PreventStabby.getPlugin().getPlayerManager().refreshPlayersCacheTime(uuid); PlayerData data = PreventStabby.getPlugin().getPlayerManager().getPlayer(uuid);
return PreventStabby.getPlugin().getPlayerManager().getPlayer(uuid); data.refreshCacheTime();
return data;
} catch (NullPointerException e) { } catch (NullPointerException e) {
// If player data is not in cache get it from database and put into cache // If player data is not in cache get it from database and put into cache
try { try {