mirror of
https://github.com/Ellpeck/PrettyPipes.git
synced 2024-11-22 11:53:29 +01:00
network serialization
This commit is contained in:
parent
87de8e353d
commit
0a674ba1b5
2 changed files with 91 additions and 39 deletions
|
@ -1,14 +1,62 @@
|
|||
package de.ellpeck.prettypipes.network;
|
||||
|
||||
import de.ellpeck.prettypipes.Utility;
|
||||
import de.ellpeck.prettypipes.blocks.pipe.PipeTileEntity;
|
||||
import net.minecraft.nbt.CompoundNBT;
|
||||
import net.minecraft.nbt.ListNBT;
|
||||
import net.minecraft.nbt.NBTUtil;
|
||||
import net.minecraft.tileentity.TileEntity;
|
||||
import net.minecraft.util.math.BlockPos;
|
||||
import net.minecraft.world.World;
|
||||
import net.minecraftforge.common.util.Constants;
|
||||
import net.minecraftforge.common.util.INBTSerializable;
|
||||
import org.jgrapht.graph.DefaultWeightedEdge;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class NetworkEdge extends DefaultWeightedEdge {
|
||||
public class NetworkEdge extends DefaultWeightedEdge implements INBTSerializable<CompoundNBT> {
|
||||
|
||||
public TileEntity startPipe;
|
||||
public List<TileEntity> pipes = new ArrayList<>();
|
||||
public TileEntity endPipe;
|
||||
public final World world;
|
||||
public BlockPos startPipe;
|
||||
public BlockPos endPipe;
|
||||
public final List<BlockPos> pipes = new ArrayList<>();
|
||||
private final Map<Integer, PipeTileEntity> tileCache = new HashMap<>();
|
||||
|
||||
public NetworkEdge(World world) {
|
||||
this.world = world;
|
||||
}
|
||||
|
||||
public PipeTileEntity getPipe(int index) {
|
||||
PipeTileEntity tile = this.tileCache.get(index);
|
||||
if (tile == null || tile.isRemoved()) {
|
||||
tile = Utility.getTileEntity(PipeTileEntity.class, this.world, this.pipes.get(index));
|
||||
this.tileCache.put(index, tile);
|
||||
}
|
||||
return tile;
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompoundNBT serializeNBT() {
|
||||
CompoundNBT nbt = new CompoundNBT();
|
||||
nbt.put("start", NBTUtil.writeBlockPos(this.startPipe));
|
||||
nbt.put("end", NBTUtil.writeBlockPos(this.endPipe));
|
||||
ListNBT list = new ListNBT();
|
||||
for (BlockPos pos : this.pipes)
|
||||
list.add(NBTUtil.writeBlockPos(pos));
|
||||
nbt.put("pipes", list);
|
||||
return nbt;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deserializeNBT(CompoundNBT nbt) {
|
||||
this.startPipe = NBTUtil.readBlockPos(nbt.getCompound("start"));
|
||||
this.endPipe = NBTUtil.readBlockPos(nbt.getCompound("end"));
|
||||
this.pipes.clear();
|
||||
ListNBT list = nbt.getList("pipes", Constants.NBT.TAG_COMPOUND);
|
||||
for (int i = 0; i < list.size(); i++)
|
||||
this.pipes.add(NBTUtil.readBlockPos(list.getCompound(i)));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -8,28 +8,23 @@ import de.ellpeck.prettypipes.blocks.pipe.PipeTileEntity;
|
|||
import net.minecraft.block.Block;
|
||||
import net.minecraft.block.BlockState;
|
||||
import net.minecraft.nbt.CompoundNBT;
|
||||
import net.minecraft.nbt.ListNBT;
|
||||
import net.minecraft.nbt.NBTUtil;
|
||||
import net.minecraft.util.Direction;
|
||||
import net.minecraft.util.Tuple;
|
||||
import net.minecraft.util.math.BlockPos;
|
||||
import net.minecraft.world.World;
|
||||
import net.minecraftforge.common.capabilities.Capability;
|
||||
import net.minecraftforge.common.capabilities.ICapabilitySerializable;
|
||||
import net.minecraftforge.common.util.Constants;
|
||||
import net.minecraftforge.common.util.LazyOptional;
|
||||
import org.apache.commons.lang3.tuple.Pair;
|
||||
import org.jgrapht.Graphs;
|
||||
import org.jgrapht.alg.shortestpath.DijkstraShortestPath;
|
||||
import org.jgrapht.graph.DefaultEdge;
|
||||
import org.jgrapht.graph.DefaultWeightedEdge;
|
||||
import org.jgrapht.graph.SimpleGraph;
|
||||
import org.jgrapht.graph.SimpleWeightedGraph;
|
||||
import org.jheaps.tree.FibonacciHeap;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.annotation.Nullable;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
public class PipeNetwork implements ICapabilitySerializable<CompoundNBT> {
|
||||
|
||||
|
@ -49,12 +44,30 @@ public class PipeNetwork implements ICapabilitySerializable<CompoundNBT> {
|
|||
|
||||
@Override
|
||||
public CompoundNBT serializeNBT() {
|
||||
return new CompoundNBT();
|
||||
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;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deserializeNBT(CompoundNBT nbt) {
|
||||
|
||||
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);
|
||||
for (int i = 0; i < edges.size(); i++) {
|
||||
NetworkEdge edge = new NetworkEdge(this.world);
|
||||
edge.deserializeNBT(edges.getCompound(i));
|
||||
this.addEdge(edge);
|
||||
}
|
||||
}
|
||||
|
||||
public void addNode(BlockPos pos, BlockState state) {
|
||||
|
@ -74,21 +87,19 @@ public class PipeNetwork implements ICapabilitySerializable<CompoundNBT> {
|
|||
// if we only have one neighbor, then there can't be any new connections
|
||||
if (neighbors.size() <= 1 && !this.graph.containsVertex(pos))
|
||||
return;
|
||||
for (NetworkEdge edge : neighbors) {
|
||||
BlockPos end = edge.endPipe.getPos();
|
||||
this.refreshNode(end, this.world.getBlockState(end));
|
||||
}
|
||||
System.out.println(this.graph.toString());
|
||||
for (NetworkEdge edge : neighbors)
|
||||
this.refreshNode(edge.endPipe, this.world.getBlockState(edge.endPipe));
|
||||
}
|
||||
|
||||
private void refreshNode(BlockPos pos, BlockState state) {
|
||||
Set<NetworkEdge> edges = this.graph.edgesOf(pos);
|
||||
this.graph.removeAllEdges(new ArrayList<>(edges));
|
||||
|
||||
for (NetworkEdge edge : this.createAllEdges(pos, state, false)) {
|
||||
this.graph.addEdge(edge.startPipe.getPos(), edge.endPipe.getPos(), edge);
|
||||
this.graph.setEdgeWeight(edge, edge.pipes.size());
|
||||
this.graph.removeAllEdges(new ArrayList<>(this.graph.edgesOf(pos)));
|
||||
for (NetworkEdge edge : this.createAllEdges(pos, state, false))
|
||||
this.addEdge(edge);
|
||||
}
|
||||
|
||||
private void addEdge(NetworkEdge edge) {
|
||||
this.graph.addEdge(edge.startPipe, edge.endPipe, edge);
|
||||
this.graph.setEdgeWeight(edge, edge.pipes.size());
|
||||
}
|
||||
|
||||
private List<NetworkEdge> createAllEdges(BlockPos pos, BlockState state, boolean allAround) {
|
||||
|
@ -108,17 +119,11 @@ public class PipeNetwork implements ICapabilitySerializable<CompoundNBT> {
|
|||
BlockState currState = this.world.getBlockState(currPos);
|
||||
if (!(currState.getBlock() instanceof PipeBlock))
|
||||
return null;
|
||||
NetworkEdge edge = new NetworkEdge();
|
||||
PipeTileEntity startPipe = Utility.getTileEntity(PipeTileEntity.class, this.world, pos);
|
||||
if (startPipe != null) {
|
||||
edge.startPipe = startPipe;
|
||||
edge.pipes.add(startPipe);
|
||||
}
|
||||
edge.pipes.add(Utility.getTileEntity(PipeTileEntity.class, this.world, currPos));
|
||||
NetworkEdge edge = new NetworkEdge(this.world);
|
||||
edge.startPipe = pos;
|
||||
edge.pipes.add(pos);
|
||||
edge.pipes.add(currPos);
|
||||
|
||||
Set<BlockPos> seen = new HashSet<>();
|
||||
seen.add(pos);
|
||||
seen.add(currPos);
|
||||
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
|
||||
|
@ -132,13 +137,12 @@ public class PipeNetwork implements ICapabilitySerializable<CompoundNBT> {
|
|||
if (!currState.get(PipeBlock.DIRECTIONS.get(nextDir)).isConnected())
|
||||
continue;
|
||||
BlockPos offset = currPos.offset(nextDir);
|
||||
if (seen.contains(offset))
|
||||
continue;
|
||||
seen.add(offset);
|
||||
BlockState offState = this.world.getBlockState(offset);
|
||||
if (!(offState.getBlock() instanceof PipeBlock))
|
||||
continue;
|
||||
edge.pipes.add(Utility.getTileEntity(PipeTileEntity.class, this.world, offset));
|
||||
if (edge.pipes.contains(offset))
|
||||
continue;
|
||||
edge.pipes.add(offset);
|
||||
currPos = offset;
|
||||
currState = offState;
|
||||
found = true;
|
||||
|
|
Loading…
Reference in a new issue