Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 17 additions & 18 deletions src/com/backtobedrock/augmentedhardcore/domain/data/PlayerData.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<AbstractPlaytime> playtime = new ArrayList<>();
private final Map<UUID, Map<Class<?>, IObserver>> observers;
Expand All @@ -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<Integer, Ban> bans) {
public PlayerData(AugmentedHardcore plugin, OfflinePlayer player, String lastKnownIp, LocalDateTime lastDeath, int lives, int lifeParts, boolean spectatorBanned, long timeTillNextRevive, long timeTillNextLifePart, long timeTillNextMaxHealth, NavigableMap<Integer, Ban> bans) {
this.plugin = plugin;
this.player = player;
this.lastDeath = lastDeath;
this.bans = bans;
Expand All @@ -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<Integer, Ban> cBans = new TreeMap<>();
String cLastKnownIp = section.getString("LastKnownIp", null);
LocalDateTime cLastDeath = LocalDateTime.parse(section.getString("LastDeath", LocalDateTime.now().toString()));
Expand All @@ -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() {
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,12 @@ public void insertPlayerDataSync(PlayerData data) {

@Override
public CompletableFuture<PlayerData> 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down