Skip to content

Commit f592ea7

Browse files
authored
Merge branch 'main' into v4-revamp
2 parents b6f7045 + d2c9e0e commit f592ea7

5 files changed

Lines changed: 47 additions & 4 deletions

File tree

docs/shop.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ It is important to note that the information here is not public, and players can
77
The rate limit for this endpoint is one request per hour per API key (and thus per player).
88

99
Each shop object has the following properties:
10+
- `id` - The unique identifier of the shop
1011
- `item` - The Material/name of the item being traded
1112
- `price` - The price of one transaction
1213
- `amount` - The amount of items in one transaction
@@ -30,6 +31,7 @@ Example **POST** response
3031
[
3132
{
3233
"1": {
34+
"id": 120,
3335
"item": "COPPER_BLOCK",
3436
"price": 2,
3537
"amount": 4,

docs/sse.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ These are the current events available:
1616
"TownCreated", "TownDeleted", "TownRenamed", "TownMayorChanged", "TownMerged", "TownRuined", "TownReclaimed",
1717
"TownJoinedNation", "TownLeftNation",
1818
"ResidentJoinedTown", "ResidentLeftTown",
19-
"ShopSoldItem", "ShopBoughtItem", "ShopOutOfStock", "ShopOutOfSpace", "ShopOutOfGold"
19+
"ShopSoldItem", "ShopBoughtItem", "ShopOutOfStock", "ShopOutOfSpace", "ShopOutOfGold", "ShopCreated", "ShopDeleted"
2020
```
2121
Clients must specify which events to listen to by specifying a `?listen=` query parameter in the URL.
2222
Example: `https://api.earthmc.net/v4/events?listen=NewDay,TownDeleted,NationRenamed`. This would make it so only these events are sent to the client.
@@ -26,5 +26,5 @@ Most Town events include a `town` field with the name & UUID of the town. The sa
2626
Some events are only sent to the relevant player. These are:
2727
- TownJoinedNation, TownLeftNation - Sent to the leader of the nation
2828
- ResidentJoinedTown, ResidentLeftTown - Sent to the mayor of the town
29-
- ShopSoldItem, ShopBoughtItem, ShopOutOfStock, ShopOutOfSpace, ShopOutOfGold - Sent to the shop owner
29+
- ShopSoldItem, ShopBoughtItem, ShopOutOfStock, ShopOutOfSpace, ShopOutOfGold, ShopCreated, ShopDeleted - Sent to the shop owner
3030

src/main/java/net/earthmc/emcapi/sse/SSEManager.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
import java.util.UUID;
1818
import java.util.concurrent.ConcurrentHashMap;
1919
import java.util.concurrent.TimeUnit;
20-
import java.util.concurrent.atomic.AtomicLong;
2120

2221
public class SSEManager {
2322
private final EMCAPI plugin;
@@ -32,7 +31,7 @@ public class SSEManager {
3231
"TownCreated", "TownDeleted", "TownRenamed", "TownMayorChanged", "TownMerged", "TownRuined", "TownReclaimed",
3332
"TownJoinedNation", "TownLeftNation",
3433
"ResidentJoinedTown", "ResidentLeftTown",
35-
"ShopSoldItem", "ShopBoughtItem", "ShopOutOfStock", "ShopOutOfSpace", "ShopOutOfGold"
34+
"ShopSoldItem", "ShopBoughtItem", "ShopOutOfStock", "ShopOutOfSpace", "ShopOutOfGold", "ShopCreated", "ShopDeleted"
3635
);
3736

3837
public SSEManager(EMCAPI plugin) {

src/main/java/net/earthmc/emcapi/sse/listeners/ShopSSEListener.java

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package net.earthmc.emcapi.sse.listeners;
22

3+
import com.ghostchu.quickshop.api.event.Phase;
34
import com.ghostchu.quickshop.api.event.economy.ShopSuccessPurchaseEvent;
5+
import com.ghostchu.quickshop.api.event.management.ShopCreateEvent;
6+
import com.ghostchu.quickshop.api.event.management.ShopDeleteEvent;
47
import com.ghostchu.quickshop.api.obj.QUser;
58
import com.ghostchu.quickshop.api.shop.Shop;
69
import com.google.gson.JsonObject;
@@ -44,6 +47,44 @@ public void onShopPurchase(ShopSuccessPurchaseEvent event) {
4447
checkShopOut(shop);
4548
}
4649

50+
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
51+
public void onShopCreate(ShopCreateEvent event) {
52+
53+
if (!event.isPhase(Phase.POST)) {
54+
return;
55+
}
56+
57+
Shop shop = event.shop().orElse(null);
58+
if (shop == null) {
59+
return;
60+
}
61+
UUID owner = shop.getOwner().getUniqueId();
62+
if (owner == null ) {
63+
return;
64+
}
65+
66+
JsonObject message = new JsonObject();
67+
message.add("shop", EndpointUtils.getShopObject(shop));
68+
sse.sendEvent("ShopCreated", message, owner);
69+
}
70+
71+
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
72+
public void onShopDelete(ShopDeleteEvent event) {
73+
Shop shop = event.shop().orElse(null);
74+
if (shop == null) {
75+
return;
76+
}
77+
if (!event.isPhase(Phase.POST)) {
78+
return;
79+
}
80+
UUID owner = shop.getOwner().getUniqueId();
81+
if (owner == null) return;
82+
83+
JsonObject message = new JsonObject();
84+
message.add("shop", EndpointUtils.getShopObject(shop));
85+
sse.sendEvent("ShopDeleted", message, owner);
86+
}
87+
4788
private void checkOwnerBalance(UUID owner) {
4889
Resident res = TownyAPI.getInstance().getResident(owner);
4990
if (res == null || res.getAccount().getHoldingBalance() > 0) return;

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ public static JsonObject generateNameUUIDJsonObject(String name, UUID uuid) {
176176

177177
public static JsonObject getShopObject(Shop shop) {
178178
JsonObject jsonObject = new JsonObject();
179+
jsonObject.addProperty("id", shop.getShopId());
179180
jsonObject.addProperty("owner", String.valueOf(shop.getOwner().getUniqueId()));
180181
jsonObject.addProperty("item", shop.getItem().getType().name());
181182
jsonObject.addProperty("price", shop.getPrice());

0 commit comments

Comments
 (0)