|
1 | | -package dev.compactmods.machines.api.room; |
2 | | - |
3 | | -import dev.compactmods.machines.api.WallConstants; |
4 | | -import dev.compactmods.machines.api.room.spatial.IRoomBoundaries; |
5 | | -import dev.compactmods.machines.api.util.AABBAligner; |
6 | | -import dev.compactmods.machines.api.util.AABBHelper; |
7 | | -import dev.compactmods.machines.api.util.BlockSpaceUtil; |
8 | | -import dev.compactmods.machines.api.util.VectorUtils; |
9 | | -import net.minecraft.core.BlockPos; |
10 | | -import net.minecraft.core.Direction; |
11 | | -import net.minecraft.core.Vec3i; |
12 | | -import net.minecraft.core.registries.BuiltInRegistries; |
13 | | -import net.minecraft.resources.ResourceLocation; |
14 | | -import net.minecraft.server.level.ServerLevel; |
15 | | -import net.minecraft.world.level.LevelAccessor; |
16 | | -import net.minecraft.world.level.block.Block; |
17 | | -import net.minecraft.world.level.block.Blocks; |
18 | | -import net.minecraft.world.level.block.Mirror; |
19 | | -import net.minecraft.world.level.block.Rotation; |
20 | | -import net.minecraft.world.level.block.state.BlockState; |
21 | | -import net.minecraft.world.level.levelgen.structure.templatesystem.StructurePlaceSettings; |
22 | | -import net.minecraft.world.phys.AABB; |
23 | | -import net.minecraft.world.phys.Vec3; |
24 | | -import org.joml.Vector3d; |
25 | | - |
26 | | -public class CompactRoomGenerator { |
27 | | - |
28 | | - /** |
29 | | - * Generates a wall or platform in a given direction. |
30 | | - * Uses the solid wall block. |
31 | | - * |
32 | | - * @param world |
33 | | - * @param outerBounds |
34 | | - * @param wallDirection |
35 | | - * @since 3.0.0 |
36 | | - */ |
37 | | - public static void generateCompactWall(LevelAccessor world, AABB outerBounds, Direction wallDirection, BlockState block) { |
38 | | - AABB wallBounds = BlockSpaceUtil.getWallBounds(outerBounds, wallDirection); |
39 | | - BlockSpaceUtil.blocksInside(wallBounds).forEach(wallBlock -> { |
40 | | - world.setBlock(wallBlock, block, Block.UPDATE_ALL); |
41 | | - }); |
42 | | - } |
43 | | - |
44 | | - /** |
45 | | - * Generates a machine "internal" structure in a world via a machine size and a central point. |
46 | | - * |
47 | | - * @param world |
48 | | - * @param outerBounds Outer dimensions of the room. |
49 | | - */ |
50 | | - public static void generateRoom(LevelAccessor world, AABB outerBounds) { |
51 | | - final var block = BuiltInRegistries.BLOCK.get(WallConstants.SOLID_WALL); |
52 | | - if (block != null) { |
53 | | - final var solidWall = block.defaultBlockState(); |
54 | | - generateRoom(world, outerBounds, solidWall); |
55 | | - } |
56 | | - } |
57 | | - |
58 | | - /** |
59 | | - * Generates a machine structure in a world via machine boundaries and a wall block. |
60 | | - * |
61 | | - * @param world |
62 | | - * @param outerBounds Outer dimensions of the room. |
63 | | - * @param block Block to use for walls. |
64 | | - */ |
65 | | - public static void generateRoom(LevelAccessor world, AABB outerBounds, BlockState block) { |
66 | | - |
67 | | - // Generate the walls |
68 | | - for (final var dir : Direction.values()) |
69 | | - generateCompactWall(world, outerBounds, dir, block); |
70 | | - |
71 | | - // Clear out the inside of the room |
72 | | - AABB machineInternal = outerBounds.deflate(1); |
73 | | - BlockSpaceUtil.blocksInside(machineInternal) |
74 | | - .forEach(p -> world.setBlock(p, Blocks.AIR.defaultBlockState(), 7)); |
75 | | - } |
76 | | - |
77 | | - public static void populateStructure(ServerLevel level, ResourceLocation template, AABB roomInnerBounds, RoomStructureInfo.RoomStructurePlacement placement) { |
78 | | - level.getStructureManager().get(template).ifPresent(tem -> { |
79 | | - |
80 | | - Vector3d templateSize = VectorUtils.convert3d(tem.getSize()); |
81 | | - |
82 | | - if (!AABBHelper.fitsInside(roomInnerBounds, templateSize)) { |
83 | | - // skip: structure too large to place in room |
84 | | - return; |
85 | | - } |
86 | | - |
87 | | - var placementSettings = new StructurePlaceSettings() |
88 | | - .setRotation(Rotation.NONE) |
89 | | - .setMirror(Mirror.NONE); |
90 | | - |
91 | | - AABB placementBounds = null; |
92 | | - final AABBAligner aligner = AABBAligner.create(roomInnerBounds, templateSize); |
93 | | - switch (placement) { |
94 | | - case CENTERED -> placementBounds = aligner |
95 | | - .center(roomInnerBounds.getCenter()) |
96 | | - .align(); |
97 | | - |
98 | | - case CENTERED_CEILING -> placementBounds = aligner |
99 | | - .boundedDirection(Direction.UP) |
100 | | - .align(); |
101 | | - |
102 | | - case CENTERED_FLOOR -> placementBounds = aligner |
103 | | - .boundedDirection(Direction.DOWN) |
104 | | - .align(); |
105 | | - } |
106 | | - |
107 | | - if(placementBounds != null) { |
108 | | - BlockPos placeAt = BlockPos.containing(AABBHelper.minCorner(placementBounds)); |
109 | | - tem.placeInWorld(level, placeAt, placeAt, placementSettings, level.random, Block.UPDATE_ALL); |
110 | | - } |
111 | | - }); |
112 | | - } |
113 | | -} |
| 1 | +package dev.compactmods.machines.api.room; |
| 2 | + |
| 3 | +import dev.compactmods.machines.api.WallConstants; |
| 4 | +import dev.compactmods.machines.api.room.spatial.IRoomBoundaries; |
| 5 | +import dev.compactmods.machines.api.util.AABBAligner; |
| 6 | +import dev.compactmods.machines.api.util.AABBHelper; |
| 7 | +import dev.compactmods.machines.api.util.BlockSpaceUtil; |
| 8 | +import dev.compactmods.machines.api.util.VectorUtils; |
| 9 | +import net.minecraft.core.BlockPos; |
| 10 | +import net.minecraft.core.Direction; |
| 11 | +import net.minecraft.core.Vec3i; |
| 12 | +import net.minecraft.core.registries.BuiltInRegistries; |
| 13 | +import net.minecraft.resources.ResourceLocation; |
| 14 | +import net.minecraft.server.level.ServerLevel; |
| 15 | +import net.minecraft.world.level.Level; |
| 16 | +import net.minecraft.world.level.LevelAccessor; |
| 17 | +import net.minecraft.world.level.block.Block; |
| 18 | +import net.minecraft.world.level.block.Blocks; |
| 19 | +import net.minecraft.world.level.block.Mirror; |
| 20 | +import net.minecraft.world.level.block.Rotation; |
| 21 | +import net.minecraft.world.level.block.state.BlockState; |
| 22 | +import net.minecraft.world.level.levelgen.structure.templatesystem.StructurePlaceSettings; |
| 23 | +import net.minecraft.world.phys.AABB; |
| 24 | +import net.minecraft.world.phys.Vec3; |
| 25 | +import org.joml.Vector3d; |
| 26 | + |
| 27 | +public class CompactRoomGenerator { |
| 28 | + |
| 29 | + /** |
| 30 | + * Generates a wall or platform in a given direction. |
| 31 | + * Uses the solid wall block. |
| 32 | + * |
| 33 | + * @param world |
| 34 | + * @param outerBounds |
| 35 | + * @param wallDirection |
| 36 | + * @since 3.0.0 |
| 37 | + */ |
| 38 | + public static void generateCompactWall(LevelAccessor world, AABB outerBounds, Direction wallDirection, BlockState block) { |
| 39 | + AABB wallBounds = BlockSpaceUtil.getWallBounds(outerBounds, wallDirection); |
| 40 | + BlockSpaceUtil.blocksInside(wallBounds).forEach(wallBlock -> { |
| 41 | + world.setBlock(wallBlock, block, Block.UPDATE_ALL); |
| 42 | + }); |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * Generates a machine "internal" structure in a world via a machine size and a central point. |
| 47 | + * |
| 48 | + * @param world |
| 49 | + * @param outerBounds Outer dimensions of the room. |
| 50 | + */ |
| 51 | + public static void generateRoom(LevelAccessor world, AABB outerBounds) { |
| 52 | + final var block = BuiltInRegistries.BLOCK.get(WallConstants.SOLID_WALL); |
| 53 | + if (block != null) { |
| 54 | + final var solidWall = block.defaultBlockState(); |
| 55 | + generateRoom(world, outerBounds, solidWall); |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * Generates a machine structure in a world via machine boundaries and a wall block. |
| 61 | + * |
| 62 | + * @param world |
| 63 | + * @param outerBounds Outer dimensions of the room. |
| 64 | + * @param block Block to use for walls. |
| 65 | + */ |
| 66 | + public static void generateRoom(LevelAccessor world, AABB outerBounds, BlockState block) { |
| 67 | + |
| 68 | + // Generate the walls |
| 69 | + for (final var dir : Direction.values()) |
| 70 | + generateCompactWall(world, outerBounds, dir, block); |
| 71 | + |
| 72 | + // Clear out the inside of the room |
| 73 | + AABB machineInternal = outerBounds.deflate(1); |
| 74 | + BlockSpaceUtil.blocksInside(machineInternal) |
| 75 | + .forEach(p -> world.setBlock(p, Blocks.AIR.defaultBlockState(), 7)); |
| 76 | + } |
| 77 | + |
| 78 | + public static void populateStructure(ServerLevel level, ResourceLocation template, AABB roomInnerBounds, RoomStructureInfo.RoomStructurePlacement placement) { |
| 79 | + level.getStructureManager().get(template).ifPresent(tem -> { |
| 80 | + |
| 81 | + Vector3d templateSize = VectorUtils.convert3d(tem.getSize()); |
| 82 | + |
| 83 | + if (!AABBHelper.fitsInside(roomInnerBounds, templateSize)) { |
| 84 | + // skip: structure too large to place in room |
| 85 | + return; |
| 86 | + } |
| 87 | + |
| 88 | + var placementSettings = new StructurePlaceSettings() |
| 89 | + .setRotation(Rotation.NONE) |
| 90 | + .setMirror(Mirror.NONE); |
| 91 | + |
| 92 | + AABB placementBounds = null; |
| 93 | + final AABBAligner aligner = AABBAligner.create(roomInnerBounds, templateSize); |
| 94 | + switch (placement) { |
| 95 | + case CENTERED -> placementBounds = aligner |
| 96 | + .center(roomInnerBounds.getCenter()) |
| 97 | + .align(); |
| 98 | + |
| 99 | + case CENTERED_CEILING -> placementBounds = aligner |
| 100 | + .boundedDirection(Direction.UP) |
| 101 | + .align(); |
| 102 | + |
| 103 | + case CENTERED_FLOOR -> placementBounds = aligner |
| 104 | + .boundedDirection(Direction.DOWN) |
| 105 | + .align(); |
| 106 | + } |
| 107 | + |
| 108 | + if(placementBounds != null) { |
| 109 | + BlockPos placeAt = BlockPos.containing(AABBHelper.minCorner(placementBounds)); |
| 110 | + tem.placeInWorld(level, placeAt, placeAt, placementSettings, level.random, Block.UPDATE_ALL); |
| 111 | + } |
| 112 | + }); |
| 113 | + } |
| 114 | +} |
0 commit comments