Skip to content

Commit b59f651

Browse files
committed
Add bankhistory to nation and town endpoints
Only the leader/mayor, chancellor/councillor, and/or treasurer can access this info with their API key
1 parent 4a4f18c commit b59f651

5 files changed

Lines changed: 107 additions & 2 deletions

File tree

docs/nations.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,23 @@ Example **POST** response
210210
}
211211
},
212212
"pending": {} // A JSON dictionary representing pending pacts this nation has.
213-
}
213+
},
214+
"bankhistory": [
215+
{
216+
"time": 1779962429347, // The time the transaction took place, in epoch milliseconds
217+
"type": "WITHDRAW", // Transaction type (DEPOSIT, WITHDRAW, ADD, SUBTRACT)
218+
"amount": 270, // Transaction amount
219+
"balance": 4500, // Balance after the transaction
220+
"reason": "Withdraw by Veyronity" // The transaction reason. Either caused by a player or server-side action (e.g. upkeep)
221+
},
222+
{
223+
"time": 1779962436293,
224+
"type": "DEPOSIT",
225+
"amount": 1000,
226+
"balance": 5500,
227+
"reason": "Deposit from Veyronity"
228+
}
229+
]
214230
}
215231
]
216232
```

docs/towns.md

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,8 +287,30 @@ Example **POST** response
287287
"z": -9
288288
}
289289
}
290+
],
291+
"bankhistory": [
292+
{
293+
"time": 1779962429347, // The time the transaction took place, in epoch milliseconds
294+
"type": "WITHDRAW", // Transaction type (DEPOSIT, WITHDRAW, ADD, SUBTRACT)
295+
"amount": 300.0, // Transaction amount
296+
"balance": 700.0, // Balance after the transaction
297+
"reason": "Withdraw by Veyronity" // The transaction reason. Either caused by a player or server-side action (e.g. upkeep)
298+
},
299+
{
300+
"time": 1779962434994,
301+
"type": "DEPOSIT",
302+
"amount": 200.0,
303+
"balance": 900.0,
304+
"reason": "Deposit from Veyronity"
305+
},
306+
{
307+
"time": 1779962436293,
308+
"type": "DEPOSIT",
309+
"amount": 100.0,
310+
"balance": 1000.0,
311+
"reason": "Deposit from Veyronity"
312+
}
290313
]
291314
}
292-
}
293315
]
294316
```

src/main/java/net/earthmc/emcapi/endpoint/towny/NationsEndpoint.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import net.earthmc.emcapi.integration.EmbargoesIntegration;
1515
import net.earthmc.emcapi.integration.Integrations;
1616
import net.earthmc.emcapi.integration.PactsIntegration;
17+
import net.earthmc.emcapi.manager.KeyManager;
1718
import net.earthmc.emcapi.manager.NationMetadataManager;
1819
import net.earthmc.emcapi.object.endpoint.PostEndpoint;
1920
import net.earthmc.emcapi.util.EndpointUtils;
@@ -99,6 +100,7 @@ public JsonElement getJsonElement(Nation nation, @Nullable String key) {
99100

100101
nationObject.add("embargoes", getEmbargoesObject(nation));
101102
nationObject.add("pacts", getPactsObject(nation));
103+
nationObject.add("bankhistory", getBankHistory(nation, key));
102104

103105
return nationObject;
104106
}
@@ -150,4 +152,21 @@ private JsonObject getPactsObject(Nation nation) {
150152

151153
return json;
152154
}
155+
156+
private JsonArray getBankHistory(Nation nation, @Nullable String key) {
157+
JsonArray empty = new JsonArray();
158+
if (!TownyEconomyHandler.isActive()) {
159+
return empty;
160+
}
161+
UUID keyOwner = KeyManager.getKeyOwner(key);
162+
if (keyOwner == null) {
163+
return empty;
164+
}
165+
Resident res = TownyAPI.getInstance().getResident(keyOwner);
166+
if (res == null || !nation.equals(res.getNationOrNull()) || !(nation.isKing(res) || res.hasNationRank("Chancellor") || res.hasNationRank("Treasurer"))) {
167+
return empty;
168+
}
169+
170+
return EndpointUtils.getBankHistoryArray(nation);
171+
}
153172
}

