From d25fd588caab8d953e5d5cd58c1f3c9ed8099e4b Mon Sep 17 00:00:00 2001 From: Patrick 'Quezler' Mounier Date: Wed, 18 May 2022 08:16:00 +0200 Subject: [PATCH 1/5] =?UTF-8?q?Upgrade=20deserialize=E2=80=99s=20`if=20els?= =?UTF-8?q?e`=20to=20`guard=20clauses`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../block/tileentity/TileEntityBotanyPot.java | 88 ++++++++----------- 1 file changed, 38 insertions(+), 50 deletions(-) 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..061f9a7b 100644 --- a/src/main/java/net/darkhax/botanypots/block/tileentity/TileEntityBotanyPot.java +++ b/src/main/java/net/darkhax/botanypots/block/tileentity/TileEntityBotanyPot.java @@ -447,59 +447,47 @@ public void deserialize (CompoundNBT dataTag) { final String rawSoilId = dataTag.getString("Soil"); final ResourceLocation soilId = ResourceLocation.tryCreate(rawSoilId); - - if (soilId != null) { - - final SoilInfo foundSoil = BotanyPotHelper.getSoil(soilId); - - if (foundSoil != null) { - - this.soil = foundSoil; + + if(soilId == null) { + BotanyPots.LOGGER.error("Botany Pot at {} has invalid soil type {}. Soil and crop will be discarded.", this.pos, rawSoilId); + return; + } + + final SoilInfo foundSoil = BotanyPotHelper.getSoil(soilId); + + 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); + return; + } - // 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); - } - } + 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) { + BotanyPots.LOGGER.error("Botany Pot at {} has an invalid crop Id of {}. The crop will be discarded.", this.pos, rawCropId); + return; } - - else { - - BotanyPots.LOGGER.error("Botany Pot at {} had a soil of type {} which no longer exists. Soil and crop will be discarded.", this.pos, rawSoilId); + + 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); + return; } - } - - else { - - BotanyPots.LOGGER.error("Botany Pot at {} has invalid soil type {}. Soil and crop will be discarded.", this.pos, rawSoilId); + + 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); } } } From cbf102135075180ed54349edb3190f189c7ab151 Mon Sep 17 00:00:00 2001 From: Patrick 'Quezler' Mounier Date: Wed, 18 May 2022 08:39:50 +0200 Subject: [PATCH 2/5] Stash first attempt --- .../block/tileentity/TileEntityBotanyPot.java | 24 ++++++++++++++----- 1 file changed, 18 insertions(+), 6 deletions(-) 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 061f9a7b..01a594c4 100644 --- a/src/main/java/net/darkhax/botanypots/block/tileentity/TileEntityBotanyPot.java +++ b/src/main/java/net/darkhax/botanypots/block/tileentity/TileEntityBotanyPot.java @@ -446,11 +446,17 @@ public void deserialize (CompoundNBT dataTag) { if (dataTag.contains("Soil")) { final String rawSoilId = dataTag.getString("Soil"); - final ResourceLocation soilId = ResourceLocation.tryCreate(rawSoilId); + ResourceLocation soilId = ResourceLocation.tryCreate(rawSoilId); if(soilId == null) { - BotanyPots.LOGGER.error("Botany Pot at {} has invalid soil type {}. Soil and crop will be discarded.", this.pos, rawSoilId); - return; + final SoilInfo recoveredSoil = BotanyPotHelper.getSoilForItem(soilStack); +// System.out.print(recoveredSoil); + if(recoveredSoil != null) { + soilId = recoveredSoil.getId(); + } else { + BotanyPots.LOGGER.error("Botany Pot at {} has invalid soil type {}. Soil and crop will be discarded.", this.pos, rawSoilId); + return; + } } final SoilInfo foundSoil = BotanyPotHelper.getSoil(soilId); @@ -466,11 +472,17 @@ public void deserialize (CompoundNBT dataTag) { if (dataTag.contains("Crop")) { final String rawCropId = dataTag.getString("Crop"); - final ResourceLocation cropId = ResourceLocation.tryCreate(rawCropId); + 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); - return; + final CropInfo recoveredCrop = BotanyPotHelper.getCropForItem(cropStack); +// System.out.print(recoveredCrop); + if(recoveredCrop != null) { + cropId = recoveredCrop.getId(); + } else { + BotanyPots.LOGGER.error("Botany Pot at {} has an invalid crop Id of {}. The crop will be discarded.", this.pos, rawCropId); + return; + } } final CropInfo cropInfo = BotanyPotHelper.getCrop(cropId); From 8e83d42b8d5574e515a74c20a3a289925ea28d4a Mon Sep 17 00:00:00 2001 From: Patrick 'Quezler' Mounier Date: Wed, 18 May 2022 08:56:20 +0200 Subject: [PATCH 3/5] Stash second attempt --- .../block/tileentity/TileEntityBotanyPot.java | 95 +++++++++---------- 1 file changed, 43 insertions(+), 52 deletions(-) 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 01a594c4..bfe7a94c 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"); ResourceLocation soilId = ResourceLocation.tryCreate(rawSoilId); - if(soilId == null) { - final SoilInfo recoveredSoil = BotanyPotHelper.getSoilForItem(soilStack); -// System.out.print(recoveredSoil); - if(recoveredSoil != null) { - soilId = recoveredSoil.getId(); + 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) { + 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 { - BotanyPots.LOGGER.error("Botany Pot at {} has invalid soil type {}. Soil and crop will be discarded.", this.pos, rawSoilId); - return; + this.soil = foundSoil; } } + } - final SoilInfo foundSoil = BotanyPotHelper.getSoil(soilId); - - 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); - return; - } - - this.soil = foundSoil; - - // Crops are only loaded if the soil exists. - if (dataTag.contains("Crop")) { + // Recover soil from stack. + if (soil == null && this.soilStack != ItemStack.EMPTY) { + final SoilInfo recoveredSoil = BotanyPotHelper.getSoilForItem(soilStack); + if (recoveredSoil != null) this.soil = recoveredSoil; + } - final String rawCropId = dataTag.getString("Crop"); - ResourceLocation cropId = ResourceLocation.tryCreate(rawCropId); + // Crops are only loaded if the soil exists. + if(this.soil == null) return; - if(cropId == null) { - final CropInfo recoveredCrop = BotanyPotHelper.getCropForItem(cropStack); -// System.out.print(recoveredCrop); - if(recoveredCrop != null) { - cropId = recoveredCrop.getId(); - } else { - BotanyPots.LOGGER.error("Botany Pot at {} has an invalid crop Id of {}. The crop will be discarded.", this.pos, rawCropId); - return; - } - } + // Recover crop from id. + if (dataTag.contains("Crop")) { + final String rawCropId = dataTag.getString("Crop"); + 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) { + 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); - return; + }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; + } - this.crop = cropInfo; + // Growth ticks are only loaded a crop exists too. + if(this.crop == null) return; - // Growth ticks are only loaded if a crop and soil exist. - this.currentGrowthTicks = dataTag.getInt("GrowthTicks"); + this.currentGrowthTicks = dataTag.contains("GrowthTicks") ? dataTag.getInt("GrowthTicks") : 0; - // Reset total growth ticks on tile load to account for data - // changes. - this.totalGrowthTicks = this.crop.getGrowthTicksForSoil(this.soil); - } - } + // Reset total growth ticks on tile load to account for data changes. + this.totalGrowthTicks = this.crop.getGrowthTicksForSoil(this.soil); } public ItemStack getSoilStack () { From 108ec24012d014c1b9a52f0bbeedc32239f1b0d1 Mon Sep 17 00:00:00 2001 From: Patrick 'Quezler' Mounier Date: Wed, 18 May 2022 08:58:27 +0200 Subject: [PATCH 4/5] This defaults to returning 0 already --- .../botanypots/block/tileentity/TileEntityBotanyPot.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 bfe7a94c..76d1117f 100644 --- a/src/main/java/net/darkhax/botanypots/block/tileentity/TileEntityBotanyPot.java +++ b/src/main/java/net/darkhax/botanypots/block/tileentity/TileEntityBotanyPot.java @@ -489,7 +489,7 @@ public void deserialize (CompoundNBT dataTag) { // Growth ticks are only loaded a crop exists too. if(this.crop == null) return; - this.currentGrowthTicks = dataTag.contains("GrowthTicks") ? dataTag.getInt("GrowthTicks") : 0; + this.currentGrowthTicks = dataTag.getInt("GrowthTicks"); // Reset total growth ticks on tile load to account for data changes. this.totalGrowthTicks = this.crop.getGrowthTicksForSoil(this.soil); From d7453e7f06fa8822e34d2345bd4845599455cc2f Mon Sep 17 00:00:00 2001 From: Patrick 'Quezler' Mounier Date: Wed, 18 May 2022 10:27:57 +0200 Subject: [PATCH 5/5] These two can stay final --- .../botanypots/block/tileentity/TileEntityBotanyPot.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) 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 76d1117f..5435f53f 100644 --- a/src/main/java/net/darkhax/botanypots/block/tileentity/TileEntityBotanyPot.java +++ b/src/main/java/net/darkhax/botanypots/block/tileentity/TileEntityBotanyPot.java @@ -439,7 +439,7 @@ public void deserialize (CompoundNBT dataTag) { // Recover soil from id. if (dataTag.contains("Soil")) { final String rawSoilId = dataTag.getString("Soil"); - ResourceLocation soilId = ResourceLocation.tryCreate(rawSoilId); + final ResourceLocation soilId = ResourceLocation.tryCreate(rawSoilId); if (soilId == null) { BotanyPots.LOGGER.error("Botany Pot at {} has invalid soil type {}. Soil and crop will be discarded.", this.pos, rawSoilId); @@ -466,7 +466,7 @@ public void deserialize (CompoundNBT dataTag) { // Recover crop from id. if (dataTag.contains("Crop")) { final String rawCropId = dataTag.getString("Crop"); - ResourceLocation cropId = ResourceLocation.tryCreate(rawCropId); + 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); @@ -553,4 +553,4 @@ private List getDrops () { return this.dropsCache; } -} \ No newline at end of file +}