240 lines
8.0 KiB
Java
240 lines
8.0 KiB
Java
package net.knarcraft.paidsigns;
|
|
|
|
import net.knarcraft.knarlib.KnarLib;
|
|
import net.knarcraft.knarlib.formatting.Translator;
|
|
import net.knarcraft.knarlib.util.UpdateChecker;
|
|
import net.knarcraft.paidsigns.command.AddCommand;
|
|
import net.knarcraft.paidsigns.command.AddConditionCommand;
|
|
import net.knarcraft.paidsigns.command.AddConditionTabCompleter;
|
|
import net.knarcraft.paidsigns.command.AddTabCompleter;
|
|
import net.knarcraft.paidsigns.command.EditCommand;
|
|
import net.knarcraft.paidsigns.command.EditTabCompleter;
|
|
import net.knarcraft.paidsigns.command.ListCommand;
|
|
import net.knarcraft.paidsigns.command.ListTabCompleter;
|
|
import net.knarcraft.paidsigns.command.PaidSignsTabCommand;
|
|
import net.knarcraft.paidsigns.command.ReloadTabCommand;
|
|
import net.knarcraft.paidsigns.command.RemoveConditionCommand;
|
|
import net.knarcraft.paidsigns.command.RemoveConditionTabCompleter;
|
|
import net.knarcraft.paidsigns.command.RemoveTabCommand;
|
|
import net.knarcraft.paidsigns.formatting.PaidSignsTranslatableMessage;
|
|
import net.knarcraft.paidsigns.listener.SignBreakListener;
|
|
import net.knarcraft.paidsigns.listener.SignListener;
|
|
import net.knarcraft.paidsigns.manager.EconomyManager;
|
|
import net.knarcraft.paidsigns.manager.PaidSignManager;
|
|
import net.knarcraft.paidsigns.manager.TrackedSignManager;
|
|
import net.milkbowl.vault.economy.Economy;
|
|
import org.bukkit.command.CommandExecutor;
|
|
import org.bukkit.command.PluginCommand;
|
|
import org.bukkit.command.TabCompleter;
|
|
import org.bukkit.command.TabExecutor;
|
|
import org.bukkit.configuration.file.FileConfiguration;
|
|
import org.bukkit.plugin.PluginManager;
|
|
import org.bukkit.plugin.RegisteredServiceProvider;
|
|
import org.bukkit.plugin.ServicesManager;
|
|
import org.bukkit.plugin.java.JavaPlugin;
|
|
|
|
/**
|
|
* The PaidSigns plugin's main class
|
|
*/
|
|
public final class PaidSigns extends JavaPlugin {
|
|
|
|
private static PaidSigns paidSigns;
|
|
private static Translator translator;
|
|
private PaidSignManager signManager;
|
|
private String language;
|
|
private boolean ignoreCase;
|
|
private boolean ignoreColor;
|
|
private boolean refundsEnabled;
|
|
private short refundPercentage;
|
|
private boolean refundAlways;
|
|
|
|
/**
|
|
* Instantiates a new paid signs object
|
|
*/
|
|
@SuppressWarnings("unused")
|
|
public PaidSigns() {
|
|
paidSigns = this;
|
|
}
|
|
|
|
/**
|
|
* Gets an instance of this plugin
|
|
*
|
|
* @return <p>An instance of this plugin</p>
|
|
*/
|
|
public static PaidSigns getInstance() {
|
|
return paidSigns;
|
|
}
|
|
|
|
/**
|
|
* Gets the translator to use for this plugin
|
|
*
|
|
* @return <p>The translator to use for this plugin</p>
|
|
*/
|
|
public static Translator getTranslator() {
|
|
return translator;
|
|
}
|
|
|
|
@Override
|
|
public void onEnable() {
|
|
KnarLib.setPlugin(this);
|
|
|
|
setupVault();
|
|
loadConfig();
|
|
|
|
//Initialize translator
|
|
translator = new Translator();
|
|
translator.registerMessageCategory(PaidSignsTranslatableMessage.BOOLEAN_TRUE);
|
|
translator.loadLanguages(language);
|
|
|
|
signManager = new PaidSignManager(PaidSignManager.loadSigns());
|
|
TrackedSignManager.loadTrackedSigns();
|
|
|
|
PluginManager pluginManager = getServer().getPluginManager();
|
|
pluginManager.registerEvents(new SignListener(), this);
|
|
pluginManager.registerEvents(new SignBreakListener(), this);
|
|
|
|
registerCommands();
|
|
|
|
UpdateChecker.checkForUpdate(this, "https://api.spigotmc.org/legacy/update.php?resource=105842",
|
|
() -> this.getDescription().getVersion(), null);
|
|
}
|
|
|
|
@Override
|
|
public void onDisable() {
|
|
}
|
|
|
|
/**
|
|
* Reloads this plugin
|
|
*/
|
|
public void reload() {
|
|
this.reloadConfig();
|
|
loadConfig();
|
|
translator.loadLanguages(language);
|
|
signManager = new PaidSignManager(PaidSignManager.loadSigns());
|
|
TrackedSignManager.loadTrackedSigns();
|
|
}
|
|
|
|
/**
|
|
* Gets the paid sign manager used to manage paid signs
|
|
*
|
|
* @return <p>The paid sign manager</p>
|
|
*/
|
|
public PaidSignManager getSignManager() {
|
|
return signManager;
|
|
}
|
|
|
|
/**
|
|
* Gets the default setting for whether to ignore the case of paid sign ids
|
|
*
|
|
* @return <p>The default ignore case value</p>
|
|
*/
|
|
public boolean ignoreCase() {
|
|
return this.ignoreCase;
|
|
}
|
|
|
|
/**
|
|
* Gets the default setting for whether to ignore the color of paid sign ids
|
|
*
|
|
* @return <p>The default ignore color value</p>
|
|
*/
|
|
public boolean ignoreColor() {
|
|
return this.ignoreColor;
|
|
}
|
|
|
|
/**
|
|
* Checks whether refunds are currently enabled
|
|
*
|
|
* @return <p>Whether refunds are currently enabled</p>
|
|
*/
|
|
public boolean areRefundsEnabled() {
|
|
return this.refundsEnabled;
|
|
}
|
|
|
|
/**
|
|
* Gets the percentage of the initial cost to refund the sign creator
|
|
*
|
|
* @return <p>The percentage of the cost to refund</p>
|
|
*/
|
|
public short getRefundPercentage() {
|
|
if (this.refundPercentage < 0) {
|
|
return 0;
|
|
} else if (refundPercentage > 100) {
|
|
return 100;
|
|
}
|
|
return this.refundPercentage;
|
|
}
|
|
|
|
/**
|
|
* Gets whether refunds should always happen, even if signs are not broken by players
|
|
*
|
|
* @return <p>True if refunds should always happen</p>
|
|
*/
|
|
public boolean refundAlways() {
|
|
return this.refundAlways;
|
|
}
|
|
|
|
/**
|
|
* Registers the commands used by this plugin
|
|
*/
|
|
private void registerCommands() {
|
|
TabExecutor paidSignsExecutor = new PaidSignsTabCommand();
|
|
registerCommand("paidSigns", paidSignsExecutor, paidSignsExecutor);
|
|
registerCommand("addPaidSign", new AddCommand(), new AddTabCompleter());
|
|
registerCommand("listPaidSigns", new ListCommand(), new ListTabCompleter());
|
|
registerCommand("addPaidSignCondition", new AddConditionCommand(), new AddConditionTabCompleter());
|
|
registerCommand("removePaidSignCondition", new RemoveConditionCommand(),
|
|
new RemoveConditionTabCompleter());
|
|
registerCommand("editPaidSign", new EditCommand(), new EditTabCompleter());
|
|
|
|
TabExecutor removeTabExecutor = new RemoveTabCommand();
|
|
registerCommand("removePaidSign", removeTabExecutor, removeTabExecutor);
|
|
TabExecutor reloadTabExecutor = new ReloadTabCommand();
|
|
registerCommand("reload", reloadTabExecutor, reloadTabExecutor);
|
|
}
|
|
|
|
/**
|
|
* Registers a command if possible
|
|
*
|
|
* @param command <p>The command to register</p>
|
|
* @param commandExecutor <p>The command executor for executing the command</p>
|
|
* @param tabCompleter <p>The tab completer for tab-completing the command</p>
|
|
*/
|
|
private void registerCommand(String command, CommandExecutor commandExecutor, TabCompleter tabCompleter) {
|
|
PluginCommand pluginCommand = this.getCommand(command);
|
|
if (pluginCommand != null) {
|
|
pluginCommand.setExecutor(commandExecutor);
|
|
pluginCommand.setTabCompleter(tabCompleter);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Loads the configuration file
|
|
*/
|
|
private void loadConfig() {
|
|
FileConfiguration config = this.getConfig();
|
|
config.options().copyDefaults(true);
|
|
this.saveDefaultConfig();
|
|
this.saveConfig();
|
|
language = config.getString("language", "en");
|
|
ignoreCase = config.getBoolean("ignoreCase", true);
|
|
ignoreColor = config.getBoolean("ignoreColor", false);
|
|
refundsEnabled = config.getBoolean("refundsEnabled", true);
|
|
refundPercentage = (short) config.getInt("refundPercentage", 100);
|
|
refundAlways = config.getBoolean("refundAlways", false);
|
|
}
|
|
|
|
/**
|
|
* Sets up Vault by getting plugins from their providers
|
|
*/
|
|
private void setupVault() {
|
|
ServicesManager servicesManager = this.getServer().getServicesManager();
|
|
RegisteredServiceProvider<Economy> economyProvider = servicesManager.getRegistration(Economy.class);
|
|
if (economyProvider != null) {
|
|
EconomyManager.initialize(economyProvider.getProvider());
|
|
} else {
|
|
throw new IllegalStateException("Error: Vault could not be loaded");
|
|
}
|
|
}
|
|
|
|
}
|