Skip to content

Commit

Permalink
fix: network updating twice when activeness changes
Browse files Browse the repository at this point in the history
This bug made it so that every time a network device is placed, it would trigger an extra update
after the initial update to connect
to an network.

This extra update was caused by the fact that the device was
going from inactive to active after the connection was made.

We now only update the network if the direction has changed, since that is the
only component that can influence graph scanning for now.

This bug also caused some flickering with the network transmitter
in combination with redstone mode.
  • Loading branch information
raoulvdberge committed Nov 3, 2023
1 parent cb071db commit 1c2323d
Show file tree
Hide file tree
Showing 15 changed files with 111 additions and 12 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
### Fixed

- Inactive Wireless Transmitter model being emissive.
- Unneeded network graph updating after placing a network device.

## [2.0.0-milestone.3.1] - 2023-10-30

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,6 @@ public void setRemoved() {
PlatformApi.INSTANCE.requestNetworkNodeRemoval(this, level);
}

@Override
@SuppressWarnings("deprecation")
public void setBlockState(final BlockState newBlockState) {
super.setBlockState(newBlockState);
if (level == null || level.isClientSide || node.getNetwork() == null) {
return;
}
PlatformApi.INSTANCE.requestNetworkNodeUpdate(this, level);
}

@Override
public T getNode() {
return node;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import com.refinedmods.refinedstorage2.platform.common.Platform;
import com.refinedmods.refinedstorage2.platform.common.content.BlockEntities;
import com.refinedmods.refinedstorage2.platform.common.content.ContentNames;
import com.refinedmods.refinedstorage2.platform.common.support.AbstractDirectionalBlock;
import com.refinedmods.refinedstorage2.platform.common.support.network.AbstractSchedulingNetworkNodeContainerBlockEntity;
import com.refinedmods.refinedstorage2.platform.common.upgrade.UpgradeDestinations;

Expand Down Expand Up @@ -145,6 +146,12 @@ protected void setTaskExecutor(final TaskExecutor<TaskContext> taskExecutor) {
this.taskExecutor = taskExecutor;
}

@Override
protected boolean doesBlockStateChangeWarrantNetworkNodeUpdate(final BlockState oldBlockState,
final BlockState newBlockState) {
return AbstractDirectionalBlock.doesBlockStateChangeWarrantNetworkNodeUpdate(oldBlockState, newBlockState);
}

protected record TaskContext(Network network, Player player) {
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import com.refinedmods.refinedstorage2.platform.common.Platform;
import com.refinedmods.refinedstorage2.platform.common.content.BlockEntities;
import com.refinedmods.refinedstorage2.platform.common.content.ContentNames;
import com.refinedmods.refinedstorage2.platform.common.support.AbstractDirectionalBlock;
import com.refinedmods.refinedstorage2.platform.common.support.FilterModeSettings;
import com.refinedmods.refinedstorage2.platform.common.support.FilterWithFuzzyMode;
import com.refinedmods.refinedstorage2.platform.common.support.containermenu.ExtendedMenuProvider;
Expand Down Expand Up @@ -151,4 +152,10 @@ public void postDoWork() {
final Player fakePlayer = getFakePlayer(serverLevel);
strategy.apply(filter, actor, getNode()::getNetwork, fakePlayer);
}

@Override
protected boolean doesBlockStateChangeWarrantNetworkNodeUpdate(final BlockState oldBlockState,
final BlockState newBlockState) {
return AbstractDirectionalBlock.doesBlockStateChangeWarrantNetworkNodeUpdate(oldBlockState, newBlockState);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import com.refinedmods.refinedstorage2.platform.common.Platform;
import com.refinedmods.refinedstorage2.platform.common.content.BlockEntities;
import com.refinedmods.refinedstorage2.platform.common.content.ContentNames;
import com.refinedmods.refinedstorage2.platform.common.support.AbstractDirectionalBlock;
import com.refinedmods.refinedstorage2.platform.common.support.FilterWithFuzzyMode;
import com.refinedmods.refinedstorage2.platform.common.support.containermenu.AbstractSingleAmountContainerMenu;
import com.refinedmods.refinedstorage2.platform.common.support.containermenu.ExtendedMenuProvider;
Expand Down Expand Up @@ -177,4 +178,10 @@ public boolean canAcceptIncomingConnection(final Direction incomingDirection, fi
}
return true;
}

@Override
protected boolean doesBlockStateChangeWarrantNetworkNodeUpdate(final BlockState oldBlockState,
final BlockState newBlockState) {
return AbstractDirectionalBlock.doesBlockStateChangeWarrantNetworkNodeUpdate(oldBlockState, newBlockState);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import com.refinedmods.refinedstorage2.platform.common.content.BlockEntities;
import com.refinedmods.refinedstorage2.platform.common.content.ContentNames;
import com.refinedmods.refinedstorage2.platform.common.content.Items;
import com.refinedmods.refinedstorage2.platform.common.support.AbstractDirectionalBlock;
import com.refinedmods.refinedstorage2.platform.common.support.network.AbstractSchedulingNetworkNodeContainerBlockEntity;
import com.refinedmods.refinedstorage2.platform.common.upgrade.UpgradeDestinations;

Expand Down Expand Up @@ -116,4 +117,10 @@ private long getAmountStillNeeded(final long amount, final long currentAmount, f
}
return Math.min(stillNeeding, amount);
}

@Override
protected boolean doesBlockStateChangeWarrantNetworkNodeUpdate(final BlockState oldBlockState,
final BlockState newBlockState) {
return AbstractDirectionalBlock.doesBlockStateChangeWarrantNetworkNodeUpdate(oldBlockState, newBlockState);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import com.refinedmods.refinedstorage2.platform.api.storage.channel.PlatformStorageChannelType;
import com.refinedmods.refinedstorage2.platform.api.support.resource.ItemResource;
import com.refinedmods.refinedstorage2.platform.common.storage.channel.StorageChannelTypes;
import com.refinedmods.refinedstorage2.platform.common.support.AbstractDirectionalBlock;
import com.refinedmods.refinedstorage2.platform.common.support.network.AbstractRedstoneModeNetworkNodeContainerBlockEntity;

import java.util.List;
Expand Down Expand Up @@ -81,4 +82,10 @@ public void removeWatcher(final GridWatcher watcher) {
public final int getPriority() {
return NetworkNodeContainerPriorities.GRID;
}

@Override
protected boolean doesBlockStateChangeWarrantNetworkNodeUpdate(final BlockState oldBlockState,
final BlockState newBlockState) {
return AbstractDirectionalBlock.doesBlockStateChangeWarrantNetworkNodeUpdate(oldBlockState, newBlockState);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import com.refinedmods.refinedstorage2.platform.common.content.BlockEntities;
import com.refinedmods.refinedstorage2.platform.common.content.ContentNames;
import com.refinedmods.refinedstorage2.platform.common.content.Items;
import com.refinedmods.refinedstorage2.platform.common.support.AbstractDirectionalBlock;
import com.refinedmods.refinedstorage2.platform.common.support.FilterModeSettings;
import com.refinedmods.refinedstorage2.platform.common.support.FilterWithFuzzyMode;
import com.refinedmods.refinedstorage2.platform.common.support.containermenu.ExtendedMenuProvider;
Expand Down Expand Up @@ -158,4 +159,10 @@ private long getAmountStillAvailableForImport(final long amount,
}
return Math.min(stillAvailableToImport, amount);
}

@Override
protected boolean doesBlockStateChangeWarrantNetworkNodeUpdate(final BlockState oldBlockState,
final BlockState newBlockState) {
return AbstractDirectionalBlock.doesBlockStateChangeWarrantNetworkNodeUpdate(oldBlockState, newBlockState);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.refinedmods.refinedstorage2.platform.common.content.BlockEntities;
import com.refinedmods.refinedstorage2.platform.common.content.ContentNames;
import com.refinedmods.refinedstorage2.platform.common.storage.StorageConfigurationContainerImpl;
import com.refinedmods.refinedstorage2.platform.common.support.AbstractDirectionalBlock;
import com.refinedmods.refinedstorage2.platform.common.support.BlockEntityWithDrops;
import com.refinedmods.refinedstorage2.platform.common.support.FilterWithFuzzyMode;
import com.refinedmods.refinedstorage2.platform.common.support.containermenu.ExtendedMenuProvider;
Expand Down Expand Up @@ -271,4 +272,10 @@ public NonNullList<ItemStack> getDrops() {
public void writeScreenOpeningData(final ServerPlayer player, final FriendlyByteBuf buf) {
filter.getFilterContainer().writeToUpdatePacket(buf);
}

@Override
protected boolean doesBlockStateChangeWarrantNetworkNodeUpdate(final BlockState oldBlockState,
final BlockState newBlockState) {
return AbstractDirectionalBlock.doesBlockStateChangeWarrantNetworkNodeUpdate(oldBlockState, newBlockState);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.refinedmods.refinedstorage2.platform.common.content.BlockEntities;
import com.refinedmods.refinedstorage2.platform.common.content.ContentNames;
import com.refinedmods.refinedstorage2.platform.common.storage.StorageConfigurationContainerImpl;
import com.refinedmods.refinedstorage2.platform.common.support.AbstractDirectionalBlock;
import com.refinedmods.refinedstorage2.platform.common.support.FilterWithFuzzyMode;
import com.refinedmods.refinedstorage2.platform.common.support.containermenu.ExtendedMenuProvider;
import com.refinedmods.refinedstorage2.platform.common.support.network.AbstractRedstoneModeNetworkNodeContainerBlockEntity;
Expand Down Expand Up @@ -167,4 +168,10 @@ public Component getDisplayName() {
public AbstractContainerMenu createMenu(final int syncId, final Inventory inventory, final Player player) {
return new ExternalStorageContainerMenu(syncId, player, filter.getFilterContainer(), configContainer);
}

@Override
protected boolean doesBlockStateChangeWarrantNetworkNodeUpdate(final BlockState oldBlockState,
final BlockState newBlockState) {
return AbstractDirectionalBlock.doesBlockStateChangeWarrantNetworkNodeUpdate(oldBlockState, newBlockState);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -176,4 +176,5 @@ public void writeScreenOpeningData(final ServerPlayer player, final FriendlyByte
buf.writeLong(getNode().getCapacity());
filter.getFilterContainer().writeToUpdatePacket(buf);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import com.refinedmods.refinedstorage2.platform.common.Platform;
import com.refinedmods.refinedstorage2.platform.common.content.BlockEntities;
import com.refinedmods.refinedstorage2.platform.common.content.ContentNames;
import com.refinedmods.refinedstorage2.platform.common.support.AbstractDirectionalBlock;
import com.refinedmods.refinedstorage2.platform.common.support.FilterWithFuzzyMode;
import com.refinedmods.refinedstorage2.platform.common.support.containermenu.ExtendedMenuProvider;
import com.refinedmods.refinedstorage2.platform.common.support.network.AbstractRedstoneModeNetworkNodeContainerBlockEntity;
Expand Down Expand Up @@ -322,4 +323,10 @@ private void sendDisplayUpdate(final Level level, final long amount, final boole
level.sendBlockUpdated(worldPosition, getBlockState(), getBlockState(), Block.UPDATE_ALL);
LOGGER.debug("Sending display update for storage monitor {} with amount {}", worldPosition, amount);
}

@Override
protected boolean doesBlockStateChangeWarrantNetworkNodeUpdate(final BlockState oldBlockState,
final BlockState newBlockState) {
return AbstractDirectionalBlock.doesBlockStateChangeWarrantNetworkNodeUpdate(oldBlockState, newBlockState);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.refinedmods.refinedstorage2.platform.common.support.direction.DirectionType;

import java.util.Objects;
import javax.annotation.Nullable;

import net.minecraft.core.Direction;
Expand Down Expand Up @@ -68,4 +69,18 @@ public Direction extractDirection(@Nullable final BlockState state) {
}
return getDirectionType().extractDirection(direction);
}

public static boolean doesBlockStateChangeWarrantNetworkNodeUpdate(final BlockState oldBlockState,
final BlockState newBlockState) {
if (!(newBlockState.getBlock() instanceof AbstractDirectionalBlock<?> newDirectionalBlock)) {
return true;
}
if (!(oldBlockState.getBlock() instanceof AbstractDirectionalBlock<?> oldDirectionalBlock)) {
return true;
}
return !Objects.equals(
oldDirectionalBlock.getDirection(oldBlockState),
newDirectionalBlock.getDirection(newBlockState)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.refinedmods.refinedstorage2.api.network.component.EnergyNetworkComponent;
import com.refinedmods.refinedstorage2.api.network.node.AbstractNetworkNode;
import com.refinedmods.refinedstorage2.platform.api.PlatformApi;
import com.refinedmods.refinedstorage2.platform.api.support.network.AbstractNetworkNodeContainerBlockEntity;
import com.refinedmods.refinedstorage2.platform.api.support.network.ConnectionSink;
import com.refinedmods.refinedstorage2.platform.common.support.AbstractDirectionalBlock;
Expand Down Expand Up @@ -57,15 +58,15 @@ public void updateActiveness(final BlockState state, @Nullable final BooleanProp
}

protected void activenessChanged(final boolean newActive) {
LOGGER.info("Activeness change for node at {}: {} -> {}", getBlockPos(), getNode().isActive(), newActive);
LOGGER.debug("Activeness change for node at {}: {} -> {}", getBlockPos(), getNode().isActive(), newActive);
getNode().setActive(newActive);
}

private void updateActivenessBlockState(final BlockState state,
final BooleanProperty activenessProperty,
final boolean active) {
if (level != null) {
LOGGER.info(
LOGGER.debug(
"Sending block update at {} due to activeness change: {} -> {}",
getBlockPos(),
state.getValue(activenessProperty),
Expand Down Expand Up @@ -142,4 +143,25 @@ protected final void updateBlock() {
}
level.sendBlockUpdated(getBlockPos(), getBlockState(), getBlockState(), Block.UPDATE_ALL);
}

protected boolean doesBlockStateChangeWarrantNetworkNodeUpdate(
final BlockState oldBlockState,
final BlockState newBlockState
) {
return false;
}

@Override
@SuppressWarnings("deprecation")
public void setBlockState(final BlockState newBlockState) {
final BlockState oldBlockState = getBlockState();
super.setBlockState(newBlockState);
if (!doesBlockStateChangeWarrantNetworkNodeUpdate(oldBlockState, newBlockState)) {
return;
}
if (level == null || level.isClientSide || getNode().getNetwork() == null) {
return;
}
PlatformApi.INSTANCE.requestNetworkNodeUpdate(this, level);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.refinedmods.refinedstorage2.platform.common.Platform;
import com.refinedmods.refinedstorage2.platform.common.content.BlockEntities;
import com.refinedmods.refinedstorage2.platform.common.content.ContentNames;
import com.refinedmods.refinedstorage2.platform.common.support.AbstractDirectionalBlock;
import com.refinedmods.refinedstorage2.platform.common.support.containermenu.ExtendedMenuProvider;
import com.refinedmods.refinedstorage2.platform.common.support.network.AbstractRedstoneModeNetworkNodeContainerBlockEntity;
import com.refinedmods.refinedstorage2.platform.common.upgrade.UpgradeContainer;
Expand Down Expand Up @@ -125,4 +126,10 @@ public boolean isInRange(final ResourceKey<Level> dimension, final Vec3 position
);
return distance <= getRange();
}

@Override
protected boolean doesBlockStateChangeWarrantNetworkNodeUpdate(final BlockState oldBlockState,
final BlockState newBlockState) {
return AbstractDirectionalBlock.doesBlockStateChangeWarrantNetworkNodeUpdate(oldBlockState, newBlockState);
}
}

0 comments on commit 1c2323d

Please sign in to comment.