1
0
mirror of https://github.com/BentoBoxWorld/Warps.git synced 2024-11-15 23:25:27 +01:00
addon-welcomewarpsigns/src/main/java/world/bentobox/warps/Warp.java

196 lines
5.1 KiB
Java
Raw Normal View History

package world.bentobox.warps;
2017-10-26 03:33:23 +02:00
2018-05-29 00:42:03 +02:00
import java.util.HashSet;
import java.util.Set;
import java.util.UUID;
2018-05-29 00:42:03 +02:00
import org.bukkit.World;
2019-01-16 21:46:39 +01:00
import world.bentobox.bentobox.api.addons.Addon;
import world.bentobox.bentobox.api.configuration.Config;
2019-01-16 21:46:39 +01:00
import world.bentobox.bentobox.util.Util;
import world.bentobox.level.Level;
import world.bentobox.warps.commands.WarpCommand;
import world.bentobox.warps.commands.WarpsCommand;
import world.bentobox.warps.config.Settings;
2017-10-26 03:33:23 +02:00
/**
* Addin to BSkyBlock that enables welcome warp signs
* @author tastybento
*
*/
public class Warp extends Addon {
// ---------------------------------------------------------------------
// Section: Variables
// ---------------------------------------------------------------------
2017-10-26 03:33:23 +02:00
/**
* This variable stores string for Level addon.
*/
private static final String LEVEL_ADDON_NAME = "Level";
/**
* Warp panel Manager
*/
private WarpPanelManager warpPanelManager;
2017-10-26 03:33:23 +02:00
/**
* Worlds Sign manager.
*/
private WarpSignsManager warpSignsManager;
2017-10-26 03:33:23 +02:00
/**
* This variable stores in which worlds this addon is working.
*/
2018-05-29 00:42:03 +02:00
private Set<World> registeredWorlds;
/**
* This variable stores if addon settings.
*/
private Settings settings;
/**
* This variable stores if addon is hooked or not.
*/
private boolean hooked;
// ---------------------------------------------------------------------
// Section: Methods
// ---------------------------------------------------------------------
/**
* Executes code when loading the addon. This is called before {@link #onEnable()}. This should preferably
* be used to setup configuration and worlds.
*/
2017-10-26 03:33:23 +02:00
@Override
public void onLoad()
{
super.onLoad();
// Save default config.yml
this.saveDefaultConfig();
2017-10-26 03:33:23 +02:00
// Load the plugin's config
this.loadSettings();
}
/**
* Executes code when reloading the addon.
*/
@Override
public void onReload()
{
super.onReload();
if (this.hooked) {
this.warpSignsManager.saveWarpList();
this.loadSettings();
this.getLogger().info("WelcomeWarp addon reloaded.");
}
}
@Override
public void onEnable() {
2017-10-26 03:33:23 +02:00
// Check if it is enabled - it might be loaded, but not enabled.
if (!this.getPlugin().isEnabled()) {
2018-10-31 02:12:30 +01:00
this.setState(State.DISABLED);
2017-10-26 03:33:23 +02:00
return;
}
registeredWorlds = new HashSet<>();
// Register commands
this.getPlugin().getAddonsManager().getGameModeAddons().forEach(gameModeAddon -> {
if (!this.settings.getDisabledGameModes().contains(gameModeAddon.getDescription().getName()))
{
if (gameModeAddon.getPlayerCommand().isPresent())
{
this.registeredWorlds.add(gameModeAddon.getOverWorld());
new WarpCommand(this, gameModeAddon.getPlayerCommand().get());
new WarpsCommand(this, gameModeAddon.getPlayerCommand().get());
this.hooked = true;
}
}
2017-10-27 00:58:58 +02:00
});
if (hooked)
{
// Start warp signs
warpSignsManager = new WarpSignsManager(this, this.getPlugin());
warpPanelManager = new WarpPanelManager(this);
// Load the listener
getServer().getPluginManager().registerEvents(new WarpSignsListener(this), this.getPlugin());
}
2017-10-26 03:33:23 +02:00
}
2017-10-26 03:33:23 +02:00
@Override
public void onDisable(){
// Save the warps
if (warpSignsManager != null)
warpSignsManager.saveWarpList();
2017-10-26 03:33:23 +02: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("WelcomeWarp settings could not load! Addon disabled.");
this.setState(State.DISABLED);
}
}
2017-10-26 03:33:23 +02:00
/**
* Get warp panel manager
* @return
2017-10-26 03:33:23 +02:00
*/
public WarpPanelManager getWarpPanelManager() {
return warpPanelManager;
2017-10-26 03:33:23 +02:00
}
public WarpSignsManager getWarpSignsManager() {
return warpSignsManager;
2017-10-26 03:33:23 +02:00
}
2018-05-29 00:42:03 +02:00
public String getPermPrefix(World world) {
return this.getPlugin().getIWM().getPermissionPrefix(world);
2018-05-29 00:42:03 +02:00
}
/**
* Check if an event is in a registered world
* @param world - world to check
* @return true if it is
*/
public boolean inRegisteredWorld(World world) {
return registeredWorlds.contains(Util.getWorld(world));
}
/**
* @return the settings
*/
public Settings getSettings() {
return settings;
}
/**
* Get the island level
* @param world - world
* @param uniqueId - player's UUID
* @return island level or null if there is no level plugin
*/
public Long getLevel(World world, UUID uniqueId) {
return this.getPlugin().getAddonsManager().getAddonByName(LEVEL_ADDON_NAME).map(l -> ((Level) l).getIslandLevel(world, uniqueId)).orElse(null);
}
2017-10-26 03:33:23 +02:00
}