Skip to content

Commit bf9713a

Browse files
committed
Network custom dimension physics to client
1 parent 62455f6 commit bf9713a

6 files changed

Lines changed: 119 additions & 14 deletions

File tree

common/src/main/java/dev/ryanhcode/sable/SableCommonEvents.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import dev.ryanhcode.sable.physics.chunk.VoxelNeighborhoodState;
88
import dev.ryanhcode.sable.physics.config.FloatingBlockMaterialDataHandler;
99
import dev.ryanhcode.sable.physics.config.block_properties.PhysicsBlockPropertiesDefinitionLoader;
10+
import dev.ryanhcode.sable.physics.config.dimension_physics.DimensionPhysicsData;
1011
import dev.ryanhcode.sable.physics.floating_block.FloatingBlockController;
1112
import dev.ryanhcode.sable.sublevel.ServerSubLevel;
1213
import dev.ryanhcode.sable.sublevel.SubLevel;
@@ -93,6 +94,7 @@ public static void handleBlockChange(final ServerLevel level, final LevelChunk c
9394

9495
public static void syncDataPacket(final VeilPacketManager.PacketSink sink) {
9596
sink.sendPacket(PhysicsBlockPropertiesDefinitionLoader.INSTANCE.getDefinitions().stream().map(ClientboundPhysicsPropertyPacket::new).toArray(CustomPacketPayload[]::new));
97+
sink.sendPacket(DimensionPhysicsData.compilePacket());
9698
sink.sendPacket(FloatingBlockMaterialDataHandler.allMaterials.entrySet().stream().map(e -> new ClientboundFloatingBlockMaterialPacket(e.getKey(), e.getValue())).toArray(CustomPacketPayload[]::new));
9799
}
98100
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package dev.ryanhcode.sable.network.packets.tcp;
2+
3+
import dev.ryanhcode.sable.Sable;
4+
import dev.ryanhcode.sable.network.tcp.SableTCPPacket;
5+
import dev.ryanhcode.sable.physics.config.dimension_physics.DimensionPhysics;
6+
import dev.ryanhcode.sable.physics.config.dimension_physics.DimensionPhysicsData;
7+
import foundry.veil.api.network.handler.PacketContext;
8+
import io.netty.buffer.ByteBuf;
9+
import net.minecraft.client.Minecraft;
10+
import net.minecraft.core.registries.Registries;
11+
import net.minecraft.network.codec.ByteBufCodecs;
12+
import net.minecraft.network.codec.StreamCodec;
13+
import net.minecraft.network.protocol.common.custom.CustomPacketPayload;
14+
import net.minecraft.resources.ResourceKey;
15+
import net.minecraft.world.level.Level;
16+
17+
import java.util.ArrayList;
18+
import java.util.List;
19+
20+
public record ClientboundDimensionPhysicsPacket(List<DimensionPhysics> dimensionPhysics) implements SableTCPPacket {
21+
public static final CustomPacketPayload.Type<ClientboundDimensionPhysicsPacket> TYPE = new CustomPacketPayload.Type<>(Sable.sablePath("dimension_physics"));
22+
23+
public static final StreamCodec<ByteBuf, ClientboundDimensionPhysicsPacket> CODEC = StreamCodec.composite(
24+
ByteBufCodecs.collection(
25+
ArrayList::new,
26+
DimensionPhysics.STREAM_CODEC
27+
), ClientboundDimensionPhysicsPacket::dimensionPhysics,
28+
ClientboundDimensionPhysicsPacket::new
29+
);
30+
31+
32+
@Override
33+
public void handle(final PacketContext context) {
34+
Minecraft.getInstance().execute(() -> {
35+
DimensionPhysicsData.clearPhysics();
36+
for (final DimensionPhysics dimensionPhysic : this.dimensionPhysics) {
37+
final ResourceKey<Level> dimension = ResourceKey.create(Registries.DIMENSION, dimensionPhysic.dimension());
38+
DimensionPhysicsData.putPhysics(dimension, dimensionPhysic);
39+
}
40+
});
41+
}
42+
43+
@Override
44+
public Type<? extends CustomPacketPayload> type() {
45+
return TYPE;
46+
}
47+
}

common/src/main/java/dev/ryanhcode/sable/network/tcp/SableTCPPackets.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ public static void init() {
2626

2727
PACKET_MANAGER.registerClientbound(ClientboundPhysicsPropertyPacket.TYPE, ClientboundPhysicsPropertyPacket.CODEC, ClientboundPhysicsPropertyPacket::handle);
2828
PACKET_MANAGER.registerClientbound(ClientboundFloatingBlockMaterialPacket.TYPE, ClientboundFloatingBlockMaterialPacket.CODEC, ClientboundFloatingBlockMaterialPacket::handle);
29+
PACKET_MANAGER.registerClientbound(ClientboundDimensionPhysicsPacket.TYPE, ClientboundDimensionPhysicsPacket.CODEC, ClientboundDimensionPhysicsPacket::handle);
30+
2931
PACKET_MANAGER.registerClientbound(ClientboundRecentlySplitSubLevelPacket.TYPE, ClientboundRecentlySplitSubLevelPacket.CODEC, ClientboundRecentlySplitSubLevelPacket::handle);
3032

3133
PACKET_MANAGER.registerClientbound(ClientboundSableUDPActivationPacket.TYPE, ClientboundSableUDPActivationPacket.CODEC, ClientboundSableUDPActivationPacket::handle);

common/src/main/java/dev/ryanhcode/sable/physics/config/dimension_physics/BezierResourceFunction.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
import com.mojang.serialization.DataResult;
55
import com.mojang.serialization.codecs.RecordCodecBuilder;
66
import dev.ryanhcode.sable.util.SableCodecUtil;
7+
import io.netty.buffer.ByteBuf;
8+
import net.minecraft.network.codec.ByteBufCodecs;
9+
import net.minecraft.network.codec.StreamCodec;
710

811
import java.util.ArrayList;
912
import java.util.List;
@@ -14,6 +17,11 @@ public class BezierResourceFunction {
1417
(bezierResourceFunction -> DataResult.success(bezierResourceFunction.getPoints()))
1518
);
1619

20+
public static final StreamCodec<ByteBuf, BezierResourceFunction> STREAM_CODEC = StreamCodec.composite(
21+
ByteBufCodecs.collection(ArrayList::new, BezierPoint.STREAM_CODEC), BezierResourceFunction::getPoints,
22+
BezierResourceFunction::new
23+
);
24+
1725
private final List<BezierPoint> points;
1826

1927
public BezierResourceFunction(final List<BezierPoint> points) {
@@ -74,5 +82,12 @@ public record BezierPoint(double altitude, double value, double slope) {
7482
SableCodecUtil.positiveDouble(true).fieldOf("value").forGetter(BezierPoint::value),
7583
Codec.DOUBLE.fieldOf("slope").forGetter(BezierPoint::slope)
7684
).apply(instance, BezierPoint::new));
85+
86+
public static final StreamCodec<ByteBuf, BezierPoint> STREAM_CODEC = StreamCodec.composite(
87+
ByteBufCodecs.DOUBLE, BezierPoint::altitude,
88+
ByteBufCodecs.DOUBLE, BezierPoint::value,
89+
ByteBufCodecs.DOUBLE, BezierPoint::slope,
90+
BezierPoint::new
91+
);
7792
}
7893
}

