RuneHive-Tarnish
Neural OSRS Enhancement Framework
Loading...
Searching...
No Matches
DefaultStore.java
1package com.osroyale.content.store.impl;
2
3import com.osroyale.content.store.*;
4import com.osroyale.content.store.currency.CurrencyType;
5import com.osroyale.game.task.TickableTask;
6import com.osroyale.game.world.World;
7import com.osroyale.game.world.entity.mob.player.Player;
8import com.osroyale.game.world.entity.mob.player.PlayerRight;
9import com.osroyale.game.world.items.Item;
10import com.osroyale.game.world.items.containers.ItemContainer;
11import com.osroyale.net.packet.out.*;
12
13import java.util.Arrays;
14import java.util.Objects;
15
58
59public final class DefaultStore extends Store {
60
62 public final StoreItem[] items;
63
65 final ItemContainer original;
66
68 final boolean restock;
69
70 private final SellType sellType;
71
73 private StoreRestockTask restockTask;
74
76 public DefaultStore(StoreItem[] items, String name, SellType sellType, boolean restock, CurrencyType currency) {
77 super(name, ItemContainer.StackPolicy.ALWAYS, currency, sellType == SellType.ANY ? 80 : items.length);
78 this.items = items;
79 this.restock = restock;
80 this.sellType = sellType;
81 this.original = new ItemContainer(items.length, ItemContainer.StackPolicy.ALWAYS);
82 this.original.setItems(items, false);
83 this.container.setItems(items, false);
84 Arrays.stream(items).filter(Objects::nonNull).forEach(item -> itemCache.put(item.getId(), item.getAmount()));
85 }
86
88 private boolean needsRestock() {
89 return container.stream().filter(Objects::nonNull).anyMatch(i -> !itemCache.containsKey(i.getId()) || (itemCache.containsKey(i.getId()) && i.getAmount() < itemCache.get(i.getId())));
90 }
91
93 boolean restockCompleted() {
94 return container.stream().filter(Objects::nonNull).allMatch(i -> {
95 if (itemCache.containsKey(i.getId()) && i.getAmount() >= itemCache.get(i.getId())) {//shop item.
96 return true;
97 } else if (!itemCache.containsKey(i.getId())) {//unique item.
98 return false;
99 }
100 return false;
101 });
102 }
103
104 @Override
105 public void itemContainerAction(Player player, int id, int slot, int action, boolean purchase) {
106 switch (action) {
107 case 1:
108 if (purchase) {
109 this.sendPurchaseValue(player, slot);
110 } else {
111 this.sendSellValue(player, slot);
112 }
113 break;
114 case 5:
115 player.send(new SendInputAmount("Enter amount", 10, amount -> {
116 if (purchase) {
117 this.purchase(player, new Item(id, Integer.parseInt(amount)), slot);
118 } else {
119 this.sell(player, new Item(id, Integer.parseInt(amount)), slot, true);
120 }
121 }));
122 break;
123 default:
124 int amount = 0;
125
126 if (action == 2) {
127 amount = 1;
128 }
129 if (action == 3) {
130 amount = 10;
131 }
132 if (action == 4) {
133 amount = 100;
134 }
135
136 if (purchase) {
137 this.purchase(player, new Item(id, amount), slot);
138 } else {
139 this.sell(player, new Item(id, amount), slot, false);
140 }
141 break;
142 }
143 }
144
145 @Override
146 public void open(Player player) {
147 if (PlayerRight.isIronman(player)) {
148 if (Arrays.stream(StoreConstant.IRON_MAN_STORES).noneMatch(s -> s.equalsIgnoreCase(name))) {
149 player.send(new SendMessage("As an iron man you do not have access to this store!"));
150 return;
151 }
152 }
153
154 player.attributes.set("SHOP", name);
155
156 if (!STORES.containsKey(name)) {
157 STORES.put(name, this);
158 }
159
160 players.add(player);
161 player.inventory.refresh();
162 refresh(player);
163 player.send(new SendString(name, 47502));
164 player.interfaceManager.openInventory(StoreConstant.INTERFACE_ID, 3822);
165 }
166
167 @Override
168 public void close(Player player) {
169 players.remove(player);
170 player.attributes.remove("SHOP");
171 }
172
173 @Override
174 public void refresh(Player player) {
175 player.send(new SendString("Store size: " + items.length, 47507));
176 player.send(new SendString(CurrencyType.getValue(player, currencyType), 47508));
177
178 final Item[] items = container.toArray();
179
180 int lastItem = 0;
181 for (int i = 0; i < items.length; i++) {
182 Item item = items[i];
183
184 if (item == null) {
185 continue;
186 }
187
188 if (item instanceof StoreItem) {
189 StoreItem storeItem = (StoreItem) items[i];
190 player.send(new SendString(storeItem.getShopValue() + "," + storeItem.getShopCurrency(this).getId(), 47552 + i));
191 lastItem = i;
192 }
193 }
194
195 final int scrollBarSize = lastItem <= 32 ? 0 : (lastItem / 8) * 72;
196 player.send(new SendScrollbar(47550, scrollBarSize));
197 player.send(new SendItemOnInterface(3823, player.inventory.toArray()));
198 players.stream().filter(Objects::nonNull).forEach(p -> player.send(new SendItemOnInterface(47551, container.toArray())));
199 if (restock) {
200 if (restockTask != null && restockTask.isRunning()) {
201 return;
202 }
203 if (!needsRestock()) {
204 return;
205 }
206 restockTask = new StoreRestockTask(this);
207 World.schedule(restockTask);
208 }
209 }
210
211 @Override
212 public StoreType type() {
213 return StoreType.DEFAULT;
214 }
215
216 @Override
217 public SellType sellType() {
218 return sellType;
219 }
220
221
223 private static final class StoreRestockTask extends TickableTask {
224
226 private final DefaultStore container;
227
229 StoreRestockTask(DefaultStore container) {
230 super(false, 0);
231 this.container = container;
232 }
233
234 @Override
235 protected void tick() {
236 if (container.restockCompleted() || !container.restock) {
237 this.cancel();
238 return;
239 }
240
241 if (tick >= 15) {
242 final Item[] items = container.container.toArray();
243 boolean restocked = false;
244 for (Item item : items) {
245 if (item == null) {
246 continue;
247 }
248 if (item instanceof StoreItem) {
249 if (restock((StoreItem) item)) {
250 restocked = true;
251 }
252 }
253 }
254 if (restocked) {
255 for (Player player : container.players) {
256 if (player != null) {
257 player.send(new SendItemOnInterface(47551, container.container.toArray()));
258 }
259 }
260 }
261 tick = 0;
262 }
263
264 }
265
267 private boolean restock(StoreItem item) {
268 if (!item.canReduce()) {
269 return false;
270 }
271
272 final int reduceAmount = item.getAmount() > 100 ? (int) ((double) item.getAmount() * 0.05D) : 1;
273
274 // if the item is not an original item
275 if (!container.original.contains(item.getId())) {
276 if (item.getAmount() - 1 <= 0) {
277 container.container.remove(item);
278 } else {
279 item.decrementAmountBy(reduceAmount);
280 }
281 return true;
282 } else {
283 // the item is an original item
284 final boolean originalItem = container.itemCache.containsKey(item.getId());
285 final int originalAmount = container.itemCache.get(item.getId());
286 // increment the original item if its not fully stocked
287 if (originalItem && item.getAmount() < originalAmount) {
288 item.incrementAmount();
289 return true;
290 } else if (originalItem && item.getAmount() > originalAmount) { // decrement original item if its over stocked
291 item.decrementAmountBy(reduceAmount);
292 return true;
293 }
294 }
295 return false;
296 }
297 }
298}
DefaultStore(StoreItem[] items, String name, SellType sellType, boolean restock, CurrencyType currency)
synchronized final void cancel()
Definition Task.java:147
final void setItems(Item[] items, boolean copy)