RuneHive-Game
Loading...
Searching...
No Matches
PriceChecker.java
Go to the documentation of this file.
1package com.runehive.game.world.items.containers.pricechecker;
2
3import com.runehive.game.world.InterfaceConstants;
4import com.runehive.game.world.entity.mob.player.Player;
5import com.runehive.game.world.items.Item;
6import com.runehive.game.world.items.ItemDefinition;
7import com.runehive.game.world.items.containers.ItemContainer;
8import com.runehive.game.world.items.containers.ItemContainerAdapter;
9import com.runehive.net.packet.out.*;
10import com.runehive.util.Utility;
11
12import java.text.NumberFormat;
13import java.util.Optional;
14
15/**
16 * The price checker container.
17 *
18 * @author Daniel
19 */
20public class PriceChecker extends ItemContainer {
21
22 /** Holds all the string identifications */
23 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,};
24
25 /** The price checker display widget identifier. */
26 private static final int PRICE_CHECKER_DISPLAY_ID = 48542;
27
28 /** The player instance. */
29 public Player player;
30
31 /** The price type. */
33
34 /** The item being searched. */
36
37 /** Creates a new <code>PriceChecker<code>. */
39 super(28, StackPolicy.STANDARD);
40 this.player = player;
41 this.priceType = PriceType.VALUE;
43 }
44
45 /** Opens the price checker interface. */
46 public void open() {
47 refresh();
48 player.send(new SendString("", 48583));
49 player.send(new SendString("\\nSelect item to search", 48582));
50 player.send(new SendItemOnInterface(48581));
51 player.attributes.set("PRICE_CHECKER_KEY", true);
52 player.interfaceManager.openInventory(48500, 5063);
53 }
54
55 /** Closes the price checker interface. */
56 public void close() {
58 searchedItem = null;
59 player.attributes.set("PRICE_CHECKER_KEY", false);
60 }
61
62 /** Searches for an item. */
63 public void search(String name) {
64 ItemDefinition found = null;
65
66 for (ItemDefinition definition : ItemDefinition.DEFINITIONS) {
67 if (definition == null)
68 continue;
69 String searched = definition.getName().toLowerCase().replace("'", "");
70 if (!searched.contains(name.toLowerCase()))
71 continue;
72 found = definition;
73 break;
74 }
75
76 if (found == null) {
77 player.dialogueFactory.sendStatement("There were no results found for", "<col=255>" + name).execute();
78 return;
79 }
80
81 player.send(new SendString(Utility.formatDigits(found.getValue()), 48582));
82 player.send(new SendString(found.getName(), 48583));
83 player.send(new SendItemOnInterface(48581, new Item(found.getId())));
84 }
85
86 /** Sets the calculating value of the price checker. */
87 public void setValue(PriceType type) {
88 priceType = type;
89 refresh();
90 }
91
92 /** Searches for an item. */
93 public void searchItem(String name) {
94 Item searched = null;
95
97 if (item != null && (item.getName().equalsIgnoreCase(name) && !item.isNoted())) {
98 searched = new Item(item.getId(), 1);
99 break;
100 }
101 }
102
103 if (searched == null) {
104 player.send(new SendMessage("There was no item found under the name of " + name + "."));
105 } else {
106 searchedItem = searched;
107 player.send(new SendItemOnInterfaceSlot(48581, searchedItem, 0));
108 player.send(new SendString(searchedItem == null ? "" : "<col=ffb000>" + searchedItem.getName() + ":", 48582));
109 player.send(new SendString(searchedItem == null ? "" : Utility.formatDigits(searchedItem.getValue(priceType)), 48583));
110 }
111 }
112
113 /** Deposits an item into the price checker. */
114 public void deposit(int slot, int amount) {
115 Item item = player.inventory.get(slot);
116 if (item == null)
117 return;
118
119 if (!item.isTradeable()) {
120 player.send(new SendMessage("This is item is untradeable!"));
121 return;
122 }
123
124 int id = item.getId();
125
126 int invAmount = player.inventory.computeAmountForId(id);
127
128 if (invAmount < amount) {
129 amount = invAmount;
130 }
131
132 setFiringEvents(false);
133 add(id, amount);
134 player.inventory.remove(item.getId(), amount);
135 setFiringEvents(true);
136 refresh();
137 }
138
139 /** Withdraws an item from the price checker. */
140 public void withdraw(int itemId, int amount) {
141 int slot = computeIndexForId(itemId);
142 if (itemId < 0) return;
143
144 Item item = get(slot);
145 if (item == null || itemId != item.getId()) {
146 return;
147 }
148
149 int contains = computeAmountForId(itemId);
150
151 if (contains < amount) {
152 amount = contains;
153 }
154
155 int id = item.getId();
156 setFiringEvents(false);
157 if (!new Item(id).isStackable() && amount > player.inventory.getFreeSlots()) {
158 amount = player.inventory.getFreeSlots();
159 }
160
161 int fuckingSlot = player.inventory.computeIndexForId(id);
162 if (fuckingSlot != -1) {
163 Item fuckingStan = player.inventory.get(fuckingSlot);
164 if (Integer.MAX_VALUE - fuckingStan.getAmount() < amount) {
165 amount = Integer.MAX_VALUE - fuckingStan.getAmount();
166 player.send(new SendMessage("Your inventory didn't have enough space to withdraw all that!"));
167 }
168 }
169
170
171 if (remove(item.getId(), amount)) {
172 player.inventory.add(id, amount);
173 shift();
174 }
175
176 setFiringEvents(true);
177 refresh();
178 }
179
180 /** Deposits all the items into the price checker. */
181 public void depositAll() {
182 Item[] items = player.inventory.toArray();
183 for (int slot = 0; slot < items.length; slot++) {
184 Item item = items[slot];
185 if (item == null) {
186 continue;
187 }
188
189 deposit(slot, item.getAmount());
190 }
191 refresh();
192 }
193
194 /** Withdraw all the items from the price checker. */
195 public void withdrawAll() {
196 for (Item item : getItems()) {
197 if (item != null) {
198 if (this.remove(item)) {
199 player.inventory.add(item, -1, false);
200 }
201 }
202 }
203 refresh();
204 }
205
206 public void refresh() {
208 }
209
210 @Override
211 public void onRefresh() {
212 for (int index = 0; index < STRINGS.length; index++) {
213 String value = "";
214
215 if (getItems()[index] != null) {
216 int price = getItems()[index].getValue(priceType);
217 int amount = getItems()[index].getAmount();
218
219 value = getItems()[index].isStackable() ? Utility.formatDigits(amount) + " x " + Utility.formatDigits(price) + "\\n" + "= " + Utility.formatDigits(price * amount) : Utility.formatDigits(price);
220 }
221
222 player.send(new SendString(value, STRINGS[index]));
223 }
224
225 player.inventory.refresh();
226 player.send(new SendString("", 48582));
227 player.send(new SendString("", 48583));
228 player.send(new SendItemOnInterfaceSlot(48581, searchedItem, 0));
229 player.send(new SendConfig(237, priceType == PriceType.VALUE ? 1 : 0));
231 player.send(new SendString(searchedItem == null ? "" : "<col=ffb000>" + searchedItem.getName() + ":", 48582));
232 player.send(new SendString(searchedItem == null ? "" : Utility.formatDigits(searchedItem.getValue(priceType)), 48583));
233 player.send(new SendString("" + (priceType == PriceType.VALUE ? NumberFormat.getInstance().format(containerValue(PriceType.VALUE)) : NumberFormat.getInstance().format(containerValue(PriceType.HIGH_ALCH_VALUE))), 48513));
234 }
235
236 private final class PriceCheckerListener extends ItemContainerAdapter {
237
239 super(player);
240 }
241
242 @Override
243 public int getWidgetId() {
244 return 5063;
245 }
246
247 @Override
248 public String getCapacityExceededMsg() {
249 return "Your price checker is currently full!";
250 }
251
252 @Override
253 public void itemUpdated(ItemContainer container, Optional<Item> oldItem, Optional<Item> newItem, int index, boolean refresh, boolean login) {
254 }
255
256 @Override
257 public void bulkItemsUpdated(ItemContainer container) {
258 refresh();
259 }
260 }
261}
The class that contains helpful information on interfaces.
This class represents a character controlled by a player.
Definition Player.java:125
Represents all of an in-game Item's attributes.
boolean isNoted()
Gets the item note state.
static ItemDefinition[] DEFINITIONS
An array of item definitions.
The container class that represents an item that can be interacted with.
Definition Item.java:21
final int getId()
Gets the identification of this item.
Definition Item.java:324
final int getAmount()
Gets the quantity of this item.
Definition Item.java:342
int getValue(PriceType type)
Gets the value for this item.
Definition Item.java:98
ItemContainerAdapter(Player player)
Creates a new ItemContainerAdapter.
ItemContainer(int capacity, StackPolicy policy, Item[] items)
Creates a new ItemContainer.
void setFiringEvents(boolean firingEvents)
Sets the value for firingEvents.
final int computeAmountForId(int id)
Computes the total quantity of the Items in this container with id.
Item[] items
The Items within this container.
boolean add(Item item)
Attempts to deposit item into this container.
final int computeIndexForId(int id)
Computes the first index found that id is in.
final boolean addListener(ItemContainerListener listener)
Adds an ItemContainerListener to this container.
boolean contains(int id)
Determines if this container contains id.
void shift()
Percolates the null indices to the end of the stack.
long containerValue(PriceType type)
Gets the total worth of the container using the item's values.
void bulkItemsUpdated(ItemContainer container)
Fired when an Items are added, removed, or replaced in bulk.
void itemUpdated(ItemContainer container, Optional< Item > oldItem, Optional< Item > newItem, int index, boolean refresh, boolean login)
Fired when an Item is added, removed, or replaced.
static final int PRICE_CHECKER_DISPLAY_ID
The price checker display widget identifier.
void withdrawAll()
Withdraw all the items from the price checker.
void depositAll()
Deposits all the items into the price checker.
void onRefresh()
Any functionality that should occur when refreshed.
void deposit(int slot, int amount)
Deposits an item into the price checker.
void withdraw(int itemId, int amount)
Withdraws an item from the price checker.
void setValue(PriceType type)
Sets the calculating value of the price checker.
final int[] STRINGS
Holds all the string identifications.
The OutgoingPacket responsible for changing settings on a client.
The OutgoingPacket that sends a message to a Players chatbox in the client.
The OutgoingPacket that sends a string to a Players itemcontainer in the client.
Handles miscellaneous methods.
Definition Utility.java:27
static String formatDigits(final int amount)
Formats digits for integers.
Definition Utility.java:41
An enumerated type defining policies for stackable Items.
STANDARD
The STANDARD policy, items are only stacked if they are defined as stackable in their ItemDefinition ...