RuneHive-Game
Loading...
Searching...
No Matches
FameHandler.java
Go to the documentation of this file.
1package com.runehive.content.famehall;
2
3import com.google.gson.Gson;
4import com.google.gson.GsonBuilder;
5import com.google.gson.JsonParser;
6import com.google.gson.reflect.TypeToken;
7import com.runehive.game.world.entity.mob.player.Player;
8import com.runehive.game.world.entity.mob.player.PlayerRight;
9import com.runehive.game.world.World;
10import com.runehive.game.world.items.Item;
11import com.runehive.net.packet.out.*;
12import com.runehive.util.Utility;
13
14import java.io.FileReader;
15import java.io.FileWriter;
16import java.lang.reflect.Type;
17import java.nio.file.Path;
18import java.nio.file.Paths;
19import java.util.HashMap;
20import java.util.List;
21import java.util.Map;
22import java.util.Map.Entry;
23
24/**
25 * This class handles the fame system.
26 *
27 * @author Daniel
28 */
29public class FameHandler {
30
31 /** Holds the hall of fame entries. */
32 public static Map<FameEntry, Fame> HALL_OF_FAME = new HashMap<>();
33
34 /**
35 * Checks if player should be entered in the hall of fame.
36 *
37 * @param player
38 * The player instance.
39 * @param fameEntry
40 * The fame entry.
41 */
42 public static void activate(Player player, FameEntry fameEntry) {
43 if (!HALL_OF_FAME.containsKey(fameEntry)) {
44 HALL_OF_FAME.put(fameEntry, new Fame(fameEntry.getType(), PlayerRight.getCrown(player) + " " + player.getName(), Utility.getDate()));
45 player.send(new SendMessage("Congratulations, you have left your mark on the hall of fame!"));
46 World.sendBroadcast(1, player.getName() + " is now on the hall of fame for " + fameEntry.getEntry(), false);
47 save();
48 }
49 }
50
51 /**
52 * Removes a fame entry from the hall of fame.
53 *
54 * @param fameEntry
55 * The fame entry to withdraw.
56 */
57 public static void remove(FameEntry fameEntry) {
58 if (HALL_OF_FAME.containsKey(fameEntry))
59 HALL_OF_FAME.remove(fameEntry);
60 }
61
62 /**
63 * Filters the hall of fame entries for a certain fame type.
64 *
65 * @param type
66 * The fame type to filer.
67 * @return Map of all the entries.
68 */
69 public static Map<FameEntry, Fame> getEntries(FameType type) {
70 Map<FameEntry, Fame> hall = new HashMap<>();
71 for (Entry<FameEntry, Fame> entries : HALL_OF_FAME.entrySet()) {
72 if (entries.getKey().getType() == type)
73 hall.put(entries.getKey(), entries.getValue());
74 }
75 return hall;
76 }
77
78 /**
79 * Gets the fame by the fame entry.
80 *
81 * @param entry
82 * The entry to get.
83 * @return The fame.
84 */
85 private static Fame getEntry(FameEntry entry) {
86 Fame result = null;
87 for (Entry<FameEntry, Fame> fame : getEntries(entry.getType()).entrySet()) {
88 if (fame.getKey().name().equalsIgnoreCase(entry.name())) {
89 result = fame.getValue();
90 break;
91 }
92 }
93 return result;
94 }
95
96 /**
97 * Opens the hall of fame itemcontainer.
98 *
99 * @param player
100 * The player instance.
101 * @param type
102 * The fame type tab.
103 */
104 public static void open(Player player, FameType type) {
105 int string = 58533;
106 List<FameEntry> entry_list = FameEntry.getEntries(type);
107 Item[] items = new Item[entry_list.size()];
108
109 for (int index = 0; index < entry_list.size(); index++) {
110 FameEntry display = entry_list.get(index);
111 Fame fame = getEntry(display);
112
113 items[index] = new Item(display.getDisplay());
114 player.send(new SendString(display == null ? "" : display.getEntry(), string));
115 string++;
116 player.send(new SendString(fame == null ? "---" : fame.getName(), string));
117 string++;
118 player.send(new SendString(fame == null ? "---" : fame.getAccomplished(), string));
119 string++;
120 string++;
121 }
122
123 player.send(new SendConfig(1150, type.ordinal()));
124 player.send(new SendString(HALL_OF_FAME.size() + "/" + FameEntry.values().length + " Remaining", 58523));
125 player.send(new SendString("<col=" + (type.ordinal() == 0 ? "ffffff" : "ff9933") + ">Player Killing", 58515));
126 player.send(new SendString("<col=" + (type.ordinal() == 1 ? "ffffff" : "ff9933") + ">Monster Killing", 58516));
127 player.send(new SendString("<col=" + (type.ordinal() == 2 ? "ffffff" : "ff9933") + ">Skilling", 58517));
128 player.send(new SendString("<col=" + (type.ordinal() == 3 ? "ffffff" : "ff9933") + ">Miscellaneous", 58518));
129 player.send(new SendItemOnInterface(58531, items));
130 player.send(new SendScrollbar(58530, (entry_list.size() * 34)));
131 player.interfaceManager.open(58500);
132 }
133
134 /**
135 * Searches through all the fame entries for a specific player.
136 *
137 * @param player
138 * The player instance.
139 * @param context
140 * The context being searched.
141 */
142 public static void search(Player player, String context) {
143 int index = 0;
144 for (Entry<FameEntry, Fame> entries : HALL_OF_FAME.entrySet()) {
145 if (entries.getValue().getName().equalsIgnoreCase(context)) {
146 index++;
147 }
148 }
149
150 String message = index == 0 ? Utility.formatName(context) + " was not found in the hall of fame." : index + " entries were found in the hall of fame for " + Utility.formatName(context) + ".";
151 player.send(new SendMessage(message));
152 }
153
154 /**
155 * Loads the hall of fames and puts them into the map.
156 */
157 public static void load() {
158 Type type = new TypeToken<Map<FameEntry, Fame>>() {
159 }.getType();
160
161 Path path = Paths.get("data", "/content/fame/hall_of_fame_list.json");
162 try (FileReader reader = new FileReader(path.toFile())) {
163 JsonParser parser = new JsonParser();
164 HALL_OF_FAME = new GsonBuilder().create().fromJson(parser.parse(reader), type);
165 } catch (Exception e) {
166 e.printStackTrace();
167 }
168 }
169
170 /**
171 * Saves the hall of fames into a json file.
172 */
173 public static void save() {
174 Gson gson = new GsonBuilder().setPrettyPrinting().disableHtmlEscaping().create();
175 try (FileWriter fw = new FileWriter("./data/content/fame/hall_of_fame_list.json")) {
176 fw.write(gson.toJson(HALL_OF_FAME));
177 } catch (final Exception e) {
178 e.printStackTrace();
179 }
180 }
181}
This class handles the fame system.
static void search(Player player, String context)
Searches through all the fame entries for a specific player.
static void activate(Player player, FameEntry fameEntry)
Checks if player should be entered in the hall of fame.
static Fame getEntry(FameEntry entry)
Gets the fame by the fame entry.
static Map< FameEntry, Fame > getEntries(FameType type)
Filters the hall of fame entries for a certain fame type.
static void load()
Loads the hall of fames and puts them into the map.
static void open(Player player, FameType type)
Opens the hall of fame itemcontainer.
static Map< FameEntry, Fame > HALL_OF_FAME
Holds the hall of fame entries.
static void save()
Saves the hall of fames into a json file.
String getName()
Gets the player name.
Definition Fame.java:49
String getAccomplished()
Gets the accomplished date.
Definition Fame.java:58
Represents the game world.
Definition World.java:46
static void sendBroadcast(int time, String message, boolean countdown)
Sends a game message.
Definition World.java:420
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
The OutgoingPacket responsible for changing settings on a client.
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 String formatName(final String input)
Definition Utility.java:645
static String getDate()
Gets the date of server.
Definition Utility.java:126
Holds all the fame entries.
String getEntry()
Gets the entry string.
static List< FameEntry > getEntries(FameType type)
Gets all the entries based on the fame type.
int getDisplay()
Gets the display item.
FameType getType()
Gets the fame type.
All the hall of fame types.
Definition FameType.java:8
static String getCrown(Player player)
Gets the crown display.