From 03c2a3d3a8301fab27c877c3d5f6c36e9b9c559d Mon Sep 17 00:00:00 2001 From: LexManos Date: Fri, 24 Apr 2020 19:18:57 -0700 Subject: [PATCH] Fix potential NPEs in RegistryObject.orElseThrow/isPresent/ifPresent Closes #6647 --- .../java/net/minecraftforge/fml/RegistryObject.java | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/main/java/net/minecraftforge/fml/RegistryObject.java b/src/main/java/net/minecraftforge/fml/RegistryObject.java index 5eb2e57ea..af1bb0cde 100644 --- a/src/main/java/net/minecraftforge/fml/RegistryObject.java +++ b/src/main/java/net/minecraftforge/fml/RegistryObject.java @@ -82,6 +82,7 @@ public final class RegistryObject> impl /** * Directly retrieves the wrapped Registry Object. This value will automatically be updated when the backing registry is updated. + * Will throw NPE if the value is null, use isPresent to check first. Or use any of the other guarded functions. */ @Override @Nonnull @@ -97,7 +98,7 @@ public final class RegistryObject> impl this.value = registry.getValue(getId()); } - public ResourceLocation getId() + public ResourceLocation getId() { return this.name; } @@ -112,7 +113,7 @@ public final class RegistryObject> impl * @return {@code true} if there is a mod object present, otherwise {@code false} */ public boolean isPresent() { - return get() != null; + return this.value != null; } /** @@ -124,7 +125,7 @@ public final class RegistryObject> impl * null */ public void ifPresent(Consumer consumer) { - if (get() != null) + if (isPresent()) consumer.accept(get()); } @@ -196,7 +197,7 @@ public final class RegistryObject> impl return Objects.requireNonNull(mapper.apply(get())); } } - + /** * If a mod object is present, lazily apply the provided mapping function to it, * returning a supplier for the transformed result. If this object is empty, or the @@ -259,7 +260,7 @@ public final class RegistryObject> impl * {@code exceptionSupplier} is null */ public T orElseThrow(Supplier exceptionSupplier) throws X { - if (get() != null) { + if (isPresent()) { return get(); } else { throw exceptionSupplier.get();