Prevent call to Item#initializeClient when in datagen (#8097)

When running under data-generation, the Minecraft singleton is unavailable
even though the operating env is under the CLIENT dist. Adding this check
prevents crashes from devs unaware of this, as most uses of
IItemRenderProperties (usually for a custom BEWLR) make use of the
Minecraft singleton and therefore will cause NPEs if not guarded against.

A similar thing happens in MobEffect and Block.
This commit is contained in:
sciwhiz12 authored and GitHub committed 2021-09-24 23:10:34 +02:00
1 parent be1698bb15
commit 347a0b3c0d
3 files changed
+9 -6

No files matched your search

@@ -17,7 +17,7 @@
}
public void m_6742_(LivingEntity p_19467_, int p_19468_) {
@@ -179,4 +_,28 @@
@@ -179,4 +_,29 @@
public boolean m_19486_() {
return this.f_19447_ == MobEffectCategory.BENEFICIAL;
}
@@ -34,7 +34,8 @@
+ }
+
+ private void initClient() {
+ if (net.minecraftforge.fml.loading.FMLEnvironment.dist == net.minecraftforge.api.distmarker.Dist.CLIENT) {
+ // Minecraft instance isn't available in datagen, so don't call initializeClient if in datagen
+ if (net.minecraftforge.fml.loading.FMLEnvironment.dist == net.minecraftforge.api.distmarker.Dist.CLIENT && !net.minecraftforge.fml.loading.FMLLoader.getLaunchHandler().isData()) {
+ initializeClient(properties -> {
+ this.effectRenderer = properties;
+ });
@@ -104,7 +104,7 @@
}
public ItemStack m_7968_() {
@@ -371,6 +_,31 @@
@@ -371,6 +_,32 @@
return true;
}
@@ -120,7 +120,8 @@
+ }
+
+ private void initClient() {
+ if (net.minecraftforge.fml.loading.FMLEnvironment.dist == net.minecraftforge.api.distmarker.Dist.CLIENT) {
+ // Minecraft instance isn't available in datagen, so don't call initializeClient if in datagen
+ if (net.minecraftforge.fml.loading.FMLEnvironment.dist == net.minecraftforge.api.distmarker.Dist.CLIENT && !net.minecraftforge.fml.loading.FMLLoader.getLaunchHandler().isData()) {
+ initializeClient(properties -> {
+ if (properties == this)
+ throw new IllegalStateException("Don't extend IItemRenderProperties in your item, use an anonymous class instead.");
@@ -91,7 +91,7 @@
}
public void m_5871_(ItemStack p_49816_, @Nullable BlockGetter p_49817_, List<Component> p_49818_, TooltipFlag p_49819_) {
@@ -486,6 +_,81 @@
@@ -486,6 +_,82 @@
protected ImmutableMap<BlockState, VoxelShape> m_152458_(Function<BlockState, VoxelShape> p_152459_) {
return this.f_49792_.m_61056_().stream().collect(ImmutableMap.toImmutableMap(Function.identity(), p_152459_));
}
@@ -110,7 +110,8 @@
+ }
+
+ private void initClient() {
+ if (net.minecraftforge.fml.loading.FMLEnvironment.dist == net.minecraftforge.api.distmarker.Dist.CLIENT) {
+ // Minecraft instance isn't available in datagen, so don't call initializeClient if in datagen
+ if (net.minecraftforge.fml.loading.FMLEnvironment.dist == net.minecraftforge.api.distmarker.Dist.CLIENT && !net.minecraftforge.fml.loading.FMLLoader.getLaunchHandler().isData()) {
+ initializeClient(properties -> {
+ if (properties == this)
+ throw new IllegalStateException("Don't extend IBlockRenderProperties in your block, use an anonymous class instead.");