From e5d9b02382bb73b8b0503517b41e4a9ff0f0bc76 Mon Sep 17 00:00:00 2001 From: Jake Hoolz <84546680+JakeHoolz@users.noreply.github.com> Date: Sat, 30 Aug 2025 14:34:26 -0500 Subject: [PATCH] refactor: inject plugin into PlayerData --- .../domain/data/PlayerData.java | 35 +++++++++---------- .../mappers/player/MySQLPlayerMapper.java | 1 + .../mappers/player/YAMLPlayerMapper.java | 4 +-- .../repositories/PlayerRepository.java | 2 +- 4 files changed, 21 insertions(+), 21 deletions(-) diff --git a/src/com/backtobedrock/augmentedhardcore/domain/data/PlayerData.java b/src/com/backtobedrock/augmentedhardcore/domain/data/PlayerData.java index ba435ff..7bf28e8 100644 --- a/src/com/backtobedrock/augmentedhardcore/domain/data/PlayerData.java +++ b/src/com/backtobedrock/augmentedhardcore/domain/data/PlayerData.java @@ -23,14 +23,13 @@ import org.bukkit.entity.Player; import org.bukkit.event.entity.EntityDamageEvent; import org.bukkit.event.entity.PlayerDeathEvent; -import org.bukkit.plugin.java.JavaPlugin; import org.javatuples.Pair; import java.time.LocalDateTime; import java.util.*; public class PlayerData { - private final AugmentedHardcore plugin = JavaPlugin.getPlugin(AugmentedHardcore.class); + private final AugmentedHardcore plugin; private final OfflinePlayer player; private final List playtime = new ArrayList<>(); private final Map, IObserver>> observers; @@ -49,22 +48,24 @@ public class PlayerData { private boolean spectatorBanned; private LocalDateTime lastDeath; - public PlayerData(OfflinePlayer player) { + public PlayerData(AugmentedHardcore plugin, OfflinePlayer player) { this( + plugin, player, null, LocalDateTime.now(), - JavaPlugin.getPlugin(AugmentedHardcore.class).getConfigurations().getLivesAndLifePartsConfiguration().getLivesAtStart(), - JavaPlugin.getPlugin(AugmentedHardcore.class).getConfigurations().getLivesAndLifePartsConfiguration().getLifePartsAtStart(), + plugin.getConfigurations().getLivesAndLifePartsConfiguration().getLivesAtStart(), + plugin.getConfigurations().getLivesAndLifePartsConfiguration().getLifePartsAtStart(), false, - JavaPlugin.getPlugin(AugmentedHardcore.class).getConfigurations().getReviveConfiguration().isReviveOnFirstJoin() ? 0L : JavaPlugin.getPlugin(AugmentedHardcore.class).getConfigurations().getReviveConfiguration().getTimeBetweenRevives(), - JavaPlugin.getPlugin(AugmentedHardcore.class).getConfigurations().getLivesAndLifePartsConfiguration().getPlaytimePerLifePart(), - JavaPlugin.getPlugin(AugmentedHardcore.class).getConfigurations().getMaxHealthConfiguration().getPlaytimePerHalfHeart(), + plugin.getConfigurations().getReviveConfiguration().isReviveOnFirstJoin() ? 0L : plugin.getConfigurations().getReviveConfiguration().getTimeBetweenRevives(), + plugin.getConfigurations().getLivesAndLifePartsConfiguration().getPlaytimePerLifePart(), + plugin.getConfigurations().getMaxHealthConfiguration().getPlaytimePerHalfHeart(), new TreeMap<>() ); } - public PlayerData(OfflinePlayer player, String lastKnownIp, LocalDateTime lastDeath, int lives, int lifeParts, boolean spectatorBanned, long timeTillNextRevive, long timeTillNextLifePart, long timeTillNextMaxHealth, NavigableMap bans) { + public PlayerData(AugmentedHardcore plugin, OfflinePlayer player, String lastKnownIp, LocalDateTime lastDeath, int lives, int lifeParts, boolean spectatorBanned, long timeTillNextRevive, long timeTillNextLifePart, long timeTillNextMaxHealth, NavigableMap bans) { + this.plugin = plugin; this.player = player; this.lastDeath = lastDeath; this.bans = bans; @@ -81,9 +82,7 @@ public PlayerData(OfflinePlayer player, String lastKnownIp, LocalDateTime lastDe this.lastKnownIp = lastKnownIp; } - public static PlayerData deserialize(ConfigurationSection section, OfflinePlayer player) { - AugmentedHardcore plugin = JavaPlugin.getPlugin(AugmentedHardcore.class); - + public static PlayerData deserialize(AugmentedHardcore plugin, ConfigurationSection section, OfflinePlayer player) { NavigableMap cBans = new TreeMap<>(); String cLastKnownIp = section.getString("LastKnownIp", null); LocalDateTime cLastDeath = LocalDateTime.parse(section.getString("LastDeath", LocalDateTime.now().toString())); @@ -105,7 +104,7 @@ public static PlayerData deserialize(ConfigurationSection section, OfflinePlayer } } - return new PlayerData(player, cLastKnownIp, cLastDeath, cLives, cLifeParts, cSpectatorBanned, cTimeTillNextRevive, cTimeTillNextLifePart, cTimeTillNextMaxHealth, cBans); + return new PlayerData(plugin, player, cLastKnownIp, cLastDeath, cLives, cLifeParts, cSpectatorBanned, cTimeTillNextRevive, cTimeTillNextLifePart, cTimeTillNextMaxHealth, cBans); } public long getTimeTillNextRevive() { @@ -882,11 +881,11 @@ public void setSpectatorBanned(boolean spectatorBanned) { } public void reset() { - this.setLives(JavaPlugin.getPlugin(AugmentedHardcore.class).getConfigurations().getLivesAndLifePartsConfiguration().getLivesAtStart()); - this.setLifeParts(JavaPlugin.getPlugin(AugmentedHardcore.class).getConfigurations().getLivesAndLifePartsConfiguration().getLifePartsAtStart()); - this.setTimeTillNextRevive(JavaPlugin.getPlugin(AugmentedHardcore.class).getConfigurations().getReviveConfiguration().isReviveOnFirstJoin() ? 0L : JavaPlugin.getPlugin(AugmentedHardcore.class).getConfigurations().getReviveConfiguration().getTimeBetweenRevives()); - this.setTimeTillNextLifePart(JavaPlugin.getPlugin(AugmentedHardcore.class).getConfigurations().getLivesAndLifePartsConfiguration().getPlaytimePerLifePart()); - this.setTimeTillNextMaxHealth(JavaPlugin.getPlugin(AugmentedHardcore.class).getConfigurations().getMaxHealthConfiguration().getPlaytimePerHalfHeart()); + this.setLives(this.plugin.getConfigurations().getLivesAndLifePartsConfiguration().getLivesAtStart()); + this.setLifeParts(this.plugin.getConfigurations().getLivesAndLifePartsConfiguration().getLifePartsAtStart()); + this.setTimeTillNextRevive(this.plugin.getConfigurations().getReviveConfiguration().isReviveOnFirstJoin() ? 0L : this.plugin.getConfigurations().getReviveConfiguration().getTimeBetweenRevives()); + this.setTimeTillNextLifePart(this.plugin.getConfigurations().getLivesAndLifePartsConfiguration().getPlaytimePerLifePart()); + this.setTimeTillNextMaxHealth(this.plugin.getConfigurations().getMaxHealthConfiguration().getPlaytimePerHalfHeart()); this.bans.clear(); this.plugin.getServerRepository().getServerData(this.plugin.getServer()).thenAcceptAsync(serverData -> serverData.getBan(this.player.getUniqueId()).finish()); this.plugin.getPlayerRepository().deletePlayerData(this.player); diff --git a/src/com/backtobedrock/augmentedhardcore/mappers/player/MySQLPlayerMapper.java b/src/com/backtobedrock/augmentedhardcore/mappers/player/MySQLPlayerMapper.java index 1c4ad85..6320d20 100644 --- a/src/com/backtobedrock/augmentedhardcore/mappers/player/MySQLPlayerMapper.java +++ b/src/com/backtobedrock/augmentedhardcore/mappers/player/MySQLPlayerMapper.java @@ -56,6 +56,7 @@ private PlayerData getPlayerData(OfflinePlayer player) { while (resultSet.next()) { if (playerData == null) { playerData = new PlayerData( + this.plugin, player, resultSet.getString("last_known_ip"), resultSet.getTimestamp("last_death") == null ? LocalDateTime.now() : resultSet.getTimestamp("last_death").toLocalDateTime(), diff --git a/src/com/backtobedrock/augmentedhardcore/mappers/player/YAMLPlayerMapper.java b/src/com/backtobedrock/augmentedhardcore/mappers/player/YAMLPlayerMapper.java index 7782855..1ba4e8a 100644 --- a/src/com/backtobedrock/augmentedhardcore/mappers/player/YAMLPlayerMapper.java +++ b/src/com/backtobedrock/augmentedhardcore/mappers/player/YAMLPlayerMapper.java @@ -47,12 +47,12 @@ public void insertPlayerDataSync(PlayerData data) { @Override public CompletableFuture getByPlayer(OfflinePlayer player) { - return CompletableFuture.supplyAsync(() -> PlayerData.deserialize(this.getConfig(player), player)); + return CompletableFuture.supplyAsync(() -> PlayerData.deserialize(this.plugin, this.getConfig(player), player)); } @Override public PlayerData getByPlayerSync(OfflinePlayer player) { - return PlayerData.deserialize(this.getConfig(player), player); + return PlayerData.deserialize(this.plugin, this.getConfig(player), player); } @Override diff --git a/src/com/backtobedrock/augmentedhardcore/repositories/PlayerRepository.java b/src/com/backtobedrock/augmentedhardcore/repositories/PlayerRepository.java index 0fc971f..6bc2e64 100644 --- a/src/com/backtobedrock/augmentedhardcore/repositories/PlayerRepository.java +++ b/src/com/backtobedrock/augmentedhardcore/repositories/PlayerRepository.java @@ -68,7 +68,7 @@ public PlayerData getByPlayerSync(OfflinePlayer player) { private PlayerData getFromDataAndCache(OfflinePlayer player, PlayerData playerData) { if (playerData == null) { - playerData = new PlayerData(player); + playerData = new PlayerData(this.plugin, player); if (player.hasPlayedBefore()) this.mapper.insertPlayerDataAsync(playerData); }