Keep the plan on this client; the host does not need the mod.
Build / build (push) Failing after 28s
Build / build (push) Failing after 28s
Replace player attachments with client-owned files. Singleplayer writes satistaskory.dat next to the world. Remote writes satistaskory/remote/<address>.dat. displayTest IGNORE_ALL_VERSION. No packets, no server jar.
This commit is contained in:
@@ -4,32 +4,17 @@ import org.slf4j.Logger;
|
||||
|
||||
import com.mojang.logging.LogUtils;
|
||||
|
||||
import com.sufficient.satistaskory.infrastructure.persistence.GoalPlan;
|
||||
|
||||
import net.neoforged.api.distmarker.Dist;
|
||||
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)
|
||||
@Mod(value = Satistaskory.MODID, dist = Dist.CLIENT)
|
||||
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.");
|
||||
LOGGER.info("Satistaskory loaded. Client-only. Goals are the domain.");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,14 +7,14 @@ 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.persistence.ClientGoalPersistence;
|
||||
import com.sufficient.satistaskory.infrastructure.recipe.VanillaRecipeProvider;
|
||||
|
||||
import net.minecraft.client.Minecraft;
|
||||
import net.minecraft.client.player.LocalPlayer;
|
||||
|
||||
/**
|
||||
* Client plan for the current world. Written to the player in this world's save.
|
||||
* Client plan for the current world. Written to a file this client owns.
|
||||
*/
|
||||
public final class PlannerSession {
|
||||
private static PersistingGoalStore store;
|
||||
@@ -41,7 +41,7 @@ public final class PlannerSession {
|
||||
|
||||
public static void onJoinWorld() {
|
||||
bootstrap();
|
||||
store.loadFromWorld(PlayerGoalPersistence.load());
|
||||
store.loadFromWorld(ClientGoalPersistence.load());
|
||||
}
|
||||
|
||||
public static void onLeaveWorld() {
|
||||
|
||||
+92
@@ -0,0 +1,92 @@
|
||||
package com.sufficient.satistaskory.infrastructure.persistence;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.List;
|
||||
|
||||
import com.mojang.serialization.DataResult;
|
||||
|
||||
import com.sufficient.satistaskory.Satistaskory;
|
||||
import com.sufficient.satistaskory.domain.Goal;
|
||||
|
||||
import net.minecraft.client.Minecraft;
|
||||
import net.minecraft.client.multiplayer.ServerData;
|
||||
import net.minecraft.client.server.IntegratedServer;
|
||||
import net.minecraft.nbt.CompoundTag;
|
||||
import net.minecraft.nbt.NbtAccounter;
|
||||
import net.minecraft.nbt.NbtIo;
|
||||
import net.minecraft.nbt.NbtOps;
|
||||
import net.minecraft.nbt.Tag;
|
||||
import net.minecraft.world.level.storage.LevelResource;
|
||||
|
||||
/**
|
||||
* Client-owned files. The server does not need this mod.
|
||||
* Singleplayer: {@code <world>/satistaskory.dat}. Remote: {@code satistaskory/remote/<address>.dat}.
|
||||
*/
|
||||
public final class ClientGoalPersistence {
|
||||
private ClientGoalPersistence() {}
|
||||
|
||||
public static List<Goal> load() {
|
||||
Path file = currentFile();
|
||||
if (file == null || !Files.isRegularFile(file)) {
|
||||
return List.of();
|
||||
}
|
||||
try {
|
||||
CompoundTag root = NbtIo.readCompressed(file, NbtAccounter.unlimitedHeap());
|
||||
Tag planTag = root.get("Plan");
|
||||
if (planTag == null) {
|
||||
return List.of();
|
||||
}
|
||||
DataResult<GoalPlan> parsed = GoalPlan.CODEC.parse(NbtOps.INSTANCE, planTag);
|
||||
return parsed.result().map(GoalPlan::toGoals).orElseGet(() -> {
|
||||
Satistaskory.LOGGER.warn("Ignoring unreadable Satistaskory plan at {}", file);
|
||||
return List.of();
|
||||
});
|
||||
} catch (IOException e) {
|
||||
Satistaskory.LOGGER.warn("Could not read Satistaskory plan at {}", file, e);
|
||||
return List.of();
|
||||
}
|
||||
}
|
||||
|
||||
public static void save(List<Goal> goals) {
|
||||
Path file = currentFile();
|
||||
if (file == null) {
|
||||
return;
|
||||
}
|
||||
DataResult<Tag> encoded = GoalPlan.CODEC.encodeStart(NbtOps.INSTANCE, GoalPlan.fromGoals(goals));
|
||||
Tag planTag = encoded.result().orElse(null);
|
||||
if (planTag == null) {
|
||||
Satistaskory.LOGGER.warn("Could not encode Satistaskory plan");
|
||||
return;
|
||||
}
|
||||
CompoundTag root = new CompoundTag();
|
||||
root.putInt("DataVersion", 1);
|
||||
root.put("Plan", planTag);
|
||||
try {
|
||||
Path parent = file.getParent();
|
||||
if (parent != null) {
|
||||
Files.createDirectories(parent);
|
||||
}
|
||||
NbtIo.writeCompressed(root, file);
|
||||
} catch (IOException e) {
|
||||
Satistaskory.LOGGER.warn("Could not write Satistaskory plan at {}", file, e);
|
||||
}
|
||||
}
|
||||
|
||||
private static Path currentFile() {
|
||||
Minecraft minecraft = Minecraft.getInstance();
|
||||
IntegratedServer integrated = minecraft.getSingleplayerServer();
|
||||
if (integrated != null) {
|
||||
return integrated.getWorldPath(LevelResource.ROOT).resolve("satistaskory.dat");
|
||||
}
|
||||
ServerData server = minecraft.getCurrentServer();
|
||||
if (server != null && server.ip != null && !server.ip.isBlank()) {
|
||||
return minecraft.gameDirectory.toPath()
|
||||
.resolve("satistaskory")
|
||||
.resolve("remote")
|
||||
.resolve(ClientSaveKeys.remoteFileName(server.ip));
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
+22
@@ -0,0 +1,22 @@
|
||||
package com.sufficient.satistaskory.infrastructure.persistence;
|
||||
|
||||
import java.util.Locale;
|
||||
|
||||
/**
|
||||
* File names for remote-server plans on this client. Not a domain type.
|
||||
*/
|
||||
public final class ClientSaveKeys {
|
||||
private ClientSaveKeys() {}
|
||||
|
||||
public static String remoteFileName(String address) {
|
||||
String cleaned = address.toLowerCase(Locale.ROOT).replaceAll("[^a-z0-9._-]+", "_");
|
||||
cleaned = cleaned.replaceAll("^_+|_+$", "");
|
||||
if (cleaned.isEmpty()) {
|
||||
cleaned = "unknown";
|
||||
}
|
||||
if (cleaned.length() > 80) {
|
||||
cleaned = cleaned.substring(0, 80);
|
||||
}
|
||||
return cleaned + ".dat";
|
||||
}
|
||||
}
|
||||
+2
-2
@@ -9,7 +9,7 @@ import com.sufficient.satistaskory.domain.Goal;
|
||||
import com.sufficient.satistaskory.domain.ItemId;
|
||||
|
||||
/**
|
||||
* RAM store that writes the player's world save after each change.
|
||||
* RAM store that writes this client's plan file after each change.
|
||||
* Load and leave-world must not write; otherwise logout would wipe the save.
|
||||
*/
|
||||
public final class PersistingGoalStore implements GoalStore {
|
||||
@@ -61,7 +61,7 @@ public final class PersistingGoalStore implements GoalStore {
|
||||
|
||||
private void persist() {
|
||||
if (persist) {
|
||||
PlayerGoalPersistence.save(inner.all());
|
||||
ClientGoalPersistence.save(inner.all());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
-49
@@ -1,49 +0,0 @@
|
||||
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);
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -44,6 +44,10 @@ displayName="${mod_name}" #mandatory
|
||||
# The authors of the mod, displayed in the mod UI (optional)
|
||||
authors="sufficient"
|
||||
|
||||
# Client-only. The server must not require this mod to be installed.
|
||||
# IGNORE_ALL_VERSION: joining a host that does not have Satistaskory is allowed.
|
||||
displayTest="IGNORE_ALL_VERSION"
|
||||
|
||||
# The description text for the mod (multi line!) (#mandatory)
|
||||
description='''
|
||||
A Satisfactory-inspired crafting planner and goal tracker
|
||||
@@ -75,7 +79,7 @@ A Satisfactory-inspired crafting planner and goal tracker
|
||||
# AFTER - This mod is loaded AFTER the dependency
|
||||
ordering="NONE"
|
||||
# Side this dependency is applied on - BOTH, CLIENT, or SERVER
|
||||
side="BOTH"
|
||||
side="CLIENT"
|
||||
|
||||
# Here's another dependency
|
||||
[[dependencies.${mod_id}]]
|
||||
@@ -84,7 +88,7 @@ A Satisfactory-inspired crafting planner and goal tracker
|
||||
# This version range declares a minimum of the current minecraft version up to but not including the next major version
|
||||
versionRange="${minecraft_version_range}"
|
||||
ordering="NONE"
|
||||
side="BOTH"
|
||||
side="CLIENT"
|
||||
|
||||
# Features are specific properties of the game environment, that you may want to declare you require. This example declares
|
||||
# that your mod requires GL version 3.2 or higher. Other features will be added. They are side aware so declaring this won't
|
||||
|
||||
Reference in New Issue
Block a user