118 lines
4.1 KiB
Java
118 lines
4.1 KiB
Java
package fr.molzonas.painfulloss;
|
|
|
|
import fr.molzonas.painfulloss.commands.PainfulLossCommand;
|
|
import fr.molzonas.painfulloss.enums.PropertiesEnum;
|
|
import fr.molzonas.painfulloss.listeners.DeathListener;
|
|
import fr.molzonas.painfulloss.provider.CountPriceProvider;
|
|
import fr.molzonas.painfulloss.provider.EssentialsPriceProvider;
|
|
import fr.molzonas.painfulloss.provider.PriceProvider;
|
|
import fr.molzonas.painfulloss.utils.Message;
|
|
import lombok.Getter;
|
|
import org.bukkit.Bukkit;
|
|
import org.bukkit.command.PluginCommand;
|
|
import org.bukkit.plugin.Plugin;
|
|
import org.bukkit.plugin.java.JavaPlugin;
|
|
|
|
import java.util.Locale;
|
|
import java.util.Optional;
|
|
import java.util.logging.Level;
|
|
|
|
public final class PainfulLoss extends JavaPlugin {
|
|
public static final Locale DEFAULT_LOCALE = Locale.ENGLISH;
|
|
public static final String DEFAULT_LOCALE_TAG = "en-US";
|
|
@Getter private static PainfulLoss instance = null;
|
|
@Getter private static PriceProvider priceProvider = null;
|
|
|
|
@Override
|
|
public void onEnable() {
|
|
PainfulLoss.instance = this;
|
|
Message.init(DEFAULT_LOCALE);
|
|
configurationInit();
|
|
langInit();
|
|
registerCommands();
|
|
initPriceProvider();
|
|
registerListeners();
|
|
info("Plugin has been enabled!");
|
|
}
|
|
|
|
@Override
|
|
public void onDisable() {
|
|
info("Plugin has been disabled!");
|
|
}
|
|
|
|
private void configurationInit() {
|
|
saveDefaultConfig();
|
|
saveResource("lang/messages_en.properties", false);
|
|
saveResource("lang/messages_fr.properties", false);
|
|
}
|
|
|
|
private void langInit() {
|
|
Locale locale = Locale.forLanguageTag(PropertiesEnum.LOCALE.getString());
|
|
Message.init(locale);
|
|
}
|
|
|
|
private void registerCommands() {
|
|
PluginCommand command = getCommand("painfulloss");
|
|
if (command != null) {
|
|
PainfulLossCommand exec = new PainfulLossCommand();
|
|
command.setExecutor(exec);
|
|
command.setTabCompleter(exec);
|
|
}
|
|
}
|
|
|
|
private void registerListeners() {
|
|
Bukkit.getPluginManager().registerEvents(new DeathListener(), this);
|
|
}
|
|
|
|
private void initPriceProvider() {
|
|
Optional<PriceProvider> ultimateShopProvider = Optional.empty();
|
|
Optional<PriceProvider> essentialsProvider = Optional.empty();
|
|
PriceProvider countProvider = new CountPriceProvider();
|
|
|
|
// Ultimate Shop
|
|
Plugin ultimateShop = getServer().getPluginManager().getPlugin("UltimateShop");
|
|
if (ultimateShop != null && ultimateShop.isEnabled()) {
|
|
info("UltimateShop detected (but inactive for Painful Loss)");
|
|
}
|
|
|
|
// Essentials
|
|
Plugin essentials = getServer().getPluginManager().getPlugin("Essentials");
|
|
if (essentials instanceof com.earth2me.essentials.IEssentials ess && essentials.isEnabled()) {
|
|
EssentialsPriceProvider epp = new EssentialsPriceProvider(ess);
|
|
if (epp.isReady()) {
|
|
essentialsProvider = Optional.of(epp);
|
|
info("Essentials detected, worth.yml loaded");
|
|
} else {
|
|
info("Essentials detected, worth.yml empty or unavailable");
|
|
}
|
|
}
|
|
|
|
// Selection
|
|
String provider = PropertiesEnum.PRICE_PROVIDER.getString();
|
|
if (provider != null) {
|
|
switch (provider.toLowerCase()) {
|
|
case "ultimateshop": priceProvider = ultimateShopProvider.orElse(countProvider); break;
|
|
case "essentials": priceProvider = essentialsProvider.orElse(countProvider); break;
|
|
default: priceProvider = countProvider; break;
|
|
}
|
|
} else {
|
|
priceProvider = ultimateShopProvider.orElse(essentialsProvider.orElse(countProvider));
|
|
}
|
|
}
|
|
|
|
public void reload() {
|
|
reloadConfig();
|
|
Message.clearCache();
|
|
langInit();
|
|
initPriceProvider();
|
|
}
|
|
|
|
public static void log(Level level, String message) {
|
|
Bukkit.getLogger().log(level, "[Painful Loss] {0}", message);
|
|
}
|
|
|
|
public static void info(String message) {
|
|
PainfulLoss.log(Level.INFO, message);
|
|
}
|
|
}
|