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

51 lines
1.3 KiB
Java
Raw Normal View History

2020-04-14 14:10:58 +02:00
package de.ellpeck.prettypipes.network;
2021-12-02 12:31:04 +01:00
import net.minecraft.core.BlockPos;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.ListTag;
import net.minecraft.nbt.NbtUtils;
import net.minecraft.nbt.Tag;
2024-02-03 15:17:58 +01:00
import net.neoforged.neoforge.common.util.INBTSerializable;
2020-04-14 14:10:58 +02:00
import org.jgrapht.graph.DefaultWeightedEdge;
import java.util.ArrayList;
import java.util.List;
2021-12-02 12:31:04 +01:00
public class NetworkEdge extends DefaultWeightedEdge implements INBTSerializable<CompoundTag> {
2020-04-14 14:10:58 +02:00
2020-04-14 15:02:21 +02:00
public final List<BlockPos> pipes = new ArrayList<>();
2020-04-14 18:51:43 +02:00
public NetworkEdge() {
2020-04-14 15:02:21 +02:00
}
2021-12-02 12:31:04 +01:00
public NetworkEdge(CompoundTag nbt) {
2020-04-14 17:14:24 +02:00
this.deserializeNBT(nbt);
}
2020-04-20 03:37:23 +02:00
public BlockPos getStartPipe() {
return this.pipes.get(0);
}
public BlockPos getEndPipe() {
return this.pipes.get(this.pipes.size() - 1);
}
2020-04-14 15:02:21 +02:00
@Override
2021-12-02 12:31:04 +01:00
public CompoundTag serializeNBT() {
2021-12-02 17:46:56 +01:00
var nbt = new CompoundTag();
var list = new ListTag();
for (var pos : this.pipes)
2021-12-02 12:31:04 +01:00
list.add(NbtUtils.writeBlockPos(pos));
2020-04-14 15:02:21 +02:00
nbt.put("pipes", list);
return nbt;
}
@Override
2021-12-02 12:31:04 +01:00
public void deserializeNBT(CompoundTag nbt) {
2020-04-14 15:02:21 +02:00
this.pipes.clear();
2021-12-02 17:46:56 +01:00
var list = nbt.getList("pipes", Tag.TAG_COMPOUND);
for (var i = 0; i < list.size(); i++)
2021-12-02 12:31:04 +01:00
this.pipes.add(NbtUtils.readBlockPos(list.getCompound(i)));
2020-04-14 15:02:21 +02:00
}
2020-04-14 14:10:58 +02:00
}