mirror of
https://github.com/YouHaveTrouble/PreventStabby.git
synced 2026-05-12 13:26:56 +00:00
semi-major api changes, javadocs
This commit is contained in:
@@ -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() {
|
||||
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) {
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
package me.youhavetrouble.preventstabby.players;
|
||||
|
||||
import me.youhavetrouble.preventstabby.PreventStabby;
|
||||
import me.youhavetrouble.preventstabby.util.PreventStabbyListener;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.EventPriority;
|
||||
import org.bukkit.event.Listener;
|
||||
|
||||
@PreventStabbyListener
|
||||
public class PlayerDeathListener implements Listener {
|
||||
|
||||
@EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
|
||||
public void onPlayerDeath(org.bukkit.event.entity.EntityDeathEvent event) {
|
||||
|
||||
if (!(event.getEntity() instanceof Player)) return;
|
||||
|
||||
Player player = (Player) event.getEntity();
|
||||
|
||||
PlayerData playerData = PreventStabby.getPlugin().getSmartCache().getPlayerData(player.getUniqueId());
|
||||
playerData.setCombattime(0);
|
||||
playerData.setLastCombatCheck(false);
|
||||
playerData.setInCombat(false);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
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.PluginMessages;
|
||||
import me.youhavetrouble.preventstabby.util.PreventStabbyListener;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.util.UUID;
|
||||
|
||||
@PreventStabbyListener
|
||||
public class PlayerJoinAndLeaveListener implements Listener {
|
||||
/**
|
||||
* This event is here to get players saved options on join
|
||||
*/
|
||||
@EventHandler(ignoreCancelled = true)
|
||||
public void onPlayerJoin(org.bukkit.event.player.PlayerJoinEvent event) {
|
||||
Player player = event.getPlayer();
|
||||
UUID uuid = player.getUniqueId();
|
||||
|
||||
PlayerData playerData = PreventStabby.getPlugin().getPlayerManager().getPlayer(player.getUniqueId());
|
||||
long time = Instant.now().getEpochSecond();
|
||||
if (playerData == null) {
|
||||
PreventStabby.getPlugin().getPlayerManager().addPlayer(uuid, new PlayerData(uuid,false));
|
||||
Bukkit.getScheduler().runTaskAsynchronously(PreventStabby.getPlugin(), () -> {
|
||||
PlayerData data = PreventStabby.getPlugin().getSqLite().getPlayerInfo(uuid);
|
||||
PreventStabby.getPlugin().getPlayerManager().addPlayer(uuid, data);
|
||||
data.setLoginTimestamp(time);
|
||||
});
|
||||
return;
|
||||
}
|
||||
playerData.refreshCacheTime();
|
||||
playerData.setLoginTimestamp(time);
|
||||
}
|
||||
|
||||
/**
|
||||
* This event is here to save player's data to database
|
||||
* Also punishes players who log out during combat
|
||||
*/
|
||||
@EventHandler
|
||||
public void onPlayerLeave(org.bukkit.event.player.PlayerQuitEvent event) {
|
||||
Player player = event.getPlayer();
|
||||
Bukkit.getScheduler().runTaskAsynchronously(PreventStabby.getPlugin(), () -> PreventStabby.getPlugin().getSqLite().updatePlayerInfo(player.getUniqueId(), PreventStabby.getPlugin().getPlayerManager().getPlayer(player.getUniqueId())));
|
||||
|
||||
if (!PreventStabby.getPlugin().getConfigCache().isPunish_for_combat_logout()) return;
|
||||
|
||||
SmartCache smartCache = PreventStabby.getPlugin().getSmartCache();
|
||||
PlayerData playerData = smartCache.getPlayerData(player.getUniqueId());
|
||||
|
||||
if (!playerData.isInCombat()) return;
|
||||
|
||||
player.setHealth(0);
|
||||
if (PreventStabby.getPlugin().getConfigCache().isPunish_for_combat_logout_announce())
|
||||
PluginMessages.broadcastMessage(player, PreventStabby.getPlugin().getConfigCache().getPunish_for_combat_logout_message());
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
@@ -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) {
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
package me.youhavetrouble.preventstabby.players;
|
||||
|
||||
import me.youhavetrouble.preventstabby.PreventStabby;
|
||||
import me.youhavetrouble.preventstabby.util.PreventStabbyListener;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.EventPriority;
|
||||
import org.bukkit.event.Listener;
|
||||
import java.time.Instant;
|
||||
|
||||
@PreventStabbyListener
|
||||
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 = PreventStabby.getPlugin().getSmartCache();
|
||||
PlayerData playerData = smartCache.getPlayerData(player.getUniqueId());
|
||||
playerData.setTeleportTimestamp(Instant.now().getEpochSecond());
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user