Fix potential NPEs in RegistryObject.orElseThrow/isPresent/ifPresent Closes #6647

This commit is contained in:
LexManos 2020-04-24 19:18:57 -07:00
parent 9a6d1c390e
commit 03c2a3d3a8

View file

@ -82,6 +82,7 @@ public final class RegistryObject<T extends IForgeRegistryEntry<? super T>> impl
/** /**
* Directly retrieves the wrapped Registry Object. This value will automatically be updated when the backing registry is updated. * 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 @Override
@Nonnull @Nonnull
@ -112,7 +113,7 @@ public final class RegistryObject<T extends IForgeRegistryEntry<? super T>> impl
* @return {@code true} if there is a mod object present, otherwise {@code false} * @return {@code true} if there is a mod object present, otherwise {@code false}
*/ */
public boolean isPresent() { public boolean isPresent() {
return get() != null; return this.value != null;
} }
/** /**
@ -124,7 +125,7 @@ public final class RegistryObject<T extends IForgeRegistryEntry<? super T>> impl
* null * null
*/ */
public void ifPresent(Consumer<? super T> consumer) { public void ifPresent(Consumer<? super T> consumer) {
if (get() != null) if (isPresent())
consumer.accept(get()); consumer.accept(get());
} }
@ -259,7 +260,7 @@ public final class RegistryObject<T extends IForgeRegistryEntry<? super T>> impl
* {@code exceptionSupplier} is null * {@code exceptionSupplier} is null
*/ */
public <X extends Throwable> T orElseThrow(Supplier<? extends X> exceptionSupplier) throws X { public <X extends Throwable> T orElseThrow(Supplier<? extends X> exceptionSupplier) throws X {
if (get() != null) { if (isPresent()) {
return get(); return get();
} else { } else {
throw exceptionSupplier.get(); throw exceptionSupplier.get();