Files
src
README.md
Code
14 1,340
src/main/java/shadow/shadowthebot/ShadowthebotPlugin.java
package shadow.shadowthebot;
import java.io.File;
import java.io.IOException;
import java.util.Map;
import java.util.logging.Level;
import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender;
import org.bukkit.configuration.InvalidConfigurationException;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import shadow.shadowthebot.bot.BotManager;
import shadow.shadowthebot.command.ShadowCommand;
import shadow.shadowthebot.gui.GuiManager;
public final class ShadowthebotPlugin extends JavaPlugin {
private YamlConfiguration guiConfig;
private YamlConfiguration messagesConfig;
private BotManager botManager;
private GuiManager guiManager;
private boolean debugEnabled;
@Override
public void onEnable() {
saveDefaultConfig();
saveResourceIfMissing("gui.yml");
saveResourceIfMissing("messages.yml");
loadExternalConfigurations();
debugEnabled = getConfig().getBoolean("debug", false);
botManager = new BotManager(this);
guiManager = new GuiManager(this, botManager);
getServer().getPluginManager().registerEvents(botManager, this);
getServer().getPluginManager().registerEvents(guiManager, this);
botManager.start();
ShadowCommand command = new ShadowCommand(this, botManager, guiManager);
getCommand("shadowthebot").setExecutor(command);
getCommand("shadowthebot").setTabCompleter(command);
debug("Plugin enabled.");
}
@Override
public void onDisable() {
if (botManager != null) {
botManager.shutdown();
}
}
public void reloadPluginConfigurations() {
reloadConfig();
loadExternalConfigurations();
debugEnabled = getConfig().getBoolean("debug", false);
}
private void saveResourceIfMissing(String name) {
File file = new File(getDataFolder(), name);
if (!file.exists()) {
saveResource(name, false);
}
}
private void loadExternalConfigurations() {
guiConfig = loadYaml("gui.yml", guiConfig);
messagesConfig = loadYaml("messages.yml", messagesConfig);
}
private YamlConfiguration loadYaml(String name, YamlConfiguration previous) {
File file = new File(getDataFolder(), name);
YamlConfiguration candidate = new YamlConfiguration();
try {
candidate.load(file);
return candidate;
} catch (IOException | InvalidConfigurationException exception) {
getLogger().log(Level.SEVERE, "Could not load " + name + "; retaining the previous valid configuration.", exception);
return previous == null ? new YamlConfiguration() : previous;
}
}
public YamlConfiguration guiConfig() {
return guiConfig;
}
public BotManager botManager() {
return botManager;
}
public boolean isDebugEnabled() {
return debugEnabled;
}
public void setDebugEnabled(boolean debugEnabled) {
this.debugEnabled = debugEnabled;
}
public String permission(String key) {
return getConfig().getString("permissions." + key, "shadowthebot." + key);
}
public boolean hasPermission(CommandSender sender, String key) {
return sender.hasPermission(permission(key));
}
public String message(String key, Map<String, String> placeholders) {
String text = messagesConfig == null
? key
: messagesConfig.getString(key, key);
for (Map.Entry<String, String> entry : placeholders.entrySet()) {
text = text.replace("{" + entry.getKey() + "}", entry.getValue());
}
return ChatColor.translateAlternateColorCodes('&', text);
}
public void sendMessage(CommandSender sender, String key) {
sendMessage(sender, key, Map.of());
}
public void sendMessage(CommandSender sender, String key, Map<String, String> placeholders) {
sender.sendMessage(message(key, placeholders));
}
public void debug(String message) {
if (debugEnabled) {
getLogger().info("[Shadowthebot] " + message);
}
}
public void severe(String message, Throwable throwable) {
getLogger().log(Level.SEVERE, "[Shadowthebot] " + message, throwable);
}
}