/satistaskory auto-remove. Off by default. A gain in held target items reduces quantity and removes the goal at 0. Player inventory only. Already-held stacks are a baseline, not a wipe.
This commit is contained in:
@@ -0,0 +1,48 @@
|
||||
package com.sufficient.satistaskory.application;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import com.sufficient.satistaskory.domain.Goal;
|
||||
import com.sufficient.satistaskory.domain.InventoryState;
|
||||
import com.sufficient.satistaskory.domain.ItemId;
|
||||
|
||||
/**
|
||||
* When auto-remove is on: an increase in held target items counts as obtained.
|
||||
* Dumping items does not put quantity back. First look after enable/join is a baseline, not a gain.
|
||||
*/
|
||||
public final class ObtainedCompletion {
|
||||
private final Map<ItemId, Integer> lastHeld = new HashMap<>();
|
||||
private boolean primed;
|
||||
|
||||
public void reset() {
|
||||
lastHeld.clear();
|
||||
primed = false;
|
||||
}
|
||||
|
||||
public void apply(Planner planner, InventoryState inventory) {
|
||||
if (!primed) {
|
||||
seed(planner, inventory);
|
||||
primed = true;
|
||||
return;
|
||||
}
|
||||
for (Goal goal : List.copyOf(planner.goals())) {
|
||||
int now = inventory.get(goal.targetItem());
|
||||
int was = lastHeld.getOrDefault(goal.targetItem(), 0);
|
||||
int gained = now - was;
|
||||
if (gained > 0) {
|
||||
planner.reduce(goal.targetItem(), gained);
|
||||
}
|
||||
}
|
||||
seed(planner, inventory);
|
||||
}
|
||||
|
||||
private void seed(Planner planner, InventoryState inventory) {
|
||||
lastHeld.clear();
|
||||
lastHeld.putAll(inventory.contents());
|
||||
for (Goal goal : planner.goals()) {
|
||||
lastHeld.putIfAbsent(goal.targetItem(), 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.sufficient.satistaskory.application;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
@@ -23,6 +24,10 @@ public final class Planner {
|
||||
this.inventory = Objects.requireNonNull(inventory, "inventory");
|
||||
}
|
||||
|
||||
public List<Goal> goals() {
|
||||
return store.all();
|
||||
}
|
||||
|
||||
public ProgressSnapshot snapshot() {
|
||||
return refresh();
|
||||
}
|
||||
@@ -64,6 +69,24 @@ public final class Planner {
|
||||
return refresh();
|
||||
}
|
||||
|
||||
/**
|
||||
* Count obtained items toward the goal. Removes the goal when quantity hits 0.
|
||||
*/
|
||||
public void reduce(ItemId item, int amount) {
|
||||
Objects.requireNonNull(item, "item");
|
||||
if (amount < 1) {
|
||||
return;
|
||||
}
|
||||
store.find(item).ifPresent(existing -> {
|
||||
int left = existing.quantity() - amount;
|
||||
if (left < 1) {
|
||||
store.remove(item);
|
||||
} else {
|
||||
store.put(new Goal(item, left));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private ProgressSnapshot refresh() {
|
||||
return progress.calculate(store.all(), inventory.get());
|
||||
}
|
||||
|
||||
@@ -40,4 +40,8 @@ public final class InventoryState {
|
||||
}
|
||||
return total;
|
||||
}
|
||||
|
||||
public Map<ItemId, Integer> contents() {
|
||||
return available;
|
||||
}
|
||||
}
|
||||
|
||||
+25
-1
@@ -26,7 +26,11 @@ public final class ClientCommands {
|
||||
PlannerSession.get().clear();
|
||||
ctx.getSource().sendSuccess(() -> Component.literal("Cleared crafting goals"), false);
|
||||
return 1;
|
||||
})));
|
||||
}))
|
||||
.then(Commands.literal("auto-remove")
|
||||
.executes(ClientCommands::toggleAutoRemove)
|
||||
.then(Commands.literal("on").executes(ctx -> setAutoRemove(ctx, true)))
|
||||
.then(Commands.literal("off").executes(ctx -> setAutoRemove(ctx, false)))));
|
||||
}
|
||||
|
||||
private static int add(CommandContext<CommandSourceStack> ctx) {
|
||||
@@ -43,4 +47,24 @@ public final class ClientCommands {
|
||||
ctx.getSource().sendSuccess(() -> Component.literal("Added " + item), false);
|
||||
return 1;
|
||||
}
|
||||
|
||||
private static int toggleAutoRemove(CommandContext<CommandSourceStack> ctx) {
|
||||
boolean on = ClientSettings.toggleAutoRemove();
|
||||
tellAutoRemove(ctx, on);
|
||||
return 1;
|
||||
}
|
||||
|
||||
private static int setAutoRemove(CommandContext<CommandSourceStack> ctx, boolean on) {
|
||||
ClientSettings.setAutoRemove(on);
|
||||
tellAutoRemove(ctx, on);
|
||||
return 1;
|
||||
}
|
||||
|
||||
private static void tellAutoRemove(CommandContext<CommandSourceStack> ctx, boolean on) {
|
||||
ctx.getSource().sendSuccess(
|
||||
() -> Component.translatable(on
|
||||
? "satistaskory.message.auto_remove.on"
|
||||
: "satistaskory.message.auto_remove.off"),
|
||||
false);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,78 @@
|
||||
package com.sufficient.satistaskory.infrastructure.client;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
import com.google.gson.JsonObject;
|
||||
|
||||
import com.sufficient.satistaskory.Satistaskory;
|
||||
|
||||
import net.minecraft.client.Minecraft;
|
||||
|
||||
/**
|
||||
* Client preferences. Not per-world. Auto-remove is off until the player turns it on.
|
||||
*/
|
||||
public final class ClientSettings {
|
||||
private static final Gson GSON = new GsonBuilder().setPrettyPrinting().create();
|
||||
private static boolean autoRemove;
|
||||
|
||||
private ClientSettings() {}
|
||||
|
||||
public static boolean autoRemove() {
|
||||
return autoRemove;
|
||||
}
|
||||
|
||||
public static boolean toggleAutoRemove() {
|
||||
return setAutoRemove(!autoRemove);
|
||||
}
|
||||
|
||||
public static boolean setAutoRemove(boolean value) {
|
||||
autoRemove = value;
|
||||
save();
|
||||
return autoRemove;
|
||||
}
|
||||
|
||||
public static void load() {
|
||||
Path file = file();
|
||||
if (file == null || !Files.isRegularFile(file)) {
|
||||
autoRemove = false;
|
||||
return;
|
||||
}
|
||||
try {
|
||||
JsonObject json = GSON.fromJson(Files.readString(file), JsonObject.class);
|
||||
autoRemove = json != null && json.has("autoRemove") && json.get("autoRemove").getAsBoolean();
|
||||
} catch (RuntimeException | IOException e) {
|
||||
Satistaskory.LOGGER.warn("Could not read Satistaskory settings at {}", file, e);
|
||||
autoRemove = false;
|
||||
}
|
||||
}
|
||||
|
||||
private static void save() {
|
||||
Path file = file();
|
||||
if (file == null) {
|
||||
return;
|
||||
}
|
||||
JsonObject json = new JsonObject();
|
||||
json.addProperty("autoRemove", autoRemove);
|
||||
try {
|
||||
Path parent = file.getParent();
|
||||
if (parent != null) {
|
||||
Files.createDirectories(parent);
|
||||
}
|
||||
Files.writeString(file, GSON.toJson(json));
|
||||
} catch (IOException e) {
|
||||
Satistaskory.LOGGER.warn("Could not write Satistaskory settings at {}", file, e);
|
||||
}
|
||||
}
|
||||
|
||||
private static Path file() {
|
||||
Minecraft minecraft = Minecraft.getInstance();
|
||||
if (minecraft == null) {
|
||||
return null;
|
||||
}
|
||||
return minecraft.gameDirectory.toPath().resolve("satistaskory").resolve("settings.json");
|
||||
}
|
||||
}
|
||||
@@ -1,9 +1,11 @@
|
||||
package com.sufficient.satistaskory.infrastructure.client;
|
||||
|
||||
import com.sufficient.satistaskory.application.ObtainedCompletion;
|
||||
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.ProgressSnapshot;
|
||||
import com.sufficient.satistaskory.domain.RecipeResolution;
|
||||
import com.sufficient.satistaskory.infrastructure.inventory.PlayerInventoryAdapter;
|
||||
import com.sufficient.satistaskory.infrastructure.persistence.PersistingGoalStore;
|
||||
@@ -19,6 +21,7 @@ import net.minecraft.client.player.LocalPlayer;
|
||||
public final class PlannerSession {
|
||||
private static PersistingGoalStore store;
|
||||
private static Planner planner;
|
||||
private static final ObtainedCompletion obtained = new ObtainedCompletion();
|
||||
|
||||
private PlannerSession() {}
|
||||
|
||||
@@ -30,6 +33,8 @@ public final class PlannerSession {
|
||||
ProgressCalculation progress = new ProgressCalculation(resolution, new MaterialAggregation());
|
||||
store = new PersistingGoalStore();
|
||||
planner = new Planner(store, progress, PlannerSession::currentInventory);
|
||||
ClientSettings.load();
|
||||
obtained.reset();
|
||||
}
|
||||
|
||||
public static Planner get() {
|
||||
@@ -39,15 +44,27 @@ public final class PlannerSession {
|
||||
return planner;
|
||||
}
|
||||
|
||||
public static ProgressSnapshot snapshot() {
|
||||
bootstrap();
|
||||
if (ClientSettings.autoRemove()) {
|
||||
obtained.apply(planner, currentInventory());
|
||||
} else {
|
||||
obtained.reset();
|
||||
}
|
||||
return planner.snapshot();
|
||||
}
|
||||
|
||||
public static void onJoinWorld() {
|
||||
bootstrap();
|
||||
store.loadFromWorld(ClientGoalPersistence.load());
|
||||
obtained.reset();
|
||||
}
|
||||
|
||||
public static void onLeaveWorld() {
|
||||
if (store != null) {
|
||||
store.unload();
|
||||
}
|
||||
obtained.reset();
|
||||
}
|
||||
|
||||
private static InventoryState currentInventory() {
|
||||
|
||||
@@ -46,7 +46,7 @@ public final class PlayOverlay {
|
||||
return;
|
||||
}
|
||||
|
||||
ProgressSnapshot snapshot = PlannerSession.get().snapshot();
|
||||
ProgressSnapshot snapshot = PlannerSession.snapshot();
|
||||
if (snapshot.goals().isEmpty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -4,5 +4,7 @@
|
||||
"key.categories.satistaskory": "Satistaskory",
|
||||
"key.satistaskory.add_goal": "Add to Crafting Goals",
|
||||
"satistaskory.tooltip.add": "[%s] Add to Crafting Goals",
|
||||
"satistaskory.message.added": "Added %s to crafting goals"
|
||||
"satistaskory.message.added": "Added %s to crafting goals",
|
||||
"satistaskory.message.auto_remove.on": "Auto-remove ON: picking up a goal item counts it done. Dumping it in a chest does not bring it back.",
|
||||
"satistaskory.message.auto_remove.off": "Auto-remove OFF: goals stay until you [-] or Clear."
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user