Skip to content

Commit 729aa49

Browse files
committed
Fix several tunnel binding and data issues
1 parent e7561a7 commit 729aa49

17 files changed

+384
-355
lines changed

src/main/java/dev/compactmods/machines/compat/theoneprobe/providers/TunnelProvider.java

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@ public void addProbeInfo(ProbeMode probeMode, IProbeInfo info, Player playerEnti
4242
IProbeInfo v = info.vertical(info.defaultLayoutStyle().spacing(-1));
4343

4444
if (level.getBlockEntity(hitData.getPos()) instanceof TunnelWallEntity tile) {
45-
final var connectedTo = tile.getConnectedPosition();
45+
46+
47+
4648

4749
if (probeMode == ProbeMode.EXTENDED) {
4850
TunnelDefinition definition = tile.getTunnelType();
@@ -65,29 +67,32 @@ public void addProbeInfo(ProbeMode probeMode, IProbeInfo info, Player playerEnti
6567
.item(new ItemStack(Items.COMPASS))
6668
.text(new TranslatableComponent(sideTranslated));
6769

68-
ServerLevel connectedWorld = (ServerLevel) level;
69-
BlockPos outPosBlock = connectedTo.getBlockPosition();
70-
71-
try {
72-
final var state = connectedTo.state(level.getServer());
73-
74-
// If connected block isn't air, show a connected block line
75-
if (!state.isAir()) {
76-
String blockName = IProbeInfo.STARTLOC + state.getBlock().getDescriptionId() + IProbeInfo.ENDLOC;
77-
HitResult trace = new BlockHitResult(
78-
hitData.getHitVec(), hitData.getSideHit(),
79-
outPosBlock, false);
80-
81-
ItemStack pick = state
82-
.getBlock()
83-
.getCloneItemStack(state, trace, connectedWorld, outPosBlock, playerEntity);
84-
85-
v.horizontal(center)
86-
.item(pick)
87-
.text(new TranslatableComponent(CompactMachines.MOD_ID.concat(".connected_block"), blockName));
70+
final var connectedTo = tile.getConnectedPosition();
71+
if(connectedTo != null) {
72+
ServerLevel connectedWorld = (ServerLevel) level;
73+
BlockPos outPosBlock = connectedTo.getBlockPosition();
74+
75+
try {
76+
final var state = connectedTo.state(level.getServer());
77+
78+
// If connected block isn't air, show a connected block line
79+
if (!state.isAir()) {
80+
String blockName = IProbeInfo.STARTLOC + state.getBlock().getDescriptionId() + IProbeInfo.ENDLOC;
81+
HitResult trace = new BlockHitResult(
82+
hitData.getHitVec(), hitData.getSideHit(),
83+
outPosBlock, false);
84+
85+
ItemStack pick = state
86+
.getBlock()
87+
.getCloneItemStack(state, trace, connectedWorld, outPosBlock, playerEntity);
88+
89+
v.horizontal(center)
90+
.item(pick)
91+
.text(new TranslatableComponent(CompactMachines.MOD_ID.concat(".connected_block"), blockName));
92+
}
93+
} catch (Exception ex) {
94+
// no-op: we don't want to spam the log here
8895
}
89-
} catch (Exception ex) {
90-
// no-op: we don't want to spam the log here
9196
}
9297
}
9398
}

src/main/java/dev/compactmods/machines/core/Tunnels.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,9 @@ public static boolean isRegistered(ResourceLocation id) {
4747
}
4848

4949
public static TunnelDefinition getDefinition(ResourceLocation id) {
50-
return isRegistered(id) ? TUNNEL_DEF_REGISTRY.get().getValue(id) : Tunnels.UNKNOWN.get();
50+
if (isRegistered(id)) return TUNNEL_DEF_REGISTRY.get().getValue(id);
51+
CompactMachines.LOGGER.warn("Unknown tunnel requested: {}", id);
52+
return Tunnels.UNKNOWN.get();
5153
}
5254

5355
// ================================================================================================================

src/main/java/dev/compactmods/machines/graph/CMGraphRegistration.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,17 @@ public class CMGraphRegistration {
2727
() -> new RegistryBuilder<IGraphEdgeType>().setName(EDGES_RL));
2828

2929

30-
public static final RegistryObject<CompactMachineNode> MACH_NODE = NODE_TYPES.register("machine", CompactMachineNode::new);
31-
public static final RegistryObject<DimensionGraphNode> DIM_NODE = NODE_TYPES.register("dimension", DimensionGraphNode::new);
32-
public static final RegistryObject<CompactMachineRoomNode> ROOM_NODE = NODE_TYPES.register("room", CompactMachineRoomNode::new);
33-
public static final RegistryObject<TunnelNode> TUNNEL_NODE = NODE_TYPES.register("tunnel", TunnelNode::new);
34-
public static final RegistryObject<TunnelTypeNode> TUNNEL_TYPE_NODE = NODE_TYPES.register("tunnel_type", TunnelTypeNode::new);
30+
public static final RegistryObject<IGraphNodeType> MACH_NODE = NODE_TYPES.register("machine", () -> GraphNodeType.MACHINE);
31+
public static final RegistryObject<IGraphNodeType> DIM_NODE = NODE_TYPES.register("dimension", () -> GraphNodeType.DIMENSION);
32+
public static final RegistryObject<IGraphNodeType> ROOM_NODE = NODE_TYPES.register("room", () -> GraphNodeType.ROOM);
33+
public static final RegistryObject<IGraphNodeType> TUNNEL_NODE = NODE_TYPES.register("tunnel", () -> GraphNodeType.TUNNEL);
34+
public static final RegistryObject<IGraphNodeType> TUNNEL_TYPE_NODE = NODE_TYPES.register("tunnel_type", () -> GraphNodeType.TUNNEL_TYPE);
3535

3636
public static final RegistryObject<IGraphEdgeType> MACHINE_LINK = EDGE_TYPES.register("machine_link", () -> GraphEdgeType.MACHINE_LINK);
37+
38+
// Tunnel edges
3739
public static final RegistryObject<IGraphEdgeType> TUNNEL_TYPE = EDGE_TYPES.register("tunnel_type", () -> GraphEdgeType.TUNNEL_TYPE);
40+
public static final RegistryObject<IGraphEdgeType> TUNNEL_MACHINE_LINK = EDGE_TYPES.register("tunnel_machine", () -> GraphEdgeType.TUNNEL_MACHINE);
3841

3942
public static void init(IEventBus bus) {
4043
NODE_TYPES.register(bus);

src/main/java/dev/compactmods/machines/graph/DimensionGraphNode.java

Lines changed: 2 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -8,42 +8,11 @@
88

99
import java.util.Objects;
1010

11-
public final class DimensionGraphNode extends GraphNodeBase implements IGraphNodeType {
11+
public record DimensionGraphNode(ResourceKey<Level> dimension) implements IGraphNode {
1212

13-
private static final Codec<DimensionGraphNode> CODEC = RecordCodecBuilder.create(i -> i.group(
13+
public static final Codec<DimensionGraphNode> CODEC = RecordCodecBuilder.create(i -> i.group(
1414
ResourceKey.codec(Registry.DIMENSION_REGISTRY).fieldOf("dim").forGetter(DimensionGraphNode::dimension)
1515
).apply(i, DimensionGraphNode::new));
16-
private final ResourceKey<Level> dimension;
17-
18-
public DimensionGraphNode() {
19-
this.dimension = null;
20-
}
21-
22-
public DimensionGraphNode(ResourceKey<Level> dimension) {
23-
this.dimension = dimension;
24-
}
25-
26-
@Override
27-
public Codec<DimensionGraphNode> codec() {
28-
return CODEC;
29-
}
30-
31-
public ResourceKey<Level> dimension() {
32-
return dimension;
33-
}
34-
35-
@Override
36-
public boolean equals(Object obj) {
37-
if (obj == this) return true;
38-
if (obj == null || obj.getClass() != this.getClass()) return false;
39-
var that = (DimensionGraphNode) obj;
40-
return Objects.equals(this.dimension, that.dimension);
41-
}
42-
43-
@Override
44-
public int hashCode() {
45-
return Objects.hash(dimension);
46-
}
4716

4817
@Override
4918
public String toString() {

src/main/java/dev/compactmods/machines/graph/GraphEdgeType.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@
22

33
import com.mojang.serialization.Codec;
44
import dev.compactmods.machines.machine.graph.MachineRoomEdge;
5+
import dev.compactmods.machines.tunnel.graph.TunnelMachineEdge;
56
import dev.compactmods.machines.tunnel.graph.TunnelTypeEdge;
67
import net.minecraft.resources.ResourceLocation;
78
import org.jetbrains.annotations.Nullable;
89

910
public enum GraphEdgeType implements IGraphEdgeType {
1011
TUNNEL_TYPE(TunnelTypeEdge.CODEC),
11-
MACHINE_LINK(MachineRoomEdge.CODEC);
12+
MACHINE_LINK(MachineRoomEdge.CODEC),
13+
TUNNEL_MACHINE(TunnelMachineEdge.CODEC);
1214

1315
private final Codec<IGraphEdge> codec;
1416
private ResourceLocation regName;
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package dev.compactmods.machines.graph;
2+
3+
import com.mojang.serialization.Codec;
4+
import dev.compactmods.machines.machine.graph.CompactMachineNode;
5+
import dev.compactmods.machines.room.graph.CompactMachineRoomNode;
6+
import dev.compactmods.machines.tunnel.graph.TunnelNode;
7+
import dev.compactmods.machines.tunnel.graph.TunnelTypeNode;
8+
import net.minecraft.resources.ResourceLocation;
9+
import org.jetbrains.annotations.Nullable;
10+
11+
public enum GraphNodeType implements IGraphNodeType {
12+
MACHINE(CompactMachineNode.CODEC),
13+
TUNNEL(TunnelNode.CODEC),
14+
ROOM(CompactMachineRoomNode.CODEC),
15+
TUNNEL_TYPE(TunnelTypeNode.CODEC),
16+
DIMENSION(DimensionGraphNode.CODEC);
17+
18+
private final Codec<IGraphNode> codec;
19+
private ResourceLocation regName;
20+
21+
@SuppressWarnings("unchecked")
22+
<T extends IGraphNode> GraphNodeType(Codec<T> codec) {
23+
this.codec = (Codec<IGraphNode>) codec;
24+
}
25+
26+
@Override
27+
@SuppressWarnings("unchecked")
28+
public Codec<IGraphNode> codec() {
29+
return codec;
30+
}
31+
32+
@Override
33+
public IGraphNodeType setRegistryName(ResourceLocation name) {
34+
this.regName = name;
35+
return this;
36+
}
37+
38+
@Nullable
39+
@Override
40+
public ResourceLocation getRegistryName() {
41+
return regName;
42+
}
43+
44+
@Override
45+
public Class<IGraphNodeType> getRegistryType() {
46+
return CMGraphRegistration.NODE_TYPE_REG.get().getRegistrySuperType();
47+
}
48+
}

src/main/java/dev/compactmods/machines/location/LevelBlockPosition.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,17 +43,18 @@ private LevelBlockPosition() {
4343
public LevelBlockPosition(IDimensionalBlockPosition base) {
4444
this.dimension = base.dimensionKey();
4545
this.position = base.getExactPosition();
46+
this.rotation = Vec3.ZERO;
4647
}
4748

4849
public LevelBlockPosition(ResourceKey<Level> world, BlockPos positionBlock) {
4950
this(world, Vec3.ZERO, Vec3.ZERO);
5051
this.position = new Vec3(positionBlock.getX(), positionBlock.getY(), positionBlock.getZ());
52+
this.rotation = Vec3.ZERO;
5153
}
5254

5355
public LevelBlockPosition(ResourceKey<Level> world, Vec3 positionBlock) {
5456
this(world, positionBlock, Vec3.ZERO);
5557
this.dimension = world;
56-
5758
this.rotation = Vec3.ZERO;
5859
}
5960

@@ -67,8 +68,6 @@ public static LevelBlockPosition fromEntity(LivingEntity entity) {
6768
return new LevelBlockPosition(entity.level.dimension(), entity.position());
6869
}
6970

70-
71-
7271
public ServerLevel level(@Nonnull MinecraftServer server) {
7372
return server.getLevel(this.dimension);
7473
}
@@ -97,6 +96,9 @@ public static LevelBlockPosition fromNBT(CompoundTag nbt) {
9796

9897
@Override
9998
public CompoundTag serializeNBT() {
99+
if(this.rotation == null)
100+
this.rotation = Vec3.ZERO;
101+
100102
DataResult<Tag> nbt = CODEC.encodeStart(NbtOps.INSTANCE, this);
101103
return (CompoundTag) nbt.result().orElse(null);
102104
}

src/main/java/dev/compactmods/machines/machine/CompactMachineBlockEntity.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import dev.compactmods.machines.core.Registration;
88
import dev.compactmods.machines.machine.graph.DimensionMachineGraph;
99
import dev.compactmods.machines.machine.graph.CompactMachineNode;
10-
import dev.compactmods.machines.machine.graph.LegacyMachineConnections;
10+
import dev.compactmods.machines.machine.graph.legacy.LegacyMachineConnections;
1111
import dev.compactmods.machines.room.graph.CompactMachineRoomNode;
1212
import dev.compactmods.machines.tunnel.TunnelWallEntity;
1313
import dev.compactmods.machines.tunnel.graph.TunnelConnectionGraph;

src/main/java/dev/compactmods/machines/machine/graph/CompactMachineNode.java

Lines changed: 2 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,10 @@
44
import com.mojang.serialization.codecs.RecordCodecBuilder;
55
import dev.compactmods.machines.CompactMachines;
66
import dev.compactmods.machines.graph.CMGraphRegistration;
7-
import dev.compactmods.machines.graph.GraphNodeBase;
87
import dev.compactmods.machines.graph.IGraphNode;
98
import dev.compactmods.machines.graph.IGraphNodeType;
109
import dev.compactmods.machines.location.LevelBlockPosition;
1110
import net.minecraft.core.BlockPos;
12-
import net.minecraft.core.Registry;
1311
import net.minecraft.resources.ResourceKey;
1412
import net.minecraft.resources.ResourceLocation;
1513
import net.minecraft.world.level.Level;
@@ -19,7 +17,8 @@
1917
/**
2018
* Represents a machine's external point. This can be either inside a machine or in a dimension somewhere.
2119
*/
22-
public final class CompactMachineNode extends GraphNodeBase implements IGraphNodeType {
20+
public record CompactMachineNode(ResourceKey<Level> dimension, BlockPos position)
21+
implements IGraphNode {
2322

2423
public static final ResourceLocation TYPE = new ResourceLocation(CompactMachines.MOD_ID, "machine");
2524

@@ -28,55 +27,15 @@ public final class CompactMachineNode extends GraphNodeBase implements IGraphNod
2827
BlockPos.CODEC.fieldOf("position").forGetter(CompactMachineNode::position),
2928
ResourceLocation.CODEC.fieldOf("type").forGetter(x -> TYPE)
3029
).apply(i, (dim, pos, type) -> new CompactMachineNode(dim, pos)));
31-
private final ResourceKey<Level> dimension;
32-
private final BlockPos position;
33-
34-
public CompactMachineNode() {
35-
this.dimension = ResourceKey.create(Registry.DIMENSION_REGISTRY, new ResourceLocation(CompactMachines.MOD_ID, "empty"));
36-
this.position = BlockPos.ZERO;
37-
}
38-
39-
/**
40-
*/
41-
public CompactMachineNode(ResourceKey<Level> dimension, BlockPos position) {
42-
this.dimension = dimension;
43-
this.position = position;
44-
}
4530

4631
public String toString() {
4732
return "Compact Machine {%s}".formatted(position);
4833
}
4934

50-
@Override
51-
public boolean equals(Object o) {
52-
if (this == o) return true;
53-
if (o == null || getClass() != o.getClass()) return false;
54-
CompactMachineNode that = (CompactMachineNode) o;
55-
return position.equals(that.position);
56-
}
57-
58-
@Override
59-
public int hashCode() {
60-
return Objects.hash(position);
61-
}
62-
63-
@Override
64-
public Codec<CompactMachineNode> codec() {
65-
return CODEC;
66-
}
67-
6835
public LevelBlockPosition dimpos() {
6936
return new LevelBlockPosition(dimension, position);
7037
}
7138

72-
public ResourceKey<Level> dimension() {
73-
return dimension;
74-
}
75-
76-
public BlockPos position() {
77-
return position;
78-
}
79-
8039
@Override
8140
public IGraphNodeType getType() {
8241
return CMGraphRegistration.MACH_NODE.get();

src/main/java/dev/compactmods/machines/machine/graph/DimensionMachineGraph.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import dev.compactmods.machines.CompactMachines;
99
import dev.compactmods.machines.api.codec.CodecExtensions;
1010
import dev.compactmods.machines.graph.IGraphEdge;
11+
import dev.compactmods.machines.graph.IGraphNode;
1112
import dev.compactmods.machines.graph.IGraphNodeType;
1213
import dev.compactmods.machines.room.graph.CompactMachineRoomNode;
1314
import net.minecraft.core.BlockPos;
@@ -31,7 +32,7 @@
3132
public class DimensionMachineGraph extends SavedData {
3233

3334
private final ResourceKey<Level> level;
34-
private final MutableValueGraph<IGraphNodeType, IGraphEdge> graph;
35+
private final MutableValueGraph<IGraphNode, IGraphEdge> graph;
3536
private final Map<BlockPos, CompactMachineNode> machines;
3637
private final Map<ChunkPos, CompactMachineRoomNode> rooms;
3738

0 commit comments

Comments
 (0)