RuneHive-Tarnish
Neural OSRS Enhancement Framework
Loading...
Searching...
No Matches
ProfileRepository.java
1package com.osroyale.game.world.entity.mob.player.profile;
2
3import com.google.gson.reflect.TypeToken;
4import com.osroyale.game.world.entity.mob.player.Player;
5import com.osroyale.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
57
58public final class ProfileRepository {
59 private static final Path FILE_PATH = Path.of("data", "profile", "world_profile_list.json");
60
62 private static Map<String, Profile> profiles = new HashMap<>();
63
65 public static boolean exist(String name) {
66 return profiles.containsKey(name);
67 }
68
70 public static boolean isFriend(Player player, String other) {
71 return player.relations.isFriendWith(other);
72 }
73
75 public static List<String> getRegistry(String host) {
76 List<String> list = new ArrayList<>();
77 for (Profile profile : profiles.values()) {
78 for (String host_list : profile.getHost()) {
79 if (host_list != null && host_list.equalsIgnoreCase(host)) {
80 list.add(profile.getName());
81 }
82 }
83 }
84 return list;
85 }
86
88 public static void put(Profile profile) {
89 final String name = profile.getName();
90 if (profiles.containsKey(name)) {
91 profiles.replace(name, profile);
92 } else {
93 profiles.put(name, profile);
94 }
95 save();
96 }
97
99 public static void load() {
100 if (!Files.exists(FILE_PATH)) return;
101
102 final Type type = new TypeToken<Map<String, Profile>>() {
103 }.getType();
104
105 try (final FileReader reader = new FileReader(FILE_PATH.toFile())) {
106 profiles = GsonUtils.json().fromJson(reader, type);
107 } catch (final Exception e) {
108 e.printStackTrace();
109 }
110 }
111
113 public static void save() {
114 Thread.startVirtualThread(() -> {
115 try {
116 final Path path = FILE_PATH;
117 Path parent = path.getParent();
118 if (parent == null) {
119 throw new UnsupportedOperationException("Path must have a parent " + path);
120 }
121 if (!Files.exists(parent)) {
122 parent = Files.createDirectories(parent);
123 }
124
125 final Path tempFile = Files.createTempFile(parent, path.getFileName().toString(), ".tmp");
126 Files.write(tempFile, GsonUtils.json().toJson(profiles).getBytes());
127
128 Files.move(tempFile, path, StandardCopyOption.ATOMIC_MOVE, StandardCopyOption.REPLACE_EXISTING);
129 } catch (final Exception e) {
130 e.printStackTrace();
131 }
132 });
133 }
134
135}