2018-12-30 14:31:26 +01:00
|
|
|
package world.bentobox.challenges;
|
|
|
|
|
|
|
|
|
2019-01-26 14:36:48 +01:00
|
|
|
import org.bukkit.Bukkit;
|
2019-01-24 22:28:25 +01:00
|
|
|
import java.util.Optional;
|
|
|
|
|
2019-01-26 14:36:48 +01:00
|
|
|
import world.bentobox.bentobox.api.addons.Addon;
|
2019-01-10 10:36:06 +01:00
|
|
|
import world.bentobox.bentobox.api.configuration.Config;
|
2019-01-24 22:28:25 +01:00
|
|
|
import world.bentobox.bentobox.hooks.VaultHook;
|
2018-12-30 14:31:26 +01:00
|
|
|
import world.bentobox.challenges.commands.ChallengesCommand;
|
|
|
|
import world.bentobox.challenges.commands.admin.Challenges;
|
|
|
|
import world.bentobox.challenges.listeners.ResetListener;
|
|
|
|
import world.bentobox.challenges.listeners.SaveListener;
|
2019-01-24 22:28:25 +01:00
|
|
|
import world.bentobox.level.Level;
|
|
|
|
|
2018-12-30 14:31:26 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Add-on to BSkyBlock that enables challenges
|
|
|
|
* @author tastybento
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public class ChallengesAddon extends Addon {
|
|
|
|
|
2019-01-10 10:36:06 +01:00
|
|
|
// ---------------------------------------------------------------------
|
|
|
|
// Section: Variables
|
|
|
|
// ---------------------------------------------------------------------
|
|
|
|
|
2018-12-30 14:31:26 +01:00
|
|
|
private ChallengesManager challengesManager;
|
2019-01-10 10:36:06 +01:00
|
|
|
|
2019-01-10 10:16:04 +01:00
|
|
|
private ChallengesImportManager importManager;
|
2019-01-10 10:36:06 +01:00
|
|
|
|
|
|
|
private Settings settings;
|
|
|
|
|
2018-12-30 14:31:26 +01:00
|
|
|
private boolean hooked;
|
|
|
|
|
2019-01-23 20:34:40 +01:00
|
|
|
/**
|
2019-01-24 22:28:25 +01:00
|
|
|
* This boolean indicate if economy is enabled.
|
2019-01-23 20:34:40 +01:00
|
|
|
*/
|
|
|
|
private boolean economyProvided;
|
|
|
|
|
2019-01-24 22:28:25 +01:00
|
|
|
/**
|
|
|
|
* VaultHook that process economy.
|
|
|
|
*/
|
2019-01-28 19:21:48 +01:00
|
|
|
private VaultHook vaultHook;
|
2019-01-24 22:28:25 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Level addon.
|
|
|
|
*/
|
|
|
|
private Level levelAddon;
|
|
|
|
|
2019-01-23 20:34:40 +01:00
|
|
|
/**
|
|
|
|
* This indicate if level addon exists.
|
|
|
|
*/
|
|
|
|
private boolean levelProvided;
|
2019-01-10 10:36:06 +01:00
|
|
|
|
|
|
|
// ---------------------------------------------------------------------
|
|
|
|
// Section: Constants
|
|
|
|
// ---------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Permission prefix for addon.
|
|
|
|
*/
|
|
|
|
private static final String PERMISSION_PREFIX = "addon";
|
|
|
|
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------
|
|
|
|
// Section: Methods
|
|
|
|
// ---------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
2018-12-30 14:31:26 +01:00
|
|
|
@Override
|
|
|
|
public void onLoad() {
|
|
|
|
// Save default config.yml
|
2019-01-10 10:36:06 +01:00
|
|
|
this.saveDefaultConfig();
|
|
|
|
// Load the plugin's config
|
|
|
|
this.loadSettings();
|
2018-12-30 14:31:26 +01:00
|
|
|
}
|
|
|
|
|
2019-01-10 10:36:06 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
2018-12-30 14:31:26 +01:00
|
|
|
@Override
|
|
|
|
public void onEnable() {
|
|
|
|
// Check if it is enabled - it might be loaded, but not enabled.
|
2019-01-10 10:36:06 +01:00
|
|
|
if (this.getPlugin() == null || !this.getPlugin().isEnabled()) {
|
2018-12-30 14:31:26 +01:00
|
|
|
Bukkit.getLogger().severe("BentoBox is not available or disabled!");
|
|
|
|
this.setState(State.DISABLED);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Challenges Manager
|
2019-01-10 10:36:06 +01:00
|
|
|
this.challengesManager = new ChallengesManager(this);
|
2018-12-30 14:31:26 +01:00
|
|
|
// Challenge import setup
|
2019-01-10 10:36:06 +01:00
|
|
|
this.importManager = new ChallengesImportManager(this);
|
|
|
|
|
2019-01-23 19:19:18 +01:00
|
|
|
this.getPlugin().getAddonsManager().getGameModeAddons().forEach(gameModeAddon -> {
|
|
|
|
if (!this.settings.getDisabledGameModes().contains(gameModeAddon.getDescription().getName()))
|
|
|
|
{
|
|
|
|
if (gameModeAddon.getPlayerCommand().isPresent())
|
|
|
|
{
|
|
|
|
new ChallengesCommand(this, gameModeAddon.getPlayerCommand().get());
|
|
|
|
this.hooked = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (gameModeAddon.getAdminCommand().isPresent())
|
|
|
|
{
|
|
|
|
new Challenges(this, gameModeAddon.getAdminCommand().get());
|
|
|
|
this.hooked = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2019-01-10 10:36:06 +01:00
|
|
|
|
|
|
|
if (this.hooked) {
|
|
|
|
// Try to find Level addon and if it does not exist, display a warning
|
2019-01-23 20:34:40 +01:00
|
|
|
|
2019-01-24 22:28:25 +01:00
|
|
|
Optional<Addon> level = this.getAddonByName("Level");
|
2019-01-23 20:34:40 +01:00
|
|
|
|
2019-01-24 22:28:25 +01:00
|
|
|
if (!level.isPresent())
|
|
|
|
{
|
2019-01-10 10:36:06 +01:00
|
|
|
this.logWarning("Level add-on not found so level challenges will not work!");
|
2019-01-24 22:28:25 +01:00
|
|
|
this.levelAddon = null;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this.levelProvided = true;
|
|
|
|
this.levelAddon = (Level) level.get();
|
2018-12-30 14:31:26 +01:00
|
|
|
}
|
2019-01-10 10:36:06 +01:00
|
|
|
|
2019-01-28 19:21:48 +01:00
|
|
|
Optional<VaultHook> vault = this.getPlugin().getVault();
|
|
|
|
|
|
|
|
if (!vault.isPresent() || !vault.get().hook())
|
|
|
|
{
|
|
|
|
this.vaultHook = null;
|
|
|
|
this.logWarning("Economy plugin not found so money options will not work!");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this.economyProvided = true;
|
|
|
|
this.vaultHook = vault.get();
|
|
|
|
}
|
2019-01-23 20:34:40 +01:00
|
|
|
|
2019-01-10 10:36:06 +01:00
|
|
|
// Register the reset listener
|
|
|
|
this.registerListener(new ResetListener(this));
|
|
|
|
// Register the autosave listener.
|
|
|
|
this.registerListener(new SaveListener(this));
|
|
|
|
} else {
|
|
|
|
this.logError("Challenges could not hook into AcidIsland or BSkyBlock so will not do anything!");
|
2018-12-30 14:31:26 +01:00
|
|
|
this.setState(State.DISABLED);
|
|
|
|
}
|
2019-01-10 10:36:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public void onReload()
|
|
|
|
{
|
|
|
|
if (this.hooked) {
|
|
|
|
this.challengesManager.save();
|
|
|
|
|
|
|
|
this.loadSettings();
|
|
|
|
this.getLogger().info("Challenges addon reloaded.");
|
2018-12-30 14:31:26 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-10 10:36:06 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
2018-12-30 14:31:26 +01:00
|
|
|
@Override
|
2019-01-10 10:36:06 +01:00
|
|
|
public void onDisable() {
|
|
|
|
if (this.hooked) {
|
|
|
|
this.challengesManager.save();
|
|
|
|
}
|
2019-01-26 14:36:48 +01:00
|
|
|
|
|
|
|
if (this.settings != null)
|
|
|
|
{
|
|
|
|
new Config<>(this, Settings.class).saveConfigObject(this.settings);
|
|
|
|
}
|
2019-01-10 10:36:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This method loads addon configuration settings in memory.
|
|
|
|
*/
|
|
|
|
private void loadSettings() {
|
|
|
|
this.settings = new Config<>(this, Settings.class).loadConfigObject();
|
|
|
|
|
|
|
|
if (this.settings == null) {
|
|
|
|
// Disable
|
|
|
|
this.logError("Challenges settings could not load! Addon disabled.");
|
|
|
|
this.setState(State.DISABLED);
|
2018-12-30 14:31:26 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-10 10:36:06 +01:00
|
|
|
|
|
|
|
// ---------------------------------------------------------------------
|
|
|
|
// Section: Getters
|
|
|
|
// ---------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return challengesManager
|
|
|
|
*/
|
2018-12-30 14:31:26 +01:00
|
|
|
public ChallengesManager getChallengesManager() {
|
2019-01-10 10:36:06 +01:00
|
|
|
return this.challengesManager;
|
2018-12-30 14:31:26 +01:00
|
|
|
}
|
|
|
|
|
2019-01-10 10:36:06 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return Permission Prefix.
|
|
|
|
*/
|
2018-12-30 14:31:26 +01:00
|
|
|
@Override
|
|
|
|
public String getPermissionPrefix() {
|
2019-01-10 10:36:06 +01:00
|
|
|
return PERMISSION_PREFIX;
|
2018-12-30 14:31:26 +01:00
|
|
|
}
|
|
|
|
|
2019-01-10 10:36:06 +01:00
|
|
|
|
2018-12-30 14:31:26 +01:00
|
|
|
/**
|
|
|
|
* @return the importManager
|
|
|
|
*/
|
2019-01-10 10:16:04 +01:00
|
|
|
public ChallengesImportManager getImportManager() {
|
2019-01-10 10:36:06 +01:00
|
|
|
return this.importManager;
|
2018-12-30 14:31:26 +01:00
|
|
|
}
|
|
|
|
|
2019-01-10 10:36:06 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return the challenge settings.
|
|
|
|
*/
|
|
|
|
public Settings getChallengesSettings()
|
|
|
|
{
|
|
|
|
return this.settings;
|
|
|
|
}
|
2019-01-23 20:34:40 +01:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @return economyProvided variable.
|
|
|
|
*/
|
|
|
|
public boolean isEconomyProvided()
|
|
|
|
{
|
2019-01-26 15:14:43 +01:00
|
|
|
return this.economyProvided;
|
2019-01-23 20:34:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-01-24 22:28:25 +01:00
|
|
|
/**
|
|
|
|
* Returns VaultHook. Used to get easier access to Economy. NEVER USE WITHOUT isEconomyProvided or null
|
|
|
|
* check.
|
|
|
|
* @return VaultHook or null.
|
|
|
|
*/
|
|
|
|
public VaultHook getEconomyProvider()
|
|
|
|
{
|
2019-01-28 19:21:48 +01:00
|
|
|
return vaultHook;
|
2019-01-24 22:28:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-01-23 20:34:40 +01:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @return levelProvided variable.
|
|
|
|
*/
|
|
|
|
public boolean isLevelProvided()
|
|
|
|
{
|
|
|
|
return levelProvided;
|
|
|
|
}
|
2019-01-24 22:28:25 +01:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This method returns Level addon. Used to easier access to Level. NEVER USE WITHOUT isLevelProvided or null
|
|
|
|
* @return LevelAddon or null.
|
|
|
|
*/
|
|
|
|
public Level getLevelAddon()
|
|
|
|
{
|
|
|
|
return levelAddon;
|
|
|
|
}
|
2018-12-30 14:31:26 +01:00
|
|
|
}
|