Skip to content

Commit

Permalink
SEND IT
Browse files Browse the repository at this point in the history
  • Loading branch information
robotgryphon committed Aug 4, 2024
1 parent e33337c commit b0fc5ec
Show file tree
Hide file tree
Showing 34 changed files with 2,235 additions and 2,095 deletions.
212 changes: 115 additions & 97 deletions core-api/src/main/java/dev/compactmods/machines/api/CompactMachines.java
Original file line number Diff line number Diff line change
@@ -1,97 +1,115 @@
package dev.compactmods.machines.api;

import dev.compactmods.machines.api.dimension.CompactDimension;
import dev.compactmods.machines.api.dimension.MissingDimensionException;
import dev.compactmods.machines.api.room.CompactRoomGenerator;
import dev.compactmods.machines.api.room.IRoomApi;
import dev.compactmods.machines.api.room.RoomInstance;
import dev.compactmods.machines.api.room.RoomTemplate;
import dev.compactmods.machines.api.room.data.IRoomDataAttachmentAccessor;
import dev.compactmods.machines.api.room.history.IPlayerHistoryApi;
import dev.compactmods.machines.api.room.upgrade.RoomUpgradeDefinition;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.MinecraftServer;
import net.neoforged.neoforge.attachment.AttachmentHolder;
import net.neoforged.neoforge.attachment.IAttachmentHolder;
import net.neoforged.neoforge.registries.DeferredRegister;
import org.jetbrains.annotations.ApiStatus;

import java.util.Optional;
import java.util.UUID;

public class CompactMachines {
public final static String MOD_ID = "compactmachines";

public static String id(String path) {
return ResourceLocation.isValidPath(path) ? (MOD_ID + ":" + path) : MOD_ID + ":invalid";
}

public static ResourceLocation modRL(String path) {
return ResourceLocation.fromNamespaceAndPath(MOD_ID, path);
}

public static DeferredRegister<RoomUpgradeDefinition<?>> roomUpgradeDR(String namespace) {
return DeferredRegister.create(RoomUpgradeDefinition.REG_KEY, namespace);
}

public static IRoomApi roomApi() {
return Internal.ROOM_API;
}

public static IPlayerHistoryApi playerHistoryApi() {
return Internal.PLAYER_HISTORY_API;
}

public static Optional<RoomInstance> room(String roomCode) {
return Internal.ROOM_API.registrar().get(roomCode);
}

/**
* Registers a new room instance and generates the structure in the compact world.
*
* @param server Server to generate room on.
* @param template
* @param owner
* @return
*/
public static RoomInstance newRoom(MinecraftServer server, RoomTemplate template, UUID owner) throws MissingDimensionException {
final var instance = CompactMachines.roomApi().registrar().createNew(template, owner);
final var compactDim = CompactDimension.forServer(server);
CompactRoomGenerator.generateRoom(compactDim, instance.boundaries().outerBounds());

if(!template.structures().isEmpty()) {
for(var struct : template.structures()) {
CompactRoomGenerator.populateStructure(compactDim, struct.template(), instance.boundaries().innerBounds(), struct.placement());
}
}

return instance;
}

public static boolean isValidRoomCode(String roomCode) {
return Internal.ROOM_API.roomCodeValidator().test(roomCode);
}

public static Optional<? extends IAttachmentHolder> existingRoomData(String code) {
return Internal.ROOM_DATA_ACCESSOR.get(code);
}

public static IAttachmentHolder roomData(String code) {
return Internal.ROOM_DATA_ACCESSOR.getOrCreate(code);
}


/**
* Set up when the server or single-player instance changes.
* NOT for API consumers to use! Use the methods provided here for safety.
*
* @since 6.0.0
*/
@ApiStatus.Internal
@Deprecated
public final class Internal {
public static IRoomApi ROOM_API;
public static IRoomDataAttachmentAccessor ROOM_DATA_ACCESSOR;
public static IPlayerHistoryApi PLAYER_HISTORY_API;
}
}
package dev.compactmods.machines.api;

