Skip to content

Commit

Permalink
use some more longs in acceptor cache
Browse files Browse the repository at this point in the history
  • Loading branch information
thiakil committed Aug 29, 2024
1 parent 2659fab commit bd5d580
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ public InventoryNetwork(Collection<InventoryNetwork> networks) {
public List<AcceptorData> calculateAcceptors(TransitRequest request, TransporterStack stack, Long2ObjectMap<ChunkAccess> chunkMap,
Map<GlobalPos, Set<TransporterStack>> additionalFlowingStacks) {
List<AcceptorData> toReturn = new ArrayList<>();
for (Map.Entry<BlockPos, Map<Direction, IItemHandler>> entry : acceptorCache.getAcceptorEntrySet()) {
BlockPos pos = entry.getKey();
for (Long2ObjectMap.Entry<Map<Direction, IItemHandler>> entry : acceptorCache.getAcceptorEntrySet()) {
BlockPos pos = BlockPos.of(entry.getLongKey());
if (!pos.equals(stack.homeLocation)) {
BlockEntity acceptor = WorldUtils.getTileEntity(getWorld(), chunkMap, pos);
Map<TransitResponse, AcceptorData> dataMap = new HashMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ public void onUpdateServer() {
//Otherwise, try to insert it into the destination inventory
//Get the handler we are trying to insert into from the network's acceptor cache
Direction side = stack.getSide(this).getOpposite();
IItemHandler acceptor = network.getCachedAcceptor(nextPos, side);
IItemHandler acceptor = network.getCachedAcceptor(next, side);
if (acceptor == null && stack.getPathType().isHome()) {
//TODO: Cache this capability. The issue is that when we are sending it back home
// if it pulled the item itself, then it isn't in our cached acceptors, and thus won't be able to insert it
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,10 @@ public BlockPos getBlockPos() {
return transmitterTile.getBlockPos();
}

public long getWorldPositionLong() {
return transmitterTile.getWorldPositionLong();
}

@Override
public Level getLevel() {
return transmitterTile.getLevel();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ private Destination getDestination(LongList ret, @Nullable LogisticalTransporter
//If we are connected to a side, idle towards it, the path not pointing at a transmitter
// is gracefully handled
transportStack.idleDir = side;
ret.add(relativePos(start.asLong(), side));
ret.add(WorldUtils.relativePos(start.asLong(), side));
return createDestination(ret);
}
}
Expand Down Expand Up @@ -527,7 +527,4 @@ public interface DestChecker {
}
}

private static long relativePos(long pos, Direction direction) {
return BlockPos.asLong(BlockPos.getX(pos) + direction.getStepX(), BlockPos.getY(pos) + direction.getStepY(), BlockPos.getZ(pos) + direction.getStepZ());
}
}
3 changes: 2 additions & 1 deletion src/main/java/mekanism/common/item/ItemNetworkReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ private void displayTransmitterInfo(Player player, Level level, BlockPos pos, Ti
private void displayConnectedNetworks(Player player, Level world, BlockPos pos) {
Set<DynamicNetwork<?, ?, ?>> iteratedNetworks = new ObjectOpenHashSet<>();
BlockPos.MutableBlockPos mutable = new BlockPos.MutableBlockPos();
long posAsLong = pos.asLong();
for (Direction side : EnumUtils.DIRECTIONS) {
mutable.setWithOffset(pos, side);
BlockEntity tile = WorldUtils.getTileEntity(world, mutable);
Expand All @@ -123,7 +124,7 @@ private void displayConnectedNetworks(Player player, Level world, BlockPos pos)
if (transmitterNetwork.hasAcceptor(pos) && !iteratedNetworks.contains(transmitterNetwork)) {
displayBorder(player, compileList(transmitter.getSupportedTransmissionTypes()), false);
player.sendSystemMessage(MekanismLang.NETWORK_READER_CONNECTED_SIDES.translateColored(EnumColor.GRAY, EnumColor.DARK_GRAY,
compileList(transmitterNetwork.getAcceptorDirections(pos))));
compileList(transmitterNetwork.getAcceptorDirections(posAsLong))));
displayEndBorder(player);
iteratedNetworks.add(transmitterNetwork);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -256,11 +256,11 @@ public boolean hasAcceptor(BlockPos acceptorPos) {
return acceptorCache.hasAcceptor(acceptorPos);
}

public ACCEPTOR getCachedAcceptor(BlockPos acceptorPos, Direction side) {
public ACCEPTOR getCachedAcceptor(long acceptorPos, Direction side) {
return acceptorCache.getCachedAcceptor(acceptorPos, side);
}

public Set<Direction> getAcceptorDirections(BlockPos pos) {
public Set<Direction> getAcceptorDirections(long pos) {
return acceptorCache.getAcceptorDirections(pos);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package mekanism.common.lib.transmitter.acceptor;

import it.unimi.dsi.fastutil.longs.Long2ObjectMap;
import it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap;
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
import java.util.Collection;
import java.util.Collections;
Expand All @@ -10,19 +12,20 @@
import java.util.Set;
import mekanism.common.content.network.transmitter.Transmitter;
import mekanism.common.lib.transmitter.TransmitterNetworkRegistry;
import mekanism.common.util.WorldUtils;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import org.jetbrains.annotations.Nullable;

public class NetworkAcceptorCache<ACCEPTOR> {

private final Map<BlockPos, Map<Direction, ACCEPTOR>> cachedAcceptors = new Object2ObjectOpenHashMap<>();
private final Long2ObjectMap<Map<Direction, ACCEPTOR>> cachedAcceptors = new Long2ObjectOpenHashMap<>();
private final Map<Transmitter<ACCEPTOR, ?, ?>, Set<Direction>> changedAcceptors = new Object2ObjectOpenHashMap<>();

public void updateTransmitterOnSide(Transmitter<ACCEPTOR, ?, ?> transmitter, Direction side) {
transmitter.refreshAcceptorConnections(side);
ACCEPTOR acceptor = transmitter.canConnectToAcceptor(side) ? transmitter.getAcceptor(side) : null;
BlockPos acceptorPos = transmitter.getBlockPos().relative(side);
long acceptorPos = WorldUtils.relativePos(transmitter.getWorldPositionLong(), side);
if (acceptor == null) {
Map<Direction, ACCEPTOR> cached = cachedAcceptors.get(acceptorPos);
if (cached != null) {
Expand All @@ -37,8 +40,8 @@ public void updateTransmitterOnSide(Transmitter<ACCEPTOR, ?, ?> transmitter, Dir
}

public void adoptAcceptors(NetworkAcceptorCache<ACCEPTOR> other) {
for (Entry<BlockPos, Map<Direction, ACCEPTOR>> entry : other.cachedAcceptors.entrySet()) {
BlockPos pos = entry.getKey();
for (Long2ObjectMap.Entry<Map<Direction, ACCEPTOR>> entry : other.cachedAcceptors.long2ObjectEntrySet()) {
long pos = entry.getLongKey();
if (cachedAcceptors.containsKey(pos)) {
cachedAcceptors.get(pos).putAll(entry.getValue());
} else {
Expand Down Expand Up @@ -83,8 +86,8 @@ public void deregister() {
/**
* @apiNote Listeners should not be added to these LazyOptionals here as they may not correspond to an actual handler and may not get invalidated.
*/
public Set<Map.Entry<BlockPos, Map<Direction, ACCEPTOR>>> getAcceptorEntrySet() {
return cachedAcceptors.entrySet();
public Set<Long2ObjectMap.Entry<Map<Direction, ACCEPTOR>>> getAcceptorEntrySet() {
return cachedAcceptors.long2ObjectEntrySet();
}

/**
Expand All @@ -100,15 +103,15 @@ public int getAcceptorCount() {
}

public boolean hasAcceptor(BlockPos acceptorPos) {
return cachedAcceptors.containsKey(acceptorPos);
return cachedAcceptors.containsKey(acceptorPos.asLong());
}

@Nullable
public ACCEPTOR getCachedAcceptor(BlockPos acceptorPos, Direction side) {
public ACCEPTOR getCachedAcceptor(long acceptorPos, Direction side) {
return cachedAcceptors.getOrDefault(acceptorPos, Collections.emptyMap()).get(side);
}

public Set<Direction> getAcceptorDirections(BlockPos pos) {
public Set<Direction> getAcceptorDirections(long pos) {
//TODO: Do this better?
return cachedAcceptors.get(pos).keySet();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,11 @@ public abstract class TileEntityUpdateable extends BlockEntity implements ITileW
private GlobalPos cachedCoord;
private boolean cacheCoord;
private long lastSave;
private final long worldPositionLong;

public TileEntityUpdateable(TileEntityTypeRegistryObject<?> type, BlockPos pos, BlockState state) {
super(type.get(), pos, state);
this.worldPositionLong = pos.asLong();
}

/**
Expand Down Expand Up @@ -210,6 +212,10 @@ public GlobalPos getTileGlobalPos() {
return cacheCoord && cachedCoord != null ? cachedCoord : ITileWrapper.super.getTileGlobalPos();
}

public long getWorldPositionLong() {
return worldPositionLong;
}

@Override
public Chunk3D getTileChunk() {
if (cacheCoord && cachedCoord != null) {
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/mekanism/common/util/WorldUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -830,4 +830,16 @@ public static boolean canSeeSun(@Nullable Level world, BlockPos pos) {
public static BlockPos getBlockPosFromChunkPos(long chunkPos) {
return new BlockPos((int) chunkPos, 0, (int) (chunkPos >> 32));
}

/**
* Get a packed block pos on the relative side.
* @see BlockPos#relative(Direction)
*
* @param pos Source position
* @param direction direction to offset in
* @return the packed offset position
*/
public static long relativePos(long pos, Direction direction) {
return BlockPos.asLong(BlockPos.getX(pos) + direction.getStepX(), BlockPos.getY(pos) + direction.getStepY(), BlockPos.getZ(pos) + direction.getStepZ());
}
}

0 comments on commit bd5d580

Please sign in to comment.