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
1 changed files with 6 additions and 5 deletions

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
@ -97,7 +98,7 @@ public final class RegistryObject<T extends IForgeRegistryEntry<? super T>> impl
this.value = registry.getValue(getId()); this.value = registry.getValue(getId());
} }
public ResourceLocation getId() public ResourceLocation getId()
{ {
return this.name; return this.name;
} }
@ -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());
} }
@ -196,7 +197,7 @@ public final class RegistryObject<T extends IForgeRegistryEntry<? super T>> impl
return Objects.requireNonNull(mapper.apply(get())); return Objects.requireNonNull(mapper.apply(get()));
} }
} }
/** /**
* If a mod object is present, lazily apply the provided mapping function to it, * 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 * returning a supplier for the transformed result. If this object is empty, or the
@ -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();