RuneHive-Game
Loading...
Searching...
No Matches
ExchangeSession.java
Go to the documentation of this file.
1package com.runehive.game.world.entity.mob.player.exchange;
2
3import com.google.common.collect.ImmutableSet;
4import com.runehive.content.activity.ActivityType;
5import com.runehive.content.gambling.GambleStage;
6import com.runehive.game.task.impl.DuelNotificationTask;
7import com.runehive.game.task.impl.SessionRemovalNotificationTask;
8import com.runehive.game.world.World;
9import com.runehive.game.world.entity.mob.player.Player;
10import com.runehive.game.world.entity.mob.player.PlayerRight;
11import com.runehive.game.world.items.Item;
12import com.runehive.game.world.items.containers.ItemContainer;
13import com.runehive.game.world.items.containers.inventory.Inventory;
14import com.runehive.game.world.position.Position;
15
16import java.util.*;
17import java.util.function.Consumer;
18
19/**
20 * @author <a href="http://www.rune-server.org/members/stand+up/">Stand Up</a>
21 * @since 23-1-2017.
22 */
23public abstract class ExchangeSession {
24
25 /** The collection of sessions */
26 public static final Set<ExchangeSession> SESSIONS = new HashSet<>();
27
28 /** The controller of this session. */
29 public final Player player;
30
31 /** The other player in this session. */
32 public final Player other;
33
34 /** The type of this exchange session. */
36
37 /** The items which are in this exchange session. */
38 public final Map<Player, ItemContainer> item_containers = new HashMap<>();
39
40 /**
41 * The attachment to the session stage, this will more than likely be a player object
42 * that will be attached when they have confirmed a certain stage.
43 */
44 private Object attachment;
45
46 /** Constructs a new {@link ExchangeSession}. */
48 this.player = player;
49 this.other = other;
50 this.type = type;
52 }
53
54 /** Determines if the player can offer or withdraw items. */
55 public boolean canOffer = true;
56
57 /** Determines if this exchange has been finalized. */
58 public boolean finalized;
59
60 /** The method invoked when a player requests the {@code other} player. */
61 public abstract boolean onRequest();
62
63 /** The method invoked when a PLAYER accepts a certain exchange component. */
64 public abstract void accept(Player player, String COMPONENT);
65
66 /** Checks if the item can be added to the container. */
67 public abstract boolean canAddItem(Player player, Item item, int slot);
68
69 /** Checks if the item can be removed from the container. */
70 public abstract boolean canRemoveItem(Player player, Item item, int slot);
71
72 /** The method invoked when a button is clicked. */
73 public abstract boolean onButtonClick(Player player, int button);
74
75 /** Updates the main components of the itemcontainer. */
76 public abstract void updateMainComponents(String component);
77
78 /** Updates the itemcontainer when an item is offered or removed. */
79 public abstract void updateOfferComponents();
80
81 /** Any functionality that should be handled when the itemcontainer closes. */
82 public abstract void onReset();
83
84 /** Attempts to start an exchange session. */
85 public boolean request() {
86 if (inAnySession()) {
87 player.exchangeSession.reset();
88 return false;
89 }
90 /* if (PlayerRight.isAdministrator(player) && !PlayerRight.isOwner(player) || !PlayerRight.isDeveloper(player)) {
91 player.message("You can not exchange as an administrator.");
92 return false;
93 }
94 if (PlayerRight.isAdministrator(other) && !PlayerRight.isOwner(other) || !PlayerRight.isDeveloper(other)) {
95 player.message("You can not exchange an administrator.");
96 return false;
97 }*/
98
99 if (getSession(other).isPresent() && getSession(other).get().inAnySession()) {
100 player.message("This player is currently is a " + type.name + " with another player.");
101 return false;
102 }
103 if (Objects.equals(player, other)) {
104 player.message("You cannot " + type.name + " with yourself.");
105 return false;
106 }
107
108 if(player.getGambling().getStage().equals(GambleStage.PLACING_BET) || player.getGambling().getStage().equals(GambleStage.IN_PROGRESS)) {
109 return false;
110 }
111
112 if(other.getGambling().getStage().equals(GambleStage.PLACING_BET) || other.getGambling().getStage().equals(GambleStage.IN_PROGRESS)) {
113 return false;
114 }
115
117 player.message("You can not " + type.name + " as you are an iron man.");
118 return false;
119 }
121 player.message(other.getName() + " can not be " + type.name + "d as they are an iron man.");
122 return false;
123 }
124 if (player.exchangeSession.requested_players.contains(other)) {
125 player.message("You have already sent a request to this player.");
126 return false;
127 }
128 if (player.locking.locked()) {
129 player.message("You cannot send a " + type.name + " request right now.");
130 return false;
131 }
132 if (other.locking.locked()) {
133 player.message(other.getName() + " is currently busy.");
134 return false;
135 }
136 if (player.playerAssistant.busy()) {
137 player.message("Please finish what you are doing before you do that.");
138 return false;
139 }
140 if (other.playerAssistant.busy()) {
141 player.message(other.getName() + " is currently busy.");
142 return false;
143 }
144 if (player.inActivity(ActivityType.DUEL_ARENA)) {
145 player.message("You can not do that whilst in a duel!");
146 return false;
147 }
148 if (other.inActivity(ActivityType.DUEL_ARENA)) {
149 other.message("You can not do that whilst in a duel!");
150 return false;
151 }
152 if (!onRequest()) {
153 return false;
154 }
155 if(this.type == ExchangeSessionType.DUEL && !other.getPosition().inLocation(new Position(3311, 3201), new Position(3394, 3287), true)) {
156 player.exchangeSession.reset();
157 return false;
158 }
159 return SESSIONS.add(this);
160 }
161
162 /** Attempts to deposit an item to the container. */
163 public final boolean add(Player player, int slot, int amount) {
164 Item invItem = player.inventory.get(slot);
165 if (invItem == null) {
166 return false;
167 }
168 if (!Item.valid(invItem) || !player.inventory.contains(invItem.getId())) {
169 return false;
170 }
171 if (!canOffer) {
172 return false;
173 }
174 if (!inSession(player, type)) {
175 return false;
176 }
177 if (!canAddItem(player, invItem, slot)) {
178 return false;
179 }
180 if (!invItem.isTradeable() && !PlayerRight.isOwner(player)) {
181 player.message("You can't offer this item.");
182 return false;
183 }
184 Item item = new Item(invItem.getId(), amount);
185 int count = player.inventory.computeAmountForId(item.getId());
186 if (item.getAmount() > count) {
187 item.setAmount(count);
188 }
189 if (item_containers.get(player).add(item)) {
190 player.inventory.remove(item);
192 return true;
193 }
194 return false;
195 }
196
197 /** Attempts to withdraw an item from the container. */
198 public final boolean remove(Player player, int slot, int amount) {
199 Item containerItem = this.item_containers.get(player).get(slot);
200 if (player == null || containerItem == null) {
201 return false;
202 }
203 if (!Item.valid(containerItem) || !this.item_containers.get(player).contains(containerItem.getId())) {
204 return false;
205 }
206 if (!canOffer) {
207 return false;
208 }
209 if (!inSession(player, type)) {
210 return false;
211 }
212 if (!canRemoveItem(player, containerItem, slot)) {
213 return false;
214 }
215 Item item = new Item(containerItem.getId(), amount == -1 ? this.item_containers.get(player).computeAmountForId(containerItem.getId()) : amount);
216 int count = item_containers.get(player).computeAmountForId(item.getId());
217 if (item.getAmount() > count) {
218 item.setAmount(count);
219 }
220 if (item_containers.get(player).remove(item)) {
221 player.inventory.add(item);
222 item_containers.get(player).shift();
223
224 if (this.player.equals(player)) {
226 } else if (player.equals(other)) {
227 World.schedule(this.type == ExchangeSessionType.DUEL ? new DuelNotificationTask(this.player) : new SessionRemovalNotificationTask(this.player));
228 }
229
231 return true;
232 }
233 return false;
234 }
235
236 /** Assigns an attachment to this stage object */
237 public void setAttachment(Object attachment) {
238 this.attachment = attachment;
239 }
240
241 /** Retrieves the attachment object to this class */
242 public Object getAttachment() {
243 return attachment;
244 }
245
246 /** Determines if the trade stage has an attachment */
247 public boolean hasAttachment() {
248 return Objects.nonNull(attachment);
249 }
250
251 /** Determines if the player is a session with {@code other} which matches the {@code type}. */
253 return SESSIONS.stream().anyMatch(session -> allMatch(session, player.getName(), other.getName()) && session.type.equals(type));
254 }
255
256 /** Determines if the player is a session which matches the {@code type}. */
258 return getSession(player, type).isPresent();
259 }
260
261 /** Determines if the player is <b>any</b> session. */
262 public boolean inAnySession() {
263 return getSession(player).isPresent();
264 }
265
266 /** Deposites all items to the exchange session. */
267 public void depositeAll(Player player) {
268 Inventory inventory = player.inventory;
269 if (inventory.isEmpty()) {
270 player.message("There is nothing in your inventory to deposit!");
271 return;
272 }
273
274 List<Item> transfer = new ArrayList<>();
275 for (Item item : inventory.getItems()) {
276 if (item == null || !item.isTradeable()) {
277 continue;
278 }
279
280 transfer.add(item);
281 }
282
283 this.item_containers.get(player).addAll(transfer);
284 inventory.removeAll(transfer);
285
286 inventory.refresh();
288 }
289
290 /** Withdraws all items from the exchange session. */
291 protected void withdrawAll(Player player) {
292 ItemContainer container = this.item_containers.get(player);
293
294 if (container.isEmpty()) {
295 player.message("There is nothing in this container to withdraw!");
296 return;
297 }
298 player.inventory.addAll(container);
299 container.clear();
301 }
302
303 /** Finalizes the exchange session procedure for the specified {@code player} in a session. */
305 final Optional<ExchangeSession> session = getSession(player);
306
307 if (!session.isPresent() || session.get().finalized) {
308 return;
309 }
310
311 session.get().finalized = true;
312
313 switch (type) {
314 case RESTORE:
315 session.get().forEach(player -> {
317 items.forEach(player.inventory::add);
318 items.clear();
319 });
320 break;
321 case DISPOSE:
322 session.get().forEach(s -> item_containers.get(s).clear());
323 break;
324 case HALT:
325 //nothing happens when halted, the session only gets finalized.
326 break;
327 }
328 SESSIONS.remove(this);
329 onReset();
330 }
331
332 /**
333 * Gets the session if applicable the player is in.
334 *
335 * @param player the player to get the session for.
336 * @return the session wrapped in an optional.
337 */
338 public static Optional<ExchangeSession> getSession(Player player) {
339 return SESSIONS.stream().filter(s -> anyMatch(s, player.getName())).findAny();
340 }
341
342 /**
343 * Gets the session if applicable the player is in.
344 *
345 * @param player the player to get the session for.
346 * @return the session wrapped in an optional.
347 */
348 public static Optional<ExchangeSession> getSession(Player player, ExchangeSessionType type) {
349 return SESSIONS.stream().filter(s -> s.type.equals(type) && anyMatch(s, player.getName())).findAny();
350 }
351
352 /**
353 * Gets the session if applicable the player is in.
354 *
355 * @param player the player to get the session for.
356 * @return the session wrapped in an optional.
357 */
358 public static Optional<ExchangeSession> getSession(Player player, Player other) {
359 return SESSIONS.stream().filter(s -> allMatch(s, player.getName(), other.getName())).findAny();
360 }
361
362 /**
363 * Gets the session if applicable the player is in.
364 *
365 * @param player the player to get the session for.
366 * @return the session wrapped in an optional.
367 */
368 public static Optional<ExchangeSession> getSession(Player player, Player other, ExchangeSessionType type) {
369 return SESSIONS.stream().filter(s -> allMatch(s, player.getName(), other.getName()) && s.type.equals(type)).findAny();
370 }
371
372 /**
373 * Executes the specified {@code action} for every player in this session.
374 *
375 * @param action the action to execute.
376 */
377 public void forEach(Consumer<Player> action) {
378 ImmutableSet.of(player, other).forEach(action);
379 }
380
381 /**
382 * Gets the other player in the exchange session.
383 *
384 * @param p the player you <b>don't</b> want to get.
385 * @return the other player.
386 */
388 return p.getName().equals(player.getName()) ? this.other : this.player;
389 }
390
391 private static boolean anyMatch(ExchangeSession session, String name) {
392 return Arrays.stream(new String[]{session.player.getName(), session.other.getName()}).anyMatch(n -> n.equals(name));
393 }
394
395 private static boolean allMatch(ExchangeSession session, String name, String otherName) {
396 return Arrays.stream(new String[]{session.player.getName(), session.other.getName()}).anyMatch(n -> n.equals(name) || n.equals(otherName));
397 }
398}
Represents the game world.
Definition World.java:46
static void schedule(Task task)
Submits a new event.
Definition World.java:247
This class represents a character controlled by a player.
Definition Player.java:125
String getName()
Gets the name of this entity.
Definition Player.java:774
final boolean add(Player player, int slot, int amount)
Attempts to deposit an item to the container.
static Optional< ExchangeSession > getSession(Player player)
Gets the session if applicable the player is in.
abstract boolean canRemoveItem(Player player, Item item, int slot)
Checks if the item can be removed from the container.
boolean hasAttachment()
Determines if the trade stage has an attachment.
abstract void updateOfferComponents()
Updates the itemcontainer when an item is offered or removed.
static Optional< ExchangeSession > getSession(Player player, Player other, ExchangeSessionType type)
Gets the session if applicable the player is in.
abstract boolean canAddItem(Player player, Item item, int slot)
Checks if the item can be added to the container.
boolean canOffer
Determines if the player can offer or withdraw items.
static Optional< ExchangeSession > getSession(Player player, ExchangeSessionType type)
Gets the session if applicable the player is in.
static final Set< ExchangeSession > SESSIONS
The collection of sessions.
abstract void updateMainComponents(String component)
Updates the main components of the itemcontainer.
Object getAttachment()
Retrieves the attachment object to this class.
boolean request()
Attempts to start an exchange session.
Object attachment
The attachment to the session stage, this will more than likely be a player object that will be attac...
void withdrawAll(Player player)
Withdraws all items from the exchange session.
final Map< Player, ItemContainer > item_containers
The items which are in this exchange session.
boolean finalized
Determines if this exchange has been finalized.
abstract void accept(Player player, String COMPONENT)
The method invoked when a PLAYER accepts a certain exchange component.
static boolean inSession(Player player, Player other, ExchangeSessionType type)
Determines if the player is a session with other which matches the type.
abstract boolean onButtonClick(Player player, int button)
The method invoked when a button is clicked.
ExchangeSession(Player player, Player other, ExchangeSessionType type)
Constructs a new ExchangeSession.
boolean inAnySession()
Determines if the player is any session.
void setAttachment(Object attachment)
Assigns an attachment to this stage object.
void finalize(ExchangeCompletionType type)
Finalizes the exchange session procedure for the specified player in a session.
void forEach(Consumer< Player > action)
Executes the specified action for every player in this session.
abstract boolean onRequest()
The method invoked when a player requests the other player.
static boolean inSession(Player player, ExchangeSessionType type)
Determines if the player is a session which matches the type.
static boolean allMatch(ExchangeSession session, String name, String otherName)
static Optional< ExchangeSession > getSession(Player player, Player other)
Gets the session if applicable the player is in.
void depositeAll(Player player)
Deposites all items to the exchange session.
Player getOther(Player p)
Gets the other player in the exchange session.
abstract void onReset()
Any functionality that should be handled when the itemcontainer closes.
static boolean anyMatch(ExchangeSession session, String name)
final ExchangeSessionType type
The type of this exchange session.
The container class that represents an item that can be interacted with.
Definition Item.java:21
final void setAmount(int amount)
Sets the quantity of this item.
Definition Item.java:351
final int getId()
Gets the identification of this item.
Definition Item.java:324
static boolean valid(Item item)
Determines if item is valid.
Definition Item.java:259
final int getAmount()
Gets the quantity of this item.
Definition Item.java:342
An abstraction game representing a group of Items.
final Item get(int index)
Gets the Item located on index.
void clear()
Removes all of the items from this container.
boolean removeAll(Collection<? extends Item > items)
Attempts to withdraw items in bulk from this container.
An ItemContainer implementation that manages the inventory for a Player.
void refresh()
Refreshes the players inventory.
Represents a single tile on the game world.
Definition Position.java:14
Holds all activity types that are timed.
static boolean isIronman(Player player)
Checks if the player is an ironman.
An enumerated type defining policies for stackable Items.
STANDARD
The STANDARD policy, items are only stacked if they are defined as stackable in their ItemDefinition ...