From fbf2d6760b7e6ef070d3231387521e48c54d46c5 Mon Sep 17 00:00:00 2001 From: raoulvdberge Date: Thu, 30 Mar 2023 16:53:29 +0200 Subject: [PATCH] Reduced block updates when a controller is turning on and off constantly. Fixes part of #3468 --- CHANGELOG.md | 1 + .../refinedstorage/apiimpl/network/Network.java | 16 +++++++++++----- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3be76a8fe..e46a5be63 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). ### Fixed - Fixed not being able to search with JEI when the Grid is open. +- Reduced block updates when a controller is turning on and off constantly. ## [v1.11.5] - 2023-02-12 diff --git a/src/main/java/com/refinedmods/refinedstorage/apiimpl/network/Network.java b/src/main/java/com/refinedmods/refinedstorage/apiimpl/network/Network.java index a24afc297..dbfac940a 100644 --- a/src/main/java/com/refinedmods/refinedstorage/apiimpl/network/Network.java +++ b/src/main/java/com/refinedmods/refinedstorage/apiimpl/network/Network.java @@ -56,6 +56,7 @@ import java.util.function.Predicate; public class Network implements INetwork, IRedstoneConfigurable { private static final int THROTTLE_INACTIVE_TO_ACTIVE = 20; private static final int THROTTLE_ACTIVE_TO_INACTIVE = 4; + private static final int THROTTLE_ENERGY_TYPE_CHANGE = 20; private static final String NBT_ENERGY = "Energy"; private static final String NBT_ITEM_STORAGE_TRACKER_ID = "ItemStorageTrackerId"; @@ -89,6 +90,7 @@ public class Network implements INetwork, IRedstoneConfigurable { private boolean throttlingDisabled = true; // Will be enabled after first update private boolean couldRun; private int ticksSinceUpdateChanged; + private int ticksSinceEnergyTypeChanged; private int ticks; private long[] tickTimes = new long[100]; private int tickCounter = 0; @@ -213,12 +215,16 @@ public class Network implements INetwork, IRedstoneConfigurable { ControllerBlock.EnergyType energyType = getEnergyType(); if (lastEnergyType != energyType) { - lastEnergyType = energyType; - - BlockState state = level.getBlockState(pos); - if (state.getBlock() instanceof ControllerBlock) { - level.setBlockAndUpdate(pos, state.setValue(ControllerBlock.ENERGY_TYPE, energyType)); + ++ticksSinceEnergyTypeChanged; + if (ticksSinceEnergyTypeChanged > THROTTLE_ENERGY_TYPE_CHANGE) { + lastEnergyType = energyType; + BlockState state = level.getBlockState(pos); + if (state.getBlock() instanceof ControllerBlock) { + level.setBlockAndUpdate(pos, state.setValue(ControllerBlock.ENERGY_TYPE, energyType)); + } } + } else { + ticksSinceEnergyTypeChanged = 0; } tickTimes[tickCounter % tickTimes.length] = Util.getNanos() - tickStart;