src/main/java/net/earthmc/emcapi/endpoint/towny/TownsEndpoint.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import com.google.gson.JsonObject;
66
import com.palmergames.bukkit.towny.TownyAPI;
77
import com.palmergames.bukkit.towny.TownyEconomyHandler;
8+
import com.palmergames.bukkit.towny.object.Resident;
89
import com.palmergames.bukkit.towny.object.Town;
910
import com.palmergames.bukkit.towny.object.TownBlock;
1011
import com.palmergames.bukkit.towny.permissions.TownyPerms;
@@ -13,6 +14,7 @@
1314
import net.earthmc.emcapi.integration.Integrations;
1415
import net.earthmc.emcapi.integration.QuartersIntegration;
1516
import net.earthmc.emcapi.integration.WarpsIntegration;
17+
import net.earthmc.emcapi.manager.KeyManager;
1618
import net.earthmc.emcapi.manager.TownMetadataManager;
1719
import net.earthmc.emcapi.object.endpoint.PostEndpoint;
1820
import net.earthmc.emcapi.util.EndpointUtils;
@@ -125,6 +127,7 @@ public JsonElement getJsonElement(Town town, @Nullable String key) {
125127
townObject.add("ranks", ranksObject);
126128

127129
townObject.add("warps", getWarpsObject(town));
130+
townObject.add("bankhistory", getBankHistory(town, key));
128131

129132
return townObject;
130133
}
@@ -142,4 +145,21 @@ private JsonArray getWarpsObject(Town town) {
142145

143146
return json;
144147
}
148+
149+
private JsonArray getBankHistory(Town town, @Nullable String key) {
150+
JsonArray empty = new JsonArray();
151+
if (!TownyEconomyHandler.isActive()) {
152+
return empty;
153+
}
154+
UUID keyOwner = KeyManager.getKeyOwner(key);
155+
if (keyOwner == null) {
156+
return empty;
157+
}
158+
Resident res = TownyAPI.getInstance().getResident(keyOwner);
159+
if (res == null || !town.equals(res.getTownOrNull()) || !(town.isMayor(res) || res.hasTownRank("Councillor") || res.hasTownRank("Treasurer"))) {
160+
return empty;
161+
}
162+
163+
return EndpointUtils.getBankHistoryArray(town);
164+
}
145165
}

src/main/java/net/earthmc/emcapi/util/EndpointUtils.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
import com.palmergames.bukkit.towny.object.Resident;
88
import com.palmergames.bukkit.towny.object.Town;
99
import com.palmergames.bukkit.towny.object.TownyPermission;
10+
import com.palmergames.bukkit.towny.object.Government;
11+
import com.palmergames.bukkit.towny.object.economy.BankTransaction;
1012
import net.earthmc.emcapi.EMCAPI;
1113
import net.earthmc.lynchpin.api.towny.pacts.Pact;
1214
import net.earthmc.lynchpin.api.towny.warps.Warp;
@@ -231,4 +233,30 @@ public static JsonObject getWarpObject(Warp warp) {
231233

232234
return json;
233235
}
236+
237+
public static JsonArray getBankHistoryArray(Government government) {
238+
JsonArray json = new JsonArray();
239+
List<BankTransaction> history = government.getAccount().getAuditor().getTransactions();
240+
if (history.isEmpty()) {
241+
return json;
242+
}
243+
for (int i = 0; i < Math.min(10, history.size()); i++) {
244+
BankTransaction transaction = history.get(i);
245+
json.add(getBankTransactionObject(transaction));
246+
}
247+
248+
return json;
249+
}
250+
251+
private static JsonObject getBankTransactionObject(BankTransaction transaction) {
252+
JsonObject json = new JsonObject();
253+
254+
json.addProperty("time", transaction.time());
255+
json.addProperty("type", transaction.getType().name());
256+
json.addProperty("amount", transaction.getAmount());
257+
json.addProperty("balance", transaction.getBalance());
258+
json.addProperty("reason", transaction.getReason());
259+
260+
return json;
261+
}
234262
}

0 commit comments

Comments
 (0)