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.
* @return True if player has personal pvp enabled, false otherwise.
* @see PlayerManager#getPlayerPvPState(UUID)
*/
public boolean isPvpEnabled() {
return pvpEnabled;
}
/**
* Sets player's personal pvp state.
* @param pvpEnabled Pvp state to set.
*/
public void setPvpEnabled(boolean pvpEnabled) {
this.pvpEnabled = pvpEnabled;
}
@@ -44,11 +49,7 @@ public class PlayerData {
return cachetime;
}
/**
* 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() {
protected void refreshCacheTime() {
this.cachetime = Instant.now().getEpochSecond() + PreventStabby.getPlugin().getConfigCache().getCache_time();
}
@@ -61,16 +62,13 @@ public class PlayerData {
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) {
protected void setCombattime(long 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() {
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) {
PlayerData data = playerList.get(uuid);
if (data == null) return;
@@ -70,25 +70,46 @@ public class PlayerManager {
if (player == null || player.isDead()) return;
data.refreshCombatTime();
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) {
return playerList.get(uuid);
}
public void addPlayer(UUID uuid, PlayerData data) {
protected void addPlayer(UUID uuid, PlayerData 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) {
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) {
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) {
SmartCache smartCache = PreventStabby.getPlugin().getSmartCache();
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) {
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) {
if (hasLoginProtection(attacker) || hasTeleportProtection(attacker)) return false;
@@ -149,8 +186,8 @@ public class PlayerManager {
}
/**
* @param uuid Player UUIDs
* @return true if any of the provided UUIDs has spawn protection
* @param uuid Player UUIDs.
* @return True if any of the provided UUIDs has spawn protection.
*/
public boolean hasLoginProtection(UUID... uuid) {
SmartCache smartCache = PreventStabby.getPlugin().getSmartCache();
@@ -161,15 +198,27 @@ public class PlayerManager {
return false;
}
/**
* @param uuid Player UUID.
* @return True if player tied to the uuid currently has teleport protection.
*/
public boolean hasTeleportProtection(UUID uuid) {
SmartCache smartCache = PreventStabby.getPlugin().getSmartCache();
return Instant.now().getEpochSecond() < smartCache.getPlayerData(uuid).getTeleportTimestamp();
}
/**
* Returns current forced pvp state.
* @return Current forced pvp state.
*/
public PvpState getForcedPvpState() {
return pvpForcedState;
}
/**
* Sets current forced pvp state.
* @param forcedPvpState New forced pvp state.
*/
public void setForcedPvpState(PvpState forcedPvpState) {
this.pvpForcedState = forcedPvpState;
}
@@ -16,7 +16,7 @@ public class SmartCache {
try {
Player player = Bukkit.getPlayer(e.getKey());
if (player != null && player.isOnline()) {
PreventStabby.getPlugin().getPlayerManager().refreshPlayersCacheTime(e.getKey());
e.getValue().refreshCacheTime();
}
} catch (NullPointerException ignored) {}
}
@@ -32,8 +32,9 @@ public class SmartCache {
public PlayerData getPlayerData(UUID uuid) {
// Try to get data from cache and refresh it
try {
PreventStabby.getPlugin().getPlayerManager().refreshPlayersCacheTime(uuid);
return PreventStabby.getPlugin().getPlayerManager().getPlayer(uuid);
PlayerData data = PreventStabby.getPlugin().getPlayerManager().getPlayer(uuid);
data.refreshCacheTime();
return data;
} catch (NullPointerException e) {
// If player data is not in cache get it from database and put into cache
try {