Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,26 @@ public static double infrastructureWorth(String realmId) {
return 0;
}

/** False before the first daily pass, so a menu can avoid showing a missing figure as +0. */
public static boolean hasInfrastructureWorth(Faction realm) {
return realm != null && hasInfrastructureWorth(realm.getId());
}

public static boolean hasInfrastructureWorth(String realmId) {
if (realmId == null) {
return false;
}
if (headlines.containsKey(realmId)) {
return true;
}
for (String key : headlines.keySet()) {
if (realmId.equalsIgnoreCase(key)) {
return true;
}
}
return false;
}

public static Terms applyTerms(Destination destination, int ratePercent, long feeCents) {
if (destination == null) {
return new Terms(0, 0, 0, 0);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package net.tfminecraft.simplefactions.guild.hub;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

/** Lore for the realm headline and the installation confirmation. */
public final class InfrastructureMenuCopy {
private InfrastructureMenuCopy() {}

public static String headline(double worth) {
return "§7Infrastructure is worth about " + signed(worth) + " a day to your realm";
}

public static String headlineUnknown() {
return "§7Infrastructure worth is worked out once a day.";
}

public static String calculating() {
return "§7Working out what this would earn...";
}

public static List<String> installationPreview(HubEstimates.InstallationPreview preview) {
if (preview == null) {
return List.of(calculating());
}
List<String> lines = new ArrayList<>();
if (preview.infrastructureHere() > 0) {
lines.add("§a" + signed(preview.infrastructureHere()) + " infrastructure here");
} else {
lines.add("§7This adds no infrastructure here");
}
lines.add("§7about " + signed(preview.realmPerDay()) + " a day for your realm");
if (preview.upkeep() > 0) {
lines.add("§7upkeep " + compact(preview.upkeep()));
}
return lines;
}

static String signed(double value) {
String number = compact(Math.abs(value));
if (value < 0) {
return "-" + number;
}
return "+" + number;
}

static String compact(double value) {
double rounded = Math.round(value * 100.0) / 100.0;
if (Math.abs(rounded - Math.rint(rounded)) < 0.001) {
return Long.toString((long) Math.rint(rounded));
}
return String.format(Locale.ROOT, "%.2f", rounded);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
import net.tfminecraft.simplefactions.guild.GuildModifierOverride;
import net.tfminecraft.simplefactions.guild.branch.Branch;
import net.tfminecraft.simplefactions.guild.branch.BranchModifier;
import net.tfminecraft.simplefactions.managers.FactionManager;
import net.tfminecraft.simplefactions.managers.ProvinceManager;
import net.tfminecraft.simplefactions.managers.RelationManager;
import net.tfminecraft.simplefactions.objects.Faction;

/**
Expand Down Expand Up @@ -65,6 +67,29 @@ public static double estimate(
return Math.round(net * 100.0) / 100.0;
}

/**
* The realm guild's infrastructure reaches every guild in the realm, so an
* up or down of that branch is their incomes added together.
*/
public static boolean showsRealm(Guild guild, Branch branch) {
return guild != null
&& guild.isBase()
&& branch != null
&& branch.getModifier(GuildModifier.INFRASTRUCTURE) != null;
}

public static double estimateRealm(
Prepared prepared,
Guild realmGuild,
Map<GuildModifier, Double> current,
Map<GuildModifier, Double> hypothetical) {
double before = realmIncome(prepared, realmGuild, current);
double after = realmIncome(prepared, realmGuild, hypothetical);
double upkeepChange = Math.max(0.0, hypothetical.getOrDefault(GuildModifier.INFRASTRUCTURE_UPKEEP, 0.0))
- Math.max(0.0, current.getOrDefault(GuildModifier.INFRASTRUCTURE_UPKEEP, 0.0));
return Math.round((after - before - upkeepChange) * 100.0) / 100.0;
}

public static Map<GuildModifier, Double> modifiers(Guild guild) {
EnumMap<GuildModifier, Double> amounts = new EnumMap<>(GuildModifier.class);
for (GuildModifier modifier : GuildModifier.values()) {
Expand Down Expand Up @@ -105,6 +130,39 @@ private static ProvinceManager copy(ProvinceManager source) {
return snapshot;
}

private static double realmIncome(
Prepared prepared, Guild realmGuild, Map<GuildModifier, Double> amounts) {
ProvinceManager snapshot = copy(prepared.snapshot);
GuildModifierOverride.use(realmGuild, amounts);
try {
snapshot.recalculateQuiet();
double sum = 0;
Faction realm = realmGuild.getFaction();
for (Guild guild : FactionManager.getAllGuilds()) {
if (guild == null || !sameRealm(realm, guild.getFaction())) {
continue;
}
sum += snapshot.getIncome(guild, false) * (1.0 - taxFraction(guild));
}
return sum;
} finally {
GuildModifierOverride.clear();
}
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.

private static boolean sameRealm(Faction realm, Faction guildFaction) {
if (realm == null || guildFaction == null) {
return false;
}
if (realm == guildFaction) {
return true;
}
if (realm.getId() != null && realm.getId().equalsIgnoreCase(guildFaction.getId())) {
return true;
}
return RelationManager.sameRealm(realm, guildFaction);
}

private static double income(ProvinceManager snapshot, Guild guild, Map<GuildModifier, Double> amounts) {
GuildModifierOverride.use(guild, amounts);
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import net.tfminecraft.simplefactions.installation.Installation;
import net.tfminecraft.simplefactions.installation.InstallationKind;
import net.tfminecraft.simplefactions.installation.handler.ConstructResult;
import net.tfminecraft.simplefactions.managers.inventory.InstallationBuildConfirm;
import net.tfminecraft.simplefactions.settlement.handler.CapitalResult;
import net.tfminecraft.simplefactions.tiers.Title;
import net.tfminecraft.simplefactions.tiers.admin.TitleAdminCommand;
Expand Down Expand Up @@ -623,15 +624,15 @@ private boolean dispatchCommand(CommandSender sender, Command cmd, String label,
return true;
}
String name = String.join(" ", Arrays.copyOfRange(args, 2, args.length));
ConstructResult result = f.getInstallationHandler().construct(
if (InstallationBuildConfirm.open(
p,
f,
kind,
name,
province,
p.getLocation().getBlockX(),
p.getLocation().getBlockZ());
p.sendMessage(result.getMessage());
if(result.isSuccess()) {
p.playSound(p, Sound.BLOCK_ANVIL_USE, 1f, 1f);
p.getLocation().getBlockZ())) {
p.playSound(p, Sound.BLOCK_NOTE_BLOCK_BIT, 1f, 1f);
}
return true;
} else if(cmd.getName().equalsIgnoreCase(cmd1) && args[0].equalsIgnoreCase("deconstruct")) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import net.tfminecraft.simplefactions.managers.holder.SFInventoryHolder;
import net.tfminecraft.simplefactions.managers.holder.WarInventoryHolder;
import net.tfminecraft.simplefactions.managers.inventory.CampaignInstallationPickView;
import net.tfminecraft.simplefactions.managers.inventory.InstallationBuildConfirm;
import net.tfminecraft.simplefactions.managers.inventory.CampaignRaidLaunchView;
import net.tfminecraft.simplefactions.managers.inventory.CampaignView;
import net.tfminecraft.simplefactions.managers.inventory.CompanyView;
Expand Down Expand Up @@ -1001,6 +1002,8 @@ public void clearPendingOnQuit(PlayerQuitEvent e) {
if (pendingCompanyFounds.remove(e.getPlayer()) != null) {
confirming.remove(e.getPlayer());
}
InstallationBuildConfirm.forget(e.getPlayer());
confirming.remove(e.getPlayer());
slotChanges.remove(e.getPlayer());
}

Expand Down Expand Up @@ -1477,6 +1480,17 @@ public void clickButton(InventoryClickEvent e) {
}
return;
}
key = new NamespacedKey(SimpleFactions.plugin, "installation_build");
data = m.getPersistentDataContainer().get(key, PersistentDataType.STRING);
if (data != null) {
if (item.getType().equals(Material.RED_CONCRETE)) {
InstallationBuildConfirm.cancel(p);
} else {
InstallationBuildConfirm.accept(p);
}
confirming.remove(p);
return;
}
key = new NamespacedKey(SimpleFactions.plugin, "installation");
data = m.getPersistentDataContainer().get(key, PersistentDataType.STRING);
if(data != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,28 @@ public void recalculate() {
for(Guild guild : FactionManager.getAllGuilds()) getIncome(guild);
}

/**
* Same flood as {@link #recalculate()} without writing guild trade breakdowns.
* A realm-wide branch preview runs this off the server thread.
*/
public void recalculateQuiet() {
if (!Cache.provincesEnabled) {
return;
}
HubNetwork.refreshIfLive(this);
recalculateInfrastructure();
dropMissingGuilds();
for (Guild g : FactionManager.getAllGuilds()) {
if (!g.hasCapital()) continue;
recalculateGuild(g);
}
for (Guild g : FactionManager.getAllGuilds()) {
if (!g.hasCapital()) continue;
recalculateProduction(g);
}
recalculateProsperity();
}

public void recalculateForSingleGuild(Guild g, boolean save) {
if (!Cache.provincesEnabled) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,9 @@ public static void schedule(
Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> {
double delta;
try {
delta = BranchIncomePreview.estimate(prepared, guild, current, hypothetical, taxFraction);
delta = BranchIncomePreview.showsRealm(guild, branch)
? BranchIncomePreview.estimateRealm(prepared, guild, current, hypothetical)
: BranchIncomePreview.estimate(prepared, guild, current, hypothetical, taxFraction);
} catch (RuntimeException ex) {
plugin.getLogger().log(
Level.WARNING,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,11 @@
import net.tfminecraft.simplefactions.managers.RelationManager;
import net.tfminecraft.simplefactions.objects.Faction;
import net.tfminecraft.simplefactions.rest.BannerFetcher;
import net.tfminecraft.simplefactions.Cache;
import net.tfminecraft.simplefactions.SimpleFactions;
import net.tfminecraft.simplefactions.guild.Guild;
import net.tfminecraft.simplefactions.guild.hub.HubEstimates;
import net.tfminecraft.simplefactions.guild.hub.InfrastructureMenuCopy;
import net.tfminecraft.simplefactions.utils.FactionRanker;
import net.tfminecraft.simplefactions.enums.MenuItemType;
import net.tfminecraft.simplefactions.enums.RankType;
Expand Down Expand Up @@ -129,6 +132,9 @@ public void factionView(Player player, Faction f, Inventory i) {
i.setItem(11, creator.createMenuItem(player, f, MenuItemType.GOVERNMENT));
i.setItem(12, creator.createMenuItem(player, f, MenuItemType.WEALTH));
i.setItem(13, creator.createMenuItem(player, f, MenuItemType.PRESTIGE));
if (Cache.provincesEnabled) {
i.setItem(21, infrastructureHeadline(f));
}
i.setItem(14, creator.createMenuItem(player, f, MenuItemType.MEMBERS));
i.setItem(23, creator.createMenuItem(player, f, MenuItemType.GUILDS));
String hubOffers = net.tfminecraft.simplefactions.guild.hub.HubAgreementService.joinSummary(player.getName());
Expand All @@ -151,6 +157,19 @@ public void factionView(Player player, Faction f, Inventory i) {
if(open) player.openInventory(i);
}

@SuppressWarnings("deprecation")
private ItemStack infrastructureHeadline(Faction faction) {
ItemStack item = new ItemStack(Material.RAIL);
ItemMeta meta = item.getItemMeta();
meta.setDisplayName("§6Infrastructure");
String line = HubEstimates.hasInfrastructureWorth(faction)
? InfrastructureMenuCopy.headline(HubEstimates.infrastructureWorth(faction))
: InfrastructureMenuCopy.headlineUnknown();
meta.setLore(List.of(line));
item.setItemMeta(meta);
return item;
}

// Keep the existing legacy text representation, formatting, and exact-string comparisons.
@SuppressWarnings("deprecation")
public void factionGuildsView(Player player, Faction f, Inventory i) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import net.tfminecraft.simplefactions.SimpleFactions;
import net.tfminecraft.simplefactions.guild.branch.Branch;
import net.tfminecraft.simplefactions.guild.branch.BranchModifier;
import net.tfminecraft.simplefactions.guild.income.BranchIncomePreview;
import net.tfminecraft.simplefactions.guild.income.Cashflow;
import net.tfminecraft.simplefactions.guild.income.Ledger;
import net.tfminecraft.simplefactions.guild.income.LedgerHistory;
Expand Down Expand Up @@ -348,7 +349,7 @@ private List<String> upgradeLore(Guild guild, Branch branch, Double delta) {
lore.add("");
lore.add(StringFormatter.formatHex("#d4c9aeCurrent Net Trade Income: #7fbd73"+net.tfminecraft.simplefactions.utils.Formatter.formatDouble(
guild.getTradeBreakdown().getNetTradeIncome() - guild.getHubTaxBreakdown().getTotalTax())));
lore.add(incomeChangeLine(delta));
lore.add(incomeChangeLine(delta, BranchIncomePreview.showsRealm(guild, branch)));
}
lore.add("");
lore.add(StringFormatter.formatHex("#50e846§lClick to Upgrade"));
Expand Down Expand Up @@ -395,7 +396,7 @@ private List<String> downgradeLore(Guild guild, Branch branch, Double delta) {
} else {
lore.add(StringFormatter.formatHex("#d4c9aeCurrent Net Trade Income: #7fbd73"+net.tfminecraft.simplefactions.utils.Formatter.formatDouble(
guild.getTradeBreakdown().getNetTradeIncome() - guild.getHubTaxBreakdown().getTotalTax())));
lore.add(incomeChangeLine(delta));
lore.add(incomeChangeLine(delta, BranchIncomePreview.showsRealm(guild, branch)));
}
lore.add("");
lore.add(StringFormatter.formatHex(
Expand All @@ -406,15 +407,16 @@ private List<String> downgradeLore(Guild guild, Branch branch, Double delta) {
return lore;
}

private String incomeChangeLine(Double delta) {
String incomeChangeLine(Double delta, boolean realm) {
String label = realm ? "Estimated Realm Income Change" : "Estimated Income Change";
if (delta == null) {
return StringFormatter.formatHex("#f2e5c2Estimated Income Change#d6cf69: #7a706aCalculating...");
return StringFormatter.formatHex("#f2e5c2" + label + "#d6cf69: #7a706aCalculating...");
}
if (delta.isNaN()) {
return StringFormatter.formatHex("#cf493aIncome estimate unavailable");
}
return StringFormatter.formatHex(
"#f2e5c2Estimated Income Change#d6cf69: "
"#f2e5c2" + label + "#d6cf69: "
+ (delta >= 0 ? "#4fd945+" : "#cf493a")
+ String.format("%.2f", delta)
+ "d/day"
Expand Down
Loading
Loading