Fix structures issues/api (#7895)

This commit is contained in:
Jake Potrebic
2022-06-08 12:00:19 -07:00
parent f136fe47a3
commit f823b12e0a
3 changed files with 32 additions and 10 deletions

View File

@@ -35,6 +35,7 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
+import java.util.Objects;
+import java.util.Optional;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.function.Consumer;
+import java.util.function.Supplier;
+
+@DefaultQualifier(NonNull.class)
@@ -69,13 +70,21 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
+ });
+ }
+
+ public abstract API convertToApi(NamespacedKey key, MINECRAFT nms);
+ public abstract @Nullable API convertToApi(NamespacedKey key, MINECRAFT nms);
+
+ public API convertToApi(ResourceLocation resourceLocation, MINECRAFT nms) {
+ public API convertToApiOrThrow(ResourceLocation resourceLocation, MINECRAFT nms) {
+ return Objects.requireNonNull(this.convertToApi(resourceLocation, nms), resourceLocation + " has a null api representation");
+ }
+
+ public @Nullable API convertToApi(ResourceLocation resourceLocation, MINECRAFT nms) {
+ return this.convertToApi(CraftNamespacedKey.fromMinecraft(resourceLocation), nms);
+ }
+
+ public API convertToApi(Holder<MINECRAFT> nmsHolder) {
+ public API convertToApiOrThrow(Holder<MINECRAFT> nmsHolder) {
+ return Objects.requireNonNull(this.convertToApi(nmsHolder), nmsHolder + " has a null api representation");
+ }
+
+ public @Nullable API convertToApi(Holder<MINECRAFT> nmsHolder) {
+ final Optional<ResourceKey<MINECRAFT>> key = nmsHolder.unwrapKey();
+ if (nmsHolder.isBound() && key.isPresent()) {
+ return this.convertToApi(key.get().location(), nmsHolder.value());
@@ -90,6 +99,17 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
+ throw new IllegalStateException("Cannot convert " + nmsHolder + " to an API type in: " + this.registryKey);
+ }
+
+ public void convertToApi(Iterable<Holder<MINECRAFT>> holders, Consumer<API> apiConsumer, boolean throwOnNull) {
+ for (Holder<MINECRAFT> holder : holders) {
+ final @Nullable API api = this.convertToApi(holder);
+ if (api == null && throwOnNull) {
+ throw new NullPointerException(holder + " has a null api representation");
+ } else if (api != null) {
+ apiConsumer.accept(api);
+ }
+ }
+ }
+
+ public MINECRAFT getMinecraftValue(API apiValue) {
+ return this.registry.get().getOptional(CraftNamespacedKey.toMinecraft(apiValue.getKey())).orElseThrow();
+ }