From a9870c3f8aa7bca0cd4ca48b322de54bd480573b Mon Sep 17 00:00:00 2001 From: QuBerto Date: Sat, 14 Sep 2024 23:20:25 +0200 Subject: [PATCH 1/2] Add loot endpoint for raids etc Add username to endpoint to better identify the player --- .../java/com/eventsapi/EventsAPIPlugin.java | 135 +++++++++++++++++- .../java/com/eventsapi/enums/WIDGET_IDS.java | 45 ++++++ .../notifications/LootNotification.java | 44 ++++++ .../notifications/NpcKillNotification.java | 8 +- .../java/com/eventsapi/utils/Endpoint.java | 1 + 5 files changed, 226 insertions(+), 7 deletions(-) create mode 100644 src/main/java/com/eventsapi/enums/WIDGET_IDS.java create mode 100644 src/main/java/com/eventsapi/notifications/LootNotification.java diff --git a/src/main/java/com/eventsapi/EventsAPIPlugin.java b/src/main/java/com/eventsapi/EventsAPIPlugin.java index ae99168..991aa9a 100644 --- a/src/main/java/com/eventsapi/EventsAPIPlugin.java +++ b/src/main/java/com/eventsapi/EventsAPIPlugin.java @@ -19,6 +19,7 @@ import net.runelite.client.plugins.PluginDescriptor; // Local Imports +import com.eventsapi.enums.WIDGET_IDS; import com.eventsapi.enums.LOGIN_STATE; import com.eventsapi.interfaces.ApiConnectable; import com.eventsapi.notifications.*; @@ -33,7 +34,7 @@ @Slf4j @PluginDescriptor( - name = "EventsAPI" + name = "EventsAPI" ) public class EventsAPIPlugin extends Plugin { @@ -215,19 +216,141 @@ public void onStatChanged(StatChanged statChanged) { } } + @Subscribe + public void onWidgetLoaded(WidgetLoaded widgetLoaded) { + + int groupId = widgetLoaded.getGroupId(); + logger.debug("Widget loaded: {}", groupId); + if (!IsLoggedIn() || !IsSupportedWidget(groupId)) { + return; + } + logger.debug("Supported widget: {}", groupId); + this.createAndSendWidgetNotification(widgetLoaded); + } + + private boolean IsLoggedIn() { + return client.getGameState() == GameState.LOGGED_IN; + } + + private boolean IsSupportedWidget(int WidgetId) { + logger.debug("WidgetID: " + WidgetId); + WIDGET_IDS widget = WIDGET_IDS.fromWidgetId(WidgetId); // This will return the corresponding WIDGET_IDS or null + return widget != null; // Return true if the widget is found, false otherwise + } + + + + + private void createAndSendLootNotification(NpcLootReceived lootReceived) { + // Log that loot notification is being prepared + logger.debug("Preparing loot notification for NPC: {}", lootReceived.getNpc().getName()); - private void createAndSendLootNotification(NpcLootReceived lootReceived){ + Player localPlayer = client.getLocalPlayer(); + String userName = localPlayer.getName(); int geTotalPrice = 0; int npcId = lootReceived.getNpc().getId(); List items = new ArrayList<>(); - for(ItemStack item : lootReceived.getItems()){ - items.add(new Item(item.getId(), item.getQuantity())); - geTotalPrice += item.getQuantity() * itemManager.getItemComposition(item.getId()).getPrice(); + + // Loop through each item in the loot + for (ItemStack itemStack : lootReceived.getItems()) { + int itemId = itemStack.getId(); + int itemQuantity = itemStack.getQuantity(); + int itemPrice = 0; + int totalItemPrice = 0; + String itemName = itemManager.getItemComposition(itemStack.getId()).getName(); + + // Log each item's ID, quantity, and total price + logger.debug("Item ID: {}, Quantity: {}, Item Price: {}, Total Item Price: {}", + itemId, itemQuantity, itemPrice, totalItemPrice); + + items.add(new Item(itemId, itemQuantity)); + geTotalPrice += itemStack.getQuantity() * itemManager.getItemComposition(itemStack.getId()).getPrice(); + } + + // Log the total price of the received loot + logger.debug("Total GE Price of Loot: {}", geTotalPrice); + + // Create and send the loot notification + NpcKillNotification notification = new NpcKillNotification(npcId, items, geTotalPrice, userName); + + // Log before sending the notification + logger.debug("Sending loot notification for NPC ID: {}, Player: {}", npcId, userName); + + messageHandler.sendEventNow(MESSAGE_EVENT.LOOT, notification); + + // Log that the notification has been sent + logger.debug("Loot notification sent successfully for NPC ID: {}, Player: {}", npcId, userName); + } + + private void createAndSendWidgetNotification(WidgetLoaded widgetLoaded) { + // Log that widget notification is being prepared + logger.info("Preparing widget notification for Widget ID: {}", widgetLoaded.getGroupId()); + + // Get the local player and check if they are valid + Player localPlayer = client.getLocalPlayer(); + if (localPlayer == null) { + logger.error("Local player not found. Unable to send widget notification."); + return; + } + String userName = localPlayer.getName(); + + // Get the widget ID from the loaded widget + int widgetId = widgetLoaded.getGroupId(); + + // Find the corresponding WIDGET_IDS enum based on the widget ID + WIDGET_IDS widget = WIDGET_IDS.fromWidgetId(widgetId); + if (widget == null) { + logger.error("Widget not supported: {}", widgetId); + return; + } + + ItemContainer container = client.getItemContainer(widget.getInventoryId()); + if (container == null) { + return ; // Return an empty list if no items found + } + + + List items = new ArrayList<>(); + int totalItemPrice = 0; + // Loop through each item in the loot + for (Item item : container.getItems()) { + int itemId = item.getId(); + int itemQuantity = item.getQuantity(); + + // Initialize the item price and total price variables + int itemPrice = itemManager.getItemPrice(itemId); + totalItemPrice = itemPrice * itemQuantity; + String itemName = itemManager.getItemComposition(itemId).getName(); + + // Log each item's ID, quantity, and total price + logger.debug("Item ID: {}, Name: {}, Quantity: {}, Item Price: {}, Total Item Price: {}", + itemId, itemName, itemQuantity, itemPrice, totalItemPrice); + + // Add the item to the list of items + items.add(new Item(itemId, itemQuantity)); + } - NpcKillNotification notification = new NpcKillNotification(npcId, items, geTotalPrice); + + + // Log the supported widget + logger.debug("Found supported widget: {}", widget.getName()); + + // Create the widget notification + LootNotification notification = new LootNotification(widgetId, items, totalItemPrice, userName); + + // Log the sending of the notification + logger.debug("Sending widget notification for Widget ID: {}, Player: {}", widgetId, userName); + + // Send the notification messageHandler.sendEventNow(MESSAGE_EVENT.LOOT, notification); + + // Log that the notification was sent successfully + logger.debug("Widget notification sent successfully for Widget ID: {}, Player: {}", widgetId, userName); } + + + private void createAndQueueBankNotification(){ //Ensure bankContainer is valid if(lastBankContainer == null){ diff --git a/src/main/java/com/eventsapi/enums/WIDGET_IDS.java b/src/main/java/com/eventsapi/enums/WIDGET_IDS.java new file mode 100644 index 0000000..593fb53 --- /dev/null +++ b/src/main/java/com/eventsapi/enums/WIDGET_IDS.java @@ -0,0 +1,45 @@ +package com.eventsapi.enums; +import net.runelite.api.widgets.WidgetID; +import net.runelite.api.InventoryID; + +public enum WIDGET_IDS { + BARROWS(WidgetID.BARROWS_REWARD_GROUP_ID, "Barrows", InventoryID.BARROWS_REWARD), + COX(WidgetID.CHAMBERS_OF_XERIC_REWARD_GROUP_ID, "COX", InventoryID.CHAMBERS_OF_XERIC_CHEST), + TOB(WidgetID.THEATRE_OF_BLOOD_GROUP_ID, "TOB", InventoryID.THEATRE_OF_BLOOD_CHEST), + TOA(WidgetID.TOA_REWARD_GROUP_ID, "TOA", InventoryID.TOA_REWARD_CHEST), + FISHING_TRAWLER(WidgetID.FISHING_TRAWLER_REWARD_GROUP_ID, "Fishing Trawler", InventoryID.FISHING_TRAWLER_REWARD), + WILDERNESS_LOOT(WidgetID.WILDERNESS_LOOT_CHEST, "Wilderness Loot Chest", InventoryID.WILDERNESS_LOOT_CHEST); + + private final int widgetId; + private final String name; + private final InventoryID inventoryId; + + WIDGET_IDS(int widgetId, String name, InventoryID inventoryId) { + this.widgetId = widgetId; + this.name = name; + this.inventoryId = inventoryId; + } + + + + public int getWidgetId() { + return widgetId; + } + + public String getName() { + return name; + } + + public InventoryID getInventoryId() { + return inventoryId; + } + + public static WIDGET_IDS fromWidgetId(int widgetId) { + for (WIDGET_IDS widget : values()) { + if (widget.getWidgetId() == widgetId) { + return widget; + } + } + return null; + } +} diff --git a/src/main/java/com/eventsapi/notifications/LootNotification.java b/src/main/java/com/eventsapi/notifications/LootNotification.java new file mode 100644 index 0000000..54bd1cb --- /dev/null +++ b/src/main/java/com/eventsapi/notifications/LootNotification.java @@ -0,0 +1,44 @@ +package com.eventsapi.notifications; + +import com.eventsapi.interfaces.Sendable; +import com.eventsapi.utils.Endpoint; +import lombok.Getter; +import lombok.Setter; +import net.runelite.api.Item; + +import java.util.List; + +public class LootNotification implements Sendable { + + private final static String API_ENDPOINT = Endpoint.LOOT_ENDPOINT; + + public LootNotification(int widgetId, List items, int gePrice, String userName){ + + this.setWidgetId(widgetId); + this.setItems(items); + this.setGePrice(gePrice); + this.setUserName(userName); + } + + public String getApiEndpoint(){ + return API_ENDPOINT; + } + public EventWrapper getEventWrapper(){ return new EventWrapper(this); } + + @Getter + @Setter + private int gePrice; + + @Getter + @Setter + private int widgetId; + + @Getter + @Setter + private List items; + + @Getter + @Setter + private String userName; + +} diff --git a/src/main/java/com/eventsapi/notifications/NpcKillNotification.java b/src/main/java/com/eventsapi/notifications/NpcKillNotification.java index 5709bca..7f8e14c 100644 --- a/src/main/java/com/eventsapi/notifications/NpcKillNotification.java +++ b/src/main/java/com/eventsapi/notifications/NpcKillNotification.java @@ -12,11 +12,12 @@ public class NpcKillNotification implements Sendable { private final static String API_ENDPOINT = Endpoint.NPC_KILL_ENDPOINT; - public NpcKillNotification(int npcId, List items, int gePrice){ + public NpcKillNotification(int npcId, List items, int gePrice, String userName){ this.setNpcId(npcId); this.setItems(items); this.setGePrice(gePrice); + this.setUserName(userName); } public String getApiEndpoint(){ @@ -35,4 +36,9 @@ public String getApiEndpoint(){ @Getter @Setter private List items; + + @Getter + @Setter + private String userName; + } diff --git a/src/main/java/com/eventsapi/utils/Endpoint.java b/src/main/java/com/eventsapi/utils/Endpoint.java index 88dd68c..651d982 100644 --- a/src/main/java/com/eventsapi/utils/Endpoint.java +++ b/src/main/java/com/eventsapi/utils/Endpoint.java @@ -2,6 +2,7 @@ public final class Endpoint { public static final String NPC_KILL_ENDPOINT = "npc_kill/"; + public static final String LOOT_ENDPOINT = "loot/"; public static final String LEVEL_CHANGE_ENDPOINT = "level_change/"; public static final String QUEST_POINT_ENDPOINT = "quest_change/"; public static final String EQUIPPED_ITEMS_ENDPOINT = "equipped_items/"; From 503718818f16dabccbd87bb8b62f2957f4d0c36e Mon Sep 17 00:00:00 2001 From: your name Date: Tue, 17 Sep 2024 20:01:57 +0200 Subject: [PATCH 2/2] Add loot endpoint for raids etc Add username to endpoint to better identify the player Fix bug where player status is being emitted, despite state of the config option --- src/main/java/com/eventsapi/ApiManager.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/eventsapi/ApiManager.java b/src/main/java/com/eventsapi/ApiManager.java index 9394180..1c36399 100644 --- a/src/main/java/com/eventsapi/ApiManager.java +++ b/src/main/java/com/eventsapi/ApiManager.java @@ -76,7 +76,9 @@ public boolean canSend(Sendable event){ logger.warn("Failed to send, please check endpoint: " + endpoint + " and token: " + token); return false; } - + if (event instanceof PlayerStatusNotification){ + return config.emitPlayerState(); + } if (event instanceof BankNotification){ return config.emitBankItems(); }