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
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## 0.7.0

- **Who may know whose it is** (D231). `ReachService.mayKnow(viewer, owner, world)` tells another plugin, such as LanternGraves, whether a player may see the owner's name on something they left behind. Stricter than chat, so it fails closed: the owner, either side's bypass, a shared circle, or both players in that world's group right now. Offline owners count only through a circle or bypass; with no circles set up, nobody but the owner.
- Mayor remembers each player's circles and bypass in `known-players.yml` when they join and leave, so the answer works while the owner is offline. A broken file is moved to `known-players.yml.bak` and nobody offline counts as known until they're seen again.
- Tested: unit tests for kids and adults in separate worlds, an adult who left the kids' world or went offline, a shared family circle, a circle-only world, bypass, no circles, an unknown owner, and the file surviving a restart or breaking. Bots on a throwaway Paper 26.3 server with LanternGraves 0.2.0 saw names or "Grave" exactly as the rule says (94/94). Not yet seen in a real game client.

## 0.6.1

- `mask-for-viewers` no longer gets a warning at start-up saying it isn't a setting. 0.6.0 read and used it all along; only the list of known settings missed it.
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ if (rules == null || rules.mayReach(from, to)) { /* send it */ }

`sharesCircle(viewerHas, to)` is for a viewer who isn't in game, such as someone on a web map. It's true only when they share a circle with `to`. World, world group and bypass don't count, and with no circles set up it's always false. The caller passes the viewer's permissions, read while they're offline, and anything it can't check should answer false.

