RuneHive-Game
Loading...
Searching...
No Matches
AchievementHandler.java
Go to the documentation of this file.
1package com.runehive.content.achievement;
2
3import com.runehive.game.world.entity.mob.player.Player;
4import com.runehive.game.world.items.containers.bank.VaultCurrency;
5import com.runehive.net.packet.out.SendAnnouncement;
6import com.runehive.net.packet.out.SendMessage;
7
8/**
9 * Handles the achievements.
10 *
11 * @author Daniel
12 */
13public class AchievementHandler {
14
15 /**
16 * Activates the achievement for the individual player. Increments the
17 * completed amount for the player. If the player has completed the
18 * achievement, they will receive their reward.
19 */
20 public static void activate(Player player, AchievementKey achievement) {
21 activate(player, achievement, 1);
22 }
23
24 /**
25 * Activates the achievement for the individual player. Increments the
26 * completed amount for the player. If the player has completed the
27 * achievement, they will receive their reward.
28 */
29 public static void activate(Player player, AchievementKey achievement, int increase) {
30 final int current = player.playerAchievements.computeIfAbsent(achievement, a -> 0);
31 for (AchievementList list : AchievementList.values()) {
32 if (list.getKey() == achievement) {
33 if (current >= list.getAmount())
34 continue;
35 player.playerAchievements.put(achievement, current + increase);
36 if (player.playerAchievements.get(achievement) >= list.getAmount()) {
37 player.bankVault.add(VaultCurrency.BLOOD_MONEY, list.getReward().getAmount());
38 player.send(new SendAnnouncement("You've completed the achievement", "'" + list.getTask() + "'", list.getType().getColor()));
39 }
40 }
41 }
42 }
43
44 public static void set(Player player, AchievementKey achievement, int increase) {
45 final int current = player.playerAchievements.computeIfAbsent(achievement, a -> 0);
46 for (AchievementList list : AchievementList.values()) {
47 if (list.getKey() == achievement) {
48 if (current >= list.getAmount())
49 continue;
50 player.playerAchievements.put(achievement, increase);
51 if (player.playerAchievements.get(achievement) >= list.getAmount()) {
52 player.bankVault.add(VaultCurrency.BLOOD_MONEY, list.getReward().getAmount());
53 player.send(new SendAnnouncement("You've completed the achievement", "'" + list.getTask() + "'", list.getType().getColor()));
54 }
55 }
56 }
57 }
58
59 /** Completes all the achievements for player (used for administrative purposes). */
60 public static void completeAll(Player player) {
61 if (!completedAll(player)) {
63 if (!player.playerAchievements.containsKey(achievement.getKey())) {
64 player.playerAchievements.put(achievement.getKey(), achievement.getAmount());
65 continue;
66 }
67 player.playerAchievements.replace(achievement.getKey(), achievement.getAmount());
68 }
69 player.send(new SendMessage("You have successfully mastered all achievements."));
70 }
71 }
72
73 /** Checks if the reward is completed. */
74 public static boolean completed(Player player, AchievementList achievement) {
75 if (!player.playerAchievements.containsKey(achievement.getKey()))
76 player.playerAchievements.put(achievement.getKey(), 0);
77 return player.playerAchievements.get(achievement.getKey()) >= achievement.getAmount();
78 }
79
80 /** Gets the total amount of achievements completed. */
81 public static int getTotalCompleted(Player player) {
82 int count = 0;
84 if (player.playerAchievements.containsKey(achievement.getKey()) && completed(player, achievement)) count++;
85 }
86 return count;
87 }
88
89 static int getDifficultyAmount(AchievementType difficulty) {
90 return AchievementList.asList(difficulty).size();
91 }
92
93 public static int getColor(Player player, AchievementType difficulty) {
94 int count = getDifficultyCompletion(player, difficulty);
95
96 if (count == 0) {
97 return 0xFF0000;
98 }
99
100 return count == getDifficultyAmount(difficulty) ? 0x257A1A : 0xFFFF00;
101 }
102
103 /** Handles getting the amount of achievements completed based on it's difficulty. */
104 public static int getDifficultyCompletion(Player player, AchievementType difficulty) {
105 int count = 0;
107 if (player.playerAchievements.containsKey(achievement.getKey()) && achievement.getType() == difficulty && completed(player, achievement))
108 count++;
109 }
110 return count;
111 }
112
113 /** Handles getting the amount of achievements based on the difficulty. */
114 public static int getDifficultyAchievement(AchievementType difficulty) {
115 int count = 0;
117 if (achievement.getType() == difficulty) count++;
118 }
119 return count;
120 }
121
122 /** Checks if a player has completed all the available achievements. */
123 public static boolean completedAll(Player player) {
124 return getTotalCompleted(player) == AchievementList.getTotal();
125 }
126}
static void activate(Player player, AchievementKey achievement, int increase)
Activates the achievement for the individual player.
static void activate(Player player, AchievementKey achievement)
Activates the achievement for the individual player.
static int getDifficultyAmount(AchievementType difficulty)
static int getDifficultyAchievement(AchievementType difficulty)
Handles getting the amount of achievements based on the difficulty.
static int getDifficultyCompletion(Player player, AchievementType difficulty)
Handles getting the amount of achievements completed based on it's difficulty.
static boolean completed(Player player, AchievementList achievement)
Checks if the reward is completed.
static void completeAll(Player player)
Completes all the achievements for player (used for administrative purposes).
static int getTotalCompleted(Player player)
Gets the total amount of achievements completed.
static boolean completedAll(Player player)
Checks if a player has completed all the available achievements.
static int getColor(Player player, AchievementType difficulty)
This class represents a character controlled by a player.
Definition Player.java:125
HashMap< AchievementKey, Integer > playerAchievements
Definition Player.java:366
boolean add(VaultCurrency currency, long amount)
Adds an amount into the player's bank vault with no checks.
The OutgoingPacket that sends a message to a Players chatbox in the client.
static List< AchievementList > asList(AchievementType difficulty)