RuneHive-Tarnish
Neural OSRS Enhancement Framework
Loading...
Searching...
No Matches
PersonalStore.java
1package com.osroyale.content.store.impl;
2
3import com.osroyale.Config;
4import com.osroyale.content.dialogue.DialogueFactory;
5import com.osroyale.content.store.*;
6import com.osroyale.content.store.currency.CurrencyType;
7import com.osroyale.game.world.World;
8import com.osroyale.game.world.entity.mob.player.Player;
9import com.osroyale.game.world.items.Item;
10import com.osroyale.game.world.items.containers.ItemContainer;
11import com.osroyale.net.packet.out.*;
12import com.osroyale.util.GameSaver;
13import com.osroyale.util.MessageColor;
14import com.osroyale.util.Utility;
15
16import java.util.*;
17
58
59public final class PersonalStore extends Store {
60
62 public static final Map<Integer, PersonalStore> FEATURED_SHOPS = new HashMap<>();
63
65 public static Map<String, Long> SOLD_ITEMS = new HashMap<>();
66
68 public final int rank;
69
71 public String title;
72
74 public String caption;
75
77 private boolean updating = false;
78
80 public PersonalStore(String name, Optional<StoreItem[]> items, int rank, String title, String caption, CurrencyType currency) {
81 super(name, ItemContainer.StackPolicy.STANDARD, currency, 28);
82 this.rank = rank;
83 this.title = title;
84 this.caption = caption;
85 items.ifPresent(i -> this.container.setItems(i, false));
86 }
87
88 public static void add(Player player, PersonalStore store) {
89 STORES.put(player.getName(), store);
90 }
91
93 public static void claimCoins(Player player) {
94// DialogueFactory factory = player.dialogueFactory;
95// if (!SOLD_ITEMS.containsKey(player.getName())) {
96// factory.sendItem("Personal Store", "There are no coins available for you to collect.", Config.CURRENCY).execute();
97// return;
98// }
99// long amount = SOLD_ITEMS.get(player.getName());
100// boolean added = player.bankVault.add(amount);
101// if (added) {
102// SOLD_ITEMS.remove(player.getName());
103// }
104// String message = added ? "Your collected coins have been sent to your bank vault." : "There was not enough room inside your bank vault to hold your coins.";
105// player.send(new SendMessage(message, MessageColor.DARK_GREEN));
106// openMenu(player);
107// factory.sendItem("Personal Store", message, Config.CURRENCY).execute();
108 }
109
110 public static void openMenu(Player player) {
111 if (player == null) {
112 return;
113 }
114
115 if (player.getName() == null) {
116 return;
117 }
118
119 long collect = SOLD_ITEMS.get(player.getName()) == null ? 0 : SOLD_ITEMS.get(player.getName());
120 player.send(new SendString("<col=d38537>Active stores:</col> " + getPersonalShops().size(), 38210));
121 player.send(new SendString("<col=d38537>Total Items Sold:</col> " + Utility.formatPrice(GameSaver.ITEMS_SOLD), 38211));
122 player.send(new SendString("<col=d38537>Total Sold Worth:</col> " + Utility.formatPrice(GameSaver.PERSONAL_ITEM_WORTH), 38213));
123
124 player.send(new SendString(collect <= 0 ? "None" : Utility.formatDigits(collect), 38207));
125 player.interfaceManager.open(38200);
126 }
127
129 public static void myShop(Player player) {
130 if (!Store.STORES.containsKey(player.getName())) {
131 PersonalStore shop = new PersonalStore(player.getName(), Optional.empty(), player.right.getCrown(), player.getName() + "'s Store", "No caption set", CurrencyType.COINS);
132 Store.STORES.put(player.getName(), shop);
133 }
134
135 STORES.get(player.getName()).open(player);
136 }
137
139 public static void changeName(Player player, String input, boolean caption) {
140 if (!Store.STORES.containsKey(player.getName())) {
141 player.send(new SendMessage("You do not have a store. Set it up by first opening it from the previous menu."));
142 return;
143 }
144
145 String type = caption ? "caption" : "title";
146 if (Arrays.stream(Config.BAD_STRINGS).anyMatch(b -> input.contains(b))) {
147 player.send(new SendMessage("You have entered an invalid shop " + type + "."));
148 player.interfaceManager.close();
149 return;
150 }
151
152 PersonalStore shop = (PersonalStore) Store.STORES.get(player.getName());
153 String context = Utility.formatName(input);
154
155 if (caption) {
156 shop.caption = context;
157 } else {
158 shop.title = context;
159 player.send(new SendString(shop.title, 40002));
160 }
161
162 player.send(new SendMessage("You have changed your shop " + type + " to: " + context + "."));
163 }
164
166 public static void edit(Player player) {
167 DialogueFactory f = player.dialogueFactory;
168 f.sendStatement("Loading your shop details...").sendOption("Change shop name", () -> {
169 f.onAction(() -> {
170 player.send(new SendInputMessage("Enter the name of your shop:", 30, input -> {
171 changeName(player, input, false);
172 }));
173 });
174 }, "Change caption", () -> {
175 f.onAction(() -> {
176 player.send(new SendInputMessage("Enter the caption of your shop:", 30, input -> {
177 changeName(player, input, true);
178 }));
179 });
180 }).execute();
181 }
182
184 public static void openPanel(Player player) {
185 List<PersonalStore> personalShops = getPersonalShops();
186 int size = personalShops.size() < 10 ? 10 : personalShops.size();
187 for (int string = 53031, index = 0; index < size; index++) {
188 PersonalStore shop = index >= personalShops.size() ? null : personalShops.get(index);
189 String tooltip = shop == null ? "" : "View <col=ffb000>" + shop.name + "<col=FFFFFF>'s shop";
190 String name = shop == null ? "" : shop.name;
191 String caption = shop == null ? "" : shop.caption;
192 if (shop != null)
193 player.viewing_shops.put(-(12505 - (index * 3)), shop);
194 player.send(new SendTooltip(tooltip, string));
195 string++;
196 player.send(new SendString(name, string));
197 string++;
198 player.send(new SendString(caption, string));
199 string++;
200 }
201
202 List<PersonalStore> featured_shops = getFeaturedShops();
203 for (int index = 0, string = 53008; index < 10; index++, string++) {
204 PersonalStore shop = index >= featured_shops.size() ? null : featured_shops.get(index);
205 String name = shop == null ? "" : shop.name;
206 String tooltip = shop == null ? "" : "View <col=ffb000>" + shop.name + "<col=FFFFFF>'s shop";
207 if (shop != null)
208 FEATURED_SHOPS.put(-(12528 + index), shop);
209
210 player.send(new SendTooltip(tooltip, string));
211 player.send(new SendString(name, string));
212 }
213 player.send(new SendString("Available stores: " + personalShops.size(), 53023));
214 player.send(new SendScrollbar(53030, (size * 28)));
215 player.interfaceManager.open(53000);
216 }
217
219 public void add(Player player, Item item, int slot, boolean addX) {
220 if (!player.interfaceManager.isInterfaceOpen(StoreConstant.INTERFACE_ID)) {
221 return;
222 }
223 if (!item.isTradeable()) {
224 player.send(new SendMessage("You can not sell untradeable items in your shop!"));
225 return;
226 }
227 if (CurrencyType.isCurrency(item.getId())) {
228 player.send(new SendMessage("You can not sell any currency in your shop!"));
229 return;
230 }
231
232 Item invItem = player.inventory.get(slot);
233 final StoreItem storeItem = new StoreItem(invItem.getId(), item.getAmount());
234
235 if (!addX) {
236 player.send(new SendInputAmount("What do you want to value your <col=027399>" + item.getName() + "</col>?", 10, value -> {
237 setValue(player, invItem, storeItem, Integer.parseInt(value), slot);
238 refresh(player);
239 }));
240 return;
241 }
242
243 player.send(new SendInputAmount("How much would you like to put in your shop?", 10, amount -> {
244 storeItem.setAmount(Integer.parseInt(amount));
245 player.send(new SendInputAmount("What do you want to value your <col=027399>" + item.getName() + "</col>?", 10, value -> {
246 setValue(player, invItem, storeItem, Integer.parseInt(value), slot);
247 refresh(player);
248 }));
249 }));
250 }
251
252 private void setValue(Player player, Item invItem, StoreItem storeItem, int value, int slot) {
253 if (!player.interfaceManager.isInterfaceOpen(StoreConstant.INTERFACE_ID)) {
254 return;
255 }
256 storeItem.setShopValue(value);
257 int amount = player.inventory.computeAmountForId(invItem.getId());
258
259 if (storeItem.getAmount() > amount && !storeItem.isStackable()) {
260 storeItem.setAmount(amount);
261 } else if (storeItem.getAmount() > player.inventory.get(slot).getAmount() && storeItem.isStackable()) {
262 storeItem.setAmount(player.inventory.get(slot).getAmount());
263 }
264
265 player.inventory.remove(storeItem, slot);
266 Optional<Item> contains = container.stream().filter(i -> i != null && storeItem.getId() == i.getId() && ((StoreItem) i).getShopValue() == storeItem.getShopValue()).findFirst();
267
268 if (contains.isPresent()) {
269 contains.get().incrementAmountBy(storeItem.getAmount());
270 } else {
271 if (!storeItem.isStackable() && storeItem.getAmount() > 1) {
272 container.add(storeItem, false, true);
273 } else {
274 container.add(storeItem);
275 }
276 }
277 }
278
280 public void remove(Player player, Item item, int slot) {
281 if (!player.interfaceManager.isInterfaceOpen(StoreConstant.INTERFACE_ID)) {
282 return;
283 }
284 final StoreItem storeItem = (StoreItem) this.container.retrieve(slot).orElse(null);
285
286 if (storeItem == null)
287 return;
288
289 if (item.getAmount() > storeItem.getAmount())
290 item.setAmount(storeItem.getAmount());
291 if (!player.inventory.hasCapacityFor(item)) {
292 item.setAmount(player.inventory.remaining());
293 if (item.getAmount() == 0) {
294 player.send(new SendMessage("You do not have enough space in your inventory to withdraw this item!"));
295 return;
296 }
297 }
298
299 if (player.inventory.remaining() >= item.getAmount() && !item.isStackable() || player.inventory.remaining() >= 1 && item.isStackable() || player.inventory.contains(item.getId()) && item.isStackable()) {
300 if ((storeItem.getAmount() - item.getAmount()) < 1) {
301 container.remove(item, slot, false, false);
302 container.shift();
303 } else {
304 storeItem.decrementAmountBy(item.getAmount());
305 }
306 player.inventory.add(item);
307 } else {
308 player.send(new SendMessage("You don't have enough space in your inventory."));
309 return;
310 }
311 refresh(player);
312 }
313
315 private void modify(Player player, int slot) {
316 if (!player.interfaceManager.isInterfaceOpen(StoreConstant.INTERFACE_ID)) {
317 return;
318 }
319 DialogueFactory factory = player.dialogueFactory;
320 StoreItem item = (StoreItem) container.retrieve(slot).orElse(null);
321 if (item == null)
322 return;
323 updating = true;
324 factory.sendOption(
325 "Change value",
326 () -> factory.onAction(() -> player.send(new SendInputAmount("Enter new value", 10, input -> {
327 item.setShopValue(Integer.parseInt(input));
328 player.send(new SendMessage("You've changed " + item.getName() + "'s value to " + Utility.formatDigits(Integer.parseInt(input)) + " " + item.getShopCurrency(this)));
329 refresh(player);
330 player.dialogueFactory.clear();
331 updating = false;
332 }))),
333
334 "Change currency",
335 () -> factory.onAction(() -> updating = false),
336
337 "Nevermind",
338 () -> factory.onAction(() -> {
339 updating = false;
340 player.dialogueFactory.clear();
341 })).execute();
342 }
343
345 private boolean isOwner(Player player) {
346 return this.name.equals(player.getName());
347 }
348
349 @Override
350 public void itemContainerAction(Player player, int id, int slot, int action, boolean purchase) {
351 switch (action) {
352 case 1:
353 if (purchase) {
354 if (this.isOwner(player)) {
355 this.modify(player, slot);
356 } else {
357 this.sendPurchaseValue(player, slot);
358 }
359 } else {
360 if (this.isOwner(player)) {
361 this.add(player, new Item(id, 1), slot, false);
362 }
363 }
364 break;
365 default:
366 int count = purchase ? this.container.retrieve(slot).orElse(null).getAmount() : player.inventory.retrieve(slot).orElse(null).getAmount();
367 int amount = action == 2 ? (purchase ? 1 : 5) : action == 3 ? 10 : action == 4 ? count : -100;
368
369 if (purchase) {
370 if (this.isOwner(player)) {
371 this.remove(player, new Item(id, amount), slot);
372 } else {
373 if (updating) {
374 player.send(new SendMessage("The owner is currently updating the shop, try again in a few seconds.", MessageColor.RED));
375 return;
376 }
377 this.purchase(player, new Item(id, amount), slot);
378 }
379 } else {
380 if (this.isOwner(player)) {
381 this.add(player, new Item(id, amount), slot, amount == -100);
382 }
383 }
384 break;
385 }
386 }
387
388 @Override
389 public void onPurchase(Player player, Item item) {
390 if (!SOLD_ITEMS.containsKey(name))
391 SOLD_ITEMS.put(name, 0L);
392
393 long amount = SOLD_ITEMS.get(name);
394 SOLD_ITEMS.replace(name, amount + item.getAmount());
395 GameSaver.ITEMS_SOLD++;
396 GameSaver.PERSONAL_ITEM_WORTH += (item.getValue() * item.getAmount());
397 World.search(name).ifPresent(p -> p.send(new SendMessage("You have coins to collect from your shop!", MessageColor.DARK_GREEN)));
398 }
399
400 @Override
401 public void refresh(Player player) {
402 for (Player p : players) {
403 if (p != null) {
404 open(p);
405 if (p != player)
406 p.send(new SendMessage("[" + title + "] The shop has been updated!"));
407 }
408 }
409 }
410
411 @Override
412 public void open(Player player) {
413 player.attributes.set("SHOP", name);
414 players.add(player);
415 StoreItem[] items = (StoreItem[]) this.container.getItems();
416
417 int lastItem = 0;
418 if (items.length != 0) {
419 for (int i = 0; i < items.length; i++) {
420 player.send(new SendString(items[i] == null ? "0" : items[i].getShopValue() + "," + 0, 40052 + i));
421 lastItem = i;
422 }
423 }
424 final int scrollBarSize = lastItem <= 32 ? 0 : (lastItem / 8) * 72;
425 player.send(new SendScrollbar(40050, scrollBarSize));
426 player.send(new SendString(title, 40002));
427 player.send(new SendItemOnInterface(40051, items));
428 player.send(new SendItemOnInterface(3823, player.inventory.toArray()));
429 player.send(new SendString("Store size: " + items.length, 40007));
430 player.interfaceManager.openInventory(StoreConstant.INTERFACE_ID, 3822);
431 }
432
433 @Override
434 public void close(Player player) {
435 players.remove(player);
436 player.attributes.remove("SHOP");
437 player.viewing_shops.clear();
438 }
439
440 @Override
441 public StoreType type() {
442 return StoreType.PERSONAL;
443 }
444
445 @Override
446 public SellType sellType() {
447 return SellType.NONE;
448 }
449}
static final String[] BAD_STRINGS
Definition Config.java:254
final DialogueFactory sendStatement(String... lines)
final DialogueFactory sendOption(String option1, Runnable action1, String option2, Runnable action2)
final DialogueFactory onAction(Runnable action)
void add(Player player, Item item, int slot, boolean addX)
static final Map< Integer, PersonalStore > FEATURED_SHOPS
static void changeName(Player player, String input, boolean caption)
PersonalStore(String name, Optional< StoreItem[]> items, int rank, String title, String caption, CurrencyType currency)
static String formatDigits(final int amount)
Definition Utility.java:78
static String formatPrice(final long amount)
Definition Utility.java:93