Goals persist as a player attachment: load on join, write after each change, unload on leave so worlds do not leak. Dying does not wipe the list. Remote dedicated servers stay RAM-only.
This commit is contained in:
@@ -4,16 +4,32 @@ import org.slf4j.Logger;
|
||||
|
||||
import com.mojang.logging.LogUtils;
|
||||
|
||||
import com.sufficient.satistaskory.infrastructure.persistence.GoalPlan;
|
||||
|
||||
import net.neoforged.bus.api.IEventBus;
|
||||
import net.neoforged.fml.ModContainer;
|
||||
import net.neoforged.fml.common.Mod;
|
||||
import net.neoforged.neoforge.attachment.AttachmentType;
|
||||
import net.neoforged.neoforge.registries.DeferredHolder;
|
||||
import net.neoforged.neoforge.registries.DeferredRegister;
|
||||
import net.neoforged.neoforge.registries.NeoForgeRegistries;
|
||||
|
||||
@Mod(Satistaskory.MODID)
|
||||
public class Satistaskory {
|
||||
public static final String MODID = "satistaskory";
|
||||
public static final Logger LOGGER = LogUtils.getLogger();
|
||||
|
||||
public static final DeferredRegister<AttachmentType<?>> ATTACHMENT_TYPES =
|
||||
DeferredRegister.create(NeoForgeRegistries.Keys.ATTACHMENT_TYPES, MODID);
|
||||
|
||||
public static final DeferredHolder<AttachmentType<?>, AttachmentType<GoalPlan>> PLAYER_GOALS =
|
||||
ATTACHMENT_TYPES.register("player_goals", () -> AttachmentType.builder(GoalPlan::empty)
|
||||
.serialize(GoalPlan.CODEC)
|
||||
.copyOnDeath()
|
||||
.build());
|
||||
|
||||
public Satistaskory(IEventBus modEventBus, ModContainer modContainer) {
|
||||
ATTACHMENT_TYPES.register(modEventBus);
|
||||
LOGGER.info("Satistaskory loaded. Goals are the domain.");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,6 +15,7 @@ import net.neoforged.bus.api.SubscribeEvent;
|
||||
import net.neoforged.fml.ModContainer;
|
||||
import net.neoforged.fml.common.EventBusSubscriber;
|
||||
import net.neoforged.fml.common.Mod;
|
||||
import net.neoforged.neoforge.client.event.ClientPlayerNetworkEvent;
|
||||
import net.neoforged.neoforge.client.event.InputEvent;
|
||||
import net.neoforged.neoforge.client.event.RegisterClientCommandsEvent;
|
||||
import net.neoforged.neoforge.client.event.RegisterGuiLayersEvent;
|
||||
@@ -39,6 +40,16 @@ public class SatistaskoryClient {
|
||||
|
||||
@EventBusSubscriber(modid = Satistaskory.MODID, value = Dist.CLIENT)
|
||||
public static class GameEvents {
|
||||
@SubscribeEvent
|
||||
public static void onJoinWorld(ClientPlayerNetworkEvent.LoggingIn event) {
|
||||
PlannerSession.onJoinWorld();
|
||||
}
|
||||
|
||||
@SubscribeEvent
|
||||
public static void onLeaveWorld(ClientPlayerNetworkEvent.LoggingOut event) {
|
||||
PlannerSession.onLeaveWorld();
|
||||
}
|
||||
|
||||
@SubscribeEvent
|
||||
public static void onClientCommands(RegisterClientCommandsEvent event) {
|
||||
ClientCommands.register(event.getDispatcher());
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
package com.sufficient.satistaskory.application;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
import com.sufficient.satistaskory.domain.Goal;
|
||||
import com.sufficient.satistaskory.domain.ItemId;
|
||||
|
||||
/**
|
||||
* Where active goals live. Domain does not know the backing store (ADR-004).
|
||||
*/
|
||||
public interface GoalStore {
|
||||
List<Goal> all();
|
||||
|
||||
Optional<Goal> find(ItemId item);
|
||||
|
||||
void put(Goal goal);
|
||||
|
||||
void remove(ItemId item);
|
||||
|
||||
void clear();
|
||||
}
|
||||
@@ -7,7 +7,7 @@ import java.util.Optional;
|
||||
import com.sufficient.satistaskory.domain.Goal;
|
||||
import com.sufficient.satistaskory.domain.ItemId;
|
||||
|
||||
public final class InMemoryGoalStore {
|
||||
public final class InMemoryGoalStore implements GoalStore {
|
||||
private final List<Goal> goals = new ArrayList<>();
|
||||
|
||||
public List<Goal> all() {
|
||||
|
||||
@@ -10,14 +10,14 @@ import com.sufficient.satistaskory.domain.ProgressCalculation;
|
||||
import com.sufficient.satistaskory.domain.ProgressSnapshot;
|
||||
|
||||
/**
|
||||
* Application facade for the first slice. Persistence is this store, in RAM.
|
||||
* Application facade for the first slice. Persistence is the store's job.
|
||||
*/
|
||||
public final class Planner {
|
||||
private final InMemoryGoalStore store;
|
||||
private final GoalStore store;
|
||||
private final ProgressCalculation progress;
|
||||
private final Supplier<InventoryState> inventory;
|
||||
|
||||
public Planner(InMemoryGoalStore store, ProgressCalculation progress, Supplier<InventoryState> inventory) {
|
||||
public Planner(GoalStore store, ProgressCalculation progress, Supplier<InventoryState> inventory) {
|
||||
this.store = Objects.requireNonNull(store, "store");
|
||||
this.progress = Objects.requireNonNull(progress, "progress");
|
||||
this.inventory = Objects.requireNonNull(inventory, "inventory");
|
||||
|
||||
+17
-3
@@ -1,21 +1,23 @@
|
||||
package com.sufficient.satistaskory.infrastructure.client;
|
||||
|
||||
import com.sufficient.satistaskory.application.InMemoryGoalStore;
|
||||
import com.sufficient.satistaskory.application.Planner;
|
||||
import com.sufficient.satistaskory.domain.InventoryState;
|
||||
import com.sufficient.satistaskory.domain.MaterialAggregation;
|
||||
import com.sufficient.satistaskory.domain.ProgressCalculation;
|
||||
import com.sufficient.satistaskory.domain.RecipeResolution;
|
||||
import com.sufficient.satistaskory.infrastructure.inventory.PlayerInventoryAdapter;
|
||||
import com.sufficient.satistaskory.infrastructure.persistence.PersistingGoalStore;
|
||||
import com.sufficient.satistaskory.infrastructure.persistence.PlayerGoalPersistence;
|
||||
import com.sufficient.satistaskory.infrastructure.recipe.VanillaRecipeProvider;
|
||||
|
||||
import net.minecraft.client.Minecraft;
|
||||
import net.minecraft.client.player.LocalPlayer;
|
||||
|
||||
/**
|
||||
* Client-side in-memory plan. Gone when the game restarts.
|
||||
* Client plan for the current world. Written to the player in this world's save.
|
||||
*/
|
||||
public final class PlannerSession {
|
||||
private static PersistingGoalStore store;
|
||||
private static Planner planner;
|
||||
|
||||
private PlannerSession() {}
|
||||
@@ -26,7 +28,8 @@ public final class PlannerSession {
|
||||
}
|
||||
RecipeResolution resolution = new RecipeResolution(new VanillaRecipeProvider());
|
||||
ProgressCalculation progress = new ProgressCalculation(resolution, new MaterialAggregation());
|
||||
planner = new Planner(new InMemoryGoalStore(), progress, PlannerSession::currentInventory);
|
||||
store = new PersistingGoalStore();
|
||||
planner = new Planner(store, progress, PlannerSession::currentInventory);
|
||||
}
|
||||
|
||||
public static Planner get() {
|
||||
@@ -36,6 +39,17 @@ public final class PlannerSession {
|
||||
return planner;
|
||||
}
|
||||
|
||||
public static void onJoinWorld() {
|
||||
bootstrap();
|
||||
store.loadFromWorld(PlayerGoalPersistence.load());
|
||||
}
|
||||
|
||||
public static void onLeaveWorld() {
|
||||
if (store != null) {
|
||||
store.unload();
|
||||
}
|
||||
}
|
||||
|
||||
private static InventoryState currentInventory() {
|
||||
LocalPlayer player = Minecraft.getInstance().player;
|
||||
if (player == null) {
|
||||
|
||||
@@ -0,0 +1,59 @@
|
||||
package com.sufficient.satistaskory.infrastructure.persistence;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import com.mojang.serialization.Codec;
|
||||
import com.mojang.serialization.codecs.RecordCodecBuilder;
|
||||
|
||||
import com.sufficient.satistaskory.domain.Goal;
|
||||
import com.sufficient.satistaskory.domain.ItemId;
|
||||
|
||||
/**
|
||||
* NBT shape for the player's goal list. Not a domain type.
|
||||
*/
|
||||
public record GoalPlan(List<GoalEntry> goals) {
|
||||
public static final GoalPlan EMPTY = new GoalPlan(List.of());
|
||||
|
||||
public static final Codec<GoalPlan> CODEC = RecordCodecBuilder.create(instance -> instance.group(
|
||||
GoalEntry.CODEC.listOf().fieldOf("goals").forGetter(GoalPlan::goals)
|
||||
).apply(instance, GoalPlan::new));
|
||||
|
||||
public GoalPlan {
|
||||
goals = List.copyOf(goals);
|
||||
}
|
||||
|
||||
public static GoalPlan empty() {
|
||||
return EMPTY;
|
||||
}
|
||||
|
||||
public static GoalPlan fromGoals(List<Goal> goals) {
|
||||
List<GoalEntry> entries = new ArrayList<>(goals.size());
|
||||
for (Goal goal : goals) {
|
||||
entries.add(new GoalEntry(goal.targetItem().toString(), goal.quantity()));
|
||||
}
|
||||
return new GoalPlan(entries);
|
||||
}
|
||||
|
||||
public List<Goal> toGoals() {
|
||||
List<Goal> result = new ArrayList<>();
|
||||
for (GoalEntry entry : goals) {
|
||||
if (entry.quantity() < 1) {
|
||||
continue;
|
||||
}
|
||||
try {
|
||||
result.add(new Goal(ItemId.parse(entry.item()), entry.quantity()));
|
||||
} catch (IllegalArgumentException ignored) {
|
||||
// skip a corrupt row; do not fail the world
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public record GoalEntry(String item, int quantity) {
|
||||
public static final Codec<GoalEntry> CODEC = RecordCodecBuilder.create(instance -> instance.group(
|
||||
Codec.STRING.fieldOf("item").forGetter(GoalEntry::item),
|
||||
Codec.INT.fieldOf("quantity").forGetter(GoalEntry::quantity)
|
||||
).apply(instance, GoalEntry::new));
|
||||
}
|
||||
}
|
||||
+67
@@ -0,0 +1,67 @@
|
||||
package com.sufficient.satistaskory.infrastructure.persistence;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
import com.sufficient.satistaskory.application.GoalStore;
|
||||
import com.sufficient.satistaskory.application.InMemoryGoalStore;
|
||||
import com.sufficient.satistaskory.domain.Goal;
|
||||
import com.sufficient.satistaskory.domain.ItemId;
|
||||
|
||||
/**
|
||||
* RAM store that writes the player's world save after each change.
|
||||
* Load and leave-world must not write; otherwise logout would wipe the save.
|
||||
*/
|
||||
public final class PersistingGoalStore implements GoalStore {
|
||||
private final InMemoryGoalStore inner = new InMemoryGoalStore();
|
||||
private boolean persist = true;
|
||||
|
||||
@Override
|
||||
public List<Goal> all() {
|
||||
return inner.all();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<Goal> find(ItemId item) {
|
||||
return inner.find(item);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void put(Goal goal) {
|
||||
inner.put(goal);
|
||||
persist();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void remove(ItemId item) {
|
||||
inner.remove(item);
|
||||
persist();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clear() {
|
||||
inner.clear();
|
||||
persist();
|
||||
}
|
||||
|
||||
public void loadFromWorld(List<Goal> goals) {
|
||||
persist = false;
|
||||
inner.clear();
|
||||
for (Goal goal : goals) {
|
||||
inner.put(goal);
|
||||
}
|
||||
persist = true;
|
||||
}
|
||||
|
||||
public void unload() {
|
||||
persist = false;
|
||||
inner.clear();
|
||||
persist = true;
|
||||
}
|
||||
|
||||
private void persist() {
|
||||
if (persist) {
|
||||
PlayerGoalPersistence.save(inner.all());
|
||||
}
|
||||
}
|
||||
}
|
||||
+49
@@ -0,0 +1,49 @@
|
||||
package com.sufficient.satistaskory.infrastructure.persistence;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
import com.sufficient.satistaskory.Satistaskory;
|
||||
import com.sufficient.satistaskory.domain.Goal;
|
||||
|
||||
import net.minecraft.client.Minecraft;
|
||||
import net.minecraft.client.server.IntegratedServer;
|
||||
import net.minecraft.server.level.ServerPlayer;
|
||||
|
||||
/**
|
||||
* Player attachment in this world's save. Singleplayer and LAN host.
|
||||
* A remote server has no integrated save; the session stays RAM-only there.
|
||||
*/
|
||||
public final class PlayerGoalPersistence {
|
||||
private PlayerGoalPersistence() {}
|
||||
|
||||
public static List<Goal> load() {
|
||||
Minecraft minecraft = Minecraft.getInstance();
|
||||
IntegratedServer server = minecraft.getSingleplayerServer();
|
||||
if (server == null || minecraft.player == null) {
|
||||
return List.of();
|
||||
}
|
||||
ServerPlayer player = server.getPlayerList().getPlayer(minecraft.player.getUUID());
|
||||
if (player == null) {
|
||||
return List.of();
|
||||
}
|
||||
return player.getData(Satistaskory.PLAYER_GOALS).toGoals();
|
||||
}
|
||||
|
||||
public static void save(List<Goal> goals) {
|
||||
Minecraft minecraft = Minecraft.getInstance();
|
||||
IntegratedServer server = minecraft.getSingleplayerServer();
|
||||
if (server == null || minecraft.player == null) {
|
||||
return;
|
||||
}
|
||||
UUID id = minecraft.player.getUUID();
|
||||
GoalPlan plan = GoalPlan.fromGoals(goals);
|
||||
server.execute(() -> {
|
||||
ServerPlayer player = server.getPlayerList().getPlayer(id);
|
||||
if (player == null) {
|
||||
return;
|
||||
}
|
||||
player.setData(Satistaskory.PLAYER_GOALS, plan);
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user