Skip to content

Commit d2c9e0e

Browse files
authored
Added ShopDeleted and ShopCreated SSEs and Id property to shops. (#44)
* Added ShopDeleted and ShopCreated SSEs and Id property to shops. * lowercase * lowercase
1 parent 4fe60ab commit d2c9e0e

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
@@ -5,6 +5,7 @@ The shop endpoint provides information about player-owned QuickShops.
55
It is important to note that the information here is not public, and players can only access their own shops' information, using their API key.
66

77
Each shop object has the following properties:
8+
- `id` - The unique identifier of the shop
89
- `item` - The Material/name of the item being traded
910
- `price` - The price of one transaction
1011
- `amount` - The amount of items in one transaction
@@ -28,6 +29,7 @@ Example **POST** response
2829
[
2930
{
3031
"1": {
32+
"id": 120,
3133
"item": "COPPER_BLOCK",
3234
"price": 2,
3335
"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
@@ -174,6 +174,7 @@ public static JsonObject generateNameUUIDJsonObject(String name, UUID uuid) {
174174

175175
public static JsonObject getShopObject(Shop shop) {
176176
JsonObject jsonObject = new JsonObject();
177+
jsonObject.addProperty("id", shop.getShopId());
177178
jsonObject.addProperty("item", shop.getItem().getType().name());
178179
jsonObject.addProperty("price", shop.getPrice());
179180
jsonObject.addProperty("amount", shop.getItem().getAmount());

0 commit comments

Comments
 (0)