Guest User

Untitled

a guest
Nov 21st, 2017
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.61 KB | None | 0 0
  1. /*
  2. * This library is free software; you can redistribute it and/or
  3. * modify it under the terms of the GNU General Public
  4. * License as published by the Free Software Foundation; either
  5. * version 2 of the license, or (at your option) any later version.
  6. */
  7. package com.inubot.api.methods;
  8.  
  9. import com.inubot.Inubot;
  10. import com.inubot.api.oldschool.NodeTable;
  11. import com.inubot.api.util.CacheLoader;
  12. import com.inubot.client.natives.oldschool.RSItemDefinition;
  13. import com.inubot.client.natives.oldschool.RSItemTable;
  14. import com.inubot.client.natives.oldschool.RSNode;
  15. import com.inubot.api.util.Identifiable;
  16. import com.inubot.client.natives.oldschool.RSNodeTable;
  17.  
  18. import java.util.ArrayList;
  19. import java.util.List;
  20. import java.util.stream.IntStream;
  21.  
  22. /**
  23. * This class should be used rather than the respective provider class (e.g. Bank, Equipment) in situations where you
  24. * do not need to interact with the item (counting number of items, getting cached item values etc).
  25. */
  26. public class ItemTables {
  27.  
  28. public static final int VARROCK_GENERAL_STORE = 4;
  29. public static final int VARROCK_RUNE_STORE = 5;
  30. public static final int VARROCK_STAFF_STORE = 51;
  31. public static final int PRICE_CHECKER = 90;
  32. public static final int INVENTORY = 93;
  33. public static final int EQUIPMENT = 94;
  34. public static final int BANK = 95;
  35. public static final int EXCHANGE_COLLECTION = 518;
  36.  
  37. public static RSNodeTable getRaw() {
  38. return Inubot.getInstance().getClient().getItemTables();
  39. }
  40.  
  41. public static NodeTable getStorage() {
  42. RSNodeTable raw = getRaw();
  43. return raw != null ? new NodeTable(raw) : null;
  44. }
  45.  
  46. /**
  47. * Note: ItemTable values for non-default widgets such as shop, bank are cached from when the widget was last opened.
  48. * Example: If I open bank in g/e and then walk away it will return the values from the bank.
  49. * Another note: If I opened the bank in g/e, and then walk into another region the table values will reset to
  50. * [-1, -1, -1, -1, -1, -1, -1. -1, -1, -1, -1, -1, -1, -1. -1, -1, -1, -1, -1, -1, -1. -1, -1, -1, -1, -1, -1, -1]
  51. *
  52. * @param tableKey the node key
  53. * @return The RSItemTable whose key matches the given tableKey
  54. */
  55. private static RSItemTable lookup(int tableKey) {
  56. NodeTable store = getStorage();
  57. if (store == null) {
  58. return null;
  59. }
  60. RSNode node = store.lookup(tableKey);
  61. return node != null && node instanceof RSItemTable ? (RSItemTable) node : null;
  62. }
  63.  
  64. public static Entry[] getEntriesIn(int tableKey) {
  65. RSItemTable table = lookup(tableKey);
  66. if (table == null) {
  67. return new Entry[0];
  68. }
  69. int len = table.getIds().length;
  70. if (len != table.getStackSizes().length) {
  71. return new Entry[0];
  72. }
  73. List<Entry> entries = new ArrayList<>();
  74. for (int i = 0; i < len; i++) {
  75. int id = table.getIds()[i];
  76. int amt = table.getStackSizes()[i];
  77. if (id > 0 && amt > 0) {
  78. entries.add(new Entry(i, id, amt));
  79. }
  80. }
  81. return entries.toArray(new Entry[entries.size()]);
  82. }
  83.  
  84. public static int[] getIdsIn(int tableKey) {
  85. RSItemTable table = lookup(tableKey);
  86. if (table == null) {
  87. return new int[0];
  88. }
  89. int len = table.getIds().length;
  90. if (len != table.getStackSizes().length) {
  91. return new int[0];
  92. }
  93. return IntStream.of(table.getIds()).filter(v -> v > 0).toArray();
  94. }
  95.  
  96. public static int getIdAt(int tableKey, int index) {
  97. int[] ids = getIdsIn(tableKey);
  98. if (ids.length > index) {
  99. return ids[index];
  100. }
  101. return -1;
  102. }
  103.  
  104. public static int getQuantityAt(int tableKey, int index) {
  105. int[] qtys = getQuantitiesIn(tableKey);
  106. if (qtys.length > index) {
  107. return qtys[index];
  108. }
  109. return -1;
  110. }
  111.  
  112. public static Entry getEntryAt(int tableKey, int index) {
  113. RSItemTable table = lookup(tableKey);
  114. if (table == null) {
  115. return null;
  116. }
  117. int len = table.getIds().length;
  118. if (len != table.getStackSizes().length) {
  119. return null;
  120. }
  121. return len > index ? new Entry(index, table.getIds()[index], table.getStackSizes()[index]) : null;
  122. }
  123.  
  124. public static int[] getQuantitiesIn(int tableKey) {
  125. RSItemTable table = lookup(tableKey);
  126. if (table == null) {
  127. return new int[0];
  128. }
  129. int len = table.getStackSizes().length;
  130. if (len != table.getIds().length) {
  131. return new int[0];
  132. }
  133. return IntStream.of(table.getStackSizes()).filter(v -> v > 0).toArray();
  134. }
  135.  
  136. public static Entry[] getInventory() {
  137. return getEntriesIn(INVENTORY);
  138. }
  139.  
  140. public static Entry[] getEquipment() {
  141. return getEntriesIn(EQUIPMENT);
  142. }
  143.  
  144. public static Entry[] getPriceChecker() {
  145. return getEntriesIn(PRICE_CHECKER);
  146. }
  147.  
  148. public static Entry[] getBank() { //the items are cached until you enter a new region
  149. return getEntriesIn(BANK);
  150. }
  151.  
  152. public static Entry[] getExchangeCollection() {
  153. return getEntriesIn(EXCHANGE_COLLECTION);
  154. }
  155.  
  156. public static Entry[] getVarrockGeneralStore() {
  157. return getEntriesIn(VARROCK_GENERAL_STORE);
  158. }
  159.  
  160. public static Entry[] getVarrockRuneStore() {
  161. return getEntriesIn(VARROCK_RUNE_STORE);
  162. }
  163.  
  164. public static Entry[] getVarrockStaffStore() {
  165. return getEntriesIn(VARROCK_STAFF_STORE);
  166. }
  167.  
  168. public static class Entry implements Identifiable {
  169.  
  170. private final int index;
  171. private final int id;
  172. private final int quantity;
  173. private final RSItemDefinition definition;
  174.  
  175. public Entry(int index, int id, int quantity) {
  176. this.index = index;
  177. this.id = id;
  178. this.quantity = quantity;
  179. this.definition = CacheLoader.findItemDefinition(id);
  180. }
  181.  
  182. public int getId() {
  183. return id;
  184. }
  185.  
  186. public int getQuantity() {
  187. return quantity;
  188. }
  189.  
  190. public RSItemDefinition getDefinition() {
  191. return definition;
  192. }
  193.  
  194. public String getName() {
  195. return definition == null ? null : definition.getName();
  196. }
  197.  
  198. public String[] getActions() {
  199. return definition == null ? new String[0] : definition.getActions();
  200. }
  201.  
  202. public String[] getGroundActions() {
  203. return definition == null ? new String[0] : definition.getGroundActions();
  204. }
  205.  
  206. public int getIndex() {
  207. return index;
  208. }
  209. }
  210. }
Add Comment
Please, Sign In to add comment