Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add loot Endpoint + username #11

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
4 changes: 3 additions & 1 deletion src/main/java/com/eventsapi/ApiManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
135 changes: 129 additions & 6 deletions src/main/java/com/eventsapi/EventsAPIPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;
Expand All @@ -33,7 +34,7 @@

@Slf4j
@PluginDescriptor(
name = "EventsAPI"
name = "EventsAPI"
)
public class EventsAPIPlugin extends Plugin
{
Expand Down Expand Up @@ -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<Item> 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<Item> 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){
Expand Down
45 changes: 45 additions & 0 deletions src/main/java/com/eventsapi/enums/WIDGET_IDS.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
44 changes: 44 additions & 0 deletions src/main/java/com/eventsapi/notifications/LootNotification.java
Original file line number Diff line number Diff line change
@@ -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<Item> 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<Item> items;

@Getter
@Setter
private String userName;

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@ public class NpcKillNotification implements Sendable {

private final static String API_ENDPOINT = Endpoint.NPC_KILL_ENDPOINT;

public NpcKillNotification(int npcId, List<Item> items, int gePrice){
public NpcKillNotification(int npcId, List<Item> items, int gePrice, String userName){

this.setNpcId(npcId);
this.setItems(items);
this.setGePrice(gePrice);
this.setUserName(userName);
}

public String getApiEndpoint(){
Expand All @@ -35,4 +36,9 @@ public String getApiEndpoint(){
@Getter
@Setter
private List<Item> items;

@Getter
@Setter
private String userName;

}
1 change: 1 addition & 0 deletions src/main/java/com/eventsapi/utils/Endpoint.java
Original file line number Diff line number Diff line change
Expand Up @@ -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/";
Expand Down