diff --git a/bukkit/src/main/java/net/william278/husksync/data/BukkitData.java b/bukkit/src/main/java/net/william278/husksync/data/BukkitData.java index b1ba8c60..862fc5c0 100644 --- a/bukkit/src/main/java/net/william278/husksync/data/BukkitData.java +++ b/bukkit/src/main/java/net/william278/husksync/data/BukkitData.java @@ -48,7 +48,7 @@ import java.util.stream.Collectors; import java.util.stream.Stream; -import static net.william278.husksync.util.BukkitTypeMatcher.*; +import static net.william278.husksync.util.BukkitKeyedAdapter.*; public abstract class BukkitData implements Data { @@ -696,7 +696,8 @@ public Map getGenericStatistics() { public Map> getBlockStatistics() { return blockStatistics.entrySet().stream().filter(entry -> entry.getKey() != null).collect( TreeMap::new, - (m, e) -> m.put(e.getKey().getKey().toString(), convertStatistics(e.getValue())), TreeMap::putAll + (m, e) -> getKeyName(e.getKey()).ifPresent(key -> m.put(key, convertStatistics(e.getValue()))), + TreeMap::putAll ); } @@ -705,7 +706,8 @@ public Map> getBlockStatistics() { public Map> getItemStatistics() { return itemStatistics.entrySet().stream().filter(entry -> entry.getKey() != null).collect( TreeMap::new, - (m, e) -> m.put(e.getKey().getKey().toString(), convertStatistics(e.getValue())), TreeMap::putAll + (m, e) -> getKeyName(e.getKey()).ifPresent(key -> m.put(key, convertStatistics(e.getValue()))), + TreeMap::putAll ); } @@ -714,7 +716,8 @@ public Map> getItemStatistics() { public Map> getEntityStatistics() { return entityStatistics.entrySet().stream().filter(entry -> entry.getKey() != null).collect( TreeMap::new, - (m, e) -> m.put(e.getKey().getKey().toString(), convertStatistics(e.getValue())), TreeMap::putAll + (m, e) -> getKeyName(e.getKey()).ifPresent(key -> m.put(key, convertStatistics(e.getValue()))), + TreeMap::putAll ); } @@ -722,13 +725,8 @@ public Map> getEntityStatistics() { private Map convertStatistics(@NotNull Map stats) { return stats.entrySet().stream().filter(entry -> entry.getKey() != null).collect( TreeMap::new, - (m, e) -> { - try { - m.put(e.getKey().getKey().toString(), e.getValue()); - } catch (Throwable t) { - // Ignore; skip elements with invalid keys (e.g., legacy materials) - } - }, TreeMap::putAll + (m, e) -> getKeyName(e.getKey()).ifPresent(key -> m.put(key, e.getValue())), + TreeMap::putAll ); } diff --git a/bukkit/src/main/java/net/william278/husksync/user/BukkitUser.java b/bukkit/src/main/java/net/william278/husksync/user/BukkitUser.java index 24451864..ff492f7a 100644 --- a/bukkit/src/main/java/net/william278/husksync/user/BukkitUser.java +++ b/bukkit/src/main/java/net/william278/husksync/user/BukkitUser.java @@ -40,7 +40,7 @@ import java.util.function.Consumer; import java.util.logging.Level; -import static net.william278.husksync.util.BukkitTypeMatcher.matchMaterial; +import static net.william278.husksync.util.BukkitKeyedAdapter.matchMaterial; /** * Bukkit platform implementation of an {@link OnlineUser} diff --git a/bukkit/src/main/java/net/william278/husksync/util/BukkitTypeMatcher.java b/bukkit/src/main/java/net/william278/husksync/util/BukkitKeyedAdapter.java similarity index 54% rename from bukkit/src/main/java/net/william278/husksync/util/BukkitTypeMatcher.java rename to bukkit/src/main/java/net/william278/husksync/util/BukkitKeyedAdapter.java index 40b77bf1..6b59ada8 100644 --- a/bukkit/src/main/java/net/william278/husksync/util/BukkitTypeMatcher.java +++ b/bukkit/src/main/java/net/william278/husksync/util/BukkitKeyedAdapter.java @@ -19,6 +19,7 @@ package net.william278.husksync.util; +import org.bukkit.Keyed; import org.bukkit.Material; import org.bukkit.Statistic; import org.bukkit.entity.EntityType; @@ -26,26 +27,48 @@ import org.jetbrains.annotations.Nullable; import java.util.Arrays; +import java.util.Optional; -public final class BukkitTypeMatcher { +// Utility class for adapting "Keyed" Bukkit objects +public final class BukkitKeyedAdapter { @Nullable public static Statistic matchStatistic(@NotNull String key) { - return Arrays.stream(Statistic.values()) - .filter(stat -> stat.getKey().toString().equals(key)) - .findFirst().orElse(null); + try { + return Arrays.stream(Statistic.values()) + .filter(stat -> stat.getKey().toString().equals(key)) + .findFirst().orElse(null); + } catch (Throwable e) { + return null; + } } @Nullable public static EntityType matchEntityType(@NotNull String key) { - return Arrays.stream(EntityType.values()) - .filter(entityType -> entityType.getKey().toString().equals(key)) - .findFirst().orElse(null); + try { + return Arrays.stream(EntityType.values()) + .filter(entityType -> entityType.getKey().toString().equals(key)) + .findFirst().orElse(null); + } catch (Throwable e) { + return null; + } } @Nullable public static Material matchMaterial(@NotNull String key) { - return Material.matchMaterial(key); + try { + return Material.matchMaterial(key); + } catch (Throwable e) { + return null; + } + } + + public static Optional getKeyName(@NotNull Keyed keyed) { + try { + return Optional.of(keyed.getKey().toString()); + } catch (Throwable e) { + return Optional.empty(); + } } } diff --git a/bukkit/src/main/java/net/william278/husksync/util/BukkitLegacyConverter.java b/bukkit/src/main/java/net/william278/husksync/util/BukkitLegacyConverter.java index 6753eb3a..0b573500 100644 --- a/bukkit/src/main/java/net/william278/husksync/util/BukkitLegacyConverter.java +++ b/bukkit/src/main/java/net/william278/husksync/util/BukkitLegacyConverter.java @@ -44,7 +44,7 @@ import java.util.*; import java.util.logging.Level; -import static net.william278.husksync.util.BukkitTypeMatcher.*; +import static net.william278.husksync.util.BukkitKeyedAdapter.*; public class BukkitLegacyConverter extends LegacyConverter {