use of com.gmail.stefvanschiedev.buildinggame.utils.stats.StatType in project buildinggame by stefvanschie.
the class StatManager method setup.
/**
* Loads/Reloads all statistics and configures the database if necessary
*
* @since 2.2.0
*/
public void setup() {
YamlConfiguration config = SettingsManager.getInstance().getConfig();
YamlConfiguration stats = SettingsManager.getInstance().getStats();
if (config.getBoolean("stats.database.enable")) {
database = new MySQLDatabase(Main.getInstance());
if (database.setup()) {
database.getAllPlayers().forEach(uuid -> {
for (StatType statType : StatType.values()) registerStat(Bukkit.getOfflinePlayer(uuid), statType, database.getStat(uuid.toString(), statType.toString().toLowerCase(Locale.getDefault())));
});
return;
}
database = null;
Main.getInstance().getLogger().warning("Database usage failed; returning to flat-file storage");
}
stats.getKeys(false).forEach(uuid -> {
OfflinePlayer player = Bukkit.getOfflinePlayer(UUID.fromString(uuid));
stats.getConfigurationSection(uuid).getKeys(false).forEach(stat -> {
if (stat.equalsIgnoreCase("plays") && StatType.PLAYS.isEnabled(config)) {
registerStat(player, StatType.PLAYS, stats.getInt(uuid + '.' + stat));
} else if (stat.equalsIgnoreCase("first") && StatType.FIRST.isEnabled(config)) {
registerStat(player, StatType.FIRST, stats.getInt(uuid + '.' + stat));
} else if (stat.equalsIgnoreCase("second") && StatType.SECOND.isEnabled(config)) {
registerStat(player, StatType.SECOND, stats.getInt(uuid + '.' + stat));
} else if (stat.equalsIgnoreCase("third") && StatType.THIRD.isEnabled(config)) {
registerStat(player, StatType.THIRD, stats.getInt(uuid + '.' + stat));
} else if (stat.equalsIgnoreCase("broken") && StatType.BROKEN.isEnabled(config)) {
registerStat(player, StatType.BROKEN, stats.getInt(uuid + '.' + stat));
} else if (stat.equalsIgnoreCase("placed") && StatType.PLACED.isEnabled(config)) {
registerStat(player, StatType.PLACED, stats.getInt(uuid + '.' + stat));
} else if (stat.equalsIgnoreCase("walked") && StatType.WALKED.isEnabled(config)) {
registerStat(player, StatType.WALKED, stats.getInt(uuid + '.' + stat));
} else if (stat.equalsIgnoreCase("points_given") && StatType.POINTS_GIVEN.isEnabled(config)) {
registerStat(player, StatType.POINTS_GIVEN, stats.getInt(uuid + '.' + stat));
} else if (stat.equalsIgnoreCase("points_received") && StatType.POINTS_RECEIVED.isEnabled(config)) {
registerStat(player, StatType.POINTS_RECEIVED, stats.getInt(uuid + '.' + stat));
}
});
});
}
use of com.gmail.stefvanschiedev.buildinggame.utils.stats.StatType in project buildinggame by stefvanschie.
the class StatSignCreate method onSignChange.
/**
* Handles players creating statistic signs
*
* @param e an event indicating that a sign's text has changed
* @see SignChangeEvent
* @since 3.1.0
*/
@EventHandler
public static void onSignChange(SignChangeEvent e) {
YamlConfiguration messages = SettingsManager.getInstance().getMessages();
YamlConfiguration signs = SettingsManager.getInstance().getSigns();
if (!e.getLine(0).equalsIgnoreCase("[buildinggame]"))
return;
String lineOne = e.getLine(1);
if (!lineOne.equalsIgnoreCase("stat") && !lineOne.equalsIgnoreCase("statistic"))
return;
var player = e.getPlayer();
String lineTwo = e.getLine(2);
StatType type;
try {
type = StatType.valueOf(lineTwo.toUpperCase(Locale.getDefault()));
} catch (IllegalArgumentException ex) {
MessageManager.getInstance().send(player, ChatColor.RED + "'" + lineTwo + "' isn't a valid statistic");
return;
}
String lineThree = e.getLine(3);
int number;
try {
number = Integer.parseInt(lineThree);
} catch (NumberFormatException ex) {
MessageManager.getInstance().send(player, ChatColor.RED + "'" + lineThree + "' isn't a valid number");
return;
}
if (!player.hasPermission("bg.sign.create")) {
MessageManager.getInstance().send(player, messages.getString("global.permissionNode"));
return;
}
int i = 0;
for (var string : signs.getKeys(false)) {
try {
i = Integer.parseInt(string);
} catch (NumberFormatException ignore) {
}
}
i++;
var location = e.getBlock().getLocation();
signs.set(i + ".number", number);
signs.set(i + ".stat", type.toString());
signs.set(i + ".type", "stat");
signs.set(i + ".world", location.getWorld().getName());
signs.set(i + ".x", location.getBlockX());
signs.set(i + ".y", location.getBlockY());
signs.set(i + ".z", location.getBlockZ());
SettingsManager.getInstance().save();
SignManager.getInstance().setup();
e.setCancelled(true);
}
use of com.gmail.stefvanschiedev.buildinggame.utils.stats.StatType 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.StatType 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.StatType in project buildinggame by stefvanschie.
the class Achievement method loadAchievements.
/**
* Loads all achievements from the config.yml
*
* @since 6.3.0
*/
public static void loadAchievements() {
YamlConfiguration config = SettingsManager.getInstance().getConfig();
config.getConfigurationSection("achievements").getKeys(false).forEach(achievementSection -> {
StatType statType = StatType.valueOf(config.getString("achievements." + achievementSection + ".stat").toUpperCase(Locale.getDefault()));
int amountRequired = config.getInt("achievements." + achievementSection + ".amount-required");
String message = config.getString("achievements." + achievementSection + ".message");
ACHIEVEMENTS.add(new Achievement(statType, amountRequired, message));
});
}
Aggregations