Skip to content

Commit

Permalink
fix: Handle Bukkit objects that don't fully implement Keyed
Browse files Browse the repository at this point in the history
  • Loading branch information
WiIIiam278 committed Dec 26, 2023
1 parent a3e269c commit 414246f
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 21 deletions.
20 changes: 9 additions & 11 deletions bukkit/src/main/java/net/william278/husksync/data/BukkitData.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -696,7 +696,8 @@ public Map<String, Integer> getGenericStatistics() {
public Map<String, Map<String, Integer>> 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
);
}

Expand All @@ -705,7 +706,8 @@ public Map<String, Map<String, Integer>> getBlockStatistics() {
public Map<String, Map<String, Integer>> 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
);
}

Expand All @@ -714,21 +716,17 @@ public Map<String, Map<String, Integer>> getItemStatistics() {
public Map<String, Map<String, Integer>> 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
);
}

@NotNull
private <T extends Keyed> Map<String, Integer> convertStatistics(@NotNull Map<T, Integer> 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
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,33 +19,56 @@

package net.william278.husksync.util;

import org.bukkit.Keyed;
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;
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<String> getKeyName(@NotNull Keyed keyed) {
try {
return Optional.of(keyed.getKey().toString());
} catch (Throwable e) {
return Optional.empty();
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down

0 comments on commit 414246f

Please sign in to comment.