Skip to content

Commit 7835359

Browse files
committed
Begin on graph universe
1 parent df35a73 commit 7835359

5 files changed

Lines changed: 156 additions & 0 deletions

File tree

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,50 @@
11
package com.kneelawk.graphlib.v3.api;
22

3+
import java.util.NoSuchElementException;
4+
5+
import org.jetbrains.annotations.Nullable;
6+
7+
import net.minecraft.resources.ResourceLocation;
8+
9+
import com.kneelawk.graphlib.v3.api.graph.GraphUniverse;
10+
import com.kneelawk.graphlib.v3.impl.GraphLibImpl;
11+
312
/**
413
* GraphLib Public API.
514
*/
615
public final class GraphLib {
716
private GraphLib() {}
17+
18+
/**
19+
* Gets whether the given universe has been registered.
20+
*
21+
* @param universeId the id of the universe to check.
22+
* @return {@code true} if the universe has been registered.
23+
*/
24+
public static boolean universeExists(ResourceLocation universeId) {
25+
return GraphLibImpl.UNIVERSES.containsKey(universeId);
26+
}
27+
28+
/**
29+
* Gets the graph universe registered with the given id if any.
30+
*
31+
* @param universeId the id of the graph universe to lookup.
32+
* @return the graph universe with the given id or {@code null} if none could be found.
33+
*/
34+
public static @Nullable GraphUniverse getUniverse(ResourceLocation universeId) {
35+
return GraphLibImpl.UNIVERSES.get(universeId);
36+
}
37+
38+
/**
39+
* Gets the graph universe given with the given id or throws.
40+
*
41+
* @param universeId the id of the graph universe to lookup.
42+
* @return the graph universe with the given id.
43+
* @throws NoSuchElementException if no graph universe could be found with the given id.
44+
*/
45+
public static GraphUniverse getUniverseOrThrow(ResourceLocation universeId) {
46+
GraphUniverse universe = getUniverse(universeId);
47+
if (universe == null) throw new NoSuchElementException("No universe exists with the name " + universeId);
48+
return universe;
49+
}
850
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.kneelawk.graphlib.v3.api.graph;
2+
3+
import org.jetbrains.annotations.ApiStatus;
4+
import org.jetbrains.annotations.NotNull;
5+
6+
import com.mojang.serialization.Codec;
7+
import com.mojang.serialization.DataResult;
8+
9+
import net.minecraft.resources.ResourceLocation;
10+
11+
import com.kneelawk.codextra.api.attach.AttachmentKey;
12+
import com.kneelawk.graphlib.v3.api.GraphLib;
13+
14+
/**
15+
* Manages all graphs and graph accessories.
16+
*/
17+
@ApiStatus.NonExtendable
18+
public interface GraphUniverse {
19+
20+
/**
21+
* Graph-Universe attachment key for use in codecs.
22+
*/
23+
AttachmentKey<GraphUniverse> ATTACHMENT_KEY = AttachmentKey.ofStaticFieldName();
24+
25+
/**
26+
* Codec for referencing a graph universe.
27+
*/
28+
Codec<GraphUniverse> REF_CODEC = ResourceLocation.CODEC.comapFlatMap(id -> {
29+
if (!GraphLib.universeExists(id))
30+
return DataResult.error(() -> "The graph universe '" + id + "' does not exist");
31+
return DataResult.success(GraphLib.getUniverseOrThrow(id));
32+
}, GraphUniverse::getId);
33+
34+
/**
35+
* Gets the unique id of this universe.
36+
*
37+
* @return this universe's unique id.
38+
*/
39+
@NotNull
40+
ResourceLocation getId();
41+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/**
2+
* GraphLib interface.
3+
*/
4+
@ParametersAreNonnullByDefault
5+
@MethodsReturnNonnullByDefault
6+
@FieldsAreNonnullByDefault
7+
package com.kneelawk.graphlib.v3.api.graph;
8+
9+
import javax.annotation.ParametersAreNonnullByDefault;
10+
11+
import net.minecraft.FieldsAreNonnullByDefault;
12+
import net.minecraft.MethodsReturnNonnullByDefault;
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.kneelawk.graphlib.v3.api.util;
2+
3+
import com.mojang.serialization.Codec;
4+
import com.mojang.serialization.MapCodec;
5+
import com.mojang.serialization.codecs.RecordCodecBuilder;
6+
7+
import com.kneelawk.graphlib.v3.api.graph.GraphUniverse;
8+
9+
/**
10+
* Describes an object in a {@link GraphUniverse}.
11+
*
12+
* @param universe the universe the object is within.
13+
* @param obj the object.
14+
* @param <T> the type of the object.
15+
*/
16+
public record InUniverse<T>(GraphUniverse universe, T obj) {
17+
/**
18+
* Creates an in-universe map codec for the given type.
19+
* <p>
20+
* <b>This provides the {@link GraphUniverse#ATTACHMENT_KEY} attachment.</b>
21+
* <p>
22+
* This uses the {@code universe} map key for the universe, but anything else can be used by the obj-codec.
23+
*
24+
* @param objCodec the codec for the object to be associated with a universe.
25+
* @param <T> the wrapped object type.
26+
* @return the created map codec.
27+
*/
28+
public static <T> MapCodec<InUniverse<T>> mapCodec(MapCodec<T> objCodec) {
29+
return GraphUniverse.ATTACHMENT_KEY.keyAttachingCodec(GraphUniverse.REF_CODEC.fieldOf("universe"),
30+
RecordCodecBuilder.mapCodec(instance -> instance.group(
31+
GraphUniverse.ATTACHMENT_KEY.retrieve(),
32+
objCodec.forGetter(InUniverse::obj)
33+
).apply(instance, InUniverse::new)), InUniverse::universe);
34+
}
35+
36+
/**
37+
* Codecs-only version of {@link #mapCodec(MapCodec)}.
38+
* <p>
39+
* <b>This provides the {@link GraphUniverse#ATTACHMENT_KEY} attachment.</b>
40+
*
41+
* @param objCodec the codec for the object to be associated with a universe.
42+
* @param <T> the wrapped object type.
43+
* @return the created codec.
44+
*/
45+
public static <T> Codec<InUniverse<T>> codec(Codec<T> objCodec) {
46+
return mapCodec(objCodec.fieldOf("value")).codec();
47+
}
48+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.kneelawk.graphlib.v3.impl;
2+
3+
import java.util.Map;
4+
5+
import it.unimi.dsi.fastutil.objects.Object2ObjectLinkedOpenHashMap;
6+
7+
import net.minecraft.resources.ResourceLocation;
8+
9+
import com.kneelawk.graphlib.v3.api.graph.GraphUniverse;
10+
11+
public class GraphLibImpl {
12+
public static final Map<ResourceLocation, GraphUniverse> UNIVERSES = new Object2ObjectLinkedOpenHashMap<>();
13+
}

0 commit comments

Comments
 (0)