Files
src
pom.xml
Code
6 317
src/main/java/com/raidcaptain/drops/RaidCaptainDrops.java
package com.raidcaptain.drops;
import com.raidcaptain.drops.command.RaidCaptainCommand;
import com.raidcaptain.drops.listener.RaidCaptainDeathListener;
import org.bukkit.Bukkit;
import org.bukkit.plugin.java.JavaPlugin;
public final class RaidCaptainDrops extends JavaPlugin {
private static final boolean DEFAULT_ENABLED = true;
private static final boolean DEFAULT_DROP_ENABLED = true;
private static final int DEFAULT_CHANCE = 100;
private static final int DEFAULT_LEVEL = 1;
private static final boolean DEFAULT_MESSAGES_ENABLED = true;
private static final String DEFAULT_DROP_MESSAGE = "&aA Raid Captain dropped an Ominous Bottle!";
private boolean enabled;
private boolean dropEnabled;
private int dropChance;
private int bottleLevel;
private boolean messagesEnabled;
private String dropMessage;
@Override
public void onEnable() {
saveDefaultConfig();
reloadPluginConfig();
Bukkit.getPluginManager().registerEvents(new RaidCaptainDeathListener(this), this);
RaidCaptainCommand command = new RaidCaptainCommand(this);
if (getCommand("raidcaptain") == null) {
getLogger().severe("The raidcaptain command is missing from plugin.yml.");
return;
}
getCommand("raidcaptain").setExecutor(command);
getCommand("raidcaptain").setTabCompleter(command);
}
public void reloadPluginConfig() {
reloadConfig();
loadSettings();
}
private void loadSettings() {
enabled = readBoolean("enabled", DEFAULT_ENABLED);
dropEnabled = readBoolean("drop.enabled", DEFAULT_DROP_ENABLED);
int configuredChance = readInteger("drop.chance", DEFAULT_CHANCE);
if (configuredChance < 0 || configuredChance > 100) {
getLogger().warning("Invalid drop.chance; using " + DEFAULT_CHANCE + ".");
dropChance = DEFAULT_CHANCE;
} else {
dropChance = configuredChance;
}
int configuredLevel = readInteger("drop.level", DEFAULT_LEVEL);
if (configuredLevel < 1 || configuredLevel > 5) {
getLogger().warning("Invalid drop.level; using " + DEFAULT_LEVEL + ".");
bottleLevel = DEFAULT_LEVEL;
} else {
bottleLevel = configuredLevel;
}
messagesEnabled = readBoolean("messages.enabled", DEFAULT_MESSAGES_ENABLED);
Object configuredMessage = getConfig().get("messages.bottle-drop");
if (configuredMessage == null) {
dropMessage = DEFAULT_DROP_MESSAGE;
} else if (configuredMessage instanceof String message && !message.isBlank()) {
dropMessage = message;
} else {
getLogger().warning("Invalid messages.bottle-drop; using the default message.");
dropMessage = DEFAULT_DROP_MESSAGE;
}
}
private boolean readBoolean(String path, boolean defaultValue) {
Object value = getConfig().get(path);
if (value == null) {
return defaultValue;
}
if (value instanceof Boolean booleanValue) {
return booleanValue;
}
getLogger().warning("Invalid " + path + "; using " + defaultValue + ".");
return defaultValue;
}
private int readInteger(String path, int defaultValue) {
Object value = getConfig().get(path);
if (value == null) {
return defaultValue;
}
if (value instanceof Integer integerValue) {
return integerValue;
}
getLogger().warning("Invalid " + path + "; using " + defaultValue + ".");
return defaultValue;
}
public boolean isPluginEnabled() {
return enabled;
}
public boolean isDropEnabled() {
return dropEnabled;
}
public int getDropChance() {
return dropChance;
}
public int getBottleLevel() {
return bottleLevel;
}
public boolean areMessagesEnabled() {
return messagesEnabled;
}
public String getDropMessage() {
return dropMessage;
}
}