Search in sources :

Example 41 with Query

use of me.staartvin.statz.database.datatype.Query in project Statz by Staartvin.

the class GUIManager method getStatisticMessages.

private List<String> getStatisticMessages(PlayerStat stat, PlayerInfo info) {
    List<String> messages = new ArrayList<>();
    List<Query> results = info.getDataOfPlayerStat(stat);
    for (Query q : results) {
        messages.add(StatzUtil.getInfoString(q, stat, "You"));
    }
    return messages;
}
Also used : Query(me.staartvin.statz.database.datatype.Query)

Example 42 with Query

use of me.staartvin.statz.database.datatype.Query in project Statz by Staartvin.

the class GUIManager method getSpecificStatisticInventory.

/**
 * Much like {@link #getSpecificStatisticInventory(UUID, PlayerStat, String)}}, this method provides an inventory
 * that shows the data of a specific stat type.
 * @param uuid UUID of the player to view
 * @param statType Type of stat to view
 * @param playerName Name of the player to view
 * @return an inventory that represents the data Statz has collected of the given player.
 */
public Inventory getSpecificStatisticInventory(UUID uuid, PlayerStat statType, String playerName) {
    PlayerInfo info = plugin.getDataManager().getPlayerInfo(uuid, statType);
    List<Query> results = info.getDataOfPlayerStat(statType);
    int invSize = (results.size() + 8) / 9 * 9 > 63 ? 63 : (results.size() + 8) / 9 * 9;
    Inventory inv = Bukkit.createInventory(null, invSize, inventoryTitle + ChatColor.RED + playerName);
    int count = 0;
    for (Query query : results) {
        if (count >= invSize) {
            break;
        }
        // Get icon of this stat type
        Material iconMaterial = plugin.getStatisticDescriptionConfig().getIconMaterial(statType);
        // Create an itemstack to show in the inventory
        ItemStack itemStack = new ItemStack(iconMaterial);
        itemStack.setAmount(1);
        String displayName = plugin.getStatisticDescriptionConfig().getHumanFriendlyTitle(statType);
        // Get item meta to add messages
        ItemMeta itemMeta = itemStack.getItemMeta();
        // Set display name to the human friendly name of this statistic
        itemMeta.setDisplayName(displayName);
        // Create a list of messages shown when hovering over the item
        List<String> messages = new ArrayList<>();
        String highDetailDescription = ChatColor.stripColor(DescriptionMatcher.getHighDetailDescription(query, statType));
        if (highDetailDescription != null) {
            messages.addAll(fitTextToScreen(highDetailDescription));
        }
        itemMeta.setLore(messages);
        itemStack.setItemMeta(itemMeta);
        inv.setItem(count, itemStack);
        count++;
    }
    return inv;
}
Also used : Query(me.staartvin.statz.database.datatype.Query) PlayerInfo(me.staartvin.statz.datamanager.player.PlayerInfo) Material(org.bukkit.Material) ItemStack(org.bukkit.inventory.ItemStack) Inventory(org.bukkit.inventory.Inventory) ItemMeta(org.bukkit.inventory.meta.ItemMeta)

Example 43 with Query

use of me.staartvin.statz.database.datatype.Query in project Statz by Staartvin.

the class GUIManager method getStatisticsListInventory.

/**
 * Get an inventory that represents the graphical form of /statz list.
 * It includes all data that has been recorded by Statz grouped per stat.
 * @param uuid UUID of the player to check
 * @param playerName Name of the player to check
 * @return An inventory that shows the data of the given player.
 */
