Skip to content

Commit 64d6777

Browse files
committed
feat: Move primitive stream codecs for results that may occur without Shogi installed to shogi-api
TwelveIterations/HardcoreRevival#194
1 parent 54b8170 commit 64d6777

2 files changed

Lines changed: 33 additions & 26 deletions

File tree

common/src/main/java/net/blay09/mods/shogi/common/network/ShogiDefaultStreamCodecs.java

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package net.blay09.mods.shogi.common.network;
22

3-
import com.google.gson.JsonElement;
43
import net.blay09.mods.shogi.common.effect.compose.AggregateEffect;
54
import net.blay09.mods.shogi.common.effect.compose.AndEffect;
65
import net.blay09.mods.shogi.common.effect.compose.AnyEffect;
@@ -19,15 +18,9 @@
1918
import net.blay09.mods.shogi.common.effect.variable.MacroAssignmentEffect;
2019
import net.blay09.mods.shogi.common.effect.variable.MissingVariableFailure;
2120
import net.blay09.mods.shogi.common.effect.variable.VariableEffect;
22-
import net.blay09.mods.shogi.effect.ConstantEffect;
23-
import net.blay09.mods.shogi.effect.EmptyEffect;
2421
import net.blay09.mods.shogi.effect.ShogiEffect;
25-
import net.blay09.mods.shogi.effect.ShogiEmpty;
26-
import net.blay09.mods.shogi.effect.failure.ShogiDeferred;
2722
import net.blay09.mods.shogi.network.ShogiStreamCodecs;
2823
import net.minecraft.network.RegistryFriendlyByteBuf;
29-
import net.minecraft.network.chat.Component;
30-
import net.minecraft.network.chat.ComponentSerialization;
3124
import net.minecraft.network.codec.ByteBufCodecs;
3225
import net.minecraft.network.codec.StreamCodec;
3326

@@ -37,10 +30,6 @@
3730

