RuneHive-Tarnish
Neural OSRS Enhancement Framework
Loading...
Searching...
No Matches
Activity.java
1package com.osroyale.content.activity;
2
3import com.osroyale.Config;
4import com.osroyale.content.activity.panel.Activity_Panel;
5import com.osroyale.content.consume.FoodData;
6import com.osroyale.content.event.EventDispatcher;
7import com.osroyale.content.event.InteractionEvent;
8import com.osroyale.content.event.InteractionEventListener;
9import com.osroyale.content.event.impl.*;
10import com.osroyale.game.Animation;
11import com.osroyale.game.Graphic;
12import com.osroyale.game.world.World;
13import com.osroyale.game.world.entity.Entity;
14import com.osroyale.game.world.entity.mob.Mob;
15import com.osroyale.game.world.entity.mob.npc.NpcDeath;
16import com.osroyale.game.world.entity.mob.player.Player;
17import com.osroyale.game.world.items.Item;
18import com.osroyale.game.world.items.containers.equipment.EquipmentType;
19
20import java.util.Optional;
21import java.util.function.Consumer;
22import java.util.function.Predicate;
23
67
68public abstract class Activity implements InteractionEventListener {
69
71 protected static final int START = 0;
72
74 protected static final int FINISH = -1;
75
77 protected static final int PAUSE = -2;
78
80 private final int cooldown;
81
83 private int instance;
84
86 private int ticks;
87
89 private Activity_Panel panel;
90
92 public Activity(int cooldown, int instance) {
93 this.instance = instance;
94 this.cooldown = cooldown;
95 }
96
97 public static <T extends Activity> Optional<T> search(Player player, Class<T> clazz) {
98 final Activity activity = player.activity;
99
100 if (activity == null) {
101 return Optional.empty();
102 }
103
104 if (clazz.isInstance(activity)) {
105 return Optional.of(clazz.cast(activity));
106 }
107
108 return Optional.empty();
109 }
110
111 public static boolean evaluate(Mob mob, Predicate<Activity> predicate) {
112 return mob != null && mob.activity != null && predicate.test(mob.activity);
113 }
114
115 public static void forActivity(Mob mob, Consumer<Activity> consumer) {
116 if (mob == null) {
117 return;
118 }
119
120 if (mob.activity == null) {
121 return;
122 }
123
124 consumer.accept(mob.activity);
125 }
126
127 public boolean canEquipItem(Player player, Item item, EquipmentType type) {
128 return true;
129 }
130
131 public boolean canEat(Player player, FoodData foodType) {
132 return true;
133 }
134
135 public boolean canUseSpecial(Player player) {
136 return true;
137 }
138
139 public boolean canUsePrayer(Player player) {
140 return true;
141 }
142
143 public boolean canDrinkPotions(Player player) {
144 return true;
145 }
146
147 public boolean canLogout(Player player) {
148 return true;
149 }
150
151 public boolean canSpellCast(Player player) {
152 return true;
153 }
154
156 public void sequence() {
157 update();
158
159 if (ticks == PAUSE) {
160 return;
161 }
162
163 if (ticks > 0) {
164 ticks--;
165 } else if (ticks == START) {
166 start();
167 } else if (ticks == FINISH) {
168 finish();
169 }
170 }
171
173 protected abstract void start();
174
176 public abstract void finish();
177
179 public abstract void cleanup();
180
182 public void update() {
183
184 }
185
186 public abstract ActivityType getType();
187
189 public void onLogout(Player player) {
190 remove(player);
191 }
192
194 public void onDeath(Mob mob) {
195 if (mob.isNpc()) {
196 World.schedule(new NpcDeath(mob.getNpc()));
197 return;
198 }
199 remove(mob);
201 mob.unpoison();
202 mob.unvenom();
203 finish();
204 }
205
206 protected void restart(int delay, Runnable runnable) {
207 World.schedule(delay, runnable::run); //deprecated lambda
208 }
209
210 public boolean onStep(Mob mob) {
211 return false;
212 }
213
215 public void setPanel(Activity_Panel panel) {
216 this.panel = panel;
217 }
218
220 public Optional<Activity_Panel> getPanel() {
221 return Optional.ofNullable(panel);
222 }
223
225 public void onRegionChange(Player player) {
226 remove(player);
227 }
228
230 public boolean canTeleport(Player player) {
231 return false;
232 }
233
235 public void add(Mob mob) {
236 if (mob.isNpc() && !mob.isRegistered()) {
237 mob.register();
238 mob.unpoison();
239 mob.unvenom();
240 }
241 mob.setActivity(this);
242 mob.instance = instance;
243 getListener().ifPresent(mob.getCombat()::addListener);
244 }
245
247 public void remove(Mob mob) {
248 getListener().ifPresent(mob.getCombat()::removeListener);
249
250 if (mob.isNpc()) {
251 mob.getNpc().unregister();
252 } else {
253 mob.instance = Entity.DEFAULT_INSTANCE;
254 mob.getPlayer().setActivity(null);
255 mob.getPlayer().graphic(Graphic.RESET, true);
256 mob.getPlayer().animate(Animation.RESET, true);
257 // mob.getPlayer().send(new SendMessage("You have lost your current progress as you have teleported."));
258 }
259 }
260
262 public void removeAll(Mob... mobs) {
263 if (mobs.length != 0)
264 for (Mob mob : mobs) {
265 if (mob.isRegistered())
266 remove(mob);
267 }
268 }
269
271 public void setPause(boolean pause) {
272 ticks = pause ? PAUSE : START;
273 }
274
276 protected final void resetCooldown() {
277 cooldown(cooldown);
278 }
279
281 public void cooldown(int cooldown) {
282 this.ticks = cooldown;
283 }
284
286 protected final void finishCooldown() {
287 ticks = FINISH;
288 }
289
291 protected final void pause() {
292 ticks = PAUSE;
293 }
294
296 protected final boolean isPaused() {
297 return ticks == PAUSE;
298 }
299
300 public ActivityDeathType deathType() {
301 return ActivityDeathType.NORMAL;
302 }
303
305 public int getInstance() {
306 return instance;
307 }
308
309 public void setInstance(int instance) {
310 this.instance = instance;
311 }
312
314 public int getTicks() {
315 return ticks;
316 }
317
319 protected Optional<? extends ActivityListener<? extends Activity>> getListener() {
320 return Optional.empty();
321 }
322
323 protected boolean clickItem(Player player, ItemInteractionEvent event) {
324 return false;
325 }
326
327 protected boolean clickNpc(Player player, NpcInteractionEvent event) {
328 return false;
329 }
330
331 protected boolean clickObject(Player player, ObjectInteractionEvent event) {
332 return false;
333 }
334
335 protected boolean clickButton(Player player, ClickButtonInteractionEvent event) {
336 return false;
337 }
338
339 protected boolean useItem(Player player, ItemOnItemInteractionEvent event) {
340 return false;
341 }
342
343 protected boolean pickupItem(Player player, PickupItemInteractionEvent event) {
344 return false;
345 }
346
347 protected boolean useItem(Player player, ItemOnObjectInteractionEvent event) {
348 return false;
349 }
350
351 protected boolean itemContainerAction(Player player, ItemContainerInteractionEvent event) {
352 return false;
353 }
354
355 @Override
356 public boolean onEvent(Player player, InteractionEvent interactionEvent) {
357 final EventDispatcher dispatcher = new EventDispatcher(interactionEvent);
358 dispatcher.dispatch(InteractionEvent.InteractionType.CLICK_BUTTON, e -> clickButton(player, (ClickButtonInteractionEvent) e));
359 dispatcher.dispatch(InteractionEvent.InteractionType.ITEM_ON_ITEM, e -> useItem(player, (ItemOnItemInteractionEvent) e));
360 dispatcher.dispatch(InteractionEvent.InteractionType.ITEM_ON_OBJECT, e -> useItem(player, (ItemOnObjectInteractionEvent) e));
361 dispatcher.dispatch(InteractionEvent.InteractionType.FIRST_ITEM_CLICK, e -> clickItem(player, (FirstItemClickInteractionEvent) e));
362 dispatcher.dispatch(InteractionEvent.InteractionType.SECOND_ITEM_CLICK, e -> clickItem(player, (SecondItemClickInteractionEvent) e));
363 dispatcher.dispatch(InteractionEvent.InteractionType.THIRD_ITEM_CLICK, e -> clickItem(player, (ThirdItemClickInteractionEvent) e));
364// dispatcher.dispatch(InteractionType.FOURTH_ITEM_CLICK, e -> clickItem(player, (FourthItemClick) e));
365 dispatcher.dispatch(InteractionEvent.InteractionType.FIRST_CLICK_NPC, e -> clickNpc(player, (FirstNpcClick) e));
366 dispatcher.dispatch(InteractionEvent.InteractionType.SECOND_CLICK_NPC, e -> clickNpc(player, (SecondNpcClick) e));
367// dispatcher.dispatch(InteractionType.CLICK_NPC, e -> clickNpc(player, (ThirdNpcClick) e));
368// dispatcher.dispatch(InteractionType.CLICK_NPC, e -> clickNpc(player, (FourthNpcClick) e));
369 dispatcher.dispatch(InteractionEvent.InteractionType.FIRST_CLICK_OBJECT, e -> clickObject(player, (FirstObjectClick) e));
370 dispatcher.dispatch(InteractionEvent.InteractionType.SECOND_CLICK_OBJECT, e -> clickObject(player, (SecondObjectClick) e));
371 dispatcher.dispatch(InteractionEvent.InteractionType.THIRD_CLICK_OBJECT, e -> clickObject(player, (ThirdObjectClick) e));
372 dispatcher.dispatch(InteractionEvent.InteractionType.PICKUP_ITEM, e -> pickupItem(player, (PickupItemInteractionEvent) e));
373 dispatcher.dispatch(InteractionEvent.InteractionType.ITEM_CONTAINER_INTERACTION_EVENT, e -> itemContainerAction(player, (ItemContainerInteractionEvent) e));
374 return interactionEvent.isHandled();
375 }
376}
static final Position DEFAULT_POSITION
Definition Config.java:235
void setPanel(Activity_Panel panel)
Optional<? extends ActivityListener<? extends Activity > > getListener()
boolean canTeleport(Player player)
Activity(int cooldown, int instance)
Definition Activity.java:92
Optional< Activity_Panel > getPanel()
static void schedule(Task task)
Definition World.java:284
void move(Position position)
Definition Mob.java:377
abstract Combat<? extends Mob > getCombat()