Search in sources :

Example 41 with PlayerStat

use of me.staartvin.statz.datamanager.player.PlayerStat 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 42 with PlayerStat

use of me.staartvin.statz.datamanager.player.PlayerStat in project Statz by Staartvin.

the class GUIManager method onClickInvSlot.

/**
 * Redirect Player to correct inventory if he/she clicks on an ItemStack.
 * @param event
 */
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void onClickInvSlot(final InventoryClickEvent event) {
    Inventory inv = event.getClickedInventory();
    // Clicked outside inv window.
    if (inv == null) {
        return;
    }
    // Check if clicking a Statz GUI window
    if (!inv.getTitle().contains(inventoryTitle)) {
        return;
    }
    String targetPlayer = ChatColor.stripColor(inv.getTitle().replace(inventoryTitle, "").trim());
    // Check if we can find the target player of this inventory.
    if (targetPlayer == null || targetPlayer.equalsIgnoreCase("")) {
        return;
    }
    event.setCancelled(true);
    int slot = event.getSlot();
    ItemStack clickedItem = event.getCurrentItem();
    if (clickedItem == null) {
        return;
    }
    if (!clickedItem.hasItemMeta()) {
        return;
    }
    String displayName = clickedItem.getItemMeta().getDisplayName();
    if (displayName == null) {
        return;
    }
    PlayerStat statType = null;
    for (PlayerStat stat : PlayerStat.values()) {
        String humanName = plugin.getStatisticDescriptionConfig().getHumanFriendlyTitle(stat);
        if (humanName == null) {
            continue;
        }
        if (humanName.equalsIgnoreCase(displayName)) {
            statType = stat;
            break;
        }
    }
    // We couldn't find what stat the player clicked on.
    if (statType == null) {
        return;
    }
    boolean itemIsClickable = false;
    // Go through messages of item and see if it is clickable.
    for (String message : clickedItem.getItemMeta().getLore()) {
        if (message.contains("Click me for more info!")) {
            itemIsClickable = true;
        }
    }
    // If it is not clickable, ignore the click.
    if (!itemIsClickable) {
        return;
    }
    // Get UUID of target player
    UUID targetUUID;
    OfflinePlayer offlinePlayer = plugin.getServer().getOfflinePlayer(targetPlayer);
    if (offlinePlayer == null || offlinePlayer.getUniqueId() == null) {
        return;
    }
    targetUUID = offlinePlayer.getUniqueId();
    // Close currently opened inventory
    event.getWhoClicked().closeInventory();
    // Open stat specific inventory.
    Inventory specificInv = getSpecificStatisticInventory(targetUUID, statType, targetPlayer);
    showInventory((Player) event.getWhoClicked(), specificInv);
}
Also used : PlayerStat(me.staartvin.statz.datamanager.player.PlayerStat) OfflinePlayer(org.bukkit.OfflinePlayer) ItemStack(org.bukkit.inventory.ItemStack) Inventory(org.bukkit.inventory.Inventory) EventHandler(org.bukkit.event.EventHandler)

Example 43 with PlayerStat

use of me.staartvin.statz.datamanager.player.PlayerStat 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)

Example 44 with PlayerStat

use of me.staartvin.statz.datamanager.player.PlayerStat in project Statz by Staartvin.

the class ConfigHandler method getDisabledStats.

public List<PlayerStat> getDisabledStats() {
    List<String> disabledStatsString = plugin.getConfig().getStringList("disabled statistics");
    List<PlayerStat> disabledStats = new ArrayList<>();
    for (String disabledStatString : disabledStatsString) {
        PlayerStat stat = null;
        try {
            stat = PlayerStat.valueOf(disabledStatString.toUpperCase().replace(" ", "_"));
        } catch (IllegalArgumentException e) {
        }
        if (stat == null)
            continue;
        disabledStats.add(stat);
    }
    return disabledStats;
}
Also used : PlayerStat(me.staartvin.statz.datamanager.player.PlayerStat) ArrayList(java.util.ArrayList)

Aggregations

PlayerStat (me.staartvin.statz.datamanager.player.PlayerStat)44 Player (org.bukkit.entity.Player)30 EventHandler (org.bukkit.event.EventHandler)29 Query (me.staartvin.statz.database.datatype.Query)11 PlayerInfo (me.staartvin.statz.datamanager.player.PlayerInfo)11 UUID (java.util.UUID)7 ItemStack (org.bukkit.inventory.ItemStack)6 Test (org.junit.Test)5 ArrayList (java.util.ArrayList)3 OfflinePlayer (org.bukkit.OfflinePlayer)3 Entity (org.bukkit.entity.Entity)3 Block (org.bukkit.block.Block)2 EntityDamageByEntityEvent (org.bukkit.event.entity.EntityDamageByEntityEvent)2 Inventory (org.bukkit.inventory.Inventory)2 Map (java.util.Map)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 Material (org.bukkit.Material)1 Arrow (org.bukkit.entity.Arrow)1 Item (org.bukkit.entity.Item)1 Vehicle (org.bukkit.entity.Vehicle)1