RuneHive-Tarnish
Neural OSRS Enhancement Framework
Loading...
Searching...
No Matches
ExchangeSession.java
1package com.osroyale.game.world.entity.mob.player.exchange;
2
3import com.google.common.collect.ImmutableSet;
4import com.osroyale.content.activity.ActivityType;
5import com.osroyale.content.gambling.GambleStage;
6import com.osroyale.game.task.impl.DuelNotificationTask;
7import com.osroyale.game.task.impl.SessionRemovalNotificationTask;
8import com.osroyale.game.world.World;
9import com.osroyale.game.world.entity.mob.player.Player;
10import com.osroyale.game.world.entity.mob.player.PlayerRight;
11import com.osroyale.game.world.items.Item;
12import com.osroyale.game.world.items.containers.ItemContainer;
13import com.osroyale.game.world.items.containers.inventory.Inventory;
14import com.osroyale.game.world.position.Position;
15
16import java.util.*;
17import java.util.function.Consumer;
18
59
60public abstract class ExchangeSession {
61
63 public static final Set<ExchangeSession> SESSIONS = new HashSet<>();
64
66 public final Player player;
67
69 public final Player other;
70
73
75 public final Map<Player, ItemContainer> item_containers = new HashMap<>();
76
81 private Object attachment;
82
85 this.player = player;
86 this.other = other;
87 this.type = type;
89 }
90
92 public boolean canOffer = true;
93
95 public boolean finalized;
96
98 public abstract boolean onRequest();
99
101 public abstract void accept(Player player, String COMPONENT);
102
104 public abstract boolean canAddItem(Player player, Item item, int slot);
105
107 public abstract boolean canRemoveItem(Player player, Item item, int slot);
108
110 public abstract boolean onButtonClick(Player player, int button);
111
113 public abstract void updateMainComponents(String component);
114
116 public abstract void updateOfferComponents();
117
119 public abstract void onReset();
120
122 public boolean request() {
123 if (inAnySession()) {
124 player.exchangeSession.reset();
125 return false;
126 }
127 /* if (PlayerRight.isAdministrator(player) && !PlayerRight.isOwner(player) || !PlayerRight.isDeveloper(player)) {
128 player.message("You can not exchange as an administrator.");
129 return false;
130 }
131 if (PlayerRight.isAdministrator(other) && !PlayerRight.isOwner(other) || !PlayerRight.isDeveloper(other)) {
132 player.message("You can not exchange an administrator.");
133 return false;
134 }*/
135
136 if (getSession(other).isPresent() && getSession(other).get().inAnySession()) {
137 player.message("This player is currently is a " + type.name + " with another player.");
138 return false;
139 }
140 if (Objects.equals(player, other)) {
141 player.message("You cannot " + type.name + " with yourself.");
142 return false;
143 }
144
145 if(player.getGambling().getStage().equals(GambleStage.PLACING_BET) || player.getGambling().getStage().equals(GambleStage.IN_PROGRESS)) {
146 return false;
147 }
148
149 if(other.getGambling().getStage().equals(GambleStage.PLACING_BET) || other.getGambling().getStage().equals(GambleStage.IN_PROGRESS)) {
150 return false;
151 }
152
153 if (PlayerRight.isIronman(player) && !PlayerRight.isOwner(other)) {
154 player.message("You can not " + type.name + " as you are an iron man.");
155 return false;
156 }
157 if (PlayerRight.isIronman(other) && !PlayerRight.isOwner(player)) {
158 player.message(other.getName() + " can not be " + type.name + "d as they are an iron man.");
159 return false;
160 }
161 if (player.exchangeSession.requested_players.contains(other)) {
162 player.message("You have already sent a request to this player.");
163 return false;
164 }
165 if (player.locking.locked()) {
166 player.message("You cannot send a " + type.name + " request right now.");
167 return false;
168 }
169 if (other.locking.locked()) {
170 player.message(other.getName() + " is currently busy.");
171 return false;
172 }
173 if (player.playerAssistant.busy()) {
174 player.message("Please finish what you are doing before you do that.");
175 return false;
176 }
177 if (other.playerAssistant.busy()) {
178 player.message(other.getName() + " is currently busy.");
179 return false;
180 }
181 if (player.inActivity(ActivityType.DUEL_ARENA)) {
182 player.message("You can not do that whilst in a duel!");
183 return false;
184 }
185 if (other.inActivity(ActivityType.DUEL_ARENA)) {
186 other.message("You can not do that whilst in a duel!");
187 return false;
188 }
189 if (!onRequest()) {
190 return false;
191 }
192 if(this.type == ExchangeSessionType.DUEL && !other.getPosition().inLocation(new Position(3311, 3201), new Position(3394, 3287), true)) {
193 player.exchangeSession.reset();
194 return false;
195 }
196 return SESSIONS.add(this);
197 }
198
200 public final boolean add(Player player, int slot, int amount) {
201 Item invItem = player.inventory.get(slot);
202 if (invItem == null) {
203 return false;
204 }
205 if (!Item.valid(invItem) || !player.inventory.contains(invItem.getId())) {
206 return false;
207 }
208 if (!canOffer) {
209 return false;
210 }
211 if (!inSession(player, type)) {
212 return false;
213 }
214 if (!canAddItem(player, invItem, slot)) {
215 return false;
216 }
217 if (!invItem.isTradeable() && !PlayerRight.isOwner(player)) {
218 player.message("You can't offer this item.");
219 return false;
220 }
221 Item item = new Item(invItem.getId(), amount);
222 int count = player.inventory.computeAmountForId(item.getId());
223 if (item.getAmount() > count) {
224 item.setAmount(count);
225 }
226 if (item_containers.get(player).add(item)) {
227 player.inventory.remove(item);
229 return true;
230 }
231 return false;
232 }
233
235 public final boolean remove(Player player, int slot, int amount) {
236 Item containerItem = this.item_containers.get(player).get(slot);
237 if (player == null || containerItem == null) {
238 return false;
239 }
240 if (!Item.valid(containerItem) || !this.item_containers.get(player).contains(containerItem.getId())) {
241 return false;
242 }
243 if (!canOffer) {
244 return false;
245 }
246 if (!inSession(player, type)) {
247 return false;
248 }
249 if (!canRemoveItem(player, containerItem, slot)) {
250 return false;
251 }
252 Item item = new Item(containerItem.getId(), amount == -1 ? this.item_containers.get(player).computeAmountForId(containerItem.getId()) : amount);
253 int count = item_containers.get(player).computeAmountForId(item.getId());
254 if (item.getAmount() > count) {
255 item.setAmount(count);
256 }
257 if (item_containers.get(player).remove(item)) {
258 player.inventory.add(item);
259 item_containers.get(player).shift();
260
261 if (this.player.equals(player)) {
263 } else if (player.equals(other)) {
264 World.schedule(this.type == ExchangeSessionType.DUEL ? new DuelNotificationTask(this.player) : new SessionRemovalNotificationTask(this.player));
265 }
266
268 return true;
269 }
270 return false;
271 }
272
274 public void setAttachment(Object attachment) {
275 this.attachment = attachment;
276 }
277
279 public Object getAttachment() {
280 return attachment;
281 }
282
284 public boolean hasAttachment() {
285 return Objects.nonNull(attachment);
286 }
287
290 return SESSIONS.stream().anyMatch(session -> allMatch(session, player.getName(), other.getName()) && session.type.equals(type));
291 }
292
295 return getSession(player, type).isPresent();
296 }
297
299 public boolean inAnySession() {
300 return getSession(player).isPresent();
301 }
302
304 public void depositeAll(Player player) {
305 Inventory inventory = player.inventory;
306 if (inventory.isEmpty()) {
307 player.message("There is nothing in your inventory to deposit!");
308 return;
309 }
310
311 List<Item> transfer = new ArrayList<>();
312 for (Item item : inventory.getItems()) {
313 if (item == null || !item.isTradeable()) {
314 continue;
315 }
316
317 transfer.add(item);
318 }
319
320 this.item_containers.get(player).addAll(transfer);
321 inventory.removeAll(transfer);
322
323 inventory.refresh();
325 }
326
328 protected void withdrawAll(Player player) {
329 ItemContainer container = this.item_containers.get(player);
330
331 if (container.isEmpty()) {
332 player.message("There is nothing in this container to withdraw!");
333 return;
334 }
335 player.inventory.addAll(container);
336 container.clear();
338 }
339
342 final Optional<ExchangeSession> session = getSession(player);
343
344 if (!session.isPresent() || session.get().finalized) {
345 return;
346 }
347
348 session.get().finalized = true;
349
350 switch (type) {
351 case RESTORE:
352 session.get().forEach(player -> {
354 items.forEach(player.inventory::add);
355 items.clear();
356 });
357 break;
358 case DISPOSE:
359 session.get().forEach(s -> item_containers.get(s).clear());
360 break;
361 case HALT:
362 //nothing happens when halted, the session only gets finalized.
363 break;
364 }
365 SESSIONS.remove(this);
366 onReset();
367 }
368
375 public static Optional<ExchangeSession> getSession(Player player) {
376 return SESSIONS.stream().filter(s -> anyMatch(s, player.getName())).findAny();
377 }
378
385 public static Optional<ExchangeSession> getSession(Player player, ExchangeSessionType type) {
386 return SESSIONS.stream().filter(s -> s.type.equals(type) && anyMatch(s, player.getName())).findAny();
387 }
388
395 public static Optional<ExchangeSession> getSession(Player player, Player other) {
396 return SESSIONS.stream().filter(s -> allMatch(s, player.getName(), other.getName())).findAny();
397 }
398
405 public static Optional<ExchangeSession> getSession(Player player, Player other, ExchangeSessionType type) {
406 return SESSIONS.stream().filter(s -> allMatch(s, player.getName(), other.getName()) && s.type.equals(type)).findAny();
407 }
408
414 public void forEach(Consumer<Player> action) {
415 ImmutableSet.of(player, other).forEach(action);
416 }
417
425 return p.getName().equals(player.getName()) ? this.other : this.player;
426 }
427
428 private static boolean anyMatch(ExchangeSession session, String name) {
429 return Arrays.stream(new String[]{session.player.getName(), session.other.getName()}).anyMatch(n -> n.equals(name));
430 }
431
432 private static boolean allMatch(ExchangeSession session, String name, String otherName) {
433 return Arrays.stream(new String[]{session.player.getName(), session.other.getName()}).anyMatch(n -> n.equals(name) || n.equals(otherName));
434 }
435}
static void schedule(Task task)
Definition World.java:284
abstract boolean canRemoveItem(Player player, Item item, int slot)
ExchangeSession(Player player, Player other, ExchangeSessionType type)
static Optional< ExchangeSession > getSession(Player player, ExchangeSessionType type)
abstract boolean canAddItem(Player player, Item item, int slot)
abstract void accept(Player player, String COMPONENT)
abstract boolean onButtonClick(Player player, int button)
final boolean add(Player player, int slot, int amount)
static boolean inSession(Player player, ExchangeSessionType type)
static Optional< ExchangeSession > getSession(Player player, Player other)
static Optional< ExchangeSession > getSession(Player player)
static boolean inSession(Player player, Player other, ExchangeSessionType type)
static Optional< ExchangeSession > getSession(Player player, Player other, ExchangeSessionType type)
boolean removeAll(Collection<? extends Item > items)