CAMPAIGN TRADING / JAVA SOURCE
SettlementTradingCampaignMod.java
src/settlementtradingcampaign/SettlementTradingCampaignMod.java
Preserved original · 4,984 bytesDownload original file
package settlementtradingcampaign;
import forgottentowns.mod.api.*;
import java.util.*;
/** Campaign add-on loading authored settlements from its captured JSON table. */
public final class SettlementTradingCampaignMod implements GameMod {
/** Creates the campaign settlement trading entry point. */
public SettlementTradingCampaignMod() { }
/** Registers campaign markets without importing campaign or engine implementation classes.
* @param context startup API including the manifest-listed markets table
* @throws IllegalArgumentException if the required core is unavailable or an authored market is malformed
*/
@Override public void register(ModContext context) {
var core=context.requireSettlementTradeCore("settlement_trading");
var economy=core.economy(); var production=core.productionRules();
Object table = context.dataTable("markets");
if (!(table instanceof List<?> entries)) throw new IllegalArgumentException("markets must be an array");
List<UnlockedMarket> markets = new ArrayList<>();
Set<String> ids = new HashSet<>(), stages = new HashSet<>();
for (Object entry : entries) {
if (!(entry instanceof Map<?, ?> data) || !(data.get("exports") instanceof Map<?, ?> exports))
throw new IllegalArgumentException("Each market requires exports");
if(!data.keySet().equals(Set.of("id","stage","nameKey","biome","exports","gold")))throw new IllegalArgumentException("Unknown or missing campaign market field");
Map<String, Integer> goods = new TreeMap<>();
exports.forEach((id, value) -> goods.put(id.toString(), quantity(value)));
String id = text(data, "id"), stage = text(data, "stage"), name = text(data, "nameKey"), biome = text(data, "biome");
if (!id.matches("settlement_trading_campaign:[a-z][a-z0-9_]*") || !ids.add(id)
|| !stage.matches("c[0-9]+_[a-z0-9_]+") || !stages.add(stage)
|| !name.startsWith("mods.settlement_trading_campaign.") || goods.size() > economy.maximumExports()
|| !Set.of("MEADOWS", "RANDOM", "JUNGLE", "DESERT", "SNOW", "MOUNTAINS").contains(biome))
throw new IllegalArgumentException("Invalid or duplicate campaign market " + id);
markets.add(new UnlockedMarket(stage, new SettlementTrading.Market(id, name, biome, goods, stage, quantity(data.get("gold")))));
}
context.onRegistriesReady(registries -> {
for (var entry : markets) for (String resource : entry.market().exports().keySet())
if (registries.find("resources", resource).isEmpty()) throw new IllegalArgumentException("Unknown campaign trade resource " + resource);
});
context.registerSettlementTrading(new Rules(List.copyOf(markets),economy,production));
}
private static String text(Map<?, ?> data, String key) {
if (!(data.get(key) instanceof String value) || value.isBlank()) throw new IllegalArgumentException("Missing market " + key);
return value;
}
private static int quantity(Object value) {
if (!(value instanceof Number number) || !Double.isFinite(number.doubleValue())
|| number.doubleValue() < 0 || number.doubleValue() > 100_000 || number.doubleValue() != number.intValue())
throw new IllegalArgumentException("Market quantities must be integers within 0..100000");
return number.intValue();
}
private record UnlockedMarket(String stage, SettlementTrading.Market market) { }
private record Rules(List<UnlockedMarket> authored,TradeEconomy economy,TradeProductionRules productionRules) implements SettlementTrading {
@Override public int maximumExports() { return economy.maximumExports(); }
@Override public int price(String id,int base,boolean buying) { return economy.price(base,buying); }
@Override public boolean supports(World world) { return !world.campaignStage().isBlank() && !world.campaignProfile().isBlank(); }
@Override public boolean includesSavedTowns(World world) { return false; }
@Override public Map<String, Integer> estimate(World world, Production production) {
return SettlementTrading.recordedExports(production, economy.maximumEstimate());
}
@Override public boolean configurableExports(World world) { return true; }
@Override public List<Market> markets(World world) {
return authored.stream().filter(m -> !m.stage().equals(world.campaignStage()) && world.completedStages().contains(m.stage()))
.map(UnlockedMarket::market).toList();
}
@Override public int travelDays(String biome) {
return economy.travelDays(biome);
}
@Override public int seasonalGold(int roll) { return economy.seasonalGold(roll); }
}
}