Skip to content

Commit

Permalink
Have fluid tanks make use of the update delay config for determining …
Browse files Browse the repository at this point in the history
…how quickly they lose the lighting that is provided to them from their stored fluid. Greatly reduces lighting update frequency in some setups
  • Loading branch information
pupnewfster committed Aug 26, 2024
1 parent 089b919 commit a097f3d
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 8 deletions.
1 change: 1 addition & 0 deletions src/main/java/mekanism/client/render/HUDRenderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
43 changes: 35 additions & 8 deletions src/main/java/mekanism/common/tile/TileEntityFluidTank.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);

This comment has been minimized.

Copy link
@thiakil

thiakil Aug 26, 2024

Member

i think this needs another method to do this, or use the raw vanilla method - recheckLighting checks if the chunk is loaded, which is quite redundant here

perhaps a TileEntityMekanism protected method that skips the isloaded check?

}
}
}
}

Expand All @@ -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?
Expand All @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit a097f3d

Please sign in to comment.