Repository navigation
Read each realm's bankruptcy once per trade recalculation #202
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
5 commits
Select commit
Hold shift + click to select a range
4dc6668
Stop guild menus from stalling the server with trade recalculations
XxFran10xX 500ce78
Keep every zero-income guild menu recalculating and copy lists off-th…
XxFran10xX a0aff74
Let the first guild view after a restart recalculate again
XxFran10xX 506a959
Read each realm's treasury once per recalculation, not its whole stab…
XxFran10xX 076002d
Keep treasury answers only on the server thread with an open ledger
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
77 changes: 77 additions & 0 deletions
77
src/main/java/net/tfminecraft/simplefactions/government/stability/BankruptcyMemo.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,77 @@ | ||
| package net.tfminecraft.simplefactions.government.stability; | ||
|
|
||
| import java.util.IdentityHashMap; | ||
| import java.util.Map; | ||
|
|
||
| import org.bukkit.Bukkit; | ||
|
|
||
| import net.tfminecraft.denareconomy.DenarEconomy; | ||
| import net.tfminecraft.denareconomy.ledger.Ledger; | ||
| import net.tfminecraft.simplefactions.guild.Guild; | ||
|
|
||
| /** | ||
| * Whether each realm guild is bankrupt, read once per trade recalculation on the server thread. | ||
| * The flood asks for a province owner's stability once per foreign guild it reaches, and each ask | ||
| * reads the realm guild's treasury from the ledger. During a run on the server thread nothing | ||
| * else can post to a treasury or remove a bank, and nothing in a recalculation does, so every ask | ||
| * in that run gets the same answer. Everything else in stability, including unrest that decays | ||
| * over time, is still read live on every ask. | ||
| * | ||
| * <p>Runs off the server thread (async previews) read live as before: the server can change a | ||
| * guild's bank while they run. If the ledger is closed, every ask reads live too, because a | ||
| * closed ledger reads every balance as zero. | ||
| */ | ||
| public final class BankruptcyMemo { | ||
| private static final ThreadLocal<Map<Guild, Boolean>> RUN = new ThreadLocal<>(); | ||
|
|
||
| private BankruptcyMemo() {} | ||
|
|
||
| /** What {@link #begin} replaced, so {@link #end} can put it back. */ | ||
| public static final class Run { | ||
| private final Map<Guild, Boolean> outer; | ||
|
|
||
| private Run(Map<Guild, Boolean> outer) { | ||
| this.outer = outer; | ||
| } | ||
| } | ||
|
|
||
| /** Starts a run on this thread. A nested run, such as a preview, gets its own answers. */ | ||
| public static Run begin() { | ||
| Run run = new Run(RUN.get()); | ||
| if (onServerThread()) RUN.set(new IdentityHashMap<>()); | ||
| else RUN.remove(); | ||
| return run; | ||
| } | ||
|
|
||
| public static void end(Run run) { | ||
| if (run.outer == null) RUN.remove(); | ||
| else RUN.set(run.outer); | ||
| } | ||
|
|
||
| /** {@link Guild#isBankrupt()}, read once per guild in a server-thread run and live otherwise. */ | ||
| public static boolean isBankrupt(Guild guild) { | ||
| Map<Guild, Boolean> run = RUN.get(); | ||
| if (run == null) return guild.isBankrupt(); | ||
| if (!ledgerOpen()) { | ||
| // A failed read closes the ledger for good, and answers from before it are stale. | ||
| run.clear(); | ||
| return guild.isBankrupt(); | ||
| } | ||
| Boolean bankrupt = run.get(guild); | ||
| if (bankrupt == null) { | ||
| bankrupt = guild.isBankrupt(); | ||
| // Keep only answers the ledger stayed open for; this very read may have closed it. | ||
| if (ledgerOpen()) run.put(guild, bankrupt); | ||
| } | ||
| return bankrupt; | ||
| } | ||
|
|
||
| private static boolean onServerThread() { | ||
| return Bukkit.getServer() != null && Bukkit.isPrimaryThread(); | ||
| } | ||
|
|
||
| private static boolean ledgerOpen() { | ||
| Ledger ledger = DenarEconomy.getLedger(); | ||
| return ledger != null && ledger.isOpen(); | ||
| } | ||
| } |
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
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.