Repository navigation
Expand file tree
/
Copy pathDatabase.java
More file actions
750 lines (651 loc) · 30.5 KB
/
Copy pathDatabase.java
File metadata and controls
750 lines (651 loc) · 30.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
package net.tfminecraft.simplefactions.database;
import net.tfminecraft.simplefactions.identity.LeaderCharacters;
import net.tfminecraft.simplefactions.war.battle.engine.core.Battle;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.bukkit.Bukkit;
import org.bukkit.Chunk;
import net.tfminecraft.simplefactions.army.Military;
import net.tfminecraft.simplefactions.army.MilitaryExpansion;
import net.tfminecraft.simplefactions.army.Regiment;
import net.tfminecraft.simplefactions.loaders.RegimentLoader;
import net.tfminecraft.simplefactions.guild.branch.Branch;
import net.tfminecraft.simplefactions.guild.loans.Loan;
import net.tfminecraft.simplefactions.guild.upgrade.Upgrade;
import net.tfminecraft.simplefactions.guild.upgrade.UpgradeExpansion;
import net.tfminecraft.simplefactions.guild.Guild;
import net.tfminecraft.simplefactions.guild.income.LedgerHistory;
import net.tfminecraft.simplefactions.loaders.BranchLoader;
import net.tfminecraft.simplefactions.loaders.RankLoader;
import net.tfminecraft.simplefactions.loaders.TitleLoader;
import net.tfminecraft.simplefactions.loaders.UpgradeLoader;
import net.tfminecraft.simplefactions.managers.FactionManager;
import net.tfminecraft.simplefactions.managers.LogManager;
import net.tfminecraft.simplefactions.managers.RelationManager;
import net.tfminecraft.simplefactions.objects.Bank;
import net.tfminecraft.simplefactions.objects.Faction;
import net.tfminecraft.simplefactions.objects.Modifier;
import net.tfminecraft.simplefactions.objects.PrestigeRank;
import net.tfminecraft.simplefactions.tiers.Title;
import net.tfminecraft.simplefactions.war.core.War;
import net.tfminecraft.simplefactions.war.core.WarMapper;
import net.tfminecraft.simplefactions.war.resolution.WarReparationsObligation;
import net.tfminecraft.simplefactions.enums.Stance;
import net.tfminecraft.simplefactions.government.StabilityModifier;
import net.tfminecraft.simplefactions.laws.Law;
import net.tfminecraft.simplefactions.laws.LawGroup;
public class Database {
public int getTimer() {
try {
File file = new File("plugins/SimpleFactions/Cache", "data.json");
if (!file.exists()) return 0;
TimerData data = JsonUtil.readJson(file, TimerData.class);
return data != null ? data.time : 0;
} catch (Exception e) {
e.printStackTrace();
return 0;
}
}
public int getDay() {
try {
File file = new File("plugins/SimpleFactions/Cache", "data.json");
if (!file.exists()) return 0;
TimerData data = JsonUtil.readJson(file, TimerData.class);
return data != null ? data.day : 0;
} catch (Exception e) {
e.printStackTrace();
return 0;
}
}
public void saveTimer(int time, int day) {
try {
File folder = new File("plugins/SimpleFactions/Cache");
if (!folder.exists()) folder.mkdirs();
File file = new File(folder, "data.json");
TimerData data = new TimerData();
data.time = time;
data.day = day;
JsonUtil.writeJson(file, data);
} catch (Exception e) {
e.printStackTrace();
}
}
/* =====================================================
* FACTIONS
* ===================================================== */
public void loadFactions() {
Bukkit.getLogger().info("[SimpleFactions] Loading factions (Gson)");
File folder = new File("plugins/SimpleFactions/Data");
if (!folder.exists()) folder.mkdirs();
// Suppressed for the whole load: each faction and guild calls updateWealth, which
// cascades into a full-server prestige pass over a partial list. FactionManager.run
// recomputes once everything is in.
File[] files = folder.listFiles();
if (files == null) {
throw new IllegalStateException("Cannot read faction directory: " + folder.getAbsolutePath());
}
boolean wasLoading = FactionManager.loading;
FactionManager.loading = true;
try {
for (File file : files) {
if (!file.getName().endsWith(".json")) continue;
try {
FactionData data = JsonUtil.readJson(file, FactionData.class);
if (data == null || data.id == null) continue;
// --- Provinces ---
List<Integer> provinces = new ArrayList<>();
for (Number n : data.provinces) provinces.add(n.intValue());
// --- Titles ---
List<Title> titles = new ArrayList<>();
for (String tid : data.titles) {
Title t = TitleLoader.getById(tid);
if (t != null) titles.add(t);
}
int capital = data.capital != null ? data.capital : -1;
int extraCap = data.extraNodeCapacity != null ? data.extraNodeCapacity.intValue() : 0;
double citizenTax = data.citizenTax != null ? data.citizenTax : 5.0;
double guildTax = data.guildTax != null ? data.guildTax : 5.0;
double vassalTax = data.vassalTax != null ? data.vassalTax : 5.0;
double dividendTax = data.dividendTax != null ? data.dividendTax : 5.0;
double tariffs = data.tariffs != null ? data.tariffs : 5.0;
// Deep copy specific taxes
HashMap<String, HashMap<String, Double>> specificTaxes = new HashMap<>();
if (data.specificTaxes != null) {
for (HashMap.Entry<String, HashMap<String, Double>> entry : data.specificTaxes.entrySet()) {
if (entry.getValue() != null) {
specificTaxes.put(entry.getKey(), new HashMap<>(entry.getValue()));
}
}
}
Faction f = new Faction(
data.id,
data.rgb,
provinces,
titles,
data.leader,
data.name,
data.rulerTitle,
data.banner,
data.government,
data.culture,
data.religion,
extraCap,
loadModifiers(data.prestigeModifiers),
citizenTax,
guildTax,
vassalTax,
dividendTax,
tariffs,
specificTaxes,
capital,
data.laws,
data.governmentData
);
f.getVehicleFeeHandler().load(data.vehicleFees, data.vehicleTypeFees);
if (data.lawChangedAt != null) {
for (Map.Entry<String, Long> entry : data.lawChangedAt.entrySet()) {
LawGroup group = f.getLawHandler().getGroup(entry.getKey());
if (group != null && entry.getValue() != null) group.setChangedAt(entry.getValue());
}
}
// --- Rank / founding ---
// Rank is derived state that only climbs one level per updatePrestige, so it
// has to be restored rather than re-derived from a cold ladder on every boot.
if (data.rank != null) {
PrestigeRank restored = RankLoader.getByString(data.rank);
if (restored != null) f.setRank(restored);
}
f.setFoundedAt(data.foundedAt != null ? data.foundedAt : System.currentTimeMillis()/1000L);
f.setCapitalMoves(data.capitalMoves != null ? data.capitalMoves : 0);
f.setEspionage(data.espionage);
f.rememberLeaderCharacter(data.leaderCharacter, data.leaderCharacterOf);
if (data.settlements != null) {
f.getSettlementHandler().load(data.settlements);
}
if (data.installations != null) {
f.getInstallationHandler().load(data.installations);
}
if (data.installationQueue != null) {
f.getInstallationHandler().loadConstruction(data.installationQueue);
}
// --- Relations ---
if (data.relations == null) {
LogManager.relations("JSON %s relations=null", f.getId());
} else {
LogManager.relations("JSON %s relations=%s", f.getId(), data.relations);
for (String r : data.relations) {
FactionManager.addDBRelation(f, r);
}
}
if(data.tradeRelations != null) {
for (String r : data.tradeRelations) {
FactionManager.addDBTradeRelation(f, r);
}
}
if(data.treatyRelations != null) {
for (String r : data.treatyRelations) {
FactionManager.addDBTreatyRelation(f, r);
}
}
// --- Tier ---
if (data.tierIndex != null) {
f.getTier().setIndex(data.tierIndex.intValue());
}
// --- Military ---
Military m = f.getMilitary();
for (String s : data.military) {
String[] split = s.split("\\.");
Regiment r = m.getRegiment(split[0]);
if (r == null) continue;
r.setCurrentSlots(Integer.parseInt(split[1]));
// "id.current.free"; older "id.current" saves always had the regiments.yml default free.
Regiment prototype = RegimentLoader.getByString(split[0]);
r.setFreeSlots(split.length > 2 ? Integer.parseInt(split[2])
: prototype == null ? 0 : prototype.getFreeSlots());
}
// Laws are already applied; overlord grants are re-derived once relations load.
m.refreshLawSlots();
for (String s : data.militaryQueue) {
String[] split = s.split("\\.");
m.addQueueItem(m.getRegiment(split[0]), Integer.parseInt(split[1]));
}
// --- Guild ---
if (data.guilds != null) {
for (GuildData gd : data.guilds) {
if(gd.loans != null) {
for (LoanData ld : gd.loans) {
FactionManager.addDBLoan(ld);
}
}
Guild g = new Guild(gd, f);
g.rememberLeaderCharacter(gd.leaderCharacter, gd.leaderCharacterOf);
// --- Bank ---
if ("true".equalsIgnoreCase(gd.bank)) {
Chunk c = Bukkit.getWorld(gd.world)
.getChunkAt(gd.xPos.intValue(), gd.zPos.intValue());
g.setBank(new Bank(g, gd.balance != null ? gd.balance : 0.0, c));
} else if ("false".equalsIgnoreCase(gd.bank)) {
g.setBank(null);
}
// --- Upgrade Queue ---
if (gd.upgradeQueue != null) {
for (UpgradeExpansionData ued : gd.upgradeQueue) {
Upgrade upgrade = g.getUpgrade(ued.upgrade);
if (upgrade != null) {
g.addQueuedUpgrade(upgrade, ued.timeLeft);
}
}
}
f.getGuildHandler().addGuild(g);
// Restored upgrades and persistent modifiers count even without a saved bank.
g.updateWealth();
}
}
if (data.warReparationsObligations != null) {
for (WarReparationsObligationData obligationData : data.warReparationsObligations) {
if (obligationData == null
|| obligationData.payeeFactionId == null
|| obligationData.payeeFactionId.isBlank()) {
continue;
}
double percent = obligationData.incomePercent != null ? obligationData.incomePercent : 0.0;
int days = obligationData.daysRemaining != null ? obligationData.daysRemaining : 0;
if (percent <= 0 || days <= 0) {
continue;
}
f.addWarReparationsObligation(new WarReparationsObligation(
obligationData.payeeFactionId, percent, days));
}
}
FactionManager.factions.add(f);
f.updateWealth();
} catch (Exception e) {
e.printStackTrace();
}
}
} finally {
FactionManager.loading = wasLoading;
}
}
/* =====================================================
* SAVE
* ===================================================== */
public void saveFaction(Faction f) {
saveFactionChecked(f);
}
/** Office mutations must not acknowledge success unless the faction save completed. */
public boolean saveFactionChecked(Faction f) {
try {
File file = new File("plugins/SimpleFactions/Data", f.getId() + ".json");
FactionData data = new FactionData();
data.id = f.getId();
data.name = f.getName();
data.rgb = f.getRGB();
data.leader = f.getLeader();
rememberLeaderCharacters(f);
data.leaderCharacter = f.getLeaderCharacter();
data.leaderCharacterOf = f.getLeaderCharacterOf();
data.rulerTitle = f.getRulerTitle();
data.government = f.getGovernmentString();
data.culture = f.getCulture();
data.religion = f.getReligion();
data.citizenTax = f.getTaxHandler().getCitizenTax();
data.guildTax = f.getTaxHandler().getGuildTax();
data.vassalTax = f.getTaxHandler().getVassalTax();
data.dividendTax = f.getTaxHandler().getDividendTax();
data.tariffs = f.getTaxHandler().getTariffs();
data.specificTaxes = serializeSpecificTaxes(f.getTaxHandler());
data.vehicleFees = new HashMap<>(f.getVehicleFeeHandler().serializeRates());
data.vehicleTypeFees = new HashMap<>();
for (Map.Entry<String, Map<String, Double>> entry
: f.getVehicleFeeHandler().serializeTypeRates().entrySet()) {
data.vehicleTypeFees.put(entry.getKey(), new HashMap<>(entry.getValue()));
}
data.capital = f.getCapital();
data.extraNodeCapacity = (double) f.getExtraNodeCapacity();
data.banner = new ArrayList<>(f.getBannerPatterns());
// --- Government ---
data.governmentData = f.getGovernment().serialize();
data.settlements = f.getSettlementHandler().serialize();
data.installations = f.getInstallationHandler().serialize();
data.installationQueue = f.getInstallationHandler().serializeConstruction();
for (int p : f.getProvinces()) data.provinces.add(p);
for (Title t : f.getTitles()) data.titles.add(t.getId());
// --- Military ---
for (Regiment r : f.getMilitary().getRegiments()) {
if (!r.isLevy()) {
data.military.add(r.getId() + "." + r.getCurrentSlots() + "." + r.getFreeSlots());
}
}
for (MilitaryExpansion e : f.getMilitary().getQueue()) {
data.militaryQueue.add(e.getRegiment().getId() + "." + e.getTimeLeft());
}
// --- Relations ---
f.getRelations().forEach((id, rel) ->
data.relations.add(id + "(" + rel.getType().getId() + "."
+ rel.getAttitude().getId() + "." + rel.getOpinion() + ")"));
LogManager.relations("SAVE %s relations=%s", f.getId(), data.relations);
// --- Trade Relations ---
f.getDiplomacyHandler().getTradeRelations().forEach((id, rel) ->
data.tradeRelations.add(id + "(" + rel.getId() + ")"));
f.getDiplomacyHandler().getTreatyRelations().forEach((id, rel) -> {
if (rel != null && rel.isTreaty() && !rel.isClearTreaty()) {
data.treatyRelations.add(id + "(" + rel.getId() + ")");
}
});
// --- Modifiers ---
f.getPrestigeModifiers().forEach(m -> {
if(m.isPersistent())
data.prestigeModifiers.add(m.getType() + "(" + m.getAmount() + ")");
});
// --- Rank / founding ---
data.rank = f.getRank() != null ? f.getRank().getId() : null;
data.foundedAt = f.getFoundedAt();
data.capitalMoves = f.getCapitalMoves();
data.espionage = f.getEspionage();
// --- Guild ---
for (Guild g : f.getGuildHandler().getGuilds()) {
GuildData gd = new GuildData();
gd.id = g.getId();
gd.name = g.getOwnName();
gd.leader = g.getLeader();
gd.leaderCharacter = g.getLeaderCharacter();
gd.leaderCharacterOf = g.getLeaderCharacterOf();
gd.rgb = g.getRGB();
gd.type = g.getType().getId();
gd.capital = g.getCapital();
gd.members = new ArrayList<>(g.getMembers());
gd.banner = new ArrayList<>(g.getBannerPatterns());
gd.stance = g.isBase() ? g.getFaction().getOverlord() != null ?
g.getStance(g.getFaction().getOverlord()).name() :
g.getStance(g.getFaction()).name() :
g.getStance(g.getFaction()).name();
gd.creditScore = g.getLoanHandler().getCreditScore();
gd.usedLoanOffers = g.getLoanHandler().getUsedOffers();
gd.repressed = g.isRepressed();
gd.favoured = g.isFavoured();
gd.dividendPercent = g.getDividendPercent();
gd.donation = g.getDonationAmount();
gd.dividendEligible = g.getDividendEligibleSnapshot();
gd.casinoProfit = g.getLedger().getCasinoProfit();
gd.vehicleFeeIncome = g.getLedger().getVehicleFeeIncome();
Map<String, Double> citizenTaxes = g.getLedger().getCitizenTaxesCopy();
gd.citizenTaxes = citizenTaxes.isEmpty() ? null : citizenTaxes;
LedgerHistory history = g.getLedger().getHistory();
Map<String, Map<String, Double>> lastDay = history.getLastDayCopy();
gd.ledgerLastDay = lastDay.isEmpty() ? null : lastDay;
Map<String, Map<String, Double>> lifetime = history.getLifetimeCopy();
gd.ledgerLifetime = lifetime.isEmpty() ? null : lifetime;
Map<String, Double> depositsToday = history.getDepositsTodayCopy();
gd.depositsToday = depositsToday.isEmpty() ? null : depositsToday;
gd.company = g.getCompany() != null ? g.getCompany().serialize() : null;
// --- Bank ---
if (g.getBank() != null) {
Bank b = g.getBank();
gd.bank = "true";
gd.world = b.getChunk().getWorld().getName();
gd.xPos = (double) b.getChunk().getX();
gd.zPos = (double) b.getChunk().getZ();
gd.balance = b.getWealth();
} else {
gd.bank = "false";
}
// --- Modifiers ---
g.getWealthModifiers().forEach(m -> {
if(m.isPersistent())
gd.wealthModifiers.add(m.getType() + "(" + m.getAmount() + ")");
});
gd.pillageHits = new ArrayList<>();
if (g.getPillageHits() != null) {
for (StabilityModifier modifier : g.getPillageHits()) {
if (modifier == null) {
continue;
}
StabilityModifierData modifierData = new StabilityModifierData();
modifierData.name = modifier.getName();
modifierData.modifier = modifier.getModifier();
modifierData.decay = modifier.getDecay();
gd.pillageHits.add(modifierData);
}
}
for (Map.Entry<Integer, Branch> e : g.getBranches().entrySet()) {
Branch b = e.getValue();
GuildBranchData bd = new GuildBranchData();
bd.id = b.getId();
bd.level = b.getLevel();
gd.branches.add(bd);
}
for (Upgrade u : g.getUpgrades()) {
GuildBranchData bd = new GuildBranchData();
bd.id = u.getId();
bd.level = u.getLevel();
gd.upgrades.add(bd);
}
for (UpgradeExpansion e : g.getUpgradeQueue()) {
UpgradeExpansionData ued = new UpgradeExpansionData();
ued.upgrade = e.getUpgrade().getId();
ued.timeLeft = e.getTimeLeft();
gd.upgradeQueue.add(ued);
}
for(Loan loan : g.getLoanHandler().getLoansGiven()) {
gd.loans.add(new LoanData(loan));
}
data.guilds.add(gd);
}
// --- Laws ---
for (LawGroup group : f.getLawHandler().getGroupList()) {
Law current = group.getCurrent();
if (current != null) {
data.laws.add(group.getId() + ":" + current.getId());
}
if (group.getChangedAt() > 0) {
data.lawChangedAt.put(group.getId(), group.getChangedAt());
}
}
data.overlord = RelationManager.getOverlord(f);
data.tierIndex = (double) f.getTier().getIndex();
data.warReparationsObligations = new ArrayList<>();
for (WarReparationsObligation obligation : f.getWarReparationsObligations()) {
if (obligation == null || !obligation.isActive()) {
continue;
}
WarReparationsObligationData obligationData = new WarReparationsObligationData();
obligationData.payeeFactionId = obligation.getPayeeFactionId();
obligationData.incomePercent = obligation.getIncomePercent();
obligationData.daysRemaining = obligation.getDaysRemaining();
data.warReparationsObligations.add(obligationData);
}
JsonUtil.writeJsonAtomic(file, data);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/* =====================================================
* HELPERS
* ===================================================== */
/**
* Brings the realm's and its guilds' leader characters up to date before
* they are written. A name learned here must still reach the map: the join
* refresh that runs later would find nothing new and leave nation.json
* stale, so the map is told now.
*/
static void rememberLeaderCharacters(Faction f) {
boolean changed = LeaderCharacters.refresh(f);
for (Guild g : f.getGuildHandler().getGuilds()) {
if (g != null) changed |= LeaderCharacters.refresh(g);
}
if (changed && FactionManager.getMap() != null) {
FactionManager.getMap().markLeaderNamesChanged();
}
}
public static List<Modifier> loadModifiers(List<String> raw) {
List<Modifier> list = new ArrayList<>();
for (String s : raw) {
String type = s.substring(0, s.indexOf("("));
double amt = Double.parseDouble(s.substring(s.indexOf("(") + 1, s.indexOf(")")));
list.add(new Modifier(type, amt, true));
}
return list;
}
private HashMap<String, HashMap<String, Double>> serializeSpecificTaxes(net.tfminecraft.simplefactions.objects.handler.TaxHandler taxHandler) {
HashMap<String, HashMap<String, Double>> result = new HashMap<>();
HashMap<net.tfminecraft.simplefactions.government.proposal.TaxTarget, HashMap<String, Double>> specificTaxes = taxHandler.getSpecificTaxes();
for (Map.Entry<net.tfminecraft.simplefactions.government.proposal.TaxTarget, HashMap<String, Double>> entry : specificTaxes.entrySet()) {
result.put(entry.getKey().name(), new HashMap<>(entry.getValue()));
}
return result;
}
public void deleteFaction(Faction f) {
File file = new File("plugins/SimpleFactions/Data", f.getId() + ".json");
if (file.exists()) file.delete();
}
public void saveWar(War war) {
try {
File folder = new File("plugins/SimpleFactions/Wars");
if (!folder.exists()) folder.mkdirs();
File file = new File(folder, "war_" + war.getId() + ".json");
WarData data = WarMapper.toData(war);
JsonUtil.writeJson(file, data);
} catch (Exception e) {
e.printStackTrace();
}
}
public List<War> loadWars() {
List<War> wars = new ArrayList<>();
File folder = new File("plugins/SimpleFactions/Wars");
if (!folder.exists() || !folder.isDirectory()) return wars;
File[] files = folder.listFiles();
if (files == null) return wars;
for (File file : files) {
if (!file.getName().endsWith(".json")) continue;
try {
WarData data = JsonUtil.readJson(file, WarData.class);
if (data == null) continue;
War war = WarMapper.fromData(data);
if (war != null) {
wars.add(war);
}
} catch (Exception e) {
e.printStackTrace();
}
}
return wars;
}
public void deleteWar(War war) {
File file = new File("plugins/SimpleFactions/Wars", "war_" + war.getId() + ".json");
if (file.exists()) file.delete();
}
public void saveBattle(net.tfminecraft.simplefactions.war.battle.engine.core.Battle battle) {
if (battle == null) {
return;
}
try {
File folder = new File("plugins/SimpleFactions/Battles");
if (!folder.exists()) {
folder.mkdirs();
}
File file = new File(folder, "battle_" + battle.getId() + ".json");
net.tfminecraft.simplefactions.database.BattleData data =
net.tfminecraft.simplefactions.war.battle.persistence.BattleMapper.toData(battle);
JsonUtil.writeJson(file, data);
} catch (Exception e) {
e.printStackTrace();
}
}
public void saveWarband(net.tfminecraft.simplefactions.war.battle.warband.Warband warband) {
if (warband == null) {
return;
}
try {
File folder = new File("plugins/SimpleFactions/Warbands");
if (!folder.exists()) {
folder.mkdirs();
}
File file = new File(folder, "warband_" + warband.getId() + ".json");
net.tfminecraft.simplefactions.database.WarbandData data =
net.tfminecraft.simplefactions.war.battle.persistence.WarbandMapper.toData(warband);
JsonUtil.writeJson(file, data);
} catch (Exception e) {
e.printStackTrace();
}
}
public void deleteBattleFile(String battleId) {
if (battleId == null) {
return;
}
File file = new File("plugins/SimpleFactions/Battles", "battle_" + battleId + ".json");
if (file.exists()) {
file.delete();
}
}
public void deleteWarbandFile(String warbandId) {
if (warbandId == null) {
return;
}
File file = new File("plugins/SimpleFactions/Warbands", "warband_" + warbandId + ".json");
if (file.exists()) {
file.delete();
}
}
public java.util.List<net.tfminecraft.simplefactions.war.battle.warband.Warband> loadWarbands() {
java.util.List<net.tfminecraft.simplefactions.war.battle.warband.Warband> warbands = new ArrayList<>();
File folder = new File("plugins/SimpleFactions/Warbands");
if (!folder.exists() || !folder.isDirectory()) {
return warbands;
}
File[] files = folder.listFiles();
if (files == null) {
return warbands;
}
for (File file : files) {
if (!file.getName().endsWith(".json")) {
continue;
}
try {
net.tfminecraft.simplefactions.database.WarbandData data =
JsonUtil.readJson(file, net.tfminecraft.simplefactions.database.WarbandData.class);
net.tfminecraft.simplefactions.war.battle.warband.Warband warband =
net.tfminecraft.simplefactions.war.battle.persistence.WarbandMapper.fromData(data);
if (warband != null) {
warbands.add(warband);
}
} catch (Exception e) {
e.printStackTrace();
}
}
return warbands;
}
public java.util.List<net.tfminecraft.simplefactions.war.battle.engine.core.Battle> loadBattles() {
java.util.List<net.tfminecraft.simplefactions.war.battle.engine.core.Battle> loaded = new ArrayList<>();
File folder = new File("plugins/SimpleFactions/Battles");
if (!folder.exists() || !folder.isDirectory()) {
return loaded;
}
File[] files = folder.listFiles();
if (files == null) {
return loaded;
}
for (File file : files) {
if (!file.getName().endsWith(".json")) {
continue;
}
try {
net.tfminecraft.simplefactions.database.BattleData data =
JsonUtil.readJson(file, net.tfminecraft.simplefactions.database.BattleData.class);
net.tfminecraft.simplefactions.war.battle.engine.core.Battle battle =
net.tfminecraft.simplefactions.war.battle.persistence.BattleMapper.fromData(data);
if (battle != null) {
loaded.add(battle);
}
} catch (Exception e) {
e.printStackTrace();
}
}
return loaded;
}
}