RuneHive-Game
Loading...
Searching...
No Matches
Pets.java
Go to the documentation of this file.
1package com.runehive.content.pet;
2
3import com.runehive.content.achievement.AchievementHandler;
4import com.runehive.content.achievement.AchievementKey;
5import com.runehive.content.collectionlog.CollectionLog;
6import com.runehive.content.collectionlog.CollectionLogData;
7import com.runehive.game.Animation;
8import com.runehive.game.task.Task;
9import com.runehive.game.world.World;
10import com.runehive.game.world.entity.mob.npc.Npc;
11import com.runehive.game.world.entity.mob.player.Player;
12import com.runehive.game.world.items.Item;
13import com.runehive.game.world.position.Position;
14import com.runehive.net.packet.out.SendMessage;
15import com.runehive.net.packet.out.SendScrollbar;
16import com.runehive.net.packet.out.SendString;
17import com.runehive.util.MessageColor;
18import com.runehive.util.Utility;
19
20import java.util.*;
21
22/**
23 * Handles spawning, rewarding and picking up of pets.
24 *
25 * @author Daniel
26 */
27public class Pets {
28
29 /** The cost of insuring a pet. */
30 public final static int INSRUANCE_COST = 8000000;
31
32 /** Handles calculating the chance of a player receiving a skilling pet. */
33 public static void onReward(Player player, int item, int chance) {
34 if (Utility.random(chance) == 0)
35 onObtain(player, item);
36 }
37
38 /**
39 * Handles rewarding with a 100% a chance
40 * @param player
41 * @param item
42 * @param chance
43 */
44 public static void onReward(Player player, int item) {
45 onObtain(player, item);
46 }
47
48 /** Generates a unique equation for special defined pets. */
49 public static boolean onReward(Player player, PetData pet) {
50 int chance = -1;
51 if (pet == PetData.JAD)
52 chance = 200;
53 if (pet == PetData.ROCKY)
54 chance = 5000;
55 if (pet == PetData.ROCK_GOLEM)
56 chance = 6500;
57 if (pet == PetData.BABY_DARTH)
58 chance = 30000;
59
60 if (chance != -1 && Utility.random(chance) == 0) {
61 onObtain(player, pet.getItem());
62 return true;
63 }
64
65 return false;
66 }
67
68 /** Handles a player receiving a pet onReward. */
69 private static void onObtain(Player player, int item) {
70 Position position = Utility.findAccessableTile(player);
71 if(PetData.forItem(item).isPresent() && (player.inventory.contains(item) || player.bank.contains(item) || (player.pet != null && player.pet.id == PetData.forItem(item).get().getNpc()))) {
72 player.send(new SendMessage("<col=FF0000>You have a funny feeling like you would've been followed."));
73 return;
74
75 }
76 if (player.pet == null && position != null) {
77 onSpawn(player, item, false);
78 } else if (player.inventory.hasCapacityFor(new Item(item, 1))) {
79 player.inventory.add(item, 1);
80 player.send(new SendMessage("You feel something weird sneaking into your backpack", MessageColor.RED));
81 } else {
82 player.send(new SendMessage("There was no space for the pet, he has vanished.", MessageColor.RED));
83 }
84
85 if (item == 20693) { //phoenix pet from wintertodt
87 }
88 }
89
90 /** Handles spawning a pet. */
91 public static boolean onSpawn(Player player, int item, boolean drop) {
92 if (!PetData.forItem(item).isPresent())
93 return false;
94 if (player.pet != null) {
95 player.send(new SendMessage("You already have a pet following you!"));
96 return true;
97 }
98 Position position = Utility.findAccessableTile(player);
99 if (position == null) {
100 player.send(new SendMessage("You cannot drop your pet from your current location!"));
101 return true;
102 }
103 PetData pets = PetData.forItem(item).get();
104 Npc pet = new Npc(pets.getNpc(), position);
105 pet.owner = player;
106
107 if (drop) {
108 player.face(position);
109 player.animate(new Animation(827));
110 player.inventory.remove(item, 1);
111 }
112
113 pet.register();
114 pet.interact(player);
115 pet.follow(player);
116 pet.instance = player.instance;
117 player.pet = pet;
118
119 if (!drop) {
120 player.message("<col=FF0000>You have a funny feeling like you're being followed.");
121 World.sendMessage("<icon=11> <col=FF0000>osroyale: <col=" + player.right.getColor() + ">" + player.getName() + "</col> has just received a pet " + pet.getName() + "!");
123 }
124 return true;
125 }
126
127 /** Handles speaking to a pet. */
128 public static boolean dialogue(Player player, Npc npc) {
129 Optional<PetData> pet = PetData.forNpc(npc.id);
130 if (!pet.isPresent()) {
131 return false;
132 }
133 if (npc.owner == null || !npc.owner.equals(player)) {
134 player.send(new SendMessage(npc.getName() + " seems uninterested in speaking to you."));
135 return true;
136 }
137 pet.get().dialogue(player.dialogueFactory);
138 return true;
139 }
140
141 /** Handles what happens to a pet when a player dies. TODO: MULTIPLE OF THE SAME PET */
142 public static void onDeath(Player player) {
143 Map<PetData, Boolean> lostPets = new HashMap<>();
144 if (player.pet != null) {
145 Optional<PetData> petData = PetData.forNpc(player.pet.id);
146 petData.ifPresent(pet -> {
147 lostPets.put(pet, true);
148 });
149 }
150 for (Item items : player.inventory) {
151 if (items != null) {
152 Optional<PetData> petData = PetData.forItem(items.getId());
153 petData.ifPresent(pet -> {
154 lostPets.put(pet, false);
155 });
156 }
157 }
158 for (PetData pets : lostPets.keySet()) {
159 if (hasInsurance(player, pets)) {
160 player.lostPets.add(pets);
161 player.inventory.remove(pets.getItem());
162 if (lostPets.get(pets)) {
163 player.pet.unregister();
164 player.pet = null;
165 }
166 player.message("You have lost your pet, luckily you insured it! Speak to the insurance agent to claim.");
167 } else {
168 player.inventory.remove(pets.getItem());
169 if (lostPets.get(pets)) {
170 player.pet.unregister();
171 player.pet = null;
172 } else {
173 Npc npc = new Npc(pets.getNpc(), player.getPosition());
174 npc.unregister();
175 World.schedule(abandon(npc));
176 }
177 player.send(new SendMessage("Your " + pets.getName() + " has disappeared forever! Make sure to insure it!"));
178 }
179 }
180 }
181
182
183 /** The pet abandon task. */
184 public static Task abandon(Npc npc) {
185 return new Task(5) {
186 int count = 0;
187
188 @Override
189 protected void onSchedule() {
190 npc.resetWaypoint();
191 npc.resetFace();
192 }
193
194 @Override
195 protected void execute() {
196 if (count >= 15) {
197 cancel();
198 return;
199 }
200 if (npc.movement.isMoving()) {
201 return;
202 }
203 Position[] boundaries = Utility.getInnerBoundaries(npc.getPosition().transform(-3, -2), 7, 7);
204 npc.walk(Utility.randomElement(boundaries));
205 count++;
206 }
207
208 @Override
209 protected void onCancel(boolean logout) {
210 npc.unregister();
211 }
212 };
213 }
214
215
216 /** Handles what happens to a pet when a player logouts. */
217 public static void onLogout(Player player) {
218 if (player.pet != null) {
219 player.pet.unregister();
220 }
221 }
222
223 /** Handles what happens to a pet when a player logs in. */
224 public static void onLogin(Player player) {
225 if (player.pet != null) {
226 Npc npc = player.pet;
227 npc.owner = player;
228 npc.register();
229 World.schedule(5, () -> {
230 npc.face(player);
231 npc.interact(player);
232 npc.follow(player);
233 });
234 }
235 }
236
237 /** Handles purchasing insurance for a pet. */
238 public static void buyInsurance(Player player, Item item) {
239 Optional<PetData> petData = PetData.forItem(item.getId());
240 if (!petData.isPresent()) {
241 player.dialogueFactory.sendNpcChat(7601, "I can't insure " + item.getName() + "!", "What do I look like to you?", "Show me some respect and use a pet on me.").execute();
242 return;
243 }
244 if (hasInsurance(player, petData.get())) {
245 player.dialogueFactory.sendNpcChat(7601, "You already have insurance for " + petData.get().getName() + "!").execute();
246 return;
247 }
248 if (!player.inventory.contains(995, INSRUANCE_COST)) {
249 player.dialogueFactory.sendNpcChat(7601, "Naw, it's going to cost " + Utility.formatDigits(INSRUANCE_COST) + " coins", "to insure your pet. Start hustling fam.").execute();
250 return;
251 }
252 player.inventory.remove(995, INSRUANCE_COST);
253 player.petInsurance.add(petData.get());
254 player.dialogueFactory.sendNpcChat(7601, "You can put your trust in my that I will", "protect your little " + petData.get().getName() + ".").execute();
255 }
256
257 /** Claims all the lost pets. */
258 public static void claimLostPets(Player player) {
259 if (player.lostPets.isEmpty()) {
260 player.dialogueFactory.sendNpcChat(7601, "You have no lost pets to claim!");
261 return;
262 }
263 int cost = player.lostPets.size() * 250_000;
264 if (!player.inventory.contains(995, cost)) {
265 player.dialogueFactory.sendNpcChat(7601, "You need " + Utility.formatDigits(cost) + " coins to claim all", "of your lost pets.");
266 return;
267 }
268 if (player.inventory.getFreeSlots() < player.lostPets.size()) {
269 player.dialogueFactory.sendNpcChat(7601, "You need " + player.lostPets.size() + " free inventory spaces", "to claim your pets!");
270 return;
271 }
272 player.inventory.remove(995, cost);
273 player.lostPets.forEach(petData -> player.inventory.add(new Item(petData.getItem(), 1)));
274 player.lostPets.clear();
275 player.dialogueFactory.sendNpcChat(7601, "You have claimed all your lost pets for " + Utility.formatDigits(cost) + " coins.");
276 }
277
278 /** Handles opening the insurance interface. */
279 public static void openInsurance(Player player) {
280 int size = PetData.values().length;
281 for (int index = 0, string = 37115; index < size; index++) {
282 PetData pet = PetData.forOrdinal(index).get();
283 player.send(new SendString("<col=" + (hasInsurance(player, pet) ? "347043" : "F24444") + ">" + pet.getName(), string++));
284 }
285 player.send(new SendString("<col=000000>This is a list of all pets that are allowed to be insured", 37111));
286 player.send(new SendString("<col=000000>for <col=3c50b2>" + Utility.formatDigits(INSRUANCE_COST) + "<col=000000> gp. Insurance will protect a pet on death.", 37112));
287 player.send(new SendString("<col=000000>Red = <col=F24444>Un-insured<col=000000> | Green = <col=347043>Insured", 37113));
288 player.send(new SendString("", 37114));
289 player.send(new SendString("Total Pets: " + size, 37107));
290 player.send(new SendString("Pet Insurance Information", 37103));
291 player.send(new SendScrollbar(37110, size * 22));
292 player.interfaceManager.open(37100);
293 }
294
295 /** Handles opening the insurance interface. */
296 public static void openLostPets(Player player) {
297 List<PetData> pets = new ArrayList<>();
298 pets.addAll(player.lostPets);
299 int size = pets.size() < 7 ? 7 : pets.size();
300 for (int index = 0, string = 37115; index < size; index++) {
301 boolean valid = !pets.isEmpty() && index < pets.size();
302 PetData pet = valid ? pets.get(index) : null;
303 player.send(new SendString(valid ? "<col=255>" + pet.getName() : "", string++));
304 }
305 int cost = player.lostPets.size() * 250_000;
306 player.send(new SendString("<col=000000>This is a list of all pets available to be claimed", 37111));
307 player.send(new SendString("<col=000000>Total Free <col=3c50b2>" + Utility.formatDigits(cost) + "<col=000000> gp", 37112));
308 player.send(new SendString("", 37113));
309 player.send(new SendString(pets.isEmpty() ? "You have no pets to collect!" : "", 37114));
310 player.send(new SendString("Total Lost: " + pets.size(), 37107));
311 player.send(new SendString("Lost Pets", 37103));
312 player.send(new SendScrollbar(37110, 0));
313 player.interfaceManager.open(37100);
314 }
315
316 /** Checks if player already has insurance for a pet. */
317 private static boolean hasInsurance(Player player, PetData pet) {
318 return player.petInsurance.contains(pet);
319 }
320}
static void activate(Player player, AchievementKey achievement)
Activates the achievement for the individual player.
static void logItem(Player player, CollectionLogData data, int item, int amount)
final DialogueFactory execute()
Retrieves the next dialogue in the chain and executes it.
final DialogueFactory sendNpcChat(int id, String... lines)
Appends an NpcDialogue to the current dialogue chain.
Handles spawning, rewarding and picking up of pets.
Definition Pets.java:27
static void openInsurance(Player player)
Handles opening the insurance interface.
Definition Pets.java:279
static Task abandon(Npc npc)
The pet abandon task.
Definition Pets.java:184
static void onObtain(Player player, int item)
Handles a player receiving a pet onReward.
Definition Pets.java:69
static boolean onReward(Player player, PetData pet)
Generates a unique equation for special defined pets.
Definition Pets.java:49
static boolean onSpawn(Player player, int item, boolean drop)
Handles spawning a pet.
Definition Pets.java:91
static void onDeath(Player player)
Handles what happens to a pet when a player dies.
Definition Pets.java:142
static void claimLostPets(Player player)
Claims all the lost pets.
Definition Pets.java:258
static boolean hasInsurance(Player player, PetData pet)
Checks if player already has insurance for a pet.
Definition Pets.java:317
static void openLostPets(Player player)
Handles opening the insurance interface.
Definition Pets.java:296
static final int INSRUANCE_COST
The cost of insuring a pet.
Definition Pets.java:30
static boolean dialogue(Player player, Npc npc)
Handles speaking to a pet.
Definition Pets.java:128
static void buyInsurance(Player player, Item item)
Handles purchasing insurance for a pet.
Definition Pets.java:238
static void onReward(Player player, int item)
Handles rewarding with a 100% a chance.
Definition Pets.java:44
static void onLogin(Player player)
Handles what happens to a pet when a player logs in.
Definition Pets.java:224
static void onReward(Player player, int item, int chance)
Handles calculating the chance of a player receiving a skilling pet.
Definition Pets.java:33
static void onLogout(Player player)
Handles what happens to a pet when a player logouts.
Definition Pets.java:217
Class that models a single animation used by an entity.
A game representing a cyclic unit of work.
Definition Task.java:11
Represents the game world.
Definition World.java:46
static void schedule(Task task)
Submits a new event.
Definition World.java:247
static void sendMessage(String... messages)
Sends a global message.
Definition World.java:396
abstract boolean equals(Object obj)
void interact(Mob mob)
Sets the mob interacting with another mob.
Definition Mob.java:278
final void resetWaypoint()
Resets the waypoint.
Definition Mob.java:249
void face(GameObject object)
Sets the client update flag to face a certain direction.
Definition Mob.java:289
void resetFace()
Resets the mob's face location.
Definition Mob.java:330
Represents a non-player character in the in-game world.
Definition Npc.java:29
String getName()
Gets the name of this entity.
Definition Npc.java:166
void register()
Registers an entity to the World.
Definition Npc.java:112
void unregister()
Unregisters an entity from the World.
Definition Npc.java:126
void open(int identification)
Opens an interface for the player.
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
The container class that represents an item that can be interacted with.
Definition Item.java:21
final int getId()
Gets the identification of this item.
Definition Item.java:324
boolean remove(Item item)
Attempts to withdraw item from this container.
boolean add(Item item)
Attempts to deposit item into this container.
final boolean hasCapacityFor(Item... item)
Determines if this container has the capacity for item.
boolean contains(int id)
Determines if this container contains id.
Represents a single tile on the game world.
Definition Position.java:14
Position transform(int diffX, int diffY, int diffZ)
Creates a new location based on this location.
The OutgoingPacket that sends a message to a Players chatbox in the client.
The OutgoingPacket that sends a string to a Players itemcontainer in the client.
Handles miscellaneous methods.
Definition Utility.java:27
static int random(int bound)
Definition Utility.java:239
static Position findAccessableTile(Interactable source)
Definition Utility.java:478
static String formatDigits(final int amount)
Formats digits for integers.
Definition Utility.java:41
static< T > T randomElement(Collection< T > collection)
Picks a random element out of any array type.
Definition Utility.java:248
static Position[] getInnerBoundaries(Position position, int width, int length)
Definition Utility.java:621
Holds the data for pets.
Definition PetData.java:14
static Optional< PetData > forOrdinal(int ordinal)
Gets the pet data based on the given ordinal.
Definition PetData.java:495
static Optional< PetData > forNpc(int id)
Gets the pet data based on the given npc identification.
Definition PetData.java:490
static Optional< PetData > forItem(int id)
Gets the pet data based on the given item identification.
Definition PetData.java:485
int getNpc()
Gets the npc identification of the pet.
Definition PetData.java:480
Holds an enum of colors for ease.