Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ The full documentation index is in [`docs/README.md`](docs/README.md).

- `featureframework-bom` — one import for aligned framework artifact versions.
- `featureframework-api` — stable public runtime, feature metadata, and capability contracts.
- `featureframework-hud-api` — immutable Adventure frames and a platform-neutral, expiring priority-lease coordinator; see [HUD ownership](docs/toolkits/HUD-OWNERSHIP.md).
- `featureframework-theme-api` — platform-neutral theme identifiers, colour operations, builders, and registry contracts.
- `featureframework-toolkit` — platform-neutral configuration, localization, cache, HTTP, text, and token utilities.
- `featureframework-core` — feature model, host/runtime, lifecycle, dependency loading, services, and resource ownership.
Expand Down
26 changes: 26 additions & 0 deletions docs/toolkits/HUD-OWNERSHIP.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Shared HUD ownership

`featureframework-hud-api` supplies `HudService`, immutable `HudFrame` values and the platform-independent `HudCoordinator`. Adventure is provided by the platform. The API has no Bukkit entities, worlds, scheduler, SQL or static runtime singleton.

A host publishes `HudService` through its platform service boundary. ServerFeatures supplies the Paper host; other plugins acquire leases through the live service. Do not create a second shared coordinator when a host is already present. Producers must handle service replacement by closing old handles and acquiring new ones.

```java
var lease = hud.claim("MyPlugin:adventure", playerId, HudService.Channel.SIDEBAR,
HudService.SESSION, Duration.ofSeconds(10));
lease.update(new HudFrame.Sidebar(Component.text("Adventure"), lines));
lease.renew(Duration.ofSeconds(10));
// On departure, release instead of restoring a captured old scoreboard:
lease.close();
```

Channels are sidebar, action bar, boss bar and title. Priorities are ambient (0), session (100), contextual (200), and critical (300). Equal priorities retain acquisition order. A newly acquired null frame suppresses underlying content without drawing. Frames must match the lease channel. Sidebars are bounded to 15 lines and managed boss-bar frames to two bars; progress is finite and clamped to 0..1. Frame collections, including boss-bar flags, are defensively copied.

All operations belong to the host owner thread. The coordinator accepts an injected thread check and monotonic clock. Hosts call `tick` to expire handles and request action-bar keepalive about once per second. Rendered equality suppresses unchanged writes. Title producers should use bounded lifetimes matching their display duration so stale titles do not reappear later.

Owner names are plugin names, optionally followed by colon-separated feature scopes. `releaseOwner("MyPlugin")` also releases `MyPlugin:feature` scopes. Hosts release scopes on plugin disable and player quit, and close the coordinator on shutdown. Handles are idempotently closeable; a stale handle cannot renew, update or remove a newer claim. Releasing the winning claim reveals the latest lower-priority frame, including changes made while hidden.

A renderer failure does not prevent other keys from being processed during a tick or shutdown. Errors are reported after that work. Hosts should isolate/throttle renderer error reporting. The renderer owns platform-side cleanup and must preserve unrelated teams/objectives.

The Paper action-bar toolkit also accepts an injected `Delivery` transport carrying priority. Normal cycle frames use ambient priority and manual overrides use critical priority; the default constructor preserves raw Paper delivery for hosts without shared HUDs.

Tests cover restoration of updated underlying content, suppression, expiry, stable ties, independent players, stale handles, keepalive/deduplication, failing renderers and a 100-player/10-run simulation.
1 change: 1 addition & 0 deletions featureframework-bom/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<packaging>pom</packaging>
<name>FeatureFramework BOM</name>
<dependencyManagement><dependencies>
<dependency><groupId>${project.groupId}</groupId><artifactId>featureframework-hud-api</artifactId><version>${project.version}</version></dependency>
<dependency><groupId>${project.groupId}</groupId><artifactId>featureframework-api</artifactId><version>${project.version}</version></dependency>
<dependency><groupId>${project.groupId}</groupId><artifactId>featureframework-theme-api</artifactId><version>${project.version}</version></dependency>
<dependency><groupId>${project.groupId}</groupId><artifactId>featureframework-toolkit</artifactId><version>${project.version}</version></dependency>
Expand Down
11 changes: 11 additions & 0 deletions featureframework-hud-api/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0">
<modelVersion>4.0.0</modelVersion>
<parent><groupId>nl.hauntedmc.featureframework</groupId><artifactId>featureframework-parent</artifactId><version>${revision}</version></parent>
<artifactId>featureframework-hud-api</artifactId>
<name>FeatureFramework HUD API</name>
<dependencies>
<dependency><groupId>net.kyori</groupId><artifactId>adventure-api</artifactId><scope>provided</scope></dependency>
<dependency><groupId>org.junit.jupiter</groupId><artifactId>junit-jupiter</artifactId><scope>test</scope></dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
package nl.hauntedmc.featureframework.hud;

