forked from Solara-Development/Neptune
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHotbarService.java
More file actions
132 lines (107 loc) · 4.83 KB
/
HotbarService.java
File metadata and controls
132 lines (107 loc) · 4.83 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
package dev.lrxh.neptune.feature.hotbar;
import dev.lrxh.neptune.API;
import dev.lrxh.neptune.Neptune;
import dev.lrxh.neptune.configs.ConfigService;
import dev.lrxh.neptune.feature.hotbar.impl.CustomItem;
import dev.lrxh.neptune.feature.hotbar.impl.Hotbar;
import dev.lrxh.neptune.feature.hotbar.impl.Item;
import dev.lrxh.neptune.feature.hotbar.impl.ItemAction;
import dev.lrxh.neptune.profile.data.ProfileState;
import dev.lrxh.neptune.providers.manager.IService;
import dev.lrxh.neptune.utils.ConfigFile;
import lombok.Getter;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.Player;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Getter
public class HotbarService extends IService {
private static HotbarService instance;
private final Map<ProfileState, Hotbar> items = new HashMap<>();
private final Neptune plugin;
public HotbarService() {
this.plugin = Neptune.get();
}
public static HotbarService get() {
if (instance == null) instance = new HotbarService();
return instance;
}
Item getItem(Hotbar inventory, int slot) {
Item[] slots = inventory.getSlots();
if (slot >= 0 && slot < slots.length) {
return slots[slot];
} else {
return null;
}
}
public void giveItems(Player player) {
player.getInventory().clear();
player.getActivePotionEffects().clear();
ProfileState profileState = API.getProfile(player).getState();
if (profileState.equals(ProfileState.IN_KIT_EDITOR)) return;
Hotbar inventory = items.get(profileState);
if (inventory != null) {
for (int slot = 0; slot <= 8; slot++) {
Item item = getItemForSlot(inventory, slot);
if (item != null && item.isEnabled()) {
player.getInventory().setItem(item.getSlot(), item.constructItem(player.getUniqueId()));
}
}
}
player.updateInventory();
}
public Item getItemForSlot(Hotbar inventory, int slot) {
return getItem(inventory, slot);
}
@Override
public void load() {
FileConfiguration config = ConfigService.get().getHotbarConfig().getConfiguration();
if (config.getConfigurationSection("ITEMS") != null) {
for (String section : getKeys("ITEMS")) {
Hotbar inventory = new Hotbar();
for (String itemName : getKeys("ITEMS." + section)) {
String path = "ITEMS." + section + "." + itemName + ".";
String displayName = config.getString(path + "NAME");
String material = config.getString(path + "MATERIAL");
List<String> lore = config.getStringList(path + "LORE");
boolean enabled = config.getBoolean(path + "ENABLED");
byte slot = (byte) config.getInt(path + "SLOT");
int customModelData = config.getInt(path + "CUSTOM_MODEL_DATA", 0);
if (!enabled) continue;
try {
Item item = new Item(ItemAction.valueOf(itemName), displayName, material, lore, true, slot, customModelData);
if (slot >= 0 && slot < inventory.getSlots().length) {
inventory.setSlot(slot, item);
}
} catch (IllegalArgumentException ignored) {
}
items.put(ProfileState.valueOf(section), inventory);
}
}
}
if (config.getConfigurationSection("CUSTOM_ITEMS") != null) {
for (String itemName : getKeys("CUSTOM_ITEMS")) {
String path = "CUSTOM_ITEMS." + itemName + ".";
String displayName = config.getString(path + "NAME");
String material = config.getString(path + "MATERIAL");
byte slot = (byte) config.getInt(path + "SLOT");
List<String> lore = config.getStringList(path + "LORE");
String command = config.getString(path + "COMMAND");
List<String> commands = config.getStringList(path + "COMMANDS");
ProfileState profileState = ProfileState.valueOf(config.getString(path + "STATE"));
int customModelData = config.getInt(path + "CUSTOM_MODEL_DATA", 0);
if (command != null && !command.isEmpty() && !commands.contains(command)) commands.add(command);
CustomItem customItem = new CustomItem(displayName, material, lore, slot, commands, customModelData);
items.get(profileState).addItem(customItem, slot);
}
}
}
@Override
public void save() {
}
@Override
public ConfigFile getConfigFile() {
return ConfigService.get().getHotbarConfig();
}
}