From a097f3d94ac605af83c1a409e8e238cae6128b57 Mon Sep 17 00:00:00 2001 From: Sara Freimer Date: Sun, 25 Aug 2024 19:33:40 -0500 Subject: [PATCH] Have fluid tanks make use of the update delay config for determining how quickly they lose the lighting that is provided to them from their stored fluid. Greatly reduces lighting update frequency in some setups --- .../mekanism/client/render/HUDRenderer.java | 1 + .../common/tile/TileEntityFluidTank.java | 43 +++++++++++++++---- 2 files changed, 36 insertions(+), 8 deletions(-) diff --git a/src/main/java/mekanism/client/render/HUDRenderer.java b/src/main/java/mekanism/client/render/HUDRenderer.java index 022d6dc11b7..587fb100620 100644 --- a/src/main/java/mekanism/client/render/HUDRenderer.java +++ b/src/main/java/mekanism/client/render/HUDRenderer.java @@ -148,6 +148,7 @@ private void renderHUDElement(Font font, GuiGraphics guiGraphics, int x, int y, MekanismRenderer.color(guiGraphics, color); guiGraphics.blit(element.getIcon(), iconRight ? x + font.width(element.getText()) + 2 : x, y, 0, 0, 16, 16, 16, 16); MekanismRenderer.resetColor(guiGraphics); + //TODO: Batch the string draw calls that are in this class guiGraphics.drawString(font, element.getText(), iconRight ? x : x + 18, y + 5, element.getColor(), false); } diff --git a/src/main/java/mekanism/common/tile/TileEntityFluidTank.java b/src/main/java/mekanism/common/tile/TileEntityFluidTank.java index e90b6651c03..26ce4d1b5c5 100644 --- a/src/main/java/mekanism/common/tile/TileEntityFluidTank.java +++ b/src/main/java/mekanism/common/tile/TileEntityFluidTank.java @@ -23,6 +23,7 @@ import mekanism.common.capabilities.holder.slot.IInventorySlotHolder; import mekanism.common.capabilities.holder.slot.InventorySlotHelper; import mekanism.common.capabilities.proxy.ProxyFluidHandler; +import mekanism.common.config.MekanismConfig; import mekanism.common.integration.computer.ComputerException; import mekanism.common.integration.computer.SpecialComputerMethodWrapper.ComputerFluidTankWrapper; import mekanism.common.integration.computer.SpecialComputerMethodWrapper.ComputerIInventorySlotWrapper; @@ -100,7 +101,8 @@ public class TileEntityFluidTank extends TileEntityMekanism implements IConfigur @WrappingComputerMethod(wrapper = ComputerIInventorySlotWrapper.class, methodNames = "getOutputItem", docPlaceholder = "output slot") OutputInventorySlot outputSlot; - private boolean updateClientLight = false; + private int lastLightLevel; + private int lightUpdateDelay; public TileEntityFluidTank(IBlockProvider blockProvider, BlockPos pos, BlockState state) { super(blockProvider, pos, state); @@ -135,9 +137,19 @@ protected IInventorySlotHolder getInitialInventory(IContentsListener listener) { @Override protected void onUpdateClient() { super.onUpdateClient(); - if (updateClientLight) { - WorldUtils.recheckLighting(level, worldPosition); - updateClientLight = false; + checkLight(); + } + + private void checkLight() { + if (lightUpdateDelay > 0) { + lightUpdateDelay--; + if (lightUpdateDelay == 0) { + int lightLevel = getBlockType().getLightEmission(getBlockState(), level, worldPosition); + if (lightLevel != lastLightLevel) { + lastLightLevel = lightLevel; + WorldUtils.recheckLighting(level, worldPosition); + } + } } } @@ -151,6 +163,7 @@ protected boolean onUpdateServer() { needsPacket = true; } } + checkLight(); float scale = MekanismUtils.getScale(prevScale, fluidTank); //TODO - 1.21: Figure out handling of stacked tanks where it may be going back and forth between being full and not? @@ -159,8 +172,9 @@ protected boolean onUpdateServer() { if (prevScale == 0 || scale == 0) { //If it was empty and no longer is, or wasn't empty and now is empty we want to recheck the block lighting // as the fluid may have changed and have a light value - //TODO - 1.21: Do we want to use the active delay for turning lighting off? - WorldUtils.recheckLighting(level, worldPosition); + if (lightUpdateDelay == 0) { + lightUpdateDelay = prevScale == 0 ? 1 : MekanismConfig.general.blockDeactivationDelay.get(); + } } prevScale = scale; sendUpdatePacket = true; @@ -325,6 +339,18 @@ public void addContainerTrackers(MekanismContainer container) { container.track(SyncableEnum.create(ContainerEditMode.BY_ID, ContainerEditMode.BOTH, () -> editMode, value -> editMode = value)); } + @Override + public void loadAdditional(@NotNull CompoundTag nbt, @NotNull HolderLookup.Provider provider) { + super.loadAdditional(nbt, provider); + NBTUtils.setIntIfPresent(nbt, SerializationConstants.DELAY, value -> lightUpdateDelay = value); + } + + @Override + public void saveAdditional(@NotNull CompoundTag nbtTags, @NotNull HolderLookup.Provider provider) { + super.saveAdditional(nbtTags, provider); + nbtTags.putInt(SerializationConstants.DELAY, lightUpdateDelay); + } + @NotNull @Override public CompoundTag getReducedUpdateTag(@NotNull HolderLookup.Provider provider) { @@ -370,11 +396,12 @@ public void handleUpdateTag(@NotNull CompoundTag tag, @NotNull HolderLookup.Prov //NBTUtils.setFluidStackIfPresent(provider, tag, SerializationConstants.FLUID, fluid -> fluidTank.setStack(fluid)); //NBTUtils.setFluidStackIfPresent(provider, tag, SerializationConstants.VALVE, fluid -> valveFluid = fluid); NBTUtils.setFloatIfPresent(tag, SerializationConstants.SCALE, scale -> { - if (MekanismUtils.scaleChanged(prevScale, scale)) { + if (lightUpdateDelay == 0 && MekanismUtils.scaleChanged(prevScale, scale)) { if (prevScale == 0 || scale == 0) { //If it was empty and no longer is, or wasn't empty and now is empty we want to recheck the block lighting // as the fluid may have changed and have a light value, mark that the client should update the light value - updateClientLight = true; + //Note: If we previously had no fluid, we queue the lighting for the next client tick + lightUpdateDelay = prevScale == 0 ? 1 : MekanismConfig.general.blockDeactivationDelay.get(); } } prevScale = scale;