import java.time.Duration;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Objects;
import java.util.UUID;
import java.util.function.LongSupplier;

/** Platform-independent lease engine. The host drives expiry/keepalive and enforces its thread. */
public final class HudCoordinator implements HudService, AutoCloseable {
@FunctionalInterface
public interface Renderer { void render(UUID player, Channel channel, HudFrame frame); }
private record Key(UUID player, Channel channel) {}
private final Map<Key, Map<Long, Claim>> claims = new HashMap<>();
private final Map<Key, HudFrame> rendered = new HashMap<>();
private final Renderer renderer;
private final LongSupplier clock;
private final Runnable threadCheck;
private long sequence;
private boolean closed;

public HudCoordinator(Renderer renderer, LongSupplier clock, Runnable threadCheck) {
this.renderer = Objects.requireNonNull(renderer);
this.clock = Objects.requireNonNull(clock);
this.threadCheck = Objects.requireNonNull(threadCheck);
}

@Override public Lease claim(String owner, UUID player, Channel channel, int priority, Duration lifetime) {
check();
if (owner == null || owner.isBlank()) throw new IllegalArgumentException("Missing HUD owner");
Key key = new Key(Objects.requireNonNull(player), Objects.requireNonNull(channel));
Claim claim = new Claim(owner, key, priority, ++sequence, deadline(lifetime));
claims.computeIfAbsent(key, ignored -> new LinkedHashMap<>()).put(claim.id, claim);
try { render(key, false); }
catch (RuntimeException failure) {
claims.get(key).remove(claim.id);
try { render(key, false); }
catch (RuntimeException restoreFailure) { if (restoreFailure != failure) failure.addSuppressed(restoreFailure); }
throw failure;
}
return claim;
}

/** Expire leases every host tick; request action-bar keepalive approximately once a second. */
public void tick(boolean keepalive) {
check();
RuntimeException failure=null;
for (Key key : new ArrayList<>(claims.keySet())) {
claims.get(key).values().removeIf(claim -> claim.expires <= clock.getAsLong());
try { render(key, keepalive && key.channel == Channel.ACTION_BAR); }
catch(RuntimeException problem) { if(failure==null) failure=problem; else if(problem!=failure) failure.addSuppressed(problem); }
}
if(failure!=null) throw failure;
}

@Override public void releaseOwner(String owner) {
check();
Objects.requireNonNull(owner);
RuntimeException failure = null;
for (Key key : new ArrayList<>(claims.keySet())) {
claims.get(key).values().removeIf(claim -> (claim.owner.equals(owner) || claim.owner.startsWith(owner + ":")));
try { render(key, false); }
catch (RuntimeException problem) { if (failure == null) failure = problem; else if (problem != failure) failure.addSuppressed(problem); }
}
if (failure != null) throw failure;
}

@Override public void removePlayer(UUID player) {
check();
Objects.requireNonNull(player);
RuntimeException failure = null;
for (Key key : new ArrayList<>(claims.keySet())) if (key.player.equals(player)) {
claims.remove(key);
rendered.remove(key);
try { renderer.render(player, key.channel, null); }
catch (RuntimeException problem) { if (failure == null) failure = problem; else if (problem != failure) failure.addSuppressed(problem); }
}
if (failure != null) throw failure;
}

private void render(Key key, boolean force) {
Map<Long, Claim> entries = claims.get(key);
Claim winner = entries == null ? null : entries.values().stream()
.filter(claim -> claim.expires > clock.getAsLong())
.min(Comparator.<Claim>comparingInt(claim -> claim.priority).reversed()
.thenComparingLong(claim -> claim.id)).orElse(null);
HudFrame frame = winner == null ? null : winner.frame;
if (force || !Objects.equals(frame, rendered.get(key))) {
renderer.render(key.player, key.channel, frame);
if (frame == null) rendered.remove(key); else rendered.put(key, frame);
}
if (entries != null && entries.isEmpty()) claims.remove(key);
}

private long deadline(Duration lifetime) {
if (lifetime == null || lifetime.isNegative() || lifetime.isZero())
throw new IllegalArgumentException("Positive HUD lease duration required");
return Math.addExact(clock.getAsLong(), lifetime.toNanos());
}
private void check() {
threadCheck.run();
if (closed) throw new IllegalStateException("HUD coordinator is closed");
}
@Override public void close() {
if (closed) return;
threadCheck.run();
RuntimeException failure=null;
for (Key key : new ArrayList<>(claims.keySet())) {
try { renderer.render(key.player, key.channel, null); }
catch(RuntimeException problem) { if(failure==null) failure=problem; else if(problem!=failure) failure.addSuppressed(problem); }
}
claims.clear(); rendered.clear(); closed = true;
if(failure!=null) throw failure;
}

private final class Claim implements Lease {
private final String owner;
private final Key key;
private final int priority;
private final long id;
private long expires;
private HudFrame frame;
private Claim(String owner, Key key, int priority, long id, long expires) {
this.owner = owner; this.key = key; this.priority = priority; this.id = id; this.expires = expires;
}
public boolean active() {
threadCheck.run();
return !closed && expires > clock.getAsLong()
&& claims.getOrDefault(key, Map.of()).get(id) == this;
}
public void update(HudFrame next) {
check();
if (!active()) throw new IllegalStateException("HUD lease expired");
if (next != null && next.channel() != key.channel) throw new IllegalArgumentException("Wrong HUD channel");
frame = next;
render(key, false);
}
public void renew(Duration lifetime) {
check();
if (!active()) throw new IllegalStateException("HUD lease expired");
expires = deadline(lifetime);
}
public void close() {
threadCheck.run();
if (closed) return;
Map<Long, Claim> entries = claims.get(key);
if (entries != null && entries.remove(id, this)) render(key, false);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package nl.hauntedmc.featureframework.hud;

import java.time.Duration;
import java.util.List;
import java.util.Objects;
import net.kyori.adventure.bossbar.BossBar;
import net.kyori.adventure.text.Component;

/** Immutable content; no platform entities or mutable Adventure boss bars cross this boundary. */
public sealed interface HudFrame {
HudService.Channel channel();

record Sidebar(Component title, List<Component> lines) implements HudFrame {
public Sidebar {
Objects.requireNonNull(title);
lines = List.copyOf(lines);
if (lines.size() > 15) throw new IllegalArgumentException("A sidebar has at most 15 lines");
}
public HudService.Channel channel() { return HudService.Channel.SIDEBAR; }
}
record ActionBar(Component text) implements HudFrame {
public ActionBar { Objects.requireNonNull(text); }
public HudService.Channel channel() { return HudService.Channel.ACTION_BAR; }
}
record Bar(Component title, float progress, BossBar.Color color, BossBar.Overlay overlay,
java.util.Set<BossBar.Flag> flags) {
public Bar(Component title,float progress,BossBar.Color color,BossBar.Overlay overlay) {
this(title,progress,color,overlay,java.util.Set.of());
}
public Bar {
Objects.requireNonNull(title);
flags=java.util.Set.copyOf(flags);
Objects.requireNonNull(color);
Objects.requireNonNull(overlay);
if (!Float.isFinite(progress)) throw new IllegalArgumentException("Non-finite progress");
progress = Math.clamp(progress, 0, 1);
}
}
record Bars(List<Bar> bars) implements HudFrame {
public Bars {
bars = List.copyOf(bars);
if (bars.size() > 2) throw new IllegalArgumentException("At most two managed bars");
}
public HudService.Channel channel() { return HudService.Channel.BOSS_BAR; }
}
record Title(Component title, Component subtitle, Duration fadeIn, Duration stay,
Duration fadeOut) implements HudFrame {
public Title {
Objects.requireNonNull(title);
Objects.requireNonNull(subtitle);
for (Duration time : List.of(fadeIn, stay, fadeOut))
if (time.isNegative()) throw new IllegalArgumentException("Negative title duration");
}
public HudService.Channel channel() { return HudService.Channel.TITLE; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package nl.hauntedmc.featureframework.hud;

import java.time.Duration;
import java.util.UUID;

/** Owner-thread HUD arbitration. A null frame suppresses underlying content without drawing. */
public interface HudService {
enum Channel { SIDEBAR, ACTION_BAR, BOSS_BAR, TITLE }
int AMBIENT = 0;
int SESSION = 100;
int CONTEXT = 200;
int CRITICAL = 300;

/** Owner is the plugin name, optionally followed by colon-separated feature scopes. */
Lease claim(String owner, UUID player, Channel channel, int priority, Duration lifetime);
/** Releases this owner and its colon-separated descendant feature scopes. */
void releaseOwner(String owner);
void removePlayer(UUID player);

interface Lease extends AutoCloseable {
void update(HudFrame frame);
void renew(Duration lifetime);
boolean active();
@Override void close();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/** Immutable HUD contracts and owner-thread priority arbitration, independent of a platform host. */
package nl.hauntedmc.featureframework.hud;
Loading
Loading