Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/shop.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -28,6 +29,7 @@ Example **POST** response
[
{
"1": {
"id": 120,
"item": "COPPER_BLOCK",
"price": 2,
"amount": 4,
Expand Down
4 changes: 2 additions & 2 deletions docs/sse.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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

3 changes: 1 addition & 2 deletions src/main/java/net/earthmc/emcapi/sse/SSEManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions src/main/java/net/earthmc/emcapi/util/EndpointUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down