mirror of
https://github.com/YouHaveTrouble/PreventStabby.git
synced 2026-05-12 13:26:56 +00:00
massive changes
This commit is contained in:
@@ -0,0 +1,86 @@
|
||||
package me.youhavetrouble.preventstabby.players;
|
||||
|
||||
import me.youhavetrouble.preventstabby.PreventStabby;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.util.UUID;
|
||||
|
||||
public class PlayerData {
|
||||
|
||||
private UUID playerUuid;
|
||||
private long cachetime, combattime, loginTimestamp, teleportTimestamp;
|
||||
private boolean pvpEnabled, lastCombatCheck, inCombat;
|
||||
|
||||
public PlayerData(boolean pvpEnabled) {
|
||||
this.pvpEnabled = pvpEnabled;
|
||||
this.combattime = Instant.now().getEpochSecond()-1;
|
||||
this.loginTimestamp = Instant.now().getEpochSecond()-1;
|
||||
this.teleportTimestamp = Instant.now().getEpochSecond()-1;
|
||||
this.inCombat = false;
|
||||
refreshCachetime();
|
||||
}
|
||||
|
||||
public UUID getPlayerUuid() {
|
||||
return playerUuid;
|
||||
}
|
||||
|
||||
public boolean isPvpEnabled() {
|
||||
return pvpEnabled;
|
||||
}
|
||||
|
||||
public void setPvpEnabled(boolean pvpEnabled) {
|
||||
this.pvpEnabled = pvpEnabled;
|
||||
}
|
||||
|
||||
public long getCachetime() {
|
||||
return cachetime;
|
||||
}
|
||||
|
||||
public void refreshCachetime() {
|
||||
this.cachetime = Instant.now().getEpochSecond()+ PreventStabby.getPlugin().getConfigCache().getCache_time();
|
||||
}
|
||||
|
||||
public long getCombattime() {
|
||||
return combattime;
|
||||
}
|
||||
|
||||
public void setCombattime(long combattime) {
|
||||
this.combattime = combattime;
|
||||
}
|
||||
|
||||
public void refreshCombatTime() {
|
||||
this.combattime = Instant.now().getEpochSecond()+ PreventStabby.getPlugin().getConfigCache().getCombat_time();
|
||||
}
|
||||
|
||||
public boolean getLastCombatCheck() {
|
||||
return lastCombatCheck;
|
||||
}
|
||||
|
||||
public void setLastCombatCheck(boolean bool) {
|
||||
lastCombatCheck = bool;
|
||||
}
|
||||
|
||||
public void setLoginTimestamp(long loginTimestamp) {
|
||||
this.loginTimestamp = loginTimestamp + PreventStabby.getPlugin().getConfigCache().getLogin_protection_time()-1;
|
||||
}
|
||||
|
||||
public long getLoginTimestamp() {
|
||||
return loginTimestamp;
|
||||
}
|
||||
|
||||
public void setTeleportTimestamp(long teleportTimestamp) {
|
||||
this.teleportTimestamp = teleportTimestamp + PreventStabby.getPlugin().getConfigCache().getTeleport_protection_time()-1;
|
||||
}
|
||||
|
||||
public long getTeleportTimestamp() {
|
||||
return teleportTimestamp;
|
||||
}
|
||||
|
||||
public boolean isInCombat() {
|
||||
return inCombat;
|
||||
}
|
||||
|
||||
public void setInCombat(boolean inCombat) {
|
||||
this.inCombat = inCombat;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,160 @@
|
||||
package me.youhavetrouble.preventstabby.players;
|
||||
|
||||
import me.youhavetrouble.preventstabby.PreventStabby;
|
||||
import me.youhavetrouble.preventstabby.api.event.PlayerLeaveCombatEvent;
|
||||
import me.youhavetrouble.preventstabby.config.ConfigCache;
|
||||
import me.youhavetrouble.preventstabby.util.CombatTimer;
|
||||
import me.youhavetrouble.preventstabby.util.PluginMessages;
|
||||
import lombok.Getter;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.scheduler.BukkitTask;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.UUID;
|
||||
|
||||
public class PlayerManager {
|
||||
|
||||
@Getter
|
||||
HashMap<UUID, PlayerData> playerList = new HashMap<>();
|
||||
|
||||
public final BukkitTask combatTrackerTask;
|
||||
|
||||
public PlayerManager() {
|
||||
for (Player p : Bukkit.getOnlinePlayers()) {
|
||||
PlayerData playerData = PreventStabby.getPlugin().getSqLite().getPlayerInfo(p.getUniqueId());
|
||||
playerList.put(p.getUniqueId(), playerData);
|
||||
}
|
||||
|
||||
combatTrackerTask = Bukkit.getScheduler().runTaskTimerAsynchronously(PreventStabby.getPlugin(), () -> {
|
||||
Iterator<PlayerData> iterator = playerList.values().iterator();
|
||||
while (iterator.hasNext()) {
|
||||
PlayerData playerData = iterator.next();
|
||||
UUID uuid = playerData.getPlayerUuid();
|
||||
if (!CombatTimer.isInCombat(uuid)) {
|
||||
if (playerData.getLastCombatCheck()) {
|
||||
Player player = Bukkit.getPlayer(uuid);
|
||||
if (player == null)
|
||||
return;
|
||||
PlayerLeaveCombatEvent playerLeaveCombatEvent = new PlayerLeaveCombatEvent(player);
|
||||
Bukkit.getScheduler().runTask(PreventStabby.getPlugin(), () -> {
|
||||
Bukkit.getPluginManager().callEvent(playerLeaveCombatEvent);
|
||||
if (playerLeaveCombatEvent.isCancelled()) {
|
||||
playerData.refreshCombatTime();
|
||||
return;
|
||||
}
|
||||
playerData.setLastCombatCheck(false);
|
||||
playerData.setInCombat(false);
|
||||
PluginMessages.sendActionBar(uuid, PreventStabby.getPlugin().getConfigCache().getLeaving_combat());
|
||||
});
|
||||
}
|
||||
} else {
|
||||
playerData.setLastCombatCheck(true);
|
||||
}
|
||||
}
|
||||
}, 20, 20);
|
||||
|
||||
|
||||
}
|
||||
|
||||
public void refreshPlayersCacheTime(UUID uuid) {
|
||||
playerList.get(uuid).refreshCachetime();
|
||||
}
|
||||
|
||||
public void refreshPlayersCombatTime(UUID uuid) {
|
||||
try {
|
||||
PlayerData data = playerList.get(uuid);
|
||||
if (data == null)
|
||||
return;
|
||||
Player player = Bukkit.getPlayer(uuid);
|
||||
if (player == null || player.isDead())
|
||||
return;
|
||||
|
||||
data.refreshCombatTime();
|
||||
data.setInCombat(true);
|
||||
} catch (Exception ignored) {
|
||||
}
|
||||
}
|
||||
|
||||
public PlayerData getPlayer(UUID uuid) {
|
||||
return playerList.get(uuid);
|
||||
}
|
||||
|
||||
public void addPlayer(UUID uuid, PlayerData data) {
|
||||
playerList.put(uuid, data);
|
||||
}
|
||||
|
||||
public boolean getPlayerPvPState(UUID uuid) {
|
||||
return PreventStabby.getPlugin().getSmartCache().getPlayerData(uuid).isPvpEnabled();
|
||||
}
|
||||
|
||||
public void setPlayerPvpState(UUID uuid, boolean state) {
|
||||
PreventStabby.getPlugin().getSmartCache().getPlayerData(uuid).setPvpEnabled(state);
|
||||
}
|
||||
|
||||
public boolean togglePlayerPvpState(UUID uuid) {
|
||||
SmartCache smartCache = PreventStabby.getPlugin().getSmartCache();
|
||||
if (smartCache.getPlayerData(uuid).isPvpEnabled()) {
|
||||
smartCache.getPlayerData(uuid).setPvpEnabled(false);
|
||||
return false;
|
||||
} else {
|
||||
smartCache.getPlayerData(uuid).setPvpEnabled(true);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
public boolean canDamage(UUID attacker, UUID victim, boolean sendDenyMessage) {
|
||||
return canDamage(attacker, victim, sendDenyMessage, true);
|
||||
}
|
||||
|
||||
public boolean canDamage(UUID attacker, UUID victim, boolean sendDenyMessage, boolean checkVictimSpawnProtection) {
|
||||
|
||||
if (hasLoginProtection(attacker) || hasTeleportProtection(attacker))
|
||||
return false;
|
||||
|
||||
if (checkVictimSpawnProtection && hasLoginProtection(victim))
|
||||
return false;
|
||||
|
||||
if (checkVictimSpawnProtection && hasTeleportProtection(victim))
|
||||
return false;
|
||||
|
||||
SmartCache smartCache = PreventStabby.getPlugin().getSmartCache();
|
||||
|
||||
if (!smartCache.getPlayerData(attacker).isPvpEnabled()) {
|
||||
if (sendDenyMessage) {
|
||||
ConfigCache config = PreventStabby.getPlugin().getConfigCache();
|
||||
PluginMessages.sendActionBar(attacker, config.getCannot_attack_attacker());
|
||||
}
|
||||
return false;
|
||||
}
|
||||
if (!smartCache.getPlayerData(victim).isPvpEnabled()) {
|
||||
if (sendDenyMessage) {
|
||||
ConfigCache config = PreventStabby.getPlugin().getConfigCache();
|
||||
PluginMessages.sendActionBar(attacker, config.getCannot_attack_victim());
|
||||
}
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @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();
|
||||
for (UUID checkedUuid : uuid) {
|
||||
if (Instant.now().getEpochSecond() < smartCache.getPlayerData(checkedUuid).getLoginTimestamp())
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean hasTeleportProtection(UUID uuid) {
|
||||
SmartCache smartCache = PreventStabby.getPlugin().getSmartCache();
|
||||
return Instant.now().getEpochSecond() < smartCache.getPlayerData(uuid).getTeleportTimestamp();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
package me.youhavetrouble.preventstabby.players;
|
||||
|
||||
import me.youhavetrouble.preventstabby.PreventStabby;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.entity.Player;
|
||||
import java.time.Instant;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
|
||||
public class SmartCache {
|
||||
|
||||
public void runSmartCache() {
|
||||
Bukkit.getScheduler().runTaskTimerAsynchronously(PreventStabby.getPlugin(), () -> {
|
||||
// Refresh cache timer if player is online
|
||||
for (Map.Entry<UUID, PlayerData> e : PreventStabby.getPlugin().getPlayerManager().getPlayerList().entrySet()) {
|
||||
try {
|
||||
Player player = Bukkit.getPlayer(e.getKey());
|
||||
if (player != null && player.isOnline()) {
|
||||
PreventStabby.getPlugin().getPlayerManager().refreshPlayersCacheTime(e.getKey());
|
||||
}
|
||||
} catch (NullPointerException ignored) {}
|
||||
}
|
||||
// Check for entries that should be invalidated
|
||||
try {
|
||||
long now = Instant.now().getEpochSecond();
|
||||
PreventStabby.getPlugin().getPlayerManager().getPlayerList().entrySet()
|
||||
.removeIf(cacheEntry -> cacheEntry.getValue().getCachetime() < now);
|
||||
} catch (Exception ignored) {}
|
||||
}, 100, 100);
|
||||
}
|
||||
|
||||
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);
|
||||
} catch (NullPointerException e) {
|
||||
// If player data is not in cache get it from database and put into cache
|
||||
try {
|
||||
PlayerData playerData = PreventStabby.getPlugin().getSqLite().getPlayerInfo(uuid);
|
||||
PreventStabby.getPlugin().getPlayerManager().addPlayer(uuid, playerData);
|
||||
return playerData;
|
||||
} catch (NullPointerException ex) {
|
||||
// Return false if database call fails
|
||||
return new PlayerData(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void setPlayerPvpState(UUID uuid, boolean state) {
|
||||
// If player is in cache update that
|
||||
if (PreventStabby.getPlugin().getPlayerManager().getPlayer(uuid) != null) {
|
||||
PreventStabby.getPlugin().getPlayerManager().getPlayer(uuid).setPvpEnabled(state);
|
||||
}
|
||||
// Update the database aswell
|
||||
PreventStabby.getPlugin().getSqLite().updatePlayerInfo(uuid, new PlayerData(state));
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user