public Inventory getStatisticsListInventory(UUID uuid, String playerName) {
    PlayerInfo data = plugin.getDataManager().getPlayerInfo(uuid);
    if (data == null) {
        data = plugin.getDataManager().loadPlayerData(uuid);
    }
    int count = 0;
    Map<Integer, ItemStack> slots = new HashMap<>();
    for (PlayerStat statType : data.getStatistics()) {
        // Skip Players statistic
        if (statType == PlayerStat.PLAYERS) {
            continue;
        }
        List<Query> storedRows = data.getRows(statType);
        // Get icon of this stat type
        Material iconMaterial = plugin.getStatisticDescriptionConfig().getIconMaterial(statType);
        // Create an itemstack to show in the inventory
        ItemStack itemStack = new ItemStack(iconMaterial);
        itemStack.setAmount(1);
        String displayName = plugin.getStatisticDescriptionConfig().getHumanFriendlyTitle(statType);
        // Get item meta to add messages
        ItemMeta itemMeta = itemStack.getItemMeta();
        // Set display name to the human friendly name of this statistic
        itemMeta.setDisplayName(displayName);
        // Create a list of messages shown when hovering over the item
        List<String> messages = new ArrayList<>();
        if (storedRows == null || storedRows.isEmpty()) {
            messages.add(ChatColor.RED + "No information about " + playerName + "!");
            // Don't do anything when result is empty.
            continue;
        } else {
            // Create temp playerinfo object to obtain description
            PlayerInfo tempInfo = new PlayerInfo(uuid);
            tempInfo.setData(statType, storedRows);
            String totalDescription = ChatColor.stripColor(DescriptionMatcher.getTotalDescription(tempInfo, statType));
            if (totalDescription != null) {
                messages.add(ChatColor.YELLOW + totalDescription);
            }
            if (statType != PlayerStat.JOINS && statType != PlayerStat.VOTES) {
                messages.add("");
                messages.add(ChatColor.RED + "Click me for more info!");
            }
        }
        itemMeta.setLore(messages);
        itemStack.setItemMeta(itemMeta);
        // Store in slots.
        slots.put(count, itemStack);
        // inv.setItem(count, itemStack);
        count++;
    }
    Inventory inv = Bukkit.createInventory(null, (slots.size() + 8) / 9 * 9, inventoryTitle + ChatColor.RED + playerName);
    for (Map.Entry<Integer, ItemStack> entry : slots.entrySet()) {
        inv.setItem(entry.getKey(), entry.getValue());
    }
    return inv;
}
Also used : Query(me.staartvin.statz.database.datatype.Query) PlayerStat(me.staartvin.statz.datamanager.player.PlayerStat) Material(org.bukkit.Material) PlayerInfo(me.staartvin.statz.datamanager.player.PlayerInfo) ItemStack(org.bukkit.inventory.ItemStack) ItemMeta(org.bukkit.inventory.meta.ItemMeta) Inventory(org.bukkit.inventory.Inventory)

Example 44 with Query

use of me.staartvin.statz.database.datatype.Query in project Statz by Staartvin.

the class ListCommand method onCommand.

