use of com.gmail.stefvanschiedev.buildinggame.utils.stats.Stat in project buildinggame by stefvanschie.
the class StatManager method saveToFile.
/**
* Saves all statistics to the stats.yml file
*
* @since 2.2.0
*/
public synchronized void saveToFile() {
YamlConfiguration config = SettingsManager.getInstance().getConfig();
YamlConfiguration stats = SettingsManager.getInstance().getStats();
this.stats.forEach((statType, statList) -> statList.forEach(stat -> {
String type = statType.toString().toLowerCase(Locale.getDefault());
if (statType.isEnabled(config)) {
stats.set(stat.getPlayer().getUniqueId() + "." + type, stat.getValue());
}
}));
SettingsManager.getInstance().save();
}
use of com.gmail.stefvanschiedev.buildinggame.utils.stats.Stat in project buildinggame by stefvanschie.
the class StatManager method registerStat.
/**
* Registers the stat with the given player, type and value unless the config has the given stat type disabled
*
* @param player the player who obtained the statistic
* @param type the type of statistic
* @param value the value of the statistic
* @since 2.2.0
*/
@Contract("_, null, _ -> fail")
public synchronized void registerStat(OfflinePlayer player, @NotNull StatType type, int value) {
YamlConfiguration config = SettingsManager.getInstance().getConfig();
if (!type.isEnabled(config)) {
return;
}
Stat oldStat = getStat(player, type);
if (oldStat != null)
stats.get(type).remove(oldStat);
if (!stats.containsKey(type))
stats.put(type, Collections.synchronizedList(new ArrayList<>()));
List<Stat> statsByType = stats.get(type);
int size = statsByType.size();
int index = 0;
for (; index <= size; index++) {
if (index == size)
break;
if (statsByType.get(index).getValue() < value)
break;
}
Stat newStat = new Stat(player, value);
statsByType.add(index, newStat);
TopStatHologram.update(type);
if (player.isOnline()) {
Achievement.getAchievements(type).stream().filter(achievement -> !achievement.checkConditions(type, oldStat) && achievement.checkConditions(type, newStat)).forEach(achievement -> achievement.grant(player.getPlayer()));
}
}
use of com.gmail.stefvanschiedev.buildinggame.utils.stats.Stat in project buildinggame by stefvanschie.
the class StatManager method saveToDatabase.
/**
* Saves all statistics to the database
*
* @since 4.0.0
*/
public synchronized void saveToDatabase() {
YamlConfiguration config = SettingsManager.getInstance().getConfig();
this.stats.entrySet().stream().filter(entry -> entry.getKey().isEnabled(config)).forEach(entry -> entry.getValue().forEach(stat -> {
getMySQLDatabase().setStat(stat.getPlayer().getUniqueId().toString(), entry.getKey(), stat.getValue());
}));
}
use of com.gmail.stefvanschiedev.buildinggame.utils.stats.Stat in project buildinggame by stefvanschie.
the class StatSignUpdater method run.
/**
* {@inheritDoc}
*/
@Override
public void run() {
YamlConfiguration config = SettingsManager.getInstance().getConfig();
YamlConfiguration messages = SettingsManager.getInstance().getMessages();
Map<StatSign, String[]> signTexts = new HashMap<>();
SignManager.getInstance().getStatSigns().forEach(sign -> {
String[] lines = new String[4];
if (sign.getType().isEnabled(config)) {
var stats = StatManager.getInstance().getStats(sign.getType());
if (stats == null)
return;
OfflinePlayer offlinePlayer = null;
int value = -1;
if (stats.size() > sign.getNumber() - 1) {
Stat stat = stats.get(sign.getNumber() - 1);
offlinePlayer = stat.getPlayer();
value = stat.getValue();
}
for (int i = 0; i < 4; i++) lines[i] = replace(MessageManager.translate(messages.getString("signs.stat." + sign.getType().toString().toLowerCase(Locale.getDefault()).replace("_", "-") + ".line-" + (i + 1))), sign, offlinePlayer, value);
} else {
lines[0] = "";
lines[1] = ChatColor.RED + "Stat type";
lines[2] = ChatColor.RED + "is disabled";
lines[3] = "";
}
signTexts.put(sign, lines);
});
if (!Main.getInstance().isEnabled())
return;
new BukkitRunnable() {
@Override
public void run() {
signTexts.forEach((statSign, lines) -> {
PotentialBlockPosition blockPos = statSign.getBlockPosition();
Block block = blockPos.getBlock();
if (block == null || !(block.getState() instanceof Sign)) {
return;
}
Sign sign = (Sign) block.getState();
var length = lines.length;
for (var i = 0; i < length; i++) sign.setLine(i, lines[i]);
sign.update();
});
}
}.runTask(Main.getInstance());
}
Aggregations