RuneHive-Game
Loading...
Searching...
No Matches
GroupActivity.java
Go to the documentation of this file.
1package com.runehive.content.activity;
2
3import com.runehive.net.packet.out.SendMessage;
4import com.runehive.game.world.entity.mob.Mob;
5
6import java.util.HashMap;
7import java.util.Map;
8import java.util.function.BiConsumer;
9
10/**
11 * A {@code GroupActivity} is an extension of {@link Activity} that holds a list
12 * of active activities.
13 *
14 * @author Michael | Chex
15 */
16public abstract class GroupActivity extends Activity {
17
18 /** A map of activities that handles each mob individually. */
19 protected final Map<Mob, Activity> activities;
20
21 /** The next instance level. */
22 private static int nextInstance;
23
24 /**
25 * Constructs a new {@link GroupActivity} object.
26 *
27 * @param cooldown the initial cooldown in ticks
28 */
29 protected GroupActivity(int cooldown, int capacity) {
30 super(cooldown, getNextInstance());
31 this.activities = new HashMap<>(capacity);
32 }
33
34 @Override
35 protected void start() {
36 activities.values().forEach(Activity::start);
37 }
38
39 @Override
40 public void finish() {
41 for (Map.Entry<Mob, Activity> entry : activities.entrySet()) {
42 entry.getValue().finish();
43 entry.getValue().cleanup();
44 }
45 }
46
47 @Override
48 public void cleanup() {
49 activities.forEach((mob, activity) -> activity.cleanup());
50 }
51
52 protected void removeAll() {
53 activities.forEach((mob, activity) -> remove(mob));
54 activities.clear();
55 }
56
57 /**
58 * Checks if this group is active.
59 *
60 * @return {@code true} if there are active activities
61 */
62 public final boolean isActive() {
63 return !activities.isEmpty();
64 }
65
66 /**
67 * Gets the size of the activities in this group.
68 *
69 * @return the amount of activities in this group
70 */
71 public int getActiveSize() {
72 return activities.size();
73 }
74
75 /**
76 * Loops through all the activities.
77 *
78 * @param activity the consumer
79 */
80 public void forEachActivity(BiConsumer<Mob, Activity> activity) {
81 activities.forEach(activity);
82 }
83
84 /**
85 * Adds an activity to the {@code activities} group.
86 *
87 * @param mob the mob to add
88 * @param activity the mob's activity
89 */
90 public final void addActivity(Mob mob, Activity activity) {
91 if (!activities.containsKey(mob)) {
92 activities.put(mob, activity);
93 add(mob);
94 }
95 }
96
97 /**
98 * Removes an activity from the {@code activities} group.
99 *
100 * @param mob the mob that owns the activity
101 */
102 public final void removeActivity(Mob mob) {
103 if (activities.containsKey(mob)) {
104 activities.get(mob).finish();
105 activities.get(mob).cleanup();
106 activities.remove(mob);
107 remove(mob);
108 }
109 }
110
111 /**
112 * Sends a message to all the players in the group.
113 *
114 * @param message the message to send
115 */
116 public final void groupMessage(String message) {
117 activities.keySet().forEach(mob -> {
118 if (mob.isPlayer())
119 mob.getPlayer().send(new SendMessage(message));
120 });
121 }
122
124 return activities.get(mob);
125 }
126
127 /**
128 * Gets the next instance.
129 *
130 * @return the instacne level
131 */
132 private static int getNextInstance() {
133 return nextInstance++;
134 }
135
136}
final int cooldown
The sequencing cooldown.
Definition Activity.java:43
Activity(int cooldown, int instance)
Constructs a new SequencedMinigame object.
Definition Activity.java:55
abstract void start()
Starts the next activity stage.
void add(Mob mob)
Adds a mob to the activity.
void start()
Starts the next activity stage.
static int nextInstance
The next instance level.
final void removeActivity(Mob mob)
Removes an activity from the activities group.
final void addActivity(Mob mob, Activity activity)
Adds an activity to the activities group.
final boolean isActive()
Checks if this group is active.
final void groupMessage(String message)
Sends a message to all the players in the group.
final Map< Mob, Activity > activities
A map of activities that handles each mob individually.
void forEachActivity(BiConsumer< Mob, Activity > activity)
Loops through all the activities.
static int getNextInstance()
Gets the next instance.
void cleanup()
Cleans up the activity when finished.
int getActiveSize()
Gets the size of the activities in this group.
GroupActivity(int cooldown, int capacity)
Constructs a new GroupActivity object.
Handles the mob class.
Definition Mob.java:66
The OutgoingPacket that sends a message to a Players chatbox in the client.