@Override
public boolean onCommand(final CommandSender sender, final Command cmd, final String label, String[] args) {
    // Target playername
    String playerName = null;
    // Target stat type
    String statType = null;
    // Target UUID
    UUID uuid = null;
    // Target page number
    int pageNumber = 1;
    boolean hasGivenPlayerName = false;
    // If this is true, a list of stats will be shown, otherwise we'll show a specific stat.
    boolean showList = true;
    // If user uses 'force', we don't check whether the player has already played before, we just obey.
    boolean useForce = false;
    List<String> fakeArgs = new ArrayList<>();
    for (String arg : args) {
        if (arg.trim().equalsIgnoreCase("force")) {
            useForce = true;
        } else {
            fakeArgs.add(arg);
        }
    }
    args = fakeArgs.toArray(new String[] {});
    if (args.length >= 3) {
        // [0] = 'list', [1] = 'player name', [2] = 'stat name', [3] = 'page number'
        playerName = args[1];
        hasGivenPlayerName = true;
        // Page number not a stat type
        if (StringUtils.isNumeric(args[2])) {
            try {
                pageNumber = Integer.parseInt(args[2]);
            } catch (NumberFormatException e) {
                // Third argument was not a number, so give an error
                sender.sendMessage(Lang.INCORRECT_PAGE_NUMBER.getConfigValue());
                return true;
            }
        } else {
            statType = args[2];
            showList = false;
        }
    } else if (args.length == 2) {
        // If [1] is a number, the input was a page number, not a name
        if (StringUtils.isNumeric(args[1])) {
            try {
                pageNumber = Integer.parseInt(args[1]);
            } catch (NumberFormatException e) {
                // [1] argument was not a number, so give an error
                sender.sendMessage(Lang.INCORRECT_PAGE_NUMBER.getConfigValue());
                return true;
            }
        } else {
            // [1] is a name
            playerName = args[1];
            hasGivenPlayerName = true;
        }
    } else /*if (args.length == 1)*/
    {
        // No player name given and no page number
        // Default to page number = 1
        hasGivenPlayerName = false;
    }
    if (!hasGivenPlayerName) {
        // Only players are allowed to check themselves.
        if (!(sender instanceof Player)) {
            sender.sendMessage(Lang.INCORRECT_COMMAND_USAGE.getConfigValue("/statz list <player>"));
            return true;
        }
        // Check permissions
        if (!sender.hasPermission("statz.list.self")) {
            sender.sendMessage(Lang.INSUFFICIENT_PERMISSIONS.getConfigValue("statz.list.self"));
            return true;
        }
        Player player = (Player) sender;
        playerName = player.getName();
        uuid = player.getUniqueId();
    } else {
        @SuppressWarnings("deprecation") OfflinePlayer targetPlayer = plugin.getServer().getOfflinePlayer(playerName);
        // Need to bring this back
        if (!targetPlayer.hasPlayedBefore() && !useForce) {
            sender.sendMessage(Lang.PLAYER_NEVER_PLAYED_BEFORE.getConfigValue(playerName));
            return true;
        }
        if (targetPlayer.isOnline()) {
            Player player = targetPlayer.getPlayer();
            playerName = player.getName();
            uuid = player.getUniqueId();
        } else {
            playerName = targetPlayer.getName();
            uuid = targetPlayer.getUniqueId();
        }
        if (playerName == null || uuid == null) {
            sender.sendMessage(ChatColor.RED + "Could not find player!");
            return true;
        }
        if (!sender.getName().equalsIgnoreCase(playerName)) {
            // Check permissions
            if (!sender.hasPermission("statz.list.others")) {
                sender.sendMessage(Lang.INSUFFICIENT_PERMISSIONS.getConfigValue("statz.list.others"));
                return true;
            }
        }
    }
    // Show a list of all stats
    if (showList) {
        // Check to see if we show GUI or not.
        if (plugin.getConfigHandler().isStatzGUIenabled() && sender instanceof Player) {
            Player player = (Player) sender;
            plugin.getGUIManager().showInventory(player, plugin.getGUIManager().getStatisticsListInventory(uuid, playerName));
        } else {
            // Only show text.
            plugin.getServer().getScheduler().runTaskAsynchronously(plugin, new Runnable() {

                private String playerName;

                private UUID uuid;

                private int pageNumber;

                private Runnable init(String playerName, UUID uuid, int pageNumber) {
                    this.playerName = playerName;
                    this.uuid = uuid;
                    this.pageNumber = pageNumber - 1;
                    return this;
                }

                public void run() {
                    long startTime = System.currentTimeMillis();
                    plugin.getDataManager().sendStatisticsList(sender, playerName, uuid, pageNumber, Arrays.asList(PlayerStat.values()));
                    plugin.debugMessage("/statz list command took " + (System.currentTimeMillis() - startTime) + " ms.");
                }
            }.init(playerName, uuid, pageNumber));
        }
    } else {
        // Show specific stat
        PlayerStat stat = null;
        for (PlayerStat s : PlayerStat.values()) {
            if (s.toString().equalsIgnoreCase(statType)) {
                stat = s;
                break;
            }
        }
        if (stat == null) {
            sender.sendMessage(Lang.INCORRECT_STAT_TYPE.getConfigValue(statType));
            return true;
        }
        plugin.getServer().getScheduler().runTaskAsynchronously(plugin, new Runnable() {

            private String playerName;

            private UUID uuid;

            private PlayerStat statType;

            private Runnable init(String playerName, UUID uuid, PlayerStat statType) {
                this.playerName = playerName;
                this.uuid = uuid;
                this.statType = statType;
                return this;
            }

            public void run() {
                long startTime = System.currentTimeMillis();
                List<String> messages = new ArrayList<>();
                PlayerInfo info = plugin.getDataManager().getPlayerInfo(uuid, statType);
                sender.sendMessage(Lang.SPECIFIC_STAT_HEADER.getConfigValue(statType, playerName));
                // Only use valid info.
                if (statType == PlayerStat.PLAYERS) {
                    sender.sendMessage(Lang.NO_STATISTICS_TO_SHOW.getConfigValue());
                    return;
                }
                for (Query query : info.getDataOfPlayerStat(statType)) {
                    // Invalid query
                    if (query == null)
                        continue;
                    messages.add(StatzUtil.getInfoString(query, statType, playerName));
                }
                for (String message : messages) {
                    sender.sendMessage(message);
                }
                plugin.debugMessage("/statz list command took " + (System.currentTimeMillis() - startTime) + " ms.");
            }
        }.init(playerName, uuid, stat));
    }
    return true;
}
Also used : Player(org.bukkit.entity.Player) OfflinePlayer(org.bukkit.OfflinePlayer) Query(me.staartvin.statz.database.datatype.Query) ArrayList(java.util.ArrayList) PlayerStat(me.staartvin.statz.datamanager.player.PlayerStat) PlayerInfo(me.staartvin.statz.datamanager.player.PlayerInfo) OfflinePlayer(org.bukkit.OfflinePlayer) UUID(java.util.UUID)

Aggregations

Query (me.staartvin.statz.database.datatype.Query)44 Test (org.junit.Test)25 PlayerInfo (me.staartvin.statz.datamanager.player.PlayerInfo)13 PlayerStat (me.staartvin.statz.datamanager.player.PlayerStat)11 ArrayList (java.util.ArrayList)8 UUID (java.util.UUID)6 RowRequirement (me.staartvin.statz.database.datatype.RowRequirement)4 HashMap (java.util.HashMap)2 Material (org.bukkit.Material)2 Player (org.bukkit.entity.Player)2 Inventory (org.bukkit.inventory.Inventory)2 ItemStack (org.bukkit.inventory.ItemStack)2 ItemMeta (org.bukkit.inventory.meta.ItemMeta)2 BufferedWriter (java.io.BufferedWriter)1 File (java.io.File)1 FileWriter (java.io.FileWriter)1 IOException (java.io.IOException)1 Date (java.util.Date)1 Map (java.util.Map)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1