forked from Solara-Development/Neptune
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKitService.java
More file actions
186 lines (158 loc) · 6.9 KB
/
KitService.java
File metadata and controls
186 lines (158 loc) · 6.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package dev.lrxh.neptune.game.kit;
import dev.lrxh.api.arena.IArena;
import dev.lrxh.api.kit.IKit;
import dev.lrxh.api.kit.IKitService;
import dev.lrxh.neptune.Neptune;
import dev.lrxh.neptune.configs.ConfigService;
import dev.lrxh.neptune.game.arena.Arena;
import dev.lrxh.neptune.game.arena.ArenaService;
import dev.lrxh.neptune.game.kit.impl.KitRule;
import dev.lrxh.neptune.providers.manager.IService;
import dev.lrxh.neptune.providers.manager.Value;
import dev.lrxh.neptune.utils.ConfigFile;
import dev.lrxh.neptune.utils.ItemUtils;
import dev.lrxh.neptune.utils.PotionEffectUtils;
import dev.lrxh.neptune.utils.ServerUtils;
import lombok.Getter;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.inventory.ItemStack;
import org.bukkit.potion.PotionEffect;
import java.util.*;
@Getter
public class KitService extends IService implements IKitService {
private static KitService instance;
public final LinkedHashSet<Kit> kits = new LinkedHashSet<>();
public static KitService get() {
if (instance == null) instance = new KitService();
return instance;
}
@Override
public void load() {
FileConfiguration config = ConfigService.get().getKitsConfig().getConfiguration();
if (config.contains("kits")) {
for (String kitName : getKeys("kits")) {
try {
String path = "kits." + kitName + ".";
String displayName = config.getString(path + "displayName", kitName);
ItemStack icon = ItemUtils.deserializeItem(config.getString(path + "icon", ""));
List<ItemStack> items = ItemUtils.deserialize(config.getString(path + "items", ""));
int slot = config.getInt(path + "slot", kits.size() + 1);
int kitEditorSlot = config.getInt(path + "kitEditor-slot", slot);
double health = config.getDouble(path + "health", 20);
double damageMultiplier = config.getDouble(path + "damage-multiplier", 1.0);
HashSet<Arena> arenas = new HashSet<>();
if (!config.getStringList(path + "arenas").isEmpty()) {
for (String arenaName : config.getStringList(path + "arenas")) {
Arena arena = ArenaService.get().getArenaByName(arenaName);
if (arena == null) {
ServerUtils.error("KitService: Arena " + arenaName + " not found for kit " + kitName);
continue;
}
arenas.add(arena);
}
}
HashMap<KitRule, Boolean> rules = new HashMap<>();
for (KitRule kitRule : KitRule.values()) {
rules.put(kitRule, config.getBoolean(path + kitRule.getSaveName(), false));
}
List<PotionEffect> potionEffects = new ArrayList<>();
if (!config.getStringList(path + "potionEffects").isEmpty()) {
for (String potion : config.getStringList(path + "potionEffects")) {
potionEffects.add(PotionEffectUtils.deserialize(potion));
}
}
kits.add(new Kit(kitName, displayName, items, arenas, icon, rules, slot, health, kitEditorSlot, potionEffects, damageMultiplier));
} catch (Exception e) {
Neptune.get().getLogger().severe("Error occurred while loading a kit with key: " + kitName);
Neptune.get().setErrored();
throw e;
}
}
}
}
public boolean add(Kit kit) {
for (Kit k : kits) {
if (k.equals(kit)) return true;
}
kits.add(kit);
return false;
}
public boolean addKit(IKit kit) {
return add((Kit) kit);
}
@Override
public void save() {
getConfigFile().getConfiguration().getKeys(false).forEach(key -> getConfigFile().getConfiguration().set(key, null));
kits.forEach(kit -> {
String path = "kits." + kit.getName().replaceAll("\\s+", "") + ".";
List<Value> values = new ArrayList<>();
values.add(new Value("displayName", kit.getDisplayName()));
values.add(new Value("items", ItemUtils.serialize(kit.getItems())));
values.add(new Value("arenas", kit.getArenasAsString()));
values.add(new Value("potionEffects", kit.getPotionsAsString()));
values.add(new Value("icon", ItemUtils.serialize(kit.getIcon())));
values.add(new Value("slot", kit.getSlot()));
values.add(new Value("health", kit.getHealth()));
values.add(new Value("kitEditor-slot", kit.getKitEditorSlot()));
values.add(new Value("damage-multiplier", kit.getDamageMultiplier()));
for (Map.Entry<KitRule, Boolean> kitRuleEntry : kit.getRules().entrySet()) {
values.add(new Value(kitRuleEntry.getKey().getSaveName(), kit.is(kitRuleEntry.getKey())));
}
save(values, path);
});
}
public Kit getKitByName(String kitName) {
for (Kit kit : kits) {
if (kit.getName().equalsIgnoreCase(kitName)) {
return kit;
}
}
return null;
}
public Kit getKitByDisplay(String kitName) {
for (Kit kit : kits) {
if (kit.getDisplayName().equals(kitName)) {
return kit;
}
}
return null;
}
public List<String> getKitNames() {
List<String> names = new ArrayList<>();
for (Kit kit : kits) {
names.add(kit.getName());
}
return names;
}
public void removeArenasFromKits(Arena arena) {
for (Kit kit : kits) {
kit.getArenas().remove(arena);
}
}
public void removeArena(IArena arena) {
removeArenasFromKits((Arena) arena);
}
@Override
public ConfigFile getConfigFile() {
return ConfigService.get().getKitsConfig();
}
public LinkedHashSet<IKit> getAllKits() {
return new LinkedHashSet<>(kits);
}
public Kit copyFrom(IKit kit) {
return new Kit(
kit.getName(),
kit.getDisplayName(),
kit.getItems(),
kit.getAllArenas().stream().map(ArenaService.get()::copyFrom).collect(HashSet::new, HashSet::add, HashSet::addAll),
kit.getIcon(),
kit.getRule().entrySet().stream().collect(HashMap::new,
(map, entry) -> map.put((KitRule) entry.getKey(), entry.getValue()), HashMap::putAll),
kit.getSlot(),
kit.getHealth(),
kit.getKitEditorSlot(),
kit.getPotionEffects(),
kit.getDamageMultiplier()
);
}
}