Skip to content

Commit

Permalink
Several API and data saving cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
robotgryphon committed Jun 14, 2024
1 parent 21174d2 commit 78f0974
Show file tree
Hide file tree
Showing 88 changed files with 624 additions and 702 deletions.
Original file line number Diff line number Diff line change
@@ -1,52 +1,45 @@
package dev.compactmods.machines.api.room;
package dev.compactmods.machines.api;

import dev.compactmods.machines.api.room.owner.IRoomOwners;
import dev.compactmods.machines.api.room.spatial.IRoomChunkManager;
import dev.compactmods.machines.api.room.spatial.IRoomChunks;
import dev.compactmods.machines.api.room.spawn.IRoomSpawnManager;
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.core.Registry;
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;
import java.util.function.Predicate;

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

/**
* Set up when the server or single-player instance changes.
* NOT for API consumers to use! Use the methods provided here for safety.
*
* @since 3.0.0
*/
@ApiStatus.Internal
public static IRoomApi INSTANCE;
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 Predicate<String> roomCodeValidator() {
return INSTANCE.roomCodeValidator();
public static IRoomApi roomApi() {
return Internal.ROOM_API;
}

public static boolean isValidRoomCode(String roomCode) {
return INSTANCE.roomCodeValidator().test(roomCode);
public static IPlayerHistoryApi playerHistoryApi() {
return Internal.PLAYER_HISTORY_API;
}

/**
* Fetches a room instance from the registrar.
*
* @param roomCode The room identifier.
* @since 3.0.0
*/
public static Optional<RoomInstance> room(String roomCode) {
return INSTANCE.registrar().get(roomCode);
return Internal.ROOM_API.registrar().get(roomCode);
}

/**
Expand All @@ -58,7 +51,7 @@ public static Optional<RoomInstance> room(String roomCode) {
* @return
*/
public static RoomInstance newRoom(MinecraftServer server, RoomTemplate template, UUID owner) throws MissingDimensionException {
final var instance = INSTANCE.registrar().createNew(template, owner);
final var instance = CompactMachines.roomApi().registrar().createNew(template, owner);
final var compactDim = CompactDimension.forServer(server);
CompactRoomGenerator.generateRoom(compactDim, instance.boundaries().outerBounds());

Expand All @@ -71,24 +64,30 @@ public static RoomInstance newRoom(MinecraftServer server, RoomTemplate template
return instance;
}

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

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

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

public static IRoomChunks chunks(String roomCode) {
return INSTANCE.chunks(roomCode);
}

public static Registry<RoomTemplate> getTemplates(MinecraftServer server) {
final var regAccess = server.registryAccess();
return regAccess.registryOrThrow(RoomTemplate.REGISTRY_KEY);
/**
* 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
Expand Up @@ -21,9 +21,9 @@ public interface Translations {
.withStyle(ChatFormatting.ITALIC);

interface IDs {
String TELEPORT_OUT_OF_BOUNDS = Util.makeDescriptionId("messages", CompactMachinesApi.modRL("teleport_oob"));
String HOW_DID_YOU_GET_HERE = Util.makeDescriptionId("messages", CompactMachinesApi.modRL("how_did_you_get_here"));
String HINT_HOLD_SHIFT = Util.makeDescriptionId("messages", CompactMachinesApi.modRL("hint.hold_shift"));
String UNBREAKABLE_BLOCK = Util.makeDescriptionId("messages", CompactMachinesApi.modRL("solid_wall"));
String TELEPORT_OUT_OF_BOUNDS = Util.makeDescriptionId("messages", CompactMachines.modRL("teleport_oob"));
String HOW_DID_YOU_GET_HERE = Util.makeDescriptionId("messages", CompactMachines.modRL("how_did_you_get_here"));
String HINT_HOLD_SHIFT = Util.makeDescriptionId("messages", CompactMachines.modRL("hint.hold_shift"));
String UNBREAKABLE_BLOCK = Util.makeDescriptionId("messages", CompactMachines.modRL("solid_wall"));
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package dev.compactmods.machines.api.advancement;

import dev.compactmods.machines.api.CompactMachinesApi;
import dev.compactmods.machines.api.CompactMachines;
import net.minecraft.resources.ResourceLocation;

/**
Expand All @@ -11,67 +11,67 @@ public interface Advancements {
* Granted when the player is teleported out by leaving room boundaries, or
* when in an invalid state (such as trying to leave a machine room with no entry history)
*/
ResourceLocation HOW_DID_YOU_GET_HERE = CompactMachinesApi.modRL("how_did_you_get_here");
ResourceLocation HOW_DID_YOU_GET_HERE = CompactMachines.modRL("how_did_you_get_here");

/**
* Root advancement. Required for vanilla's tree design.
*/
ResourceLocation ROOT = CompactMachinesApi.modRL("root");
ResourceLocation ROOT = CompactMachines.modRL("root");

/**
* Granted when a player first crafts machine items.
*/
ResourceLocation FOUNDATIONS = CompactMachinesApi.modRL("foundations");
ResourceLocation FOUNDATIONS = CompactMachines.modRL("foundations");

/**
* Granted on first pickup of a PSD item.
*/
ResourceLocation GOT_SHRINKING_DEVICE = CompactMachinesApi.modRL("got_shrinking_device");
ResourceLocation GOT_SHRINKING_DEVICE = CompactMachines.modRL("got_shrinking_device");

/**
* Granted if a player tries to enter a machine room they're currently in.
*/
ResourceLocation RECURSIVE_ROOMS = CompactMachinesApi.modRL("recursion");
ResourceLocation RECURSIVE_ROOMS = CompactMachines.modRL("recursion");

/**
* Was granted when players gained a tiny machine item. TBR in 1.20 once tiny machines are no longer
* part of the mod.
*/
@Deprecated(since = "2.0.0", forRemoval = true)
ResourceLocation CLAIMED_TINY_MACHINE = CompactMachinesApi.modRL("claimed_machine_tiny");
ResourceLocation CLAIMED_TINY_MACHINE = CompactMachines.modRL("claimed_machine_tiny");

/**
* Was granted when players gained a small machine item. TBR in 1.20 once small machines are no longer
* part of the mod.
*/
@Deprecated(since = "2.0.0", forRemoval = true)
ResourceLocation CLAIMED_SMALL_MACHINE = CompactMachinesApi.modRL("claimed_machine_small");
ResourceLocation CLAIMED_SMALL_MACHINE = CompactMachines.modRL("claimed_machine_small");

/**
* Was granted when players gained a normal machine item. TBR in 1.20 once normal machines are no longer
* part of the mod.
*/
@Deprecated(since = "2.0.0", forRemoval = true)
ResourceLocation CLAIMED_NORMAL_MACHINE = CompactMachinesApi.modRL("claimed_machine_normal");
ResourceLocation CLAIMED_NORMAL_MACHINE = CompactMachines.modRL("claimed_machine_normal");

/**
* Was granted when players gained a large machine item. TBR in 1.20 once large machines are no longer
* part of the mod.
*/
@Deprecated(since = "2.0.0", forRemoval = true)
ResourceLocation CLAIMED_LARGE_MACHINE = CompactMachinesApi.modRL("claimed_machine_large");
ResourceLocation CLAIMED_LARGE_MACHINE = CompactMachines.modRL("claimed_machine_large");

/**
* Was granted when players gained a giant machine item. TBR in 1.20 once giant machines are no longer
* part of the mod.
*/
@Deprecated(since = "2.0.0", forRemoval = true)
ResourceLocation CLAIMED_GIANT_MACHINE = CompactMachinesApi.modRL("claimed_machine_giant");
ResourceLocation CLAIMED_GIANT_MACHINE = CompactMachines.modRL("claimed_machine_giant");

/**
* Was granted when players gained a maximum-size machine item. TBR in 1.20 once maximum-size machines are no longer
* part of the mod.
*/
@Deprecated(since = "2.0.0", forRemoval = true)
ResourceLocation CLAIMED_MAX_MACHINE = CompactMachinesApi.modRL("claimed_machine_max");
ResourceLocation CLAIMED_MAX_MACHINE = CompactMachines.modRL("claimed_machine_max");
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package dev.compactmods.machines.api.command;

import dev.compactmods.machines.api.CompactMachinesApi;
import dev.compactmods.machines.api.CompactMachines;
import net.minecraft.Util;
import net.minecraft.network.chat.Component;
import net.minecraft.world.entity.player.Player;
Expand All @@ -16,15 +16,15 @@ public interface CommandTranslations {
.translatableWithFallback(IDs.MACHINE_GIVEN, "Created a new machine item and gave it to %s.", player.getDisplayName());

interface IDs {
String CANNOT_GIVE_MACHINE = Util.makeDescriptionId("commands.machines", CompactMachinesApi.modRL("cannot_give_machine_item"));
String CANNOT_GIVE_MACHINE = Util.makeDescriptionId("commands.machines", CompactMachines.modRL("cannot_give_machine_item"));

String MACHINE_GIVEN = Util.makeDescriptionId("commands.machines", CompactMachinesApi.modRL("machine_given_successfully"));
String MACHINE_GIVEN = Util.makeDescriptionId("commands.machines", CompactMachines.modRL("machine_given_successfully"));

/**
* Used for displaying the number of registered rooms via summary commands.
*/
String ROOM_COUNT = Util.makeDescriptionId("commands.rooms", CompactMachinesApi.modRL("room_reg_count"));
String ROOM_COUNT = Util.makeDescriptionId("commands.rooms", CompactMachines.modRL("room_reg_count"));

String SPAWN_CHANGED_SUCCESSFULLY = Util.makeDescriptionId("commands.rooms", CompactMachinesApi.modRL("spawn_changed_successfully"));
String SPAWN_CHANGED_SUCCESSFULLY = Util.makeDescriptionId("commands.rooms", CompactMachines.modRL("spawn_changed_successfully"));
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package dev.compactmods.machines.api.dimension;

import dev.compactmods.machines.api.CompactMachinesApi;
import dev.compactmods.machines.api.CompactMachines;
import net.minecraft.core.HolderLookup;
import net.minecraft.core.registries.Registries;
import net.minecraft.resources.ResourceKey;
Expand All @@ -18,10 +18,10 @@

public abstract class CompactDimension {
public static final ResourceKey<Level> LEVEL_KEY = ResourceKey
.create(Registries.DIMENSION, CompactMachinesApi.modRL("compact_world"));
.create(Registries.DIMENSION, CompactMachines.modRL("compact_world"));

public static final ResourceKey<DimensionType> DIM_TYPE_KEY = ResourceKey
.create(Registries.DIMENSION_TYPE, CompactMachinesApi.modRL("compact_world"));
.create(Registries.DIMENSION_TYPE, CompactMachines.modRL("compact_world"));

private CompactDimension() {}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package dev.compactmods.machines.api.machine;

import dev.compactmods.machines.api.CompactMachinesApi;
import dev.compactmods.machines.api.CompactMachines;
import dev.compactmods.machines.api.util.KeyHelper;
import net.minecraft.resources.ResourceKey;
import net.minecraft.resources.ResourceLocation;
Expand All @@ -10,15 +10,15 @@

public interface MachineConstants {

ResourceLocation BOUND_MACHINE_BLOCK_ID = CompactMachinesApi.modRL( "machine");
ResourceLocation BOUND_MACHINE_ENTITY = CompactMachinesApi.modRL( "machine");
ResourceLocation BOUND_MACHINE_BLOCK_ID = CompactMachines.modRL( "machine");
ResourceLocation BOUND_MACHINE_ENTITY = CompactMachines.modRL( "machine");
ResourceKey<Block> BOUND_MACHINE_BLOCK_RESKEY = KeyHelper.blockResKey("machine");
ResourceKey<Item> BOUND_MACHINE_ITEM_RESKEY = KeyHelper.itemResKey("machine");


ResourceLocation UNBOUND_MACHINE_BLOCK_ID = CompactMachinesApi.modRL( "new_machine");
ResourceLocation UNBOUND_MACHINE_ITEM_ID = CompactMachinesApi.modRL( "new_machine");
ResourceLocation UNBOUND_MACHINE_ENTITY = CompactMachinesApi.modRL( "new_machine");
ResourceLocation UNBOUND_MACHINE_BLOCK_ID = CompactMachines.modRL( "new_machine");
ResourceLocation UNBOUND_MACHINE_ITEM_ID = CompactMachines.modRL( "new_machine");
ResourceLocation UNBOUND_MACHINE_ENTITY = CompactMachines.modRL( "new_machine");

ResourceKey<Block> UNBOUND_MACHINE_BLOCK_RESKEY = KeyHelper.blockResKey("new_machine");
ResourceKey<Item> UNBOUND_MACHINE_ITEM_RESKEY = KeyHelper.itemResKey("new_machine");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package dev.compactmods.machines.api.machine;

import dev.compactmods.machines.api.CompactMachinesApi;
import dev.compactmods.machines.api.CompactMachines;
import net.minecraft.ChatFormatting;
import net.minecraft.Util;
import net.minecraft.core.BlockPos;
Expand All @@ -16,9 +16,9 @@ public interface MachineTranslations {
.withStyle(ChatFormatting.YELLOW);

interface IDs {
String OWNER = Util.makeDescriptionId("machine", CompactMachinesApi.modRL("machine.owner"));
String SIZE = Util.makeDescriptionId("machine", CompactMachinesApi.modRL("machine.size"));
String BOUND_TO = Util.makeDescriptionId("machine", CompactMachinesApi.modRL("machine.bound_to"));
String NEW_MACHINE = Util.makeDescriptionId("machine", CompactMachinesApi.modRL("new_machine"));
String OWNER = Util.makeDescriptionId("machine", CompactMachines.modRL("machine.owner"));
String SIZE = Util.makeDescriptionId("machine", CompactMachines.modRL("machine.size"));
String BOUND_TO = Util.makeDescriptionId("machine", CompactMachines.modRL("machine.bound_to"));
String NEW_MACHINE = Util.makeDescriptionId("machine", CompactMachines.modRL("new_machine"));
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package dev.compactmods.machines.api.room;

import dev.compactmods.machines.api.room.registration.IRoomRegistrar;
import dev.compactmods.machines.api.room.spatial.IRoomChunkManager;
import dev.compactmods.machines.api.room.spatial.IRoomChunks;
import dev.compactmods.machines.api.room.spawn.IRoomSpawnManager;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.mojang.serialization.Codec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import dev.compactmods.machines.api.CompactMachinesApi;
import dev.compactmods.machines.api.CompactMachines;
import dev.compactmods.machines.api.machine.MachineColor;
import dev.compactmods.machines.api.machine.MachineTranslations;
import net.minecraft.ChatFormatting;
Expand Down Expand Up @@ -34,9 +34,9 @@
public record RoomTemplate(RoomDimensions internalDimensions, MachineColor defaultMachineColor, List<RoomStructureInfo> structures)
implements TooltipProvider {

public static final ResourceKey<Registry<RoomTemplate>> REGISTRY_KEY = ResourceKey.createRegistryKey(CompactMachinesApi.modRL("room_templates"));
public static final ResourceKey<Registry<RoomTemplate>> REGISTRY_KEY = ResourceKey.createRegistryKey(CompactMachines.modRL("room_templates"));

public static final ResourceLocation NO_TEMPLATE = CompactMachinesApi.modRL("empty");
public static final ResourceLocation NO_TEMPLATE = CompactMachines.modRL("empty");

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

Expand Down
Loading

0 comments on commit 78f0974

Please sign in to comment.