Add independent eight-hour Night Vision progression, save-gated crafting and holder eligibility, authenticated helmet items and guarded inventory movement. Verify source attribution, native item/recipe/event adapters and actual plugin lifecycle. Drain final state asynchronously and reject stale lifecycle callbacks. Per-viewer revelation remains the next story; no deployment is included.
74 lines
3.5 KiB
Java
74 lines
3.5 KiB
Java
package games.dmg.spigotstealth;
|
|
|
|
import java.time.Duration;
|
|
import java.util.HashMap;
|
|
import java.util.Map;
|
|
import java.util.Objects;
|
|
import java.util.Set;
|
|
import java.util.UUID;
|
|
import java.util.concurrent.CompletableFuture;
|
|
import java.util.concurrent.ConcurrentHashMap;
|
|
import java.util.function.Consumer;
|
|
import java.util.function.LongSupplier;
|
|
|
|
/** Server-thread qualifying intervals; durable rewards are acknowledged by the existing I/O executor. */
|
|
public final class EyeProgressionService {
|
|
public static final Duration UNLOCK_THRESHOLD = Duration.ofHours(8);
|
|
private record Interval(long startedNanos, long creditedMillis) { }
|
|
private final StealthStateManager states;
|
|
private final LongSupplier nanoTime;
|
|
private final Consumer<UUID> unlocked;
|
|
private final Map<UUID, Interval> active = new HashMap<>();
|
|
private final Set<UUID> announced = ConcurrentHashMap.newKeySet();
|
|
|
|
/** The notification callback must marshal Bukkit work onto the server thread. */
|
|
public EyeProgressionService(StealthStateManager states, LongSupplier nanoTime, Consumer<UUID> unlocked) {
|
|
this.states = Objects.requireNonNull(states, "states");
|
|
this.nanoTime = Objects.requireNonNull(nanoTime, "nanoTime");
|
|
this.unlocked = Objects.requireNonNull(unlocked, "unlocked");
|
|
states.durableSnapshot().eyes().forEach((id, progress) -> {
|
|
if (progress.unlocked()) { announced.add(id); }
|
|
});
|
|
}
|
|
|
|
public CompletableFuture<Void> begin(UUID id) {
|
|
Objects.requireNonNull(id, "id");
|
|
if (active.containsKey(id)) { return checkpoint(id); }
|
|
active.put(id, new Interval(nanoTime.getAsLong(), 0));
|
|
return CompletableFuture.completedFuture(null);
|
|
}
|
|
|
|
public CompletableFuture<Void> checkpoint(UUID id) {
|
|
Interval interval = active.get(id);
|
|
if (interval == null) { return CompletableFuture.completedFuture(null); }
|
|
// Keep the original start: repeated checkpoints must not discard fractional milliseconds.
|
|
long total = Math.max(interval.creditedMillis(), Math.max(0, nanoTime.getAsLong() - interval.startedNanos()) / 1_000_000);
|
|
long elapsed = total - interval.creditedMillis();
|
|
if (elapsed == 0) { return CompletableFuture.completedFuture(null); }
|
|
active.put(id, new Interval(interval.startedNanos(), total));
|
|
return states.update(state -> {
|
|
EyeProgress previous = state.eyeProgress(id);
|
|
long accumulated = Math.addExact(previous.accumulatedMillis(), elapsed);
|
|
return state.withEyeProgress(id, new EyeProgress(accumulated,
|
|
previous.unlocked() || accumulated >= UNLOCK_THRESHOLD.toMillis(), previous.unknownFields()));
|
|
}).thenRun(() -> {
|
|
if (isUnlocked(id) && announced.add(id)) { unlocked.accept(id); }
|
|
});
|
|
}
|
|
|
|
public CompletableFuture<Void> stop(UUID id) {
|
|
CompletableFuture<Void> saved = checkpoint(id);
|
|
active.remove(id);
|
|
return saved;
|
|
}
|
|
|
|
public CompletableFuture<Void> stopAll() {
|
|
return CompletableFuture.allOf(activePlayerIds().stream().map(this::stop).toArray(CompletableFuture[]::new));
|
|
}
|
|
|
|
/** Defensive offline sweep: do not invent credit for the unobserved interval after the last checkpoint. */
|
|
public void abandon(UUID id) { active.remove(id); }
|
|
public boolean isUnlocked(UUID id) { return states.durableSnapshot().eyeProgress(id).unlocked(); }
|
|
public Set<UUID> activePlayerIds() { return Set.copyOf(active.keySet()); }
|
|
}
|