Skip to content

Commit

Permalink
[WIP] Overhauling data management
Browse files Browse the repository at this point in the history
  • Loading branch information
robotgryphon committed Jun 9, 2024
1 parent 15e36a6 commit 3d81108
Show file tree
Hide file tree
Showing 61 changed files with 956 additions and 564 deletions.
6 changes: 6 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,10 @@ plugins {

// alias(neoforged.plugins.common)
alias(neoforged.plugins.userdev).apply(false)
}

subprojects {
repositories {
mavenLocal()
}
}
4 changes: 2 additions & 2 deletions neoforge-main/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ runs {

if(!System.getenv().containsKey("CI")) {
// JetBrains Runtime Hotswap
jvmArgument("-XX:+AllowEnhancedClassRedefinition")
// jvmArgument("-XX:+AllowEnhancedClassRedefinition")
}

modSource(sourceSets.main.get())
Expand Down Expand Up @@ -153,7 +153,7 @@ dependencies {
// Core Projects and Libraries
this {
implementation(neoforged.neoforge)

testImplementation(neoforged.testframework)
implementation(libraries.jnanoid)
jarJar(libraries.jnanoid)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package dev.compactmods.machines;

import dev.compactmods.machines.api.room.RoomApi;
import dev.compactmods.machines.api.room.RoomTemplate;
import dev.compactmods.machines.api.CompactMachinesApi;
import dev.compactmods.machines.api.room.upgrade.RoomUpgradeType;
import dev.compactmods.machines.shrinking.Shrinking;
import net.minecraft.commands.synchronization.ArgumentTypeInfo;
import net.minecraft.core.registries.BuiltInRegistries;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package dev.compactmods.machines;

import dev.compactmods.machines.api.CompactMachinesApi;
import dev.compactmods.machines.api.dimension.CompactDimension;
import dev.compactmods.machines.client.CompactMachinesClient;
import dev.compactmods.machines.command.Commands;
import dev.compactmods.machines.client.config.ClientConfig;
Expand All @@ -9,6 +10,7 @@
import dev.compactmods.machines.config.CommonConfig;
import dev.compactmods.machines.config.ServerConfig;
import dev.compactmods.machines.data.functions.LootFunctions;
import dev.compactmods.machines.data.room.RoomAttachmentDataManager;
import dev.compactmods.machines.dimension.Dimension;
import dev.compactmods.machines.dimension.WorldBorderFixer;
import dev.compactmods.machines.machine.Machines;
Expand All @@ -19,12 +21,15 @@
import dev.compactmods.machines.shrinking.Shrinking;
import dev.compactmods.machines.villager.Villagers;
import net.minecraft.util.FastColor;
import net.minecraft.world.level.Level;
import net.neoforged.bus.api.IEventBus;
import net.neoforged.fml.ModContainer;
import net.neoforged.fml.common.Mod;
import net.neoforged.fml.config.ModConfig;
import net.neoforged.fml.loading.FMLEnvironment;
import net.neoforged.neoforge.common.NeoForge;
import net.neoforged.neoforge.event.level.LevelEvent;
import net.neoforged.neoforge.event.server.ServerStoppingEvent;

@Mod(CompactMachinesApi.MOD_ID)
public class CompactMachines {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ public class Suggestors {
public static final SuggestionProvider<CommandSourceStack> ROOM_TEMPLATES = (ctx, builder) ->
SharedSuggestionProvider.suggestResource(getRegistryValues(ctx, RoomTemplate.REGISTRY_KEY), builder);

public static final SuggestionProvider<CommandSourceStack> OWNED_ROOM_CODES = (ctx, builder) -> {
final var owner = ctx.getSource().getPlayerOrException();

final var codes = RoomApi.owners()
.findByOwner(owner.getUUID())
.toList();

return SharedSuggestionProvider.suggest(codes, builder);
};
// public static final SuggestionProvider<CommandSourceStack> OWNED_ROOM_CODES = (ctx, builder) -> {
// final var owner = ctx.getSource().getPlayerOrException();
//
// final var codes = RoomApi.owners()
// .findByOwner(owner.getUUID())
// .toList();
//
// return SharedSuggestionProvider.suggest(codes, builder);
// };

public static final SuggestionProvider<CommandSourceStack> ROOM_CODES = (ctx, builder) -> {
final var codes = RoomApi.registrar()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import dev.compactmods.machines.api.dimension.MissingDimensionException;
import dev.compactmods.machines.player.PlayerEntryPointHistory;
import dev.compactmods.machines.api.room.history.PlayerHistoryApi;
import dev.compactmods.machines.util.PlayerUtil;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.commands.Commands;
Expand Down Expand Up @@ -38,12 +38,8 @@ private static int execExecutingPlayer(CommandContext<CommandSourceStack> ctx) t
final MinecraftServer server = ctx.getSource().getServer();

server.submitAsync(() -> {
try {
PlayerEntryPointHistory.forServer(server).clearHistory(player);
} catch (MissingDimensionException e) {
throw new RuntimeException(e);
}
});
PlayerHistoryApi.historyManager().clearHistory(player);
});

PlayerUtil.teleportPlayerToRespawnOrOverworld(ctx.getSource().getServer(), player);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,14 +141,14 @@ public static int findByOwner(CommandContext<CommandSourceStack> ctx) throws Com
final var owner = EntityArgument.getPlayer(ctx, "owner");
final var source = ctx.getSource();

final var owned = RoomApi.owners().findByOwner(owner.getUUID()).toList();

// TODO Localization
if (owned.isEmpty()) {
source.sendSuccess(() -> Component.literal("No rooms found."), false);
} else {
owned.forEach(roomCode -> source.sendSuccess(() -> Component.literal("Room: " + roomCode), false));
}
// final var owned = RoomApi.owners().findByOwner(owner.getUUID()).toList();
//
// // TODO Localization
// if (owned.isEmpty()) {
// source.sendSuccess(() -> Component.literal("No rooms found."), false);
// } else {
// owned.forEach(roomCode -> source.sendSuccess(() -> Component.literal("Room: " + roomCode), false));
// }


return 0;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package dev.compactmods.machines.data;

import dev.compactmods.machines.LoggingUtil;
import net.minecraft.core.HolderLookup;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.NbtIo;
import net.minecraft.server.MinecraftServer;
import net.neoforged.neoforge.attachment.AttachmentHolder;

import java.io.IOException;
import java.util.HashMap;
import java.util.function.BiFunction;

public class CMDataAttachmentFileManager<Key, T extends AttachmentHolder & CMDataFile> {

protected final MinecraftServer server;
private final BiFunction<MinecraftServer, Key, T> creator;
private final HashMap<Key, T> cache;

protected CMDataAttachmentFileManager(MinecraftServer server, BiFunction<MinecraftServer, Key, T> creator) {
this.server = server;
this.creator = creator;
this.cache = new HashMap<>();
}

public T data(Key key) {
return cache.computeIfAbsent(key, k -> creator.apply(server, key));
}

public void save(HolderLookup.Provider provider) {
cache.forEach((key, data) -> {
CompoundTag fullTag = new CompoundTag();
fullTag.putString("version", "1.0");

if(data.hasAttachments()) {
var ad = data.serializeAttachments(provider);
if(ad != null)
fullTag.put("attachments", ad);
}

try {
var path = data.getDataLocation(server).resolve(key.toString() + ".dat");
NbtIo.writeCompressed(fullTag, path);
} catch (IOException e) {
LoggingUtil.modLog().error("Failed to write data: " + e.getMessage(), e);
}
});
}

public boolean hasData(String code) {
return cache.containsKey(code);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package dev.compactmods.machines.data;

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

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;

public interface CMDataFile {

default String getDataVersion() {
return "1.0.0";
}

default Path getDataLocation(MinecraftServer server) {
return server.getWorldPath(LevelResource.ROOT)
.resolve(CompactMachinesApi.MOD_ID)
.resolve("data");
}

static void ensureDirExists(Path dir) {
if(!Files.exists(dir)) {
try {
Files.createDirectories(dir);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package dev.compactmods.machines.data;

import com.mojang.serialization.Codec;

public interface CodecHolder<T> {

Codec<T> codec();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package dev.compactmods.machines.data;

import dev.compactmods.machines.data.manager.CMKeyedDataFileManager;
import dev.compactmods.machines.data.manager.CMSingletonDataFileManager;
import dev.compactmods.machines.data.manager.IDataFileManager;
import dev.compactmods.machines.data.manager.IKeyedDataFileManager;
import dev.compactmods.machines.data.room.RoomDataAttachments;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.MinecraftServer;

import java.util.function.BiFunction;
import java.util.function.Function;

public class DataManagerBuilder {

public static <Key, T extends CMDataFile & CodecHolder<T>> DataManagerBuilder.Keyed<Key, T> keyed(MinecraftServer server, ResourceLocation id) {
return new Keyed<>(server, id);
}

public static <T extends CMDataFile & CodecHolder<T>> DataManagerBuilder.Singleton<T> singleton(MinecraftServer server, ResourceLocation id) {
return new Singleton<>(server, id);
}

public static class Keyed<Key, T extends CMDataFile & CodecHolder<T>> {

private final MinecraftServer server;
private final ResourceLocation id;
private BiFunction<MinecraftServer, Key, T> factory;

public Keyed(MinecraftServer server, ResourceLocation id) {
this.server = server;
this.id = id;
}

public IKeyedDataFileManager<Key, T> build() {
return new CMKeyedDataFileManager<>(server, factory);
}
}

public static class Singleton<T extends CMDataFile & CodecHolder<T>> {

private final MinecraftServer server;
private final ResourceLocation id;
private Function<MinecraftServer, T> factory;

public Singleton(MinecraftServer server, ResourceLocation id) {
this.server = server;
this.id = id;
}

public IDataFileManager<T> build() {
return new CMSingletonDataFileManager<>(server, "", factory);
}
}
}
Loading

0 comments on commit 3d81108

Please sign in to comment.