99 lines
4.8 KiB
Java
99 lines
4.8 KiB
Java
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") || args[0].equals("all"))) {
|
|
player.sendMessage(status(player.getUniqueId(), args[0]));
|
|
return true;
|
|
}
|
|
if (args.length != 2 || !(args[0].equals("players") || args[0].equals("all"))
|
|
|| !(args[1].equals("on") || args[1].equals("off"))) return false;
|
|
String route = args[0];
|
|
boolean enabled = args[1].equals("on");
|
|
UUID account = player.getUniqueId();
|
|
var save = route.equals("players")
|
|
? preferences.setPlayerAuras(account, enabled) : preferences.setAllParticles(account, enabled);
|
|
save.whenComplete((saved, failure) -> {
|
|
if (failure != null) report(failure);
|
|
try {
|
|
nextTick.accept(() -> {
|
|
if (closed) return;
|
|
if (failure != null) {
|
|
if (player.isOnline()) player.sendMessage(
|
|
(route.equals("players") ? "Active-character aura" : "All-particles")
|
|
+ " preference could not be saved; " + status(account, route));
|
|
return;
|
|
}
|
|
boolean published = preferences.publish(saved);
|
|
if (player.isOnline()) player.sendMessage(acknowledgement(account, route, 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("all", "players"), args[0]);
|
|
if (args.length == 2 && (args[0].equals("players") || args[0].equals("all")))
|
|
return matching(List.of("off", "on"), args[1]);
|
|
return List.of();
|
|
}
|
|
|
|
private String status(UUID account, String route) {
|
|
if (route.equals("all"))
|
|
return "All Fantasy Stick particles are " + state(preferences.allParticlesEnabled(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, String route, boolean published) {
|
|
if (route.equals("all")) return "All Fantasy Stick particles are "
|
|
+ (published ? "now " : "already ") + state(preferences.allParticlesEnabled(account)) + ".";
|
|
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; }
|
|
}
|