fix: `Enum#valueOf` throwing on legacy stat-map conversion

feat/data-edit-commands
William 11 months ago
parent d4f61bd646
commit 8d2e5a6a52
No known key found for this signature in database

@ -48,6 +48,8 @@ import java.util.logging.Level;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import static net.william278.husksync.util.BukkitTypeMatcher.*;
public abstract class BukkitData implements Data {
@Override
@ -608,7 +610,7 @@ public abstract class BukkitData implements Data {
Map.Entry::getKey,
entry -> entry.getValue().entrySet().stream()
.flatMap(blockEntry -> {
Material material = Material.matchMaterial(blockEntry.getKey());
Material material = matchMaterial(blockEntry.getKey());
return material != null ? Stream.of(new AbstractMap.SimpleEntry<>(material, blockEntry.getValue())) : Stream.empty();
})
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue))
@ -622,7 +624,7 @@ public abstract class BukkitData implements Data {
Map.Entry::getKey,
entry -> entry.getValue().entrySet().stream()
.flatMap(itemEntry -> {
Material material = Material.matchMaterial(itemEntry.getKey());
Material material = matchMaterial(itemEntry.getKey());
return material != null ? Stream.of(new AbstractMap.SimpleEntry<>(material, itemEntry.getValue())) : Stream.empty();
})
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue))
@ -661,20 +663,6 @@ public abstract class BukkitData implements Data {
return new StatisticsMap(genericStats, blockStats, itemStats, entityStats);
}
@Nullable
private static Statistic matchStatistic(@NotNull String key) {
return Arrays.stream(Statistic.values())
.filter(stat -> stat.getKey().toString().equals(key))
.findFirst().orElse(null);
}
@Nullable
private static EntityType matchEntityType(@NotNull String key) {
return Arrays.stream(EntityType.values())
.filter(entityType -> entityType.getKey().toString().equals(key))
.findFirst().orElse(null);
}
@Override
public void apply(@NotNull BukkitUser user, @NotNull BukkitHuskSync plugin) throws IllegalStateException {
genericStatistics.forEach((stat, value) -> applyStat(user, stat, null, value));

@ -40,6 +40,8 @@ import java.util.Arrays;
import java.util.function.Consumer;
import java.util.logging.Level;
import static net.william278.husksync.util.BukkitTypeMatcher.matchMaterial;
/**
* Bukkit platform implementation of an {@link OnlineUser}
*/
@ -80,7 +82,7 @@ public class BukkitUser extends OnlineUser implements BukkitUserDataHolder {
public void sendToast(@NotNull MineDown title, @NotNull MineDown description,
@NotNull String iconMaterial, @NotNull String backgroundType) {
try {
final Material material = Material.matchMaterial(iconMaterial);
final Material material = matchMaterial(iconMaterial);
Toast.builder((BukkitHuskSync) plugin)
.setTitle(title.toComponent())
.setDescription(description.toComponent())

@ -19,6 +19,7 @@
package net.william278.husksync.util;
import com.google.common.collect.Maps;
import net.william278.husksync.HuskSync;
import net.william278.husksync.adapter.DataAdapter;
import net.william278.husksync.data.BukkitData;
@ -43,6 +44,8 @@ import java.time.OffsetDateTime;
import java.util.*;
import java.util.logging.Level;
import static net.william278.husksync.util.BukkitTypeMatcher.*;
public class BukkitLegacyConverter extends LegacyConverter {
public BukkitLegacyConverter(@NotNull HuskSync plugin) {
@ -197,36 +200,44 @@ public class BukkitLegacyConverter extends LegacyConverter {
@NotNull
private BukkitData.Statistics readStatisticMaps(@NotNull JSONObject untyped, @NotNull JSONObject blocks,
@NotNull JSONObject items, @NotNull JSONObject entities) {
final Map<Statistic, Integer> genericStats = new HashMap<>();
untyped.keys().forEachRemaining(stat -> genericStats.put(Statistic.valueOf(stat), untyped.getInt(stat)));
final Map<Statistic, Map<Material, Integer>> blockStats = new HashMap<>();
blocks.keys().forEachRemaining(stat -> {
final JSONObject blockStat = blocks.getJSONObject(stat);
final Map<Material, Integer> blockMap = new HashMap<>();
blockStat.keys().forEachRemaining(block -> blockMap.put(Material.valueOf(block), blockStat.getInt(block)));
blockStats.put(Statistic.valueOf(stat), blockMap);
});
// Read generic stats
final Map<Statistic, Integer> genericStats = Maps.newHashMap();
untyped.keys().forEachRemaining(stat -> genericStats.put(matchStatistic(stat), untyped.getInt(stat)));
final Map<Statistic, Map<Material, Integer>> itemStats = new HashMap<>();
items.keys().forEachRemaining(stat -> {
final JSONObject itemStat = items.getJSONObject(stat);
final Map<Material, Integer> itemMap = new HashMap<>();
itemStat.keys().forEachRemaining(item -> itemMap.put(Material.valueOf(item), itemStat.getInt(item)));
itemStats.put(Statistic.valueOf(stat), itemMap);
});
// Read block & item stats
final Map<Statistic, Map<Material, Integer>> blockStats, itemStats;
blockStats = readMaterialStatistics(blocks);
itemStats = readMaterialStatistics(items);
final Map<Statistic, Map<EntityType, Integer>> entityStats = new HashMap<>();
// Read entity stats
final Map<Statistic, Map<EntityType, Integer>> entityStats = Maps.newHashMap();
entities.keys().forEachRemaining(stat -> {
final JSONObject entityStat = entities.getJSONObject(stat);
final Map<EntityType, Integer> entityMap = new HashMap<>();
entityStat.keys().forEachRemaining(entity -> entityMap.put(EntityType.valueOf(entity), entityStat.getInt(entity)));
entityStats.put(Statistic.valueOf(stat), entityMap);
entityStat.keys().forEachRemaining(entity -> entityMap.put(matchEntityType(entity), entityStat.getInt(entity)));
entityStats.put(matchStatistic(stat), entityMap);
});
return BukkitData.Statistics.from(genericStats, blockStats, itemStats, entityStats);
}
@NotNull
private Map<Statistic, Map<Material, Integer>> readMaterialStatistics(@NotNull JSONObject items) {
final Map<Statistic, Map<Material, Integer>> itemStats = Maps.newHashMap();
items.keys().forEachRemaining(stat -> {
final JSONObject itemStat = items.getJSONObject(stat);
final Map<Material, Integer> itemMap = Maps.newHashMap();
itemStat.keys().forEachRemaining(item -> {
final Material material = matchMaterial(item);
if (material != null) {
itemMap.put(material, itemStat.getInt(item));
}
});
itemStats.put(matchStatistic(stat), itemMap);
});
return itemStats;
}
// Deserialize a legacy item stack array
@NotNull
public ItemStack[] deserializeLegacyItemStacks(@NotNull String items) {

@ -0,0 +1,32 @@
package net.william278.husksync.util;
import org.bukkit.Material;
import org.bukkit.Statistic;
import org.bukkit.entity.EntityType;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Arrays;
public final class BukkitTypeMatcher {
@Nullable
public static Statistic matchStatistic(@NotNull String key) {
return Arrays.stream(Statistic.values())
.filter(stat -> stat.getKey().toString().equals(key))
.findFirst().orElse(null);
}
@Nullable
public static EntityType matchEntityType(@NotNull String key) {
return Arrays.stream(EntityType.values())
.filter(entityType -> entityType.getKey().toString().equals(key))
.findFirst().orElse(null);
}
@Nullable
public static Material matchMaterial(@NotNull String key) {
return Material.matchMaterial(key);
}
}
Loading…
Cancel
Save