3831
public final class ShogiDefaultStreamCodecs {
3932
private static boolean initialized;
40-
private static final StreamCodec<RegistryFriendlyByteBuf, Throwable> THROWABLE_CODEC = StreamCodec.unit(new Exception());
41-
private static final StreamCodec<RegistryFriendlyByteBuf, ShogiDeferred> DEFERRED_CODEC = StreamCodec.unit(ShogiDeferred.INSTANCE);
42-
private static final StreamCodec<RegistryFriendlyByteBuf, ShogiEmpty> EMPTY_CODEC = StreamCodec.unit(EmptyEffect.INSTANCE);
43-
private static final StreamCodec<RegistryFriendlyByteBuf, JsonElement> JSON_CODEC = ByteBufCodecs.lenientJson(Short.MAX_VALUE).cast();
4433
private static final StreamCodec<RegistryFriendlyByteBuf, ShogiEffect<?>> EFFECT_CODEC = ShogiStreamCodecs.dynamicObjectCodec()
4534
.map(value -> (ShogiEffect<?>) value, value -> value);
4635
private static final StreamCodec<RegistryFriendlyByteBuf, List<ShogiEffect<?>>> EFFECT_LIST_CODEC = EFFECT_CODEC.apply(ByteBufCodecs.list());
@@ -54,14 +43,6 @@ public static synchronized void registerDefaults() {
5443
}
5544
initialized = true;
5645

57-
ShogiStreamCodecs.register(id("int"), Integer.class, ByteBufCodecs.VAR_INT.cast());
58-
ShogiStreamCodecs.register(id("float"), Float.class, ByteBufCodecs.FLOAT.cast());
59-
ShogiStreamCodecs.register(id("bool"), Boolean.class, ByteBufCodecs.BOOL.cast());
60-
ShogiStreamCodecs.register(id("string"), String.class, ByteBufCodecs.STRING_UTF8.cast());
61-
ShogiStreamCodecs.register(id("json"), JsonElement.class, JSON_CODEC.cast());
62-
ShogiStreamCodecs.register(id("component"), Component.class, ComponentSerialization.STREAM_CODEC);
63-
ShogiStreamCodecs.register(id("throwable"), Throwable.class, THROWABLE_CODEC);
64-
ShogiStreamCodecs.register(id("list"), List.class, ShogiStreamCodecs.LIST_STREAM_CODEC);
6546
ShogiStreamCodecs.register(id("failure"), FailureInformation.class, FailureInformation.STREAM_CODEC);
6647
ShogiStreamCodecs.register(id("refusal"), RefusalInformation.class, RefusalInformation.STREAM_CODEC);
6748
ShogiStreamCodecs.register(id("missing_variable_failure"), MissingVariableFailure.class, MissingVariableFailure.STREAM_CODEC);
@@ -71,13 +52,6 @@ public static synchronized void registerDefaults() {
7152
ShogiStreamCodecs.register(id("hunger_cost"), HungerCostInformation.class, HungerCostInformation.STREAM_CODEC);
7253
ShogiStreamCodecs.register(id("item_cost"), ItemCostInformation.class, ItemCostInformation.STREAM_CODEC);
7354
ShogiStreamCodecs.register(id("cooldown"), CooldownInformation.class, CooldownInformation.STREAM_CODEC);
74-
ShogiStreamCodecs.register(id("deferred"), ShogiDeferred.class, DEFERRED_CODEC);
75-
ShogiStreamCodecs.register(id("empty"), ShogiEmpty.class, EMPTY_CODEC);
76-
ShogiStreamCodecs.register(id("constant_effect"), ConstantEffect.class, StreamCodec.composite(
77-
JSON_CODEC,
78-
ConstantEffect::value,
79-
ConstantEffect::new
80-
));
8155
ShogiStreamCodecs.register(id("aggregate_effect"), AggregateEffect.class, StreamCodec.composite(
8256
EFFECT_LIST_CODEC,
8357
AggregateEffect::effects,

shogi-api/src/main/java/net/blay09/mods/shogi/network/ShogiStreamCodecs.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
package net.blay09.mods.shogi.network;
22

3+
import com.google.gson.JsonElement;
34
import com.mojang.datafixers.util.Either;
5+
import net.blay09.mods.shogi.effect.ConstantEffect;
6+
import net.blay09.mods.shogi.effect.EmptyEffect;
7+
import net.blay09.mods.shogi.effect.ShogiEmpty;
8+
import net.blay09.mods.shogi.effect.failure.ShogiDeferred;
49
import net.minecraft.network.RegistryFriendlyByteBuf;
10+
import net.minecraft.network.chat.Component;
11+
import net.minecraft.network.chat.ComponentSerialization;
512
import net.minecraft.network.codec.ByteBufCodecs;
613
import net.minecraft.network.codec.StreamCodec;
714
import net.minecraft.resources.Identifier;
@@ -28,6 +35,10 @@ private record Entry(Identifier id, Class<?> type, StreamCodec<RegistryFriendlyB
2835

2936
private static final StreamCodec<RegistryFriendlyByteBuf, Object> UNKNOWN_CODEC = StreamCodec.unit(UNKNOWN_VALUE);
3037
private static final StreamCodec<RegistryFriendlyByteBuf, Identifier> IDENTIFIER_STREAM_CODEC = Identifier.STREAM_CODEC.cast();
38+
private static final StreamCodec<RegistryFriendlyByteBuf, Throwable> THROWABLE_CODEC = StreamCodec.unit(new Exception());
39+
private static final StreamCodec<RegistryFriendlyByteBuf, ShogiDeferred> DEFERRED_CODEC = StreamCodec.unit(ShogiDeferred.INSTANCE);
40+
private static final StreamCodec<RegistryFriendlyByteBuf, ShogiEmpty> EMPTY_CODEC = StreamCodec.unit(EmptyEffect.INSTANCE);
41+
private static final StreamCodec<RegistryFriendlyByteBuf, JsonElement> JSON_CODEC = ByteBufCodecs.lenientJson(Short.MAX_VALUE).cast();
3142

3243
/**
3344
* Dynamic object stream codec that dispatches by registered runtime type identifier.
@@ -42,9 +53,31 @@ private record Entry(Identifier id, Class<?> type, StreamCodec<RegistryFriendlyB
4253
*/
4354
public static final StreamCodec<RegistryFriendlyByteBuf, List<Object>> LIST_STREAM_CODEC = STREAM_CODEC.apply(ByteBufCodecs.list());
4455

56+
static {
57+
register(id("int"), Integer.class, ByteBufCodecs.VAR_INT.cast());
58+
register(id("float"), Float.class, ByteBufCodecs.FLOAT.cast());
59+
register(id("bool"), Boolean.class, ByteBufCodecs.BOOL.cast());
60+
register(id("string"), String.class, ByteBufCodecs.STRING_UTF8.cast());
61+
register(id("list"), List.class, LIST_STREAM_CODEC);
62+
register(id("json"), JsonElement.class, JSON_CODEC.cast());
63+
register(id("component"), Component.class, ComponentSerialization.STREAM_CODEC);
64+
register(id("throwable"), Throwable.class, THROWABLE_CODEC);
65+
register(id("deferred"), ShogiDeferred.class, DEFERRED_CODEC);
66+
register(id("empty"), ShogiEmpty.class, EMPTY_CODEC);
67+
register(id("constant_effect"), ConstantEffect.class, StreamCodec.composite(
68+
JSON_CODEC,
69+
ConstantEffect::value,
70+
ConstantEffect::new
71+
));
72+
}
73+
4574
private ShogiStreamCodecs() {
4675
}
4776

77+
private static Identifier id(String path) {
78+
return Identifier.fromNamespaceAndPath("shogi", path);
79+
}
80+
4881
/**
4982
* Registers a runtime type and stream codec for dynamic payload synchronization.
5083
*

0 commit comments

Comments
 (0)