9
0
mirror of https://github.com/Samsuik/Sakura.git synced 2025-12-19 14:59:30 +00:00

Move skipWireUpdate redstone implementation check

This commit is contained in:
Samsuik
2025-03-06 18:17:31 +00:00
parent 6c49e8527f
commit 072ee3b51f
2 changed files with 35 additions and 36 deletions

View File

@@ -78,8 +78,9 @@ public final class RedstoneNetwork {
} }
} }
public boolean prepareAndRegister(Level level) { public boolean prepareAndRegisterListeners(Level level, RedstoneNetworkSource networkSource) {
Object2ObjectLinkedOpenHashMap<BlockPos, RedstoneWireUpdate> processedWires = new Object2ObjectLinkedOpenHashMap<>(); Object2ObjectLinkedOpenHashMap<BlockPos, RedstoneWireUpdate> processedWires = new Object2ObjectLinkedOpenHashMap<>();
boolean skipWireUpdates = networkSource.redstoneImplementation() != WorldConfiguration.Misc.RedstoneImplementation.VANILLA;
for (RedstoneWireUpdate wireUpdate : this.wireUpdates.reversed()) { for (RedstoneWireUpdate wireUpdate : this.wireUpdates.reversed()) {
BlockPos wirePos = wireUpdate.getPosition(); BlockPos wirePos = wireUpdate.getPosition();
//noinspection ConstantValue //noinspection ConstantValue
@@ -89,7 +90,7 @@ public final class RedstoneNetwork {
if (state.is(Blocks.PISTON_HEAD)) { if (state.is(Blocks.PISTON_HEAD)) {
return false; return false;
} }
} else if (this.originalWirePower.get(wirePos).firstPower() != wireUpdate.getPower()) { } else if (skipWireUpdates && this.originalWirePower.get(wirePos).firstPower() != wireUpdate.getPower()) {
// Filter out wires updates that are not the first and last update // Filter out wires updates that are not the first and last update
// This significantly reduces the amount of updates when unpowering // This significantly reduces the amount of updates when unpowering
wireUpdate.skipWireUpdate(); wireUpdate.skipWireUpdate();
@@ -156,7 +157,7 @@ public final class RedstoneNetwork {
} }
} }
public boolean applyFromCache(Level level, WorldConfiguration.Misc.RedstoneImplementation redstoneImplementation) { public boolean applyFromCache(Level level) {
this.expiry.refresh(level.getGameTime()); this.expiry.refresh(level.getGameTime());
if (!this.isRegistered() || !this.verifyWiresInNetwork(level)) { if (!this.isRegistered() || !this.verifyWiresInNetwork(level)) {
return false; return false;
@@ -167,7 +168,7 @@ public final class RedstoneNetwork {
int updateFrom = 0; int updateFrom = 0;
for (RedstoneWireUpdate wireUpdate : this.wireUpdates) { for (RedstoneWireUpdate wireUpdate : this.wireUpdates) {
if (wireUpdate.canSkipWireUpdate() && redstoneImplementation != WorldConfiguration.Misc.RedstoneImplementation.VANILLA) { if (wireUpdate.canSkipWireUpdate()) {
updateFrom = wireUpdate.getUpdateIndex(); updateFrom = wireUpdate.getUpdateIndex();
continue; continue;
} }

View File

@@ -41,30 +41,30 @@ public final class RedstoneWireCache {
} }
public boolean tryApplyFromCache(BlockPos pos, @Nullable Orientation orientation, int newPower, int oldPower) { public boolean tryApplyFromCache(BlockPos pos, @Nullable Orientation orientation, int newPower, int oldPower) {
if (!this.isTrackingWireUpdates()) { LocalValueConfig localConfig = this.level.localConfig().config(pos);
LocalValueConfig localConfig = this.level.localConfig().config(pos); if (!localConfig.redstoneCache || this.isTrackingWireUpdates()) {
if (!localConfig.redstoneCache) { return false;
return false; }
}
// Ignore any wire changes while updating the network.
if (this.updatingNetwork != null) { if (this.updatingNetwork != null) {
return true; return true;
} }
RedstoneNetworkSource networkSource = RedstoneNetworkSource.createNetworkSource(this.level, localConfig, pos, orientation, newPower, oldPower); RedstoneNetworkSource networkSource = RedstoneNetworkSource.createNetworkSource(this.level, localConfig, pos, orientation, newPower, oldPower);
RedstoneNetwork network = this.networkCache.get(networkSource); RedstoneNetwork network = this.networkCache.get(networkSource);
if (network != null) { if (network != null) {
try { try {
this.updatingNetwork = network; this.updatingNetwork = network;
return network.applyFromCache(this.level, localConfig.redstoneImplementation); return network.applyFromCache(this.level);
} finally { } finally {
this.updatingNetwork = null; this.updatingNetwork = null;
} }
} } else {
// Start tracking power and neighbor updates
this.networkSource = networkSource; this.networkSource = networkSource;
return false;
} }
return false;
} }
public void trackWirePower(BlockPos pos, int newPower, int oldPower) { public void trackWirePower(BlockPos pos, int newPower, int oldPower) {
@@ -103,16 +103,14 @@ public final class RedstoneWireCache {
return; return;
} }
if (!this.wireUpdates.isEmpty()) { // Cache expires if it has not been used in 600 ticks
// Cache expires if it has not been used in 600 ticks TickExpiry expiration = new TickExpiry(this.level.getGameTime(), 600);
TickExpiry expiration = new TickExpiry(this.level.getGameTime(), 600); RedstoneNetwork redstoneNetwork = new RedstoneNetwork(
RedstoneNetwork redstoneNetwork = new RedstoneNetwork( this.wireUpdates, this.updates, this.originalWirePower, expiration
this.wireUpdates, this.updates, this.originalWirePower, expiration );
);
if (redstoneNetwork.prepareAndRegister(this.level)) { if (redstoneNetwork.prepareAndRegisterListeners(this.level, this.networkSource)) {
this.networkCache.put(this.networkSource, redstoneNetwork); this.networkCache.put(this.networkSource, redstoneNetwork);
}
} }
this.wireUpdates.clear(); this.wireUpdates.clear();