From c347f3c479d5f64e908890510d7189f6a598bbb3 Mon Sep 17 00:00:00 2001 From: Sampsa <69092953+S4mpsa@users.noreply.github.com> Date: Fri, 12 Jul 2024 16:06:24 +0300 Subject: Implement cache limits on ME Output Hatch & Bus (#2718) * Implement cache limits on ME output hatches and buses * Make void excess actually void excess * Spotless my beloved * Add method to change base capacity * Make existing hatches have infinite cache and implement dynamic tooltip for current cache size * Improve dynamic tooltip and allow persisitng base cache modifications * Spotless spotless spotless spotless * Remove some unused code --- .../GT_MetaTileEntity_MultiBlockBase.java | 16 ++- .../GT_MetaTileEntity_Hatch_OutputBus_ME.java | 103 +++++++++++++++---- .../GT_MetaTileEntity_Hatch_Output_ME.java | 112 +++++++++++++++++---- 3 files changed, 187 insertions(+), 44 deletions(-) (limited to 'src/main/java') diff --git a/src/main/java/gregtech/api/metatileentity/implementations/GT_MetaTileEntity_MultiBlockBase.java b/src/main/java/gregtech/api/metatileentity/implementations/GT_MetaTileEntity_MultiBlockBase.java index 810f9a123f..3d34a6c62a 100644 --- a/src/main/java/gregtech/api/metatileentity/implementations/GT_MetaTileEntity_MultiBlockBase.java +++ b/src/main/java/gregtech/api/metatileentity/implementations/GT_MetaTileEntity_MultiBlockBase.java @@ -2125,9 +2125,11 @@ public abstract class GT_MetaTileEntity_MultiBlockBase extends MetaTileEntity public List getItemOutputSlots(ItemStack[] toOutput) { List ret = new ArrayList<>(); for (final GT_MetaTileEntity_Hatch tBus : filterValidMTEs(mOutputBusses)) { - final IInventory tBusInv = tBus.getBaseMetaTileEntity(); - for (int i = 0; i < tBusInv.getSizeInventory(); i++) { - ret.add(tBus.getStackInSlot(i)); + if (!(tBus instanceof GT_MetaTileEntity_Hatch_OutputBus_ME)) { + final IInventory tBusInv = tBus.getBaseMetaTileEntity(); + for (int i = 0; i < tBusInv.getSizeInventory(); i++) { + ret.add(tBus.getStackInSlot(i)); + } } } return ret; @@ -2165,7 +2167,9 @@ public abstract class GT_MetaTileEntity_MultiBlockBase extends MetaTileEntity public boolean canDumpItemToME() { for (GT_MetaTileEntity_Hatch tHatch : filterValidMTEs(mOutputBusses)) { if (tHatch instanceof GT_MetaTileEntity_Hatch_OutputBus_ME) { - return true; + if ((((GT_MetaTileEntity_Hatch_OutputBus_ME) tHatch).canAcceptItem())) { + return true; + } } } return false; @@ -2175,7 +2179,9 @@ public abstract class GT_MetaTileEntity_MultiBlockBase extends MetaTileEntity public boolean canDumpFluidToME() { for (IFluidStore tHatch : getFluidOutputSlots(new FluidStack[0])) { if (tHatch instanceof GT_MetaTileEntity_Hatch_Output_ME) { - return true; + if ((((GT_MetaTileEntity_Hatch_Output_ME) tHatch).canAcceptFluid())) { + return true; + } } } return false; 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 16817e1a9c..ee1dac8d8f 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 @@ -18,6 +18,9 @@ import net.minecraft.util.ChatComponentTranslation; import net.minecraft.util.EnumChatFormatting; import net.minecraftforge.common.util.ForgeDirection; +import com.gtnewhorizons.modularui.api.screen.ModularWindow; +import com.gtnewhorizons.modularui.api.screen.UIBuildContext; + import appeng.api.AEApi; import appeng.api.implementations.IPowerChannelState; import appeng.api.networking.GridFlags; @@ -28,6 +31,7 @@ import appeng.api.storage.IMEMonitor; import appeng.api.storage.data.IAEItemStack; import appeng.api.storage.data.IItemList; import appeng.api.util.AECableType; +import appeng.items.storage.ItemBasicStorageCell; import appeng.me.GridAccessException; import appeng.me.helpers.AENetworkProxy; import appeng.me.helpers.IGridProxyable; @@ -36,6 +40,7 @@ import appeng.util.Platform; import appeng.util.ReadableNumberConverter; import gregtech.GT_Mod; import gregtech.api.enums.ItemList; +import gregtech.api.gui.modularui.GT_UIInfos; import gregtech.api.interfaces.ITexture; import gregtech.api.interfaces.tileentity.IGregTechTileEntity; import gregtech.api.metatileentity.MetaTileEntity; @@ -46,15 +51,17 @@ import gregtech.api.util.GT_Utility; public class GT_MetaTileEntity_Hatch_OutputBus_ME extends GT_MetaTileEntity_Hatch_OutputBus implements IPowerChannelState { + private long baseCapacity = 1_600; + private BaseActionSource requestSource = null; private @Nullable AENetworkProxy gridProxy = null; final IItemList itemCache = AEApi.instance() .storage() .createItemList(); long lastOutputTick = 0; + long lastInputTick = 0; long tickCounter = 0; boolean lastOutputFailed = false; - boolean infiniteCache = true; boolean additionalConnection = false; public GT_MetaTileEntity_Hatch_OutputBus_ME(int aID, String aName, String aNameRegional) { @@ -63,15 +70,15 @@ public class GT_MetaTileEntity_Hatch_OutputBus_ME extends GT_MetaTileEntity_Hatc aName, aNameRegional, 3, - new String[] { "Item Output for Multiblocks", "Stores directly into ME", - "Can cache infinite amount of items.", "Change cache behavior by right-clicking with screwdriver.", + new String[] { "Item Output for Multiblocks", "Stores directly into ME", "Can cache 1600 items by default", + "Change cache size by inserting a storage cell", "Change ME connection behavior by right-clicking with wire cutter" }, - 0); + 1); } public GT_MetaTileEntity_Hatch_OutputBus_ME(String aName, int aTier, String[] aDescription, ITexture[][][] aTextures) { - super(aName, aTier, 0, aDescription, aTextures); + super(aName, aTier, 1, aDescription, aTextures); } @Override @@ -101,6 +108,32 @@ public class GT_MetaTileEntity_Hatch_OutputBus_ME extends GT_MetaTileEntity_Hatc return aStack.stackSize == 0; } + private long getCachedAmount() { + long itemAmount = 0; + for (IAEItemStack item : itemCache) { + itemAmount += item.getStackSize(); + } + return itemAmount; + } + + private long getCacheCapacity() { + ItemStack upgradeItemStack = mInventory[0]; + if (upgradeItemStack != null && upgradeItemStack.getItem() instanceof ItemBasicStorageCell) { + return ((ItemBasicStorageCell) upgradeItemStack.getItem()).getBytesLong(upgradeItemStack) * 8; + } + return baseCapacity; + } + + /** + * Check if the internal cache can still fit more items in it + */ + public boolean canAcceptItem() { + if (getCachedAmount() < getCacheCapacity()) { + return true; + } + return false; + } + /** * Attempt to store items in connected ME network. Returns how many items did not fit (if the network was down e.g.) * @@ -108,12 +141,17 @@ public class GT_MetaTileEntity_Hatch_OutputBus_ME extends GT_MetaTileEntity_Hatc * @return amount of items left over */ public int store(final ItemStack stack) { - if (!infiniteCache && lastOutputFailed) return stack.stackSize; - itemCache.add( - AEApi.instance() - .storage() - .createItemStack(stack)); - return 0; + if (lastOutputFailed) return stack.stackSize; + // Always allow insertion on the same tick so we can output the entire recipe + if (canAcceptItem() || (lastInputTick == tickCounter)) { + itemCache.add( + AEApi.instance() + .storage() + .createItemStack(stack)); + lastInputTick = tickCounter; + return 0; + } + return stack.stackSize; } private BaseActionSource getRequest() { @@ -141,15 +179,14 @@ public class GT_MetaTileEntity_Hatch_OutputBus_ME extends GT_MetaTileEntity_Hatc @Override public boolean onRightclick(IGregTechTileEntity aBaseMetaTileEntity, EntityPlayer aPlayer) { - return false; + GT_UIInfos.openGTTileEntityUI(aBaseMetaTileEntity, aPlayer); + return true; } @Override public void onScrewdriverRightClick(ForgeDirection side, EntityPlayer aPlayer, float aX, float aY, float aZ) { if (!getBaseMetaTileEntity().getCoverInfoAtSide(side) .isGUIClickable()) return; - infiniteCache = !infiniteCache; - aPlayer.addChatComponentMessage(new ChatComponentTranslation("GT5U.hatch.infiniteCache." + infiniteCache)); } @Override @@ -227,6 +264,23 @@ public class GT_MetaTileEntity_Hatch_OutputBus_ME extends GT_MetaTileEntity_Hatc super.onPostTick(aBaseMetaTileEntity, aTick); } + @Override + public void addAdditionalTooltipInformation(ItemStack stack, List tooltip) { + + if (stack.hasTagCompound() && stack.stackTagCompound.hasKey("baseCapacity")) { + tooltip.add( + "Current cache capacity: " + EnumChatFormatting.YELLOW + + ReadableNumberConverter.INSTANCE + .toWideReadableForm(stack.stackTagCompound.getLong("baseCapacity"))); + } + } + + @Override + public void setItemNBT(NBTTagCompound aNBT) { + super.setItemNBT(aNBT); + aNBT.setLong("baseCapacity", baseCapacity); + } + @Override public void saveNBTData(NBTTagCompound aNBT) { super.saveNBTData(aNBT); @@ -239,9 +293,9 @@ public class GT_MetaTileEntity_Hatch_OutputBus_ME extends GT_MetaTileEntity_Hatc tag.setLong("size", s.getStackSize()); items.appendTag(tag); } - aNBT.setBoolean("infiniteCache", infiniteCache); aNBT.setBoolean("additionalConnection", additionalConnection); aNBT.setTag("cachedItems", items); + aNBT.setLong("baseCapacity", baseCapacity); getProxy().writeToNBT(aNBT); } @@ -279,10 +333,12 @@ public class GT_MetaTileEntity_Hatch_OutputBus_ME extends GT_MetaTileEntity_Hatc } } } - if (aNBT.hasKey("infiniteCache")) { - infiniteCache = aNBT.getBoolean("infiniteCache"); - } additionalConnection = aNBT.getBoolean("additionalConnection"); + baseCapacity = aNBT.getLong("baseCapacity"); + // Set the base capacity of existing hatches to be infinite + if (baseCapacity == 0) { + baseCapacity = Long.MAX_VALUE; + } getProxy().readFromNBT(aNBT); } @@ -301,10 +357,11 @@ public class GT_MetaTileEntity_Hatch_OutputBus_ME extends GT_MetaTileEntity_Hatc ss.add( "The bus is " + ((getProxy() != null && getProxy().isActive()) ? EnumChatFormatting.GREEN + "online" : EnumChatFormatting.RED + "offline" + getAEDiagnostics()) + EnumChatFormatting.RESET); + IWideReadableNumberConverter nc = ReadableNumberConverter.INSTANCE; + ss.add("Item cache capacity: " + nc.toWideReadableForm(getCacheCapacity())); if (itemCache.isEmpty()) { ss.add("The bus has no cached items"); } else { - IWideReadableNumberConverter nc = ReadableNumberConverter.INSTANCE; ss.add(String.format("The bus contains %d cached stacks: ", itemCache.size())); int counter = 0; for (IAEItemStack s : itemCache) { @@ -322,6 +379,12 @@ public class GT_MetaTileEntity_Hatch_OutputBus_ME extends GT_MetaTileEntity_Hatc @Override public boolean useModularUI() { - return false; + return true; } + + @Override + public void addUIWidgets(ModularWindow.Builder builder, UIBuildContext buildContext) { + getBaseMetaTileEntity().add1by1Slot(builder); + } + } diff --git a/src/main/java/gregtech/common/tileentities/machines/GT_MetaTileEntity_Hatch_Output_ME.java b/src/main/java/gregtech/common/tileentities/machines/GT_MetaTileEntity_Hatch_Output_ME.java index 4df8ff68c5..80ab7615b9 100644 --- a/src/main/java/gregtech/common/tileentities/machines/GT_MetaTileEntity_Hatch_Output_ME.java +++ b/src/main/java/gregtech/common/tileentities/machines/GT_MetaTileEntity_Hatch_Output_ME.java @@ -10,6 +10,7 @@ import java.util.List; import javax.annotation.Nullable; import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTBase; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.nbt.NBTTagList; @@ -18,6 +19,10 @@ import net.minecraft.util.EnumChatFormatting; import net.minecraftforge.common.util.ForgeDirection; import net.minecraftforge.fluids.FluidStack; +import com.glodblock.github.common.item.FCBaseItemCell; +import com.gtnewhorizons.modularui.api.screen.ModularWindow; +import com.gtnewhorizons.modularui.api.screen.UIBuildContext; + import appeng.api.AEApi; import appeng.api.config.Actionable; import appeng.api.config.PowerMultiplier; @@ -42,6 +47,7 @@ import appeng.util.ReadableNumberConverter; import gregtech.GT_Mod; import gregtech.api.enums.GT_Values; import gregtech.api.enums.ItemList; +import gregtech.api.gui.modularui.GT_UIInfos; import gregtech.api.interfaces.ITexture; import gregtech.api.interfaces.tileentity.IGregTechTileEntity; import gregtech.api.metatileentity.MetaTileEntity; @@ -51,15 +57,17 @@ import gregtech.api.util.GT_Utility; public class GT_MetaTileEntity_Hatch_Output_ME extends GT_MetaTileEntity_Hatch_Output implements IPowerChannelState { + private long baseCapacity = 128_000; + private BaseActionSource requestSource = null; private @Nullable AENetworkProxy gridProxy = null; final IItemList fluidCache = AEApi.instance() .storage() .createFluidList(); long lastOutputTick = 0; + long lastInputTick = 0; long tickCounter = 0; boolean lastOutputFailed = false; - boolean infiniteCache = true; boolean additionalConnection = false; public GT_MetaTileEntity_Hatch_Output_ME(int aID, String aName, String aNameRegional) { @@ -69,13 +77,13 @@ public class GT_MetaTileEntity_Hatch_Output_ME extends GT_MetaTileEntity_Hatch_O aNameRegional, 3, new String[] { "Fluid Output for Multiblocks", "Stores directly into ME", - "Can cache infinite amount of fluids.", "Change cache behavior by right-clicking with screwdriver.", + "Can cache up to 128kL of fluids by default", "Change cache size by inserting a fluid storage cell", "Change ME connection behavior by right-clicking with wire cutter" }, - 0); + 1); } public GT_MetaTileEntity_Hatch_Output_ME(String aName, int aTier, String[] aDescription, ITexture[][][] aTextures) { - super(aName, aTier, 0, aDescription, aTextures); + super(aName, aTier, 1, aDescription, aTextures); } @Override @@ -109,11 +117,42 @@ public class GT_MetaTileEntity_Hatch_Output_ME extends GT_MetaTileEntity_Hatch_O if (doFill) { return tryFillAE(aFluid); } else { - if ((!infiniteCache && lastOutputFailed) || aFluid == null) return 0; + if (lastOutputFailed || aFluid == null) return 0; return aFluid.amount; } } + @Override + public int getCapacity() { + return 0; + } + + private long getCachedAmount() { + long fluidAmount = 0; + for (IAEFluidStack fluid : fluidCache) { + fluidAmount += fluid.getStackSize(); + } + return fluidAmount; + } + + private long getCacheCapacity() { + ItemStack upgradeItemStack = mInventory[0]; + if (upgradeItemStack != null && upgradeItemStack.getItem() instanceof FCBaseItemCell) { + return ((FCBaseItemCell) upgradeItemStack.getItem()).getBytes(upgradeItemStack) * 8; + } + return baseCapacity; + } + + /** + * Check if the internal cache can still fit more fluids in it + */ + public boolean canAcceptFluid() { + if (getCachedAmount() < getCacheCapacity()) { + return true; + } + return false; + } + /** * Attempt to store fluid in connected ME network. Returns how much fluid is accepted (if the network was down e.g.) * @@ -121,12 +160,17 @@ public class GT_MetaTileEntity_Hatch_Output_ME extends GT_MetaTileEntity_Hatch_O * @return amount of fluid filled */ public int tryFillAE(final FluidStack aFluid) { - if ((!infiniteCache && lastOutputFailed) || aFluid == null) return 0; - fluidCache.add( - AEApi.instance() - .storage() - .createFluidStack(aFluid)); - return aFluid.amount; + if (lastOutputFailed || aFluid == null) return 0; + // Always allow insertion on the same tick so we can output the entire recipe + if (canAcceptFluid() || (lastInputTick == tickCounter)) { + fluidCache.add( + AEApi.instance() + .storage() + .createFluidStack(aFluid)); + lastInputTick = tickCounter; + return aFluid.amount; + } + return 0; } private BaseActionSource getRequest() { @@ -154,7 +198,8 @@ public class GT_MetaTileEntity_Hatch_Output_ME extends GT_MetaTileEntity_Hatch_O @Override public boolean onRightclick(IGregTechTileEntity aBaseMetaTileEntity, EntityPlayer aPlayer) { - return false; + GT_UIInfos.openGTTileEntityUI(aBaseMetaTileEntity, aPlayer); + return true; } @Override @@ -172,8 +217,6 @@ public class GT_MetaTileEntity_Hatch_Output_ME extends GT_MetaTileEntity_Hatch_O // Don't allow to lock fluid in me fluid hatch if (!getBaseMetaTileEntity().getCoverInfoAtSide(side) .isGUIClickable()) return; - infiniteCache = !infiniteCache; - aPlayer.addChatComponentMessage(new ChatComponentTranslation("GT5U.hatch.infiniteCacheFluid." + infiniteCache)); } @Override @@ -252,6 +295,24 @@ public class GT_MetaTileEntity_Hatch_Output_ME extends GT_MetaTileEntity_Hatch_O super.onPostTick(aBaseMetaTileEntity, aTick); } + @Override + public void addAdditionalTooltipInformation(ItemStack stack, List tooltip) { + + if (stack.hasTagCompound() && stack.stackTagCompound.hasKey("baseCapacity")) { + tooltip.add( + "Current cache capacity: " + EnumChatFormatting.YELLOW + + ReadableNumberConverter.INSTANCE + .toWideReadableForm(stack.stackTagCompound.getLong("baseCapacity")) + + "L"); + } + } + + @Override + public void setItemNBT(NBTTagCompound aNBT) { + super.setItemNBT(aNBT); + aNBT.setLong("baseCapacity", baseCapacity); + } + @Override public void saveNBTData(NBTTagCompound aNBT) { super.saveNBTData(aNBT); @@ -267,8 +328,8 @@ public class GT_MetaTileEntity_Hatch_Output_ME extends GT_MetaTileEntity_Hatch_O fluids.appendTag(tag); } aNBT.setTag("cachedFluids", fluids); - aNBT.setBoolean("infiniteCache", infiniteCache); aNBT.setBoolean("additionalConnection", additionalConnection); + aNBT.setLong("baseCapacity", baseCapacity); getProxy().writeToNBT(aNBT); } @@ -293,10 +354,12 @@ public class GT_MetaTileEntity_Hatch_Output_ME extends GT_MetaTileEntity_Hatch_O } } } - if (aNBT.hasKey("infiniteCache")) { - infiniteCache = aNBT.getBoolean("infiniteCache"); - } additionalConnection = aNBT.getBoolean("additionalConnection"); + baseCapacity = aNBT.getLong("baseCapacity"); + // Set the base capacity of existing hatches to be infinite + if (baseCapacity == 0) { + baseCapacity = Long.MAX_VALUE; + } getProxy().readFromNBT(aNBT); } @@ -315,10 +378,11 @@ public class GT_MetaTileEntity_Hatch_Output_ME extends GT_MetaTileEntity_Hatch_O ss.add( "The hatch is " + ((getProxy() != null && getProxy().isActive()) ? EnumChatFormatting.GREEN + "online" : EnumChatFormatting.RED + "offline" + getAEDiagnostics()) + EnumChatFormatting.RESET); + IWideReadableNumberConverter nc = ReadableNumberConverter.INSTANCE; + ss.add("Fluid cache capacity: " + nc.toWideReadableForm(getCacheCapacity()) + " mB"); if (fluidCache.isEmpty()) { ss.add("The bus has no cached fluids"); } else { - IWideReadableNumberConverter nc = ReadableNumberConverter.INSTANCE; ss.add(String.format("The hatch contains %d cached fluids: ", fluidCache.size())); int counter = 0; for (IAEFluidStack s : fluidCache) { @@ -380,4 +444,14 @@ public class GT_MetaTileEntity_Hatch_Output_ME extends GT_MetaTileEntity_Hatch_O return input; } + + @Override + public boolean useModularUI() { + return true; + } + + @Override + public void addUIWidgets(ModularWindow.Builder builder, UIBuildContext buildContext) { + getBaseMetaTileEntity().add1by1Slot(builder); + } } -- cgit