diff options
author | Glease <4586901+Glease@users.noreply.github.com> | 2022-10-05 23:56:27 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-10-05 17:56:27 +0200 |
commit | 5b2633323c378c1366dc415b00f411c1b64f5b86 (patch) | |
tree | 100d253be0bfeab37c517b7d2e99bddf0427e672 /src/main/java/gtPlusPlus | |
parent | 0db16cc7b1cdaae3c89f76c41ab51458ef98e6dd (diff) | |
download | GT5-Unofficial-5b2633323c378c1366dc415b00f411c1b64f5b86.tar.gz GT5-Unofficial-5b2633323c378c1366dc415b00f411c1b64f5b86.tar.bz2 GT5-Unofficial-5b2633323c378c1366dc415b00f411c1b64f5b86.zip |
handle drops with a smaller max stack size properly (#391)
* handle drops with a smaller max stack size properly
also inverted a few if statements to reduce nesting levels. the effect and semantic is left the same
* optimize a bit
Diffstat (limited to 'src/main/java/gtPlusPlus')
-rw-r--r-- | src/main/java/gtPlusPlus/xmod/gregtech/common/tileentities/machines/basic/GT_MetaTileEntity_CropHarvestor.java | 219 |
1 files changed, 86 insertions, 133 deletions
diff --git a/src/main/java/gtPlusPlus/xmod/gregtech/common/tileentities/machines/basic/GT_MetaTileEntity_CropHarvestor.java b/src/main/java/gtPlusPlus/xmod/gregtech/common/tileentities/machines/basic/GT_MetaTileEntity_CropHarvestor.java index d18f52f3ff..96570f356b 100644 --- a/src/main/java/gtPlusPlus/xmod/gregtech/common/tileentities/machines/basic/GT_MetaTileEntity_CropHarvestor.java +++ b/src/main/java/gtPlusPlus/xmod/gregtech/common/tileentities/machines/basic/GT_MetaTileEntity_CropHarvestor.java @@ -1,5 +1,6 @@ package gtPlusPlus.xmod.gregtech.common.tileentities.machines.basic; +import com.gtnewhorizon.gtnhlib.util.map.ItemStackMap; import gregtech.api.enums.*; import gregtech.api.interfaces.ITexture; import gregtech.api.interfaces.tileentity.IGregTechTileEntity; @@ -9,7 +10,6 @@ import gregtech.api.objects.GT_ItemStack; import gregtech.api.render.TextureFactory; import gregtech.api.util.GT_Utility; import gtPlusPlus.api.objects.Logger; -import gtPlusPlus.api.objects.data.AutoMap; import gtPlusPlus.core.lib.CORE; import gtPlusPlus.core.util.math.MathUtils; import gtPlusPlus.core.util.minecraft.ItemUtils; @@ -18,6 +18,7 @@ import gtPlusPlus.xmod.gregtech.common.blocks.textures.TexturesGtBlock; import ic2.api.crops.*; import ic2.core.item.DamageHandler; import java.util.*; +import java.util.Map.Entry; import net.minecraft.entity.player.*; import net.minecraft.item.*; import net.minecraft.nbt.NBTTagCompound; @@ -193,147 +194,99 @@ public class GT_MetaTileEntity_CropHarvestor extends GT_MetaTileEntity_BasicTank return this.maxEUInput() / 32; } - public static AutoMap<ItemStack> splitOutputStacks(ItemStack aOutput) { - return splitOutputStacks(new ItemStack[] {aOutput}); - } - - public static AutoMap<ItemStack> splitOutputStacks(ItemStack[] aOutputs) { - AutoMap<ItemStack> aOutputMap = new AutoMap<ItemStack>(); - for (ItemStack aStack : aOutputs) { - if (aStack != null) { - if (aStack.stackSize <= 64) { - aOutputMap.add(aStack); - } else { - int aStacks = 0; - int aExcess = 0; - int aOriginalSize = aStack.stackSize; - while (aOriginalSize >= 64) { - aStacks += 1; - aOriginalSize -= 64; - } - aExcess = aOriginalSize; - for (int i = 0; i < aStacks; i++) { - aOutputMap.add(ItemUtils.getSimpleStack(aStack, 64)); - } - aOutputMap.add(ItemUtils.getSimpleStack(aStack, aExcess)); - } - } - } - return aOutputMap; - } - @Override public void onPostTick(IGregTechTileEntity aBaseMetaTileEntity, long aTick) { super.onPostTick(aBaseMetaTileEntity, aTick); - if (getBaseMetaTileEntity().isServerSide() - && getBaseMetaTileEntity().isAllowedToWork() - && (getBaseMetaTileEntity().hasWorkJustBeenEnabled() || aTick % 100 == 0)) { - if (this.getBaseMetaTileEntity().getUniversalEnergyStored() >= getMinimumStoredEU()) { - - int aTileX = this.getBaseMetaTileEntity().getXCoord(); - int aTileY = this.getBaseMetaTileEntity().getXCoord(); - int aTileZ = this.getBaseMetaTileEntity().getXCoord(); - - int aRadius = 10 + getRange(this.mTier); - int aSide = (aRadius - 1) / 2; - ArrayList<ItemStack> aAllDrops = new ArrayList<ItemStack>(); - - if (this.mCropCache.isEmpty() || aTick % 1200 == 0 || this.mInvalidCache) { - if (!this.mCropCache.isEmpty()) { - this.mCropCache.clear(); - } - // Logger.INFO("Looking for crops."); - for (int y = 0; y <= 2; y++) { - for (int x = (-aSide); x <= aSide; x++) { - for (int z = (-aSide); z <= aSide; z++) { - TileEntity tTileEntity = getBaseMetaTileEntity().getTileEntityOffset(x, y, z); - if (tTileEntity != null && tTileEntity instanceof ICropTile) { - ICropTile tCrop = (ICropTile) tTileEntity; - this.mCropCache.add(tCrop); - } - } + if (!getBaseMetaTileEntity().isServerSide() + || !getBaseMetaTileEntity().isAllowedToWork() + || (!getBaseMetaTileEntity().hasWorkJustBeenEnabled() && aTick % 100 != 0)) return; + + if (this.getBaseMetaTileEntity().getUniversalEnergyStored() < getMinimumStoredEU()) return; + + int aTileX = this.getBaseMetaTileEntity().getXCoord(); + int aTileY = this.getBaseMetaTileEntity().getXCoord(); + int aTileZ = this.getBaseMetaTileEntity().getXCoord(); + + int aRadius = 10 + getRange(this.mTier); + int aSide = (aRadius - 1) / 2; + Map<ItemStack, Integer> aAllDrops = new ItemStackMap<>(true); + + if (this.mCropCache.isEmpty() || aTick % 1200 == 0 || this.mInvalidCache) { + if (!this.mCropCache.isEmpty()) { + this.mCropCache.clear(); + } + // Logger.INFO("Looking for crops."); + for (int y = 0; y <= 2; y++) { + for (int x = (-aSide); x <= aSide; x++) { + for (int z = (-aSide); z <= aSide; z++) { + TileEntity tTileEntity = getBaseMetaTileEntity().getTileEntityOffset(x, y, z); + if (tTileEntity != null && tTileEntity instanceof ICropTile) { + ICropTile tCrop = (ICropTile) tTileEntity; + this.mCropCache.add(tCrop); } } } + } + } - // Process Cache - if (doesInventoryHaveSpace()) { - for (ICropTile tCrop : this.mCropCache) { - if (tCrop == null) { - this.mInvalidCache = true; - break; - } - CropCard aCrop = tCrop.getCrop(); - if (aCrop != null) { - // Logger.INFO("Found "+aCrop.displayName()+" at offset "+x+", "+y+", "+z); - if (!aCrop.canGrow(tCrop) && aCrop.canBeHarvested(tCrop)) { - if (getBaseMetaTileEntity().decreaseStoredEnergyUnits(powerUsage(), true)) { - ItemStack[] aHarvest = tCrop.harvest_automated(true); - if (aHarvest != null && aHarvest.length > 0) { - for (ItemStack aStack : aHarvest) { - if (aStack != null && aStack.stackSize > 0) { - if (this.mTier * 5 > MathUtils.randInt(1, 100)) { - aStack.stackSize += tCrop.getGain() / 10; - Logger.INFO("Bonus output given for " + aCrop.displayName()); - } - Logger.INFO("Harvested " + aCrop.displayName()); - aAllDrops.addAll(splitOutputStacks(aStack)); - } - } - } - } - } - if (this.mModeAlternative) { - processSecondaryFunctions(tCrop); - } - } + // Process Cache + if (!doesInventoryHaveSpace()) return; + + for (ICropTile tCrop : this.mCropCache) { + if (tCrop == null) { + this.mInvalidCache = true; + break; + } + CropCard aCrop = tCrop.getCrop(); + if (aCrop == null) continue; + + if (this.mModeAlternative) processSecondaryFunctions(tCrop); + + if (!aCrop.canGrow(tCrop) && aCrop.canBeHarvested(tCrop)) { + if (!getBaseMetaTileEntity().decreaseStoredEnergyUnits(powerUsage(), true)) continue; + ItemStack[] aHarvest = tCrop.harvest_automated(true); + if (aHarvest == null) continue; + + for (ItemStack aStack : aHarvest) { + if (!ItemUtils.checkForInvalidItems(aStack)) continue; + if (this.mTier * 5 > MathUtils.randInt(1, 100)) { + aStack.stackSize += Math.floor(tCrop.getGain() / 10); + Logger.INFO("Bonus output given for " + aCrop.displayName()); } + Logger.INFO("Harvested " + aCrop.displayName()); + aAllDrops.merge(aStack, aStack.stackSize, Integer::sum); + } + } + } - if (!aAllDrops.isEmpty()) { - Logger.INFO("Handling " + aAllDrops.size() + " Harvests"); - Iterator<ItemStack> iter = aAllDrops.iterator(); - while (iter.hasNext()) { - ItemStack aDrop = iter.next(); - if (ItemUtils.checkForInvalidItems(aDrop)) { - - for (int i = SLOT_OUTPUT_START; i < this.getSizeInventory(); i++) { - if (this.mInventory[i] != null) { - // Logger.INFO("Slot "+i+" contains "+this.mInventory[i].getDisplayName()); - if (GT_Utility.areStacksEqual(aDrop, this.mInventory[i], false)) { - // Same - if (this.mInventory[i].stackSize < 64 - && (this.mInventory[i].stackSize + aDrop.stackSize <= 64)) { - // can merge - // Logger.INFO("Slot "+i+" size: "+mInventory[i].stackSize+" + Drop - // Size: "+aDrop.stackSize+" = "+(mInventory[i].stackSize + - // aDrop.stackSize)); - this.mInventory[i].stackSize += aDrop.stackSize; - break; - } else if (this.mInventory[i].stackSize < 64 - && (this.mInventory[i].stackSize + aDrop.stackSize > 64)) { - // can merge - // Logger.INFO("Slot "+i+" size: "+mInventory[i].stackSize+" + Drop - // Size: "+aDrop.stackSize+" = "+(mInventory[i].stackSize + - // aDrop.stackSize)); - int aRemainder = this.mInventory[i].stackSize + aDrop.stackSize - 64; - Logger.INFO("Remainder: " + aRemainder + ", Continuing."); - this.mInventory[i].stackSize = 64; - aDrop.stackSize = aRemainder; - continue; - } else { - // Logger.INFO("Slot "+i+" size: 64, Continuing."); - continue; - } - } - } else { - // Logger.INFO("Slot "+i+" is empty, setting to "+aDrop.getDisplayName()+" - // x"+aDrop.stackSize); - this.mInventory[i] = aDrop; - break; - } - } - } - } + if (aAllDrops.isEmpty()) return; + + Logger.INFO("Handling " + aAllDrops.size() + " Harvests"); + for (int i = SLOT_OUTPUT_START; i < this.getSizeInventory() && !aAllDrops.isEmpty(); i++) { + ItemStack invStack = mInventory[i]; + if (invStack == null || GT_Utility.isStackInvalid(invStack) || invStack.stackSize == 0) { + Iterator<Entry<ItemStack, Integer>> iter = aAllDrops.entrySet().iterator(); + Entry<ItemStack, Integer> e = iter.next(); + int toAdd = e.getValue(); + int toAddThisSlot = Math.min(toAdd, e.getKey().getMaxStackSize()); + getBaseMetaTileEntity().setInventorySlotContents(i, GT_Utility.copyAmount(toAddThisSlot, e.getKey())); + toAdd -= toAddThisSlot; + if (toAdd <= toAddThisSlot) { + iter.remove(); + } else { + e.setValue(toAdd); + } + } else { + Integer toAddMaybeNull = aAllDrops.get(invStack); + if (toAddMaybeNull != null) { + int toAdd = toAddMaybeNull; + int space = Math.min(invStack.getMaxStackSize(), getInventoryStackLimit()) - invStack.stackSize; + if (toAdd <= space) { + getBaseMetaTileEntity().addStackToSlot(i, invStack, toAdd); + aAllDrops.remove(invStack); + } else { + getBaseMetaTileEntity().addStackToSlot(i, invStack, space); + aAllDrops.put(invStack, toAdd - space); } } } |