This commit is contained in:
1 parent
6ec97569d3
commit
e3f2f2481b
11 files changed
+507
-42
No files matched your search
@@ -55,6 +55,7 @@ public final class FantasyStickPlugin extends JavaPlugin {
|
||||
private EquipmentAdminCommand admin;
|
||||
private games.dmg.fantasystick.persistence.PlayerFeedbackPreferences feedbackPreferences;
|
||||
private games.dmg.fantasystick.session.FantasyFeedbackCommand feedbackCommand;
|
||||
private games.dmg.fantasystick.session.FantasyParticlesCommand particlesCommand;
|
||||
private games.dmg.fantasystick.presentation.CombatFeedback combatFeedback;
|
||||
private volatile Object lifetime;
|
||||
private CharacterPresentation presentation;
|
||||
@@ -274,6 +275,8 @@ public final class FantasyStickPlugin extends JavaPlugin {
|
||||
}
|
||||
feedbackCommand = new games.dmg.fantasystick.session.FantasyFeedbackCommand(feedbackPreferences, nextTick,
|
||||
failure -> getLogger().log(Level.WARNING, "Player feedback preference failed", failure));
|
||||
particlesCommand = new games.dmg.fantasystick.session.FantasyParticlesCommand(feedbackPreferences, nextTick,
|
||||
failure -> getLogger().log(Level.WARNING, "Player particle preference failed", failure));
|
||||
java.util.function.Consumer<Throwable> presentationFailures = problem ->
|
||||
getLogger().log(Level.WARNING, "Character presentation failed", problem);
|
||||
// This supplier belongs to one enable, never the mutable plugin field after re-enable.
|
||||
@@ -616,6 +619,10 @@ public final class FantasyStickPlugin extends JavaPlugin {
|
||||
"Missing packaged fantasyfeedback command");
|
||||
playerFeedbackCommand.setExecutor(feedbackCommand);
|
||||
playerFeedbackCommand.setTabCompleter(feedbackCommand);
|
||||
var playerParticlesCommand = java.util.Objects.requireNonNull(getCommand("fantasyparticles"),
|
||||
"Missing packaged fantasyparticles command");
|
||||
playerParticlesCommand.setExecutor(particlesCommand);
|
||||
playerParticlesCommand.setTabCompleter(particlesCommand);
|
||||
getServer().getPluginManager().registerEvents(crafting, this);
|
||||
getServer().getPluginManager().registerEvents(guard, this);
|
||||
getServer().getPluginManager().registerEvents(anvilNames, this);
|
||||
@@ -672,7 +679,7 @@ public final class FantasyStickPlugin extends JavaPlugin {
|
||||
getServer().getPluginManager().registerEvents(books, this);
|
||||
getServer().getPluginManager().registerEvents(gameModes, this);
|
||||
getServer().getPluginManager().registerEvents(sheetResourcePack, this);
|
||||
sessionVisuals = new games.dmg.fantasystick.session.SessionVisuals(sessions,
|
||||
sessionVisuals = new games.dmg.fantasystick.session.SessionVisuals(sessions, feedbackPreferences,
|
||||
failure -> getLogger().log(Level.WARNING, "Fantasy session particle delivery failed", failure));
|
||||
sessionVisuals.install(this);
|
||||
recipeRegistered = getServer().addRecipe(new CharacterSheets().recipe());
|
||||
@@ -698,6 +705,7 @@ public final class FantasyStickPlugin extends JavaPlugin {
|
||||
var closingChat = chatNames; chatNames = null;
|
||||
var closingAdmin = admin; admin = null;
|
||||
var closingFeedbackCommand = feedbackCommand; feedbackCommand = null;
|
||||
var closingParticlesCommand = particlesCommand; particlesCommand = null;
|
||||
var closingFeedbackPreferences = feedbackPreferences; feedbackPreferences = null;
|
||||
combatFeedback = null;
|
||||
var closingEvents = sessionEvents; sessionEvents = null;
|
||||
@@ -806,6 +814,7 @@ public final class FantasyStickPlugin extends JavaPlugin {
|
||||
if (closingProtocol != null) cleanup("protocol presentation", closingProtocol::close);
|
||||
if (closingAdmin != null) cleanup("admin command", closingAdmin::close);
|
||||
if (closingFeedbackCommand != null) cleanup("feedback command", closingFeedbackCommand::close);
|
||||
if (closingParticlesCommand != null) cleanup("particles command", closingParticlesCommand::close);
|
||||
if (closingFeedbackPreferences != null) cleanup("feedback preferences", closingFeedbackPreferences::close);
|
||||
if (closingSettings != null) cleanup("configuration", closingSettings::close);
|
||||
if (closingFireballSettings != null) cleanup("Fireball configuration", closingFireballSettings::close);
|
||||
|
||||
@@ -22,22 +22,35 @@ import org.yaml.snakeyaml.LoaderOptions;
|
||||
import org.yaml.snakeyaml.Yaml;
|
||||
import org.yaml.snakeyaml.constructor.SafeConstructor;
|
||||
|
||||
/** Durable account-owned routine combat/effect feedback preference. */
|
||||
/** Durable account-owned presentation preferences, serialized through one save-before-publish worker. */
|
||||
public final class PlayerFeedbackPreferences implements AutoCloseable {
|
||||
private static final String FILE = "player-preferences.yml";
|
||||
private static final String PLAYERS = "players";
|
||||
private static final String KEY = "combat-feedback";
|
||||
private static final int MAX_BYTES = 1_048_576;
|
||||
|
||||
public record Update(UUID account, boolean enabled, long sequence) {
|
||||
public Update { Objects.requireNonNull(account); }
|
||||
public enum Preference {
|
||||
COMBAT_FEEDBACK("combat-feedback", true),
|
||||
PLAYER_AURAS("player-aura-particles", false),
|
||||
ALL_PARTICLES("all-particles", true);
|
||||
|
||||
private final String key;
|
||||
private final boolean defaultValue;
|
||||
Preference(String key, boolean defaultValue) { this.key = key; this.defaultValue = defaultValue; }
|
||||
}
|
||||
|
||||
private record AccountPreference(UUID account, Preference preference) {
|
||||
private AccountPreference { Objects.requireNonNull(account); Objects.requireNonNull(preference); }
|
||||
}
|
||||
|
||||
public record Update(UUID account, Preference preference, boolean enabled, long sequence) {
|
||||
public Update { Objects.requireNonNull(account); Objects.requireNonNull(preference); }
|
||||
}
|
||||
|
||||
private final Path directory;
|
||||
private final java.util.concurrent.ExecutorService worker = Executors.newSingleThreadExecutor(
|
||||
task -> new Thread(task, "fantasy-player-feedback-preferences"));
|
||||
private final Map<UUID, Boolean> effective = new java.util.HashMap<>();
|
||||
private final Map<UUID, Long> published = new java.util.HashMap<>();
|
||||
private final Map<AccountPreference, Boolean> effective = new java.util.HashMap<>();
|
||||
private final Map<AccountPreference, Long> published = new java.util.HashMap<>();
|
||||
private long persistedSequence;
|
||||
private boolean closed;
|
||||
private int pending;
|
||||
@@ -48,13 +61,34 @@ public final class PlayerFeedbackPreferences implements AutoCloseable {
|
||||
effective.putAll(loadEnabled());
|
||||
}
|
||||
|
||||
public synchronized boolean enabled(UUID account) {
|
||||
/** Existing routine combat/effect feedback preference. */
|
||||
public synchronized boolean enabled(UUID account) { return value(account, Preference.COMBAT_FEEDBACK); }
|
||||
public synchronized boolean playerAurasEnabled(UUID account) { return value(account, Preference.PLAYER_AURAS); }
|
||||
public synchronized boolean allParticlesEnabled(UUID account) { return value(account, Preference.ALL_PARTICLES); }
|
||||
public synchronized boolean auraParticlesEnabled(UUID account) {
|
||||
return value(account, Preference.ALL_PARTICLES) && value(account, Preference.PLAYER_AURAS);
|
||||
}
|
||||
|
||||
private boolean value(UUID account, Preference preference) {
|
||||
Objects.requireNonNull(account);
|
||||
return effective.getOrDefault(account, true);
|
||||
return effective.getOrDefault(new AccountPreference(account, preference), preference.defaultValue);
|
||||
}
|
||||
|
||||
public CompletionStage<Update> set(UUID account, boolean enabled) {
|
||||
Objects.requireNonNull(account);
|
||||
return set(account, Preference.COMBAT_FEEDBACK, enabled);
|
||||
}
|
||||
|
||||
public CompletionStage<Update> setPlayerAuras(UUID account, boolean enabled) {
|
||||
return set(account, Preference.PLAYER_AURAS, enabled);
|
||||
}
|
||||
|
||||
/** Shared persisted precedence for the separately delivered all-particles command. */
|
||||
public CompletionStage<Update> setAllParticles(UUID account, boolean enabled) {
|
||||
return set(account, Preference.ALL_PARTICLES, enabled);
|
||||
}
|
||||
|
||||
private CompletionStage<Update> set(UUID account, Preference preference, boolean enabled) {
|
||||
Objects.requireNonNull(account); Objects.requireNonNull(preference);
|
||||
synchronized (this) {
|
||||
if (closed) return CompletableFuture.failedStage(new IOException("Player preference store is closed"));
|
||||
pending++;
|
||||
@@ -62,11 +96,11 @@ public final class PlayerFeedbackPreferences implements AutoCloseable {
|
||||
try {
|
||||
return CompletableFuture.supplyAsync(() -> {
|
||||
try {
|
||||
save(account, enabled);
|
||||
return new Update(account, enabled, ++persistedSequence);
|
||||
save(account, preference, enabled);
|
||||
return new Update(account, preference, enabled, ++persistedSequence);
|
||||
} catch (IOException | RuntimeException failure) {
|
||||
throw new java.util.concurrent.CompletionException(new IOException(
|
||||
"Combat feedback preference save refused or failed; effective setting unchanged", failure));
|
||||
"Player preference save refused or failed; effective setting unchanged", failure));
|
||||
} finally { finished(); }
|
||||
}, worker).minimalCompletionStage();
|
||||
} catch (RejectedExecutionException rejected) {
|
||||
@@ -78,34 +112,37 @@ public final class PlayerFeedbackPreferences implements AutoCloseable {
|
||||
public synchronized boolean publish(Update update) {
|
||||
Objects.requireNonNull(update);
|
||||
if (closed) return false;
|
||||
long previous = published.getOrDefault(update.account(), 0L);
|
||||
var key = new AccountPreference(update.account(), update.preference());
|
||||
long previous = published.getOrDefault(key, 0L);
|
||||
if (update.sequence() <= previous) return false;
|
||||
effective.put(update.account(), update.enabled());
|
||||
published.put(update.account(), update.sequence());
|
||||
effective.put(key, update.enabled());
|
||||
published.put(key, update.sequence());
|
||||
return true;
|
||||
}
|
||||
|
||||
private Map<UUID, Boolean> loadEnabled() throws IOException {
|
||||
private Map<AccountPreference, Boolean> loadEnabled() throws IOException {
|
||||
Map<String, Object> root = readRoot();
|
||||
Object rawPlayers = root.get(PLAYERS);
|
||||
if (rawPlayers == null) return Map.of();
|
||||
if (!(rawPlayers instanceof Map<?, ?> players)) throw new IOException("Invalid player preferences");
|
||||
var result = new LinkedHashMap<UUID, Boolean>();
|
||||
var result = new LinkedHashMap<AccountPreference, Boolean>();
|
||||
for (var entry : players.entrySet()) {
|
||||
if (!(entry.getKey() instanceof String rawId)) throw new IOException("Invalid player preference identity");
|
||||
UUID account;
|
||||
try { account = UUID.fromString(rawId); }
|
||||
catch (IllegalArgumentException invalid) { throw new IOException("Invalid player preference identity", invalid); }
|
||||
if (!(entry.getValue() instanceof Map<?, ?> values)) throw new IOException("Invalid player preference entry");
|
||||
Object raw = values.get(KEY);
|
||||
if (raw == null) continue;
|
||||
if (!(raw instanceof Boolean value)) throw new IOException("Invalid combat feedback preference");
|
||||
result.put(account, value);
|
||||
for (var preference : Preference.values()) {
|
||||
Object raw = values.get(preference.key);
|
||||
if (raw == null) continue;
|
||||
if (!(raw instanceof Boolean value)) throw new IOException("Invalid " + preference.key + " preference");
|
||||
result.put(new AccountPreference(account, preference), value);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private void save(UUID account, boolean enabled) throws IOException {
|
||||
private void save(UUID account, Preference preference, boolean enabled) throws IOException {
|
||||
Files.createDirectories(directory);
|
||||
if (!Files.isDirectory(directory, LinkOption.NOFOLLOW_LINKS)) throw new IOException("Unsafe data directory");
|
||||
Path lockFile = directory.resolve(".player-preferences.lock");
|
||||
@@ -118,7 +155,7 @@ public final class PlayerFeedbackPreferences implements AutoCloseable {
|
||||
Map<String, Object> root = readRoot();
|
||||
Map<String, Object> players = stringMap(root.get(PLAYERS), "player preferences");
|
||||
Map<String, Object> values = stringMap(players.get(account.toString()), "player preference entry");
|
||||
values.put(KEY, enabled);
|
||||
values.put(preference.key, enabled);
|
||||
players.put(account.toString(), values);
|
||||
root.put(PLAYERS, players);
|
||||
byte[] bytes = yaml().dump(root).getBytes(StandardCharsets.UTF_8);
|
||||
|
||||
@@ -0,0 +1,89 @@
|
||||
package games.dmg.fantasystick.session;
|
||||
|
||||
import games.dmg.fantasystick.persistence.PlayerFeedbackPreferences;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Objects;
|
||||
import java.util.UUID;
|
||||
import java.util.function.Consumer;
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.command.TabExecutor;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
/** Ordinary-player command for durable account-owned Fantasy Stick particle preferences. */
|
||||
public final class FantasyParticlesCommand implements TabExecutor, AutoCloseable {
|
||||
private final PlayerFeedbackPreferences preferences;
|
||||
private final Consumer<Runnable> nextTick;
|
||||
private final Consumer<Throwable> failures;
|
||||
private boolean closed;
|
||||
|
||||
public FantasyParticlesCommand(PlayerFeedbackPreferences preferences, Consumer<Runnable> nextTick,
|
||||
Consumer<Throwable> failures) {
|
||||
this.preferences = Objects.requireNonNull(preferences);
|
||||
this.nextTick = Objects.requireNonNull(nextTick);
|
||||
this.failures = Objects.requireNonNull(failures);
|
||||
}
|
||||
|
||||
@Override public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
|
||||
if (!(sender instanceof Player player)) {
|
||||
sender.sendMessage("Fantasy particles are a player-account preference.");
|
||||
return true;
|
||||
}
|
||||
if (args.length == 1 && args[0].equals("players")) {
|
||||
player.sendMessage(status(player.getUniqueId()));
|
||||
return true;
|
||||
}
|
||||
if (args.length != 2 || !args[0].equals("players")
|
||||
|| !(args[1].equals("on") || args[1].equals("off"))) return false;
|
||||
boolean enabled = args[1].equals("on");
|
||||
UUID account = player.getUniqueId();
|
||||
preferences.setPlayerAuras(account, enabled).whenComplete((saved, failure) -> {
|
||||
if (failure != null) report(failure);
|
||||
try {
|
||||
nextTick.accept(() -> {
|
||||
if (closed) return;
|
||||
if (failure != null) {
|
||||
if (player.isOnline()) player.sendMessage(
|
||||
"Active-character aura preference could not be saved; " + status(account));
|
||||
return;
|
||||
}
|
||||
boolean published = preferences.publish(saved);
|
||||
if (player.isOnline()) player.sendMessage(acknowledgement(account, published));
|
||||
});
|
||||
} catch (RuntimeException rejected) { report(rejected); }
|
||||
});
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override public List<String> onTabComplete(CommandSender sender, Command command, String alias, String[] args) {
|
||||
if (!(sender instanceof Player)) return List.of();
|
||||
if (args.length == 1) return matching(List.of("players"), args[0]);
|
||||
if (args.length == 2 && args[0].equals("players")) return matching(List.of("off", "on"), args[1]);
|
||||
return List.of();
|
||||
}
|
||||
|
||||
private String status(UUID account) {
|
||||
boolean saved = preferences.playerAurasEnabled(account);
|
||||
if (saved && !preferences.allParticlesEnabled(account))
|
||||
return "Active-character aura particles are on (effectively off while all particles are off).";
|
||||
return "Active-character aura particles are " + state(saved) + ".";
|
||||
}
|
||||
|
||||
private String acknowledgement(UUID account, boolean published) {
|
||||
boolean saved = preferences.playerAurasEnabled(account);
|
||||
String prefix = "Active-character aura particles are " + (published ? "now " : "already ") + state(saved);
|
||||
if (saved && !preferences.allParticlesEnabled(account))
|
||||
return prefix + " (effectively off while all particles are off).";
|
||||
return prefix + ".";
|
||||
}
|
||||
|
||||
private static List<String> matching(List<String> values, String rawPrefix) {
|
||||
String prefix = rawPrefix.toLowerCase(Locale.ROOT);
|
||||
return values.stream().filter(value -> value.startsWith(prefix)).toList();
|
||||
}
|
||||
|
||||
private static String state(boolean enabled) { return enabled ? "on" : "off"; }
|
||||
private void report(Throwable failure) { try { failures.accept(failure); } catch (RuntimeException ignored) { } }
|
||||
@Override public void close() { closed = true; }
|
||||
}
|
||||
@@ -11,21 +11,37 @@ import org.bukkit.scheduler.BukkitTask;
|
||||
/** Public particles only; never grants gameplay state, protection, health or persistence authority. */
|
||||
public final class SessionVisuals implements AutoCloseable {
|
||||
private final EquipmentSessions sessions;
|
||||
private final games.dmg.fantasystick.persistence.PlayerFeedbackPreferences preferences;
|
||||
private final Consumer<Throwable> failures;
|
||||
private BukkitTask task;
|
||||
private boolean closed;
|
||||
|
||||
public SessionVisuals(EquipmentSessions sessions, Consumer<Throwable> failures) {
|
||||
this.sessions = Objects.requireNonNull(sessions); this.failures = Objects.requireNonNull(failures);
|
||||
public SessionVisuals(EquipmentSessions sessions,
|
||||
games.dmg.fantasystick.persistence.PlayerFeedbackPreferences preferences,
|
||||
Consumer<Throwable> failures) {
|
||||
this.sessions = Objects.requireNonNull(sessions);
|
||||
this.preferences = Objects.requireNonNull(preferences);
|
||||
this.failures = Objects.requireNonNull(failures);
|
||||
}
|
||||
public void install(Plugin plugin) {
|
||||
if (closed || task != null) throw new IllegalStateException("Visual lifetime already used");
|
||||
task = plugin.getServer().getScheduler().runTaskTimer(plugin, () -> {
|
||||
if (closed) return;
|
||||
for (var player : plugin.getServer().getOnlinePlayers()) {
|
||||
var online = java.util.List.copyOf(plugin.getServer().getOnlinePlayers());
|
||||
for (var source : online) {
|
||||
try {
|
||||
var active = sessions.active(player.getUniqueId());
|
||||
if (active.isPresent() && sessions.current(active.orElseThrow())) burst(player, Color.BLUE, 3);
|
||||
var active = sessions.active(source.getUniqueId());
|
||||
if (active.isEmpty() || !sessions.current(active.orElseThrow())) continue;
|
||||
var location = source.getLocation().add(0, 1, 0);
|
||||
var dust = new Particle.DustOptions(Color.BLUE, 1);
|
||||
var tracked = source.getTrackedBy();
|
||||
for (var viewer : online) {
|
||||
if (!viewer.isOnline() || viewer.getWorld() != source.getWorld()
|
||||
|| (!viewer.getUniqueId().equals(source.getUniqueId()) && !tracked.contains(viewer))
|
||||
|| !viewer.canSee(source)
|
||||
|| !preferences.auraParticlesEnabled(viewer.getUniqueId())) continue;
|
||||
viewer.spawnParticle(Particle.DUST, location, 3, 0.3, 0.4, 0.3, 0, dust);
|
||||
}
|
||||
} catch (RuntimeException failure) { failures.accept(failure); }
|
||||
}
|
||||
}, 1L, 10L);
|
||||
|
||||
Reference in new issue
Block a user