RuneHive-Game
Loading...
Searching...
No Matches
Activity.java
Go to the documentation of this file.
1package com.runehive.content.activity;
2
3import com.runehive.Config;
4import com.runehive.content.activity.panel.Activity_Panel;
5import com.runehive.content.consume.FoodData;
6import com.runehive.content.event.EventDispatcher;
7import com.runehive.content.event.InteractionEvent;
8import com.runehive.content.event.InteractionEventListener;
9import com.runehive.content.event.impl.*;
10import com.runehive.game.Animation;
11import com.runehive.game.Graphic;
12import com.runehive.game.world.World;
13import com.runehive.game.world.entity.Entity;
14import com.runehive.game.world.entity.mob.Mob;
15import com.runehive.game.world.entity.mob.npc.NpcDeath;
16import com.runehive.game.world.entity.mob.player.Player;
17import com.runehive.game.world.items.Item;
18import com.runehive.game.world.items.containers.equipment.EquipmentType;
19
20import java.util.Optional;
21import java.util.function.Consumer;
22import java.util.function.Predicate;
23
24/**
25 * A {@code Activity} object constructs an in-game activity and sequences it
26 * through the {@link #start()} and {@link #finish()} methods with a {@code
27 * cooldown} set in game ticks.
28 *
29 * @author Michael | Chex
30 */
31public abstract class Activity implements InteractionEventListener {
32
33 /** The 'start' cooldown id. */
34 protected static final int START = 0;
35
36 /** The 'finish' cooldown id. */
37 protected static final int FINISH = -1;
38
39 /** The 'pause' cooldown id. */
40 protected static final int PAUSE = -2;
41
42 /** The sequencing cooldown. */
43 private final int cooldown;
44
45 /** The activity instance level. */
46 private int instance;
47
48 /** The remaining game ticks. */
49 private int ticks;
50
51 /** The panel for this activity. */
53
54 /** Constructs a new {@code SequencedMinigame} object. */
55 public Activity(int cooldown, int instance) {
56 this.instance = instance;
57 this.cooldown = cooldown;
58 }
59
60 public static <T extends Activity> Optional<T> search(Player player, Class<T> clazz) {
61 final Activity activity = player.activity;
62
63 if (activity == null) {
64 return Optional.empty();
65 }
66
67 if (clazz.isInstance(activity)) {
68 return Optional.of(clazz.cast(activity));
69 }
70
71 return Optional.empty();
72 }
73
74 public static boolean evaluate(Mob mob, Predicate<Activity> predicate) {
75 return mob != null && mob.activity != null && predicate.test(mob.activity);
76 }
77
78 public static void forActivity(Mob mob, Consumer<Activity> consumer) {
79 if (mob == null) {
80 return;
81 }
82
83 if (mob.activity == null) {
84 return;
85 }
86
87 consumer.accept(mob.activity);
88 }
89
90 public boolean canEquipItem(Player player, Item item, EquipmentType type) {
91 return true;
92 }
93
94 public boolean canEat(Player player, FoodData foodType) {
95 return true;
96 }
97
98 public boolean canUseSpecial(Player player) {
99 return true;
100 }
101
102 public boolean canUsePrayer(Player player) {
103 return true;
104 }
105
106 public boolean canDrinkPotions(Player player) {
107 return true;
108 }
109
110 public boolean canLogout(Player player) {
111 return true;
112 }
113
114 public boolean canSpellCast(Player player) {
115 return true;
116 }
117
118 /** Sequences the activity. */
119 public void sequence() {
120 update();
121
122 if (ticks == PAUSE) {
123 return;
124 }
125
126 if (ticks > 0) {
127 ticks--;
128 } else if (ticks == START) {
129 start();
130 } else if (ticks == FINISH) {
131 finish();
132 }
133 }
134
135 /** Starts the next activity stage. */
136 protected abstract void start();
137
138 /** Finishes the activity. */
139 public abstract void finish();
140
141 /** Cleans up the activity when finished. */
142 public abstract void cleanup();
143
144 /** The update method. */
145 public void update() {
146
147 }
148
149 public abstract ActivityType getType();
150
151 /** Called when the player logs out. */
152 public void onLogout(Player player) {
153 remove(player);
154 }
155
156 /** Called when the player die */
157 public void onDeath(Mob mob) {
158 if (mob.isNpc()) {
159 World.schedule(new NpcDeath(mob.getNpc()));
160 return;
161 }
162 remove(mob);
164 mob.unpoison();
165 mob.unvenom();
166 finish();
167 }
168
169 protected void restart(int delay, Runnable runnable) {
170 World.schedule(delay, runnable::run); //deprecated lambda
171 }
172
173 public boolean onStep(Mob mob) {
174 return false;
175 }
176
177 /** Sets the activity panel. */
179 this.panel = panel;
180 }
181
182 /** Gets an optional of the activity panel. */
183 public Optional<Activity_Panel> getPanel() {
184 return Optional.ofNullable(panel);
185 }
186
187 /** Called when the player changes region. */
188 public void onRegionChange(Player player) {
189 remove(player);
190 }
191
192 /** Called when the player attempts to teleport. */
193 public boolean canTeleport(Player player) {
194 return false;
195 }
196
197 /** Adds a mob to the activity. */
198 public void add(Mob mob) {
199 if (mob.isNpc() && !mob.isRegistered()) {
200 mob.register();
201 mob.unpoison();
202 mob.unvenom();
203 }
204 mob.setActivity(this);
205 mob.instance = instance;
206 getListener().ifPresent(mob.getCombat()::addListener);
207 }
208
209 /** Removes a mob from the activity. */
210 public void remove(Mob mob) {
211 getListener().ifPresent(mob.getCombat()::removeListener);
212
213 if (mob.isNpc()) {
214 mob.getNpc().unregister();
215 } else {
216 mob.instance = Entity.DEFAULT_INSTANCE;
217 mob.getPlayer().setActivity(null);
218 mob.getPlayer().graphic(Graphic.RESET, true);
219 mob.getPlayer().animate(Animation.RESET, true);
220 // mob.getPlayer().send(new SendMessage("You have lost your current progress as you have teleported."));
221 }
222 }
223
224 /** Removes all mobs from the activity. */
225 public void removeAll(Mob... mobs) {
226 if (mobs.length != 0)
227 for (Mob mob : mobs) {
228 if (mob.isRegistered())
229 remove(mob);
230 }
231 }
232
233 /** Sets the pause state of the activity. */
234 public void setPause(boolean pause) {
235 ticks = pause ? PAUSE : START;
236 }
237
238 /** Resets the remaining ticks to the cached cooldown ticks. */
239 protected final void resetCooldown() {
241 }
242
243 /** Applies a cooldown. */
244 public void cooldown(int cooldown) {
245 this.ticks = cooldown;
246 }
247
248 /** Sets the cooldown flag to {@link #FINISH}. */
249 protected final void finishCooldown() {
250 ticks = FINISH;
251 }
252
253 /** Sets the cooldown flag to {@link #PAUSE}. */
254 protected final void pause() {
255 ticks = PAUSE;
256 }
257
258 /** Checks if the cooldown is paused. */
259 protected final boolean isPaused() {
260 return ticks == PAUSE;
261 }
262
266
267 /** Gets this activity's instance level. */
268 public int getInstance() {
269 return instance;
270 }
271
272 public void setInstance(int instance) {
273 this.instance = instance;
274 }
275
276 /** Gets the current ticks. */
277 public int getTicks() {
278 return ticks;
279 }
280
281 /** Gets an {@link Optional} of the {@link ActivityListener} for this activity. */
282 protected Optional<? extends ActivityListener<? extends Activity>> getListener() {
283 return Optional.empty();
284 }
285
286 protected boolean clickItem(Player player, ItemInteractionEvent event) {
287 return false;
288 }
289
290 protected boolean clickNpc(Player player, NpcInteractionEvent event) {
291 return false;
292 }
293
294 protected boolean clickObject(Player player, ObjectInteractionEvent event) {
295 return false;
296 }
297
299 return false;
300 }
301
302 protected boolean useItem(Player player, ItemOnItemInteractionEvent event) {
303 return false;
304 }
305
307 return false;
308 }
309
311 return false;
312 }
313
315 return false;
316 }
317
318 @Override
319 public boolean onEvent(Player player, InteractionEvent interactionEvent) {
320 final EventDispatcher dispatcher = new EventDispatcher(interactionEvent);
327// dispatcher.dispatch(InteractionType.FOURTH_ITEM_CLICK, e -> clickItem(player, (FourthItemClick) e));
330// dispatcher.dispatch(InteractionType.CLICK_NPC, e -> clickNpc(player, (ThirdNpcClick) e));
331// dispatcher.dispatch(InteractionType.CLICK_NPC, e -> clickNpc(player, (FourthNpcClick) e));
337 return interactionEvent.isHandled();
338 }
339}
The class that contains setting-related constants for the server.
Definition Config.java:24
static final Position DEFAULT_POSITION
The default, i.e.
Definition Config.java:197
final int cooldown
The sequencing cooldown.
Definition Activity.java:43
Activity(int cooldown, int instance)
Constructs a new SequencedMinigame object.
Definition Activity.java:55
void setPause(boolean pause)
Sets the pause state of the activity.
boolean canTeleport(Player player)
Called when the player attempts to teleport.
boolean canDrinkPotions(Player player)
final boolean isPaused()
Checks if the cooldown is paused.
abstract void finish()
Finishes the activity.
boolean clickItem(Player player, ItemInteractionEvent event)
static final int PAUSE
The 'pause' cooldown id.
Definition Activity.java:40
void onLogout(Player player)
Called when the player logs out.
boolean canSpellCast(Player player)
final void resetCooldown()
Resets the remaining ticks to the cached cooldown ticks.
static final int FINISH
The 'finish' cooldown id.
Definition Activity.java:37
static< T extends Activity > Optional< T > search(Player player, Class< T > clazz)
Definition Activity.java:60
boolean canEat(Player player, FoodData foodType)
Definition Activity.java:94
int instance
The activity instance level.
Definition Activity.java:46
void onDeath(Mob mob)
Called when the player die.
boolean itemContainerAction(Player player, ItemContainerInteractionEvent event)
boolean clickObject(Player player, ObjectInteractionEvent event)
abstract void start()
Starts the next activity stage.
void setPanel(Activity_Panel panel)
Sets the activity panel.
boolean pickupItem(Player player, PickupItemInteractionEvent event)
Optional<? extends ActivityListener<? extends Activity > > getListener()
Gets an Optional of the ActivityListener for this activity.
static final int START
The 'start' cooldown id.
Definition Activity.java:34
void onRegionChange(Player player)
Called when the player changes region.
boolean useItem(Player player, ItemOnItemInteractionEvent event)
int ticks
The remaining game ticks.
Definition Activity.java:49
abstract void cleanup()
Cleans up the activity when finished.
abstract ActivityType getType()
int getInstance()
Gets this activity's instance level.
int getTicks()
Gets the current ticks.
void restart(int delay, Runnable runnable)
final void pause()
Sets the cooldown flag to PAUSE.
void removeAll(Mob... mobs)
Removes all mobs from the activity.
boolean clickNpc(Player player, NpcInteractionEvent event)
boolean canEquipItem(Player player, Item item, EquipmentType type)
Definition Activity.java:90
void add(Mob mob)
Adds a mob to the activity.
boolean onEvent(Player player, InteractionEvent interactionEvent)
boolean useItem(Player player, ItemOnObjectInteractionEvent event)
void cooldown(int cooldown)
Applies a cooldown.
static boolean evaluate(Mob mob, Predicate< Activity > predicate)
Definition Activity.java:74
final void finishCooldown()
Sets the cooldown flag to FINISH.
void sequence()
Sequences the activity.
boolean clickButton(Player player, ClickButtonInteractionEvent event)
void update()
The update method.
Activity_Panel panel
The panel for this activity.
Definition Activity.java:52
boolean canUseSpecial(Player player)
Definition Activity.java:98
boolean canUsePrayer(Player player)
Optional< Activity_Panel > getPanel()
Gets an optional of the activity panel.
static void forActivity(Mob mob, Consumer< Activity > consumer)
Definition Activity.java:78
void dispatch(InteractionType type, EventHandler eventHandler)
Class that models a single animation used by an entity.
static final Animation RESET
Represents a single graphic that can be used by entities.
Definition Graphic.java:10
static final Graphic RESET
Definition Graphic.java:17
Represents the game world.
Definition World.java:46
static void schedule(Task task)
Submits a new event.
Definition World.java:247
Represents a character in the game world, i.e.
Definition Entity.java:13
abstract void register()
Registers an entity to the World.
Handles the mob class.
Definition Mob.java:66
abstract Combat<? extends Mob > getCombat()
The combat of the mob.
void move(Position position)
Moves the mob to a set position.
Definition Mob.java:340
final boolean isNpc()
Check if an entity is an npc.
Definition Mob.java:550
void setActivity(Activity activity)
Definition Mob.java:526
This class represents a character controlled by a player.
Definition Player.java:125
The container class that represents an item that can be interacted with.
Definition Item.java:21
Holds all activity types that are timed.
The enumerated types of a players equipped item slots.