From bb4ba6e5acda3ad2ed0d02856a92259be42e5be6 Mon Sep 17 00:00:00 2001 From: Thom van den Akker Date: Sun, 1 Dec 2024 22:43:37 +0100 Subject: [PATCH] Fix a possible crash upon loading old disease data (#10495) Fix a possible crash upon loading old disease data --- .../api/util/constant/CitizenConstants.java | 5 +++++ .../citizenhandlers/CitizenDiseaseHandler.java | 12 +++++++++--- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/minecolonies/api/util/constant/CitizenConstants.java b/src/main/java/com/minecolonies/api/util/constant/CitizenConstants.java index eedc5230d21..2f88035a6ee 100755 --- a/src/main/java/com/minecolonies/api/util/constant/CitizenConstants.java +++ b/src/main/java/com/minecolonies/api/util/constant/CitizenConstants.java @@ -213,6 +213,11 @@ public final class CitizenConstants */ public static final String TAG_DISEASE = "disease"; + /** + * Disease iod tag. + */ + public static final String TAG_DISEASE_ID = "disease_id"; + /** * Disease immunity tag, */ diff --git a/src/main/java/com/minecolonies/core/entity/citizen/citizenhandlers/CitizenDiseaseHandler.java b/src/main/java/com/minecolonies/core/entity/citizen/citizenhandlers/CitizenDiseaseHandler.java index bf054786668..1736b5cf00c 100755 --- a/src/main/java/com/minecolonies/core/entity/citizen/citizenhandlers/CitizenDiseaseHandler.java +++ b/src/main/java/com/minecolonies/core/entity/citizen/citizenhandlers/CitizenDiseaseHandler.java @@ -14,6 +14,7 @@ import com.minecolonies.core.datalistener.DiseasesListener; import net.minecraft.core.BlockPos; import net.minecraft.nbt.CompoundTag; +import net.minecraft.nbt.Tag; import net.minecraft.resources.ResourceLocation; import net.minecraft.util.RandomSource; import org.jetbrains.annotations.Nullable; @@ -172,7 +173,7 @@ public void write(final CompoundTag compound) CompoundTag diseaseTag = new CompoundTag(); if (disease != null) { - diseaseTag.putString(TAG_DISEASE, disease.id().toString()); + diseaseTag.putString(TAG_DISEASE_ID, disease.id().toString()); } diseaseTag.putInt(TAG_IMMUNITY, immunityTicks); compound.put(TAG_DISEASE, diseaseTag); @@ -181,10 +182,15 @@ public void write(final CompoundTag compound) @Override public void read(final CompoundTag compound) { + if (!compound.contains(TAG_DISEASE, Tag.TAG_COMPOUND)) + { + return; + } + CompoundTag diseaseTag = compound.getCompound(TAG_DISEASE); - if (diseaseTag.contains(TAG_DISEASE)) + if (diseaseTag.contains(TAG_DISEASE_ID)) { - this.disease = DiseasesListener.getDisease(new ResourceLocation(diseaseTag.getString(TAG_DISEASE))); + this.disease = DiseasesListener.getDisease(new ResourceLocation(diseaseTag.getString(TAG_DISEASE_ID))); } this.immunityTicks = diseaseTag.getInt(TAG_IMMUNITY); }