PrettyPipes/src/main/java/de/ellpeck/prettypipes/network/PipeNetwork.java

219 lines
8.5 KiB
Java
Raw Normal View History

2020-04-14 04:21:28 +02:00
package de.ellpeck.prettypipes.network;
import de.ellpeck.prettypipes.Registry;
2020-04-14 14:10:58 +02:00
import de.ellpeck.prettypipes.Utility;
2020-04-14 04:21:28 +02:00
import de.ellpeck.prettypipes.blocks.pipe.PipeBlock;
2020-04-14 14:10:58 +02:00
import de.ellpeck.prettypipes.blocks.pipe.PipeTileEntity;
2020-04-14 17:14:24 +02:00
import de.ellpeck.prettypipes.packets.PacketHandler;
import de.ellpeck.prettypipes.packets.PacketItemEnterPipe;
2020-04-14 04:21:28 +02:00
import net.minecraft.block.BlockState;
2020-04-14 17:14:24 +02:00
import net.minecraft.item.ItemStack;
2020-04-14 04:21:28 +02:00
import net.minecraft.nbt.CompoundNBT;
2020-04-14 15:02:21 +02:00
import net.minecraft.nbt.ListNBT;
import net.minecraft.nbt.NBTUtil;
2020-04-14 04:21:28 +02:00
import net.minecraft.util.Direction;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.capabilities.ICapabilitySerializable;
2020-04-14 15:02:21 +02:00
import net.minecraftforge.common.util.Constants;
2020-04-14 04:21:28 +02:00
import net.minecraftforge.common.util.LazyOptional;
2020-04-14 17:14:24 +02:00
import org.jgrapht.GraphPath;
2020-04-14 04:21:28 +02:00
import org.jgrapht.alg.shortestpath.DijkstraShortestPath;
import org.jgrapht.graph.SimpleWeightedGraph;
2020-04-14 17:14:24 +02:00
import org.jgrapht.traverse.ClosestFirstIterator;
2020-04-14 04:21:28 +02:00
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.ArrayList;
2020-04-14 17:14:24 +02:00
import java.util.HashMap;
2020-04-14 04:21:28 +02:00
import java.util.List;
2020-04-14 17:14:24 +02:00
import java.util.Map;
2020-04-14 21:04:41 +02:00
import java.util.function.Supplier;
2020-04-14 04:21:28 +02:00
public class PipeNetwork implements ICapabilitySerializable<CompoundNBT> {
2020-04-14 14:10:58 +02:00
public final SimpleWeightedGraph<BlockPos, NetworkEdge> graph = new SimpleWeightedGraph<>(NetworkEdge.class);
private final DijkstraShortestPath<BlockPos, NetworkEdge> dijkstra = new DijkstraShortestPath<>(this.graph);
2020-04-14 17:14:24 +02:00
private final Map<BlockPos, PipeTileEntity> tileCache = new HashMap<>();
2020-04-14 04:21:28 +02:00
private final World world;
public PipeNetwork(World world) {
this.world = world;
}
@Nonnull
@Override
public <T> LazyOptional<T> getCapability(@Nonnull Capability<T> cap, @Nullable Direction side) {
return cap == Registry.pipeNetworkCapability ? LazyOptional.of(() -> (T) this) : null;
}
@Override
public CompoundNBT serializeNBT() {
2020-04-14 15:02:21 +02:00
CompoundNBT nbt = new CompoundNBT();
ListNBT nodes = new ListNBT();
for (BlockPos node : this.graph.vertexSet())
nodes.add(NBTUtil.writeBlockPos(node));
nbt.put("nodes", nodes);
ListNBT edges = new ListNBT();
for (NetworkEdge edge : this.graph.edgeSet())
edges.add(edge.serializeNBT());
nbt.put("edges", edges);
return nbt;
2020-04-14 04:21:28 +02:00
}
@Override
public void deserializeNBT(CompoundNBT nbt) {
2020-04-14 15:02:21 +02:00
this.graph.removeAllVertices(new ArrayList<>(this.graph.vertexSet()));
ListNBT nodes = nbt.getList("nodes", Constants.NBT.TAG_COMPOUND);
for (int i = 0; i < nodes.size(); i++)
this.graph.addVertex(NBTUtil.readBlockPos(nodes.getCompound(i)));
ListNBT edges = nbt.getList("edges", Constants.NBT.TAG_COMPOUND);
2020-04-14 17:14:24 +02:00
for (int i = 0; i < edges.size(); i++)
this.addEdge(new NetworkEdge(edges.getCompound(i)));
2020-04-14 04:21:28 +02:00
}
public void addNode(BlockPos pos, BlockState state) {
2020-04-14 17:14:24 +02:00
if (!this.isNode(pos)) {
2020-04-14 04:21:28 +02:00
this.graph.addVertex(pos);
this.refreshNode(pos, state);
}
}
public void removeNode(BlockPos pos) {
2020-04-14 17:14:24 +02:00
if (this.isNode(pos))
2020-04-14 04:21:28 +02:00
this.graph.removeVertex(pos);
}
2020-04-14 17:14:24 +02:00
public boolean isNode(BlockPos pos) {
return this.graph.containsVertex(pos);
}
2020-04-14 04:21:28 +02:00
public void onPipeChanged(BlockPos pos, BlockState state) {
2020-04-14 14:10:58 +02:00
List<NetworkEdge> neighbors = this.createAllEdges(pos, state, true);
2020-04-14 04:21:28 +02:00
// if we only have one neighbor, then there can't be any new connections
2020-04-14 17:14:24 +02:00
if (neighbors.size() <= 1 && !this.isNode(pos))
2020-04-14 04:21:28 +02:00
return;
2020-04-14 15:02:21 +02:00
for (NetworkEdge edge : neighbors)
this.refreshNode(edge.endPipe, this.world.getBlockState(edge.endPipe));
2020-04-14 04:21:28 +02:00
}
2020-04-14 17:14:24 +02:00
public boolean tryInsertItem(BlockPos startPipePos, BlockPos originInv, ItemStack stack) {
2020-04-14 21:04:41 +02:00
return this.routeItem(startPipePos, stack, () -> new PipeItem(stack, startPipePos, originInv));
}
public boolean routeItem(BlockPos startPipePos, ItemStack stack, Supplier<PipeItem> itemSupplier) {
2020-04-14 17:14:24 +02:00
if (!this.isNode(startPipePos))
return false;
2020-04-14 21:04:41 +02:00
if (!this.world.isBlockLoaded(startPipePos))
return false;
2020-04-14 17:14:24 +02:00
PipeTileEntity startPipe = this.getPipe(startPipePos);
if (startPipe == null)
return false;
ClosestFirstIterator<BlockPos, NetworkEdge> it = new ClosestFirstIterator<>(this.graph, startPipePos);
while (it.hasNext()) {
PipeTileEntity pipe = this.getPipe(it.next());
// don't try to insert into yourself, duh
if (pipe == startPipe)
continue;
BlockPos dest = pipe.getAvailableDestination(stack);
2020-04-14 21:04:41 +02:00
if (dest != null)
return this.routeItemToLocation(startPipePos, pipe.getPos(), dest, itemSupplier);
2020-04-14 17:14:24 +02:00
}
return false;
}
2020-04-14 21:04:41 +02:00
public boolean routeItemToLocation(BlockPos startPipePos, BlockPos destPipe, BlockPos destInventory, Supplier<PipeItem> itemSupplier) {
if (!this.isNode(startPipePos))
return false;
if (!this.world.isBlockLoaded(startPipePos))
return false;
PipeTileEntity startPipe = this.getPipe(startPipePos);
if (startPipe == null)
return false;
GraphPath<BlockPos, NetworkEdge> path = this.dijkstra.getPath(startPipePos, destPipe);
PipeItem item = itemSupplier.get();
item.setDestination(startPipePos, destPipe, destInventory, path);
if (!startPipe.items.contains(item))
startPipe.items.add(item);
PacketHandler.sendToAllLoaded(this.world, startPipePos, new PacketItemEnterPipe(startPipePos, item));
return true;
}
2020-04-14 17:14:24 +02:00
public PipeTileEntity getPipe(BlockPos pos) {
PipeTileEntity tile = this.tileCache.get(pos);
if (tile == null || tile.isRemoved()) {
tile = Utility.getTileEntity(PipeTileEntity.class, this.world, pos);
this.tileCache.put(pos, tile);
}
return tile;
}
2020-04-14 04:21:28 +02:00
private void refreshNode(BlockPos pos, BlockState state) {
2020-04-14 15:02:21 +02:00
this.graph.removeAllEdges(new ArrayList<>(this.graph.edgesOf(pos)));
for (NetworkEdge edge : this.createAllEdges(pos, state, false))
this.addEdge(edge);
}
2020-04-14 04:21:28 +02:00
2020-04-14 15:02:21 +02:00
private void addEdge(NetworkEdge edge) {
this.graph.addEdge(edge.startPipe, edge.endPipe, edge);
this.graph.setEdgeWeight(edge, edge.pipes.size());
2020-04-14 04:21:28 +02:00
}
2020-04-14 14:10:58 +02:00
private List<NetworkEdge> createAllEdges(BlockPos pos, BlockState state, boolean allAround) {
List<NetworkEdge> edges = new ArrayList<>();
for (Direction dir : Direction.values()) {
NetworkEdge edge = this.createEdge(pos, state, dir, allAround);
if (edge != null)
edges.add(edge);
}
return edges;
2020-04-14 04:21:28 +02:00
}
2020-04-14 14:10:58 +02:00
private NetworkEdge createEdge(BlockPos pos, BlockState state, Direction dir, boolean allAround) {
if (!allAround && !state.get(PipeBlock.DIRECTIONS.get(dir)).isConnected())
return null;
BlockPos currPos = pos.offset(dir);
BlockState currState = this.world.getBlockState(currPos);
if (!(currState.getBlock() instanceof PipeBlock))
return null;
2020-04-14 17:14:24 +02:00
NetworkEdge edge = new NetworkEdge();
2020-04-14 15:02:21 +02:00
edge.startPipe = pos;
edge.pipes.add(pos);
edge.pipes.add(currPos);
2020-04-14 14:10:58 +02:00
while (true) {
// if we found a vertex, we can stop since that's the next node
// we do this here since the first offset pipe also needs to check this
2020-04-14 17:14:24 +02:00
if (this.isNode(currPos)) {
2020-04-14 14:10:58 +02:00
edge.endPipe = edge.pipes.get(edge.pipes.size() - 1);
return edge;
}
boolean found = false;
for (Direction nextDir : Direction.values()) {
if (!currState.get(PipeBlock.DIRECTIONS.get(nextDir)).isConnected())
continue;
BlockPos offset = currPos.offset(nextDir);
BlockState offState = this.world.getBlockState(offset);
if (!(offState.getBlock() instanceof PipeBlock))
continue;
2020-04-14 15:02:21 +02:00
if (edge.pipes.contains(offset))
continue;
edge.pipes.add(offset);
2020-04-14 14:10:58 +02:00
currPos = offset;
currState = offState;
found = true;
2020-04-14 04:21:28 +02:00
break;
}
2020-04-14 14:10:58 +02:00
if (!found)
break;
2020-04-14 04:21:28 +02:00
}
2020-04-14 14:10:58 +02:00
return null;
2020-04-14 04:21:28 +02:00
}
public static PipeNetwork get(World world) {
return world.getCapability(Registry.pipeNetworkCapability).orElse(null);
}
}