Skip to content

Commit

Permalink
Update BlockDataService.java
Browse files Browse the repository at this point in the history
更优雅的写法且性能大致不差
  • Loading branch information
mcchampions committed Dec 8, 2024
1 parent 4ae7580 commit f09e64e
Showing 1 changed file with 16 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -119,29 +119,26 @@ public Optional<UUID> getUniversalDataUUID(Block b) {
}

public static Optional<String> getBlockData(Block b, NamespacedKey key) {
try {
if (Bukkit.isPrimaryThread()) {
BlockState state = b.getState(false);
PersistentDataContainer container = getPersistentDataContainer(state);
if (container != null) {
return Optional.ofNullable(container.get(key, PersistentDataType.STRING));
} else {
if (container == null) {
return Optional.empty();
}
} catch (IllegalStateException e) {
try {
return Bukkit.getScheduler().callSyncMethod(Slimefun.instance(), () -> {
BlockState state = b.getState(false);
PersistentDataContainer container = getPersistentDataContainer(state);
if (container != null) {
return Optional.ofNullable(container.get(key, PersistentDataType.STRING));
} else {
//noinspection OptionalOfNullableMisuse
return Optional.ofNullable((String) null);
}
}).get();
} catch (InterruptedException | ExecutionException ex) {
throw new RuntimeException(ex);
}
return Optional.ofNullable(container.get(key, PersistentDataType.STRING));
}
try {
return Bukkit.getScheduler().callSyncMethod(Slimefun.instance(), () -> {
BlockState state = b.getState(false);
PersistentDataContainer container = getPersistentDataContainer(state);
if (container == null) {
//noinspection OptionalOfNullableMisuse
return Optional.ofNullable((String) null);
}
return Optional.ofNullable(container.get(key, PersistentDataType.STRING));
}).get();
} catch (InterruptedException | ExecutionException ex) {
throw new RuntimeException(ex);
}
}

Expand Down

0 comments on commit f09e64e

Please sign in to comment.