diff --git a/src/main/java/net/darkhax/botanypots/block/tileentity/TileEntityBotanyPot.java b/src/main/java/net/darkhax/botanypots/block/tileentity/TileEntityBotanyPot.java index 3c0b5aa2..5435f53f 100644 --- a/src/main/java/net/darkhax/botanypots/block/tileentity/TileEntityBotanyPot.java +++ b/src/main/java/net/darkhax/botanypots/block/tileentity/TileEntityBotanyPot.java @@ -433,75 +433,66 @@ public void deserialize (CompoundNBT dataTag) { this.soil = null; this.crop = null; - if (dataTag.contains("CropStack")) { - - this.cropStack = ItemStack.read(dataTag.getCompound("CropStack")); - } - - if (dataTag.contains("SoilStack")) { - - this.soilStack = ItemStack.read(dataTag.getCompound("SoilStack")); - } - + if (dataTag.contains("SoilStack")) this.soilStack = ItemStack.read(dataTag.getCompound("SoilStack")); + if (dataTag.contains("CropStack")) this.cropStack = ItemStack.read(dataTag.getCompound("CropStack")); + + // Recover soil from id. if (dataTag.contains("Soil")) { - final String rawSoilId = dataTag.getString("Soil"); final ResourceLocation soilId = ResourceLocation.tryCreate(rawSoilId); - - if (soilId != null) { - + + if (soilId == null) { + BotanyPots.LOGGER.error("Botany Pot at {} has invalid soil type {}. Soil and crop will be discarded.", this.pos, rawSoilId); + } else { final SoilInfo foundSoil = BotanyPotHelper.getSoil(soilId); - - if (foundSoil != null) { - - this.soil = foundSoil; - - // Crops are only loaded if the soil exists. - if (dataTag.contains("Crop")) { - - final String rawCropId = dataTag.getString("Crop"); - final ResourceLocation cropId = ResourceLocation.tryCreate(rawCropId); - - if (cropId != null) { - - final CropInfo cropInfo = BotanyPotHelper.getCrop(cropId); - - if (cropInfo != null) { - - this.crop = cropInfo; - - // Growth ticks are only loaded if a crop and soil exist. - this.currentGrowthTicks = dataTag.getInt("GrowthTicks"); - - // Reset total growth ticks on tile load to account for data - // changes. - this.totalGrowthTicks = this.crop.getGrowthTicksForSoil(this.soil); - } - - else { - - BotanyPots.LOGGER.error("Botany Pot at {} had a crop of type {} but that crop does not exist. The crop will be discarded.", this.pos, rawCropId); - } - } - - else { - - BotanyPots.LOGGER.error("Botany Pot at {} has an invalid crop Id of {}. The crop will be discarded.", this.pos, rawCropId); - } - } - } - - else { - + + if (foundSoil == null) { BotanyPots.LOGGER.error("Botany Pot at {} had a soil of type {} which no longer exists. Soil and crop will be discarded.", this.pos, rawSoilId); + } else { + this.soil = foundSoil; } } - - else { - - BotanyPots.LOGGER.error("Botany Pot at {} has invalid soil type {}. Soil and crop will be discarded.", this.pos, rawSoilId); + } + + // Recover soil from stack. + if (soil == null && this.soilStack != ItemStack.EMPTY) { + final SoilInfo recoveredSoil = BotanyPotHelper.getSoilForItem(soilStack); + if (recoveredSoil != null) this.soil = recoveredSoil; + } + + // Crops are only loaded if the soil exists. + if(this.soil == null) return; + + // Recover crop from id. + if (dataTag.contains("Crop")) { + final String rawCropId = dataTag.getString("Crop"); + final ResourceLocation cropId = ResourceLocation.tryCreate(rawCropId); + + if(cropId == null){ + BotanyPots.LOGGER.error("Botany Pot at {} has an invalid crop Id of {}. The crop will be discarded.", this.pos, rawCropId); + }else{ + final CropInfo cropInfo = BotanyPotHelper.getCrop(cropId); + if(cropInfo == null){ + BotanyPots.LOGGER.error("Botany Pot at {} had a crop of type {} but that crop does not exist. The crop will be discarded.", this.pos, rawCropId); + }else{ + this.crop = cropInfo; + } } } + + // Recover crop from stack. + if (crop == null && this.cropStack != ItemStack.EMPTY) { + final CropInfo recoveredCrop = BotanyPotHelper.getCropForItem(cropStack); + if (recoveredCrop != null) this.crop = recoveredCrop; + } + + // Growth ticks are only loaded a crop exists too. + if(this.crop == null) return; + + this.currentGrowthTicks = dataTag.getInt("GrowthTicks"); + + // Reset total growth ticks on tile load to account for data changes. + this.totalGrowthTicks = this.crop.getGrowthTicksForSoil(this.soil); } public ItemStack getSoilStack () { @@ -562,4 +553,4 @@ private List getDrops () { return this.dropsCache; } -} \ No newline at end of file +}