Search in sources :

Example 1 with PlayerInfo

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

the class API method getTotalOf.

/**
 * Get the total count for a stat of a specific player on a specific world.
 * <br>The worldName can also be omitted to get the total of all worlds.
 * <br><br><b>This method will return null if Statz does not have info about this player regarding the specific stat.</b>
 * @param statType the {@link PlayerStat} to get info of.
 * @param uuid The UUID of the Player.
 * @param worldName Name of the world to get the info from, can also be null to find the total on all worlds.
 * @return the total count of a stat. E.g. the total amount of killed players on a world (or on all worlds).
 */
public Double getTotalOf(final PlayerStat statType, final UUID uuid, final String worldName) {
    PlayerInfo info = plugin.getDataManager().getPlayerInfo(uuid, statType);
    double value = 0;
    List<Query> results = info.getDataOfPlayerStat(statType);
    if (results == null || results.isEmpty())
        return value;
    if (worldName != null) {
        // Add every value that is in the proper world
        for (Query result : results) {
            if (result.getValue("world") != null && result.getValue("world").toString().equalsIgnoreCase(worldName)) {
                value += Double.parseDouble(result.getValue("value").toString());
            }
        }
    } else {
        // Add every value regardless of the world
        for (Query result : results) {
            value += Double.parseDouble(result.getValue("value").toString());
        }
    }
    return value;
}
Also used : Query(me.staartvin.statz.database.datatype.Query) PlayerInfo(me.staartvin.statz.datamanager.player.PlayerInfo)

Example 2 with PlayerInfo

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

the class CachingManager method addCachedQuery.

/**
 * Add a single query to the cache of a player.
 *
 * @param statType   Type of statistic the query belongs to
 * @param queryToAdd Query to add
 * @param uuid       UUID of the player
 * @throws IllegalArgumentException if the given query is null or the uuid is null.
 */
public void addCachedQuery(PlayerStat statType, Query queryToAdd, UUID uuid) throws IllegalArgumentException {
    if (queryToAdd == null) {
        throw new IllegalArgumentException("Query cannot be null.");
    }
    if (uuid == null) {
        throw new IllegalArgumentException("UUID cannot be null.");
    }
    PlayerInfo info = new PlayerInfo(uuid);
    info.addRow(statType, queryToAdd);
    this.addCachedData(uuid, info);
}
Also used : PlayerInfo(me.staartvin.statz.datamanager.player.PlayerInfo)

Example 3 with PlayerInfo

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

the class CachingManager method addCachedData.

/**
 * Add new data to the cache. The new data will be merged with the already existing cached data.
 * The merging of cached and new data can be a quite intensive process and so should preferably be run
 * asynchronously.
 * <br>
 * If no cached data exists, this will just store the given data as new data.
 *
 * @param uuid        UUID of player that the data is for
 * @param dataToCache new data to store
 * @throws IllegalArgumentException if given data is null
 */
public void addCachedData(UUID uuid, PlayerInfo dataToCache) throws IllegalArgumentException {
    if (dataToCache == null) {
        throw new IllegalArgumentException("Data to cache is null.");
    }
    PlayerInfo cachedData = getCachedPlayerData(uuid);
    // There is no cached data, so we just register new data.
    if (cachedData == null) {
        registerCachedData(uuid, dataToCache);
        return;
    }
    // Resolve conflicts and update cache.
    PlayerInfo resolvedCache = cachedData.resolveConflicts(dataToCache);
    // Update cache with new cached data.
    this.registerCachedData(uuid, resolvedCache);
}
Also used : PlayerInfo(me.staartvin.statz.datamanager.player.PlayerInfo)

Example 4 with PlayerInfo

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

the class UpdatePlayerCacheTask method run.

@Override
public void run() {
    PlayerInfo cachedData = new PlayerInfo(uuid);
    for (PlayerStat statType : PlayerStat.values()) {
        if (statType.equals(PlayerStat.PLAYERS)) {
            continue;
        }
        PlayerInfo databaseInfo = plugin.getDataManager().getFreshPlayerInfo(uuid, statType);
        // User is not loaded, or there is no cache so we don't bother overwriting the cache.
        if (databaseInfo == null) {
            continue;
        }
        // Remove all 'id' columns to prevent leakage from the database.
        for (Query query : databaseInfo.getRows()) {
            query.removeColumn("id");
        }
        cachedData = cachedData.resolveConflicts(databaseInfo);
    }
    plugin.debugMessage("Updated cache of " + uuid);
    // Store into cache.
    plugin.getCachingManager().registerCachedData(uuid, cachedData);
}
Also used : Query(me.staartvin.statz.database.datatype.Query) PlayerInfo(me.staartvin.statz.datamanager.player.PlayerInfo) PlayerStat(me.staartvin.statz.datamanager.player.PlayerStat)

Example 5 with PlayerInfo

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

the class DataManager method getPlayerInfo.

/**
 * Get all known data of a player for a given statistic. This is different from {@link #getPlayerInfo(UUID)} as
 * that method grabs all data of a player, while this method only retrieves data about a given statistic.
 *
 * @param uuid     UUID of the player
 * @param statType Type of statistic to get data of
 *
 * @return PlayerInfo object with data of the requested player and the given statistic
 *
 * @throws IllegalArgumentException if the given uuid is null.
 */
public PlayerInfo getPlayerInfo(UUID uuid, PlayerStat statType) throws IllegalArgumentException {
    if (uuid == null) {
        throw new IllegalArgumentException("UUID cannot be null.");
    }
    if (!this.isPlayerLoaded(uuid, statType)) {
        return null;
    }
    PlayerInfo info = this.getPlayerInfo(uuid);
    if (info == null) {
        return null;
    }
    // Create a new PlayerInfo object that only has the data of a given statistic.
    PlayerInfo newInfo = new PlayerInfo(uuid);
    List<Query> queriesStored = info.getDataOfPlayerStat(statType);
    // Don't store data that is null or empty.
    if (queriesStored != null && !queriesStored.isEmpty()) {
        newInfo.setData(statType, info.getDataOfPlayerStat(statType));
    }
    return newInfo;
}
Also used : Query(me.staartvin.statz.database.datatype.Query) PlayerInfo(me.staartvin.statz.datamanager.player.PlayerInfo)

Aggregations

PlayerInfo (me.staartvin.statz.datamanager.player.PlayerInfo)19 Query (me.staartvin.statz.database.datatype.Query)13 PlayerStat (me.staartvin.statz.datamanager.player.PlayerStat)11 UUID (java.util.UUID)6 Test (org.junit.Test)6 ArrayList (java.util.ArrayList)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 RowRequirement (me.staartvin.statz.database.datatype.RowRequirement)1 Table (me.staartvin.statz.database.datatype.Table)1 OfflinePlayer (org.bukkit.OfflinePlayer)1