|
| 1 | +package net.tfminecraft.simplefactions.guild.network; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.HashMap; |
| 5 | +import java.util.IdentityHashMap; |
| 6 | +import java.util.List; |
| 7 | +import java.util.Map; |
| 8 | +import java.util.Optional; |
| 9 | + |
| 10 | +import org.bukkit.Bukkit; |
| 11 | + |
| 12 | +import net.tfminecraft.simplefactions.Cache; |
| 13 | +import net.tfminecraft.simplefactions.SimpleFactions; |
| 14 | +import net.tfminecraft.simplefactions.guild.hub.VehicleFrameworkTracks; |
| 15 | +import net.tfminecraft.simplefactions.guild.network.RailRoutes.Route; |
| 16 | +import net.tfminecraft.simplefactions.guild.network.TradeGraphBuilder.ProvinceData; |
| 17 | +import net.tfminecraft.simplefactions.guild.network.TradeGraphBuilder.Site; |
| 18 | +import net.tfminecraft.simplefactions.installation.Installation; |
| 19 | +import net.tfminecraft.simplefactions.installation.InstallationKind; |
| 20 | +import net.tfminecraft.simplefactions.loaders.InstallationConfigLoader; |
| 21 | +import net.tfminecraft.simplefactions.managers.FactionManager; |
| 22 | +import net.tfminecraft.simplefactions.managers.ProvinceManager; |
| 23 | +import net.tfminecraft.simplefactions.map.ProvinceGrid; |
| 24 | +import net.tfminecraft.simplefactions.map.provinces.Province; |
| 25 | +import net.tfminecraft.simplefactions.objects.Faction; |
| 26 | + |
| 27 | +/** Takes the server-thread data needed by the pure builder. */ |
| 28 | +final class LiveTradeGraph { |
| 29 | + private static final RailRouteCache routes = new RailRouteCache(System::currentTimeMillis); |
| 30 | + |
| 31 | + private LiveTradeGraph() { } |
| 32 | + |
| 33 | + static TradeGraph build(ProvinceManager provinces) { |
| 34 | + List<Site> sites = new ArrayList<>(); |
| 35 | + Map<Installation, String> identities = new IdentityHashMap<>(); |
| 36 | + if (FactionManager.factions != null) { |
| 37 | + for (Faction faction : FactionManager.factions) { |
| 38 | + if (faction == null || faction.getId() == null || faction.getInstallationHandler() == null) continue; |
| 39 | + // Pending construction is separate from getAll(), as in SupplyHubService.findInstallation. |
| 40 | + for (Installation installation : faction.getInstallationHandler().getAll()) { |
| 41 | + if (installation == null || installation.getKind() == InstallationKind.FORT) continue; |
| 42 | + sites.add(new Site(faction.getId(), installation, InstallationConfigLoader.getHubSlots( |
| 43 | + installation.getKind(), installation.getLevel()), true)); |
| 44 | + identities.put(installation, |
| 45 | + faction.getId() + ":" + installation.getProvince() + ":" + installation.getId()); |
| 46 | + } |
| 47 | + } |
| 48 | + } |
| 49 | + Map<Integer, ProvinceData> map = new HashMap<>(); |
| 50 | + for (Province province : provinces.getProvinces()) { |
| 51 | + if (province.isValid()) { |
| 52 | + map.put(province.getId(), new ProvinceData(province.getTerrain(), province.getNeighbours())); |
| 53 | + } |
| 54 | + } |
| 55 | + SimpleFactions plugin = SimpleFactions.getInstance(); |
| 56 | + ProvinceGrid grid = plugin == null ? null : plugin.getProvinceGrid(); |
| 57 | + String world = Cache.worldName; |
| 58 | + return TradeGraphBuilder.build(sites, map, (from, to) -> { |
| 59 | + String left = identities.get(from); |
| 60 | + String right = identities.get(to); |
| 61 | + if (left == null || right == null) return Optional.empty(); |
| 62 | + return routes.route(left, right, () -> trackRoute(world, from, to)); |
| 63 | + }, point -> grid == null ? 0 : grid.getAt( |
| 64 | + (int) Math.floor(point.x()), (int) Math.floor(point.z()))); |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * The plugin check sits outside {@link VehicleFrameworkTracks}, matching hub distances. |
| 69 | + * A missing or older plugin must not fail the refresh. |
| 70 | + */ |
| 71 | + private static Optional<Route> trackRoute(String world, Installation from, Installation to) { |
| 72 | + try { |
| 73 | + if (!Bukkit.getPluginManager().isPluginEnabled("VehicleFramework")) return Optional.empty(); |
| 74 | + return VehicleFrameworkTracks.route(world, from, to); |
| 75 | + } catch (RuntimeException | LinkageError e) { |
| 76 | + return Optional.empty(); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + static void forgetRoutes() { |
| 81 | + routes.forget(); |
| 82 | + } |
| 83 | +} |
0 commit comments