RuneHive-Game
Loading...
Searching...
No Matches
ProfileRepository.java
Go to the documentation of this file.
1package com.runehive.game.world.entity.mob.player.profile;
2
3import com.google.gson.reflect.TypeToken;
4import com.runehive.game.world.entity.mob.player.Player;
5import com.runehive.util.GsonUtils;
6
7import java.io.FileReader;
8import java.lang.reflect.Type;
9import java.nio.file.Files;
10import java.nio.file.Path;
11import java.nio.file.StandardCopyOption;
12import java.util.ArrayList;
13import java.util.HashMap;
14import java.util.List;
15import java.util.Map;
16
17/**
18 * Handles the profile repository, used for gathering important information for
19 * all created profiles.
20 *
21 * @author Daniel
22 */
23public final class ProfileRepository {
24 private static final Path FILE_PATH = Path.of("data", "profile", "world_profile_list.json");
25
26 /** The hash map of all the profiles. */
27 private static Map<String, Profile> profiles = new HashMap<>();
28
29 /** Checks if a profile is registered to the parameter. */
30 public static boolean exist(String name) {
31 return profiles.containsKey(name);
32 }
33
34 /** Checks if the other player is a friend. */
35 public static boolean isFriend(Player player, String other) {
36 return player.relations.isFriendWith(other);
37 }
38
39 /** Gets all the registered accounts to a specific host. */
40 public static List<String> getRegistry(String host) {
41 List<String> list = new ArrayList<>();
42 for (Profile profile : profiles.values()) {
43 for (String host_list : profile.getHost()) {
44 if (host_list != null && host_list.equalsIgnoreCase(host)) {
45 list.add(profile.getName());
46 }
47 }
48 }
49 return list;
50 }
51
52 /** Puts a profile into the hash map. */
53 public static void put(Profile profile) {
54 final String name = profile.getName();
55 if (profiles.containsKey(name)) {
56 profiles.replace(name, profile);
57 } else {
58 profiles.put(name, profile);
59 }
60 save();
61 }
62
63 /** Loads all the profiles. */
64 public static void load() {
65 if (!Files.exists(FILE_PATH)) return;
66
67 final Type type = new TypeToken<Map<String, Profile>>() {
68 }.getType();
69
70 try (final FileReader reader = new FileReader(FILE_PATH.toFile())) {
71 profiles = GsonUtils.json().fromJson(reader, type);
72 } catch (final Exception e) {
73 e.printStackTrace();
74 }
75 }
76
77 /** Saves all the profiles. */
78 public static void save() {
79 Thread.startVirtualThread(() -> {
80 try {
81 final Path path = FILE_PATH;
82 Path parent = path.getParent();
83 if (parent == null) {
84 throw new UnsupportedOperationException("Path must have a parent " + path);
85 }
86 if (!Files.exists(parent)) {
87 parent = Files.createDirectories(parent);
88 }
89
90 final Path tempFile = Files.createTempFile(parent, path.getFileName().toString(), ".tmp");
91 Files.write(tempFile, GsonUtils.json().toJson(profiles).getBytes());
92
93 Files.move(tempFile, path, StandardCopyOption.ATOMIC_MOVE, StandardCopyOption.REPLACE_EXISTING);
94 } catch (final Exception e) {
95 e.printStackTrace();
96 }
97 });
98 }
99
100}
This class represents a character controlled by a player.
Definition Player.java:125
Handles the profile repository, used for gathering important information for all created profiles.
static boolean isFriend(Player player, String other)
Checks if the other player is a friend.
static List< String > getRegistry(String host)
Gets all the registered accounts to a specific host.
static boolean exist(String name)
Checks if a profile is registered to the parameter.
static Map< String, Profile > profiles
The hash map of all the profiles.
static void put(Profile profile)
Puts a profile into the hash map.