Repository navigation
Add configurable faction espionage and special positions #102
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
2681d16
Add faction and guild espionage with configurable special offices
XxFran10xX d19dab9
Keep reported ledger title readable and document migrated config
XxFran10xX 631b3c4
Address espionage review findings and cover migration and completion …
XxFran10xX 6823646
Complete invitations using visible character names
XxFran10xX 58c0bd4
Keep rumours as report minimum and restore masked menu navigation
XxFran10xX 344ff55
Label missing reports as botched and clean coloured menu titles
XxFran10xX 45f1f05
Preserve office state when persistence fails
XxFran10xX 90f4ba9
Simplify faction rosters and apply unrest for vacant offices
XxFran10xX a872ecf
Omit inconclusive cashflow rows from ledger summaries
XxFran10xX 6fe69ec
Address espionage review privacy, optional plugins and persistence
XxFran10xX d852d66
Bind pending founder offices to their character identity
XxFran10xX 76b0a08
Merge current main into espionage development branch
XxFran10xX 7cd88f6
Persist changed pending founder identities on office lookups
XxFran10xX 1b65f87
Roll back failed pending founder identity saves and document name fal…
XxFran10xX File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
src/main/java/net/tfminecraft/simplefactions/espionage/CharacterAptitudes.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| package net.tfminecraft.simplefactions.espionage; | ||
|
|
||
| import java.io.IOException; | ||
| import java.nio.file.Files; | ||
| import java.nio.file.Path; | ||
| import java.nio.file.StandardCopyOption; | ||
| import java.util.LinkedHashMap; | ||
| import java.util.Map; | ||
| import java.util.function.Supplier; | ||
| import com.google.gson.reflect.TypeToken; | ||
| import net.tfminecraft.simplefactions.database.JsonUtil; | ||
|
|
||
| /** Character identity, rather than faction membership, owns the permanent roll. */ | ||
| public final class CharacterAptitudes { | ||
| private final Path file; | ||
| private final Map<String, Integer> aptitudes = new LinkedHashMap<>(); | ||
|
|
||
| public CharacterAptitudes(Path file) { this.file = file; } | ||
|
|
||
| public void load() throws IOException { | ||
| aptitudes.clear(); | ||
| if (!Files.exists(file)) return; | ||
| Map<String, Integer> loaded = JsonUtil.GSON.fromJson(Files.readString(file), | ||
| new TypeToken<Map<String, Integer>>() {}.getType()); | ||
| if (loaded == null) throw new IOException("Empty character aptitude registry: " + file); | ||
| loaded.forEach((key, value) -> { | ||
| if (key != null && value != null) aptitudes.put(key, EspionageMath.clamp(value, 0, 100)); | ||
| }); | ||
| } | ||
|
|
||
| public void remember(String characterId, int aptitude) { | ||
| if (characterId != null && !characterId.isBlank()) | ||
| aptitudes.putIfAbsent(characterId, EspionageMath.clamp(aptitude, 0, 100)); | ||
| } | ||
|
|
||
| public int aptitude(String characterId, Supplier<Integer> roll) throws IOException { | ||
| if (characterId == null || characterId.isBlank()) throw new IllegalArgumentException("Missing character identity"); | ||
| Integer existing = aptitudes.get(characterId); | ||
| if (existing != null) return existing; | ||
| int result = EspionageMath.clamp(roll.get(), 0, 100); | ||
| aptitudes.put(characterId, result); | ||
| try { save(); } | ||
| catch (IOException error) { aptitudes.remove(characterId); throw error; } | ||
| return result; | ||
| } | ||
|
|
||
| public void save() throws IOException { | ||
| Files.createDirectories(file.getParent()); | ||
| Path pending = file.resolveSibling(file.getFileName() + ".tmp"); | ||
| Files.writeString(pending, JsonUtil.GSON.toJson(aptitudes)); | ||
| try { Files.move(pending, file, StandardCopyOption.REPLACE_EXISTING, StandardCopyOption.ATOMIC_MOVE); } | ||
| catch (java.nio.file.AtomicMoveNotSupportedException ignored) { | ||
| Files.move(pending, file, StandardCopyOption.REPLACE_EXISTING); | ||
| } | ||
| } | ||
| } |
105 changes: 105 additions & 0 deletions
105
src/main/java/net/tfminecraft/simplefactions/espionage/CharacterNames.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| package net.tfminecraft.simplefactions.espionage; | ||
|
|
||
| import java.nio.file.Files; | ||
| import java.nio.file.Path; | ||
| import java.util.Map; | ||
| import org.bukkit.Bukkit; | ||
| import net.tfminecraft.rpcharacters.managers.PlayerManager; | ||
| import net.tfminecraft.simplefactions.database.JsonUtil; | ||
| import com.google.gson.JsonObject; | ||
|
|
||
| /** Offline names are read without loading or modifying RPCharacters player state. */ | ||
| public final class CharacterNames { | ||
| private record Cached(String name, long expires) {} | ||
| private static final Map<String, Cached> cache = new java.util.concurrent.ConcurrentHashMap<>(); | ||
| private CharacterNames() {} | ||
|
|
||
| public static String display(org.bukkit.entity.Player viewer, String account) { | ||
| String character = of(account); | ||
| return EspionageService.bypasses(viewer) ? character + " \u00a77(" + account + ")" : character; | ||
| } | ||
|
|
||
| public static String display(org.bukkit.entity.Player viewer, String account, | ||
| net.tfminecraft.simplefactions.objects.Faction faction) { | ||
| if (!OfficeCharacters.enabled("RPCharacters") || EspionageService.canViewExact(viewer, faction)) | ||
| return display(viewer, account); | ||
| return forForeign(account); | ||
| } | ||
|
|
||
| /** A missing active character must not expose account identity in foreign reports. */ | ||
| public static String forForeign(String account) { | ||
| String name = activeName(account); | ||
| return name == null ? "Unknown" : name; | ||
| } | ||
|
|
||
| /** Exact account names take precedence; ambiguous character names never pick a player arbitrarily. */ | ||
| public static org.bukkit.entity.Player resolveOnline(org.bukkit.entity.Player sender, String input) { | ||
| String query = input.strip(); | ||
| if (query.length() >= 2 && query.startsWith("\"") && query.endsWith("\"")) | ||
| query = query.substring(1, query.length() - 1).strip(); | ||
| var result = resolve(query, Bukkit.getOnlinePlayers(), player -> of(player.getName())); | ||
| if (result.ambiguous()) sender.sendMessage("\u00a7cSeveral characters share that name. Use their Minecraft name."); | ||
| else if (result.player() == null) sender.sendMessage("\u00a7cNo online player found by that character or Minecraft name."); | ||
| return result.player(); | ||
| } | ||
|
|
||
| static Resolution resolve(String query, java.util.Collection<? extends org.bukkit.entity.Player> players, | ||
| java.util.function.Function<org.bukkit.entity.Player, String> names) { | ||
| for (var player : players) if (player.getName().equalsIgnoreCase(query)) return new Resolution(player, false); | ||
| var matches = players.stream().filter(player -> org.bukkit.ChatColor.stripColor(names.apply(player)) | ||
| .equalsIgnoreCase(query)).toList(); | ||
| return new Resolution(matches.size() == 1 ? matches.getFirst() : null, matches.size() > 1); | ||
| } | ||
|
|
||
| record Resolution(org.bukkit.entity.Player player, boolean ambiguous) {} | ||
|
|
||
| /** Bukkit replaces only the current argument, so return the untyped suffix of a full name. */ | ||
| public static java.util.List<String> inviteCompletions(java.util.Collection<String> names, String[] args) { | ||
| if (args.length < 2) return java.util.List.of(); | ||
| int skip = args.length - 2; | ||
| String prefix = String.join(" ", java.util.Arrays.copyOfRange(args, 1, args.length)); | ||
| return names.stream().filter(java.util.Objects::nonNull).map(org.bukkit.ChatColor::stripColor).distinct() | ||
| .filter(name -> name.regionMatches(true, 0, prefix, 0, prefix.length())) | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| .map(name -> name.split(" ")) | ||
| .filter(words -> words.length > skip) | ||
| .map(words -> String.join(" ", java.util.Arrays.copyOfRange(words, skip, words.length))) | ||
| .toList(); | ||
| } | ||
|
|
||
| public static String of(String playerName) { | ||
| String name = activeName(playerName); | ||
| return name == null ? playerName : name; | ||
| } | ||
|
|
||
| private static String activeName(String playerName) { | ||
| if (playerName == null) return "Unknown"; | ||
| if (!OfficeCharacters.enabled("RPCharacters")) return playerName; | ||
| var player = Bukkit.getPlayerExact(playerName); | ||
| if (player != null) { | ||
| var data = PlayerManager.get(player); | ||
| var character = data == null ? null : data.getActiveCharacter(); | ||
| if (character != null) return character.getName(); | ||
| } | ||
| Cached cached = cache.get(playerName); | ||
| if (cached != null && cached.expires > System.currentTimeMillis()) return cached.name; | ||
| String result = null; | ||
| var plugin = Bukkit.getPluginManager().getPlugin("RPCharacters"); | ||
| Path folder = plugin.getDataFolder().toPath().resolve("data/characterdata") | ||
| .resolve(Bukkit.getOfflinePlayer(playerName).getUniqueId().toString()); | ||
| if (Files.isDirectory(folder)) { | ||
| try (var files = Files.list(folder)) { | ||
| for (Path file : files.filter(path -> path.toString().endsWith(".json")).sorted().toList()) { | ||
| JsonObject json = JsonUtil.GSON.fromJson(Files.readString(file), JsonObject.class); | ||
| if (json != null && json.has("active") && json.get("active").getAsBoolean() && json.has("name")) { | ||
| result = json.get("name").getAsString(); | ||
| break; | ||
| } | ||
| } | ||
| } catch (java.io.IOException | com.google.gson.JsonParseException | IllegalStateException ignored) { | ||
| // Cache the absence too; account-oriented callers supply their own fallback. | ||
| } | ||
| } | ||
| cache.put(playerName, new Cached(result, System.currentTimeMillis() + 30_000)); | ||
| return result; | ||
| } | ||
| } | ||
33 changes: 33 additions & 0 deletions
33
src/main/java/net/tfminecraft/simplefactions/espionage/CommandIntelligence.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| package net.tfminecraft.simplefactions.espionage; | ||
|
|
||
| import java.util.function.Supplier; | ||
| import org.bukkit.entity.Player; | ||
|
|
||
| /** Only a command creating a faction/guild menu can request new daily intelligence. */ | ||
| public final class CommandIntelligence { | ||
| private static final ThreadLocal<Context> current = new ThreadLocal<>(); | ||
| private static final class Context { | ||
| final Player player; | ||
| boolean gathered; | ||
| Context(Player player) { this.player = player; } | ||
| } | ||
| private CommandIntelligence() {} | ||
|
|
||
| public static boolean execute(Player player, Supplier<Boolean> command) { | ||
| Context previous = current.get(); | ||
| current.set(new Context(player)); | ||
| try { return command.get(); } | ||
| finally { | ||
| if (previous == null) current.remove(); | ||
| else current.set(previous); | ||
| } | ||
| } | ||
|
|
||
| public static void beforeMenu() { | ||
| Context context = current.get(); | ||
| if (context != null && context.player != null && !context.gathered) { | ||
| context.gathered = true; | ||
| EspionageService.refreshReports(context.player); | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.