This commit is contained in:
1 parent
6ec97569d3
commit
e3f2f2481b
11 files changed
+507
-42
No files matched your search
@@ -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; }
|
||||
}
|
||||
Reference in new issue
Block a user