diff --git a/docs/shop.md b/docs/shop.md index 7a41557..7ac8885 100644 --- a/docs/shop.md +++ b/docs/shop.md @@ -5,6 +5,7 @@ The shop endpoint provides information about player-owned QuickShops. 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. Each shop object has the following properties: +- `id` - The unique identifier of the shop - `item` - The Material/name of the item being traded - `price` - The price of one transaction - `amount` - The amount of items in one transaction @@ -28,6 +29,7 @@ Example **POST** response [ { "1": { + "id": 120, "item": "COPPER_BLOCK", "price": 2, "amount": 4, diff --git a/docs/sse.md b/docs/sse.md index 319f243..c9da8a2 100644 --- a/docs/sse.md +++ b/docs/sse.md @@ -16,7 +16,7 @@ These are the current events available: "TownCreated", "TownDeleted", "TownRenamed", "TownMayorChanged", "TownMerged", "TownRuined", "TownReclaimed", "TownJoinedNation", "TownLeftNation", "ResidentJoinedTown", "ResidentLeftTown", -"ShopSoldItem", "ShopBoughtItem", "ShopOutOfStock", "ShopOutOfSpace", "ShopOutOfGold" +"ShopSoldItem", "ShopBoughtItem", "ShopOutOfStock", "ShopOutOfSpace", "ShopOutOfGold", "ShopCreated", "ShopDeleted" ``` Clients must specify which events to listen to by specifying a `?listen=` query parameter in the URL. 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 Some events are only sent to the relevant player. These are: - TownJoinedNation, TownLeftNation - Sent to the leader of the nation - ResidentJoinedTown, ResidentLeftTown - Sent to the mayor of the town -- ShopSoldItem, ShopBoughtItem, ShopOutOfStock, ShopOutOfSpace, ShopOutOfGold - Sent to the shop owner +- ShopSoldItem, ShopBoughtItem, ShopOutOfStock, ShopOutOfSpace, ShopOutOfGold, ShopCreated, ShopDeleted - Sent to the shop owner diff --git a/src/main/java/net/earthmc/emcapi/sse/SSEManager.java b/src/main/java/net/earthmc/emcapi/sse/SSEManager.java index 35265ed..2e8ad52 100644 --- a/src/main/java/net/earthmc/emcapi/sse/SSEManager.java +++ b/src/main/java/net/earthmc/emcapi/sse/SSEManager.java @@ -17,7 +17,6 @@ import java.util.UUID; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.TimeUnit; -import java.util.concurrent.atomic.AtomicLong; public class SSEManager { private final EMCAPI plugin; @@ -32,7 +31,7 @@ public class SSEManager { "TownCreated", "TownDeleted", "TownRenamed", "TownMayorChanged", "TownMerged", "TownRuined", "TownReclaimed", "TownJoinedNation", "TownLeftNation", "ResidentJoinedTown", "ResidentLeftTown", - "ShopSoldItem", "ShopBoughtItem", "ShopOutOfStock", "ShopOutOfSpace", "ShopOutOfGold" + "ShopSoldItem", "ShopBoughtItem", "ShopOutOfStock", "ShopOutOfSpace", "ShopOutOfGold", "ShopCreated", "ShopDeleted" ); public SSEManager(EMCAPI plugin) { diff --git a/src/main/java/net/earthmc/emcapi/sse/listeners/ShopSSEListener.java b/src/main/java/net/earthmc/emcapi/sse/listeners/ShopSSEListener.java index 37fdaf3..ab139a1 100644 --- a/src/main/java/net/earthmc/emcapi/sse/listeners/ShopSSEListener.java +++ b/src/main/java/net/earthmc/emcapi/sse/listeners/ShopSSEListener.java @@ -1,6 +1,9 @@ package net.earthmc.emcapi.sse.listeners; +import com.ghostchu.quickshop.api.event.Phase; import com.ghostchu.quickshop.api.event.economy.ShopSuccessPurchaseEvent; +import com.ghostchu.quickshop.api.event.management.ShopCreateEvent; +import com.ghostchu.quickshop.api.event.management.ShopDeleteEvent; import com.ghostchu.quickshop.api.obj.QUser; import com.ghostchu.quickshop.api.shop.Shop; import com.google.gson.JsonObject; @@ -44,6 +47,44 @@ public void onShopPurchase(ShopSuccessPurchaseEvent event) { checkShopOut(shop); } + @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) + public void onShopCreate(ShopCreateEvent event) { + + if (!event.isPhase(Phase.POST)) { + return; + } + + Shop shop = event.shop().orElse(null); + if (shop == null) { + return; + } + UUID owner = shop.getOwner().getUniqueId(); + if (owner == null ) { + return; + } + + JsonObject message = new JsonObject(); + message.add("shop", EndpointUtils.getShopObject(shop)); + sse.sendEvent("ShopCreated", message, owner); + } + + @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) + public void onShopDelete(ShopDeleteEvent event) { + Shop shop = event.shop().orElse(null); + if (shop == null) { + return; + } + if (!event.isPhase(Phase.POST)) { + return; + } + UUID owner = shop.getOwner().getUniqueId(); + if (owner == null) return; + + JsonObject message = new JsonObject(); + message.add("shop", EndpointUtils.getShopObject(shop)); + sse.sendEvent("ShopDeleted", message, owner); + } + private void checkOwnerBalance(UUID owner) { Resident res = TownyAPI.getInstance().getResident(owner); if (res == null || res.getAccount().getHoldingBalance() > 0) return; diff --git a/src/main/java/net/earthmc/emcapi/util/EndpointUtils.java b/src/main/java/net/earthmc/emcapi/util/EndpointUtils.java index f6ac66d..b365f00 100644 --- a/src/main/java/net/earthmc/emcapi/util/EndpointUtils.java +++ b/src/main/java/net/earthmc/emcapi/util/EndpointUtils.java @@ -174,6 +174,7 @@ public static JsonObject generateNameUUIDJsonObject(String name, UUID uuid) { public static JsonObject getShopObject(Shop shop) { JsonObject jsonObject = new JsonObject(); + jsonObject.addProperty("id", shop.getShopId()); jsonObject.addProperty("item", shop.getItem().getType().name()); jsonObject.addProperty("price", shop.getPrice()); jsonObject.addProperty("amount", shop.getItem().getAmount());