RuneHive-Game
Loading...
Searching...
No Matches
RequestManager.java
Go to the documentation of this file.
1package com.runehive.game.world.entity.mob.player.requests;
2
3import com.runehive.net.packet.out.SendMessage;
4import com.runehive.game.world.entity.mob.player.Player;
5import com.runehive.game.world.region.Region;
6import com.runehive.util.Utility;
7
8/**
9 * The request manager manages
10 *
11 * @author Graham Edgecombe
12 */
13public class RequestManager {
14
15 /** The player. */
16 private Player player;
17
18 /** The current state. */
20
21 /** The current request type. */
23
24 /** The current 'acquaintance'. */
26
27 /**
28 * Creates the request manager.
29 *
30 * @param player
31 * The player whose requests the manager is managing.
32 */
34 this.player = player;
35 }
36
37 /**
38 * Sends another player a request.
39 *
40 * @param other
41 * The other player to request.
42 * @param type
43 * The request type.
44 * @return {@code True} if the request was mutual
45 */
46 public boolean request(Player other, RequestType type) {
47 if (!validate(player) || !validate(other)) {
48 return false;
49 }
50
52 player.send(new SendMessage("Unable to find " + Utility.formatName(other.getName()) + "."));
53 return false;
54 }
55
56 RequestManager otherManager = other.requestManager;
57
59 return false;
60 }
61
62 requestType = type;
63 acquaintance = other;
65
66 if (mutualRequest(otherManager)) {
68 otherManager.state = RequestState.PARTICIPATING;
69 return true;
70 }
71
72 return false;
73 }
74
75 /**
76 * Performs a check to validate a player for a request.
77 *
78 * @param entity
79 * The player to validate.
80 * @return {@code True} if the player is valid
81 */
82 private boolean validate(Player entity) {
83 return entity != null && !entity.positionChange;
84 }
85
86 /**
87 * Checks if a request is mutual between two players.
88 *
89 * @param otherManager
90 * The other player's {@codeplain RequestManager}.
91 * @return {@code True} if the request was mutual
92 */
93 private boolean mutualRequest(RequestManager otherManager) {
94 boolean otherRequested = otherManager.state == RequestState.REQUESTED;
95 boolean sameRequestType = requestType == otherManager.requestType;
96 boolean mutualAcquantances = player.equals(otherManager.acquaintance);
97 return otherRequested && sameRequestType && mutualAcquantances;
98 }
99
100 /** Resets the variables for a request. */
101 private void reset() {
102 requestType = null;
103 acquaintance = null;
105 }
106
107 /** Called when an itemcontainer is closed. */
108 public void close() {
109 reset();
110 }
111
112}
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 mutualRequest(RequestManager otherManager)
Checks if a request is mutual between two players.
RequestManager(Player player)
Creates the request manager.
boolean validate(Player entity)
Performs a check to validate a player for a request.
boolean request(Player other, RequestType type)
Sends another player a request.
Represents a single region.
Definition Region.java:21
The OutgoingPacket that sends a message to a Players chatbox in the client.
Handles miscellaneous methods.
Definition Utility.java:27
static String formatName(final String input)
Definition Utility.java:645
static boolean withinDistance(Interactable source, Interactable target, int radius)
Definition Utility.java:470
Holds the different states the manager can be in.
PARTICIPATING
The player is participating in an existing request of this type, so cannot accept new requests at all...
REQUESTED
Somebody has offered some kind of request.