-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAdminToolboxPlugin.java
More file actions
232 lines (184 loc) · 8.36 KB
/
AdminToolboxPlugin.java
File metadata and controls
232 lines (184 loc) · 8.36 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
package org.modernbeta.admintoolbox;
import net.luckperms.api.LuckPerms;
import org.bstats.bukkit.Metrics;
import org.bukkit.Bukkit;
import org.bukkit.configuration.Configuration;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.plugin.RegisteredServiceProvider;
import org.bukkit.plugin.java.JavaPlugin;
import org.modernbeta.admintoolbox.commands.*;
import org.modernbeta.admintoolbox.integration.BlueMapIntegration;
import org.modernbeta.admintoolbox.integration.luckperms.LuckPermsIntegration;
import org.modernbeta.admintoolbox.integration.placeholderapi.PlaceholderAPIIntegration;
import org.modernbeta.admintoolbox.managers.FreezeManager;
import org.modernbeta.admintoolbox.managers.StreamerModeManager;
import org.modernbeta.admintoolbox.managers.admin.AdminManager;
import javax.annotation.Nullable;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.Optional;
@SuppressWarnings("UnstableApiUsage")
public class AdminToolboxPlugin extends JavaPlugin {
static AdminToolboxPlugin instance;
private ModrinthUpdateChecker updateChecker;
private AdminManager adminManager;
private FreezeManager freezeManager;
private @Nullable StreamerModeManager streamerModeManager;
PermissionAudience broadcastAudience;
private File adminStateConfigFile;
private FileConfiguration adminStateConfig;
private @Nullable BlueMapIntegration blueMapIntegration = null;
private @Nullable LuckPermsIntegration luckPermsIntegration = null;
private @Nullable PlaceholderAPIIntegration placeholderAPIIntegration = null;
private static final String ADMIN_STATE_CONFIG_FILENAME = "admin-state.yml";
private static final String BROADCAST_AUDIENCE_PERMISSION = "admintoolbox.broadcast.receive";
public static final String BROADCAST_EXEMPT_PERMISSION = "admintoolbox.broadcast.exempt";
private static final int BSTATS_PLUGIN_ID = 26406;
private static final String MODRINTH_PROJECT_ID = "TYi0LZWN";
@Override
public void onEnable() {
instance = this;
boolean shouldCheckUpdates = getConfig().getBoolean("check-updates", false);
if (shouldCheckUpdates)
getComponentLogger()
.info(new ModrinthUpdateChecker().getUpdateMessage(MODRINTH_PROJECT_ID));
this.adminManager = new AdminManager();
this.freezeManager = new FreezeManager();
this.broadcastAudience = new PermissionAudience(BROADCAST_AUDIENCE_PERMISSION);
createAdminStateConfig();
this.adminStateConfig = getAdminStateConfig();
getServer().getPluginManager().registerEvents(adminManager, this);
getServer().getPluginManager().registerEvents(freezeManager, this);
getCommand("admintoolbox").setExecutor(new PluginManageCommand());
getCommand("target").setExecutor(new TargetCommand());
getCommand("reveal").setExecutor(new RevealCommand());
getCommand("back").setExecutor(new GoBackCommand());
getCommand("forward").setExecutor(new GoForwardCommand());
getCommand("freeze").setExecutor(new FreezeCommand());
getCommand("unfreeze").setExecutor(new UnfreezeCommand());
getCommand("yell").setExecutor(new YellCommand());
getCommand("spawn").setExecutor(new SpawnCommand());
getCommand("fullbright").setExecutor(new FullbrightCommand());
initializeConfig();
try {
RegisteredServiceProvider<LuckPerms> provider = Bukkit.getServicesManager().getRegistration(LuckPerms.class);
if (provider != null) {
this.luckPermsIntegration = new LuckPermsIntegration(provider.getProvider());
this.luckPermsIntegration.registerCalculator();
this.streamerModeManager = new StreamerModeManager(this, luckPermsIntegration);
getCommand("streamermode").setExecutor(new StreamerModeCommand(streamerModeManager));
}
} catch (NoClassDefFoundError e) {
getLogger().warning("LuckPerms not found! Some features will be unavailable.");
// unregistering the command didn't always seem to work, this is more robust
getCommand("streamermode")
.setExecutor(UnavailableCommand.error("LuckPerms is required for this feature."));
}
try {
this.blueMapIntegration = new BlueMapIntegration();
} catch (NoClassDefFoundError | NoSuchElementException e) {
getLogger().warning("BlueMap API not found! Some features will be unavailable.");
}
try {
this.placeholderAPIIntegration = new PlaceholderAPIIntegration(this);
this.placeholderAPIIntegration.registerPlaceholders();
} catch (NoClassDefFoundError e) {
getLogger().warning("PlaceholderAPI is not available! Some features will be unavailable.");
}
// bStats - plugin analytics. Toggleable in server-level bStats config.
new Metrics(this, BSTATS_PLUGIN_ID);
getLogger().info(String.format("Enabled %s", getPluginMeta().getDisplayName()));
}
@Override
public void onDisable() {
this.getLuckPerms().ifPresent(LuckPermsIntegration::unregisterCalculator);
getLogger().info(String.format("Disabled %s", getPluginMeta().getDisplayName()));
}
private void createAdminStateConfig() {
this.adminStateConfigFile = new File(getDataFolder(), ADMIN_STATE_CONFIG_FILENAME);
if (!this.adminStateConfigFile.exists()) {
this.adminStateConfigFile.getParentFile().mkdirs();
saveResource(ADMIN_STATE_CONFIG_FILENAME, false);
}
this.adminStateConfig = YamlConfiguration.loadConfiguration(adminStateConfigFile);
}
public FileConfiguration getAdminStateConfig() {
// TODO: this re-reads the file from file system every time, should not be needed
// but we have run into some desynced state somehow. Figure out why!
try {
this.adminStateConfig.load(adminStateConfigFile);
} catch (Exception e) {
throw new RuntimeException(e);
}
return this.adminStateConfig;
}
public void saveAdminStateConfig() {
try {
this.adminStateConfig.save(adminStateConfigFile);
} catch (IOException e) {
// Throw this, this should never happen with the safeguards we use in onEnable
throw new RuntimeException(e);
}
}
public static AdminToolboxPlugin getInstance() {
return instance;
}
public AdminManager getAdminManager() {
return adminManager;
}
public FreezeManager getFreezeManager() {
return freezeManager;
}
public Optional<StreamerModeManager> getStreamerModeManager() {
return Optional.ofNullable(streamerModeManager);
}
public PermissionAudience getAdminAudience() {
return broadcastAudience;
}
public Optional<LuckPermsIntegration> getLuckPerms() {
return Optional.ofNullable(this.luckPermsIntegration);
}
public Optional<BlueMapIntegration> getBlueMap() {
return Optional.ofNullable(blueMapIntegration);
}
@Override
public void reloadConfig() {
super.reloadConfig();
getConfig().setDefaults(getConfigDefaults());
}
public Configuration getConfigDefaults() {
Configuration defaults = new YamlConfiguration();
defaults.set("check-updates", true);
defaults.setInlineComments("check-updates", List.of("Enable update check. When enabled, AdminToolbox will notify via the server console that a new version is available."));
// streamer-mode section
{
ConfigurationSection streamerMode = defaults.createSection("streamer-mode");
streamerMode.set("allow", true);
streamerMode.set("max-duration", 720d); // 720 minutes = 12 hours default max duration
streamerMode.set("disable-permissions", List.of("admintoolbox.broadcast.receive"));
streamerMode.set("show-indicators-when-active", false);
// docs
streamerMode.setInlineComments("allow", List.of("Enable or disable usage of streamer mode. 'true' is enabled, 'false' is disabled"));
streamerMode.setInlineComments("max-duration", List.of("The maximum duration a player can enable streamer mode for, in minutes."));
streamerMode.setInlineComments("disable-permissions", List.of("The list of permissions to disable for the given time period."));
streamerMode.setInlineComments("show-indicators-when-active", List.of("Should players see the streamer mode placeholder while in streamer mode? (See https://github.com/ModernBetaNetwork/AdminToolbox#placeholder)"));
}
return defaults;
}
private void initializeConfig() {
FileConfiguration config = getConfig();
Configuration defaults = getConfigDefaults();
config.setDefaults(defaults);
config.options().copyDefaults(true);
// Remove `enable-stats` option, it's toggleable at the server level.
if (config.isSet("enable-stats")) {
getConfig().set("enable-stats", null);
}
saveConfig();
reloadConfig();
}
}