-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathEnchantBookPlus.java
More file actions
98 lines (85 loc) · 3.1 KB
/
EnchantBookPlus.java
File metadata and controls
98 lines (85 loc) · 3.1 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
package pro.cloudnode.smp.enchantbookplus;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.plugin.java.JavaPlugin;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import pro.cloudnode.smp.enchantbookplus.event.PrepareAnvil;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.logging.Level;
import java.util.stream.Collectors;
public final class EnchantBookPlus extends JavaPlugin {
/**
* Get plugin instance.
*/
public static EnchantBookPlus getInstance() {
return getPlugin(EnchantBookPlus.class);
}
/**
* Register event listeners.
*/
private void registerEvents() {
getServer().getPluginManager().registerEvents(new PrepareAnvil(), this);
}
/**
* Config enchantments cache
*/
private @NotNull List<@NotNull ConfigEnchantmentEntry> configEnchantments = new ArrayList<>();
/**
* Config enchantments cache
*/
public @NotNull List<@NotNull ConfigEnchantmentEntry> getConfigEnchantments() {
return configEnchantments;
}
/**
* "ALL" enchantment cache
*/
private @Nullable ConfigEnchantmentEntry.AllConfigEnchantmentEntry allConfigEnchantment;
/**
* "ALL" enchantment cache
*/
public @NotNull Optional<ConfigEnchantmentEntry.@NotNull AllConfigEnchantmentEntry> getAllConfigEnchantment() {
return Optional.ofNullable(allConfigEnchantment);
}
/**
* Get enchantment from cache
*
* @param enchantment The Minecraft enchantment
*/
public @NotNull Optional<@NotNull ConfigEnchantmentEntry> getConfigEnchantment(final @NotNull Enchantment enchantment) {
final @NotNull Optional<@NotNull ConfigEnchantmentEntry> entry = getConfigEnchantments().stream().filter(c -> c.isEnchantment(enchantment)).findFirst();
return entry.isEmpty() ? getAllConfigEnchantment().map(a -> a.enchant(enchantment)) : entry;
}
/**
* Reload config
*/
public void reload() {
reloadConfig();
final @NotNull List<@NotNull ConfigEnchantmentEntry> enchants;
try {
enchants = ConfigEnchantmentEntry.config(getConfig().get("enchantments"));
}
catch (final @NotNull Exception exception) {
getLogger().log(Level.SEVERE, "Failed to load config", exception);
getServer().getPluginManager().disablePlugin(this);
return;
}
allConfigEnchantment = enchants.stream()
.filter(c -> c.name.equalsIgnoreCase("ALL")).findFirst().map(ConfigEnchantmentEntry.AllConfigEnchantmentEntry::from).orElse(null);
configEnchantments = enchants.stream().filter(c -> !c.name.equalsIgnoreCase("ALL")).collect(Collectors.toList());
}
@Override
public void onEnable() {
Permissions.init();
Objects.requireNonNull(getCommand("enchantbookplus")).setExecutor(new MainCommand());
registerEvents();
saveDefaultConfig();
reload();
}
@Override
public void onDisable() {
// Plugin shutdown logic
}
}