|
1 | | -package dev.compactmods.machines.api; |
2 | | - |
3 | | -import dev.compactmods.machines.api.dimension.CompactDimension; |
4 | | -import dev.compactmods.machines.api.dimension.MissingDimensionException; |
5 | | -import dev.compactmods.machines.api.room.CompactRoomGenerator; |
6 | | -import dev.compactmods.machines.api.room.IRoomApi; |
7 | | -import dev.compactmods.machines.api.room.RoomInstance; |
8 | | -import dev.compactmods.machines.api.room.RoomTemplate; |
9 | | -import dev.compactmods.machines.api.room.data.IRoomDataAttachmentAccessor; |
10 | | -import dev.compactmods.machines.api.room.history.IPlayerHistoryApi; |
11 | | -import dev.compactmods.machines.api.room.upgrade.RoomUpgradeDefinition; |
12 | | -import net.minecraft.resources.ResourceLocation; |
13 | | -import net.minecraft.server.MinecraftServer; |
14 | | -import net.neoforged.neoforge.attachment.AttachmentHolder; |
15 | | -import net.neoforged.neoforge.attachment.IAttachmentHolder; |
16 | | -import net.neoforged.neoforge.registries.DeferredRegister; |
17 | | -import org.jetbrains.annotations.ApiStatus; |
18 | | - |
19 | | -import java.util.Optional; |
20 | | -import java.util.UUID; |
21 | | - |
22 | | -public class CompactMachines { |
23 | | - public final static String MOD_ID = "compactmachines"; |
24 | | - |
25 | | - public static String id(String path) { |
26 | | - return ResourceLocation.isValidPath(path) ? (MOD_ID + ":" + path) : MOD_ID + ":invalid"; |
27 | | - } |
28 | | - |
29 | | - public static ResourceLocation modRL(String path) { |
30 | | - return ResourceLocation.fromNamespaceAndPath(MOD_ID, path); |
31 | | - } |
32 | | - |
33 | | - public static DeferredRegister<RoomUpgradeDefinition<?>> roomUpgradeDR(String namespace) { |
34 | | - return DeferredRegister.create(RoomUpgradeDefinition.REG_KEY, namespace); |
35 | | - } |
36 | | - |
37 | | - public static IRoomApi roomApi() { |
38 | | - return Internal.ROOM_API; |
39 | | - } |
40 | | - |
41 | | - public static IPlayerHistoryApi playerHistoryApi() { |
42 | | - return Internal.PLAYER_HISTORY_API; |
43 | | - } |
44 | | - |
45 | | - public static Optional<RoomInstance> room(String roomCode) { |
46 | | - return Internal.ROOM_API.registrar().get(roomCode); |
47 | | - } |
48 | | - |
49 | | - /** |
50 | | - * Registers a new room instance and generates the structure in the compact world. |
51 | | - * |
52 | | - * @param server Server to generate room on. |
53 | | - * @param template |
54 | | - * @param owner |
55 | | - * @return |
56 | | - */ |
57 | | - public static RoomInstance newRoom(MinecraftServer server, RoomTemplate template, UUID owner) throws MissingDimensionException { |
58 | | - final var instance = CompactMachines.roomApi().registrar().createNew(template, owner); |
59 | | - final var compactDim = CompactDimension.forServer(server); |
60 | | - CompactRoomGenerator.generateRoom(compactDim, instance.boundaries().outerBounds()); |
61 | | - |
62 | | - if(!template.structures().isEmpty()) { |
63 | | - for(var struct : template.structures()) { |
64 | | - CompactRoomGenerator.populateStructure(compactDim, struct.template(), instance.boundaries().innerBounds(), struct.placement()); |
65 | | - } |
66 | | - } |
67 | | - |
68 | | - return instance; |
69 | | - } |
70 | | - |
71 | | - public static boolean isValidRoomCode(String roomCode) { |
72 | | - return Internal.ROOM_API.roomCodeValidator().test(roomCode); |
73 | | - } |
74 | | - |
75 | | - public static Optional<? extends IAttachmentHolder> existingRoomData(String code) { |
76 | | - return Internal.ROOM_DATA_ACCESSOR.get(code); |
77 | | - } |
78 | | - |
79 | | - public static IAttachmentHolder roomData(String code) { |
80 | | - return Internal.ROOM_DATA_ACCESSOR.getOrCreate(code); |
81 | | - } |
82 | | - |
83 | | - |
84 | | - /** |
85 | | - * Set up when the server or single-player instance changes. |
86 | | - * NOT for API consumers to use! Use the methods provided here for safety. |
87 | | - * |
88 | | - * @since 6.0.0 |
89 | | - */ |
90 | | - @ApiStatus.Internal |
91 | | - @Deprecated |
92 | | - public final class Internal { |
93 | | - public static IRoomApi ROOM_API; |
94 | | - public static IRoomDataAttachmentAccessor ROOM_DATA_ACCESSOR; |
95 | | - public static IPlayerHistoryApi PLAYER_HISTORY_API; |
96 | | - } |
97 | | -} |
| 1 | +package dev.compactmods.machines.api; |
| 2 | + |
| 3 | +import dev.compactmods.machines.api.dimension.CompactDimension; |
| 4 | +import dev.compactmods.machines.api.dimension.MissingDimensionException; |
| 5 | +import dev.compactmods.machines.api.room.CompactRoomGenerator; |
| 6 | +import dev.compactmods.machines.api.room.IRoomApi; |
| 7 | +import dev.compactmods.machines.api.room.RoomInstance; |
| 8 | +import dev.compactmods.machines.api.room.template.RoomTemplate; |
| 9 | +import dev.compactmods.machines.api.room.data.IRoomDataAttachmentAccessor; |
| 10 | +import dev.compactmods.machines.api.room.history.IPlayerHistoryApi; |
| 11 | +import dev.compactmods.machines.api.room.upgrade.RoomUpgradeDefinition; |
| 12 | +import dev.compactmods.machines.api.util.BlockSpaceUtil; |
| 13 | +import net.minecraft.core.Direction; |
| 14 | +import net.minecraft.resources.ResourceLocation; |
| 15 | +import net.minecraft.server.MinecraftServer; |
| 16 | +import net.minecraft.world.level.block.Block; |
| 17 | +import net.minecraft.world.phys.AABB; |
| 18 | +import net.minecraft.world.phys.Vec2; |
| 19 | +import net.neoforged.neoforge.attachment.IAttachmentHolder; |
| 20 | +import net.neoforged.neoforge.registries.DeferredRegister; |
| 21 | +import org.jetbrains.annotations.ApiStatus; |
| 22 | + |
| 23 | +import java.util.Optional; |
| 24 | +import java.util.UUID; |
| 25 | + |
| 26 | +public class CompactMachines { |
| 27 | + public final static String MOD_ID = "compactmachines"; |
| 28 | + |
| 29 | + public static String id(String path) { |
| 30 | + return ResourceLocation.isValidPath(path) ? (MOD_ID + ":" + path) : MOD_ID + ":invalid"; |
| 31 | + } |
| 32 | + |
| 33 | + public static ResourceLocation modRL(String path) { |
| 34 | + return ResourceLocation.fromNamespaceAndPath(MOD_ID, path); |
| 35 | + } |
| 36 | + |
| 37 | + public static DeferredRegister<RoomUpgradeDefinition<?>> roomUpgradeDR(String namespace) { |
| 38 | + return DeferredRegister.create(RoomUpgradeDefinition.REG_KEY, namespace); |
| 39 | + } |
| 40 | + |
| 41 | + public static IRoomApi roomApi() { |
| 42 | + return Internal.ROOM_API; |
| 43 | + } |
| 44 | + |
| 45 | + public static IPlayerHistoryApi playerHistoryApi() { |
| 46 | + return Internal.PLAYER_HISTORY_API; |
| 47 | + } |
| 48 | + |
| 49 | + public static Optional<RoomInstance> room(String roomCode) { |
| 50 | + return Internal.ROOM_API.registrar().get(roomCode); |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * Registers a new room instance and generates the structure in the compact world. |
| 55 | + * |
| 56 | + * @param server Server to generate room on. |
| 57 | + * @param template |
| 58 | + * @param owner |
| 59 | + * @return |
| 60 | + */ |
| 61 | + public static RoomInstance newRoom(MinecraftServer server, RoomTemplate template, UUID owner) throws MissingDimensionException { |
| 62 | + final var instance = CompactMachines.roomApi().registrar().createNew(template, owner); |
| 63 | + final var compactDim = CompactDimension.forServer(server); |
| 64 | + CompactRoomGenerator.generateRoom(compactDim, instance.boundaries().outerBounds()); |
| 65 | + |
| 66 | + if (!template.structures().isEmpty()) { |
| 67 | + for (var struct : template.structures()) { |
| 68 | + CompactRoomGenerator.populateStructure(compactDim, struct.template(), instance.boundaries().innerBounds(), struct.placement()); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + final var spawnManager = CompactMachines.roomApi().spawnManager(instance.code()); |
| 73 | + template.optionalFloor().ifPresent(floorState -> { |
| 74 | + var fixedSpawn = instance.boundaries() |
| 75 | + .defaultSpawn() |
| 76 | + .add(0, 1, 0); |
| 77 | + |
| 78 | + spawnManager.setDefaultSpawn(fixedSpawn, Vec2.ZERO); |
| 79 | + |
| 80 | + AABB floorBounds = BlockSpaceUtil.getWallBounds(instance.boundaries().innerBounds(), Direction.DOWN); |
| 81 | + BlockSpaceUtil.blocksInside(floorBounds).forEach(floorBlockPos -> { |
| 82 | + compactDim.setBlock(floorBlockPos, floorState, Block.UPDATE_ALL); |
| 83 | + }); |
| 84 | + }); |
| 85 | + |
| 86 | + return instance; |
| 87 | + } |
| 88 | + |
| 89 | + public static boolean isValidRoomCode(String roomCode) { |
| 90 | + return Internal.ROOM_API.roomCodeValidator().test(roomCode); |
| 91 | + } |
| 92 | + |
| 93 | + public static Optional<? extends IAttachmentHolder> existingRoomData(String code) { |
| 94 | + return Internal.ROOM_DATA_ACCESSOR.get(code); |
| 95 | + } |
| 96 | + |
| 97 | + public static IAttachmentHolder roomData(String code) { |
| 98 | + return Internal.ROOM_DATA_ACCESSOR.getOrCreate(code); |
| 99 | + } |
| 100 | + |
| 101 | + |
| 102 | + /** |
| 103 | + * Set up when the server or single-player instance changes. |
| 104 | + * NOT for API consumers to use! Use the methods provided here for safety. |
| 105 | + * |
| 106 | + * @since 6.0.0 |
| 107 | + */ |
| 108 | + @ApiStatus.Internal |
| 109 | + @Deprecated |
| 110 | + public final class Internal { |
| 111 | + public static IRoomApi ROOM_API; |
| 112 | + public static IRoomDataAttachmentAccessor ROOM_DATA_ACCESSOR; |
| 113 | + public static IPlayerHistoryApi PLAYER_HISTORY_API; |
| 114 | + } |
| 115 | +} |
0 commit comments