From 92f3dc944eef96921656884740222c2eb2a08667 Mon Sep 17 00:00:00 2001 From: Ted Senft Date: Sun, 24 Mar 2024 04:39:22 -0400 Subject: [PATCH] Switch room template dimensions to either be an object or a single int --- .../machines/api/room/RoomDimensions.java | 54 +++++++++++++++++++ .../machines/api/room/RoomTemplate.java | 12 ++--- 2 files changed, 60 insertions(+), 6 deletions(-) create mode 100644 room-api/src/main/java/dev/compactmods/machines/api/room/RoomDimensions.java diff --git a/room-api/src/main/java/dev/compactmods/machines/api/room/RoomDimensions.java b/room-api/src/main/java/dev/compactmods/machines/api/room/RoomDimensions.java new file mode 100644 index 0000000..652e2d4 --- /dev/null +++ b/room-api/src/main/java/dev/compactmods/machines/api/room/RoomDimensions.java @@ -0,0 +1,54 @@ +package dev.compactmods.machines.api.room; + +import com.mojang.datafixers.util.Pair; +import com.mojang.serialization.Codec; +import com.mojang.serialization.DataResult; +import com.mojang.serialization.DynamicOps; +import com.mojang.serialization.codecs.RecordCodecBuilder; + +public record RoomDimensions(int width, int depth, int height) { + private static final Codec FULL_CODEC = RecordCodecBuilder.create(inst -> inst.group( + Codec.intRange(3, 45).fieldOf("width").forGetter(RoomDimensions::width), + Codec.intRange(3, 45).fieldOf("depth").forGetter(RoomDimensions::depth), + Codec.intRange(3, 45).fieldOf("height").forGetter(RoomDimensions::height) + ).apply(inst, RoomDimensions::new)); + + public static final Codec CODEC = Codec.of(FULL_CODEC, Decoder.INSTANCE); + + public RoomDimensions(int cubicSize) { + this(Math.min(cubicSize, 45), Math.min(cubicSize, 45), Math.min(cubicSize, 45)); + } + + public static RoomDimensions cubic(int cubic) { + return new RoomDimensions(cubic); + } + + @Override + public String toString() { + return "%s x %s x %s".formatted(width, depth, height); + } + + private static class Decoder implements com.mojang.serialization.Decoder { + + public static final Decoder INSTANCE = new Decoder(); + + @Override + public DataResult> decode(DynamicOps dynamicOps, T t) { + final var asNum = dynamicOps.withParser(Codec.intRange(3, 45)) + .apply(t) + .get(); + + if (asNum.left().isPresent()) + return DataResult.success(Pair.of(RoomDimensions.cubic(asNum.left().get()), t)); + + final var asObj = dynamicOps.withParser(FULL_CODEC) + .apply(t) + .get(); + + if (asObj.left().isPresent()) + return DataResult.success(Pair.of(asObj.left().get(), t)); + + return DataResult.error(() -> "Dimensions must either be a single integer between 3-45, or specify width/depth/height."); + } + } +} diff --git a/room-api/src/main/java/dev/compactmods/machines/api/room/RoomTemplate.java b/room-api/src/main/java/dev/compactmods/machines/api/room/RoomTemplate.java index 590c778..f199b49 100644 --- a/room-api/src/main/java/dev/compactmods/machines/api/room/RoomTemplate.java +++ b/room-api/src/main/java/dev/compactmods/machines/api/room/RoomTemplate.java @@ -25,31 +25,31 @@ * @param color The color of the machine blocks created for this template. * @param prefillTemplate A template (structure) file reference, if specified this will fill the new room post-generation */ -public record RoomTemplate(Vec3i internalDimensions, int color, List structures) { +public record RoomTemplate(RoomDimensions internalDimensions, int color, List structures) { public static final ResourceKey> REGISTRY_KEY = ResourceKey.createRegistryKey(new ResourceLocation(MOD_ID, "room_templates")); public static final RoomTemplate INVALID_TEMPLATE = new RoomTemplate(0, 0); public static Codec CODEC = RecordCodecBuilder.create(i -> i.group( - Vec3i.CODEC.fieldOf("dimensions").forGetter(RoomTemplate::internalDimensions), + RoomDimensions.CODEC.fieldOf("dimensions").forGetter(RoomTemplate::internalDimensions), Codec.INT.fieldOf("color").forGetter(RoomTemplate::color), RoomStructureInfo.CODEC.listOf().optionalFieldOf("structures", Collections.emptyList()) .forGetter(RoomTemplate::structures) ).apply(i, RoomTemplate::new)); public RoomTemplate(int cubicSizeInternal, int color) { - this(new Vec3i(cubicSizeInternal, cubicSizeInternal, cubicSizeInternal), color, Collections.emptyList()); + this(RoomDimensions.cubic(cubicSizeInternal), color, Collections.emptyList()); } public AABB getZeroBoundaries() { - return AABB.ofSize(Vec3.ZERO, internalDimensions.getX(), internalDimensions.getY(), internalDimensions.getZ()) + return AABB.ofSize(Vec3.ZERO, internalDimensions.width(), internalDimensions.height(), internalDimensions.depth()) .inflate(1) - .move(0, internalDimensions.getY() / 2f, 0); + .move(0, internalDimensions.height() / 2f, 0); } public AABB getBoundariesCenteredAt(Vec3 center) { - return AABB.ofSize(center, internalDimensions.getX(), internalDimensions.getY(), internalDimensions.getZ()) + return AABB.ofSize(center, internalDimensions.width(), internalDimensions.height(), internalDimensions.depth()) .inflate(1); } }