RuneHive-Game
Loading...
Searching...
No Matches
TrapManager.java
Go to the documentation of this file.
1package com.runehive.content.skill.impl.hunter.trap;
2
3import com.runehive.game.Animation;
4import com.runehive.game.task.impl.HunterTask;
5import com.runehive.game.world.entity.mob.npc.Npc;
6import com.runehive.game.world.entity.mob.player.Player;
7import com.runehive.game.world.entity.skill.Skill;
8import com.runehive.game.world.object.CustomGameObject;
9import com.runehive.game.world.object.GameObject;
10import com.runehive.game.world.position.Position;
11import com.runehive.util.Utility;
12
13import java.util.List;
14import java.util.concurrent.CopyOnWriteArrayList;
15
16public class TrapManager {
17
18 public static List<Trap> traps = new CopyOnWriteArrayList<Trap>();
19
20 public static List<Npc> HUNTER_NPC_LIST = new CopyOnWriteArrayList<Npc>();
21
22 private static final int[] exps = {3254, 3744, 6041, 8811, 10272};
23
24 public static void register(final Trap trap) {
25 trap.getGameObject().register();
26 traps.add(trap);
27 if (trap.getOwner() != null)
28 trap.getOwner().trapsLaid++;
29 }
30
31 public static void deregister(Trap trap) {
32 trap.getGameObject().unregister();
33 traps.remove(trap);
34 if (trap.getOwner() != null)
35 trap.getOwner().trapsLaid--;
36 }
37
38
39 public static boolean canLay(Player client) {
40 if (!goodArea(client)) {
41 client.message("You need to be in a hunting area to lay a trap.");
42 return false;
43 }
44// if (!client.getClickDelay().elapsed(2000))
45// return false;
46 for (final Trap trap : traps) {
47 if (trap == null)
48 continue;
49 if (trap.getGameObject().getPosition().getX() == client.getPosition().getX() && trap.getGameObject().getPosition().getY() == client.getPosition().getY()) {
50 client.message("There is already a trap here, please place yours somewhere else.");
51 return false;
52 }
53 }
54
55 int x = client.getPosition().getX();
56 int y = client.getPosition().getY();
57
58 for (final Npc npc : HUNTER_NPC_LIST) {
59 if (npc == null || !npc.isVisible())
60 continue;
61 if (x == npc.getPosition().getX() && y == npc.getPosition().getY() || x == npc.getX() && y == npc.getY()) {
62 client.message("You cannot place your trap right here, try placing it somewhere else.");
63 return false;
64 }
65 }
66 if (client.trapsLaid >= getMaximumTraps(client)) {
67 client.message("You can only have a max of " + getMaximumTraps(client) + " traps setup at once.");
68 return false;
69 }
70 return true;
71 }
72
73
74 public static void handleRegionChange(Player client) {
75 if (client.trapsLaid > 0) {
76 for (final Trap trap : traps) {
77 if (trap == null)
78 continue;
79 if (trap.getOwner() != null && trap.getOwner().getUsername().equals(client.getUsername()) && !trap.getGameObject().getPosition().isWithinDistance(client.getPosition(), 50)) {
81 client.message("You didn't watch over your trap well enough, it has collapsed.");
82 }
83 }
84 }
85 }
86
87 public static boolean goodArea(Player client) {
88 int x = client.getPosition().getX();
89 int y = client.getPosition().getY();
90 return x >= 2758 && x <= 2965 && y >= 2880 && y <= 2954 || x >= 2300 && x <= 2320 && y >= 9790 && y <= 9800;
91 }
92
93 public static int getMaximumTraps(Player client) {
94 return client.skills.getLevel(Skill.HUNTER) / 20 + 1;
95 }
96
97
98
99 public static int getObjectIDByNPCID(int npcId) {
100 switch (npcId) {
101 case 5549:
102 return 9373;
103 case 5551:
104 return 9377;
105 case 5552:
106 return 9379;
107 case 5550:
108 return 9375;
109 case 5548:
110 return 9348;
111 }
112 return 0;
113 }
114
115 public static Trap getTrapForGameObject(final GameObject object) {
116 for (final Trap trap : traps) {
117 if (trap == null)
118 continue;
119 if (trap.getGameObject().getPosition().equals(object.getPosition()))
120 return trap;
121 }
122 return null;
123 }
124
125 public static void dismantle(Player client, GameObject trap) {
126 if (trap == null)
127 return;
128 final Trap theTrap = getTrapForGameObject(trap);
129 if (theTrap != null && theTrap.getOwner() == client) {
130 deregister(theTrap);
131 if (theTrap instanceof SnareTrap)
132 client.inventory.add(10006, 1);
133 else if (theTrap instanceof BoxTrap) {
134 client.inventory.add(10008, 1);
135 client.animate(new Animation(827));
136 }
137 client.message("You dismantle the trap..");
138 } else
139 client.message("You cannot dismantle someone else's trap.");
140 }
141
142 public static void layTrap(Player client, Trap trap) {
143 int id = 10006;
144 if (trap instanceof BoxTrap) {
145 id = 10008;
146 if (client.skills.getLevel(Skill.HUNTER) < 60) {
147 client.message("You need a Hunter level of at least 60 to lay this trap.");
148 return;
149 }
150 }
151 if (!client.inventory.contains(id))
152 return;
153 if (canLay(client)) {
154 register(trap);
155// client.getClickDelay().reset();
156 client.takeStep();
157 client.face(trap.getGameObject());
158 client.animate(new Animation(827));
159 if (trap instanceof SnareTrap) {
160 client.message("You set up a bird snare..");
161 client.inventory.remove(10006, 1);
162 } else if (trap instanceof BoxTrap) {
163 if (client.skills.getLevel(Skill.HUNTER) < 27) {
164 client.message("You need a Hunter level of at least 27 to do this.");
165 return;
166 }
167 client.message("You set up a box trap..");
168 client.inventory.remove(10008, 1);
169 }
171 }
172 }
173
174 public static int requiredLevel(int npcType) {
175 int levelToReturn = 1;
176 if (npcType == 5072)
177 levelToReturn = 19;
178 else if (npcType == 5074)
179 levelToReturn = 11;
180 else if (npcType == 5075)
181 levelToReturn = 5;
182 else if (npcType == 5076)
183 levelToReturn = 9;
184 else if (npcType == 5079)
185 levelToReturn = 53;
186 else if (npcType == 5080)
187 levelToReturn = 63;
188 return levelToReturn;
189 }
190
191 public static boolean isHunterNPC(int npc) {
192 return npc >= 5072 && npc <= 5080;
193 }
194
195 public static void lootTrap(Player client, GameObject trap) {
196 if (trap != null) {
197 client.face(trap.getPosition());
198 final Trap theTrap = getTrapForGameObject(trap);
199 if (theTrap != null) {
200 if (theTrap.getOwner() != null)
201 if (theTrap.getOwner() == client) {
202 if (theTrap instanceof SnareTrap) {
203 client.inventory.add(10006, 1);
204 client.inventory.add(526, 1);
205 if (theTrap.getGameObject().getId() == 19180) {
206 client.inventory.add(10088, 20 + Utility.random(30));
207 client.inventory.add(9978, 1);
208 client.message("You've succesfully caught a crimson swift.");
210 } else if (theTrap.getGameObject().getId() == 19184) {
211 client.inventory.add(10090, 20 + Utility.random(30));
212 client.inventory.add(9978, 1);
213 client.message("You've succesfully caught a Golden Warbler.");
215 } else if (theTrap.getGameObject().getId() == 19186) {
216 client.inventory.add(10091, 20 + Utility.random(50));
217 client.inventory.add(9978, 1);
218 client.message("You've succesfully caught a Copper Longtail.");
220 } else if (theTrap.getGameObject().getId() == 19182) {
221 client.inventory.add(10089, 20 + Utility.random(30));
222 client.inventory.add(9978, 1);
223 client.message("You've succesfully caught a Cerulean Twitch.");
224 client.skills.addExperience(Skill.HUNTER, (exps[3]));
225 } else if (theTrap.getGameObject().getId() == 19178) {
226 client.inventory.add(10087, 20 + Utility.random(30));
227 client.inventory.add(9978, 1);
228 client.message("You've succesfully caught a Tropical Wagtail.");
230 }
231 } else if (theTrap instanceof BoxTrap) {
232 client.inventory.add(10008, 1);
233 if (theTrap.getGameObject().getId() == 19191) {
234 client.inventory.add(10033, 1);
236 client.message("You've succesfully caught a chinchompa!");
237 } else if (theTrap.getGameObject().getId() == 19189) {
238 client.inventory.add(10034, 1);
240 client.message("You've succesfully caught a red chinchompa!");
241 }
242 }
243 deregister(theTrap);
244 client.animate(new Animation(827));
245 } else
246 client.message("This is not your trap.");
247 }
248 }
249 }
250
251 public static void catchNPC(Trap trap, Npc npc) {
252 if (trap.getTrapState().equals(Trap.TrapState.CAUGHT))
253 return;
254 if (trap.getOwner() != null) {
255 if (trap.getOwner().skills.getLevel(Skill.HUNTER) < requiredLevel(npc.id)) {
256 trap.getOwner().message("You failed to catch the animal because your Hunter level is too low.");
257 trap.getOwner().message("You need atleast " + requiredLevel(npc.id) + " Hunter to catch this animal");
258 return;
259 }
261 if (trap instanceof SnareTrap) {
262 GameObject object = new CustomGameObject(getObjectIDByNPCID(npc.id), new Position(trap.getGameObject().getPosition().getX(), trap.getGameObject().getPosition().getY()));
263 register(new SnareTrap(object, Trap.TrapState.CAUGHT, 100, trap.getOwner()));
264 } else {
265 GameObject object = new CustomGameObject(getObjectIDByNPCID(npc.id), new Position(trap.getGameObject().getPosition().getX(), trap.getGameObject().getPosition().getY()));
266 register(new BoxTrap(object, Trap.TrapState.CAUGHT, 100, trap.getOwner()));
267 }
268 HUNTER_NPC_LIST.remove(npc);
269 npc.setVisible(false);
270 npc.appendDeath();
271 }
272 }
273
274 public static boolean hasLarupia(Player client) {
275 return false;
276// return client.equipment.getItems()[Equipment.HEAD_SLOT].getId() == 10045 && client.getEquipment().getItem()[Equipment.BODY_SLOT].getId() == 10043 && client.getEquipment().getItem()[Equipment.LEG_SLOT].getId() == 10041;
277 }
278
279 public static void handleLogout(Player p) {
280 if (p.trapsLaid > 0) {
281 for (Trap trap : traps) {
282 if (trap != null) {
283 if (trap.getOwner() != null && trap.getOwner().getUsername().equals(p.getUsername())) {
285 if (trap instanceof SnareTrap)
286 p.inventory.add(10006, 1);
287 else if (trap instanceof BoxTrap) {
288 p.inventory.add(10008, 1);
289 p.animate(new Animation(827));
290 }
291 }
292 }
293 }
294 }
295 }
296}
GameObject getGameObject()
Gets the GameObject.
Definition Trap.java:31
static void dismantle(Player client, GameObject trap)
static void layTrap(Player client, Trap trap)
static void lootTrap(Player client, GameObject trap)
static Trap getTrapForGameObject(final GameObject object)
Class that models a single animation used by an entity.
void setVisible(boolean visible)
Definition Entity.java:102
void face(GameObject object)
Sets the client update flag to face a certain direction.
Definition Mob.java:289
Represents a non-player character in the in-game world.
Definition Npc.java:29
void appendDeath()
Handles the mob death.
Definition Npc.java:147
This class represents a character controlled by a player.
Definition Player.java:125
Represents a trainable and usable skill.
Definition Skill.java:18
static final int HUNTER
The hunter skill id.
Definition Skill.java:87
void addExperience(int id, double experience)
Adds experience to a given skill.
int getLevel(int id)
Gets the level of a skill.
boolean remove(Item item)
Attempts to withdraw item from this container.
boolean add(Item item)
Attempts to deposit item into this container.
boolean contains(int id)
Determines if this container contains id.
Represents a static game object loaded from the map fs.
Represents a single tile on the game world.
Definition Position.java:14
int getY()
Gets the absolute y coordinate.
Definition Position.java:46
int getX()
Gets the absolute x coordinate.
Definition Position.java:41
Handles miscellaneous methods.
Definition Utility.java:27
static int random(int bound)
Definition Utility.java:239
The possible states a trap can be in.
Definition Trap.java:9
default int getId()
Gets the object id.