Skip to content

Commit 3631346

Browse files
committed
More codec improvements, data updates
- Creates copy of World registry key codec for use in DimensionalPos - Swaps machine tile getSpawn to use a DimensionalPos instead of BlockPos - Minor Codec renames - Move CMInternalData into CompactRoomData - RoomData now private, with access limited via CompactRoomData - PSD now calls getSpawn on the room data, instead of fetching entire RD instance and manually setting WSD dirty - Overhaul to DimPos to include a rotation (for spawning) and improve Codec usage (no more string serializing) - TeleEventHandler updated to call CompactRoomData#getInnerBounds - Add extra methods to MathUtil for chunk and BlockPos calculation - New room generation on player first entry improved - Teleportation into rooms now respects spawn rotation - Update unit tests to swap codec references
1 parent cf0c624 commit 3631346

File tree

14 files changed

+257
-164
lines changed

14 files changed

+257
-164
lines changed

src/main/java/com/robotgryphon/compactmachines/block/tiles/CompactMachineTile.java

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import com.robotgryphon.compactmachines.config.ServerConfig;
44
import com.robotgryphon.compactmachines.core.Registration;
5-
import com.robotgryphon.compactmachines.data.machine.CompactMachineInternalData;
65
import com.robotgryphon.compactmachines.data.persistent.CompactRoomData;
76
import com.robotgryphon.compactmachines.data.persistent.MachineConnections;
87
import com.robotgryphon.compactmachines.data.player.CompactMachinePlayerData;
@@ -210,13 +209,13 @@ public CompoundNBT getUpdateTag() {
210209
}
211210

212211
public Optional<ChunkPos> getInternalChunkPos() {
213-
if(level instanceof ServerWorld) {
212+
if (level instanceof ServerWorld) {
214213
MinecraftServer serv = level.getServer();
215-
if(serv == null)
214+
if (serv == null)
216215
return Optional.empty();
217216

218217
MachineConnections connections = MachineConnections.get(serv);
219-
if(connections == null)
218+
if (connections == null)
220219
return Optional.empty();
221220

222221
return connections.graph.getConnectedRoom(this.machineId);
@@ -290,11 +289,11 @@ protected void doChunkload(boolean force) {
290289
}
291290

292291
public void doPostPlaced() {
293-
if(this.level == null || this.level.isClientSide)
292+
if (this.level == null || this.level.isClientSide)
294293
return;
295294

296295
MinecraftServer serv = this.level.getServer();
297-
if(serv == null)
296+
if (serv == null)
298297
return;
299298

300299
DimensionalPosition dp = new DimensionalPosition(
@@ -309,7 +308,7 @@ public void doPostPlaced() {
309308
}
310309

311310
public void handlePlayerLeft(UUID playerID) {
312-
if(this.playerData != null)
311+
if (this.playerData != null)
313312
this.playerData.removePlayer(playerID);
314313
}
315314

@@ -323,27 +322,26 @@ public boolean mapped() {
323322
return getInternalChunkPos().isPresent();
324323
}
325324

326-
public Optional<BlockPos> getSpawn() {
327-
if(level instanceof ServerWorld) {
325+
public Optional<DimensionalPosition> getSpawn() {
326+
if (level instanceof ServerWorld) {
328327
ServerWorld serverWorld = (ServerWorld) level;
329328
MinecraftServer serv = serverWorld.getServer();
330329

331330
MachineConnections connections = MachineConnections.get(serv);
332-
if(connections == null)
331+
if (connections == null)
333332
return Optional.empty();
334333

335334
Optional<ChunkPos> connectedRoom = connections.graph.getConnectedRoom(machineId);
336335

337-
if(!connectedRoom.isPresent())
336+
if (!connectedRoom.isPresent())
338337
return Optional.empty();
339338

340339
CompactRoomData roomData = CompactRoomData.get(serv);
341-
if(roomData == null)
342-
return Optional.empty();
340+
if (roomData == null)
341+
return Optional.empty();
343342

344343
ChunkPos chunk = connectedRoom.get();
345-
return roomData.forChunk(chunk)
346-
.map(CompactMachineInternalData::getSpawn);
344+
return Optional.ofNullable(roomData.getSpawn(chunk));
347345
}
348346

349347
return Optional.empty();

src/main/java/com/robotgryphon/compactmachines/data/codec/CodecExtensions.java

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,20 @@
22

33
import com.mojang.serialization.Codec;
44
import com.mojang.serialization.DataResult;
5-
import com.mojang.serialization.DynamicOps;
6-
import com.mojang.serialization.codecs.PrimitiveCodec;
7-
import com.mojang.serialization.codecs.RecordCodecBuilder;
5+
import net.minecraft.util.RegistryKey;
6+
import net.minecraft.util.ResourceLocation;
87
import net.minecraft.util.Util;
98
import net.minecraft.util.math.ChunkPos;
109
import net.minecraft.util.math.vector.Vector3d;
11-
import org.lwjgl.system.CallbackI;
10+
import net.minecraft.util.registry.Registry;
11+
import net.minecraft.world.World;
1212

13-
import java.util.Arrays;
14-
import java.util.List;
1513
import java.util.UUID;
16-
import java.util.stream.BaseStream;
17-
import java.util.stream.Collectors;
1814
import java.util.stream.DoubleStream;
1915
import java.util.stream.IntStream;
2016

2117
public abstract class CodecExtensions {
22-
public static Codec<UUID> UUID_CODEC = Codec.STRING
18+
public static final Codec<UUID> UUID_CODEC = Codec.STRING
2319
.comapFlatMap((s) -> {
2420
try {
2521
return DataResult.success(UUID.fromString(s));
@@ -28,11 +24,14 @@ public abstract class CodecExtensions {
2824
}
2925
}, UUID::toString).stable();
3026

31-
public static Codec<Vector3d> VECTOR3D_CODEC = DoubleStreamExtensions.CODEC
27+
public static final Codec<RegistryKey<World>> WORLD_REGISTRY_KEY = ResourceLocation.CODEC
28+
.xmap(RegistryKey.elementKey(Registry.DIMENSION_REGISTRY), RegistryKey::location);
29+
30+
public static final Codec<Vector3d> VECTOR3D = DoubleStreamExtensions.CODEC
3231
.comapFlatMap(i -> DoubleStreamExtensions.fixedDoubleSize(i, 3)
3332
.map(out -> new Vector3d(out[0], out[1], out[2])), vec -> DoubleStream.of(vec.x, vec.y, vec.z));
3433

35-
public static Codec<ChunkPos> CHUNKPOS_CODEC = Codec.INT_STREAM
34+
public static final Codec<ChunkPos> CHUNKPOS = Codec.INT_STREAM
3635
.comapFlatMap(i -> Util.fixedSize(i, 2)
3736
.map(arr -> new ChunkPos(arr[0], arr[1])), pos -> IntStream.of(pos.x, pos.z));
3837
}

src/main/java/com/robotgryphon/compactmachines/data/graph/CompactMachineConnectionGraph.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ private static class CompactMachineConnectionInfo {
133133
private final List<Integer> connectedMachines;
134134

135135
public static final Codec<CompactMachineConnectionInfo> CODEC = RecordCodecBuilder.create(i -> i.group(
136-
CodecExtensions.CHUNKPOS_CODEC
136+
CodecExtensions.CHUNKPOS
137137
.fieldOf("machine")
138138
.forGetter(CompactMachineConnectionInfo::room),
139139

src/main/java/com/robotgryphon/compactmachines/data/machine/CompactMachineInternalData.java

Lines changed: 0 additions & 61 deletions
This file was deleted.

0 commit comments

Comments
 (0)