62 public static final Map<Integer, PersonalStore>
FEATURED_SHOPS =
new HashMap<>();
65 public static Map<String, Long>
SOLD_ITEMS =
new HashMap<>();
77 private boolean updating =
false;
85 items.ifPresent(i -> this.container.setItems(i,
false));
89 STORES.put(player.
getName(), store);
110 public static void openMenu(
Player player) {
111 if (player ==
null) {
115 if (player.
getName() ==
null) {
120 player.send(
new SendString(
"<col=d38537>Active stores:</col> " + getPersonalShops().size(), 38210));
125 player.interfaceManager.
open(38200);
130 if (!Store.STORES.containsKey(player.
getName())) {
132 Store.STORES.put(player.
getName(), shop);
135 STORES.get(player.
getName()).open(player);
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."));
145 String type =
caption ?
"caption" :
"title";
147 player.send(
new SendMessage(
"You have entered an invalid shop " + type +
"."));
148 player.interfaceManager.close();
153 String context =
Utility.formatName(input);
156 shop.caption = context;
158 shop.title = context;
162 player.send(
new SendMessage(
"You have changed your shop " + type +
" to: " + context +
"."));
170 player.send(
new SendInputMessage(
"Enter the name of your shop:", 30, input -> {
174 },
"Change caption", () -> {
176 player.send(
new SendInputMessage(
"Enter the caption of your shop:", 30, input -> {
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;
193 player.viewing_shops.put(-(12505 - (index * 3)), shop);
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";
213 player.send(
new SendString(
"Available stores: " + personalShops.size(), 53023));
215 player.interfaceManager.
open(53000);
219 public void add(
Player player, Item item,
int slot,
boolean addX) {
223 if (!item.isTradeable()) {
224 player.send(
new SendMessage(
"You can not sell untradeable items in your shop!"));
228 player.send(
new SendMessage(
"You can not sell any currency in your shop!"));
232 Item invItem = player.inventory.
get(slot);
233 final StoreItem storeItem =
new StoreItem(invItem.getId(), item.getAmount());
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);
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);
252 private void setValue(
Player player, Item invItem, StoreItem storeItem,
int value,
int slot) {
256 storeItem.setShopValue(value);
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());
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();
268 if (contains.isPresent()) {
269 contains.get().incrementAmountBy(storeItem.getAmount());
271 if (!storeItem.isStackable() && storeItem.getAmount() > 1) {
272 container.add(storeItem,
false,
true);
274 container.add(storeItem);
280 public void remove(
Player player, Item item,
int slot) {
284 final StoreItem storeItem = (StoreItem) this.container.retrieve(slot).orElse(
null);
286 if (storeItem ==
null)
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!"));
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);
304 storeItem.decrementAmountBy(item.getAmount());
306 player.inventory.add(item);
308 player.send(
new SendMessage(
"You don't have enough space in your inventory."));
315 private void modify(
Player player,
int slot) {
319 DialogueFactory factory = player.dialogueFactory;
320 StoreItem item = (StoreItem) container.retrieve(slot).orElse(
null);
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)));
330 player.dialogueFactory.
clear();
335 () -> factory.onAction(() -> updating =
false),
338 () -> factory.onAction(() -> {
340 player.dialogueFactory.clear();
345 private boolean isOwner(
Player player) {
346 return this.name.equals(player.
getName());
350 public void itemContainerAction(
Player player,
int id,
int slot,
int action,
boolean purchase) {
354 if (this.isOwner(player)) {
355 this.modify(player, slot);
357 this.sendPurchaseValue(player, slot);
360 if (this.isOwner(player)) {
361 this.add(player,
new Item(
id, 1), slot,
false);
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;
370 if (this.isOwner(player)) {
371 this.
remove(player,
new Item(
id, amount), slot);
374 player.send(
new SendMessage(
"The owner is currently updating the shop, try again in a few seconds.", MessageColor.RED));
377 this.purchase(player,
new Item(
id, amount), slot);
380 if (this.isOwner(player)) {
381 this.add(player,
new Item(
id, amount), slot, amount == -100);
389 public void onPurchase(Player player, Item item) {
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)));
401 public void refresh(Player player) {
402 for (Player p : players) {
406 p.send(
new SendMessage(
"[" +
title +
"] The shop has been updated!"));
412 public void open(Player player) {
413 player.attributes.set(
"SHOP", name);
415 StoreItem[] items = (StoreItem[]) this.container.
getItems();
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));
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);
434 public void close(Player player) {
435 players.remove(player);
436 player.attributes.remove(
"SHOP");
437 player.viewing_shops.clear();
441 public StoreType type() {
442 return StoreType.PERSONAL;
446 public SellType sellType() {
447 return SellType.NONE;
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)
static Map< String, Long > SOLD_ITEMS
PersonalStore(String name, Optional< StoreItem[]> items, int rank, String title, String caption, CurrencyType currency)