From b1b9900c9e4be56803436f1a12cf6dfb6b13ef42 Mon Sep 17 00:00:00 2001 From: Shaun Bennett Date: Tue, 6 Sep 2016 20:59:34 -0400 Subject: [PATCH] Add offline stat checking --- .../core/achievement/AchievementCategory.java | 17 +++--- .../core/achievement/AchievementManager.java | 33 +++++++++-- .../achievement/command/StatsCommand.java | 47 +++++++++++++++ .../core/achievement/ui/AchievementShop.java | 11 ++-- .../achievement/ui/button/ArcadeButton.java | 13 +++-- .../achievement/ui/button/CategoryButton.java | 12 ++-- .../ui/page/AchievementMainPage.java | 24 ++++---- .../achievement/ui/page/AchievementPage.java | 58 +++++++++++-------- .../achievement/ui/page/ArcadeMainPage.java | 13 ++--- 9 files changed, 160 insertions(+), 68 deletions(-) diff --git a/Plugins/Mineplex.Core/src/mineplex/core/achievement/AchievementCategory.java b/Plugins/Mineplex.Core/src/mineplex/core/achievement/AchievementCategory.java index 21c3b2466..18d6cca9b 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/achievement/AchievementCategory.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/achievement/AchievementCategory.java @@ -235,14 +235,13 @@ public enum AchievementCategory return _gameCategory; } - public void addStats(CoreClientManager clientManager, StatsManager statsManager, List lore, Player player, Player target) + public void addStats(CoreClientManager clientManager, StatsManager statsManager, List lore, Player player, String targetName, PlayerStats targetStats) { - addStats(clientManager, statsManager, lore, Integer.MAX_VALUE, player, target); + addStats(clientManager, statsManager, lore, Integer.MAX_VALUE, player, targetName, targetStats); } - public void addStats(CoreClientManager clientManager, StatsManager statsManager, List lore, int max, Player player, Player target) + public void addStats(CoreClientManager clientManager, StatsManager statsManager, List lore, int max, Player player, String targetName, PlayerStats targetStats) { - PlayerStats stats = statsManager.Get(target); for (int i = 0; i < _statDisplays.length && i < max; i++) { // If the stat is null then just display a blank line instead @@ -260,7 +259,7 @@ public enum AchievementCategory String displayName = _statDisplays[i].getDisplayName(); // Skip showing Losses, Kills, Deaths for other players - if (!clientManager.Get(player).GetRank().has(Rank.MODERATOR) && !player.equals(target) && (displayName.contains("Losses") || displayName.contains("Kills") || displayName.contains("Deaths") || displayName.equals("Time In Game") || displayName.equals("Games Played"))) + if (!clientManager.Get(player).GetRank().has(Rank.MODERATOR) && !player.getName().equals(targetName) && (displayName.contains("Losses") || displayName.contains("Kills") || displayName.contains("Deaths") || displayName.equals("Time In Game") || displayName.equals("Games Played"))) continue; double statNumber = 0; @@ -271,7 +270,7 @@ public enum AchievementCategory if (_statDisplays[i].isFullStat()) { for (String statName : _statDisplays[i].getStats()) - statNumber += stats.getStat(statName); + statNumber += targetStats.getStat(statName); } else { @@ -283,17 +282,17 @@ public enum AchievementCategory { if(statNumber == 0) { - statNumber = stats.getStat(statToPull + "." + statName); + statNumber = targetStats.getStat(statToPull + "." + statName); continue; } - double stat = stats.getStat(statToPull + "." + statName); + double stat = targetStats.getStat(statToPull + "." + statName); if(stat == 0) statNumber = statNumber / 1; else statNumber = (double) statNumber / stat; } else - statNumber += stats.getStat(statToPull + "." + statName); + statNumber += targetStats.getStat(statToPull + "." + statName); } } } diff --git a/Plugins/Mineplex.Core/src/mineplex/core/achievement/AchievementManager.java b/Plugins/Mineplex.Core/src/mineplex/core/achievement/AchievementManager.java index dbd63549c..2146b2dbb 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/achievement/AchievementManager.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/achievement/AchievementManager.java @@ -1,5 +1,6 @@ package mineplex.core.achievement; +import mineplex.core.stats.PlayerStats; import org.bukkit.ChatColor; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; @@ -22,6 +23,7 @@ import java.util.UUID; public class AchievementManager extends MiniPlugin { + private CoreClientManager _clientManager; private IncognitoManager _incognitoManager; private StatsManager _statsManager; private EloManager _eloManager; @@ -41,6 +43,7 @@ public class AchievementManager extends MiniPlugin _incognitoManager = incognitoManager; _statsManager = statsManager; _eloManager = eloManager; + _clientManager = clientManager; _shop = new AchievementShop(this, _statsManager, clientManager, donationManager, "Achievement"); } @@ -50,17 +53,22 @@ public class AchievementManager extends MiniPlugin } public AchievementData get(UUID playerUUID, Achievement type) + { + return get(_statsManager.Get(playerUUID), type); + } + + public AchievementData get(PlayerStats stats, Achievement type) { int exp = 0; for (String stat : type.getStats()) { - exp += _statsManager.Get(playerUUID).getStat(stat); + exp += stats.getStat(stat); } return type.getLevelData(exp); } - + public EloManager getEloManager() { return _eloManager; @@ -127,12 +135,17 @@ public class AchievementManager extends MiniPlugin public void openShop(Player player) { - _shop.attemptShopOpen(player); + openShop(player, player); } public void openShop(Player player, Player target) { - _shop.attemptShopOpen(player, target); + openShop(player, target.getName(), _statsManager.Get(target)); + } + + public void openShop(Player player, String targetName, PlayerStats targetStats) + { + _shop.attemptShopOpen(player, targetName, targetStats); } @EventHandler @@ -212,7 +225,12 @@ public class AchievementManager extends MiniPlugin { return Achievement.getExperienceString(getMineplexLevelNumber(sender, rank)) + " " + ChatColor.RESET; } - + + public CoreClientManager getClientManager() + { + return _clientManager; + } + public void setShopEnabled(boolean var) { _shopEnabled = var; @@ -222,4 +240,9 @@ public class AchievementManager extends MiniPlugin { return _incognitoManager; } + + public StatsManager getStatsManager() + { + return _statsManager; + } } diff --git a/Plugins/Mineplex.Core/src/mineplex/core/achievement/command/StatsCommand.java b/Plugins/Mineplex.Core/src/mineplex/core/achievement/command/StatsCommand.java index 9c1499a58..384bdf996 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/achievement/command/StatsCommand.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/achievement/command/StatsCommand.java @@ -1,5 +1,6 @@ package mineplex.core.achievement.command; +import mineplex.core.stats.PlayerStats; import org.bukkit.entity.Player; import mineplex.core.achievement.AchievementManager; @@ -9,6 +10,8 @@ import mineplex.core.common.util.C; import mineplex.core.common.util.F; import mineplex.core.common.util.UtilPlayer; +import java.sql.SQLException; + public class StatsCommand extends CommandBase { public StatsCommand(AchievementManager plugin) @@ -29,6 +32,7 @@ public class StatsCommand extends CommandBase if (target == null) { + if (Plugin.getClientManager().hasRank(caller, Rank.MODERATOR)) attemptOffline(caller, args); return; } @@ -43,4 +47,47 @@ public class StatsCommand extends CommandBase Plugin.openShop(caller, target); } } + + private void attemptOffline(Player caller, String[] args) + { + if (args.length == 0) + { + Plugin.openShop(caller); + } + else + { + UtilPlayer.message(caller, F.main("Stats", "Attempting to look up offline stats...")); + final String playerName = args[0]; + + Plugin.runAsync(() -> { + try + { + PlayerStats stats = Plugin.getStatsManager().getOfflinePlayerStats(playerName); + + if (stats == null) + { + Plugin.runSync(() -> offlinePlayerNotFound(caller, playerName)); + } + else + { + Plugin.runSync(() -> openShop(caller, playerName, stats)); + } + } catch (SQLException e) + { + Plugin.runSync(() -> UtilPlayer.message(caller, F.main("Stats", "There was an error trying to look up offline player " + F.elem(playerName)))); + e.printStackTrace(); + } + }); + } + } + + private void offlinePlayerNotFound(Player caller, String searchName) + { + UtilPlayer.message(caller, F.main("Stats", "Offline Player " + F.elem(searchName) + " not found.")); + } + + private void openShop(Player caller, String searchName, PlayerStats playerStats) + { + Plugin.openShop(caller, searchName, playerStats); + } } diff --git a/Plugins/Mineplex.Core/src/mineplex/core/achievement/ui/AchievementShop.java b/Plugins/Mineplex.Core/src/mineplex/core/achievement/ui/AchievementShop.java index f9a78db07..809c8d82e 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/achievement/ui/AchievementShop.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/achievement/ui/AchievementShop.java @@ -1,5 +1,6 @@ package mineplex.core.achievement.ui; +import mineplex.core.stats.PlayerStats; import org.bukkit.entity.Player; import mineplex.core.account.CoreClientManager; @@ -26,15 +27,15 @@ public class AchievementShop extends ShopBase @Override protected ShopPageBase> buildPagesFor(Player player) { - return BuildPagesFor(player, player); + return BuildPagesFor(player, player.getName(), getPlugin().getStatsManager().Get(player)); } - protected ShopPageBase> BuildPagesFor(Player player, Player target) + protected ShopPageBase> BuildPagesFor(Player player, String targetName, PlayerStats targetStats) { - return new AchievementMainPage(getPlugin(), _statsManager, this, getClientManager(), getDonationManager(), target.getName() + "'s Stats", player, target); + return new AchievementMainPage(getPlugin(), _statsManager, this, getClientManager(), getDonationManager(), targetName + "'s Stats", player, targetName, targetStats); } - public boolean attemptShopOpen(Player player, Player target) + public boolean attemptShopOpen(Player player, String targetName, PlayerStats targetStats) { if (!getOpenedShop().contains(player.getName())) { @@ -46,7 +47,7 @@ public class AchievementShop extends ShopBase openShopForPlayer(player); if (!getPlayerPageMap().containsKey(player.getName())) { - getPlayerPageMap().put(player.getName(), BuildPagesFor(player, target)); + getPlayerPageMap().put(player.getName(), BuildPagesFor(player, targetName, targetStats)); } openPageForPlayer(player, getOpeningPageForPlayer(player)); diff --git a/Plugins/Mineplex.Core/src/mineplex/core/achievement/ui/button/ArcadeButton.java b/Plugins/Mineplex.Core/src/mineplex/core/achievement/ui/button/ArcadeButton.java index ada6e2984..96ece67f5 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/achievement/ui/button/ArcadeButton.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/achievement/ui/button/ArcadeButton.java @@ -1,5 +1,6 @@ package mineplex.core.achievement.ui.button; +import mineplex.core.stats.PlayerStats; import org.bukkit.Sound; import org.bukkit.entity.Player; import org.bukkit.event.inventory.ClickType; @@ -19,22 +20,26 @@ public class ArcadeButton implements IButton private StatsManager _statsManager; private DonationManager _donationManager; private CoreClientManager _clientManager; - private Player _target; - public ArcadeButton(AchievementShop shop, AchievementManager achievementManager, StatsManager statsManager, DonationManager donationManager, CoreClientManager clientManager, Player target) + private String _targetName; + private PlayerStats _targetStats; + + public ArcadeButton(AchievementShop shop, AchievementManager achievementManager, StatsManager statsManager, DonationManager donationManager, CoreClientManager clientManager, String targetName, PlayerStats targetStats) { _shop = shop; _achievementManager = achievementManager; _statsManager = statsManager; _donationManager = donationManager; _clientManager = clientManager; - _target = target; + + _targetName = targetName; + _targetStats = targetStats; } @Override public void onClick(Player player, ClickType clickType) { - _shop.openPageForPlayer(player, new ArcadeMainPage(_achievementManager, _statsManager, _shop, _clientManager, _donationManager, "Arcade Games", player, _target)); + _shop.openPageForPlayer(player, new ArcadeMainPage(_achievementManager, _statsManager, _shop, _clientManager, _donationManager, "Arcade Games", player, _targetName, _targetStats)); player.playSound(player.getLocation(), Sound.CLICK, 1, 1); } diff --git a/Plugins/Mineplex.Core/src/mineplex/core/achievement/ui/button/CategoryButton.java b/Plugins/Mineplex.Core/src/mineplex/core/achievement/ui/button/CategoryButton.java index ab2d4370c..98337122d 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/achievement/ui/button/CategoryButton.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/achievement/ui/button/CategoryButton.java @@ -1,5 +1,6 @@ package mineplex.core.achievement.ui.button; +import mineplex.core.stats.PlayerStats; import org.bukkit.Sound; import org.bukkit.entity.Player; import org.bukkit.event.inventory.ClickType; @@ -24,9 +25,11 @@ public class CategoryButton implements IButton private StatsManager _statsManager; private DonationManager _donationManager; private CoreClientManager _clientManager; - private Player _target; - public CategoryButton(AchievementShop shop, AchievementManager achievementManager, StatsManager statsManager, AchievementCategory category, DonationManager donationManager, CoreClientManager clientManager, Player target) + private String _targetName; + private PlayerStats _targetStats; + + public CategoryButton(AchievementShop shop, AchievementManager achievementManager, StatsManager statsManager, AchievementCategory category, DonationManager donationManager, CoreClientManager clientManager, String targetName, PlayerStats targetStats) { _category = category; _shop = shop; @@ -34,13 +37,14 @@ public class CategoryButton implements IButton _statsManager = statsManager; _donationManager = donationManager; _clientManager = clientManager; - _target = target; + _targetName = targetName; + _targetStats = targetStats; } @Override public void onClick(Player player, ClickType clickType) { - _shop.openPageForPlayer(player, new AchievementPage(_achievementManager, _statsManager, _category, _shop, _clientManager, _donationManager, player, _target)); + _shop.openPageForPlayer(player, new AchievementPage(_achievementManager, _statsManager, _category, _shop, _clientManager, _donationManager, player, _targetName, _targetStats)); player.playSound(player.getLocation(), Sound.CLICK, 1, 1); } diff --git a/Plugins/Mineplex.Core/src/mineplex/core/achievement/ui/page/AchievementMainPage.java b/Plugins/Mineplex.Core/src/mineplex/core/achievement/ui/page/AchievementMainPage.java index 280e3ba54..8113e2f42 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/achievement/ui/page/AchievementMainPage.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/achievement/ui/page/AchievementMainPage.java @@ -3,6 +3,7 @@ package mineplex.core.achievement.ui.page; import java.util.ArrayList; import java.util.List; +import mineplex.core.stats.PlayerStats; import org.bukkit.ChatColor; import org.bukkit.Material; import org.bukkit.entity.Player; @@ -24,19 +25,22 @@ import mineplex.core.stats.StatsManager; public class AchievementMainPage extends ShopPageBase { - protected Player _target; protected StatsManager _statsManager; - public AchievementMainPage(AchievementManager plugin, StatsManager statsManager, AchievementShop shop, CoreClientManager clientManager, DonationManager donationManager, String name, Player player, Player target) + protected String _targetName; + protected PlayerStats _targetStats; + + public AchievementMainPage(AchievementManager plugin, StatsManager statsManager, AchievementShop shop, CoreClientManager clientManager, DonationManager donationManager, String name, Player player, String targetName, PlayerStats targetStats) { - this(plugin, statsManager, shop, clientManager, donationManager, name, 9 * 4, player, target); + this(plugin, statsManager, shop, clientManager, donationManager, name, player, 9 * 4, targetName, targetStats); } - - public AchievementMainPage(AchievementManager plugin, StatsManager statsManager, AchievementShop shop, CoreClientManager clientManager, DonationManager donationManager, String name, int size, Player player, Player target) + + public AchievementMainPage(AchievementManager plugin, StatsManager statsManager, AchievementShop shop, CoreClientManager clientManager, DonationManager donationManager, String name, Player player, int size, String targetName, PlayerStats targetStats) { super(plugin, shop, clientManager, donationManager, name, player, size); - _target = target; + _targetName = targetName; + _targetStats = targetStats; _statsManager = statsManager; buildPage(); @@ -58,12 +62,12 @@ public class AchievementMainPage extends ShopPageBase lore = new ArrayList(); lore.add(" "); category.addStats(getClientManager(), _statsManager, lore, category == AchievementCategory.GLOBAL ? 10 : 2, - getPlayer(), _target); + getPlayer(), _targetName, _targetStats); lore.add(" "); addAchievements(category, lore, 9); lore.add(ChatColor.RESET + "Click for more details!"); @@ -78,7 +82,7 @@ public class AchievementMainPage extends ShopPageBase 1) continue; - AchievementData data = getPlugin().get(_target, achievement); + AchievementData data = getPlugin().get(_targetStats, achievement); boolean finished = data.getLevel() >= achievement.getMaxLevel(); lore.add((finished ? C.cGreen : C.cRed) + achievement.getName()); diff --git a/Plugins/Mineplex.Core/src/mineplex/core/achievement/ui/page/AchievementPage.java b/Plugins/Mineplex.Core/src/mineplex/core/achievement/ui/page/AchievementPage.java index 3bfc06eeb..b0912addd 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/achievement/ui/page/AchievementPage.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/achievement/ui/page/AchievementPage.java @@ -10,12 +10,14 @@ import mineplex.core.achievement.AchievementData; import mineplex.core.achievement.AchievementManager; import mineplex.core.achievement.ui.AchievementShop; import mineplex.core.common.util.C; +import mineplex.core.common.util.UtilPlayer; import mineplex.core.donation.DonationManager; import mineplex.core.elo.EloManager; import mineplex.core.elo.EloManager.EloDivision; import mineplex.core.shop.item.IButton; import mineplex.core.shop.item.ShopItem; import mineplex.core.shop.page.ShopPageBase; +import mineplex.core.stats.PlayerStats; import mineplex.core.stats.StatsManager; import org.bukkit.ChatColor; @@ -33,17 +35,21 @@ public class AchievementPage extends ShopPageBase achievements = getAchievements(); for (Achievement achievement : achievements) { - AchievementData data = getPlugin().get(_target, achievement); + AchievementData data = getPlugin().get(_targetStats, achievement); boolean singleLevel = achievement.isSingleLevel(); boolean hasUnlocked = data.getLevel() >= achievement.getMaxLevel(); @@ -117,7 +123,7 @@ public class AchievementPage extends ShopPageBase lore = new ArrayList(); lore.add(" "); - _category.addStats(getClientManager(), _statsManager, lore, getPlayer(), _target); + _category.addStats(getClientManager(), _statsManager, lore, getPlayer(), _targetName, _targetStats); ItemStack item = new ItemStack(material); ItemMeta meta = item.getItemMeta(); @@ -175,27 +181,31 @@ public class AchievementPage extends ShopPageBase 1) + Player target = UtilPlayer.searchExact(_targetName); + + if (target != null) { - for (int i = 0; i < _category.GameId.length; i++) + if (_category.getFriendlyName().startsWith("Global")) + return; + if (!_category.DisplayDivision || _category.GameId.length < 1) + return; + if (_category.GameId.length > 1) { - int id = _category.GameId[i]; - int elo = _eloManager.getElo(_target, id); + for (int i = 0; i < _category.GameId.length; i++) + { + int id = _category.GameId[i]; + int elo = _eloManager.getElo(target, id); + ItemStack disp = EloDivision.getDivision(elo).getVisual(elo); + setItem(44 + i + 1, disp); + } + } else + { + int id = _category.GameId[0]; + int elo = _eloManager.getElo(target, id); ItemStack disp = EloDivision.getDivision(elo).getVisual(elo); - setItem(44 + i + 1, disp); + setItem(49, disp); } } - else - { - int id = _category.GameId[0]; - int elo = _eloManager.getElo(_target, id); - ItemStack disp = EloDivision.getDivision(elo).getVisual(elo); - setItem(49, disp); - } } public List getAchievements() diff --git a/Plugins/Mineplex.Core/src/mineplex/core/achievement/ui/page/ArcadeMainPage.java b/Plugins/Mineplex.Core/src/mineplex/core/achievement/ui/page/ArcadeMainPage.java index 41a3c8bc1..b61471755 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/achievement/ui/page/ArcadeMainPage.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/achievement/ui/page/ArcadeMainPage.java @@ -2,6 +2,7 @@ package mineplex.core.achievement.ui.page; import java.util.ArrayList; +import mineplex.core.stats.PlayerStats; import org.bukkit.ChatColor; import org.bukkit.Material; import org.bukkit.Sound; @@ -21,11 +22,9 @@ import mineplex.core.stats.StatsManager; public class ArcadeMainPage extends AchievementMainPage { - public ArcadeMainPage(AchievementManager plugin, StatsManager statsManager, AchievementShop shop, CoreClientManager clientManager, DonationManager donationManager, String name, Player player, Player target) + public ArcadeMainPage(AchievementManager plugin, StatsManager statsManager, AchievementShop shop, CoreClientManager clientManager, DonationManager donationManager, String name, Player player, String targetName, PlayerStats targetStats) { - super(plugin, statsManager, shop, clientManager, donationManager, name, 9 * 5, player, target); - - + super(plugin, statsManager, shop, clientManager, donationManager, name, player, 9 * 5, targetName, targetStats); } @Override @@ -38,11 +37,11 @@ public class ArcadeMainPage extends AchievementMainPage if (category.getGameCategory() != AchievementCategory.GameCategory.ARCADE) continue; - CategoryButton button = new CategoryButton(getShop(), getPlugin(), _statsManager, category, getDonationManager(), getClientManager(), _target); + CategoryButton button = new CategoryButton(getShop(), getPlugin(), _statsManager, category, getDonationManager(), getClientManager(), _targetName, _targetStats); ArrayList lore = new ArrayList(); lore.add(" "); - category.addStats(getClientManager(), _statsManager, lore, 2, getPlayer(), _target); + category.addStats(getClientManager(), _statsManager, lore, 2, getPlayer(), _targetName, _targetStats); lore.add(" "); addAchievements(category, lore, 9); lore.add(" "); @@ -63,7 +62,7 @@ public class ArcadeMainPage extends AchievementMainPage { public void onClick(Player player, ClickType clickType) { - getShop().openPageForPlayer(getPlayer(), new AchievementMainPage(getPlugin(), _statsManager, getShop(), getClientManager(), getDonationManager(), _target.getName() + "'s Stats", player, _target)); + getShop().openPageForPlayer(getPlayer(), new AchievementMainPage(getPlugin(), _statsManager, getShop(), getClientManager(), getDonationManager(), _targetName + "'s Stats", player, _targetName, _targetStats)); player.playSound(player.getLocation(), Sound.CLICK, 1, 1); } });