common/src/main/java/dev/ryanhcode/sable/physics/config/dimension_physics/DimensionPhysics.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
import com.mojang.datafixers.kinds.Applicative;
44
import com.mojang.serialization.Codec;
55
import com.mojang.serialization.codecs.RecordCodecBuilder;
6+
import io.netty.buffer.ByteBuf;
7+
import net.minecraft.network.codec.ByteBufCodecs;
8+
import net.minecraft.network.codec.StreamCodec;
69
import net.minecraft.resources.ResourceLocation;
710
import net.minecraft.util.ExtraCodecs;
811
import net.minecraft.world.level.Level;
@@ -30,6 +33,29 @@ public record DimensionPhysics(ResourceLocation dimension, int priority, Optiona
3033
Codec.BOOL.optionalFieldOf("ignore_chunks", false).forGetter(DimensionPhysics::ignoreChunks)
3134
).apply(Applicative.unbox(instance), DimensionPhysics::new));
3235

36+
public static final StreamCodec<ByteBuf, DimensionPhysics> STREAM_CODEC = StreamCodec.ofMember(
37+
(dim, buf) -> {
38+
ResourceLocation.STREAM_CODEC.encode(buf, dim.dimension);
39+
ByteBufCodecs.INT.encode(buf, dim.priority);
40+
ByteBufCodecs.FLOAT.apply(ByteBufCodecs::optional).encode(buf, dim.universalDrag);
41+
ByteBufCodecs.VECTOR3F.apply(ByteBufCodecs::optional).encode(buf, dim.baseGravity);
42+
ByteBufCodecs.DOUBLE.apply(ByteBufCodecs::optional).encode(buf, dim.basePressure);
43+
BezierResourceFunction.STREAM_CODEC.apply(ByteBufCodecs::optional).encode(buf, dim.pressureFunction);
44+
ByteBufCodecs.VECTOR3F.apply(ByteBufCodecs::optional).encode(buf, dim.magneticNorth);
45+
ByteBufCodecs.BOOL.encode(buf, dim.ignoreChunks);
46+
},
47+
buf -> new DimensionPhysics(
48+
ResourceLocation.STREAM_CODEC.decode(buf),
49+
ByteBufCodecs.INT.decode(buf),
50+
ByteBufCodecs.FLOAT.apply(ByteBufCodecs::optional).decode(buf),
51+
ByteBufCodecs.VECTOR3F.apply(ByteBufCodecs::optional).decode(buf),
52+
ByteBufCodecs.DOUBLE.apply(ByteBufCodecs::optional).decode(buf),
53+
BezierResourceFunction.STREAM_CODEC.apply(ByteBufCodecs::optional).decode(buf),
54+
ByteBufCodecs.VECTOR3F.apply(ByteBufCodecs::optional).decode(buf),
55+
ByteBufCodecs.BOOL.decode(buf)
56+
)
57+
);
58+
3359
public static DimensionPhysics createDefault(final Level level) {
3460
// constructs a bezier air pressure curve approximating an exponential decay, centered around sea level
3561
// clamped to at most 1.5 pressure underground, and with a 40-meter smooth drop-off at the build limit

common/src/main/java/dev/ryanhcode/sable/physics/config/dimension_physics/DimensionPhysicsData.java

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import com.mojang.serialization.JsonOps;
77
import dev.ryanhcode.sable.Sable;
88
import dev.ryanhcode.sable.companion.math.JOMLConversion;
9+
import dev.ryanhcode.sable.network.packets.tcp.ClientboundDimensionPhysicsPacket;
910
import net.minecraft.core.registries.Registries;
1011
import net.minecraft.resources.ResourceKey;
1112
import net.minecraft.resources.ResourceLocation;
@@ -85,6 +86,30 @@ public static double getUniversalDrag(final ServerLevel level) {
8586
return physics.universalDrag().orElseGet(defaultPhysics.universalDrag()::orElseThrow);
8687
}
8788

89+
public static void addPhysicsWithPriority(final ResourceKey<Level> key, final DimensionPhysics newProperties) {
90+
final DimensionPhysics existing = DIMENSION_PHYSICS_DATA.get(key);
91+
92+
if (existing != null) {
93+
if (newProperties.priority() > existing.priority()) {
94+
DIMENSION_PHYSICS_DATA.put(key, newProperties);
95+
}
96+
} else {
97+
DIMENSION_PHYSICS_DATA.put(key, newProperties);
98+
}
99+
}
100+
101+
public static void putPhysics(final ResourceKey<Level> key, final DimensionPhysics newProperties) {
102+
DIMENSION_PHYSICS_DATA.put(key, newProperties);
103+
}
104+
105+
public static void clearPhysics() {
106+
DIMENSION_PHYSICS_DATA.clear();
107+
}
108+
109+
public static ClientboundDimensionPhysicsPacket compilePacket() {
110+
return new ClientboundDimensionPhysicsPacket(DIMENSION_PHYSICS_DATA.values().stream().toList());
111+
}
112+
88113
public static class ReloadListener extends SimpleJsonResourceReloadListener {
89114

90115
private static final Gson GSON = new Gson();
@@ -97,21 +122,9 @@ public ReloadListener() {
97122
super(ReloadListener.GSON, NAME);
98123
}
99124

100-
public static void addKeyWithPriority(final Map<ResourceKey<Level>, DimensionPhysics> data, final ResourceKey<Level> key, final DimensionPhysics newProperties) {
101-
final DimensionPhysics existing = data.get(key);
102-
103-
if (existing != null) {
104-
if (newProperties.priority() > existing.priority()) {
105-
data.put(key, newProperties);
106-
}
107-
} else {
108-
data.put(key, newProperties);
109-
}
110-
}
111-
112125
@Override
113126
protected void apply(final Map<ResourceLocation, JsonElement> map, final ResourceManager resourceManager, final ProfilerFiller profiler) {
114-
DIMENSION_PHYSICS_DATA.clear();
127+
clearPhysics();
115128

116129
for (final Map.Entry<ResourceLocation, JsonElement> entry : map.entrySet()) {
117130
try {
@@ -124,7 +137,7 @@ protected void apply(final Map<ResourceLocation, JsonElement> map, final Resourc
124137
final DimensionPhysics dimensionPhysics = dataResult.getOrThrow();
125138
final ResourceKey<Level> dimension = ResourceKey.create(Registries.DIMENSION, dimensionPhysics.dimension());
126139

127-
addKeyWithPriority(DIMENSION_PHYSICS_DATA, dimension, dimensionPhysics);
140+
addPhysicsWithPriority(dimension, dimensionPhysics);
128141
} catch (final Exception e) {
129142
Sable.LOGGER.error("Error while loading dimension data \"{}\" : {} ", entry.getKey(), e.getMessage());
130143
}

0 commit comments

Comments
 (0)