Skip to content

Commit 8b7985a

Browse files
committed
Add more to structure
1 parent 276dc80 commit 8b7985a

18 files changed

Lines changed: 333 additions & 50 deletions
Lines changed: 48 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,98 @@
11
package com.kneelawk.graphlib.v3.api.graph;
22

3-
import org.jetbrains.annotations.ApiStatus;
43
import org.jetbrains.annotations.NotNull;
54
import org.jetbrains.annotations.Nullable;
65

76
import com.mojang.serialization.Codec;
87
import com.mojang.serialization.DataResult;
98

109
import net.minecraft.resources.ResourceLocation;
10+
import net.minecraft.world.level.Level;
1111

1212
import com.kneelawk.codextra.api.attach.AttachmentKey;
1313
import com.kneelawk.graphlib.v3.api.GraphLib;
14+
import com.kneelawk.graphlib.v3.api.graph.backend.DimensionManager;
15+
import com.kneelawk.graphlib.v3.api.graph.backend.NodeSpace;
1416
import com.kneelawk.graphlib.v3.api.graph.user.GraphNodeType;
1517
import com.kneelawk.graphlib.v3.api.graph.user.LinkKeyType;
18+
import com.kneelawk.graphlib.v3.api.pos.DimensionRef;
19+
import com.kneelawk.graphlib.v3.api.pos.NodePos;
1620

