RuneHive-Tarnish
Neural OSRS Enhancement Framework
Loading...
Searching...
No Matches
Pets.java
1package com.osroyale.content.pet;
2
3import com.osroyale.content.achievement.AchievementHandler;
4import com.osroyale.content.achievement.AchievementKey;
5import com.osroyale.content.collectionlog.CollectionLog;
6import com.osroyale.content.collectionlog.CollectionLogData;
7import com.osroyale.game.Animation;
8import com.osroyale.game.task.Task;
9import com.osroyale.game.world.World;
10import com.osroyale.game.world.entity.mob.npc.Npc;
11import com.osroyale.game.world.entity.mob.player.Player;
12import com.osroyale.game.world.items.Item;
13import com.osroyale.game.world.position.Position;
14import com.osroyale.net.packet.out.SendMessage;
15import com.osroyale.net.packet.out.SendScrollbar;
16import com.osroyale.net.packet.out.SendString;
17import com.osroyale.util.MessageColor;
18import com.osroyale.util.Utility;
19
20import java.util.*;
21
63
64public class Pets {
65
67 public final static int INSRUANCE_COST = 8000000;
68
70 public static void onReward(Player player, int item, int chance) {
71 if (Utility.random(chance) == 0)
72 onObtain(player, item);
73 }
74
81 public static void onReward(Player player, int item) {
82 onObtain(player, item);
83 }
84
86 public static boolean onReward(Player player, PetData pet) {
87 int chance = -1;
88 if (pet == PetData.JAD)
89 chance = 200;
90 if (pet == PetData.ROCKY)
91 chance = 5000;
92 if (pet == PetData.ROCK_GOLEM)
93 chance = 6500;
94 if (pet == PetData.BABY_DARTH)
95 chance = 30000;
96
97 if (chance != -1 && Utility.random(chance) == 0) {
98 onObtain(player, pet.getItem());
99 return true;
100 }
101
102 return false;
103 }
104
106 private static void onObtain(Player player, int item) {
107 Position position = Utility.findAccessableTile(player);
108 if(PetData.forItem(item).isPresent() && (player.inventory.contains(item) || player.bank.contains(item) || (player.pet != null && player.pet.id == PetData.forItem(item).get().getNpc()))) {
109 player.send(new SendMessage("<col=FF0000>You have a funny feeling like you would've been followed."));
110 return;
111
112 }
113 if (player.pet == null && position != null) {
114 onSpawn(player, item, false);
115 } else if (player.inventory.hasCapacityFor(new Item(item, 1))) {
116 player.inventory.add(item, 1);
117 player.send(new SendMessage("You feel something weird sneaking into your backpack", MessageColor.RED));
118 } else {
119 player.send(new SendMessage("There was no space for the pet, he has vanished.", MessageColor.RED));
120 }
121
122 if (item == 20693) { //phoenix pet from wintertodt
123 CollectionLog.logItem(player, CollectionLogData.WINTERTODT, 20693, 1);
124 }
125 }
126
128 public static boolean onSpawn(Player player, int item, boolean drop) {
129 if (!PetData.forItem(item).isPresent())
130 return false;
131 if (player.pet != null) {
132 player.send(new SendMessage("You already have a pet following you!"));
133 return true;
134 }
135 Position position = Utility.findAccessableTile(player);
136 if (position == null) {
137 player.send(new SendMessage("You cannot drop your pet from your current location!"));
138 return true;
139 }
140 PetData pets = PetData.forItem(item).get();
141 Npc pet = new Npc(pets.getNpc(), position);
142 pet.owner = player;
143
144 if (drop) {
145 player.face(position);
146 player.animate(new Animation(827));
147 player.inventory.remove(item, 1);
148 }
149
150 pet.register();
151 pet.interact(player);
152 pet.follow(player);
153 pet.instance = player.instance;
154 player.pet = pet;
155
156 if (!drop) {
157 player.message("<col=FF0000>You have a funny feeling like you're being followed.");
158 World.sendMessage("<icon=11> <col=FF0000>Tarnish: <col=" + player.right.getColor() + ">" + player.getName() + "</col> has just received a pet " + pet.getName() + "!");
159 AchievementHandler.activate(player, AchievementKey.OBTAIN_PET);
160 }
161 return true;
162 }
163
165 public static boolean dialogue(Player player, Npc npc) {
166 Optional<PetData> pet = PetData.forNpc(npc.id);
167 if (!pet.isPresent()) {
168 return false;
169 }
170 if (npc.owner == null || !npc.owner.equals(player)) {
171 player.send(new SendMessage(npc.getName() + " seems uninterested in speaking to you."));
172 return true;
173 }
174 pet.get().dialogue(player.dialogueFactory);
175 return true;
176 }
177
179 public static void onDeath(Player player) {
180 Map<PetData, Boolean> lostPets = new HashMap<>();
181 if (player.pet != null) {
182 Optional<PetData> petData = PetData.forNpc(player.pet.id);
183 petData.ifPresent(pet -> {
184 lostPets.put(pet, true);
185 });
186 }
187 for (Item items : player.inventory) {
188 if (items != null) {
189 Optional<PetData> petData = PetData.forItem(items.getId());
190 petData.ifPresent(pet -> {
191 lostPets.put(pet, false);
192 });
193 }
194 }
195 for (PetData pets : lostPets.keySet()) {
196 if (hasInsurance(player, pets)) {
197 player.lostPets.add(pets);
198 player.inventory.remove(pets.getItem());
199 if (lostPets.get(pets)) {
200 player.pet.unregister();
201 player.pet = null;
202 }
203 player.message("You have lost your pet, luckily you insured it! Speak to the insurance agent to claim.");
204 } else {
205 player.inventory.remove(pets.getItem());
206 if (lostPets.get(pets)) {
207 player.pet.unregister();
208 player.pet = null;
209 } else {
210 Npc npc = new Npc(pets.getNpc(), player.getPosition());
211 npc.unregister();
212 World.schedule(abandon(npc));
213 }
214 player.send(new SendMessage("Your " + pets.getName() + " has disappeared forever! Make sure to insure it!"));
215 }
216 }
217 }
218
219
221 public static Task abandon(Npc npc) {
222 return new Task(5) {
223 int count = 0;
224
225 @Override
226 protected void onSchedule() {
227 npc.resetWaypoint();
228 npc.resetFace();
229 }
230
231 @Override
232 protected void execute() {
233 if (count >= 15) {
234 cancel();
235 return;
236 }
237 if (npc.movement.isMoving()) {
238 return;
239 }
240 Position[] boundaries = Utility.getInnerBoundaries(npc.getPosition().transform(-3, -2), 7, 7);
241 npc.walk(Utility.randomElement(boundaries));
242 count++;
243 }
244
245 @Override
246 protected void onCancel(boolean logout) {
247 npc.unregister();
248 }
249 };
250 }
251
252
254 public static void onLogout(Player player) {
255 if (player.pet != null) {
256 player.pet.unregister();
257 }
258 }
259
261 public static void onLogin(Player player) {
262 if (player.pet != null) {
263 Npc npc = player.pet;
264 npc.owner = player;
265 npc.register();
266 World.schedule(5, () -> {
267 npc.face(player);
268 npc.interact(player);
269 npc.follow(player);
270 });
271 }
272 }
273
275 public static void buyInsurance(Player player, Item item) {
276 Optional<PetData> petData = PetData.forItem(item.getId());
277 if (!petData.isPresent()) {
278 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();
279 return;
280 }
281 if (hasInsurance(player, petData.get())) {
282 player.dialogueFactory.sendNpcChat(7601, "You already have insurance for " + petData.get().getName() + "!").execute();
283 return;
284 }
285 if (!player.inventory.contains(995, INSRUANCE_COST)) {
286 player.dialogueFactory.sendNpcChat(7601, "Naw, it's going to cost " + Utility.formatDigits(INSRUANCE_COST) + " coins", "to insure your pet. Start hustling fam.").execute();
287 return;
288 }
289 player.inventory.remove(995, INSRUANCE_COST);
290 player.petInsurance.add(petData.get());
291 player.dialogueFactory.sendNpcChat(7601, "You can put your trust in my that I will", "protect your little " + petData.get().getName() + ".").execute();
292 }
293
295 public static void claimLostPets(Player player) {
296 if (player.lostPets.isEmpty()) {
297 player.dialogueFactory.sendNpcChat(7601, "You have no lost pets to claim!");
298 return;
299 }
300 int cost = player.lostPets.size() * 250_000;
301 if (!player.inventory.contains(995, cost)) {
302 player.dialogueFactory.sendNpcChat(7601, "You need " + Utility.formatDigits(cost) + " coins to claim all", "of your lost pets.");
303 return;
304 }
305 if (player.inventory.getFreeSlots() < player.lostPets.size()) {
306 player.dialogueFactory.sendNpcChat(7601, "You need " + player.lostPets.size() + " free inventory spaces", "to claim your pets!");
307 return;
308 }
309 player.inventory.remove(995, cost);
310 player.lostPets.forEach(petData -> player.inventory.add(new Item(petData.getItem(), 1)));
311 player.lostPets.clear();
312 player.dialogueFactory.sendNpcChat(7601, "You have claimed all your lost pets for " + Utility.formatDigits(cost) + " coins.");
313 }
314
316 public static void openInsurance(Player player) {
317 int size = PetData.values().length;
318 for (int index = 0, string = 37115; index < size; index++) {
319 PetData pet = PetData.forOrdinal(index).get();
320 player.send(new SendString("<col=" + (hasInsurance(player, pet) ? "347043" : "F24444") + ">" + pet.getName(), string++));
321 }
322 player.send(new SendString("<col=000000>This is a list of all pets that are allowed to be insured", 37111));
323 player.send(new SendString("<col=000000>for <col=3c50b2>" + Utility.formatDigits(INSRUANCE_COST) + "<col=000000> gp. Insurance will protect a pet on death.", 37112));
324 player.send(new SendString("<col=000000>Red = <col=F24444>Un-insured<col=000000> | Green = <col=347043>Insured", 37113));
325 player.send(new SendString("", 37114));
326 player.send(new SendString("Total Pets: " + size, 37107));
327 player.send(new SendString("Pet Insurance Information", 37103));
328 player.send(new SendScrollbar(37110, size * 22));
329 player.interfaceManager.open(37100);
330 }
331
333 public static void openLostPets(Player player) {
334 List<PetData> pets = new ArrayList<>();
335 pets.addAll(player.lostPets);
336 int size = pets.size() < 7 ? 7 : pets.size();
337 for (int index = 0, string = 37115; index < size; index++) {
338 boolean valid = !pets.isEmpty() && index < pets.size();
339 PetData pet = valid ? pets.get(index) : null;
340 player.send(new SendString(valid ? "<col=255>" + pet.getName() : "", string++));
341 }
342 int cost = player.lostPets.size() * 250_000;
343 player.send(new SendString("<col=000000>This is a list of all pets available to be claimed", 37111));
344 player.send(new SendString("<col=000000>Total Free <col=3c50b2>" + Utility.formatDigits(cost) + "<col=000000> gp", 37112));
345 player.send(new SendString("", 37113));
346 player.send(new SendString(pets.isEmpty() ? "You have no pets to collect!" : "", 37114));
347 player.send(new SendString("Total Lost: " + pets.size(), 37107));
348 player.send(new SendString("Lost Pets", 37103));
349 player.send(new SendScrollbar(37110, 0));
350 player.interfaceManager.open(37100);
351 }
352
354 private static boolean hasInsurance(Player player, PetData pet) {
355 return player.petInsurance.contains(pet);
356 }
357}
static void activate(Player player, AchievementKey achievement)
final DialogueFactory sendNpcChat(int id, String... lines)
static void openInsurance(Player player)
Definition Pets.java:316
static boolean onReward(Player player, PetData pet)
Definition Pets.java:86
static final int INSRUANCE_COST
Definition Pets.java:67
static void claimLostPets(Player player)
Definition Pets.java:295
static boolean onSpawn(Player player, int item, boolean drop)
Definition Pets.java:128
static void openLostPets(Player player)
Definition Pets.java:333
static void onReward(Player player, int item)
Definition Pets.java:81
static void onReward(Player player, int item, int chance)
Definition Pets.java:70
static void buyInsurance(Player player, Item item)
Definition Pets.java:275
static void onLogout(Player player)
Definition Pets.java:254
static void onDeath(Player player)
Definition Pets.java:179
static void onLogin(Player player)
Definition Pets.java:261
static boolean dialogue(Player player, Npc npc)
Definition Pets.java:165
static Task abandon(Npc npc)
Definition Pets.java:221
static void sendMessage(String... messages)
Definition World.java:433
static void schedule(Task task)
Definition World.java:284
void face(GameObject object)
Definition Mob.java:326
Position transform(int diffX, int diffY, int diffZ)
static String formatDigits(final int amount)
Definition Utility.java:78
static< T > T randomElement(Collection< T > collection)
Definition Utility.java:285
static Optional< PetData > forOrdinal(int ordinal)
Definition PetData.java:517
static Optional< PetData > forNpc(int id)
Definition PetData.java:512
static Optional< PetData > forItem(int id)
Definition PetData.java:507