RuneHive-Tarnish
Neural OSRS Enhancement Framework
Loading...
Searching...
No Matches
ClanRepository.java
1package com.osroyale.content.clanchannel;
2
3import com.osroyale.content.clanchannel.channel.ClanChannel;
4import com.osroyale.content.clanchannel.content.ClanViewer;
5import com.osroyale.util.GsonUtils;
6
7import java.io.File;
8import java.io.FileWriter;
9import java.nio.file.Path;
10import java.nio.file.Paths;
11import java.util.*;
12
54
55public class ClanRepository {
56
58 private static Map<String, ClanChannel> CHANNELS = new HashMap<>();
59
61 public static Set<ClanChannel> ACTIVE_CHANNELS = new HashSet<>();
62
64 public static Set<String> ACTIVE_TAGS = new HashSet<>();
65
67 public static Set<String> ACTIVE_NAMES = new HashSet<>();
68
70 public static Set<ClanChannel> ALLTIME = new TreeSet<>();
71
73 private static Set<ClanChannel> TOP_PVP = new TreeSet<>();
74
76 private static Set<ClanChannel> TOP_PVM = new TreeSet<>();
77
79 private static Set<ClanChannel> TOP_SKILLING = new TreeSet<>();
80
82 private static Set<ClanChannel> TOP_IRON_MAN = new TreeSet<>();
83
85 public static ClanChannel getChannel(String channel) {
86 return CHANNELS.get(channel.toLowerCase().trim());
87 }
88
90 public static void addChannel(ClanChannel channel) {
91 CHANNELS.put(channel.getOwner().toLowerCase().trim(), channel);
92 }
93
95 public static void removeChannel(ClanChannel channel) {
96 CHANNELS.remove(channel.getOwner().toLowerCase().trim());
97 ACTIVE_CHANNELS.remove(channel);
98 saveClan(channel);
99 }
100
101 public static void setActive(ClanChannel channel) {
102 ACTIVE_CHANNELS.add(channel);
103 }
104
105 public static void setInactive(ClanChannel channel) {
106 if (ACTIVE_CHANNELS.remove(channel)) {
107 saveClan(channel);
108 }
109 }
110
111 public static boolean nameExist(String name) {
112 return ACTIVE_TAGS.contains(name.toLowerCase().trim());
113 }
114
115 public static boolean tagExist(String tag) {
116 for (ClanChannel channel : CHANNELS.values()) {
117 if (channel.getTag().equalsIgnoreCase(tag))
118 return true;
119 }
120 return false;
121 }
122
123 public static void saveAllActiveClans() {
124 for (ClanChannel channel : ClanRepository.ACTIVE_CHANNELS) {
125 if (channel.activeSize() <= 0) {
126 continue;
127 }
128 saveClan(channel);
129 }
130 }
131
132 private static void saveClan(ClanChannel channel) {
133 Thread.startVirtualThread(() -> {
134 final File dir = Paths.get("data", "content", "clan").toFile();
135
136 if (!dir.exists()) {
137 dir.mkdirs();
138 }
139
140 try (FileWriter fw = new FileWriter(dir.toPath().resolve(channel.getOwner() + ".json").toFile())) {
141 fw.write(GsonUtils.JSON_PRETTY_NO_NULLS.get().toJson(channel.toJson()));
142 } catch (final Exception e) {
143 e.printStackTrace();
144 }
145 });
146 }
147
149 public static void loadChannels() {
150 Path path = Paths.get("./data/content/clan/");
151 File[] files = path.toFile().listFiles();
152
153 if (files == null) {
154 System.out.println("No clan files were found.");
155 return;
156 }
157
158 for (File file : files) {
159 String fileName = file.getName();
160 if (!fileName.toLowerCase().endsWith(".json")) continue;
161 /*ClanChannel channel = ClanChannel.fromJson(file.getName().replace(".json", ""));
162 if (channel != null) {
163 if (!channel.getTag().isEmpty())
164 ACTIVE_TAGS.add(channel.getTag());
165 if (!channel.getName().isEmpty())
166 ACTIVE_NAMES.add(channel.getName());
167 ALLTIME.add(channel);
168 CHANNELS.put(channel.getOwner().toLowerCase().trim(), channel);
169 }*/
170
171 ClanChannel.load(file.getName().replace(".json", ""));
172 }
173 }
174
175 public static Optional<Set<ClanChannel>> getTopChanels(ClanType type) {
176 if (type.equals(ClanType.PVP)) {
177 return Optional.of(TOP_PVP);
178 }
179 if (type.equals(ClanType.PVM)) {
180 return Optional.of(TOP_PVM);
181 }
182 if (type.equals(ClanType.SKILLING)) {
183 return Optional.of(TOP_SKILLING);
184 }
185 if (type.equals(ClanType.IRON_MAN)) {
186 return Optional.of(TOP_IRON_MAN);
187 }
188 return Optional.empty();
189 }
190
191 public static Optional<Set<ClanChannel>> getTopChanels(ClanViewer.Filter filter) {
192 if (filter.equals(ClanViewer.Filter.ALL_TIME)) {
193 return Optional.of(ALLTIME);
194 }
195 if (filter.equals(ClanViewer.Filter.TOP_PVM)) {
196 return Optional.of(TOP_PVM);
197 }
198 if (filter.equals(ClanViewer.Filter.TOP_PVP)) {
199 return Optional.of(TOP_PVP);
200 }
201 if (filter.equals(ClanViewer.Filter.TOP_SKILLING)) {
202 return Optional.of(TOP_SKILLING);
203 }
204 if (filter.equals(ClanViewer.Filter.TOP_IRON_MAN)) {
205 return Optional.of(TOP_IRON_MAN);
206 }
207 return Optional.empty();
208 }
209}
static void removeChannel(ClanChannel channel)
static ClanChannel getChannel(String channel)
static void addChannel(ClanChannel channel)