RuneHive-Game
Loading...
Searching...
No Matches
MysteryBoxEvent.java
Go to the documentation of this file.
1package com.runehive.content.mysterybox;
2
3import com.runehive.game.task.TickableTask;
4import com.runehive.game.world.World;
5import com.runehive.game.world.entity.mob.player.Player;
6import com.runehive.net.packet.out.SendColor;
7import com.runehive.net.packet.out.SendString;
8import com.runehive.util.Utility;
9
10import java.util.*;
11
12
13public class MysteryBoxEvent extends TickableTask {
14 private final Player player;
15 private List<MysteryItem> items;
16 private List<MysteryItem> allItems;
17 private int speed;
19
20
22 super(false, 1);
23 this.player = player;
24 this.items = new ArrayList<>();
25 this.allItems = new ArrayList<>();
26 this.speed = Utility.random(30, 40);
27 this.mysteryBox = player.mysteryBox;
28 }
29
30 private void move() {
31 allItems.add(items.get(0));
32 items.remove(0);
33 MysteryItem next = getNextItem();
34 allItems.remove(next);
35 items.add(next);
36 }
37
39 MysteryItem next = null;
40 for (MysteryItem item : allItems) {
41 if (!items.contains(item)) {
42 next = item;
43 break;
44 }
45 }
46 return next;
47 }
48
49 private double getItemProbability(MysteryItem item) {
50 switch (item.rarity) {
51 case COMMON:
52 return 0.40;
53 case UNCOMMON:
54 return 0.35;
55 case RARE:
56 return 0.15;
57 case EXOTIC:
58 return 0.10;
59 default:
60 return 0;
61 }
62 }
63
64
65
66 private void reward() {
67 double randomValue = Math.random();
68 double cumulativeProbability = 0.0;
69 MysteryItem reward = null;
70
71 Collections.sort(items, Comparator.comparingDouble(this::getItemProbability).reversed());
72
73 for (MysteryItem item : items) {
74 double itemProbability = getItemProbability(item);
75 cumulativeProbability += itemProbability;
76 if (randomValue <= cumulativeProbability) {
77 reward = item;
78 break;
79 }
80 }
81
82 if(reward == null) {
83 reward = items.get(0);
84 }
85 if (reward.rarity == MysteryRarity.EXOTIC) {
86 World.sendMessage("<icon=17><col=5739B3> osroyale: <col=" + player.right.getColor() + ">" + player.getName() + " </col>has won " + Utility.getAOrAn(reward.getName()) + " <col=5739B3>" + reward.getName() + " </col>from the <col=5739B3>" + mysteryBox.box.name() + "</col>.");
87 }
88
89 player.locking.unlock();
90 player.inventory.add(reward.getId(), reward.getAmount());
91 player.message("Congratulations! You have won @red@" + Utility.getAOrAn(reward.getName()) + " " + reward.getName() + "!\nThis item falls in the rarity of " + reward.rarity.name().toLowerCase() + ".");
92 System.out.println("Rarity: " + reward.rarity.name().toLowerCase());
93 }
94 @Override
95 protected boolean canSchedule() {
96 return player.mysteryBox.box != null;
97 }
98 @Override
99 protected void onSchedule() {
100 player.dialogueFactory.clear();
101 player.locking.lock();
102 allItems.addAll(Arrays.asList(mysteryBox.box.rewards()));
103 player.inventory.remove(mysteryBox.box.item(), 1);
104 mysteryBox.count = player.inventory.computeAmountForId(mysteryBox.box.item());
105 Collections.shuffle(allItems);
106 for (int index = 0; index < 11; index++) {
107 if (index >= allItems.size())
108 continue;
109 MysteryItem item = allItems.get(index);
110 items.add(item);
111 allItems.remove(index);
112 }
113
114 player.send(new SendColor(59508, 0xF01616));
115 player.send(new SendString("You have " + mysteryBox.count + " mystery box available!", 59507));
116 }
117
118 @Override
119 protected void tick() {
120 cancel();
121 }
122
123 @Override
124 protected void onCancel(boolean logout) {
125 if (logout) {
126 player.inventory.add(mysteryBox.box.item(), 1);
127 } else {
128 reward();
129 }
130 }
131}
boolean canSchedule()
A function executed on registration.
void onCancel(boolean logout)
A function executed on cancellation.
void onSchedule()
A function executed on registration.
synchronized final void cancel()
Cancels all subsequent executions.
Definition Task.java:113
TickableTask(boolean instant, int delay)
Represents the game world.
Definition World.java:46
static void sendMessage(String... messages)
Sends a global message.
Definition World.java:396
This class represents a character controlled by a player.
Definition Player.java:125
The OutgoingPacket that sends a color to a string 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 String getAOrAn(String nextWord)
A or an.
Definition Utility.java:116