package com.io.yutian.aumaster.data; import com.io.yutian.aumaster.AuMaster; import com.io.yutian.aumaster.config.Config; import com.io.yutian.aumaster.manager.PlayerManager; import com.io.yutian.aumaster.util.AttributeUtil; import com.io.yutian.aumaster.util.RewardUtil; import com.io.yutian.rainlib.util.StringUtil; import com.io.yutian.rainlib.util.TimeUtil; import com.yaohun.onlinereward.AuOnlineReward; import com.yaohun.onlinereward.api.OnlineAPI; import me.Demon.DemonBanK.BankAPI; import me.Demon.DemonLevels.api.DLevelAPI; import me.Demon.DemonPlugin.DemonAPI; import org.bukkit.Bukkit; import org.bukkit.Sound; import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.entity.Player; import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.List; public class PlayerData { private final String name; private final File file; private final FileConfiguration configuration; private String mentorName; private long bindStartTime; private int activePoints; private List discipleNames = new ArrayList<>(); private List discipleNamesHistory = new ArrayList<>(); private long lastUnbindTime = 0; private int disciplesReachedTargetLevel; private List receivedLevelRewards = new ArrayList<>(); private List onlineRewardDiscipleNames = new ArrayList<>(); private long rewardUpdateTime; private long activePointsUpdateTime; public PlayerData(String name) { this.name = name; File path = new File(AuMaster.inst().getDataFolder(), "PlayerData"); if (!path.exists()) { path.mkdirs(); } this.file = new File(path, name + ".yml"); if (!file.exists()) { try { file.createNewFile(); } catch (IOException e) { e.printStackTrace(); } } this.configuration = YamlConfiguration.loadConfiguration(file); load(); check(); } public void check() { boolean changed = false; if (rewardUpdateTime == 0) { rewardUpdateTime = System.currentTimeMillis(); changed = true; } if (activePointsUpdateTime == 0) { activePointsUpdateTime = System.currentTimeMillis(); changed = true; } if (shouldAutoUnbind()) { String oldMentor = this.mentorName; unbind(); Player disciple = Bukkit.getPlayerExact(this.name); Player mentor = Bukkit.getPlayerExact(oldMentor); int relationshipAutoRemoveDays = Config.getRelationshipAutoRemoveDays(); if (disciple != null && disciple.isOnline()) { disciple.sendMessage(StringUtil.formatIndexed(Config.getLangData().getMessage("AutoUnbind-Disciple-Message"), oldMentor, relationshipAutoRemoveDays)); } if (mentor != null && mentor.isOnline()) { mentor.sendMessage(StringUtil.formatIndexed(Config.getLangData().getMessage("AutoUnbind-Mentor-Message"), this.name, relationshipAutoRemoveDays)); } changed = true; } if (isDisciple()) { Player player = Bukkit.getPlayerExact(this.name); if (player!= null && player.isOnline()) { AttributeUtil.addAttribute(player); } } if (isMentor()) { if (!TimeUtil.isSameDay(rewardUpdateTime, System.currentTimeMillis())) { onlineRewardDiscipleNames.clear(); } for (String discipleName : discipleNames) { if (!discipleNamesHistory.contains(discipleName)) { double playerLevel = DLevelAPI.getOffPlayer_Level(discipleName); if (playerLevel >= Config.getLevelTargetReward()) { discipleNamesHistory.add(discipleName); disciplesReachedTargetLevel++; changed = true; } } if (!onlineRewardDiscipleNames.contains(discipleName)) { com.yaohun.onlinereward.data.PlayerData playerData = AuOnlineReward.getDataManager().getPlayerData(discipleName); int onlineTime = playerData.todayOnline; if (onlineTime >= Config.getDailyOnlineRewardSeconds()) { onlineRewardDiscipleNames.add(discipleName); addActivePoints(Config.getDailyOnlineRewardPoints()); Player player = Bukkit.getPlayerExact(name); if (player != null && player.isOnline()) { DemonAPI.sendMessage(player, StringUtil.formatIndexed(Config.getLangData().getMessage("ActivePoint-OnlineTime-Got"), discipleName, Config.getDailyOnlineRewardPoints())); } rewardUpdateTime = System.currentTimeMillis(); changed = true; } } } if (!TimeUtil.isSameMonth(activePointsUpdateTime, System.currentTimeMillis())) { Player player = Bukkit.getPlayerExact(name); if (player != null && player.isOnline()) { int coins = Math.round(activePoints / 10.0f); BankAPI.addCoins(name, coins); player.sendMessage(StringUtil.formatIndexed(Config.getLangData().getMessage("Reward-ActivePoints-Got"), coins)); player.playSound(player.getLocation(), Sound.ENTITY_PLAYER_LEVELUP, 1, 1); activePointsUpdateTime = System.currentTimeMillis(); activePoints = 0; changed = true; } } } if (changed) { save(); } } public void unbind() { if (!hasMentor()) { return; } String oldMentor = this.mentorName; this.mentorName = null; this.bindStartTime = 0; this.lastUnbindTime = System.currentTimeMillis(); this.save(); PlayerData mentorData = PlayerManager.getPlayerData(oldMentor); mentorData.removeDiscipleName(this.name); mentorData.save(); } public boolean shouldAutoUnbind() { return hasMentor() && bindStartTime > 0 && (bindStartTime + 1000L * 60 * 60 * 24 * Config.getRelationshipAutoRemoveDays() <= System.currentTimeMillis()); } public void receiveReward(Integer level) { if (receivedLevelRewards.contains(level)) { return; } RewardUtil.runReward(level, name); receivedLevelRewards.add(level); save(); } public boolean canReceiveReward(int amount) { if (receivedLevelRewards.contains(amount)) { return false; } return disciplesReachedTargetLevel >= amount; } public boolean isDisciple() { return mentorName != null; } public boolean isMentor() { return !discipleNames.isEmpty(); } public boolean hasMentor() { return mentorName != null; } public String getName() { return name; } public String getMentorName() { return mentorName; } public void setMentorName(String mentorName) { this.mentorName = mentorName; } public long getBindStartTime() { return bindStartTime; } public void setBindStartTime(long bindStartTime) { this.bindStartTime = bindStartTime; } public List getDiscipleNames() { return discipleNames; } public void addDiscipleName(String discipleName) { discipleNames.add(discipleName); } public void removeDiscipleName(String discipleName) { discipleNames.remove(discipleName); } public void setDiscipleNames(List discipleNames) { this.discipleNames = discipleNames; } public void addDiscipleNameHistory(String discipleName) { discipleNamesHistory.add(discipleName); } public void removeDiscipleNameHistory(String discipleName) { discipleNamesHistory.remove(discipleName); } public void setDiscipleNamesHistory(List discipleNamesHistory) { this.discipleNamesHistory = discipleNamesHistory; } public int getDisciplesReachedTargetLevel() { return disciplesReachedTargetLevel; } public void setDisciplesReachedTargetLevel(int disciplesReachedTargetLevel) { this.disciplesReachedTargetLevel = disciplesReachedTargetLevel; } public List getDiscipleNamesHistory() { return discipleNamesHistory; } public int getActivePoints() { return activePoints; } public void addActivePoints(int points) { this.activePoints += points; } public void takeActivePoints(int points) { this.activePoints -= points; } public void setActivePoints(int activePoints) { this.activePoints = activePoints; } public long getLastUnbindTime() { return lastUnbindTime; } public void setLastUnbindTime(long lastUnbindTime) { this.lastUnbindTime = lastUnbindTime; } public List getReceivedLevelRewards() { return receivedLevelRewards; } public void setReceivedLevelRewards(List receivedLevelRewards) { this.receivedLevelRewards = receivedLevelRewards; } public List getOnlineRewardDiscipleNames() { return onlineRewardDiscipleNames; } public void setOnlineRewardDiscipleNames(List onlineRewardDiscipleNames) { this.onlineRewardDiscipleNames = onlineRewardDiscipleNames; } public long getRewardUpdateTime() { return rewardUpdateTime; } public void setRewardUpdateTime(long rewardUpdateTime) { this.rewardUpdateTime = rewardUpdateTime; } public long getActivePointsUpdateTime() { return activePointsUpdateTime; } public void setActivePointsUpdateTime(long activePointsUpdateTime) { this.activePointsUpdateTime = activePointsUpdateTime; } public void load() { this.mentorName = configuration.getString("mentorName", null); this.bindStartTime = configuration.getLong("bindStartTime", 0); this.discipleNames = new ArrayList<>(); this.discipleNamesHistory = new ArrayList<>(); this.lastUnbindTime = configuration.getLong("lastUnbindTime", 0); this.discipleNames = configuration.getStringList("discipleNames"); this.discipleNamesHistory = configuration.getStringList("discipleNamesHistory"); this.disciplesReachedTargetLevel = configuration.getInt("disciplesReachedTargetLevel", 0); this.activePoints = configuration.getInt("activePoints", 0); this.receivedLevelRewards = configuration.getIntegerList("receivedLevelRewards"); this.onlineRewardDiscipleNames = configuration.getStringList("onlineRewardDiscipleNames"); this.rewardUpdateTime = configuration.getLong("rewardUpdateTime", 0); this.activePointsUpdateTime = configuration.getLong("activePointsUpdateTime", 0); } public void save() { configuration.set("mentorName", this.mentorName); configuration.set("bindStartTime", this.bindStartTime); configuration.set("discipleNames", this.discipleNames); configuration.set("discipleNamesHistory", this.discipleNamesHistory); configuration.set("disciplesReachedTargetLevel", this.disciplesReachedTargetLevel); configuration.set("activePoints", activePoints); configuration.set("lastUnbindTime", this.lastUnbindTime); configuration.set("receivedLevelRewards", receivedLevelRewards); configuration.set("onlineRewardDiscipleNames", onlineRewardDiscipleNames); configuration.set("rewardUpdateTime", rewardUpdateTime); configuration.set("activePointsUpdateTime", activePointsUpdateTime); try { configuration.save(file); } catch (IOException e) { e.printStackTrace(); } } }