Skip to content
This repository has been archived by the owner on May 4, 2024. It is now read-only.

Commit

Permalink
Switch room template dimensions to either be an object or a single int
Browse files Browse the repository at this point in the history
  • Loading branch information
robotgryphon committed Mar 24, 2024
1 parent c841025 commit 92f3dc9
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -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<RoomDimensions> 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<RoomDimensions> 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<RoomDimensions> {

public static final Decoder INSTANCE = new Decoder();

@Override
public <T> DataResult<Pair<RoomDimensions, T>> decode(DynamicOps<T> 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.");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<RoomStructureInfo> structures) {
public record RoomTemplate(RoomDimensions internalDimensions, int color, List<RoomStructureInfo> structures) {

public static final ResourceKey<Registry<RoomTemplate>> REGISTRY_KEY = ResourceKey.createRegistryKey(new ResourceLocation(MOD_ID, "room_templates"));

public static final RoomTemplate INVALID_TEMPLATE = new RoomTemplate(0, 0);

public static Codec<RoomTemplate> 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);
}
}

0 comments on commit 92f3dc9

Please sign in to comment.