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;
import me.youhavetrouble.preventstabby.PreventStabby;
import me.youhavetrouble.preventstabby.players.PlayerData;
import org.bukkit.OfflinePlayer;
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;
@@ -25,6 +27,10 @@ public class PlayerTogglePvpEvent extends Event {
return player;
}
public PlayerData getPlayerData() {
return PreventStabby.getPlugin().getPlayerManager().getPlayer(player.getUniqueId());
}
/**
* Returns 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()) {
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) {
String msg = plugin.getConfigCache().getPlaceholder_combat_time();
msg = msg.replaceAll("%time%", String.valueOf(seconds));
@@ -5,6 +5,9 @@ import me.youhavetrouble.preventstabby.PreventStabby;
import java.time.Instant;
import java.util.UUID;
/**
* PreventStabby player data keeper.<br>
*/
public class PlayerData {
private final UUID playerUuid;
@@ -18,13 +21,17 @@ public class PlayerData {
this.loginTimestamp = Instant.now().getEpochSecond()-1;
this.teleportTimestamp = Instant.now().getEpochSecond()-1;
this.inCombat = false;
refreshCachetime();
refreshCacheTime();
}
public UUID getPlayerUuid() {
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() {
return pvpEnabled;
}
@@ -33,55 +40,75 @@ public class PlayerData {
this.pvpEnabled = pvpEnabled;
}
public long getCachetime() {
protected long getCachetime() {
return cachetime;
}
public 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();
}
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) {
this.combattime = combattime;
}
/**
* Sets player combat time to the interval set in config.
*/
public void refreshCombatTime() {
this.combattime = Instant.now().getEpochSecond()+ PreventStabby.getPlugin().getConfigCache().getCombat_time();
}
public boolean getLastCombatCheck() {
protected boolean getLastCombatCheck() {
return lastCombatCheck;
}
public void setLastCombatCheck(boolean bool) {
protected void setLastCombatCheck(boolean bool) {
lastCombatCheck = bool;
}
public void setLoginTimestamp(long loginTimestamp) {
protected void setLoginTimestamp(long loginTimestamp) {
this.loginTimestamp = loginTimestamp + PreventStabby.getPlugin().getConfigCache().getLogin_protection_time()-1;
}
public long getLoginTimestamp() {
protected long getLoginTimestamp() {
return loginTimestamp;
}
public void setTeleportTimestamp(long teleportTimestamp) {
protected void setTeleportTimestamp(long teleportTimestamp) {
this.teleportTimestamp = teleportTimestamp + PreventStabby.getPlugin().getConfigCache().getTeleport_protection_time()-1;
}
public long getTeleportTimestamp() {
protected long getTeleportTimestamp() {
return teleportTimestamp;
}
/**
* Returns player's current combat state.
* @return Player's current combat state.
*/
public boolean isInCombat() {
return inCombat;
}
public void setInCombat(boolean inCombat) {
protected void setInCombat(boolean 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.players.PlayerData;
import me.youhavetrouble.preventstabby.util.PreventStabbyListener;
import org.bukkit.entity.Player;
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.players.PlayerData;
@@ -34,7 +34,7 @@ public class PlayerJoinAndLeaveListener implements Listener {
});
return;
}
playerData.refreshCachetime();
playerData.refreshCacheTime();
playerData.setLoginTimestamp(time);
}
@@ -60,7 +60,7 @@ public class PlayerManager {
}
public void refreshPlayersCacheTime(UUID uuid) {
playerList.get(uuid).refreshCachetime();
playerList.get(uuid).refreshCacheTime();
}
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.players.PlayerData;
import me.youhavetrouble.preventstabby.players.SmartCache;
import me.youhavetrouble.preventstabby.util.PreventStabbyListener;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
@@ -11,8 +11,7 @@ public class CombatTimer {
public static void refreshPlayersCombatTime(UUID uuid) {
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);
if (player == null || !player.isOnline()) return;
@@ -22,7 +21,7 @@ public class CombatTimer {
if (playerEnterCombatEvent.isCancelled()) return;
PreventStabby.getPlugin().getPlayerManager().refreshPlayersCombatTime(uuid);
if (combattime <= now) {
if (combattime <= 0) {
PluginMessages.sendActionBar(uuid, PreventStabby.getPlugin().getConfigCache().getEntering_combat());
}
});
@@ -38,7 +37,7 @@ public class CombatTimer {
public static boolean isInCombat(UUID uuid) {
try {
return PreventStabby.getPlugin().getPlayerManager().getPlayer(uuid).getCombattime() >= Instant.now().getEpochSecond();
return PreventStabby.getPlugin().getPlayerManager().getPlayer(uuid).getCombatTime() >= Instant.now().getEpochSecond();
} catch (Exception e) {
return false;
}