68 final boolean restock;
73 private StoreRestockTask restockTask;
79 this.restock = restock;
80 this.sellType = sellType;
83 this.container.setItems(
items,
false);
84 Arrays.stream(
items).filter(Objects::nonNull).forEach(item -> itemCache.put(item.getId(), item.getAmount()));
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())));
93 boolean restockCompleted() {
94 return container.stream().filter(Objects::nonNull).allMatch(i -> {
95 if (itemCache.containsKey(i.getId()) && i.getAmount() >= itemCache.get(i.getId())) {
97 }
else if (!itemCache.containsKey(i.getId())) {
105 public void itemContainerAction(
Player player,
int id,
int slot,
int action,
boolean purchase) {
109 this.sendPurchaseValue(player, slot);
111 this.sendSellValue(player, slot);
115 player.send(
new SendInputAmount(
"Enter amount", 10, amount -> {
117 this.purchase(player,
new Item(
id, Integer.parseInt(amount)), slot);
119 this.sell(player,
new Item(
id, Integer.parseInt(amount)), slot,
true);
137 this.purchase(player,
new Item(
id, amount), slot);
139 this.sell(player,
new Item(
id, amount), slot,
false);
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!"));
154 player.attributes.set(
"SHOP", name);
156 if (!STORES.containsKey(name)) {
157 STORES.put(name,
this);
163 player.send(
new SendString(name, 47502));
164 player.interfaceManager.
openInventory(StoreConstant.INTERFACE_ID, 3822);
168 public void close(Player player) {
169 players.remove(player);
170 player.attributes.remove(
"SHOP");
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));
181 for (
int i = 0; i <
items.length; i++) {
182 Item item =
items[i];
188 if (item instanceof StoreItem) {
189 StoreItem storeItem = (StoreItem)
items[i];
190 player.send(
new SendString(storeItem.getShopValue() +
"," + storeItem.getShopCurrency(
this).getId(), 47552 + i));
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())));
200 if (restockTask !=
null && restockTask.isRunning()) {
203 if (!needsRestock()) {
206 restockTask =
new StoreRestockTask(
this);
207 World.schedule(restockTask);
212 public StoreType type() {
213 return StoreType.DEFAULT;
217 public SellType sellType() {
223 private static final class StoreRestockTask
extends TickableTask {
231 this.container = container;
235 protected void tick() {
236 if (container.restockCompleted() || !container.restock) {
242 final Item[]
items = container.container.toArray();
243 boolean restocked =
false;
244 for (Item item :
items) {
248 if (item instanceof StoreItem) {
249 if (restock((StoreItem) item)) {
255 for (Player player : container.players) {
256 if (player !=
null) {
257 player.send(
new SendItemOnInterface(47551, container.container.toArray()));
267 private boolean restock(StoreItem item) {
268 if (!item.canReduce()) {
272 final int reduceAmount = item.getAmount() > 100 ? (int) ((
double) item.getAmount() * 0.05D) : 1;
275 if (!container.original.contains(item.getId())) {
276 if (item.getAmount() - 1 <= 0) {
277 container.container.remove(item);
279 item.decrementAmountBy(reduceAmount);
284 final boolean originalItem = container.itemCache.containsKey(item.getId());
285 final int originalAmount = container.itemCache.get(item.getId());
287 if (originalItem && item.getAmount() < originalAmount) {
288 item.incrementAmount();
290 }
else if (originalItem && item.getAmount() > originalAmount) {
291 item.decrementAmountBy(reduceAmount);
DefaultStore(StoreItem[] items, String name, SellType sellType, boolean restock, CurrencyType currency)