`mayKnow(viewer, owner, world)` says whether a player may know who left something in a world, such as a grave's name. It's stricter than chat, so it fails closed: the owner, either side's `mayor.bypass`, a shared circle, or both players standing in that world's group right now (unless it's circle-only). An offline owner counts only through a circle or bypass, from what Mayor last saw of them. With no circles set up, nobody but the owner may, and it answers false for anyone Mayor has never seen. Call it from the main thread. For that, Mayor keeps `known-players.yml`: each player's ID with the circle names they held and whether they had `mayor.bypass`, updated when they join and leave and on /mayor reload. Nothing else about them is stored. If you take someone out of a circle while they're offline, Mayor still uses their old circles until they next join; delete their line from the file (with the server stopped) to make it take effect at once.

## Limits

- Signs are readable by anyone who walks past, and a book or named item can be carried to another world. Mayor only checks them when they're written.
Expand Down
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ plugins {
}

group = "com.extantape"
version = "0.6.1"
version = "0.7.0"

java {
toolchain.languageVersion.set(JavaLanguageVersion.of(25))
Expand Down
34 changes: 34 additions & 0 deletions src/main/java/com/extantape/mayor/MayorPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ public record State(Settings settings, Messages messages, Quips quips) {
private volatile List<String> problems = List.of();
/** Who each player last messaged or was messaged by, for /r. */
private final Map<UUID, UUID> partners = new ConcurrentHashMap<>();
/** Each player's circles as last seen, for rules about someone offline (D231). */
private com.extantape.mayor.rules.KnownPlayers known;

@Override
public void onEnable() {
Expand All @@ -56,6 +58,8 @@ public void onEnable() {
}
Settings defaults = new SettingsLoader(this::getResource).load(null, null, null).settings();
fallback = new State(defaults, new Messages(defaults.messages()), new Quips(List.of(), 0, new Random()));
known = new com.extantape.mayor.rules.KnownPlayers(getDataFolder().toPath(), getLogger());
known.load();
reload(Bukkit.getConsoleSender());

var plugins = getServer().getPluginManager();
Expand Down Expand Up @@ -102,6 +106,7 @@ public boolean reload(CommandSender sender) {
new Quips(settings.quips(), settings.nobodyHeard().cooldownSeconds(), new Random()));
problems = result.warnings();
registerCirclePermissions(settings.reach());
rememberOnline();
Reach reach = settings.reach();
getLogger().info(reach.active()
? "Chat rules are on, with " + reach.circles().size() + (reach.circles().size() == 1 ? " circle. " : " circles. ") + settings.filterSummary()
Expand All @@ -120,6 +125,22 @@ private void registerCirclePermissions(Reach reach) {
}
}

/** Records what each online player holds now, and saves if anything changed. */
public void rememberOnline() {
State s = state;
if (s == null || known == null) return;
boolean changed = false;
for (Player p : Bukkit.getOnlinePlayers()) changed |= known.remember(Subject.of(p), s.settings().reach().circles());
if (changed) known.save();
}

/** Records one player as they are now, and saves if anything changed. */
public void remember(Player player) {
State s = state;
if (s == null || known == null) return;
if (known.remember(Subject.of(player), s.settings().reach().circles())) known.save();
}

/** The current rules, or null while chat is paused because the config couldn't be read. */
public State state() {
return state;
Expand Down Expand Up @@ -183,4 +204,17 @@ public boolean sharesCircle(java.util.function.Predicate<String> viewerHas, Play
return false;
}
}

@Override
public boolean mayKnow(Player viewer, UUID owner, String world) {
State s = state;
if (s == null || viewer == null || owner == null) return false;
try {
Player online = Bukkit.getPlayer(owner);
Subject who = online != null ? Subject.of(online) : known == null ? null : known.recall(owner);
return s.settings().reach().mayKnow(Subject.of(viewer), who, world, online != null);
} catch (RuntimeException e) {
return false;
}
}
}
9 changes: 9 additions & 0 deletions src/main/java/com/extantape/mayor/api/ReachService.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,13 @@ public interface ReachService {
* it should answer false for anything it can't check. Safe from any thread.
*/
boolean sharesCircle(java.util.function.Predicate<String> viewerHas, Player to);

/**
* Whether {@code viewer} may know who {@code owner} is from something the owner left in {@code world}, such as a
* grave's name (D231). Stricter than chat: the owner, either side's bypass, a shared circle, or both standing in
* that world's group now. An offline owner counts only through a circle or bypass, from what Mayor last saw of
* them. False with no circles set up, for an owner Mayor has never seen, or while chat is paused. Call it from
* the main thread.
*/
boolean mayKnow(Player viewer, java.util.UUID owner, String world);
}
7 changes: 6 additions & 1 deletion src/main/java/com/extantape/mayor/listen/PlayerListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@

import java.util.Map;

/** Tells admins about config problems when they join, and forgets a player's reply partner when they leave. */
/**
* Tells admins about config problems when they join, and forgets a player's reply partner when they leave. Records each
* player's circles on join and quit, for rules about someone offline (D231).
*/
public final class PlayerListener implements Listener {
private final MayorPlugin plugin;

Expand All @@ -20,13 +23,15 @@ public PlayerListener(MayorPlugin plugin) {
@EventHandler
public void onJoin(PlayerJoinEvent event) {
Player player = event.getPlayer();
plugin.remember(player);
if (!player.hasPermission(MayorPlugin.ADMIN)) return;
if (plugin.paused()) plugin.tell(player, "admin-paused");
else if (!plugin.problems().isEmpty()) plugin.tell(player, "admin-warnings", Map.of("count", String.valueOf(plugin.problems().size())));
}

@EventHandler
public void onQuit(PlayerQuitEvent event) {
plugin.remember(event.getPlayer());
plugin.partners().remove(event.getPlayer().getUniqueId());
MayorPlugin.State state = plugin.state();
if (state != null) state.quips().forget(event.getPlayer().getUniqueId());
Expand Down
95 changes: 95 additions & 0 deletions src/main/java/com/extantape/mayor/rules/KnownPlayers.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package com.extantape.mayor.rules;

import org.bukkit.configuration.InvalidConfigurationException;
import org.bukkit.configuration.file.YamlConfiguration;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import java.util.logging.Logger;

/**
* What Mayor last saw of each player's circles and bypass, so a rule about someone who's offline (whose grave is
* this?) can still be answered (D231). Kept in known-players.yml: one line per player ID with the circle names they
* held and "bypass" if they had mayor.bypass. Nothing else about the player is stored. A player who isn't listed is
* unknown, and every rule treats unknown as no.
*/
public final class KnownPlayers {
public static final String FILE = "known-players.yml";
private static final String BYPASS_WORD = "bypass";

private final Path file;
private final Logger log;
private final Map<UUID, Set<String>> held = new ConcurrentHashMap<>();

public KnownPlayers(Path folder, Logger log) {
this.file = folder.resolve(FILE);
this.log = log;
}

/** Reads the file. A broken file is moved aside to known-players.yml.bak and Mayor starts with nobody known. */
public void load() {
held.clear();
if (!Files.exists(file)) return;
YamlConfiguration yaml = new YamlConfiguration();
try {
yaml.loadFromString(Files.readString(file));
} catch (IOException | InvalidConfigurationException e) {
log.warning(FILE + " couldn't be read, so it was moved to " + FILE + ".bak and nobody offline is known yet: " + e.getMessage());
try {
Files.move(file, file.resolveSibling(FILE + ".bak"), StandardCopyOption.REPLACE_EXISTING);
} catch (IOException ignored) {
// Left in place; the next save overwrites it, which is fine because it couldn't be read anyway.
}
return;
}
for (String key : yaml.getKeys(false)) {
try {
held.put(UUID.fromString(key), new HashSet<>(yaml.getStringList(key)));
} catch (IllegalArgumentException ignored) {
// Not a player ID: skipped.
}
}
}

/** Records what a player holds now, from the circles in use. Returns true if anything changed. */
public boolean remember(Subject player, List<String> circles) {
Set<String> now = new HashSet<>();
for (String circle : circles) if (player.has(Reach.CIRCLE_PERMISSION + circle)) now.add(circle);
if (player.has(Reach.BYPASS)) now.add(BYPASS_WORD);
return !now.equals(held.put(player.id(), now));
}

/** The player as last seen, for the rules, or null if Mayor has never seen them. Has no world. */
public Subject recall(UUID id) {
Set<String> got = held.get(id);
if (got == null) return null;
Set<String> copy = Set.copyOf(got);
return Subject.of(id, "", "", permission -> {
if (permission.equals(Reach.BYPASS)) return copy.contains(BYPASS_WORD);
return permission.startsWith(Reach.CIRCLE_PERMISSION)
&& copy.contains(permission.substring(Reach.CIRCLE_PERMISSION.length()));
});
}

/** Writes the file through a temporary file, so a crash mid-write never leaves half of it. */
public void save() {
YamlConfiguration yaml = new YamlConfiguration();
held.forEach((id, set) -> yaml.set(id.toString(), set.stream().sorted().toList()));
try {
Files.createDirectories(file.getParent());
Path tmp = file.resolveSibling(FILE + ".tmp");
Files.writeString(tmp, yaml.saveToString());
Files.move(tmp, file, StandardCopyOption.REPLACE_EXISTING, StandardCopyOption.ATOMIC_MOVE);
} catch (IOException e) {
log.warning(FILE + " couldn't be saved: " + e.getMessage());
}
}
}
20 changes: 20 additions & 0 deletions src/main/java/com/extantape/mayor/rules/Reach.java
Original file line number Diff line number Diff line change
Expand Up @@ -121,4 +121,24 @@ public boolean reachesWhileAway(java.util.function.Predicate<String> viewer, Sub
}
return false;
}

/**
* Whether {@code viewer} may know who {@code owner} is from something the owner left in {@code world}, such as a
* grave's name (owner, 2026-11-16, D231). Stricter than chat, so it fails closed: the owner themself, the viewer's
* mayor.bypass, the owner's mayor.bypass, a shared circle, or both players standing in that world's group right now
* (unless it's circle-only). An owner who's offline counts only through a circle or bypass, since where they
* stand isn't known. With no circles set up, nobody else may: there are no groups to follow. An owner Mayor knows
* nothing about ({@code owner} null) counts as no.
*
* @param ownerOnline whether {@code owner} describes a player online now, so their world is real
*/
public boolean mayKnow(Subject viewer, Subject owner, String world, boolean ownerOnline) {
if (owner != null && viewer.id().equals(owner.id())) return true;
if (!active() || owner == null || world == null) return false;
if (viewer.has(BYPASS) || owner.has(BYPASS)) return true;
if (sharedCircle(viewer, owner) != null) return true;
String here = group(world);
return ownerOnline && !circleOnly.contains(here)
&& here.equals(group(viewer.world())) && here.equals(group(owner.world()));
}
}
114 changes: 114 additions & 0 deletions src/test/java/com/extantape/mayor/rules/MayKnowTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
package com.extantape.mayor.rules;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import java.util.logging.Logger;

import static com.extantape.mayor.rules.Players.circle;
import static com.extantape.mayor.rules.Players.player;
import static org.junit.jupiter.api.Assertions.*;

/** D231: who may know whose grave it is. Kids and adults in separate worlds, a mixed circle, and no rules at all. */
class MayKnowTest {
// kids play in "kids" (with its nether), adults in "survival"; "family" is the circle parents and kids share.
private final Reach reach = new Reach(List.of("family", "adults"), Set.of("lobby"), Map.of(), List.of("_nether"));

@Test
void noCirclesMeansOnlyTheOwnerKnows() {
Reach off = new Reach(List.of(), Set.of(), Map.of(), List.of());
assertFalse(off.mayKnow(player("kid", "kids"), player("stranger", "survival"), "survival", true));
Subject kid = player("kid", "kids");
assertTrue(off.mayKnow(kid, kid, "kids", false));
}

@Test
void ownerAlwaysKnowsTheirOwn() {
Subject kid = player("kid", "survival");
assertTrue(reach.mayKnow(kid, kid, "kids", true));
}

@Test
void bothInTheGravesWorldGroupNowKnow() {
assertTrue(reach.mayKnow(player("kid2", "kids_nether"), player("kid", "kids"), "kids", true));
}

@Test
void ownerWhoLeftTheWorldOrIsOfflineCountsOnlyByCircle() {
// An adult stranger who died in the kids' world and went back, or logged off: the kids don't see the name.
assertFalse(reach.mayKnow(player("kid", "kids"), player("stranger", "survival", circle("adults")), "kids", true));
assertFalse(reach.mayKnow(player("kid", "kids"), player("stranger", "kids", circle("adults")), "kids", false));
}

@Test
void kidElsewhereDoesNotKnowAnAdultsGrave() {
assertFalse(reach.mayKnow(player("kid", "kids"), player("stranger", "survival", circle("adults")), "survival", true));
}

@Test
void adultInAnotherWorldDoesNotKnowAKidsGrave() {
assertFalse(reach.mayKnow(player("stranger", "survival", circle("adults")), player("kid", "kids"), "kids", true));
}

@Test
void sharedCircleKnowsAcrossWorlds() {
assertTrue(reach.mayKnow(player("mum", "survival", circle("family")), player("kid", "kids", circle("family")), "kids", true));
}

@Test
void circleOnlyWorldNeedsASharedCircle() {
assertFalse(reach.mayKnow(player("a", "lobby"), player("b", "lobby"), "lobby", true));
assertTrue(reach.mayKnow(player("a", "lobby", circle("family")), player("b", "lobby", circle("family")), "lobby", true));
}

@Test
void bypassOnEitherSideKnows() {
assertTrue(reach.mayKnow(player("admin", "survival", Reach.BYPASS), player("kid", "kids"), "kids", true));
assertTrue(reach.mayKnow(player("kid", "kids_nether"), player("admin", "survival", Reach.BYPASS), "survival", true));
}

@Test
void unknownOwnerFailsClosed() {
assertFalse(reach.mayKnow(player("kid", "kids"), null, "kids", true));
assertFalse(reach.mayKnow(player("kid", "kids"), player("x", "kids"), null, true));
}

@Test
void offlineOwnerIsRecalledFromWhatMayorSaw(@TempDir Path dir) {
KnownPlayers known = new KnownPlayers(dir, Logger.getAnonymousLogger());
Subject kid = player("kid", "kids", circle("family"));
assertTrue(known.remember(kid, reach.circles()));
known.save();
KnownPlayers again = new KnownPlayers(dir, Logger.getAnonymousLogger());
again.load();
Subject recalled = again.recall(kid.id());
assertNotNull(recalled);
assertTrue(reach.mayKnow(player("mum", "survival", circle("family")), recalled, "kids", false));
assertFalse(reach.mayKnow(player("stranger", "survival", circle("adults")), recalled, "kids", false));
assertFalse(reach.mayKnow(player("kid2", "kids"), recalled, "kids", false), "offline, no shared circle: no");
assertNull(again.recall(UUID.randomUUID()));
}

@Test
void rememberReportsOnlyChanges(@TempDir Path dir) {
KnownPlayers known = new KnownPlayers(dir, Logger.getAnonymousLogger());
Subject kid = player("kid", "kids", circle("family"));
assertTrue(known.remember(kid, reach.circles()));
assertFalse(known.remember(kid, reach.circles()));
}

@Test
void brokenFileIsMovedAsideAndNobodyIsKnown(@TempDir Path dir) throws Exception {
Files.writeString(dir.resolve(KnownPlayers.FILE), "- a list, not a map\n");
KnownPlayers known = new KnownPlayers(dir, Logger.getAnonymousLogger());
known.load();
assertTrue(Files.exists(dir.resolve(KnownPlayers.FILE + ".bak")));
assertNull(known.recall(UUID.nameUUIDFromBytes("kid".getBytes())));
}
}
1 change: 1 addition & 0 deletions test/live/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ It takes about 4 minutes. The summary lists failures and skipped checks. The ful
| Books | A clean edit is kept; a filtered one, or one in a circle-only world with no circle, is refused with an explanation. A filtered word is kept for a player with `mayor.filter.bypass`. |
| Nobody heard you | A note from `quips.yml`, a different one next time, and none when someone heard. |
| Share (mocked) | Recipients filtered exactly as chat, and cancelled for a no-circle sender in a circle-only world. |
| Graves (D231) | With a LanternGraves jar (`MAYOR_LG_JAR`), real graves from planned deaths: each bot sees the owner's name or "Grave" exactly as an independent model of `mayKnow` says, with the owner in the same group, moved away, sharing a circle, offline, and in a circle-only world. Skipped without the jar. |
| LuckPerms | A circle granted or removed takes effect with no relog. |
| Config | A broken reload keeps the old rules; a broken config at startup pauses chat and private messages until it's fixed and reloaded. |
| Load | 20 more bots, 300 lines in about 15 seconds: all delivered within their circle, TPS, chat latency, no kicks. |
Expand Down
Loading
Loading