RuneHive-Tarnish
Neural OSRS Enhancement Framework
Loading...
Searching...
No Matches
PriceChecker.java
1package com.osroyale.game.world.items.containers.pricechecker;
2
3import com.osroyale.game.world.InterfaceConstants;
4import com.osroyale.game.world.entity.mob.player.Player;
5import com.osroyale.game.world.items.Item;
6import com.osroyale.game.world.items.ItemDefinition;
7import com.osroyale.game.world.items.containers.ItemContainer;
8import com.osroyale.game.world.items.containers.ItemContainerAdapter;
9import com.osroyale.net.packet.out.*;
10import com.osroyale.util.Utility;
11
12import java.text.NumberFormat;
13import java.util.Optional;
14
56
57public class PriceChecker extends ItemContainer {
58
60 private final int[] STRINGS = {48550, 48551, 48552, 48553, 48554, 48555, 48556, 48557, 48558, 48559, 48560, 48561, 48562, 48563, 48564, 48565, 48566, 48567, 48568, 48569, 48570, 48571, 48572, 48573, 48574, 48575, 48576, 48577,};
61
63 private static final int PRICE_CHECKER_DISPLAY_ID = 48542;
64
66 public Player player;
67
69 private PriceType priceType;
70
72 private Item searchedItem;
73
76 super(28, StackPolicy.STANDARD);
77 this.player = player;
78 this.priceType = PriceType.VALUE;
79 addListener(new PriceCheckerListener());
80 }
81
83 public void open() {
84 refresh();
85 player.send(new SendString("", 48583));
86 player.send(new SendString("\\nSelect item to search", 48582));
87 player.send(new SendItemOnInterface(48581));
88 player.attributes.set("PRICE_CHECKER_KEY", true);
89 player.interfaceManager.openInventory(48500, 5063);
90 }
91
93 public void close() {
95 searchedItem = null;
96 player.attributes.set("PRICE_CHECKER_KEY", false);
97 }
98
100 public void search(String name) {
101 ItemDefinition found = null;
102
103 for (ItemDefinition definition : ItemDefinition.DEFINITIONS) {
104 if (definition == null)
105 continue;
106 String searched = definition.getName().toLowerCase().replace("'", "");
107 if (!searched.contains(name.toLowerCase()))
108 continue;
109 found = definition;
110 break;
111 }
112
113 if (found == null) {
114 player.dialogueFactory.sendStatement("There were no results found for", "<col=255>" + name).execute();
115 return;
116 }
117
118 player.send(new SendString(Utility.formatDigits(found.getValue()), 48582));
119 player.send(new SendString(found.getName(), 48583));
120 player.send(new SendItemOnInterface(48581, new Item(found.getId())));
121 }
122
124 public void setValue(PriceType type) {
125 priceType = type;
126 refresh();
127 }
128
130 public void searchItem(String name) {
131 Item searched = null;
132
134 if (item != null && (item.getName().equalsIgnoreCase(name) && !item.isNoted())) {
135 searched = new Item(item.getId(), 1);
136 break;
137 }
138 }
139
140 if (searched == null) {
141 player.send(new SendMessage("There was no item found under the name of " + name + "."));
142 } else {
143 searchedItem = searched;
144 player.send(new SendItemOnInterfaceSlot(48581, searchedItem, 0));
145 player.send(new SendString(searchedItem == null ? "" : "<col=ffb000>" + searchedItem.getName() + ":", 48582));
146 player.send(new SendString(searchedItem == null ? "" : Utility.formatDigits(searchedItem.getValue(priceType)), 48583));
147 }
148 }
149
151 public void deposit(int slot, int amount) {
152 Item item = player.inventory.get(slot);
153 if (item == null)
154 return;
155
156 if (!item.isTradeable()) {
157 player.send(new SendMessage("This is item is untradeable!"));
158 return;
159 }
160
161 int id = item.getId();
162
163 int invAmount = player.inventory.computeAmountForId(id);
164
165 if (invAmount < amount) {
166 amount = invAmount;
167 }
168
169 setFiringEvents(false);
170 add(id, amount);
171 player.inventory.remove(item.getId(), amount);
172 setFiringEvents(true);
173 refresh();
174 }
175
177 public void withdraw(int itemId, int amount) {
178 int slot = computeIndexForId(itemId);
179 if (itemId < 0) return;
180
181 Item item = get(slot);
182 if (item == null || itemId != item.getId()) {
183 return;
184 }
185
186 int contains = computeAmountForId(itemId);
187
188 if (contains < amount) {
189 amount = contains;
190 }
191
192 int id = item.getId();
193 setFiringEvents(false);
194 if (!new Item(id).isStackable() && amount > player.inventory.getFreeSlots()) {
195 amount = player.inventory.getFreeSlots();
196 }
197
198 int fuckingSlot = player.inventory.computeIndexForId(id);
199 if (fuckingSlot != -1) {
200 Item fuckingStan = player.inventory.get(fuckingSlot);
201 if (Integer.MAX_VALUE - fuckingStan.getAmount() < amount) {
202 amount = Integer.MAX_VALUE - fuckingStan.getAmount();
203 player.send(new SendMessage("Your inventory didn't have enough space to withdraw all that!"));
204 }
205 }
206
207
208 if (remove(item.getId(), amount)) {
209 player.inventory.add(id, amount);
210 shift();
211 }
212
213 setFiringEvents(true);
214 refresh();
215 }
216
218 public void depositAll() {
219 Item[] items = player.inventory.toArray();
220 for (int slot = 0; slot < items.length; slot++) {
221 Item item = items[slot];
222 if (item == null) {
223 continue;
224 }
225
226 deposit(slot, item.getAmount());
227 }
228 refresh();
229 }
230
232 public void withdrawAll() {
233 for (Item item : getItems()) {
234 if (item != null) {
235 if (this.remove(item)) {
236 player.inventory.add(item, -1, false);
237 }
238 }
239 }
240 refresh();
241 }
242
243 public void refresh() {
244 refresh(player, PRICE_CHECKER_DISPLAY_ID);
245 }
246
247 @Override
248 public void onRefresh() {
249 for (int index = 0; index < STRINGS.length; index++) {
250 String value = "";
251
252 if (getItems()[index] != null) {
253 int price = getItems()[index].getValue(priceType);
254 int amount = getItems()[index].getAmount();
255
256 value = getItems()[index].isStackable() ? Utility.formatDigits(amount) + " x " + Utility.formatDigits(price) + "\\n" + "= " + Utility.formatDigits(price * amount) : Utility.formatDigits(price);
257 }
258
259 player.send(new SendString(value, STRINGS[index]));
260 }
261
262 player.inventory.refresh();
263 player.send(new SendString("", 48582));
264 player.send(new SendString("", 48583));
265 player.send(new SendItemOnInterfaceSlot(48581, searchedItem, 0));
266 player.send(new SendConfig(237, priceType == PriceType.VALUE ? 1 : 0));
267 player.send(new SendItemOnInterface(InterfaceConstants.INVENTORY_STORE, player.inventory.toArray()));
268 player.send(new SendString(searchedItem == null ? "" : "<col=ffb000>" + searchedItem.getName() + ":", 48582));
269 player.send(new SendString(searchedItem == null ? "" : Utility.formatDigits(searchedItem.getValue(priceType)), 48583));
270 player.send(new SendString("" + (priceType == PriceType.VALUE ? NumberFormat.getInstance().format(containerValue(PriceType.VALUE)) : NumberFormat.getInstance().format(containerValue(PriceType.HIGH_ALCH_VALUE))), 48513));
271 }
272
273 private final class PriceCheckerListener extends ItemContainerAdapter {
274
275 PriceCheckerListener() {
276 super(player);
277 }
278
279 @Override
280 public int getWidgetId() {
281 return 5063;
282 }
283
284 @Override
285 public String getCapacityExceededMsg() {
286 return "Your price checker is currently full!";
287 }
288
289 @Override
290 public void itemUpdated(ItemContainer container, Optional<Item> oldItem, Optional<Item> newItem, int index, boolean refresh, boolean login) {
291 }
292
293 @Override
294 public void bulkItemsUpdated(ItemContainer container) {
295 refresh();
296 }
297 }
298}
ItemContainer(int capacity, StackPolicy policy, Item[] items)
final boolean addListener(ItemContainerListener listener)
static String formatDigits(final int amount)
Definition Utility.java:78