import dev.compactmods.machines.api.dimension.CompactDimension;
import dev.compactmods.machines.api.dimension.MissingDimensionException;
import dev.compactmods.machines.api.room.CompactRoomGenerator;
import dev.compactmods.machines.api.room.IRoomApi;
import dev.compactmods.machines.api.room.RoomInstance;
import dev.compactmods.machines.api.room.template.RoomTemplate;
import dev.compactmods.machines.api.room.data.IRoomDataAttachmentAccessor;
import dev.compactmods.machines.api.room.history.IPlayerHistoryApi;
import dev.compactmods.machines.api.room.upgrade.RoomUpgradeDefinition;
import dev.compactmods.machines.api.util.BlockSpaceUtil;
import net.minecraft.core.Direction;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.MinecraftServer;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.phys.AABB;
import net.minecraft.world.phys.Vec2;
import net.neoforged.neoforge.attachment.IAttachmentHolder;
import net.neoforged.neoforge.registries.DeferredRegister;
import org.jetbrains.annotations.ApiStatus;

import java.util.Optional;
import java.util.UUID;

public class CompactMachines {
public final static String MOD_ID = "compactmachines";

public static String id(String path) {
return ResourceLocation.isValidPath(path) ? (MOD_ID + ":" + path) : MOD_ID + ":invalid";
}

public static ResourceLocation modRL(String path) {
return ResourceLocation.fromNamespaceAndPath(MOD_ID, path);
}

public static DeferredRegister<RoomUpgradeDefinition<?>> roomUpgradeDR(String namespace) {
return DeferredRegister.create(RoomUpgradeDefinition.REG_KEY, namespace);
}

public static IRoomApi roomApi() {
return Internal.ROOM_API;
}

public static IPlayerHistoryApi playerHistoryApi() {
return Internal.PLAYER_HISTORY_API;
}

public static Optional<RoomInstance> room(String roomCode) {
return Internal.ROOM_API.registrar().get(roomCode);
}

/**
* Registers a new room instance and generates the structure in the compact world.
*
* @param server Server to generate room on.
* @param template
* @param owner
* @return
*/
public static RoomInstance newRoom(MinecraftServer server, RoomTemplate template, UUID owner) throws MissingDimensionException {
final var instance = CompactMachines.roomApi().registrar().createNew(template, owner);
final var compactDim = CompactDimension.forServer(server);
CompactRoomGenerator.generateRoom(compactDim, instance.boundaries().outerBounds());

if (!template.structures().isEmpty()) {
for (var struct : template.structures()) {
CompactRoomGenerator.populateStructure(compactDim, struct.template(), instance.boundaries().innerBounds(), struct.placement());
}
}

final var spawnManager = CompactMachines.roomApi().spawnManager(instance.code());
template.optionalFloor().ifPresent(floorState -> {
var fixedSpawn = instance.boundaries()
.defaultSpawn()
.add(0, 1, 0);

spawnManager.setDefaultSpawn(fixedSpawn, Vec2.ZERO);

AABB floorBounds = BlockSpaceUtil.getWallBounds(instance.boundaries().innerBounds(), Direction.DOWN);
BlockSpaceUtil.blocksInside(floorBounds).forEach(floorBlockPos -> {
compactDim.setBlock(floorBlockPos, floorState, Block.UPDATE_ALL);
});
});

return instance;
}

public static boolean isValidRoomCode(String roomCode) {
return Internal.ROOM_API.roomCodeValidator().test(roomCode);
}

public static Optional<? extends IAttachmentHolder> existingRoomData(String code) {
return Internal.ROOM_DATA_ACCESSOR.get(code);
}

public static IAttachmentHolder roomData(String code) {
return Internal.ROOM_DATA_ACCESSOR.getOrCreate(code);
}


/**
* Set up when the server or single-player instance changes.
* NOT for API consumers to use! Use the methods provided here for safety.
*
* @since 6.0.0
*/
@ApiStatus.Internal
@Deprecated
public final class Internal {
public static IRoomApi ROOM_API;
public static IRoomDataAttachmentAccessor ROOM_DATA_ACCESSOR;
public static IPlayerHistoryApi PLAYER_HISTORY_API;
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,25 +1,21 @@
package dev.compactmods.machines.api.room.data;

import dev.compactmods.machines.api.CompactMachines;
import net.minecraft.server.MinecraftServer;
import net.minecraft.world.level.storage.LevelResource;

import java.nio.file.Path;
import java.util.function.Function;

public interface CMRoomDataLocations {

Function<MinecraftServer, Path> REGISTRATION_DATA = (server) -> server.getWorldPath(LevelResource.ROOT)
.resolve(CompactMachines.MOD_ID)
.resolve("data");

Function<MinecraftServer, Path> SPAWN_DATA = (server) -> server.getWorldPath(LevelResource.ROOT)
.resolve(CompactMachines.MOD_ID)
.resolve("data")
.resolve("spawn_data");

Function<MinecraftServer, Path> ROOM_DATA_ATTACHMENTS = (server) -> server.getWorldPath(LevelResource.ROOT)
.resolve(CompactMachines.MOD_ID)
.resolve("data")
.resolve("room_data");
}
package dev.compactmods.machines.api.room.data;

import dev.compactmods.machines.api.CompactMachines;
import net.minecraft.server.MinecraftServer;
import net.minecraft.world.level.storage.LevelResource;

import java.nio.file.Path;
import java.util.function.Function;

public interface CMRoomDataLocations {

Function<MinecraftServer, Path> DATA_ROOT = (server) -> server.getWorldPath(LevelResource.ROOT)
.resolve(CompactMachines.MOD_ID);

Function<MinecraftServer, Path> PLAYER_SPAWNS = (server) -> DATA_ROOT.apply(server)
.resolve("player_spawns");

Function<MinecraftServer, Path> ROOM_DATA_ATTACHMENTS = (server) -> DATA_ROOT.apply(server)
.resolve("room_data");

}
Original file line number Diff line number Diff line change
@@ -1,43 +1,42 @@
package dev.compactmods.machines.api.room.registration;

import dev.compactmods.machines.api.room.RoomInstance;
import dev.compactmods.machines.api.room.RoomTemplate;
import dev.compactmods.machines.api.room.registration.IRoomBuilder;
import net.minecraft.world.phys.AABB;

import java.util.Optional;
import java.util.UUID;
import java.util.function.Consumer;
import java.util.stream.Stream;

public interface IRoomRegistrar {

AABB getNextBoundaries(RoomTemplate template);

IRoomBuilder builder();

default RoomInstance createNew(RoomTemplate template, UUID owner) {
return createNew(template, owner, override -> {});
}

default RoomInstance createNew(RoomTemplate template, UUID owner, Consumer<IRoomBuilder> override) {
final Consumer<IRoomBuilder> preOverride = builder -> builder.defaultMachineColor(template.defaultMachineColor())
.owner(owner)
.boundaries(getNextBoundaries(template));

// Make builder, set template defaults, then allow overrides
final var b = builder();
preOverride.andThen(override).accept(b);
return b.build();
}

boolean isRegistered(String room);

Optional<RoomInstance> get(String room);

long count();

Stream<String> allRoomCodes();

Stream<RoomInstance> allRooms();
}
package dev.compactmods.machines.api.room.registration;

import dev.compactmods.machines.api.room.RoomInstance;
import dev.compactmods.machines.api.room.template.RoomTemplate;
import net.minecraft.world.phys.AABB;

import java.util.Optional;
import java.util.UUID;
import java.util.function.Consumer;
import java.util.stream.Stream;

public interface IRoomRegistrar {

AABB getNextBoundaries(RoomTemplate template);

IRoomBuilder builder();

default RoomInstance createNew(RoomTemplate template, UUID owner) {
return createNew(template, owner, override -> {});
}

default RoomInstance createNew(RoomTemplate template, UUID owner, Consumer<IRoomBuilder> override) {
final Consumer<IRoomBuilder> preOverride = builder -> builder.defaultMachineColor(template.defaultMachineColor())
.owner(owner)
.boundaries(getNextBoundaries(template));

// Make builder, set template defaults, then allow overrides
final var b = builder();
preOverride.andThen(override).accept(b);
return b.build();
}

boolean isRegistered(String room);

Optional<RoomInstance> get(String room);

long count();

Stream<String> allRoomCodes();

Stream<RoomInstance> allRooms();
}
Loading

0 comments on commit b0fc5ec

Please sign in to comment.