From cc08b8898a3cdc3a207fe93ff7d2d6b359a66f9b Mon Sep 17 00:00:00 2001 From: repo_alt Date: Thu, 22 Jul 2021 12:41:17 +0300 Subject: Added caching to the ME output bus, to reduce stress on ME system in high output rate cases Also, added item detector cover support to the bus, so that the item detector will signal "full" in case of ME failure https://github.com/GTNewHorizons/GT-New-Horizons-Modpack/issues/8231 --- .../GT_MetaTileEntity_Hatch_OutputBus_ME.java | 100 +++++++++++++++++++-- 1 file changed, 94 insertions(+), 6 deletions(-) (limited to 'src/main/java/gregtech/common/tileentities/machines') diff --git a/src/main/java/gregtech/common/tileentities/machines/GT_MetaTileEntity_Hatch_OutputBus_ME.java b/src/main/java/gregtech/common/tileentities/machines/GT_MetaTileEntity_Hatch_OutputBus_ME.java index 9b74b979f4..ef3689f64c 100644 --- a/src/main/java/gregtech/common/tileentities/machines/GT_MetaTileEntity_Hatch_OutputBus_ME.java +++ b/src/main/java/gregtech/common/tileentities/machines/GT_MetaTileEntity_Hatch_OutputBus_ME.java @@ -21,8 +21,12 @@ import gregtech.api.interfaces.tileentity.IGregTechTileEntity; import gregtech.api.metatileentity.MetaTileEntity; import gregtech.api.metatileentity.implementations.GT_MetaTileEntity_Hatch_OutputBus; import gregtech.api.render.TextureFactory; +import gregtech.api.util.GT_Utility; + import net.minecraft.entity.player.EntityPlayer; import net.minecraft.item.ItemStack; +import net.minecraft.nbt.NBTBase; +import net.minecraft.nbt.NBTTagCompound; import net.minecraftforge.common.util.ForgeDirection; import static gregtech.api.enums.Textures.BlockIcons.OVERLAY_ME_HATCH; @@ -30,6 +34,10 @@ import static gregtech.api.enums.Textures.BlockIcons.OVERLAY_ME_HATCH; public class GT_MetaTileEntity_Hatch_OutputBus_ME extends GT_MetaTileEntity_Hatch_OutputBus { private BaseActionSource requestSource = null; private AENetworkProxy gridProxy = null; + ItemStack cachedStack = null; + long lastOutputTick = 0; + long tickCounter = 0; + boolean lastOutputFailed = false; public GT_MetaTileEntity_Hatch_OutputBus_ME(int aID, String aName, String aNameRegional) { super(aID, aName, aNameRegional, 1, new String[]{ @@ -82,16 +90,67 @@ public class GT_MetaTileEntity_Hatch_OutputBus_ME extends GT_MetaTileEntity_Hatc try { AENetworkProxy proxy = getProxy(); if (proxy == null) - return stack.stackSize; - IMEMonitor sg = proxy.getStorage().getItemInventory(); - IAEItemStack toStore = AEApi.instance().storage().createItemStack(stack); - IAEItemStack rest = Platform.poweredInsert( proxy.getEnergy(), sg, toStore, getRequest()); - if (rest != null) - return (int)rest.getStackSize(); + { + lastOutputFailed = true; + int cacheSize = cachedStack == null ? 0 : cachedStack.stackSize; + cachedStack = null; + return stack.stackSize + cacheSize; + } + if (lastOutputFailed) // if last output failed, don't buffer + { + IMEMonitor sg = proxy.getStorage().getItemInventory(); + IAEItemStack toStore = AEApi.instance().storage().createItemStack(stack); + IAEItemStack rest = Platform.poweredInsert(proxy.getEnergy(), sg, toStore, getRequest()); + if (rest != null && rest.getStackSize() > 0) + return (int) rest.getStackSize(); + else + lastOutputFailed = false; + } + else if (cachedStack != null && ((tickCounter > (lastOutputTick+20)) || !cachedStack.isItemEqual(stack))) + { + lastOutputTick = tickCounter; + boolean sameStack = cachedStack.isItemEqual(stack); + if (sameStack) + cachedStack.stackSize += stack.stackSize; + IMEMonitor sg = proxy.getStorage().getItemInventory(); + IAEItemStack toStore = AEApi.instance().storage().createItemStack(cachedStack); + IAEItemStack rest = Platform.poweredInsert(proxy.getEnergy(), sg, toStore, getRequest()); + if (rest != null && rest.getStackSize() > 0) + { + lastOutputFailed = true; + cachedStack.stackSize = (int)rest.getStackSize(); + if (sameStack) // return all that was cached to sender + { + cachedStack = null; + return (int) rest.getStackSize(); + } + else // leave the cache, and return input to sender + { + cachedStack.stackSize = (int)rest.getStackSize(); + return stack.stackSize; + } + } + else + { + if (!sameStack) + cachedStack = stack.copy(); + else + cachedStack = null; + return 0; + } + } + else + { + if (cachedStack == null) + cachedStack = stack.copy(); + else + cachedStack.stackSize += stack.stackSize; + } return 0; } catch( final GridAccessException ignored ) { + lastOutputFailed = true; } return stack.stackSize; } @@ -131,4 +190,33 @@ public class GT_MetaTileEntity_Hatch_OutputBus_ME extends GT_MetaTileEntity_Hatc @Optional.Method(modid = "appliedenergistics2") public void gridChanged() { } + + @Override + public void onPostTick(IGregTechTileEntity aBaseMetaTileEntity, long aTick) { + tickCounter = aTick; + super.onPostTick(aBaseMetaTileEntity, aTick); + } + + @Override + public void saveNBTData(NBTTagCompound aNBT) + { + super.saveNBTData(aNBT); + if (cachedStack != null) { + NBTTagCompound tTag = new NBTTagCompound(); + cachedStack.writeToNBT(tTag); + aNBT.setTag("cachedStack", tTag); + } + } + + @Override + public void loadNBTData(NBTTagCompound aNBT) { + super.loadNBTData(aNBT); + NBTBase t = aNBT.getTag("cachedStack"); + if (t instanceof NBTTagCompound) + cachedStack = GT_Utility.loadItem((NBTTagCompound)t); + } + + public boolean isLastOutputFailed() { + return lastOutputFailed; + } } -- cgit