forked from Solara-Development/Neptune
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKit.java
More file actions
285 lines (247 loc) · 9.17 KB
/
Kit.java
File metadata and controls
285 lines (247 loc) · 9.17 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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
package dev.lrxh.neptune.game.kit;
import dev.lrxh.api.arena.IArena;
import dev.lrxh.api.kit.IKit;
import dev.lrxh.api.kit.IKitRule;
import dev.lrxh.neptune.API;
import dev.lrxh.neptune.game.arena.Arena;
import dev.lrxh.neptune.game.arena.VirtualArena;
import dev.lrxh.neptune.game.kit.impl.KitRule;
import dev.lrxh.neptune.game.match.impl.participant.Participant;
import dev.lrxh.neptune.profile.ProfileService;
import dev.lrxh.neptune.profile.data.GameData;
import dev.lrxh.neptune.profile.data.KitData;
import dev.lrxh.neptune.profile.impl.Profile;
import dev.lrxh.neptune.utils.ItemUtils;
import dev.lrxh.neptune.utils.PlayerUtil;
import dev.lrxh.neptune.utils.PotionEffectUtils;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.inventory.meta.ShieldMeta;
import org.bukkit.potion.PotionEffect;
import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadLocalRandom;
@Getter
@Setter
@AllArgsConstructor
public class Kit implements IKit {
private String name;
private String displayName;
private List<ItemStack> items;
private HashSet<Arena> arenas;
private ItemStack icon;
private HashMap<KitRule, Boolean> rules;
private int queue, playing, slot, kitEditorSlot;
private double health;
private List<PotionEffect> potionEffects;
private double damageMultiplier;
public Kit(String name, String displayName, List<ItemStack> items, HashSet<Arena> arenas, ItemStack icon,
HashMap<KitRule, Boolean> rules, int slot, double health, int kitEditorSlot,
List<PotionEffect> potionEffects, double damageMultiplier) {
this.name = name;
this.displayName = displayName;
this.items = items;
this.arenas = arenas;
this.icon = icon;
this.rules = rules;
this.queue = 0;
this.playing = 0;
this.slot = slot;
this.health = health;
this.kitEditorSlot = kitEditorSlot;
this.potionEffects = potionEffects;
this.damageMultiplier = damageMultiplier;
addToProfiles();
}
public Kit(String name, Player player) {
this.name = name;
this.displayName = "&7" + name;
this.items = Arrays.stream(player.getInventory().getContents()).toList();
this.arenas = new HashSet<>();
this.icon = new ItemStack(Material.DIAMOND_SWORD);
this.rules = rules();
this.queue = 0;
this.playing = 0;
this.slot = KitService.get().kits.size() + 1;
this.health = 20;
this.kitEditorSlot = slot;
this.damageMultiplier = 1.0;
this.potionEffects = new ArrayList<>();
for (PotionEffect effect : player.getActivePotionEffects()) {
int currentDuration = effect.getDuration();
int maxDuration = PlayerUtil.getMaxDuration(player, effect.getType());
potionEffects.add(new PotionEffect(effect.getType(), Math.min(currentDuration, maxDuration),
effect.getAmplifier(), effect.isAmbient(), effect.hasParticles(), effect.hasIcon()));
}
addToProfiles();
}
public Kit(String name, List<ItemStack> items, ItemStack icon) {
this.name = name;
this.displayName = name;
this.items = items;
this.arenas = new HashSet<>();
this.rules = rules();
this.icon = icon.getType().equals(Material.AIR) ? new ItemStack(Material.BARRIER) : new ItemStack(icon);
this.queue = 0;
this.playing = 0;
this.slot = KitService.get().kits.size() + 1;
this.health = 20;
this.kitEditorSlot = slot;
this.potionEffects = new ArrayList<>();
this.damageMultiplier = 1.0;
addToProfiles();
}
private HashMap<KitRule, Boolean> rules() {
HashMap<KitRule, Boolean> rules = new HashMap<>();
for (KitRule kitRule : KitRule.values()) {
if (kitRule == KitRule.DAMAGE) rules.put(kitRule, true);
rules.put(kitRule, false);
}
return rules;
}
public void toggleArena(Arena arena) {
if (arenas.contains(arena)) {
arenas.remove(arena);
return;
}
arenas.add(arena);
}
public boolean isArenaAdded(Arena arena) {
return arenas.contains(arena);
}
private void addToProfiles() {
for (Profile profile : ProfileService.get().profiles.values()) {
profile.getGameData().getKitDataInternal().put(this, new KitData());
}
}
public List<String> getArenasAsString() {
List<String> arenasString = new ArrayList<>();
if (!arenas.isEmpty()) {
for (Arena arena : arenas) {
if (arena == null)
continue;
arenasString.add(arena.getName());
}
}
return arenasString;
}
public List<String> getPotionsAsString() {
List<String> potions = new ArrayList<>();
if (!potionEffects.isEmpty()) {
for (PotionEffect effect : potionEffects) {
if (effect == null)
continue;
potions.add(PotionEffectUtils.serialize(effect));
}
}
return potions;
}
public boolean is(KitRule kitRule) {
return rules.get(kitRule);
}
public void toggle(KitRule kitRule) {
rules.put(kitRule, !rules.get(kitRule));
}
public void removeQueue() {
if (!(queue == 0)) {
queue--;
}
}
public void addQueue() {
queue++;
}
public void removePlaying() {
if (!(playing == 0)) {
playing--;
}
}
@Override
public HashMap<IKitRule, Boolean> getRule() {
return rules.entrySet().stream().collect(HashMap::new,
(map, entry) -> map.put(entry.getKey(), entry.getValue()), HashMap::putAll);
}
public CompletableFuture<VirtualArena> getRandomArena() {
List<Arena> arenas1 = new ArrayList<>();
for (Arena arena : arenas) {
if (!arena.isEnabled())
continue;
if (!arena.isSetup() || !arena.isDoneLoading())
continue;
arenas1.add(arena);
}
if (arenas1.isEmpty())
return CompletableFuture.completedFuture(null);
Arena selected = arenas1.get(ThreadLocalRandom.current().nextInt(arenas1.size()));
return selected.createDuplicate().thenApply(arena -> arena);
}
@Override
public void giveLoadout(UUID playerUUID) {
giveLoadout(playerUUID, true);
}
public void giveLoadout(UUID playerUUID, boolean giveEffects) {
Player player = Bukkit.getPlayer(playerUUID);
if (player == null)
return;
Profile profile = API.getProfile(playerUUID);
GameData gameData = profile.getGameData();
List<ItemStack> loadout = gameData.get(this).getKitLoadout();
if (gameData.getKitData() == null || gameData.get(this) == null || loadout.isEmpty()) {
player.getInventory().setContents(items.toArray(new ItemStack[0]));
} else {
player.getInventory().setContents(loadout.toArray(new ItemStack[0]));
}
if (giveEffects) player.addPotionEffects(potionEffects);
ItemUtils.applyArmorTrim(profile);
player.updateInventory();
}
public void giveLoadout(Participant participant) {
Player player = participant.getPlayer();
if (player == null)
return;
Profile profile = API.getProfile(player);
GameData gameData = profile.getGameData();
if (gameData.getKitData() == null || gameData.get(this) == null ||
gameData.get(this).getKitLoadout().isEmpty()) {
player.getInventory().setContents(
ItemUtils.color(items.toArray(new ItemStack[0]), participant.getColor().getContentColor()));
} else {
player.getInventory()
.setContents(ItemUtils.color(gameData.get(this).getKitLoadout().toArray(new ItemStack[0]),
participant.getColor().getContentColor()));
}
player.addPotionEffects(potionEffects);
ItemUtils.applyArmorTrim(profile);
for(ItemStack item : player.getInventory()) {
if (item == null) continue;
ItemMeta meta = item.getItemMeta();
if (meta instanceof ShieldMeta shield) {
shield.setPatterns(profile.getSettingData().getShieldPatternPackage().getPatterns());
item.setItemMeta(shield);
}
}
player.updateInventory();
}
public void addPlaying() {
playing++;
}
public void delete() {
KitService.get().kits.remove(this);
KitService.get().save();
}
public HashSet<IArena> getAllArenas() {
return arenas.stream().collect(HashSet::new, HashSet::add, HashSet::addAll);
}
@Override
public boolean equals(Object o) {
if (o instanceof Kit kit) {
return kit.getName().equals(name);
}
return false;
}
}