2018-10-31 14:32:21 +01:00
|
|
|
package world.bentobox.warps;
|
2017-10-26 03:33:23 +02:00
|
|
|
|
2019-01-31 19:25:32 +01:00
|
|
|
|
2018-05-29 00:42:03 +02:00
|
|
|
import java.util.HashSet;
|
2019-08-22 21:53:38 +02:00
|
|
|
import java.util.Map;
|
2018-05-29 00:42:03 +02:00
|
|
|
import java.util.Set;
|
2018-09-28 06:01:46 +02:00
|
|
|
import java.util.UUID;
|
2018-05-29 00:42:03 +02:00
|
|
|
|
2019-08-22 21:53:38 +02:00
|
|
|
import org.bukkit.Bukkit;
|
2019-04-30 08:23:54 +02:00
|
|
|
import org.bukkit.World;
|
|
|
|
|
2019-01-16 21:46:39 +01:00
|
|
|
import world.bentobox.bentobox.api.addons.Addon;
|
2019-01-31 19:25:32 +01:00
|
|
|
import world.bentobox.bentobox.api.configuration.Config;
|
2019-01-16 21:46:39 +01:00
|
|
|
import world.bentobox.bentobox.util.Util;
|
2018-10-31 14:32:21 +01:00
|
|
|
import world.bentobox.level.Level;
|
|
|
|
import world.bentobox.warps.commands.WarpCommand;
|
|
|
|
import world.bentobox.warps.commands.WarpsCommand;
|
2019-01-31 19:25:32 +01:00
|
|
|
import world.bentobox.warps.config.Settings;
|
2017-10-26 03:33:23 +02:00
|
|
|
|
|
|
|
/**
|
2019-10-31 22:22:32 +01:00
|
|
|
* Addin to BentoBox that enables welcome warp signs
|
2017-10-26 03:33:23 +02:00
|
|
|
* @author tastybento
|
|
|
|
*
|
|
|
|
*/
|
2017-12-31 06:15:47 +01:00
|
|
|
public class Warp extends Addon {
|
2019-04-30 08:23:54 +02:00
|
|
|
// ---------------------------------------------------------------------
|
|
|
|
// Section: Variables
|
|
|
|
// ---------------------------------------------------------------------
|
2017-10-26 03:33:23 +02:00
|
|
|
|
2019-01-31 19:25:32 +01:00
|
|
|
/**
|
|
|
|
* This variable stores string for Level addon.
|
|
|
|
*/
|
2018-10-31 14:32:21 +01:00
|
|
|
private static final String LEVEL_ADDON_NAME = "Level";
|
2018-01-01 20:49:02 +01:00
|
|
|
|
2019-10-31 22:22:32 +01:00
|
|
|
/**
|
|
|
|
* Permission prefix for non-game world operation
|
|
|
|
*/
|
|
|
|
public static final String WELCOME_WARP_SIGNS = "welcomewarpsigns";
|
|
|
|
|
2019-01-31 19:25:32 +01:00
|
|
|
/**
|
|
|
|
* Warp panel Manager
|
|
|
|
*/
|
2017-12-31 06:15:47 +01:00
|
|
|
private WarpPanelManager warpPanelManager;
|
2017-10-26 03:33:23 +02:00
|
|
|
|
2019-01-31 19:25:32 +01:00
|
|
|
/**
|
|
|
|
* Worlds Sign manager.
|
|
|
|
*/
|
2017-12-31 06:15:47 +01:00
|
|
|
private WarpSignsManager warpSignsManager;
|
2017-10-26 03:33:23 +02:00
|
|
|
|
2019-01-31 19:25:32 +01:00
|
|
|
/**
|
|
|
|
* This variable stores in which worlds this addon is working.
|
|
|
|
*/
|
2018-05-29 00:42:03 +02:00
|
|
|
private Set<World> registeredWorlds;
|
|
|
|
|
2019-01-31 19:25:32 +01:00
|
|
|
/**
|
|
|
|
* This variable stores if addon settings.
|
|
|
|
*/
|
|
|
|
private Settings settings;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This variable stores if addon is hooked or not.
|
|
|
|
*/
|
|
|
|
private boolean hooked;
|
|
|
|
|
2019-09-14 03:13:03 +02:00
|
|
|
/**
|
|
|
|
* Settings config object
|
|
|
|
*/
|
|
|
|
private Config<Settings> settingsConfig;
|
|
|
|
|
2019-04-30 08:23:54 +02:00
|
|
|
// ---------------------------------------------------------------------
|
|
|
|
// Section: Methods
|
|
|
|
// ---------------------------------------------------------------------
|
2019-01-31 19:25:32 +01:00
|
|
|
|
2018-08-01 04:48:13 +02:00
|
|
|
|
2019-01-31 19:25:32 +01:00
|
|
|
/**
|
|
|
|
* 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
|
2019-01-31 19:25:32 +01:00
|
|
|
public void onLoad()
|
|
|
|
{
|
|
|
|
super.onLoad();
|
|
|
|
// Save default config.yml
|
|
|
|
this.saveDefaultConfig();
|
2017-10-26 03:33:23 +02:00
|
|
|
// Load the plugin's config
|
2019-10-31 18:09:33 +01:00
|
|
|
if (this.loadSettings()) {
|
|
|
|
// Load the master warp and warps command
|
|
|
|
new WarpCommand(this);
|
|
|
|
new WarpsCommand(this);
|
|
|
|
}
|
2019-01-31 19:25:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
2019-01-31 19:25:32 +01:00
|
|
|
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;
|
|
|
|
}
|
2019-01-31 19:25:32 +01:00
|
|
|
|
2018-08-18 06:58:56 +02:00
|
|
|
registeredWorlds = new HashSet<>();
|
|
|
|
|
2019-01-31 19:25:32 +01:00
|
|
|
// Register commands
|
|
|
|
this.getPlugin().getAddonsManager().getGameModeAddons().forEach(gameModeAddon -> {
|
2019-10-31 22:12:44 +01:00
|
|
|
if (!this.settings.getDisabledGameModes().contains(gameModeAddon.getDescription().getName())
|
|
|
|
&& gameModeAddon.getPlayerCommand().isPresent())
|
2019-01-31 19:25:32 +01:00
|
|
|
{
|
2019-10-31 22:12:44 +01:00
|
|
|
this.registeredWorlds.add(gameModeAddon.getOverWorld());
|
|
|
|
|
|
|
|
new WarpCommand(this, gameModeAddon.getPlayerCommand().get());
|
|
|
|
new WarpsCommand(this, gameModeAddon.getPlayerCommand().get());
|
|
|
|
this.hooked = true;
|
2019-01-31 19:25:32 +01:00
|
|
|
}
|
2017-10-27 00:58:58 +02:00
|
|
|
});
|
2019-01-31 19:25:32 +01:00
|
|
|
|
|
|
|
if (hooked)
|
|
|
|
{
|
|
|
|
// Start warp signs
|
|
|
|
warpSignsManager = new WarpSignsManager(this, this.getPlugin());
|
|
|
|
warpPanelManager = new WarpPanelManager(this);
|
|
|
|
// Load the listener
|
2019-09-16 01:41:17 +02:00
|
|
|
this.registerListener(new WarpSignsListener(this));
|
2019-01-31 19:25:32 +01:00
|
|
|
}
|
2017-10-26 03:33:23 +02:00
|
|
|
}
|
|
|
|
|
2019-01-31 19:25:32 +01:00
|
|
|
|
2017-10-26 03:33:23 +02:00
|
|
|
@Override
|
|
|
|
public void onDisable(){
|
|
|
|
// Save the warps
|
2017-12-31 06:15:47 +01:00
|
|
|
if (warpSignsManager != null)
|
|
|
|
warpSignsManager.saveWarpList();
|
2017-10-26 03:33:23 +02:00
|
|
|
}
|
|
|
|
|
2019-01-31 19:25:32 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This method loads addon configuration settings in memory.
|
|
|
|
*/
|
2019-10-31 18:09:33 +01:00
|
|
|
private boolean loadSettings() {
|
2019-09-14 03:13:03 +02:00
|
|
|
if (settingsConfig == null) {
|
|
|
|
settingsConfig = new Config<>(this, Settings.class);
|
|
|
|
}
|
|
|
|
this.settings = settingsConfig.loadConfigObject();
|
2019-01-31 19:25:32 +01:00
|
|
|
if (this.settings == null) {
|
|
|
|
// Disable
|
|
|
|
this.logError("WelcomeWarp settings could not load! Addon disabled.");
|
|
|
|
this.setState(State.DISABLED);
|
2019-10-31 18:09:33 +01:00
|
|
|
return false;
|
2019-01-31 19:25:32 +01:00
|
|
|
}
|
2019-09-19 01:48:38 +02:00
|
|
|
settingsConfig.saveConfigObject(settings);
|
2019-10-31 18:09:33 +01:00
|
|
|
return true;
|
2019-01-31 19:25:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-10-26 03:33:23 +02:00
|
|
|
/**
|
2017-12-31 06:15:47 +01:00
|
|
|
* Get warp panel manager
|
|
|
|
* @return
|
2017-10-26 03:33:23 +02:00
|
|
|
*/
|
2017-12-31 06:15:47 +01:00
|
|
|
public WarpPanelManager getWarpPanelManager() {
|
|
|
|
return warpPanelManager;
|
2017-10-26 03:33:23 +02:00
|
|
|
}
|
2018-01-01 20:49:02 +01:00
|
|
|
|
2017-12-31 06:15:47 +01: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) {
|
2018-09-01 21:55:55 +02:00
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
2018-08-01 04:48:13 +02:00
|
|
|
/**
|
|
|
|
* @return the settings
|
|
|
|
*/
|
2019-01-31 19:25:32 +01:00
|
|
|
public Settings getSettings() {
|
2018-08-01 04:48:13 +02:00
|
|
|
return settings;
|
|
|
|
}
|
|
|
|
|
2018-09-28 06:01:46 +02:00
|
|
|
/**
|
|
|
|
* 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) {
|
2019-01-31 19:25:32 +01:00
|
|
|
return this.getPlugin().getAddonsManager().getAddonByName(LEVEL_ADDON_NAME).map(l -> ((Level) l).getIslandLevel(world, uniqueId)).orElse(null);
|
2018-09-28 06:01:46 +02:00
|
|
|
}
|
|
|
|
|
2019-08-22 21:53:38 +02:00
|
|
|
/* (non-Javadoc)
|
|
|
|
* @see world.bentobox.bentobox.api.addons.Addon#request(java.lang.String, java.util.Map)
|
|
|
|
*
|
|
|
|
* This API enables plugins to request data from the WarpSignsManager
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public Object request(String requestLabel, Map<String, Object> metaData) {
|
|
|
|
if (metaData.isEmpty()) return null;
|
|
|
|
World world = null;
|
|
|
|
UUID uuid = null;
|
|
|
|
// Parse keys
|
|
|
|
if (metaData.containsKey("world")) {
|
|
|
|
world = Bukkit.getWorld((String)metaData.get("world"));
|
|
|
|
}
|
2019-10-31 22:22:32 +01:00
|
|
|
if (world == null) return null;
|
2019-08-22 21:53:38 +02:00
|
|
|
if (metaData.containsKey("uuid")) {
|
|
|
|
try {
|
|
|
|
uuid = UUID.fromString((String)metaData.get("uuid"));
|
|
|
|
} catch (Exception e) {
|
|
|
|
logError("Requested UUID is invalid");
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
switch(requestLabel) {
|
|
|
|
case "getSortedWarps":
|
|
|
|
return getWarpSignsManager().getSortedWarps(world);
|
|
|
|
case "getWarp":
|
2019-10-31 22:22:32 +01:00
|
|
|
return uuid == null ? null : getWarpSignsManager().getWarp(world, uuid);
|
2019-08-22 21:53:38 +02:00
|
|
|
case "getWarpMap":
|
|
|
|
return getWarpSignsManager().getWarpMap(world);
|
|
|
|
case "hasWarp":
|
2019-10-31 22:22:32 +01:00
|
|
|
return uuid == null ? null : getWarpSignsManager().hasWarp(world, uuid);
|
2019-08-22 21:53:38 +02:00
|
|
|
case "listWarps":
|
|
|
|
return getWarpSignsManager().listWarps(world);
|
|
|
|
default:
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-10-26 03:33:23 +02:00
|
|
|
}
|