Skip to content

Commit

Permalink
Reduce network inits by half when placing cables, Closes #1439
Browse files Browse the repository at this point in the history
  • Loading branch information
rubensworks committed Dec 17, 2024
1 parent 17c9d3a commit 8edbc4a
Showing 1 changed file with 21 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.cyclops.integrateddynamics.item.ItemBlockCable;

import javax.annotation.Nullable;
import java.lang.ref.WeakReference;
import java.util.Collection;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -300,6 +301,8 @@ public static InteractionResult onCableActivated(Level world, BlockPos pos, Bloc
return InteractionResult.PASS;
}

private static WeakReference<LivingEntity> CABLE_PLACER_SNAPSHOT = new WeakReference<>(null);

/**
* This should be called when a cable is added.
* This method automatically notifies the neighbours and (re-)initializes the network if this cable carries one.
Expand All @@ -308,10 +311,16 @@ public static InteractionResult onCableActivated(Level world, BlockPos pos, Bloc
* @param pos The position.
*/
public static void onCableAdded(Level world, BlockPos pos) {
CableHelpers.updateConnectionsNeighbours(world, pos, CableHelpers.ALL_SIDES);
if(!world.isClientSide()) {
NetworkHelpers.initNetwork(world, pos, null)
.ifPresent(network -> NeoForge.EVENT_BUS.post(new NetworkInitializedEvent(network, world, pos, null)));
LivingEntity placer = CABLE_PLACER_SNAPSHOT.get();
if (placer != null) {
CableHelpers.onCableAddedByPlayer(world, pos, placer);
CABLE_PLACER_SNAPSHOT = new WeakReference<>(null);
} else {
CableHelpers.updateConnectionsNeighbours(world, pos, CableHelpers.ALL_SIDES);
if (!world.isClientSide()) {
NetworkHelpers.initNetwork(world, pos, null)
.ifPresent(network -> NeoForge.EVENT_BUS.post(new NetworkInitializedEvent(network, world, pos, null)));
}
}
}

Expand All @@ -324,10 +333,14 @@ public static void onCableAdded(Level world, BlockPos pos) {
* @param placer The entity who placed the cable.
*/
public static void onCableAddedByPlayer(Level world, BlockPos pos, @Nullable LivingEntity placer) {
CableHelpers.updateConnectionsNeighbours(world, pos, CableHelpers.ALL_SIDES);
if(!world.isClientSide()) {
NetworkHelpers.initNetwork(world, pos, null)
.ifPresent(network -> NeoForge.EVENT_BUS.post(new NetworkInitializedEvent(network, world, pos, placer)));
if (world.captureBlockSnapshots) {
CABLE_PLACER_SNAPSHOT = new WeakReference<>(placer);
} else {
CableHelpers.updateConnectionsNeighbours(world, pos, CableHelpers.ALL_SIDES);
if(!world.isClientSide()) {
NetworkHelpers.initNetwork(world, pos, null)
.ifPresent(network -> NeoForge.EVENT_BUS.post(new NetworkInitializedEvent(network, world, pos, placer)));
}
}
}

Expand Down

0 comments on commit 8edbc4a

Please sign in to comment.