RuneHive-Game
Loading...
Searching...
No Matches
StakeSession.java
Go to the documentation of this file.
1package com.runehive.game.world.entity.mob.player.exchange.duel;
2
3import com.runehive.content.activity.Activity;
4import com.runehive.content.activity.impl.duelarena.DuelArenaActivity;
5import com.runehive.content.activity.impl.duelarena.DuelRule;
6import com.runehive.content.activity.impl.duelarena.DuelUtils;
7import com.runehive.game.world.InterfaceConstants;
8import com.runehive.game.world.entity.mob.player.Player;
9import com.runehive.game.world.entity.mob.player.PlayerRight;
10import com.runehive.game.world.entity.mob.player.exchange.ExchangeCompletionType;
11import com.runehive.game.world.entity.mob.player.exchange.ExchangeSession;
12import com.runehive.game.world.entity.mob.player.exchange.ExchangeSessionType;
13import com.runehive.game.world.items.Item;
14import com.runehive.net.packet.out.*;
15import com.runehive.util.MessageColor;
16import com.runehive.util.Utility;
17
18import java.util.EnumSet;
19import java.util.Optional;
20import java.util.stream.IntStream;
21
22/**
23 * The exchange session where two players can agree to a stake. <p> The agreed
24 * items will be kept in this session until the duel is over.
25 *
26 * @author nshusa
27 */
28public final class StakeSession extends ExchangeSession {
29
31 public boolean accepted;
32
34 super(owner, other, ExchangeSessionType.DUEL);
35 }
36
37 @Override
38 public boolean onRequest() {
39// if (true) {
40// DuelArenaActivity2.create(this, player, other).cooldown(-1);
41// return true;
42// }
43 if(player.playTime < 6000) {
44 player.message("You must have at least 1 hour of play time before duelling.");
45 return false;
46 }
47 if(other.playTime < 6000) {
48 player.message("The other player must have at least 1 hour of play time before duelling.");
49 return false;
50 }
51 // face player they are requesting to duel
52 player.face(other.getPosition());
53 player.exchangeSession.requested_players.add(other);
54
55 if (!other.exchangeSession.requested_players.contains(player)) {
56 player.send(new SendMessage("Sending duel request..."));
57 other.send(new SendMessage(player.getName() + ":duelreq:", MessageColor.BRONZE));
58 return false;
59 }
60
61 SESSIONS.add(this);
62
64
65 forEach(player -> {
66 player.exchangeSession.resetRequests();
68 player.send(new SendConfig(655, 0)); // stake removal notification
69 // reset duel rule configs
70 IntStream.range(631, 644).forEach(i -> player.send(new SendConfig(i, 0)));
71 player.send(new SendToggle(286, 0));
72 player.attributes.set("DUEL_KEY", true);
73 });
74 updateMainComponents("FIRST_SCREEN");
75 return true;
76 }
77
78 @Override
79 public void accept(Player player, String component) {
81 switch (component) {
82 case "FIRST_SCREEN":
83 if (!player.inventory.hasCapacityFor(item_containers.get(other).toArray())) {
84 player.send(new SendMessage("You don't have enough free slots for this many items.", MessageColor.RED));
85 break;
86 }
87
88 if (!other.inventory.hasCapacityFor(item_containers.get(player).toArray())) {
89 String username = other.getName();
90 player.send(new SendMessage(username + " doesn't have enough free slots for this many items", MessageColor.RED));
91 break;
92 }
93
94 if (hasAttachment() && !getAttachment().equals(player)) {
95 setAttachment(null);
96 updateMainComponents("SECOND_SCREEN");
97 return;
98 }
99
101 player.send(new SendString("<col=ffffff>Waiting for other player...", 31009));
102 other.send(new SendString("<col=ffffff>Other player has accepted", 31009));
103 break;
104
105 case "SECOND_SCREEN":
106 if (hasAttachment() && !getAttachment().equals(player)) {
107 setAttachment(null);
108 accepted = true;
110 activity.setPause(false);
111 return;
112 }
113
115 player.send(new SendString("<col=ffffff>Waiting for other player...", 31526));
116 other.send(new SendString("<col=ffffff>Other player has accepted", 31526));
117 break;
118
119 }
120 }
121
122 @Override
123 public boolean canAddItem(Player player, Item item, int slot) {
124 return true;
125 }
126
127 @Override
128 public boolean canRemoveItem(Player player, Item item, int slot) {
129 return true;
130 }
131
132 @Override
133 public boolean onButtonClick(Player player, int button) {
134 Optional<DuelRule> rule = DuelRule.forButton(button);
135 rule.ifPresent(r -> r.set(player, true));
136
137 switch (button) {
138 case 31018:
139 case 31008:
140 case 31002:
141 case 31523:
142 case 31502:
143 player.exchangeSession.reset(ExchangeSessionType.DUEL);
144 return true;
145
146 case 31061: // save
147 if (!player.interfaceManager.isInterfaceOpen(InterfaceConstants.FIRST_DUEL_SCREEN)) {
148 return false;
149 }
150
151 if (activity != null) {
152 EnumSet<DuelRule> flags = activity.rules.getFlags();
153 player.attributes.put("duel_rules", flags);
154 }
155 return true;
156
157 case 31065: // load
158 if (!player.interfaceManager.isInterfaceOpen(InterfaceConstants.FIRST_DUEL_SCREEN)) {
159 return false;
160 }
161
162 if (player.attributes.has("duel_rules") && activity != null) {
163 EnumSet<DuelRule> flags = player.attributes.get("duel_rules");
164 activity.rules.reset();
165 flags.forEach(it -> it.set(player, true));
166 DuelRule.showRules(activity, this.player, other);
167 }
168 return true;
169
170 case 31520:
171 if (!player.interfaceManager.isInterfaceOpen(InterfaceConstants.SECOND_DUEL_SCREEN)) {
172 return false;
173 }
174
175 accept(player, "SECOND_SCREEN");
176 return true;
177
178 case 31015:
179 if (!player.interfaceManager.isInterfaceOpen(InterfaceConstants.FIRST_DUEL_SCREEN)) {
180 return false;
181 }
182
183 accept(player, "FIRST_SCREEN");
184 return true;
185
186 }
187 return false;
188 }
189
190 @Override
191 public void updateMainComponents(String component) {
192 switch (component) {
193
194 case "FIRST_SCREEN":
196 break;
197
198 case "SECOND_SCREEN":
199 forEach(p -> {
200 Player recipient = p.getName().equals(player.getName()) ? this.other : this.player;
201
202 p.send(new SendString("Some worn items will be taken off.", 31505));
203 p.send(new SendString("Boosted stats will be restored.", 31506));
204 p.send(new SendString("Existing prayers will be stopped.", 31507));
205 IntStream.range(31509, 31520).forEach(i -> p.send(new SendString("", i))); // rules
207 IntStream.range(31531, 31560).forEach(i -> p.send(new SendString("", i)));
208 IntStream.range(31561, 31589).forEach(i -> p.send(new SendString("", i)));
209 p.send(new SendString(DuelUtils.getItemNames(this.item_containers.get(p).toArray()), 31531));
210 p.send(new SendString(DuelUtils.getItemNames(this.item_containers.get(recipient).toArray()), 31561));
211 p.send(new SendString("", 31526));
212 p.send(new SendItemOnInterface(3322, this.item_containers.get(p).toArray()));
213 p.interfaceManager.open(InterfaceConstants.SECOND_DUEL_SCREEN);
214 });
215 break;
216 }
217 }
218
219 @Override
220 public void updateOfferComponents() {
221 setAttachment(null);
222 forEach(p -> {
223 Player recipient = p.getName().equals(player.getName()) ? this.other : this.player;
224
225 p.send(new SendItemOnInterface(3322, p.inventory.toArray()));
228
229 p.send(new SendString("Dueling with: " + PlayerRight.getCrown(recipient) + " " + Utility.formatName(recipient.getUsername()), 31005));
230 p.send(new SendString("Opponent's combat level: <col=ff7000>" + other.skills.getCombatLevel(), 31006));
231
232 p.send(new SendString("", 31009));
233 p.interfaceManager.openInventory(InterfaceConstants.FIRST_DUEL_SCREEN, 3321);
234 });
235 }
236
237 @Override
238 public void onReset() {
239 forEach(player -> {
241 player.resetFace();
242 player.attributes.set("DUEL_KEY", false);
243 if (!accepted) {
244 activity.remove(player);
245 }
246 });
247 }
248
250 Optional<DuelArenaActivity> result = Activity.search(p, DuelArenaActivity.class);
251
252 if (!result.isPresent()) {
253 return;
254 }
255
256 DuelArenaActivity activity = result.get();
257
258 int count = 0;
259 for (DuelRule rule : DuelRule.values()) {
260 if (activity.rules.contains(rule)) {
261 p.send(new SendString(DuelUtils.getRuleText(rule), 31509 + count));
262 count++;
263 }
264 }
265 }
266
267}
A Activity object constructs an in-game activity and sequences it through the start() and finish() me...
Definition Activity.java:31
static< T extends Activity > Optional< T > search(Player player, Class< T > clazz)
Definition Activity.java:60
static DuelArenaActivity create(StakeSession session, Player player, Player opponent)
The class that contains helpful information on interfaces.
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
boolean hasAttachment()
Determines if the trade stage has an attachment.
static final Set< ExchangeSession > SESSIONS
The collection of sessions.
Object getAttachment()
Retrieves the attachment object to this class.
final Map< Player, ItemContainer > item_containers
The items which are in this exchange session.
ExchangeSession(Player player, Player other, ExchangeSessionType type)
Constructs a new ExchangeSession.
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.
Player getOther(Player p)
Gets the other player in the exchange session.
boolean canAddItem(Player player, Item item, int slot)
Checks if the item can be added to the container.
boolean onButtonClick(Player player, int button)
The method invoked when a button is clicked.
void accept(Player player, String component)
The method invoked when a PLAYER accepts a certain exchange component.
boolean onRequest()
The method invoked when a player requests the other player.
void updateOfferComponents()
Updates the itemcontainer when an item is offered or removed.
void updateMainComponents(String component)
Updates the main components of the itemcontainer.
boolean canRemoveItem(Player player, Item item, int slot)
Checks if the item can be removed from the container.
void onReset()
Any functionality that should be handled when the itemcontainer closes.
The container class that represents an item that can be interacted with.
Definition Item.java:21
The OutgoingPacket responsible for changing settings on a client.
The OutgoingPacket that sends a message to a Players chatbox in the client.
The OutgoingPacket that sends a string to a Players itemcontainer in the client.
Handles miscellaneous methods.
Definition Utility.java:27
static String formatName(final String input)
Definition Utility.java:645
static Optional< DuelRule > forButton(int button)
static void showRules(DuelArenaActivity activity, Player player, Player other)
static String getCrown(Player player)
Gets the crown display.
UNCLICKABLE
The state where the map is visible, but clicking is disabled.
NORMAL
The default state where the map is visible and clicking is enabled.
Holds an enum of colors for ease.