Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Recover soil/crop recipe ids from their respective itemstacks #227

Open
wants to merge 5 commits into
base: 1.16.5
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down Expand Up @@ -562,4 +553,4 @@ private List<ItemStack> getDrops () {

return this.dropsCache;
}
}
}