1721
/**
1822
* Manages all graphs and graph accessories.
1923
*/
20-
public final class GraphUniverse {
21-
24+
public interface GraphUniverse {
2225
/**
2326
* Graph-Universe attachment key for use in codecs.
2427
*/
25-
public static final AttachmentKey<GraphUniverse> ATTACHMENT_KEY = AttachmentKey.ofStaticFieldName();
26-
28+
AttachmentKey<GraphUniverse> ATTACHMENT_KEY = AttachmentKey.ofStaticFieldName();
2729
/**
2830
* Codec for referencing a graph universe.
2931
*/
30-
public static final Codec<GraphUniverse> REF_CODEC = ResourceLocation.CODEC.comapFlatMap(id -> {
32+
Codec<GraphUniverse> REF_CODEC = ResourceLocation.CODEC.comapFlatMap(id -> {
3133
if (!GraphLib.universeExists(id))
3234
return DataResult.error(() -> "The graph universe '" + id + "' does not exist");
3335
return DataResult.success(GraphLib.getUniverseOrThrow(id));
3436
}, GraphUniverse::getId);
3537

36-
private final ResourceLocation id;
37-
38-
private GraphUniverse(ResourceLocation id) {
39-
this.id = id;
40-
}
41-
4238
/**
4339
* Gets the unique id of this universe.
4440
*
4541
* @return this universe's unique id.
4642
*/
47-
public ResourceLocation getId() {
48-
return id;
49-
}
43+
ResourceLocation getId();
44+
45+
/**
46+
* {@return the graph world associated with the currently running minecraft server}
47+
* <p>
48+
* If no minecraft server is currently running (e.g. no game is running, this is a client connected to a remote
49+
* server), this will return {@code null}.
50+
*/
51+
@Nullable GraphWorld getGraphWorld();
5052

5153
/**
5254
* Gets the graph node type for the given type id.
5355
*
5456
* @param typeId the type id of the graph node type.
5557
* @return the graph node type for the given type id.
5658
*/
57-
@Nullable
58-
public GraphNodeType getNodeType(@NotNull ResourceLocation typeId) {
59-
throw new AssertionError("Stub");
60-
}
59+
@Nullable GraphNodeType getNodeType(@NotNull ResourceLocation typeId);
6160

6261
/**
6362
* Gets the link key type for the given type id.
6463
*
6564
* @param typeId the type id of the link key type.
6665
* @return the link key type for the given type id.
6766
*/
68-
@Nullable
69-
public LinkKeyType getLinkKeyType(@NotNull ResourceLocation typeId) {
70-
throw new AssertionError("Stub");
67+
@Nullable LinkKeyType getLinkKeyType(@NotNull ResourceLocation typeId);
68+
69+
/**
70+
* Gets the node space for the given type of node pos if it has been registered.
71+
*
72+
* @param type the type of node pos that the node space is associated with.
73+
* @return the node space associated with the given node pos type.
74+
*/
75+
@Nullable NodeSpace getNodeSpace(NodePos.Type<?> type);
76+
77+
/**
78+
* Gets the dimension manager for the given type of dimension ref if it has been registered.
79+
*
80+
* @param type the type of dimension ref that the dimension manager is for.
81+
* @param <R> the type of dimension ref to get the dimension manager of.
82+
* @return the dimension manager for the given type of dimension ref.
83+
*/
84+
@Nullable <R extends DimensionRef> DimensionManager<R> getDimensionManager(DimensionRef.Type<R> type);
85+
86+
/**
87+
* Gets a {@link Level} for the given dimension ref, if one is available.
88+
*
89+
* @param dimensionRef the dimension reference that the level is associated with.
90+
* @return the level associated with the given dimension reference.
91+
*/
92+
@SuppressWarnings("unchecked")
93+
default @Nullable Level getLevel(DimensionRef dimensionRef) {
94+
DimensionManager<?> manager = getDimensionManager(dimensionRef.getType());
95+
if (manager == null) return null;
96+
return ((DimensionManager<DimensionRef>) manager).getLevel(dimensionRef);
7197
}
7298
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.kneelawk.graphlib.v3.api.graph;
2+
3+
import org.jetbrains.annotations.Nullable;
4+
5+
import net.minecraft.world.level.Level;
6+
7+
import com.kneelawk.graphlib.v3.api.pos.DimensionRef;
8+
9+
/**
10+
* An immutable view of all graphs in a save (across all levels).
11+
*/
12+
public interface GraphView {
13+
/**
14+
* {@return whether this graph view is on the logical client}
15+
*/
16+
default boolean isClient() {
17+
return true;
18+
}
19+
20+
/**
21+
* {@return the graph universe associated with this graph view}
22+
*/
23+
GraphUniverse getUniverse();
24+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.kneelawk.graphlib.v3.api.graph;
2+
3+
/**
4+
* Mutable access to all graphs in a save (across all levels).
5+
*/
6+
public interface GraphWorld extends GraphView {
7+
@Override
8+
default boolean isClient() {
9+
return false;
10+
}
11+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.kneelawk.graphlib.v3.api.graph;
2+
3+
import com.kneelawk.graphlib.v3.api.graph.user.GraphNode;
4+
5+
/**
6+
* Provides access to a single connected set of {@link GraphNode}s.
7+
*/
8+
public interface ImmutableGraph extends Iterable<GraphNode> {
9+
}

modules/core-xplat/src/main/java/com/kneelawk/graphlib/v3/api/graph/NodeGraph.java

Lines changed: 0 additions & 12 deletions
This file was deleted.
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package com.kneelawk.graphlib.v3.api.graph;
2+
3+
import org.jetbrains.annotations.Nullable;
4+
5+
import net.minecraft.core.BlockPos;
6+
import net.minecraft.world.level.Level;
7+
import net.minecraft.world.level.block.entity.BlockEntity;
8+
import net.minecraft.world.level.block.state.BlockState;
9+
10+
import com.kneelawk.graphlib.v3.api.graph.user.GraphNode;
11+
import com.kneelawk.graphlib.v3.api.pos.DimensionRef;
12+
import com.kneelawk.graphlib.v3.api.pos.NodePos;
13+
14+
/**
15+
* Holds a graph node and all associated information.
16+
*/
17+
public interface NodeHolder {
18+
/**
19+
* {@return the node pos}
20+
*/
21+
NodePos pos();
22+
23+
/**
24+
* {@return the graph view that this node holder is from}
25+
*/
26+
GraphView graphView();
27+
28+
/**
29+
* {@return the graph node within the node pos}
30+
*/
31+
default GraphNode node() {
32+
return pos().node();
33+
}
34+
35+
/**
36+
* {@return the dimension reference associated with the node, if any}
37+
*/
38+
@Nullable
39+
default DimensionRef dimensionRef() {
40+
return pos().dimension();
41+
}
42+
43+
/**
44+
* {@return the level associated with this node, if any}
45+
*/
46+
@Nullable
47+
default Level level() {
48+
DimensionRef ref = dimensionRef();
49+
if (ref == null) return null;
50+
return graphView().getUniverse().getLevel(ref);
51+
}
52+
53+
/**
54+
* {@return the block pos associated with this node, if any}
55+
*/
56+
@Nullable
57+
default BlockPos blockPos() {
58+
return pos().blockPos();
59+
}
60+
61+
/**
62+
* {@return the block state at the position of this node, if this node even has a block position}
63+
*/
64+
@Nullable
65+
default BlockState blockState() {
66+
Level level = level();
67+
if (level == null) return null;
68+
BlockPos pos = blockPos();
69+
if (pos == null) return null;
70+
return level.getBlockState(pos);
71+
}
72+
73+
/**
74+
* {@return the block entity at the position of this node, if any}
75+
*/
76+
@Nullable
77+
default BlockEntity blockEntity() {
78+
Level level = level();
79+
if (level == null) return null;
80+
BlockPos pos = blockPos();
81+
if (pos == null) return null;
82+
return level.getBlockEntity(pos);
83+
}
84+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.kneelawk.graphlib.v3.api.graph.backend;
2+
3+
import org.jetbrains.annotations.Nullable;
4+
5+
import net.minecraft.world.level.Level;
6+
7+
import com.kneelawk.graphlib.v3.api.pos.DimensionRef;
8+
9+
/**
10+
* Manages all nodes for a given type of {@link com.kneelawk.graphlib.v3.api.pos.DimensionRef}.
11+
*
12+
* @param <R> the type of dimension ref this manager manages.
13+
*/
14+
public interface DimensionManager<R extends DimensionRef> {
15+
/**
16+
* Gets the node dimension for the given dimension reference, if the dimension reference actually references a
17+
* valid node dimension that exists.
18+
*
19+
* @param ref the dimension ref that references the given node dimension.
20+
* @return the node dimension referenced by the given dimension ref.
21+
*/
22+
@Nullable NodeDimension getDimension(R ref);
23+
24+
/**
25+
* Gets the level associated with the given dimension reference if the dimension reference actually references a
26+
* concrete level.
27+
*
28+
* @param ref the dimension reference of the level to lookup.
29+
* @return the level associated with the given dimension reference.
30+
*/
31+
default @Nullable Level getLevel(R ref) {
32+
NodeDimension dimension = getDimension(ref);
33+
if (dimension == null) return null;
34+
return dimension.getLevel();
35+
}
36+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.kneelawk.graphlib.v3.api.graph.backend;
2+
3+
import org.jetbrains.annotations.Nullable;
4+
5+
import net.minecraft.world.level.Level;
6+
7+
/**
8+
* Handles nodes for a single node dimension.
9+
*/
10+
public interface NodeDimension {
11+
/**
12+
* {@return the level associated with this node dimension, if this dimension is actually associated with a concrete level}
13+
*/
14+
@Nullable Level getLevel();
15+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.kneelawk.graphlib.v3.api.graph.backend;
2+
3+
/**
4+
* Provides access to nodes based on where they are.
5+
*/
6+
public interface NodeSpace {
7+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/**
2+
* Backend-implemented GraphLib interfaces.
3+
*/
4+
@ParametersAreNonnullByDefault
5+
@MethodsReturnNonnullByDefault
6+
@FieldsAreNonnullByDefault
7+
package com.kneelawk.graphlib.v3.api.graph.backend;
8+
9+
import javax.annotation.ParametersAreNonnullByDefault;
10+
11+
import net.minecraft.FieldsAreNonnullByDefault;
12+
import net.minecraft.MethodsReturnNonnullByDefault;

0 commit comments

Comments
 (0)