69 lines
3.0 KiB
Java
69 lines
3.0 KiB
Java
package games.dmg.fantasystick.events;
|
|
|
|
import java.nio.charset.StandardCharsets;
|
|
import java.time.Clock;
|
|
import java.util.LinkedHashMap;
|
|
import java.util.Objects;
|
|
import java.util.UUID;
|
|
import java.util.function.Consumer;
|
|
import org.bukkit.command.BlockCommandSender;
|
|
import org.bukkit.command.CommandSender;
|
|
import org.bukkit.entity.Player;
|
|
|
|
/** Authorized ability-setting reads and acknowledged updates; no write authority. */
|
|
public final class AbilityConfigEventBridge {
|
|
public record Actor(String kind, UUID playerId, String playerName) { }
|
|
|
|
private final Consumer<EventInput> sink;
|
|
private final Clock clock;
|
|
|
|
public AbilityConfigEventBridge(Consumer<EventInput> sink, Clock clock) {
|
|
this.sink = Objects.requireNonNull(sink);
|
|
this.clock = Objects.requireNonNull(clock);
|
|
}
|
|
|
|
/** Capture account identity on the command thread, never on the file worker. */
|
|
public Actor capture(CommandSender sender) {
|
|
try {
|
|
if (sender instanceof Player player) return new Actor("player", player.getUniqueId(), player.getName());
|
|
return new Actor(sender instanceof BlockCommandSender ? "command_block" : "console", null, null);
|
|
} catch (RuntimeException ignored) {
|
|
return null; // Omit uncertain identity, never refuse the authorized action.
|
|
}
|
|
}
|
|
|
|
public void read(Actor actor, String abilityId, String key, String value) {
|
|
read(UUID.randomUUID(), actor, abilityId, key, value);
|
|
}
|
|
|
|
public void read(UUID operation, Actor actor, String abilityId, String key, String value) {
|
|
emit(operation, "config.read", actor, abilityId, key, null, value);
|
|
}
|
|
|
|
public void changed(Actor actor, String abilityId, String key, String before, String after) {
|
|
changed(UUID.randomUUID(), actor, abilityId, key, before, after);
|
|
}
|
|
|
|
public void changed(UUID operation, Actor actor, String abilityId, String key, String before, String after) {
|
|
emit(operation, "config.changed", actor, abilityId, key, before, after);
|
|
}
|
|
|
|
private void emit(UUID operation, String type, Actor actor, String abilityId, String key, String before, String after) {
|
|
try {
|
|
var data = new LinkedHashMap<String, Object>();
|
|
data.put("actor_kind", actor.kind());
|
|
data.put("ability_id", abilityId);
|
|
data.put("key", key);
|
|
if (before != null) data.put("before", before);
|
|
data.put("after", after);
|
|
data.put("application", "future_casts_only");
|
|
data.put("outcome", "acknowledged");
|
|
var id = UUID.nameUUIDFromBytes(("fantasystick:ability-config:" + operation + ":" + type)
|
|
.getBytes(StandardCharsets.UTF_8));
|
|
var context = actor.playerId() == null ? null
|
|
: new EventInput.Context(actor.playerId(), actor.playerName(), null, null, null, null);
|
|
sink.accept(new EventInput(id, operation, clock.instant(), type, context, data));
|
|
} catch (RuntimeException ignored) { /* File logging cannot change an authorized configuration action. */ }
|
|
}
|
|
}
|