diff --git a/pom.xml b/pom.xml index 5ea0794..7afb225 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ 4.0.0 world.bentobox - WelcomeWarpSigns + warps 1.2-SNAPSHOT WelcomeWarpSigns diff --git a/src/main/java/world/bentobox/warps/Warp.java b/src/main/java/world/bentobox/warps/Warp.java index 12d2f99..452a314 100644 --- a/src/main/java/world/bentobox/warps/Warp.java +++ b/src/main/java/world/bentobox/warps/Warp.java @@ -1,18 +1,18 @@ package world.bentobox.warps; + +import org.bukkit.World; import java.util.HashSet; import java.util.Set; import java.util.UUID; -import org.bukkit.World; - -import world.bentobox.bentobox.BentoBox; import world.bentobox.bentobox.api.addons.Addon; +import world.bentobox.bentobox.api.configuration.Config; 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.PluginConfig; +import world.bentobox.warps.config.Settings; /** * Addin to BSkyBlock that enables welcome warp signs @@ -20,55 +20,113 @@ import world.bentobox.warps.config.PluginConfig; * */ public class Warp extends Addon { +// --------------------------------------------------------------------- +// Section: Variables +// --------------------------------------------------------------------- - private static final String BSKYBLOCK = "BSkyBlock"; - private static final String ACIDISLAND = "AcidIsland"; + /** + * This variable stores string for Level addon. + */ private static final String LEVEL_ADDON_NAME = "Level"; - // The plugin instance. - private BentoBox plugin; - - // Warp panel objects + /** + * Warp panel Manager + */ private WarpPanelManager warpPanelManager; - // Warps signs objects + /** + * Worlds Sign manager. + */ private WarpSignsManager warpSignsManager; + /** + * This variable stores in which worlds this addon is working. + */ private Set registeredWorlds; - private PluginConfig settings; + /** + * 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. + */ + @Override + public void onLoad() + { + super.onLoad(); + // Save default config.yml + this.saveDefaultConfig(); + // 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() { - // Load the plugin's config - settings = new PluginConfig(this); - // Get the BSkyBlock plugin. This will be available because this plugin depends on it in plugin.yml. - plugin = this.getPlugin(); // Check if it is enabled - it might be loaded, but not enabled. - if (!plugin.isEnabled()) { + if (!this.getPlugin().isEnabled()) { this.setState(State.DISABLED); return; } - registeredWorlds = new HashSet<>(); - // Start warp signs - warpSignsManager = new WarpSignsManager(this, plugin); - warpPanelManager = new WarpPanelManager(this); - // Load the listener - getServer().getPluginManager().registerEvents(new WarpSignsListener(this), plugin); - // Register commands - getPlugin().getAddonsManager().getGameModeAddons().stream() - .filter(a -> a.getDescription().getName().equals(BSKYBLOCK) || a.getDescription().getName().equals(ACIDISLAND)) - .forEach(a -> { - a.getPlayerCommand().ifPresent(c -> { - new WarpCommand(this, c); - new WarpsCommand(this, c); - registeredWorlds.add(c.getWorld()); - }); + 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; + } + } }); - // Done + + 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()); + } } + @Override public void onDisable(){ // Save the warps @@ -76,6 +134,21 @@ public class Warp extends Addon { warpSignsManager.saveWarpList(); } + + /** + * 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); + } + } + + /** * Get warp panel manager * @return @@ -104,7 +177,7 @@ public class Warp extends Addon { /** * @return the settings */ - public PluginConfig getSettings() { + public Settings getSettings() { return settings; } @@ -115,7 +188,7 @@ public class Warp extends Addon { * @return island level or null if there is no level plugin */ public Long getLevel(World world, UUID uniqueId) { - return plugin.getAddonsManager().getAddonByName(LEVEL_ADDON_NAME).map(l -> ((Level) l).getIslandLevel(world, uniqueId)).orElse(null); + return this.getPlugin().getAddonsManager().getAddonByName(LEVEL_ADDON_NAME).map(l -> ((Level) l).getIslandLevel(world, uniqueId)).orElse(null); } } diff --git a/src/main/java/world/bentobox/warps/config/PluginConfig.java b/src/main/java/world/bentobox/warps/config/PluginConfig.java deleted file mode 100644 index ea8541b..0000000 --- a/src/main/java/world/bentobox/warps/config/PluginConfig.java +++ /dev/null @@ -1,24 +0,0 @@ -package world.bentobox.warps.config; - -import world.bentobox.warps.Warp; - -public class PluginConfig { - - private int warpLevelRestriction; - - /** - * Loads the various settings from the config.yml file into the plugin - */ - public PluginConfig(Warp plugin) { - plugin.saveDefaultConfig(); - warpLevelRestriction = plugin.getConfig().getInt("warplevelrestriction",10); - // All done - } - - /** - * @return the warpLevelRestriction - */ - public int getWarpLevelRestriction() { - return warpLevelRestriction; - } -} diff --git a/src/main/java/world/bentobox/warps/config/Settings.java b/src/main/java/world/bentobox/warps/config/Settings.java new file mode 100644 index 0000000..89d2f67 --- /dev/null +++ b/src/main/java/world/bentobox/warps/config/Settings.java @@ -0,0 +1,142 @@ +package world.bentobox.warps.config; + +import java.util.HashSet; +import java.util.Set; + +import world.bentobox.bentobox.api.configuration.ConfigComment; +import world.bentobox.bentobox.api.configuration.ConfigEntry; +import world.bentobox.bentobox.api.configuration.StoreAt; +import world.bentobox.bentobox.database.objects.DataObject; + + +@StoreAt(filename="config.yml", path="addons/WelcomeWarps") +@ConfigComment("WelcomeWarps Configuration [version]") +@ConfigComment("This config file is dynamic and saved when the server is shutdown.") +@ConfigComment("You cannot edit it while the server is running because changes will") +@ConfigComment("be lost! Use in-game settings GUI or edit when server is offline.") +@ConfigComment("") +public class Settings implements DataObject +{ + @ConfigComment("") + @ConfigComment("Warp Restriction - needed levels to be able to create a warp") + @ConfigComment("0 or negative values will disable this restriction 10 is default") + @ConfigEntry(path = "warplevelrestriction") + private int warpLevelRestriction; + + @ConfigComment("") + @ConfigComment("Text that player must put on sign to make it a warp sign") + @ConfigComment("Not case sensitive!") + @ConfigEntry(path = "welcomeLine") + private String welcomeLine; + + @ConfigComment("") + @ConfigComment("This list stores GameModes in which Level addon should not work.") + @ConfigComment("To disable addon it is necessary to write its name in new line that starts with -. Example:") + @ConfigComment("disabled-gamemodes:") + @ConfigComment(" - BSkyBlock") + @ConfigEntry(path = "disabled-gamemodes") + private Set disabledGameModes = new HashSet<>(); + + @ConfigComment("") + private String uniqueId = "config"; + +// --------------------------------------------------------------------- +// Section: Constructor +// --------------------------------------------------------------------- + + + /** + * Loads the various settings from the config.yml file into the plugin + */ + public Settings() + { + // empty constructor + } + + +// --------------------------------------------------------------------- +// Section: Methods +// --------------------------------------------------------------------- + + + /** + * @return the warpLevelRestriction + */ + public int getWarpLevelRestriction() + { + return warpLevelRestriction; + } + + + /** + * @return the uniqueId + */ + @Override + public String getUniqueId() + { + return uniqueId; + } + + + /** + * @param uniqueId - unique ID the uniqueId to set + */ + @Override + public void setUniqueId(String uniqueId) + { + this.uniqueId = uniqueId; + } + + + /** + * This method sets the warpLevelRestriction object value. + * @param warpLevelRestriction the warpLevelRestriction object new value. + * + */ + public void setWarpLevelRestriction(int warpLevelRestriction) + { + this.warpLevelRestriction = warpLevelRestriction; + } + + + /** + * This method returns the welcomeLine object. + * @return the welcomeLine object. + */ + public String getWelcomeLine() + { + return welcomeLine; + } + + + /** + * This method sets the welcomeLine object value. + * @param welcomeLine the welcomeLine object new value. + * + */ + public void setWelcomeLine(String welcomeLine) + { + this.welcomeLine = welcomeLine; + } + + + /** + * This method returns the disabledGameModes object. + * @return the disabledGameModes object. + */ + public Set getDisabledGameModes() + { + return disabledGameModes; + } + + + /** + * This method sets the disabledGameModes object value. + * @param disabledGameModes the disabledGameModes object new value. + * + */ + public void setDisabledGameModes(Set disabledGameModes) + { + this.disabledGameModes = disabledGameModes; + } +} diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index eeed321..3dd16db 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -7,3 +7,8 @@ warplevelrestriction: 10 # Not case sensitive! welcomeLine: [WELCOME] +# This list stores GameModes in which Challenges addon should not work. +# To disable addon it is necessary to write its name in new line that starts with -. Example: +# disabled-gamemodes: +# - BSkyBlock +disabled-gamemodes: [] \ No newline at end of file diff --git a/src/test/java/world/bentobox/warps/WarpSignsListenerTest.java b/src/test/java/world/bentobox/warps/WarpSignsListenerTest.java index 945c869..1f5dd3b 100644 --- a/src/test/java/world/bentobox/warps/WarpSignsListenerTest.java +++ b/src/test/java/world/bentobox/warps/WarpSignsListenerTest.java @@ -1,22 +1,7 @@ package world.bentobox.warps; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; -import java.util.HashMap; -import java.util.Map; -import java.util.Optional; -import java.util.UUID; - -import org.bukkit.Bukkit; -import org.bukkit.ChatColor; -import org.bukkit.Location; -import org.bukkit.Material; -import org.bukkit.World; +import org.bukkit.*; import org.bukkit.block.Block; import org.bukkit.block.Sign; import org.bukkit.configuration.file.FileConfiguration; @@ -33,13 +18,21 @@ import org.mockito.stubbing.Answer; import org.powermock.api.mockito.PowerMockito; import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; +import java.util.HashMap; +import java.util.Map; +import java.util.Optional; +import java.util.UUID; import world.bentobox.bentobox.BentoBox; import world.bentobox.bentobox.api.user.User; import world.bentobox.bentobox.managers.IslandWorldManager; import world.bentobox.bentobox.managers.IslandsManager; import world.bentobox.bentobox.managers.LocalesManager; -import world.bentobox.warps.config.PluginConfig; +import world.bentobox.warps.config.Settings; + +import static org.junit.Assert.*; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; @RunWith(PowerMockRunner.class) @PrepareForTest({Bukkit.class}) @@ -55,7 +48,7 @@ public class WarpSignsListenerTest { private UUID uuid; private String[] lines; private FileConfiguration config; - private PluginConfig settings; + private Settings settings; private IslandsManager im; @Before @@ -110,7 +103,7 @@ public class WarpSignsListenerTest { // Lines lines = new String[] {"[WELCOME]", "line2", "line3", "line4"}; - settings = mock(PluginConfig.class); + settings = mock(Settings.class); when(settings.getWarpLevelRestriction()).thenReturn(10); when(addon.getSettings()).thenReturn(settings);