Add additional error handling for player health and statistic updating

feat/data-edit-commands
William 2 years ago
parent 101e0c11d7
commit 99483387f1

@ -106,7 +106,7 @@ public class BukkitPlayer extends OnlineUser {
if (healthToSet < 1) { if (healthToSet < 1) {
Bukkit.getScheduler().runTask(BukkitHuskSync.getInstance(), () -> player.setHealth(healthToSet)); Bukkit.getScheduler().runTask(BukkitHuskSync.getInstance(), () -> player.setHealth(healthToSet));
} else { } else {
player.setHealth(healthToSet); player.setHealth(Math.min(healthToSet, currentMaxHealth));
} }
} }
@ -355,32 +355,52 @@ public class BukkitPlayer extends OnlineUser {
@Override @Override
public CompletableFuture<Void> setStatistics(@NotNull StatisticsData statisticsData) { public CompletableFuture<Void> setStatistics(@NotNull StatisticsData statisticsData) {
return CompletableFuture.runAsync(() -> { return CompletableFuture.runAsync(() -> {
// Set untyped statistics // Set generic statistics
for (String statistic : statisticsData.untypedStatistics.keySet()) { for (String statistic : statisticsData.untypedStatistics.keySet()) {
player.setStatistic(Statistic.valueOf(statistic), statisticsData.untypedStatistics.get(statistic)); try {
player.setStatistic(Statistic.valueOf(statistic), statisticsData.untypedStatistics.get(statistic));
} catch (IllegalArgumentException e) {
BukkitHuskSync.getInstance().getLogger().log(Level.WARNING,
"Failed to set generic statistic " + statistic + " for " + username);
}
} }
// Set block statistics // Set block statistics
for (String statistic : statisticsData.blockStatistics.keySet()) { for (String statistic : statisticsData.blockStatistics.keySet()) {
for (String blockMaterial : statisticsData.blockStatistics.get(statistic).keySet()) { for (String blockMaterial : statisticsData.blockStatistics.get(statistic).keySet()) {
player.setStatistic(Statistic.valueOf(statistic), Material.valueOf(blockMaterial), try {
statisticsData.blockStatistics.get(statistic).get(blockMaterial)); player.setStatistic(Statistic.valueOf(statistic), Material.valueOf(blockMaterial),
statisticsData.blockStatistics.get(statistic).get(blockMaterial));
} catch (IllegalArgumentException e) {
BukkitHuskSync.getInstance().getLogger().log(Level.WARNING,
"Failed to set " + blockMaterial + " statistic " + statistic + " for " + username);
}
} }
} }
// Set item statistics // Set item statistics
for (String statistic : statisticsData.itemStatistics.keySet()) { for (String statistic : statisticsData.itemStatistics.keySet()) {
for (String itemMaterial : statisticsData.itemStatistics.get(statistic).keySet()) { for (String itemMaterial : statisticsData.itemStatistics.get(statistic).keySet()) {
player.setStatistic(Statistic.valueOf(statistic), Material.valueOf(itemMaterial), try {
statisticsData.itemStatistics.get(statistic).get(itemMaterial)); player.setStatistic(Statistic.valueOf(statistic), Material.valueOf(itemMaterial),
statisticsData.itemStatistics.get(statistic).get(itemMaterial));
} catch (IllegalArgumentException e) {
BukkitHuskSync.getInstance().getLogger().log(Level.WARNING,
"Failed to set " + itemMaterial + " statistic " + statistic + " for " + username);
}
} }
} }
// Set entity statistics // Set entity statistics
for (String statistic : statisticsData.entityStatistics.keySet()) { for (String statistic : statisticsData.entityStatistics.keySet()) {
for (String entityType : statisticsData.entityStatistics.get(statistic).keySet()) { for (String entityType : statisticsData.entityStatistics.get(statistic).keySet()) {
player.setStatistic(Statistic.valueOf(statistic), EntityType.valueOf(entityType), try {
statisticsData.entityStatistics.get(statistic).get(entityType)); player.setStatistic(Statistic.valueOf(statistic), EntityType.valueOf(entityType),
statisticsData.entityStatistics.get(statistic).get(entityType));
} catch (IllegalArgumentException e) {
BukkitHuskSync.getInstance().getLogger().log(Level.WARNING,
"Failed to set " + entityType + " statistic " + statistic + " for " + username);
}
} }
} }
}); });

@ -11,7 +11,7 @@ import java.util.Map;
public class StatisticsData { public class StatisticsData {
/** /**
* Map of untyped statistic names to their values * Map of generic statistic names to their values
*/ */
@SerializedName("untyped_statistics") @SerializedName("untyped_statistics")
public Map<String, Integer> untypedStatistics; public Map<String, Integer> untypedStatistics;

Loading…
Cancel
Save