RuneHive-Game
Loading...
Searching...
No Matches
ExchangeSessionManager.java
Go to the documentation of this file.
1package com.runehive.game.world.entity.mob.player.exchange;
2
3import com.runehive.net.packet.out.SendMessage;
4import com.runehive.game.world.entity.mob.player.Player;
5import com.runehive.util.MessageColor;
6
7import java.util.ArrayList;
8import java.util.List;
9import java.util.Optional;
10
11/**
12 * @author <a href="http://www.rune-server.org/members/stand+up/">Stand Up</a>
13 * @since 25-1-2017.
14 */
15public final class ExchangeSessionManager {
16
17 /**
18 * The players this player has requested.
19 */
20 public final List<Player> requested_players = new ArrayList<>();
21
22 /**
23 * The player this manager is for.
24 */
25 public final Player player;
26
27 /**
28 * Constructs a new {@link ExchangeSessionManager}.
29 * @param player the player this manager is for.
30 */
32 this.player = player;
33 }
34
35 /**
36 * Attempts to request the specified {@code session}.
37 * @param session the session to request.
38 */
39 public boolean request(ExchangeSession session) {
40 return session.request();
41 }
42
43 /**
44 * Attempts to deposit an item to an exchange session.
45 * @param slot the slot this item was added from.
46 * @param amount the amount that was added.
47 */
48 public boolean deposit(int slot, int amount) {
49 ExchangeSession session = ExchangeSession.getSession(player).orElse(null);
50
51 if(session == null) {
52 return false;
53 }
54
55 return session.add(player, slot, amount);
56 }
57
58 /**
59 * Attempts to withdraw an item from the exchange session.
60 * @param slot the slot this item was removed from.
61 * @param amount the amount that was removed.
62 */
63 public boolean withdraw(int slot, int amount) {
64 ExchangeSession session = ExchangeSession.getSession(player).orElse(null);
65
66 if(session == null) {
67 return false;
68 }
69
70 return session.remove(player, slot, amount);
71 }
72
73
74 /**
75 * Resets all the session for the player dependant on the {@code type}.
76 */
77 public void reset(ExchangeSessionType type) {
78 Optional<ExchangeSession> session = ExchangeSession.getSession(player, type);
79
80 if(!session.isPresent()) {
81 return;
82 }
83
84 session.get().forEach(p -> {
85 session.get().finalize(ExchangeCompletionType.RESTORE);
87 p.interfaceManager.close();
88 });
89
90 Player other = session.get().getOther(player);
91 other.send(new SendMessage("The other player has declined.", MessageColor.RED));
92 ExchangeSession.SESSIONS.remove(session.get());
93 }
94
95 /**
96 * Resets all the sessions the player is in regardless of the session state.
97 */
98 public boolean reset() {
99 Optional<ExchangeSession> session = ExchangeSession.getSession(player);
100
101 if(!session.isPresent()) {
102 return false;
103 }
104
105 reset(session.get().type);
106 return true;
107 }
108
109 /**
110 * Clears the list of requested players.
111 */
112 public void resetRequests() {
113 player.exchangeSession.requested_players.clear();
114 }
115}
This class represents a character controlled by a player.
Definition Player.java:125
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.
static final Set< ExchangeSession > SESSIONS
The collection of sessions.
boolean request()
Attempts to start an exchange session.
final boolean remove(Player player, int slot, int amount)
Attempts to withdraw an item from the container.
final List< Player > requested_players
The players this player has requested.
boolean withdraw(int slot, int amount)
Attempts to withdraw an item from the exchange session.
boolean deposit(int slot, int amount)
Attempts to deposit an item to an exchange session.
ExchangeSessionManager(Player player)
Constructs a new ExchangeSessionManager.
boolean request(ExchangeSession session)
Attempts to request the specified session.
boolean reset()
Resets all the sessions the player is in regardless of the session state.
void reset(ExchangeSessionType type)
Resets all the session for the player dependant on the type.
The OutgoingPacket that sends a message to a Players chatbox in the client.
RESTORE
Determines the items should be restored to the belonging owners.
Holds an enum of colors for ease.