diff --git a/.github/ISSUE_TEMPLATE/config.yml b/.github/ISSUE_TEMPLATE/config.yml new file mode 100644 index 00000000..7beb1efd --- /dev/null +++ b/.github/ISSUE_TEMPLATE/config.yml @@ -0,0 +1,5 @@ +blank_issues_enabled: false +contact_links: + - name: 💬 ​Multiverse Discord Server + url: https://discord.gg/NZtfKky + about: Need help with using Multiverse-Core or just want to chat with the devs? Join the Multiverse Discord Server for help! diff --git a/.github/ISSUE_TEMPLATE/help.md b/.github/ISSUE_TEMPLATE/help.md new file mode 100644 index 00000000..65c4cff0 --- /dev/null +++ b/.github/ISSUE_TEMPLATE/help.md @@ -0,0 +1,56 @@ +--- +name: ❓ Help! +about: Encountered a problem with Multiverse-Core? Not sure how to fix it? +title: '' +labels: 'Type: Assistance' +assignees: '' + +--- + + + +### Information + +* **Server version:** + +* **Full output of `/mv version -p`:** + +* **Server log:** + +### Help request + +**Problem** + + +**What I have tried** + + +**Screenshots** + \ No newline at end of file diff --git a/.github/ISSUE_TEMPLATE/report-a-bug.md b/.github/ISSUE_TEMPLATE/report-a-bug.md new file mode 100644 index 00000000..6ec78848 --- /dev/null +++ b/.github/ISSUE_TEMPLATE/report-a-bug.md @@ -0,0 +1,59 @@ +--- +name: 🐛 Report a Bug +about: Report a Multiverse-Core bug. Only use this if you're 100% sure it's something wrong with Multiverse-Core - otherwise, try "Help!". +title: '' +labels: 'Bug: Unconfirmed' +assignees: '' + +--- + + + +### Information + +* **Server version:** + +* **Full output of `/mv version -p`:** + +* **Server log:** + +### Details +I was **``** to reproduce my issue on a freshly setup and up-to-date server with the latest version of Multiverse plugins with no other plugins and with no kinds of other server or client mods. + +**Description** + + +**Steps to reproduce** + + +**Expected behavior** + + +**Screenshots** + diff --git a/.github/ISSUE_TEMPLATE/request-a-feature.md b/.github/ISSUE_TEMPLATE/request-a-feature.md new file mode 100644 index 00000000..4c6eb236 --- /dev/null +++ b/.github/ISSUE_TEMPLATE/request-a-feature.md @@ -0,0 +1,54 @@ +--- +name: 💡 Request a Feature +about: Suggest a feature you want to see in Multiverse-Core! +title: '' +labels: 'Type: Idea' +assignees: '' + +--- + + + +### Feature request + +**Feature description** + + +**How the feature is useful** + \ No newline at end of file diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml new file mode 100644 index 00000000..983d12cc --- /dev/null +++ b/.github/workflows/build.yml @@ -0,0 +1,22 @@ +name: Maven CI/CD + +on: + push: + branches: [ main ] + pull_request: + branches: [ main ] + +jobs: + build_and_test: + + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v2 + - name: Set up JDK 8 + uses: actions/setup-java@v1 + with: + java-version: 1.8 + + - name: Build with Maven + run: mvn -B package --file pom.xml diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index 1ecfa2d1..00000000 --- a/.travis.yml +++ /dev/null @@ -1,7 +0,0 @@ -language: java -jdk: - - oraclejdk8 -notifications: - email: false -before_install: - - sed -i.bak -e 's|https://nexus.codehaus.org/snapshots/|https://oss.sonatype.org/content/repositories/codehaus-snapshots/|g' ~/.m2/settings.xml diff --git a/LICENSE.md b/LICENSE.md new file mode 100644 index 00000000..a5dc3a8b --- /dev/null +++ b/LICENSE.md @@ -0,0 +1,6 @@ +Copyright (c) 2011, The Multiverse Team All rights reserved. + +Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: + +Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. Neither the name of the The Multiverse Team nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + diff --git a/README.md b/README.md index 3d4af1fc..171065b1 100644 --- a/README.md +++ b/README.md @@ -1,11 +1,43 @@ +![Multiverse 2](config/multiverse2-long.png) + +[![Build Status](https://img.shields.io/travis/Multiverse/Multiverse-Core?label=status&logo=travis)](https://travis-ci.org/Multiverse/Multiverse-Core) +[![Release](https://img.shields.io/nexus/r/com.onarandombox.multiversecore/Multiverse-Core?label=release&server=https%3A%2F%2Frepo.onarandombox.com%2F)](https://dev.bukkit.org/projects/multiverse-core) +[![Dev builds](https://img.shields.io/nexus/s/com.onarandombox.multiversecore/Multiverse-Core?label=dev%20builds&server=http%3A%2F%2Frepo.onarandombox.com%2F)](https://ci.onarandombox.com/job/Multiverse-Core/) +[![Discord](https://img.shields.io/discord/325459248047980545?label=discord&logo=discord)](https://discord.gg/NZtfKky) +[![Support me on Patreon](https://img.shields.io/endpoint.svg?url=https%3A%2F%2Fshieldsio-patreon.vercel.app%2Fapi%3Fusername%3Ddumptruckman%26type%3Dpatrons&style=flat)](https://patreon.com/dumptruckman) + +About +======== +[Multiverse](https://dev.bukkit.org/projects/multiverse-core) was created at the dawn of Bukkit multiworld support. It has since then grown into a **complete world management solution!** Multiverse provides the easiest to use world management solution for your Minecraft server, big or small, and with great addons like [Portals](https://dev.bukkit.org/projects/multiverse-portals) and [NetherPortals](https://dev.bukkit.org/projects/multiverse-netherportals/), what's not to love! +

+Now it's time to create your very own Multiverse server, do check out our [Wiki](https://github.com/Multiverse/Multiverse-Core/wiki) and [Usage Guide](https://github.com/Multiverse/Multiverse-Core/wiki/Basics) to get started. Feel free to hop onto our [Discord](https://discord.gg/NZtfKky) if you have any question or just want to have a chat with us! + +### Amazing sub-modules available: +* [Multiverse-NetherPortals](https://github.com/Multiverse/Multiverse-NetherPortals) -> Have separate nether and end worlds for each of your overworlds! +* [Multiverse-Portals](https://github.com/Multiverse/Multiverse-Portals) -> Make custom portals to go to any destination! +* [Multiverse-Inventories](https://github.com/Multiverse/Multiverse-Inventories) -> Have separated players stats and inventories per world or per group of worlds. +* [Multiverse-SignPortals](https://github.com/Multiverse/Multiverse-SignPortals) -> Signs as teleprompters! + Building ======== Simply build the source with maven: - - $ mvn install - +``` +$ mvn install +``` More details are available on the [build instructions wiki page](https://github.com/Multiverse/Multiverse-Core/wiki/Building). + +Contributing +======= +**Want to help improve Multiverse?** There are several ways you can support and contribute to the project. +* Take a look at our "Bug: Unconfirmed" issues, where you can find issues that need extra testing and investigation. +* Want others to love Multiverse too? You can join the [Multiverse Discord community](https://discord.gg/NZtfKky) and help others with issues and setup! +* A Multiverse guru? You can update our [Wiki](https://github.com/Multiverse/Multiverse-Core/wiki) with your latest tip, tricks and guides! The wiki open for all to edit and improve. +* Love coding? You could look at ["State: Open to PR"](https://github.com/Multiverse/Multiverse-Core/labels/State%3A%20Open%20to%20PR) and ["Resolution: Accepted"](https://github.com/Multiverse/Multiverse-Core/labels/Resolution%3A%20Accepted) issues. We're always happy to receive bug fixes and feature additions as [pull requests](https://www.freecodecamp.org/news/how-to-make-your-first-pull-request-on-github-3/). +* If you'd like to make a financial contribution to the project, do consider joining our [patreon](https://www.patreon.com/dumptruckman) or make a one-time donation [here](https://paypal.me/dumptruckman)! + +Additionally, we would like to give a big thanks to everyone that has supported Multiverse over the years, as well as those in the years to come. Thank you! + License ======= Copyright (c) 2011, The Multiverse Team All rights reserved. @@ -13,4 +45,3 @@ Copyright (c) 2011, The Multiverse Team All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. Neither the name of the The Multiverse Team nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - diff --git a/circle.yml b/circle.yml deleted file mode 100644 index 593dfcb2..00000000 --- a/circle.yml +++ /dev/null @@ -1,3 +0,0 @@ -machine: - java: - version: oraclejdk8 \ No newline at end of file diff --git a/pom.xml b/pom.xml index 132cf18b..b4a15e23 100644 --- a/pom.xml +++ b/pom.xml @@ -3,12 +3,13 @@ 4.0.0 com.onarandombox.multiversecore Multiverse-Core - 4.1.1-SNAPSHOT + 4.3.1-SNAPSHOT Multiverse-Core World Management Plugin UTF-8 UNKNOWN + bitly-access-token @@ -26,18 +27,13 @@ https://hub.spigotmc.org/nexus/content/groups/public/ - vault-repo - https://nexus.hc.to/content/repositories/pub_releases + jitpack.io + https://jitpack.io minebench-repo https://repo.minebench.de/ - - - elmakers-repo - https://maven.elmakers.com/repository/ - @@ -53,7 +49,7 @@ jenkins - http://ci.onarandombox.com + https://ci.onarandombox.com @@ -124,8 +154,6 @@ maven-surefire-plugin 3.0.0-M3 - methods - 10 **/TestCommandSender.java **/TestInstanceCreator.java @@ -181,7 +209,7 @@ org.apache.maven.plugins maven-shade-plugin - 3.1.1 + 3.2.4 package @@ -189,6 +217,7 @@ shade + true me.main__.util @@ -203,8 +232,8 @@ com.onarandombox.buscript - org.mcstats - com.onarandombox.mcstats + org.bstats + com.onarandombox.bstats com.dumptruckman.minecraft.util.Logging @@ -230,13 +259,19 @@ se.eris notnull-instrumenter-maven-plugin - 0.6.8 + 1.0.0 instrument tests-instrument + + + org.jetbrains.annotations.NotNull + javax.validation.constraints.NotNull + + @@ -265,7 +300,7 @@ - net.milkbowl.vault + com.github.MilkBowl VaultAPI 1.7 provided @@ -283,15 +318,9 @@ 2.0-SNAPSHOT - org.mcstats.bukkit - metrics - R8-SNAPSHOT - - - org.bukkit - bukkit - - + org.bstats + bstats-bukkit + 2.2.1 com.dumptruckman.minecraft @@ -317,30 +346,9 @@ test - org.powermock - powermock-module-junit4 - 2.0.0 - jar - test - - - org.powermock - powermock-api-easymock - 2.0.0 - jar - test - - - org.powermock - powermock-api-mockito2 - 2.0.0 - jar - test - - - org.easymock - easymock - 4.0.2 + org.mockito + mockito-core + 3.11.2 test diff --git a/src/main/java/com/onarandombox/MultiverseCore/MVWorld.java b/src/main/java/com/onarandombox/MultiverseCore/MVWorld.java index d8f64e97..d7367ad2 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/MVWorld.java +++ b/src/main/java/com/onarandombox/MultiverseCore/MVWorld.java @@ -214,7 +214,7 @@ public class MVWorld implements MultiverseWorld { public Double validateChange(String property, Double newValue, Double oldValue, MVWorld object) throws ChangeDeniedException { if (newValue <= 0) { - plugin.log(Level.FINE, "Someone tried to set a scale <= 0, aborting!"); + Logging.fine("Someone tried to set a scale <= 0, aborting!"); throw new ChangeDeniedException(); } return super.validateChange(property, newValue, oldValue, object); @@ -297,7 +297,7 @@ public class MVWorld implements MultiverseWorld { public GameMode validateChange(String property, GameMode newValue, GameMode oldValue, MVWorld object) throws ChangeDeniedException { for (Player p : plugin.getServer().getWorld(getName()).getPlayers()) { - plugin.log(Level.FINER, String.format("Setting %s's GameMode to %s", + Logging.finer(String.format("Setting %s's GameMode to %s", p.getName(), newValue.toString())); plugin.getPlayerListener().handleGameModeAndFlight(p, MVWorld.this); } @@ -319,16 +319,16 @@ public class MVWorld implements MultiverseWorld { // verify that the location is safe if (!bs.playerCanSpawnHereSafely(newValue)) { // it's not ==> find a better one! - plugin.log(Level.WARNING, String.format("Somebody tried to set the spawn location for '%s' to an unsafe value! Adjusting...", getAlias())); - plugin.log(Level.WARNING, "Old Location: " + plugin.getLocationManipulation().strCoordsRaw(oldValue)); - plugin.log(Level.WARNING, "New (unsafe) Location: " + plugin.getLocationManipulation().strCoordsRaw(newValue)); + Logging.warning(String.format("Somebody tried to set the spawn location for '%s' to an unsafe value! Adjusting...", getAlias())); + Logging.warning("Old Location: " + plugin.getLocationManipulation().strCoordsRaw(oldValue)); + Logging.warning("New (unsafe) Location: " + plugin.getLocationManipulation().strCoordsRaw(newValue)); SafeTTeleporter teleporter = plugin.getSafeTTeleporter(); newValue = teleporter.getSafeLocation(newValue, SPAWN_LOCATION_SEARCH_TOLERANCE, SPAWN_LOCATION_SEARCH_RADIUS); if (newValue == null) { - plugin.log(Level.WARNING, "Couldn't fix the location. I have to abort the spawn location-change :/"); + Logging.warning("Couldn't fix the location. I have to abort the spawn location-change :/"); throw new ChangeDeniedException(); } - plugin.log(Level.WARNING, "New (safe) Location: " + plugin.getLocationManipulation().strCoordsRaw(newValue)); + Logging.warning("New (safe) Location: " + plugin.getLocationManipulation().strCoordsRaw(newValue)); } } return super.validateChange(property, newValue, oldValue, object); @@ -411,7 +411,7 @@ public class MVWorld implements MultiverseWorld { // Add limit bypass to it's parent this.limitbypassperm.addParent("mv.bypass.playerlimit.*", true); } catch (IllegalArgumentException e) { - this.plugin.log(Level.FINER, "Permissions nodes were already added for " + this.name); + Logging.finer("Permissions nodes were already added for " + this.name); } } @@ -422,16 +422,16 @@ public class MVWorld implements MultiverseWorld { // Verify that location was safe if (!bs.playerCanSpawnHereSafely(location)) { if (!this.getAdjustSpawn()) { - this.plugin.log(Level.FINE, "Spawn location from world.dat file was unsafe!!"); - this.plugin.log(Level.FINE, "NOT adjusting spawn for '" + this.getAlias() + "' because you told me not to."); - this.plugin.log(Level.FINE, "To turn on spawn adjustment for this world simply type:"); - this.plugin.log(Level.FINE, "/mvm set adjustspawn true " + this.getAlias()); + Logging.fine("Spawn location from world.dat file was unsafe!!"); + Logging.fine("NOT adjusting spawn for '" + this.getAlias() + "' because you told me not to."); + Logging.fine("To turn on spawn adjustment for this world simply type:"); + Logging.fine("/mvm set adjustspawn true " + this.getAlias()); return location; } // If it's not, find a better one. SafeTTeleporter teleporter = this.plugin.getSafeTTeleporter(); - this.plugin.log(Level.WARNING, "Spawn location from world.dat file was unsafe. Adjusting..."); - this.plugin.log(Level.WARNING, "Original Location: " + plugin.getLocationManipulation().strCoordsRaw(location)); + Logging.warning("Spawn location from world.dat file was unsafe. Adjusting..."); + Logging.warning("Original Location: " + plugin.getLocationManipulation().strCoordsRaw(location)); Location newSpawn = teleporter.getSafeLocation(location, SPAWN_LOCATION_SEARCH_TOLERANCE, SPAWN_LOCATION_SEARCH_RADIUS); // I think we could also do this, as I think this is what Notch does. @@ -450,7 +450,7 @@ public class MVWorld implements MultiverseWorld { this.getName(), plugin.getLocationManipulation().locationToString(newerSpawn)); return newerSpawn; } else { - this.plugin.log(Level.SEVERE, "Safe spawn NOT found!!!"); + Logging.severe("Safe spawn NOT found!!!"); } } } diff --git a/src/main/java/com/onarandombox/MultiverseCore/MultiverseCore.java b/src/main/java/com/onarandombox/MultiverseCore/MultiverseCore.java index dbe1ca56..bc39f40e 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/MultiverseCore.java +++ b/src/main/java/com/onarandombox/MultiverseCore/MultiverseCore.java @@ -17,11 +17,9 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.HashMap; -import java.util.HashSet; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; -import java.util.Set; import java.util.logging.Level; import buscript.Buscript; @@ -34,7 +32,6 @@ import com.onarandombox.MultiverseCore.api.MVPlugin; import com.onarandombox.MultiverseCore.api.MVWorldManager; import com.onarandombox.MultiverseCore.api.MultiverseCoreConfig; import com.onarandombox.MultiverseCore.api.MultiverseMessaging; -import com.onarandombox.MultiverseCore.api.MultiverseWorld; import com.onarandombox.MultiverseCore.api.SafeTTeleporter; import com.onarandombox.MultiverseCore.commands.AnchorCommand; import com.onarandombox.MultiverseCore.commands.CheckCommand; @@ -71,6 +68,7 @@ import com.onarandombox.MultiverseCore.commands.TeleportCommand; import com.onarandombox.MultiverseCore.commands.UnloadCommand; import com.onarandombox.MultiverseCore.commands.VersionCommand; import com.onarandombox.MultiverseCore.commands.WhoCommand; +import com.onarandombox.MultiverseCore.commandtools.queue.CommandQueueManager; import com.onarandombox.MultiverseCore.destination.AnchorDestination; import com.onarandombox.MultiverseCore.destination.BedDestination; import com.onarandombox.MultiverseCore.destination.CannonDestination; @@ -91,11 +89,14 @@ import com.onarandombox.MultiverseCore.listeners.MVWeatherListener; import com.onarandombox.MultiverseCore.listeners.MVWorldInitListener; import com.onarandombox.MultiverseCore.listeners.MVWorldListener; import com.onarandombox.MultiverseCore.utils.AnchorManager; +import com.onarandombox.MultiverseCore.utils.CompatibilityLayer; import com.onarandombox.MultiverseCore.utils.MVEconomist; import com.onarandombox.MultiverseCore.utils.MVMessaging; import com.onarandombox.MultiverseCore.utils.MVPermissions; import com.onarandombox.MultiverseCore.utils.MVPlayerSession; import com.onarandombox.MultiverseCore.utils.MaterialConverter; +import com.onarandombox.MultiverseCore.utils.TestingMode; +import com.onarandombox.MultiverseCore.utils.metrics.MetricsConfigurator; import com.onarandombox.MultiverseCore.utils.SimpleBlockSafety; import com.onarandombox.MultiverseCore.utils.SimpleLocationManipulation; import com.onarandombox.MultiverseCore.utils.SimpleSafeTTeleporter; @@ -109,7 +110,6 @@ import org.bukkit.ChatColor; import org.bukkit.Difficulty; import org.bukkit.GameMode; import org.bukkit.Location; -import org.bukkit.World.Environment; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; import org.bukkit.configuration.Configuration; @@ -122,7 +122,6 @@ import org.bukkit.plugin.PluginDescriptionFile; import org.bukkit.plugin.PluginManager; import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPluginLoader; -import org.mcstats.Metrics; /** * The implementation of the Multiverse-{@link Core}. @@ -206,6 +205,7 @@ public class MultiverseCore extends JavaPlugin implements MVPlugin, Core { // Setup our Map for our Commands using the CommandHandler. private CommandHandler commandHandler; + private CommandQueueManager commandQueueManager; private static final String LOG_TAG = "[Multiverse-Core]"; @@ -257,6 +257,8 @@ public class MultiverseCore extends JavaPlugin implements MVPlugin, Core { // Setup our SafeTTeleporter this.safeTTeleporter = new SimpleSafeTTeleporter(this); this.unsafeCallWrapper = new UnsafeCallWrapper(this); + // Setup our CompatibilityLayer + CompatibilityLayer.init(); } @@ -288,6 +290,7 @@ public class MultiverseCore extends JavaPlugin implements MVPlugin, Core { // Setup the command manager this.commandHandler = new CommandHandler(this, this.ph); + this.commandQueueManager = new CommandQueueManager(this); // Call the Function to assign all the Commands to their Class. this.registerCommands(); @@ -309,7 +312,7 @@ public class MultiverseCore extends JavaPlugin implements MVPlugin, Core { this.worldManager.loadDefaultWorlds(); this.worldManager.loadWorlds(true); } else { - this.log(Level.SEVERE, "Your configs were not loaded. Very little will function in Multiverse."); + Logging.severe("Your configs were not loaded. Very little will function in Multiverse."); } this.anchorManager.loadAnchors(); @@ -346,104 +349,28 @@ public class MultiverseCore extends JavaPlugin implements MVPlugin, Core { } } + private void setupMetrics() { + if (TestingMode.isDisabled()) { + MetricsConfigurator.configureMetrics(this); + } + } + /** * Initializes the buscript javascript library. */ private void initializeBuscript() { - buscript = new Buscript(this); - // Add global variable "multiverse" to javascript environment - buscript.setScriptVariable("multiverse", this); - } + buscript = null; - /** - * Plotter for Environment-Values. - */ - private static final class EnvironmentPlotter extends Metrics.Plotter { - private MultiverseCore core; - private final Environment env; - - public EnvironmentPlotter(MultiverseCore core, Environment env) { - super(envToString(env)); - this.core = core; - this.env = env; - } - - private static String envToString(Environment env) { - return new StringBuilder().append(env.name().toUpperCase().charAt(0)) - .append(env.name().toLowerCase().substring(1)).toString(); - } - - @Override - public int getValue() { - int count = 0; - for (MultiverseWorld w : core.getMVWorldManager().getMVWorlds()) - if (w.getEnvironment() == env) - count++; - core.log(Level.FINE, String.format("Tracking %d worlds of type %s", count, env)); - return count; - } - } - - /** - * Plotter for Generator-Values. - */ - private static final class GeneratorPlotter extends Metrics.Plotter { - private MultiverseCore core; - private final String gen; - - public GeneratorPlotter(MultiverseCore core, String gen) { - super(gen); - this.core = core; - this.gen = gen; - } - - @Override - public int getValue() { - int count = 0; - for (MultiverseWorld w : core.getMVWorldManager().getMVWorlds()) - if (gen.equals(w.getGenerator())) - count++; - core.log(Level.FINE, String.format("Tracking %d worlds of type %s", count, gen)); - return count; - } - } - - private void setupMetrics() { - try { - Metrics m = new Metrics(this); - - Metrics.Graph envGraph = m.createGraph("Worlds by environment"); - for (Environment env : Environment.values()) - envGraph.addPlotter(new EnvironmentPlotter(this, env)); - - Metrics.Graph loadedWorldsGraph = m.createGraph("Worlds by environment"); - loadedWorldsGraph.addPlotter(new Metrics.Plotter("Loaded worlds") { - @Override - public int getValue() { - return getMVWorldManager().getMVWorlds().size(); - } - }); - loadedWorldsGraph.addPlotter(new Metrics.Plotter("Total number of worlds") { - @Override - public int getValue() { - return getMVWorldManager().getMVWorlds().size() - + getMVWorldManager().getUnloadedWorlds().size(); - } - }); - - Set gens = new HashSet(); - for (MultiverseWorld w : this.getMVWorldManager().getMVWorlds()) - gens.add(w.getGenerator()); - gens.remove(null); - gens.remove("null"); - Metrics.Graph genGraph = m.createGraph("Custom Generators"); - for (String gen : gens) - genGraph.addPlotter(new GeneratorPlotter(this, gen)); - - m.start(); - log(Level.FINE, "Metrics have run!"); - } catch (Exception e) { - log(Level.WARNING, "There was an issue while enabling metrics: " + e.getMessage()); + if (this.getMVConfig().getEnableBuscript()) { + try { + buscript = new Buscript(this); + // Add global variable "multiverse" to javascript environment + buscript.setScriptVariable("multiverse", this); + } catch (NullPointerException e) { + Logging.warning("Buscript failed to load! The script command will be disabled! " + + "If you would like not to see this message, " + + "use `/mv conf enablebuscript false` to disable Buscript from loading."); + } } } @@ -467,7 +394,7 @@ public class MultiverseCore extends JavaPlugin implements MVPlugin, Core { pm.registerEvents(this.entityListener, this); pm.registerEvents(this.weatherListener, this); pm.registerEvents(this.portalListener, this); - log(Level.INFO, "We are aware of the warning about the deprecated event. There is no alternative that allows us to do what we need to do. The performance impact is negligible."); + Logging.info(ChatColor.GREEN + "We are aware of the warning about the deprecated event. There is no alternative that allows us to do what we need to do and performance impact is negligible. It is safe to ignore."); pm.registerEvents(this.worldListener, this); pm.registerEvents(new MVMapListener(this), this); } @@ -586,13 +513,13 @@ public class MultiverseCore extends JavaPlugin implements MVPlugin, Core { try { wconf.load(worldsFile); } catch (IOException e) { - log(Level.WARNING, "Cannot load worlds.yml"); + Logging.warning("Cannot load worlds.yml"); } catch (InvalidConfigurationException e) { - log(Level.WARNING, "Your worlds.yml is invalid!"); + Logging.warning("Your worlds.yml is invalid!"); } if (!wconf.isConfigurationSection("worlds")) { // empty config - this.log(Level.FINE, "No worlds to migrate!"); + Logging.fine("No worlds to migrate!"); return; } @@ -605,7 +532,7 @@ public class MultiverseCore extends JavaPlugin implements MVPlugin, Core { // fine newValues.put(entry.getKey(), entry.getValue()); } else if (entry.getValue() instanceof ConfigurationSection) { - this.log(Level.FINE, "Migrating: " + entry.getKey()); + Logging.fine("Migrating: " + entry.getKey()); // we have to migrate this WorldProperties world = new WorldProperties(Collections.EMPTY_MAP); ConfigurationSection section = (ConfigurationSection) entry.getValue(); @@ -771,8 +698,8 @@ public class MultiverseCore extends JavaPlugin implements MVPlugin, Core { try { difficulty = Difficulty.valueOf(section.getString("difficulty").toUpperCase()); } catch (IllegalArgumentException e) { - this.log(Level.WARNING, "Could not parse difficulty: " + section.getString("difficulty")); - this.log(Level.WARNING, "Setting world " + entry.getKey() + " difficulty to NORMAL"); + Logging.warning("Could not parse difficulty: " + section.getString("difficulty")); + Logging.warning("Setting world " + entry.getKey() + " difficulty to NORMAL"); difficulty = Difficulty.NORMAL; } if (difficulty != null) { @@ -789,7 +716,7 @@ public class MultiverseCore extends JavaPlugin implements MVPlugin, Core { wasChanged = true; } else { // huh? - this.log(Level.WARNING, "Removing unknown entry in the config: " + entry); + Logging.warning("Removing unknown entry in the config: " + entry); // just don't add to newValues wasChanged = true; } @@ -923,8 +850,12 @@ public class MultiverseCore extends JavaPlugin implements MVPlugin, Core { /** * {@inheritDoc} + * + * @deprecated This is now deprecated, nobody needs it any longer. + * All logging is now done with {@link Logging}. */ @Override + @Deprecated public void log(Level level, String msg) { Logging.log(level, msg); } @@ -991,6 +922,15 @@ public class MultiverseCore extends JavaPlugin implements MVPlugin, Core { return this.commandHandler; } + /** + * {@inheritDoc} + */ + @Override + @Deprecated + public CommandQueueManager getCommandQueueManager() { + return commandQueueManager; + } + /** * Gets the log-tag. * @@ -1143,7 +1083,7 @@ public class MultiverseCore extends JavaPlugin implements MVPlugin, Core { this.multiverseConfig.save(new File(getDataFolder(), "config.yml")); return true; } catch (IOException e) { - this.log(Level.SEVERE, "Could not save Multiverse config.yml config. Please check your file permissions."); + Logging.severe("Could not save Multiverse config.yml config. Please check your file permissions."); return false; } } @@ -1191,12 +1131,22 @@ public class MultiverseCore extends JavaPlugin implements MVPlugin, Core { /** * {@inheritDoc} - * @deprecated This is deprecated! + * @deprecated This is deprecated! Do not use! */ @Override @Deprecated public Boolean regenWorld(String name, Boolean useNewSeed, Boolean randomSeed, String seed) { - return this.worldManager.regenWorld(name, useNewSeed, randomSeed, seed); + return this.worldManager.regenWorld(name, useNewSeed, randomSeed, seed, false); + } + + /** + * {@inheritDoc} + * @deprecated This is deprecated! Do not use! + */ + @Override + @Deprecated + public Boolean regenWorld(String name, Boolean useNewSeed, Boolean randomSeed, String seed, Boolean keepGameRules) { + return this.worldManager.regenWorld(name, useNewSeed, randomSeed, seed, keepGameRules); } /** diff --git a/src/main/java/com/onarandombox/MultiverseCore/MultiverseCoreConfiguration.java b/src/main/java/com/onarandombox/MultiverseCore/MultiverseCoreConfiguration.java index a04a6651..aaf21275 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/MultiverseCoreConfiguration.java +++ b/src/main/java/com/onarandombox/MultiverseCore/MultiverseCoreConfiguration.java @@ -56,6 +56,8 @@ public class MultiverseCoreConfiguration extends SerializationConfig implements @Property private volatile boolean displaypermerrors; @Property + private volatile boolean enablebuscript; + @Property private volatile int globaldebug; @Property private volatile boolean silentstart; @@ -94,17 +96,18 @@ public class MultiverseCoreConfiguration extends SerializationConfig implements // BEGIN CHECKSTYLE-SUPPRESSION: MagicNumberCheck enforceaccess = false; useasyncchat = true; - prefixchat = true; + prefixchat = false; prefixchatformat = "[%world%]%chat%"; teleportintercept = true; firstspawnoverride = true; displaypermerrors = true; + enablebuscript = true; globaldebug = 0; messagecooldown = 5000; teleportcooldown = 1000; this.version = 2.9; silentstart = false; - defaultportalsearch = false; + defaultportalsearch = true; portalsearchradius = 128; autopurge = true; idonotwanttodonate = false; @@ -213,6 +216,22 @@ public class MultiverseCoreConfiguration extends SerializationConfig implements return this.displaypermerrors; } + /** + * {@inheritDoc} + */ + @Override + public boolean getEnableBuscript() { + return this.enablebuscript; + } + + /** + * {@inheritDoc} + */ + @Override + public void setEnableBuscript(boolean enableBuscript) { + this.enablebuscript = enableBuscript; + } + /** * {@inheritDoc} */ diff --git a/src/main/java/com/onarandombox/MultiverseCore/api/Core.java b/src/main/java/com/onarandombox/MultiverseCore/api/Core.java index ff8513d4..5f66cddb 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/api/Core.java +++ b/src/main/java/com/onarandombox/MultiverseCore/api/Core.java @@ -8,6 +8,7 @@ package com.onarandombox.MultiverseCore.api; import buscript.Buscript; +import com.onarandombox.MultiverseCore.commandtools.queue.CommandQueueManager; import com.onarandombox.MultiverseCore.destination.DestinationFactory; import com.onarandombox.MultiverseCore.utils.AnchorManager; import com.onarandombox.MultiverseCore.utils.MVEconomist; @@ -86,6 +87,15 @@ public interface Core { */ CommandHandler getCommandHandler(); + /** + * Manager for command that requires /mv confirm before execution. + * + * @return A non-null {@link CommandQueueManager}. + * @deprecated To be moved to new command manager in 5.0.0 + */ + @Deprecated + CommandQueueManager getCommandQueueManager(); + /** * Gets the factory class responsible for loading many different destinations * on demand. @@ -116,20 +126,38 @@ public interface Core { AnchorManager getAnchorManager(); /** - * Used by queued commands to regenerate a world on a delay. + * Previously used by queued commands to regenerate a world on a delay. + * Do not use api method for any other purpose. * - * @param name Name of the world to regenerate - * @param useNewSeed If a new seed should be used - * @param randomSeed IF the new seed should be random - * @param seed The seed of the world. + * @param name Name of the world to regenerate + * @param useNewSeed If a new seed should be used + * @param randomSeed If the new seed should be random + * @param seed The seed of the world. * * @return True if success, false if fail. * - * @deprecated Use {@link MVWorldManager#regenWorld(String, boolean, boolean, String)} instead. + * @deprecated Use {@link MVWorldManager#regenWorld(String, boolean, boolean, String, boolean)} instead. */ @Deprecated Boolean regenWorld(String name, Boolean useNewSeed, Boolean randomSeed, String seed); + /** + * Used by queued commands to regenerate a world on a delay. + * Do not use api method for any other purpose. + * + * @param name Name of the world to regenerate + * @param useNewSeed If a new seed should be used + * @param randomSeed If the new seed should be random + * @param seed The seed of the world. + * @param keepGameRules If GameRules should be kept on world regen. + * + * @return True if success, false if fail. + * + * @deprecated Use {@link MVWorldManager#regenWorld(String, boolean, boolean, String, boolean)} instead. + */ + @Deprecated + Boolean regenWorld(String name, Boolean useNewSeed, Boolean randomSeed, String seed, Boolean keepGameRules); + /** * Decrements the number of plugins that have specifically hooked into core. */ diff --git a/src/main/java/com/onarandombox/MultiverseCore/api/LoggablePlugin.java b/src/main/java/com/onarandombox/MultiverseCore/api/LoggablePlugin.java index f37ef788..56852e45 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/api/LoggablePlugin.java +++ b/src/main/java/com/onarandombox/MultiverseCore/api/LoggablePlugin.java @@ -7,11 +7,17 @@ package com.onarandombox.MultiverseCore.api; +import com.dumptruckman.minecraft.util.Logging; import org.bukkit.Server; import java.util.logging.Level; -/** A simple API to require plugins to have a log method. */ +/** + * A simple API to require plugins to have a log method. + * + * @deprecated Replaced by {@link Logging}. + * */ +@Deprecated public interface LoggablePlugin { /** * Logs a message at the specified level. diff --git a/src/main/java/com/onarandombox/MultiverseCore/api/MVWorldManager.java b/src/main/java/com/onarandombox/MultiverseCore/api/MVWorldManager.java index 3eb96e2d..599ce7cb 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/api/MVWorldManager.java +++ b/src/main/java/com/onarandombox/MultiverseCore/api/MVWorldManager.java @@ -174,6 +174,16 @@ public interface MVWorldManager { */ MultiverseWorld getMVWorld(String name); + /** + * Returns a {@link MultiverseWorld} if the world with name given exists, and null if it does not. + * This will search optionally for alias names. + * + * @param name The name or optionally the alias of the world to get. + * @param checkAliases Indicates whether to check for world alias name. + * @return A {@link MultiverseWorld} or null. + */ + MultiverseWorld getMVWorld(String name, boolean checkAliases); + /** * Returns a {@link MultiverseWorld} if it exists, and null if it does not. * @@ -183,13 +193,24 @@ public interface MVWorldManager { MultiverseWorld getMVWorld(World world); /** - * Checks to see if the given name is a valid {@link MultiverseWorld}. + * Checks to see if the given name is a valid {@link MultiverseWorld} + * Searches based on world name AND alias. * * @param name The name or alias of the world to check. * @return True if the world exists, false if not. */ boolean isMVWorld(String name); + /** + * Checks to see if the given name is a valid {@link MultiverseWorld}. + * Optionally searches by alias is specified. + * + * @param name The name or alias of the world to check. + * @param checkAliases Indicates whether to check for world alias name. + * @return True if the world exists, false if not. + */ + boolean isMVWorld(String name, boolean checkAliases); + /** * Checks to see if the given world is a valid {@link MultiverseWorld}. * @@ -292,15 +313,28 @@ public interface MVWorldManager { /** * Regenerates a world. * - * @param name Name of the world to regenerate - * @param useNewSeed If a new seed should be used - * @param randomSeed IF the new seed should be random - * @param seed The seed of the world. + * @param name Name of the world to regenerate + * @param useNewSeed If a new seed should be used + * @param randomSeed If the new seed should be random + * @param seed The seed of the world. * * @return True if success, false if fail. */ boolean regenWorld(String name, boolean useNewSeed, boolean randomSeed, String seed); + /** + * Regenerates a world. + * + * @param name Name of the world to regenerate + * @param useNewSeed If a new seed should be used + * @param randomSeed If the new seed should be random + * @param seed The seed of the world. + * @param keepGameRules If GameRules should be kept on world regen. + * + * @return True if success, false if fail. + */ + boolean regenWorld(String name, boolean useNewSeed, boolean randomSeed, String seed, boolean keepGameRules); + boolean isKeepingSpawnInMemory(World world); /** @@ -314,4 +348,11 @@ public interface MVWorldManager { * does not exist. {@code includeLoaded} if the world exists and is loaded. */ boolean hasUnloadedWorld(String name, boolean includeLoaded); + + /** + * Get all the possible worlds that Multiverse has detected to be importable. + * + * @return A collection of world names that are deemed importable. + */ + Collection getPotentialWorlds(); } diff --git a/src/main/java/com/onarandombox/MultiverseCore/api/MultiverseCoreConfig.java b/src/main/java/com/onarandombox/MultiverseCore/api/MultiverseCoreConfig.java index b5574ff2..07b2bb91 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/api/MultiverseCoreConfig.java +++ b/src/main/java/com/onarandombox/MultiverseCore/api/MultiverseCoreConfig.java @@ -86,6 +86,18 @@ public interface MultiverseCoreConfig extends ConfigurationSerializable { */ boolean getDisplayPermErrors(); + /** + * Sets enableBuscript. + * @param enableBuscript The new value. + */ + void setEnableBuscript(boolean enableBuscript); + + /** + * Gets enableBuscript. + * @return enableBuscript. + */ + boolean getEnableBuscript(); + /** * Sets firstSpawnOverride. * @param firstSpawnOverride The new value. diff --git a/src/main/java/com/onarandombox/MultiverseCore/commands/CheckCommand.java b/src/main/java/com/onarandombox/MultiverseCore/commands/CheckCommand.java index 474308c6..7bc4ba48 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/commands/CheckCommand.java +++ b/src/main/java/com/onarandombox/MultiverseCore/commands/CheckCommand.java @@ -11,6 +11,7 @@ import com.onarandombox.MultiverseCore.MultiverseCore; import com.onarandombox.MultiverseCore.api.MVDestination; import com.onarandombox.MultiverseCore.destination.InvalidDestination; import com.onarandombox.MultiverseCore.utils.MVPermissions; +import com.onarandombox.MultiverseCore.utils.PlayerFinder; import org.bukkit.ChatColor; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; @@ -37,7 +38,7 @@ public class CheckCommand extends MultiverseCommand { @Override public void runCommand(CommandSender sender, List args) { - Player p = this.plugin.getServer().getPlayer(args.get(0)); + Player p = PlayerFinder.get(args.get(0), sender); if (p == null) { sender.sendMessage("Could not find player " + ChatColor.GREEN + args.get(0)); sender.sendMessage("Are they online?"); diff --git a/src/main/java/com/onarandombox/MultiverseCore/commands/ConfirmCommand.java b/src/main/java/com/onarandombox/MultiverseCore/commands/ConfirmCommand.java index 2a68f353..498a1431 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/commands/ConfirmCommand.java +++ b/src/main/java/com/onarandombox/MultiverseCore/commands/ConfirmCommand.java @@ -33,7 +33,7 @@ public class ConfirmCommand extends MultiverseCommand { @Override public void runCommand(CommandSender sender, List args) { - this.plugin.getCommandHandler().confirmQueuedCommand(sender); + this.plugin.getCommandQueueManager().runQueuedCommand(sender); } } diff --git a/src/main/java/com/onarandombox/MultiverseCore/commands/CreateCommand.java b/src/main/java/com/onarandombox/MultiverseCore/commands/CreateCommand.java index 87bdacab..6d8f3833 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/commands/CreateCommand.java +++ b/src/main/java/com/onarandombox/MultiverseCore/commands/CreateCommand.java @@ -46,10 +46,15 @@ public class CreateCommand extends MultiverseCommand { this.addCommandExample("/mv create " + ChatColor.GOLD + "moonworld" + ChatColor.GREEN + " normal" + ChatColor.DARK_AQUA + " -g BukkitFullOfMoon"); this.worldManager = this.plugin.getMVWorldManager(); } - + + private String trimWorldName(String userInput) { + // Removes relative paths. + return userInput.replaceAll("^[./\\\\]+", ""); + } + @Override public void runCommand(CommandSender sender, List args) { - String worldName = args.get(0); + String worldName = trimWorldName(args.get(0)); File worldFile = new File(this.plugin.getServer().getWorldContainer(), worldName); String env = args.get(1); String seed = CommandHandler.getFlag("-s", args); @@ -66,7 +71,13 @@ public class CreateCommand extends MultiverseCommand { useSpawnAdjust = false; } } - + + // Make sure the world name doesn't contain the words 'plugins' and '.dat' + if(worldName.contains("plugins")||worldName.contains(".dat")){ + sender.sendMessage(ChatColor.RED + "Multiverse cannot create a world that contains 'plugins' or '.dat'"); + return; + } + if (this.worldManager.isMVWorld(worldName)) { sender.sendMessage(ChatColor.RED + "Multiverse cannot create " + ChatColor.GOLD + ChatColor.UNDERLINE + "another" + ChatColor.RESET + ChatColor.RED + " world named " + worldName); @@ -117,4 +128,4 @@ public class CreateCommand extends MultiverseCommand { Command.broadcastCommandMessage(sender, "FAILED."); } } -} +} \ No newline at end of file diff --git a/src/main/java/com/onarandombox/MultiverseCore/commands/DebugCommand.java b/src/main/java/com/onarandombox/MultiverseCore/commands/DebugCommand.java index 4f6ec40c..b2ccd99e 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/commands/DebugCommand.java +++ b/src/main/java/com/onarandombox/MultiverseCore/commands/DebugCommand.java @@ -7,6 +7,7 @@ package com.onarandombox.MultiverseCore.commands; +import com.dumptruckman.minecraft.util.Logging; import com.onarandombox.MultiverseCore.MultiverseCore; import org.bukkit.ChatColor; import org.bukkit.command.CommandSender; @@ -60,7 +61,7 @@ public class DebugCommand extends MultiverseCommand { sender.sendMessage("Multiverse Debug mode is " + ChatColor.RED + "OFF"); } else { sender.sendMessage("Multiverse Debug mode is " + ChatColor.GREEN + debugLevel); - this.plugin.log(Level.FINE, "Multiverse Debug ENABLED"); + Logging.fine("Multiverse Debug ENABLED"); } } } diff --git a/src/main/java/com/onarandombox/MultiverseCore/commands/DeleteCommand.java b/src/main/java/com/onarandombox/MultiverseCore/commands/DeleteCommand.java index 2e7b3c8b..60345493 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/commands/DeleteCommand.java +++ b/src/main/java/com/onarandombox/MultiverseCore/commands/DeleteCommand.java @@ -8,9 +8,11 @@ package com.onarandombox.MultiverseCore.commands; import com.onarandombox.MultiverseCore.MultiverseCore; +import com.onarandombox.MultiverseCore.commandtools.queue.QueuedCommand; import org.bukkit.ChatColor; import org.bukkit.command.CommandSender; import org.bukkit.permissions.PermissionDefault; +import org.jetbrains.annotations.NotNull; import java.util.ArrayList; import java.util.List; @@ -35,11 +37,24 @@ public class DeleteCommand extends MultiverseCommand { public void runCommand(CommandSender sender, List args) { String worldName = args.get(0); - Class[] paramTypes = {String.class}; - List objectArgs = new ArrayList(args); - this.plugin.getCommandHandler() - .queueCommand(sender, "mvdelete", "deleteWorld", objectArgs, - paramTypes, ChatColor.GREEN + "World '" + worldName + "' Deleted!", - ChatColor.RED + "World '" + worldName + "' could NOT be deleted!"); + this.plugin.getCommandQueueManager().addToQueue(new QueuedCommand( + sender, + deleteRunnable(sender, worldName), + String.format("Are you sure you want to delete world '%s'? You cannot undo this action.", worldName) + )); + } + + private Runnable deleteRunnable(@NotNull CommandSender sender, + @NotNull String worldName) { + + return () -> { + sender.sendMessage(String.format("Deleting world '%s'...", worldName)); + if (this.plugin.getMVWorldManager().deleteWorld(worldName)) { + sender.sendMessage(String.format("%sWorld %s was deleted!", ChatColor.GREEN, worldName)); + return; + } + sender.sendMessage(String.format("%sThere was an issue deleting '%s'! Please check console for errors.", + ChatColor.RED, worldName)); + }; } } diff --git a/src/main/java/com/onarandombox/MultiverseCore/commands/GameruleCommand.java b/src/main/java/com/onarandombox/MultiverseCore/commands/GameruleCommand.java index 5cc91d9f..ca0e1e07 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/commands/GameruleCommand.java +++ b/src/main/java/com/onarandombox/MultiverseCore/commands/GameruleCommand.java @@ -10,6 +10,7 @@ package com.onarandombox.MultiverseCore.commands; import com.onarandombox.MultiverseCore.MultiverseCore; import org.bukkit.Bukkit; import org.bukkit.ChatColor; +import org.bukkit.GameRule; import org.bukkit.World; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; @@ -55,21 +56,61 @@ public class GameruleCommand extends MultiverseCommand { return; } - final String gameRule = args.get(0); + final GameRule gameRule = GameRule.getByName(args.get(0)); final String value = args.get(1); final World world; if (args.size() == 2) { world = p.getWorld(); } else { world = Bukkit.getWorld(args.get(2)); + if (world == null) { + sender.sendMessage(ChatColor.RED + "Failure!" + ChatColor.WHITE + " World " + ChatColor.AQUA + args.get(2) + + ChatColor.WHITE + " does not exist."); + return; + } } - if (world.setGameRuleValue(gameRule, value)) { - sender.sendMessage(ChatColor.GREEN + "Success!" + ChatColor.WHITE + " Gamerule " + ChatColor.AQUA + gameRule - + ChatColor.WHITE + " was set to " + ChatColor.GREEN + value); + if (gameRule == null) { + sender.sendMessage(ChatColor.RED + "Failure! " + ChatColor.AQUA + args.get(0) + ChatColor.WHITE + + " is not a valid gamerule."); } else { - sender.sendMessage(ChatColor.RED + "Failure!" + ChatColor.WHITE + " Gamerule " + ChatColor.AQUA + gameRule - + ChatColor.WHITE + " cannot be set to " + ChatColor.RED + value); + if (gameRule.getType() == Boolean.class) { + boolean booleanValue; + if (value.equalsIgnoreCase("true")) { + booleanValue = true; + } else if (value.equalsIgnoreCase("false")) { + booleanValue = false; + } else { + sender.sendMessage(getErrorMessage(gameRule.getName(), value) + "it can only be set to true or false."); + return; + } + + if (!world.setGameRule(gameRule, booleanValue)) { + sender.sendMessage(getErrorMessage(gameRule.getName(), value) + "something went wrong."); + return; + } + } else if (gameRule.getType() == Integer.class) { + try { + if (!world.setGameRule(gameRule, Integer.parseInt(value))) { + throw new NumberFormatException(); + } + } catch (NumberFormatException e) { + sender.sendMessage(getErrorMessage(gameRule.getName(), value) + "it can only be set to a positive integer."); + return; + } + } else { + sender.sendMessage(ChatColor.RED + "Failure!" + ChatColor.WHITE + " Gamerule " + ChatColor.AQUA + gameRule.getName() + + ChatColor.WHITE + " isn't supported yet, please let us know about it."); + return; + } + + sender.sendMessage(ChatColor.GREEN + "Success!" + ChatColor.WHITE + " Gamerule " + ChatColor.AQUA + gameRule.getName() + + ChatColor.WHITE + " was set to " + ChatColor.GREEN + value + ChatColor.WHITE + "."); } } + + private String getErrorMessage(String gameRule, String value) { + return ChatColor.RED + "Failure!" + ChatColor.WHITE + " Gamerule " + ChatColor.AQUA + gameRule + + ChatColor.WHITE + " could not be set to " + ChatColor.RED + value + ChatColor.WHITE + ", "; + } } diff --git a/src/main/java/com/onarandombox/MultiverseCore/commands/GamerulesCommand.java b/src/main/java/com/onarandombox/MultiverseCore/commands/GamerulesCommand.java index 321ac090..448b5c13 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/commands/GamerulesCommand.java +++ b/src/main/java/com/onarandombox/MultiverseCore/commands/GamerulesCommand.java @@ -8,14 +8,20 @@ package com.onarandombox.MultiverseCore.commands; import com.onarandombox.MultiverseCore.MultiverseCore; +import com.onarandombox.MultiverseCore.display.ColorAlternator; +import com.onarandombox.MultiverseCore.display.ContentDisplay; +import com.onarandombox.MultiverseCore.display.settings.MapDisplaySettings; import org.bukkit.Bukkit; import org.bukkit.ChatColor; +import org.bukkit.GameRule; import org.bukkit.World; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; import org.bukkit.permissions.PermissionDefault; +import java.util.HashMap; import java.util.List; +import java.util.Map; /** * Allows management of Anchor Destinations. @@ -60,17 +66,30 @@ public class GamerulesCommand extends MultiverseCommand { world = p.getWorld(); } else { world = Bukkit.getWorld(args.get(0)); + if (world == null) { + sender.sendMessage(ChatColor.RED + "Failure!" + ChatColor.WHITE + " World " + ChatColor.AQUA + args.get(0) + + ChatColor.WHITE + " does not exist."); + return; + } } - final StringBuilder gameRules = new StringBuilder(); - for (final String gameRule : world.getGameRules()) { - if (gameRules.length() != 0) { - gameRules.append(ChatColor.WHITE).append(", "); + ContentDisplay.forContent(getGameRuleMap(world)) + .header("=== Gamerules for %s%s%s ===", ChatColor.AQUA, world.getName(), ChatColor.WHITE) + .colorTool(ColorAlternator.with(ChatColor.GREEN, ChatColor.GOLD)) + .setting(MapDisplaySettings.OPERATOR, ": ") + .show(sender); + } + + private Map getGameRuleMap(World world) { + Map gameRuleMap = new HashMap<>(); + for (GameRule rule : GameRule.values()) { + Object value = world.getGameRuleValue(rule); + if (value == null) { + gameRuleMap.put(rule.getName(), "null"); + continue; } - gameRules.append(ChatColor.AQUA).append(gameRule).append(ChatColor.WHITE).append(": "); - gameRules.append(ChatColor.GREEN).append(world.getGameRuleValue(gameRule)); + gameRuleMap.put(rule.getName(), value); } - sender.sendMessage("=== Gamerules for " + ChatColor.AQUA + world.getName() + ChatColor.WHITE + " ==="); - sender.sendMessage(gameRules.toString()); + return gameRuleMap; } } diff --git a/src/main/java/com/onarandombox/MultiverseCore/commands/HelpCommand.java b/src/main/java/com/onarandombox/MultiverseCore/commands/HelpCommand.java index 90913026..59a429e4 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/commands/HelpCommand.java +++ b/src/main/java/com/onarandombox/MultiverseCore/commands/HelpCommand.java @@ -40,24 +40,16 @@ public class HelpCommand extends PaginatedCoreCommand { @Override protected List getFilteredItems(List availableItems, String filter) { + String expression = "(?i).*" + cleanFilter(filter) + ".*"; List filtered = new ArrayList(); for (Command c : availableItems) { - if (stitchThisString(c.getKeyStrings()).matches("(?i).*" + filter + ".*")) { + if (stitchThisString(c.getKeyStrings()).matches(expression) + || c.getCommandName().matches(expression) + || c.getCommandDesc().matches(expression) + || c.getCommandUsage().matches(expression) + || c.getCommandExamples().stream().anyMatch(eg -> eg.matches(expression))) { filtered.add(c); - } else if (c.getCommandName().matches("(?i).*" + filter + ".*")) { - filtered.add(c); - } else if (c.getCommandDesc().matches("(?i).*" + filter + ".*")) { - filtered.add(c); - } else if (c.getCommandUsage().matches("(?i).*" + filter + ".*")) { - filtered.add(c); - } else { - for (String example : c.getCommandExamples()) { - if (example.matches("(?i).*" + filter + ".*")) { - filtered.add(c); - break; - } - } } } return filtered; diff --git a/src/main/java/com/onarandombox/MultiverseCore/commands/ImportCommand.java b/src/main/java/com/onarandombox/MultiverseCore/commands/ImportCommand.java index b16bf239..7a52d5a1 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/commands/ImportCommand.java +++ b/src/main/java/com/onarandombox/MultiverseCore/commands/ImportCommand.java @@ -9,7 +9,7 @@ package com.onarandombox.MultiverseCore.commands; import com.onarandombox.MultiverseCore.MultiverseCore; import com.onarandombox.MultiverseCore.api.MVWorldManager; -import com.onarandombox.MultiverseCore.api.MultiverseWorld; +import com.onarandombox.MultiverseCore.utils.WorldNameChecker; import com.pneumaticraft.commandhandler.CommandHandler; import org.bukkit.ChatColor; import org.bukkit.World.Environment; @@ -18,8 +18,6 @@ import org.bukkit.command.CommandSender; import org.bukkit.permissions.PermissionDefault; import java.io.File; -import java.io.FilenameFilter; -import java.util.ArrayList; import java.util.Collection; import java.util.List; @@ -45,55 +43,17 @@ public class ImportCommand extends MultiverseCommand { this.worldManager = this.plugin.getMVWorldManager(); } - /** - * A very basic check to see if a folder has a level.dat file. - * If it does, we can safely assume it's a world folder. - * - * @param worldFolder The File that may be a world. - * @return True if it looks like a world, false if not. - */ - private static boolean checkIfIsWorld(File worldFolder) { - if (worldFolder.isDirectory()) { - File[] files = worldFolder.listFiles(new FilenameFilter() { - @Override - public boolean accept(File file, String name) { - return name.toLowerCase().endsWith(".dat"); - } - }); - if (files != null && files.length > 0) { - return true; - } - } - return false; - } - - private String getPotentialWorlds() { - File worldFolder = this.plugin.getServer().getWorldContainer(); - if (worldFolder == null) { - return ""; - } - File[] files = worldFolder.listFiles(); - String worldList = ""; - Collection worlds = this.worldManager.getMVWorlds(); - List worldStrings = new ArrayList(); - for (MultiverseWorld world : worlds) { - worldStrings.add(world.getName()); - } - for (String world : this.worldManager.getUnloadedWorlds()) { - worldStrings.add(world); - } + private String getPotentialWorldStrings() { + final Collection potentialWorlds = this.worldManager.getPotentialWorlds(); + StringBuilder worldList = new StringBuilder(); ChatColor currColor = ChatColor.WHITE; - for (File file : files) { - if (file.isDirectory() && checkIfIsWorld(file) && !worldStrings.contains(file.getName())) { - worldList += currColor + file.getName() + " "; - if (currColor == ChatColor.WHITE) { - currColor = ChatColor.YELLOW; - } else { - currColor = ChatColor.WHITE; - } - } + + for (String world : potentialWorlds) { + worldList.append(currColor).append(world).append(' '); + currColor = currColor == ChatColor.WHITE ? ChatColor.YELLOW : ChatColor.WHITE; } - return worldList; + + return worldList.toString(); } private String trimWorldName(String userInput) { @@ -106,7 +66,7 @@ public class ImportCommand extends MultiverseCommand { String worldName = trimWorldName(args.get(0)); if (worldName.toLowerCase().equals("--list") || worldName.toLowerCase().equals("-l")) { - String worldList = this.getPotentialWorlds(); + String worldList = this.getPotentialWorldStrings(); if (worldList.length() > 2) { sender.sendMessage(ChatColor.AQUA + "====[ These look like worlds ]===="); sender.sendMessage(worldList); @@ -121,6 +81,12 @@ public class ImportCommand extends MultiverseCommand { this.showHelp(sender); return; } + + // Make sure the world name doesn't contain the words 'plugins' and '.dat' + if(worldName.contains("plugins")||worldName.contains(".dat")){ + sender.sendMessage(ChatColor.RED + "Multiverse cannot create a world that contains 'plugins' or '.dat'"); + return; + } // Make sure we don't already know about this world. if (this.worldManager.isMVWorld(worldName)) { @@ -149,10 +115,10 @@ public class ImportCommand extends MultiverseCommand { if (!worldFile.exists()) { sender.sendMessage(ChatColor.RED + "FAILED."); - String worldList = this.getPotentialWorlds(); + String worldList = this.getPotentialWorldStrings(); sender.sendMessage("That world folder does not exist. These look like worlds to me:"); sender.sendMessage(worldList); - } else if (!checkIfIsWorld(worldFile)) { + } else if (!WorldNameChecker.isValidWorldFolder(worldFile)) { sender.sendMessage(ChatColor.RED + "FAILED."); sender.sendMessage(String.format("'%s' does not appear to be a world. It is lacking a .dat file.", worldName)); @@ -168,4 +134,4 @@ public class ImportCommand extends MultiverseCommand { Command.broadcastCommandMessage(sender, ChatColor.RED + "Failed!"); } } -} +} \ No newline at end of file diff --git a/src/main/java/com/onarandombox/MultiverseCore/commands/InfoCommand.java b/src/main/java/com/onarandombox/MultiverseCore/commands/InfoCommand.java index ed7eb415..701424e7 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/commands/InfoCommand.java +++ b/src/main/java/com/onarandombox/MultiverseCore/commands/InfoCommand.java @@ -115,6 +115,7 @@ public class InfoCommand extends MultiverseCommand { FancyColorScheme colors = new FancyColorScheme(ChatColor.AQUA, ChatColor.AQUA, ChatColor.GOLD, ChatColor.WHITE); message.add(new FancyHeader("General Info", colors)); message.add(new FancyMessage("World Name: ", world.getName(), colors)); + message.add(new FancyMessage("World UID: ", world.getCBWorld().getUID().toString(), colors)); message.add(new FancyMessage("World Alias: ", world.getColoredWorldString(), colors)); message.add(new FancyMessage("Game Mode: ", world.getGameMode().toString(), colors)); message.add(new FancyMessage("Difficulty: ", world.getDifficulty().toString(), colors)); @@ -151,6 +152,7 @@ public class InfoCommand extends MultiverseCommand { message = new ArrayList(); message.add(new FancyHeader("More World Settings", colors)); message.add(new FancyMessage("World Type: ", world.getWorldType().toString(), colors)); + message.add(new FancyMessage("Generator: ", world.getGenerator(), colors)); message.add(new FancyMessage("Structures: ", world.getCBWorld().canGenerateStructures() + "", colors)); message.add(new FancyMessage("Weather: ", world.isWeatherEnabled() + "", colors)); message.add(new FancyMessage("Players will get hungry: ", world.getHunger() + "", colors)); diff --git a/src/main/java/com/onarandombox/MultiverseCore/commands/ListCommand.java b/src/main/java/com/onarandombox/MultiverseCore/commands/ListCommand.java index e63ea4f2..db8ee1a4 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/commands/ListCommand.java +++ b/src/main/java/com/onarandombox/MultiverseCore/commands/ListCommand.java @@ -9,122 +9,111 @@ package com.onarandombox.MultiverseCore.commands; import com.onarandombox.MultiverseCore.MultiverseCore; import com.onarandombox.MultiverseCore.api.MultiverseWorld; +import com.onarandombox.MultiverseCore.display.ColorAlternator; +import com.onarandombox.MultiverseCore.display.ContentDisplay; +import com.onarandombox.MultiverseCore.display.ContentFilter; +import com.onarandombox.MultiverseCore.display.DisplayHandlers; +import com.onarandombox.MultiverseCore.display.settings.PagedDisplaySettings; import org.bukkit.ChatColor; -import org.bukkit.World.Environment; +import org.bukkit.World; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; import org.bukkit.permissions.PermissionDefault; +import org.jetbrains.annotations.NotNull; -import java.util.ArrayList; +import java.util.Collection; import java.util.List; +import java.util.stream.Collectors; /** * Displays a listing of all worlds that a player can enter. */ -public class ListCommand extends PaginatedCoreCommand { +public class ListCommand extends MultiverseCommand { public ListCommand(MultiverseCore plugin) { super(plugin); this.setName("World Listing"); - this.setCommandUsage("/mv list"); + this.setCommandUsage("/mv list [filter] [page]"); this.setArgRange(0, 2); this.addKey("mvlist"); this.addKey("mvl"); this.addKey("mv list"); this.setPermission("multiverse.core.list.worlds", "Displays a listing of all worlds that you can enter.", PermissionDefault.OP); - this.setItemsPerPage(8); // SUPPRESS CHECKSTYLE: MagicNumberCheck - } - - private List getFancyWorldList(Player p) { - List worldList = new ArrayList(); - for (MultiverseWorld world : this.plugin.getMVWorldManager().getMVWorlds()) { - - if (p != null && (!this.plugin.getMVPerms().canEnterWorld(p, world))) { - continue; - } - - ChatColor color = ChatColor.GOLD; - Environment env = world.getEnvironment(); - if (env == Environment.NETHER) { - color = ChatColor.RED; - } else if (env == Environment.NORMAL) { - color = ChatColor.GREEN; - } else if (env == Environment.THE_END) { - color = ChatColor.AQUA; - } - StringBuilder builder = new StringBuilder(); - builder.append(world.getColoredWorldString()).append(ChatColor.WHITE); - builder.append(" - ").append(color).append(world.getEnvironment()); - if (world.isHidden()) { - if (p == null || this.plugin.getMVPerms().hasPermission(p, "multiverse.core.modify", true)) { - // Prefix hidden worlds with an "[H]" - worldList.add(ChatColor.GRAY + "[H]" + builder.toString()); - } - } else { - worldList.add(builder.toString()); - } - } - for (String name : this.plugin.getMVWorldManager().getUnloadedWorlds()) { - if (p == null || this.plugin.getMVPerms().hasPermission(p, "multiverse.access." + name, true)) { - worldList.add(ChatColor.GRAY + name + " - UNLOADED"); - } - } - return worldList; - } - - @Override - protected List getFilteredItems(List availableItems, String filter) { - List filtered = new ArrayList(); - - for (String s : availableItems) { - if (s.matches("(?i).*" + filter + ".*")) { - filtered.add(s); - } - } - return filtered; - } - - @Override - protected String getItemText(String item) { - return item; } @Override public void runCommand(CommandSender sender, List args) { - sender.sendMessage(ChatColor.LIGHT_PURPLE + "====[ Multiverse World List ]===="); - Player p = null; - if (sender instanceof Player) { - p = (Player) sender; - } + ContentFilter filter = ContentFilter.DEFAULT; + int page = 1; - - FilterObject filterObject = this.getPageAndFilter(args); - - List availableWorlds = new ArrayList(this.getFancyWorldList(p)); - if (filterObject.getFilter().length() > 0) { - availableWorlds = this.getFilteredItems(availableWorlds, filterObject.getFilter()); - if (availableWorlds.size() == 0) { - sender.sendMessage(ChatColor.RED + "Sorry... " + ChatColor.WHITE - + "No worlds matched your filter: " + ChatColor.AQUA + filterObject.getFilter()); - return; + // Either page or filter. + if (args.size() == 1) { + try { + page = Integer.parseInt(args.get(0)); + } catch (NumberFormatException ignore) { + filter = new ContentFilter(args.get(0)); } } - if (!(sender instanceof Player)) { - for (String c : availableWorlds) { - sender.sendMessage(c); + // Filter then page. + if (args.size() == 2) { + filter = new ContentFilter(args.get(0)); + try { + page = Integer.parseInt(args.get(1)); + } catch (NumberFormatException ignore) { + sender.sendMessage(ChatColor.RED + args.get(1) + " is not valid number!"); } - return; } - int totalPages = (int) Math.ceil(availableWorlds.size() / (this.itemsPerPage + 0.0)); + ContentDisplay.forContent(getListContents(sender)) + .header("%s====[ Multiverse World List ]====", ChatColor.GOLD) + .displayHandler(DisplayHandlers.PAGE_LIST) + .colorTool(ColorAlternator.with(ChatColor.AQUA, ChatColor.GOLD)) + .filter(filter) + .setting(PagedDisplaySettings.SHOW_PAGE, page) + .show(sender); + } - if (filterObject.getPage() > totalPages) { - filterObject.setPage(totalPages); + private Collection getListContents(@NotNull CommandSender sender) { + Player player = (sender instanceof Player) ? (Player) sender : null; + + List worldList = this.plugin.getMVWorldManager().getMVWorlds().stream() + .filter(world -> player == null || plugin.getMVPerms().canEnterWorld(player, world)) + .filter(world -> canSeeWorld(player, world)) + .map(world -> hiddenText(world) + world.getColoredWorldString() + " - " + parseColouredEnvironment(world.getEnvironment())) + .collect(Collectors.toList()); + + this.plugin.getMVWorldManager().getUnloadedWorlds().stream() + .filter(world -> plugin.getMVPerms().hasPermission(sender, "multiverse.access." + world, true)) + .map(world -> ChatColor.GRAY + world + " - UNLOADED") + .forEach(worldList::add); + + return worldList; + } + + private boolean canSeeWorld(Player player, MultiverseWorld world) { + return !world.isHidden() + || player == null + || this.plugin.getMVPerms().hasPermission(player, "multiverse.core.modify", true); + } + + private String hiddenText(MultiverseWorld world) { + return (world.isHidden()) ? String.format("%s[H] ", ChatColor.GRAY) : ""; + } + + private String parseColouredEnvironment(World.Environment env) { + ChatColor color = ChatColor.GOLD; + switch (env) { + case NETHER: + color = ChatColor.RED; + break; + case NORMAL: + color = ChatColor.GREEN; + break; + case THE_END: + color = ChatColor.AQUA; + break; } - - sender.sendMessage(ChatColor.AQUA + " Page " + filterObject.getPage() + " of " + totalPages); - - this.showPage(filterObject.getPage(), sender, availableWorlds); + return color + env.toString(); } } diff --git a/src/main/java/com/onarandombox/MultiverseCore/commands/PaginatedCommand.java b/src/main/java/com/onarandombox/MultiverseCore/commands/PaginatedCommand.java index c3ddf09b..2dd11a2a 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/commands/PaginatedCommand.java +++ b/src/main/java/com/onarandombox/MultiverseCore/commands/PaginatedCommand.java @@ -13,12 +13,14 @@ import org.bukkit.entity.Player; import org.bukkit.plugin.java.JavaPlugin; import java.util.List; +import java.util.regex.Pattern; /** * A generic paginated command. * @param The type of items on the page. */ public abstract class PaginatedCommand extends Command { + private final Pattern REGEX_SPECIAL_CHARS = Pattern.compile("[.+*?\\[^\\]$(){}=!<>|:-\\\\]"); private static final int DEFAULT_ITEMS_PER_PAGE = 9; /** * The number of items per page. @@ -40,12 +42,23 @@ public abstract class PaginatedCommand extends Command { /** * Gets filtered items. + * * @param availableItems All available items. * @param filter The filter-{@link String}. * @return A list of items that match the filter. */ protected abstract List getFilteredItems(List availableItems, String filter); + /** + * Escape regex special characters from filter + * + * @param filter The filter-{@link String}. + * @return String with regex characters escaped + */ + protected String cleanFilter(String filter) { + return REGEX_SPECIAL_CHARS.matcher(filter).replaceAll("\\\\$0"); + } + /** * Constructs a single string from a list of strings. * diff --git a/src/main/java/com/onarandombox/MultiverseCore/commands/RegenCommand.java b/src/main/java/com/onarandombox/MultiverseCore/commands/RegenCommand.java index c93e36a7..4efe7ad5 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/commands/RegenCommand.java +++ b/src/main/java/com/onarandombox/MultiverseCore/commands/RegenCommand.java @@ -8,11 +8,13 @@ package com.onarandombox.MultiverseCore.commands; import com.onarandombox.MultiverseCore.MultiverseCore; +import com.onarandombox.MultiverseCore.commandtools.queue.QueuedCommand; +import com.pneumaticraft.commandhandler.CommandHandler; import org.bukkit.ChatColor; import org.bukkit.command.CommandSender; import org.bukkit.permissions.PermissionDefault; +import org.jetbrains.annotations.NotNull; -import java.util.ArrayList; import java.util.List; /** @@ -23,8 +25,8 @@ public class RegenCommand extends MultiverseCommand { public RegenCommand(MultiverseCore plugin) { super(plugin); this.setName("Regenerates a World"); - this.setCommandUsage("/mv regen" + ChatColor.GREEN + " {WORLD}" + ChatColor.GOLD + " [-s [SEED]]"); - this.setArgRange(1, 3); + this.setCommandUsage("/mv regen" + ChatColor.GREEN + " {WORLD}" + ChatColor.GOLD + " [-s [SEED]] [--keep-gamerules]"); + this.setArgRange(1, 4); this.addKey("mvregen"); this.addKey("mv regen"); this.addCommandExample("You can use the -s with no args to get a new seed:"); @@ -37,17 +39,31 @@ public class RegenCommand extends MultiverseCommand { @Override public void runCommand(CommandSender sender, List args) { - Boolean useseed = (!(args.size() == 1)); - Boolean randomseed = (args.size() == 2 && args.get(1).equalsIgnoreCase("-s")); + String worldName = args.get(0); + boolean useseed = (!(args.size() == 1)); + boolean randomseed = (args.size() == 2 && args.get(1).equalsIgnoreCase("-s")); String seed = (args.size() == 3) ? args.get(2) : ""; + boolean keepGamerules = CommandHandler.hasFlag("--keep-gamerules", args); + this.plugin.getCommandQueueManager().addToQueue(new QueuedCommand( + sender, + doWorldRegen(sender, worldName, useseed, randomseed, seed, keepGamerules), + String.format("Are you sure you want to regen '%s'? You cannot undo this action.", worldName) + )); + } - Class[] paramTypes = {String.class, Boolean.class, Boolean.class, String.class}; - List objectArgs = new ArrayList(); - objectArgs.add(args.get(0)); - objectArgs.add(useseed); - objectArgs.add(randomseed); - objectArgs.add(seed); - this.plugin.getCommandHandler().queueCommand(sender, "mvregen", "regenWorld", objectArgs, - paramTypes, ChatColor.GREEN + "World Regenerated!", ChatColor.RED + "World could NOT be regenerated!"); + private Runnable doWorldRegen(@NotNull CommandSender sender, + @NotNull String worldName, + boolean useSeed, + boolean randomSeed, + @NotNull String seed, + boolean keepGamerules) { + + return () -> { + if (this.plugin.getMVWorldManager().regenWorld(worldName, useSeed, randomSeed, seed, keepGamerules)) { + sender.sendMessage(ChatColor.GREEN + "World Regenerated!"); + return; + } + sender.sendMessage(ChatColor.RED + "World could NOT be regenerated!"); + }; } } diff --git a/src/main/java/com/onarandombox/MultiverseCore/commands/ReloadCommand.java b/src/main/java/com/onarandombox/MultiverseCore/commands/ReloadCommand.java index 3481a4ea..abe2b0a6 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/commands/ReloadCommand.java +++ b/src/main/java/com/onarandombox/MultiverseCore/commands/ReloadCommand.java @@ -27,6 +27,7 @@ public class ReloadCommand extends MultiverseCommand { this.setCommandUsage("/mv reload"); this.setArgRange(0, 0); this.addKey("mvreload"); + this.addKey("mvr"); this.addKey("mv reload"); this.addCommandExample("/mv reload"); this.setPermission("multiverse.core.reload", "Reloads worlds.yml and config.yml.", PermissionDefault.OP); diff --git a/src/main/java/com/onarandombox/MultiverseCore/commands/ScriptCommand.java b/src/main/java/com/onarandombox/MultiverseCore/commands/ScriptCommand.java index 2896a8e7..665f6aa7 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/commands/ScriptCommand.java +++ b/src/main/java/com/onarandombox/MultiverseCore/commands/ScriptCommand.java @@ -35,6 +35,10 @@ public class ScriptCommand extends MultiverseCommand { @Override public void runCommand(CommandSender sender, List args) { + if (plugin.getScriptAPI() == null) { + sender.sendMessage("Buscript failed to load while the server was starting. Scripts cannot be run."); + return; + } File file = new File(plugin.getScriptAPI().getScriptFolder(), args.get(0)); if (!file.exists()) { sender.sendMessage("That script file does not exist in the Multiverse-Core scripts directory!"); diff --git a/src/main/java/com/onarandombox/MultiverseCore/commands/SetSpawnCommand.java b/src/main/java/com/onarandombox/MultiverseCore/commands/SetSpawnCommand.java index 2c3c25a8..26dad442 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/commands/SetSpawnCommand.java +++ b/src/main/java/com/onarandombox/MultiverseCore/commands/SetSpawnCommand.java @@ -30,6 +30,7 @@ public class SetSpawnCommand extends MultiverseCommand { this.setCommandUsage("/mv setspawn"); this.setArgRange(0, 6); this.addKey("mvsetspawn"); + this.addKey("mvsets"); this.addKey("mvss"); this.addKey("mv set spawn"); this.addKey("mv setspawn"); diff --git a/src/main/java/com/onarandombox/MultiverseCore/commands/SpawnCommand.java b/src/main/java/com/onarandombox/MultiverseCore/commands/SpawnCommand.java index 5925d887..8e865545 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/commands/SpawnCommand.java +++ b/src/main/java/com/onarandombox/MultiverseCore/commands/SpawnCommand.java @@ -9,6 +9,7 @@ package com.onarandombox.MultiverseCore.commands; import com.onarandombox.MultiverseCore.MultiverseCore; import com.onarandombox.MultiverseCore.api.MultiverseWorld; +import com.onarandombox.MultiverseCore.utils.PlayerFinder; import org.bukkit.ChatColor; import org.bukkit.Location; import org.bukkit.command.CommandSender; @@ -50,7 +51,7 @@ public class SpawnCommand extends MultiverseCommand { sender.sendMessage("You don't have permission to teleport another player to spawn. (multiverse.core.spawn.other)"); return; } - Player target = this.plugin.getServer().getPlayer(args.get(0)); + Player target = PlayerFinder.get(args.get(0), sender); if (target != null) { target.sendMessage("Teleporting to this world's spawn..."); spawnAccurately(target); diff --git a/src/main/java/com/onarandombox/MultiverseCore/commands/TeleportCommand.java b/src/main/java/com/onarandombox/MultiverseCore/commands/TeleportCommand.java index 847aa6d4..9507236c 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/commands/TeleportCommand.java +++ b/src/main/java/com/onarandombox/MultiverseCore/commands/TeleportCommand.java @@ -7,9 +7,11 @@ package com.onarandombox.MultiverseCore.commands; +import com.dumptruckman.minecraft.util.Logging; import com.onarandombox.MultiverseCore.MultiverseCore; import com.onarandombox.MultiverseCore.api.Teleporter; import com.onarandombox.MultiverseCore.api.MVDestination; +import com.onarandombox.MultiverseCore.commandtools.queue.QueuedCommand; import com.onarandombox.MultiverseCore.destination.CustomTeleporterDestination; import com.onarandombox.MultiverseCore.destination.DestinationFactory; import com.onarandombox.MultiverseCore.destination.InvalidDestination; @@ -17,6 +19,7 @@ import com.onarandombox.MultiverseCore.destination.WorldDestination; import com.onarandombox.MultiverseCore.enums.TeleportResult; import com.onarandombox.MultiverseCore.event.MVTeleportEvent; import com.onarandombox.MultiverseCore.api.SafeTTeleporter; +import com.onarandombox.MultiverseCore.utils.PlayerFinder; import org.bukkit.ChatColor; import org.bukkit.Location; import org.bukkit.World; @@ -27,7 +30,6 @@ import org.bukkit.permissions.PermissionDefault; import java.util.ArrayList; import java.util.List; -import java.util.logging.Level; /** * Used to teleport players. @@ -40,7 +42,7 @@ public class TeleportCommand extends MultiverseCommand { Permission menu = new Permission("multiverse.teleport.*", "Allows you to display the teleport menu.", PermissionDefault.OP); this.setName("Teleport"); - this.setCommandUsage("/mv tp " + ChatColor.GOLD + "[PLAYER]" + ChatColor.GREEN + " {WORLD}"); + this.setCommandUsage("/mv tp " + ChatColor.GOLD + "[PLAYER]" + ChatColor.GREEN + " {DESTINATION}"); this.setArgRange(1, 2); this.addKey("mvtp"); this.addKey("mv tp"); @@ -58,7 +60,7 @@ public class TeleportCommand extends MultiverseCommand { String destinationName; if (args.size() == 2) { - teleportee = this.plugin.getServer().getPlayer(args.get(0)); + teleportee = PlayerFinder.get(args.get(0), sender); if (teleportee == null) { this.messaging.sendMessage(sender, String.format("Sorry, I couldn't find player: %s%s", ChatColor.GOLD, args.get(0)), false); @@ -74,27 +76,14 @@ public class TeleportCommand extends MultiverseCommand { } teleportee = (Player) sender; } - // Special case for cannons: - if (destinationName.matches("(?i)cannon-[\\d]+(\\.[\\d]+)?")) { - String[] cannonSpeed = destinationName.split("-"); - try { - double speed = Double.parseDouble(cannonSpeed[1]); - destinationName = "ca:" + teleportee.getWorld().getName() + ":" + teleportee.getLocation().getX() - + "," + teleportee.getLocation().getY() + "," + teleportee.getLocation().getZ() + ":" - + teleportee.getLocation().getPitch() + ":" + teleportee.getLocation().getYaw() + ":" + speed; - } catch (Exception e) { - destinationName = "i:invalid"; - } - } DestinationFactory df = this.plugin.getDestFactory(); - MVDestination d = df.getDestination(destinationName); - + MVDestination d = df.getPlayerAwareDestination(teleportee, destinationName); MVTeleportEvent teleportEvent = new MVTeleportEvent(d, teleportee, teleporter, true); this.plugin.getServer().getPluginManager().callEvent(teleportEvent); if (teleportEvent.isCancelled()) { - this.plugin.log(Level.FINE, "Someone else cancelled the MVTeleport Event!!!"); + Logging.fine("Someone else cancelled the MVTeleport Event!!!"); return; } @@ -162,26 +151,29 @@ public class TeleportCommand extends MultiverseCommand { ((CustomTeleporterDestination)d).getTeleporter() : this.playerTeleporter; TeleportResult result = teleportObject.teleport(teleporter, teleportee, d); if (result == TeleportResult.FAIL_UNSAFE) { - this.plugin.log(Level.FINE, "Could not teleport " + teleportee.getName() + Logging.fine("Could not teleport " + teleportee.getName() + " to " + plugin.getLocationManipulation().strCoordsRaw(d.getLocation(teleportee))); - this.plugin.log(Level.FINE, "Queueing Command"); - Class[] paramTypes = { CommandSender.class, Player.class, Location.class }; - List items = new ArrayList(); - items.add(teleporter); - items.add(teleportee); - items.add(d.getLocation(teleportee)); + String player = "you"; if (!teleportee.equals(teleporter)) { player = teleportee.getName(); } - String message = String.format("%sMultiverse %sdid not teleport %s%s %sto %s%s %sbecause it was unsafe.", - ChatColor.GREEN, ChatColor.WHITE, ChatColor.AQUA, player, ChatColor.WHITE, ChatColor.DARK_AQUA, d.getName(), ChatColor.WHITE); - this.plugin.getCommandHandler().queueCommand(sender, "mvteleport", "teleportPlayer", items, - paramTypes, message, "Would you like to try anyway?", "", "", UNSAFE_TELEPORT_EXPIRE_DELAY); + + this.plugin.getCommandQueueManager().addToQueue(new QueuedCommand( + sender, + doUnsafeTeleport(teleporter, teleportee, d.getLocation(teleportee)), + String.format("%sMultiverse %sdid not teleport %s%s %sto %s%s %sbecause it was unsafe. Would you like to try anyway?", + ChatColor.GREEN, ChatColor.WHITE, ChatColor.AQUA, player, ChatColor.WHITE, ChatColor.DARK_AQUA, d.getName(), ChatColor.WHITE), + UNSAFE_TELEPORT_EXPIRE_DELAY + )); } // else: Player was teleported successfully (or the tp event was fired I should say) } + private Runnable doUnsafeTeleport(CommandSender teleporter, Player player, Location location) { + return () -> this.plugin.getSafeTTeleporter().safelyTeleport(teleporter, player, location, false); + } + private boolean checkSendPermissions(CommandSender teleporter, Player teleportee, MVDestination destination) { if (teleporter.equals(teleportee)) { if (!this.plugin.getMVPerms().hasPermission(teleporter, "multiverse.teleport.self." + destination.getIdentifier(), true)) { diff --git a/src/main/java/com/onarandombox/MultiverseCore/commands/VersionCommand.java b/src/main/java/com/onarandombox/MultiverseCore/commands/VersionCommand.java index b2ac51e4..f032fd20 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/commands/VersionCommand.java +++ b/src/main/java/com/onarandombox/MultiverseCore/commands/VersionCommand.java @@ -10,12 +10,13 @@ package com.onarandombox.MultiverseCore.commands; import com.dumptruckman.minecraft.util.Logging; import com.onarandombox.MultiverseCore.MultiverseCore; import com.onarandombox.MultiverseCore.event.MVVersionEvent; -import com.onarandombox.MultiverseCore.utils.webpaste.BitlyURLShortener; import com.onarandombox.MultiverseCore.utils.webpaste.PasteFailedException; import com.onarandombox.MultiverseCore.utils.webpaste.PasteService; import com.onarandombox.MultiverseCore.utils.webpaste.PasteServiceFactory; import com.onarandombox.MultiverseCore.utils.webpaste.PasteServiceType; import com.onarandombox.MultiverseCore.utils.webpaste.URLShortener; +import com.onarandombox.MultiverseCore.utils.webpaste.URLShortenerFactory; +import com.onarandombox.MultiverseCore.utils.webpaste.URLShortenerType; import com.pneumaticraft.commandhandler.CommandHandler; import org.apache.commons.lang.StringUtils; import org.bukkit.ChatColor; @@ -24,10 +25,8 @@ import org.bukkit.command.ConsoleCommandSender; import org.bukkit.entity.Player; import org.bukkit.permissions.PermissionDefault; import org.bukkit.scheduler.BukkitRunnable; -import org.bukkit.util.StringUtil; -import java.io.*; -import java.util.HashMap; +import java.io.File; import java.util.List; import java.util.Map; @@ -35,110 +34,95 @@ import java.util.Map; * Dumps version info to the console. */ public class VersionCommand extends MultiverseCommand { - private static final URLShortener SHORTENER = new BitlyURLShortener(); + private static final URLShortener SHORTENER = URLShortenerFactory.getService(URLShortenerType.BITLY); public VersionCommand(MultiverseCore plugin) { super(plugin); this.setName("Multiverse Version"); - this.setCommandUsage("/mv version " + ChatColor.GOLD + "-[bh] [--include-plugin-list]"); + this.setCommandUsage("/mv version " + ChatColor.GOLD + "[-b|-h|-p] [--include-plugin-list]"); this.setArgRange(0, 2); this.addKey("mv version"); + this.addKey("mvver"); this.addKey("mvv"); this.addKey("mvversion"); - this.setPermission("multiverse.core.version", - "Dumps version info to the console, optionally to pastie.org with -p or pastebin.com with a -b.", PermissionDefault.TRUE); + this.setPermission( + "multiverse.core.version", + "Dumps version info to the console, optionally to pastebin.com with -b, to hastebin.com using -h, or to paste.gg with -p.", + PermissionDefault.OP) + ; } private String getLegacyString() { - StringBuilder legacyFile = new StringBuilder(); - legacyFile.append("[Multiverse-Core] Multiverse-Core Version: ").append(this.plugin.getDescription().getVersion()).append('\n'); - legacyFile.append("[Multiverse-Core] Bukkit Version: ").append(this.plugin.getServer().getVersion()).append('\n'); - legacyFile.append("[Multiverse-Core] Loaded Worlds: ").append(this.plugin.getMVWorldManager().getMVWorlds()).append('\n'); - legacyFile.append("[Multiverse-Core] Multiverse Plugins Loaded: ").append(this.plugin.getPluginCount()).append('\n'); - legacyFile.append("[Multiverse-Core] Economy being used: ").append(plugin.getEconomist().getEconomyName()).append('\n'); - legacyFile.append("[Multiverse-Core] Permissions Plugin: ").append(this.plugin.getMVPerms().getType()).append('\n'); - legacyFile.append("[Multiverse-Core] Dumping Config Values: (version ") - .append(this.plugin.getMVConfig().getVersion()).append(")").append('\n'); - legacyFile.append("[Multiverse-Core] messagecooldown: ").append(plugin.getMessaging().getCooldown()).append('\n'); - legacyFile.append("[Multiverse-Core] teleportcooldown: ").append(plugin.getMVConfig().getTeleportCooldown()).append('\n'); - legacyFile.append("[Multiverse-Core] worldnameprefix: ").append(plugin.getMVConfig().getPrefixChat()).append('\n'); - legacyFile.append("[Multiverse-Core] worldnameprefixFormat: ").append(plugin.getMVConfig().getPrefixChatFormat()).append('\n'); - legacyFile.append("[Multiverse-Core] enforceaccess: ").append(plugin.getMVConfig().getEnforceAccess()).append('\n'); - legacyFile.append("[Multiverse-Core] displaypermerrors: ").append(plugin.getMVConfig().getDisplayPermErrors()).append('\n'); - legacyFile.append("[Multiverse-Core] teleportintercept: ").append(plugin.getMVConfig().getTeleportIntercept()).append('\n'); - legacyFile.append("[Multiverse-Core] firstspawnoverride: ").append(plugin.getMVConfig().getFirstSpawnOverride()).append('\n'); - legacyFile.append("[Multiverse-Core] firstspawnworld: ").append(plugin.getMVConfig().getFirstSpawnWorld()).append('\n'); - legacyFile.append("[Multiverse-Core] debug: ").append(plugin.getMVConfig().getGlobalDebug()).append('\n'); - legacyFile.append("[Multiverse-Core] Special Code: FRN002").append('\n'); - return legacyFile.toString(); + return "[Multiverse-Core] Multiverse-Core Version: " + this.plugin.getDescription().getVersion() + '\n' + + "[Multiverse-Core] Bukkit Version: " + this.plugin.getServer().getVersion() + '\n' + + "[Multiverse-Core] Loaded Worlds: " + this.plugin.getMVWorldManager().getMVWorlds() + '\n' + + "[Multiverse-Core] Multiverse Plugins Loaded: " + this.plugin.getPluginCount() + '\n' + +"[Multiverse-Core] Economy being used: " + plugin.getEconomist().getEconomyName() + '\n' + + "[Multiverse-Core] Permissions Plugin: " + this.plugin.getMVPerms().getType() + '\n' + + "[Multiverse-Core] Dumping Config Values: (version " + this.plugin.getMVConfig().getVersion() + ")" + '\n' + + "[Multiverse-Core] enforceaccess: " + plugin.getMVConfig().getEnforceAccess() + '\n' + + "[Multiverse-Core] prefixchat: " + plugin.getMVConfig().getPrefixChat() + '\n' + + "[Multiverse-Core] prefixchatformat: " + plugin.getMVConfig().getPrefixChatFormat() + '\n' + + "[Multiverse-Core] useasyncchat: " + plugin.getMVConfig().getUseAsyncChat() + '\n' + + "[Multiverse-Core] teleportintercept: " + plugin.getMVConfig().getTeleportIntercept() + '\n' + + "[Multiverse-Core] firstspawnoverride: " + plugin.getMVConfig().getFirstSpawnOverride() + '\n' + + "[Multiverse-Core] displaypermerrors: " + plugin.getMVConfig().getDisplayPermErrors() + '\n' + + "[Multiverse-Core] enablebuscript: " + plugin.getMVConfig().getEnableBuscript() + '\n' + + "[Multiverse-Core] globaldebug: " + plugin.getMVConfig().getGlobalDebug() + '\n' + + "[Multiverse-Core] silentstart: " + plugin.getMVConfig().getSilentStart() + '\n' + + "[Multiverse-Core] messagecooldown: " + plugin.getMessaging().getCooldown() + '\n' + + "[Multiverse-Core] version: " + plugin.getMVConfig().getVersion() + '\n' + + "[Multiverse-Core] firstspawnworld: " + plugin.getMVConfig().getFirstSpawnWorld() + '\n' + + "[Multiverse-Core] teleportcooldown: " + plugin.getMVConfig().getTeleportCooldown() + '\n' + + "[Multiverse-Core] defaultportalsearch: " + plugin.getMVConfig().isUsingDefaultPortalSearch() + '\n' + + "[Multiverse-Core] portalsearchradius: " + plugin.getMVConfig().getPortalSearchRadius() + '\n' + + "[Multiverse-Core] autopurge: " + plugin.getMVConfig().isAutoPurgeEnabled() + '\n' + + "[Multiverse-Core] Special Code: FRN002" + '\n'; } private String getMarkdownString() { - StringBuilder markdownString = new StringBuilder(); - markdownString.append("# Multiverse-Core\n"); - markdownString.append("## Overview\n"); - markdownString.append("| Name | Value |\n"); - markdownString.append("| --- | --- |\n"); - markdownString.append("| Multiverse-Core Version | `").append(this.plugin.getDescription().getVersion()).append("` |\n"); - markdownString.append("| Bukkit Version | `").append(this.plugin.getServer().getVersion()).append("` |\n"); - //markdownString.append("| Loaded Worlds | `").append(this.plugin.getMVWorldManager().getMVWorlds()).append("` |\n"); - markdownString.append("| Multiverse Plugins Loaded | `").append(this.plugin.getPluginCount()).append("` |\n"); - markdownString.append("| Economy being used | `").append(plugin.getEconomist().getEconomyName()).append("` |\n"); - markdownString.append("| Permissions Plugin | `").append(this.plugin.getMVPerms().getType()).append("` |\n"); - markdownString.append("## Parsed Config\n"); - markdownString.append("These are what Multiverse thought the in-memory values of the config were.\n\n"); - markdownString.append("| Config Key | Value |\n"); - markdownString.append("| --- | --- |\n"); - markdownString.append("| version | `").append(this.plugin.getMVConfig().getVersion()).append("` |\n"); - markdownString.append("| messagecooldown | `").append(plugin.getMessaging().getCooldown()).append("` |\n"); - markdownString.append("| teleportcooldown | `").append(plugin.getMVConfig().getTeleportCooldown()).append("` |\n"); - markdownString.append("| worldnameprefix | `").append(plugin.getMVConfig().getPrefixChat()).append("` |\n"); - markdownString.append("| worldnameprefixFormat | `").append(plugin.getMVConfig().getPrefixChatFormat()).append("` |\n"); - markdownString.append("| enforceaccess | `").append(plugin.getMVConfig().getEnforceAccess()).append("` |\n"); - markdownString.append("| displaypermerrors | `").append(plugin.getMVConfig().getDisplayPermErrors()).append("` |\n"); - markdownString.append("| teleportintercept | `").append(plugin.getMVConfig().getTeleportIntercept()).append("` |\n"); - markdownString.append("| firstspawnoverride | `").append(plugin.getMVConfig().getFirstSpawnOverride()).append("` |\n"); - markdownString.append("| firstspawnworld | `").append(plugin.getMVConfig().getFirstSpawnWorld()).append("` |\n"); - markdownString.append("| debug | `").append(plugin.getMVConfig().getGlobalDebug()).append("` |\n"); - return markdownString.toString(); + return "# Multiverse-Core" + '\n' + + "## Overview" + '\n' + + "| Name | Value |" + '\n' + + "| --- | --- |" + '\n' + + "| Multiverse-Core Version | `" + this.plugin.getDescription().getVersion() + "` |" + '\n' + + "| Bukkit Version | `" + this.plugin.getServer().getVersion() + "` |" + '\n' + + "| Loaded Worlds | `" + this.plugin.getMVWorldManager().getMVWorlds() + "` |" + '\n' + + "| Multiverse Plugins Loaded | `" + this.plugin.getPluginCount() + "` |" + '\n' + + "| Economy being used | `" + plugin.getEconomist().getEconomyName() + "` |" + '\n' + + "| Permissions Plugin | `" + this.plugin.getMVPerms().getType() + "` |" + '\n' + + "## Parsed Config" + '\n' + + "These are what Multiverse thought the in-memory values of the config were." + "\n\n" + + "| Config Key | Value |" + '\n' + + "| --- | --- |" + '\n' + + "| version | `" + this.plugin.getMVConfig().getVersion() + "` |" + '\n' + + "| messagecooldown | `" + plugin.getMessaging().getCooldown() + "` |" + '\n' + + "| teleportcooldown | `" + plugin.getMVConfig().getTeleportCooldown() + "` |" + '\n' + + "| worldnameprefix | `" + plugin.getMVConfig().getPrefixChat() + "` |" + '\n' + + "| worldnameprefixFormat | `" + plugin.getMVConfig().getPrefixChatFormat() + "` |" + '\n' + + "| enforceaccess | `" + plugin.getMVConfig().getEnforceAccess() + "` |" + '\n' + + "| displaypermerrors | `" + plugin.getMVConfig().getDisplayPermErrors() + "` |" + '\n' + + "| teleportintercept | `" + plugin.getMVConfig().getTeleportIntercept() + "` |" + '\n' + + "| firstspawnoverride | `" + plugin.getMVConfig().getFirstSpawnOverride() + "` |" + '\n' + + "| firstspawnworld | `" + plugin.getMVConfig().getFirstSpawnWorld() + "` |" + '\n' + + "| debug | `" + plugin.getMVConfig().getGlobalDebug() + "` |" + '\n'; } - private String readFile(final String filename) { - String result; - try { - FileReader reader = new FileReader(filename); - BufferedReader bufferedReader = new BufferedReader(reader); - String line; - result = ""; - while ((line = bufferedReader.readLine()) != null) { - result += line + '\n'; - } - } catch (FileNotFoundException e) { - Logging.severe("Unable to find %s. Here's the traceback: %s", filename, e.getMessage()); - e.printStackTrace(); - result = String.format("ERROR: Could not load: %s", filename); - } catch (IOException e) { - Logging.severe("Something bad happend when reading %s. Here's the traceback: %s", filename, e.getMessage()); - e.printStackTrace(); - result = String.format("ERROR: Could not load: %s", filename); - } - return result; - } + private void addVersionInfoToEvent(MVVersionEvent event) { + // add the legacy version info + event.appendVersionInfo(this.getLegacyString()); - private Map getVersionFiles() { - Map files = new HashMap(); + // add the legacy file, but as markdown so it's readable + // TODO Readd this in 5.0.0 + // event.putDetailedVersionInfo("version.md", this.getMarkdownString()); - // Add the legacy file, but as markdown so it's readable - files.put("version.md", this.getMarkdownString()); - - // Add the config.yml + // add config.yml File configFile = new File(this.plugin.getDataFolder(), "config.yml"); - files.put(configFile.getName(), this.readFile(configFile.getAbsolutePath())); + event.putDetailedVersionInfo("multiverse-core/config.yml", configFile); - // Add the config.yml - File worldConfig = new File(this.plugin.getDataFolder(), "worlds.yml"); - files.put(worldConfig.getName(), this.readFile(worldConfig.getAbsolutePath())); - return files; + // add worlds.yml + File worldsFile = new File(this.plugin.getDataFolder(), "worlds.yml"); + event.putDetailedVersionInfo("multiverse-core/worlds.yml", worldsFile); } @Override @@ -148,23 +132,26 @@ public class VersionCommand extends MultiverseCommand { sender.sendMessage("Version info dumped to console. Please check your server logs."); } - MVVersionEvent versionEvent = new MVVersionEvent(this.getLegacyString(), this.getVersionFiles()); - final Map files = this.getVersionFiles(); + MVVersionEvent versionEvent = new MVVersionEvent(); + + this.addVersionInfoToEvent(versionEvent); this.plugin.getServer().getPluginManager().callEvent(versionEvent); - String versionInfo = versionEvent.getVersionInfo(); - if (CommandHandler.hasFlag("--include-plugin-list", args)) { - versionInfo = versionInfo + "\nPlugins: " + getPluginList(); + versionEvent.appendVersionInfo('\n' + "Plugins: " + getPluginList()); + versionEvent.putDetailedVersionInfo("plugins.txt", "Plugins: " + getPluginList()); } - final String data = versionInfo; + final String versionInfo = versionEvent.getVersionInfo(); + versionEvent.putDetailedVersionInfo("version.txt", versionInfo); + + final Map files = versionEvent.getDetailedVersionInfo(); // log to console - String[] lines = data.split("\n"); + String[] lines = versionInfo.split("\\r?\\n"); for (String line : lines) { if (!line.isEmpty()) { - Logging.info(line); + this.plugin.getServer().getLogger().info(line); } } @@ -175,10 +162,16 @@ public class VersionCommand extends MultiverseCommand { String pasteUrl; if (CommandHandler.hasFlag("-b", args)) { // private post to pastebin - pasteUrl = postToService(PasteServiceType.PASTEBIN, true, data, files); + pasteUrl = postToService(PasteServiceType.PASTEBIN, true, versionInfo, files); + } else if (CommandHandler.hasFlag("-g", args)) { + // private post to github + pasteUrl = postToService(PasteServiceType.GITHUB, true, versionInfo, files); } else if (CommandHandler.hasFlag("-h", args)) { - // private post to pastebin - pasteUrl = postToService(PasteServiceType.HASTEBIN, true, data, files); + // private post to hastebin + pasteUrl = postToService(PasteServiceType.HASTEBIN, true, versionInfo, files); + } else if (CommandHandler.hasFlag("-p", args)) { + // private post to paste.gg + pasteUrl = postToService(PasteServiceType.PASTEGG, true, versionInfo, files); } else { return; } @@ -204,20 +197,25 @@ public class VersionCommand extends MultiverseCommand { * @param pasteFiles Map of filenames/contents of debug info. * @return URL of visible paste */ - private static String postToService(PasteServiceType type, boolean isPrivate, String pasteData, - Map pasteFiles) { + private static String postToService(PasteServiceType type, boolean isPrivate, String pasteData, Map pasteFiles) { PasteService ps = PasteServiceFactory.getService(type, isPrivate); + try { String result; if (ps.supportsMultiFile()) { - result = ps.postData(ps.encodeData(pasteFiles), ps.getPostURL()); + result = ps.postData(pasteFiles); } else { - result = ps.postData(ps.encodeData(pasteData), ps.getPostURL()); + result = ps.postData(pasteData); } - return SHORTENER.shorten(result); + + if (SHORTENER != null) return SHORTENER.shorten(result); + return result; } catch (PasteFailedException e) { - System.out.print(e); - return "Error posting to service"; + e.printStackTrace(); + return "Error posting to service."; + } catch (NullPointerException e) { + e.printStackTrace(); + return "That service isn't supported yet."; } } diff --git a/src/main/java/com/onarandombox/MultiverseCore/commandtools/queue/CommandQueueManager.java b/src/main/java/com/onarandombox/MultiverseCore/commandtools/queue/CommandQueueManager.java new file mode 100644 index 00000000..8f6a3023 --- /dev/null +++ b/src/main/java/com/onarandombox/MultiverseCore/commandtools/queue/CommandQueueManager.java @@ -0,0 +1,161 @@ +/****************************************************************************** + * Multiverse 2 Copyright (c) the Multiverse Team 2020. * + * Multiverse 2 is licensed under the BSD License. * + * For more information please check the README.md file included * + * with this project. * + ******************************************************************************/ + +package com.onarandombox.MultiverseCore.commandtools.queue; + +import com.dumptruckman.minecraft.util.Logging; +import com.onarandombox.MultiverseCore.MultiverseCore; +import org.bukkit.Bukkit; +import org.bukkit.ChatColor; +import org.bukkit.block.data.type.CommandBlock; +import org.bukkit.command.BlockCommandSender; +import org.bukkit.command.CommandSender; +import org.bukkit.scheduler.BukkitTask; +import org.jetbrains.annotations.NotNull; + +import java.util.Map; +import java.util.WeakHashMap; + +/** + *

Manages the queuing of dangerous commands that require {@code /mv confirm} before executing.

+ * + *

Each sender can only have one command in queue at any given time. When a queued command is added + * for a sender that already has a command in queue, it will replace the old queued command.

+ */ +public class CommandQueueManager { + + private static final long TICKS_PER_SECOND = 20; + private static final DummyCommandBlockSender COMMAND_BLOCK = new DummyCommandBlockSender(); + + private final MultiverseCore plugin; + private final Map queuedCommandMap; + + public CommandQueueManager(@NotNull MultiverseCore plugin) { + this.plugin = plugin; + this.queuedCommandMap = new WeakHashMap<>(); + } + + /** + * Adds a {@link QueuedCommand} into queue. + * + * @param queuedCommand The queued command to add. + */ + public void addToQueue(QueuedCommand queuedCommand) { + CommandSender targetSender = parseSender(queuedCommand.getSender()); + + // Since only one command is stored in queue per sender, we remove the old one. + this.removeFromQueue(targetSender); + + Logging.finer("Add new command to queue for sender %s.", targetSender); + this.queuedCommandMap.put(targetSender, queuedCommand); + queuedCommand.setExpireTask(runExpireLater(queuedCommand)); + + queuedCommand.getSender().sendMessage(queuedCommand.getPrompt()); + queuedCommand.getSender().sendMessage(String.format("Run %s/mv confirm %sto continue. This will expire in %s seconds.", + ChatColor.GREEN, ChatColor.WHITE, queuedCommand.getValidDuration())); + } + + /** + * Expire task that removes a {@link QueuedCommand} from queue after valid duration defined. + * + * @param queuedCommand Command to run the expire task on. + * @return The expire {@link BukkitTask}. + */ + @NotNull + private BukkitTask runExpireLater(@NotNull QueuedCommand queuedCommand) { + return Bukkit.getScheduler().runTaskLater( + this.plugin, + expireRunnable(queuedCommand), + queuedCommand.getValidDuration() * TICKS_PER_SECOND + ); + } + + /** + * Runnable responsible for expiring the queued command. + * + * @param queuedCommand Command to create the expire task on. + * @return The expire runnable. + */ + @NotNull + private Runnable expireRunnable(@NotNull QueuedCommand queuedCommand) { + return () -> { + CommandSender targetSender = parseSender(queuedCommand.getSender()); + QueuedCommand matchingQueuedCommand = this.queuedCommandMap.get(targetSender); + if (!queuedCommand.equals(matchingQueuedCommand) || queuedCommand.getExpireTask().isCancelled()) { + // To be safe, but this shouldn't happen since we cancel old commands before add new once. + Logging.finer("This is an old queue command already."); + return; + } + queuedCommand.getSender().sendMessage("Your queued command has expired."); + this.queuedCommandMap.remove(queuedCommand.getSender()); + }; + } + + /** + * Runs the command in queue for the given sender, if any. + * + * @param sender {@link CommandSender} that confirmed the command. + * @return True if queued command ran successfully, else false. + */ + public boolean runQueuedCommand(@NotNull CommandSender sender) { + CommandSender targetSender = parseSender(sender); + QueuedCommand queuedCommand = this.queuedCommandMap.get(targetSender); + if (queuedCommand == null) { + sender.sendMessage(ChatColor.RED + "You do not have any commands in queue."); + return false; + } + Logging.finer("Running queued command..."); + queuedCommand.getAction().run(); + return removeFromQueue(targetSender); + } + + /** + * Since only one command is stored in queue per sender, we remove the old one. + * + * @param sender The {@link CommandSender} that executed the command. + * @return True if queue command is removed from sender successfully, else false. + */ + public boolean removeFromQueue(@NotNull CommandSender sender) { + CommandSender targetSender = parseSender(sender); + QueuedCommand previousCommand = this.queuedCommandMap.remove(targetSender); + if (previousCommand == null) { + Logging.finer("No queue command to remove for sender %s.", targetSender.getName()); + return false; + } + previousCommand.getExpireTask().cancel(); + Logging.finer("Removed queue command for sender %s.", targetSender.getName()); + return true; + } + + /** + * To allow all CommandBlocks to be a common sender with use of {@link DummyCommandBlockSender}. + * So confirm command can be used for a queued command on another command block. + * + * @param sender The sender to parse. + * @return The sender, or if its a command block, a {@link DummyCommandBlockSender}. + */ + @NotNull + private CommandSender parseSender(@NotNull CommandSender sender) { + Logging.fine(sender.getClass().getName()); + if (isCommandBlock(sender)) { + Logging.finer("Is command block."); + return COMMAND_BLOCK; + } + return sender; + } + + /** + * Checks if the sender is a command block. + * + * @param sender The sender to check. + * @return True if sender is a command block, else false. + */ + private boolean isCommandBlock(@NotNull CommandSender sender) { + return sender instanceof BlockCommandSender + && ((BlockCommandSender) sender).getBlock().getBlockData() instanceof CommandBlock; + } +} diff --git a/src/main/java/com/onarandombox/MultiverseCore/commandtools/queue/DummyCommandBlockSender.java b/src/main/java/com/onarandombox/MultiverseCore/commandtools/queue/DummyCommandBlockSender.java new file mode 100644 index 00000000..7ec3dfe0 --- /dev/null +++ b/src/main/java/com/onarandombox/MultiverseCore/commandtools/queue/DummyCommandBlockSender.java @@ -0,0 +1,104 @@ +package com.onarandombox.MultiverseCore.commandtools.queue; + +import org.bukkit.Bukkit; +import org.bukkit.Server; +import org.bukkit.command.CommandSender; +import org.bukkit.permissions.Permission; +import org.bukkit.permissions.PermissionAttachment; +import org.bukkit.permissions.PermissionAttachmentInfo; +import org.bukkit.plugin.Plugin; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.util.Set; + +/** + * Used by {@link CommandQueueManager}, so different commands block can be recognised as one. + */ +class DummyCommandBlockSender implements CommandSender { + + @Override + public void sendMessage(@NotNull String message) { + throw new UnsupportedOperationException(); + } + + @Override + public void sendMessage(@NotNull String[] messages) { + throw new UnsupportedOperationException(); + } + + @Override + public @NotNull Server getServer() { + return Bukkit.getServer(); + } + + @Override + public @NotNull String getName() { + return "DummyCommandBlockSender"; + } + + @Override + public boolean isPermissionSet(@NotNull String name) { + throw new UnsupportedOperationException(); + } + + @Override + public boolean isPermissionSet(@NotNull Permission perm) { + throw new UnsupportedOperationException(); + } + + @Override + public boolean hasPermission(@NotNull String name) { + throw new UnsupportedOperationException(); + } + + @Override + public boolean hasPermission(@NotNull Permission perm) { + throw new UnsupportedOperationException(); + } + + @Override + public @NotNull PermissionAttachment addAttachment(@NotNull Plugin plugin, @NotNull String name, boolean value) { + throw new UnsupportedOperationException(); + } + + @Override + public @NotNull PermissionAttachment addAttachment(@NotNull Plugin plugin) { + throw new UnsupportedOperationException(); + } + + @Override + public @Nullable PermissionAttachment addAttachment(@NotNull Plugin plugin, @NotNull String name, boolean value, int ticks) { + throw new UnsupportedOperationException(); + } + + @Override + public @Nullable PermissionAttachment addAttachment(@NotNull Plugin plugin, int ticks) { + throw new UnsupportedOperationException(); + } + + @Override + public void removeAttachment(@NotNull PermissionAttachment attachment) { + throw new UnsupportedOperationException(); + } + + @Override + public void recalculatePermissions() { + throw new UnsupportedOperationException(); + } + + @Override + public @NotNull Set getEffectivePermissions() { + throw new UnsupportedOperationException(); + } + + @Override + public boolean isOp() { + throw new UnsupportedOperationException(); + } + + @Override + public void setOp(boolean value) { + throw new UnsupportedOperationException(); + } +} diff --git a/src/main/java/com/onarandombox/MultiverseCore/commandtools/queue/QueuedCommand.java b/src/main/java/com/onarandombox/MultiverseCore/commandtools/queue/QueuedCommand.java new file mode 100644 index 00000000..5dedc99f --- /dev/null +++ b/src/main/java/com/onarandombox/MultiverseCore/commandtools/queue/QueuedCommand.java @@ -0,0 +1,86 @@ +/****************************************************************************** + * Multiverse 2 Copyright (c) the Multiverse Team 2020. * + * Multiverse 2 is licensed under the BSD License. * + * For more information please check the README.md file included * + * with this project. * + ******************************************************************************/ + +package com.onarandombox.MultiverseCore.commandtools.queue; + +import org.bukkit.command.CommandSender; +import org.bukkit.scheduler.BukkitTask; +import org.jetbrains.annotations.NotNull; + +/** + * Represents a single command used in {@link CommandQueueManager} for confirming before running potentially + * dangerous action. + */ +public class QueuedCommand { + + private static final String DEFAULT_PROMPT_MESSAGE = "The command you are trying to run is deemed dangerous."; + private static final int DEFAULT_VALID_TIME = 10; + + private final CommandSender sender; + private final Runnable action; + private final String prompt; + private final int validDuration; + private BukkitTask expireTask; + + public QueuedCommand(CommandSender sender, Runnable action) { + this(sender, action, DEFAULT_PROMPT_MESSAGE, DEFAULT_VALID_TIME); + } + + public QueuedCommand(CommandSender sender, Runnable action, String prompt) { + this(sender, action, prompt, DEFAULT_VALID_TIME); + } + + public QueuedCommand(CommandSender sender, Runnable action, int validDuration) { + this(sender, action, DEFAULT_PROMPT_MESSAGE, validDuration); + } + + /** + * Creates a new queue command, to be registered at {@link CommandQueueManager#addToQueue(QueuedCommand)}. + * + * @param sender The sender that ran the command needed for confirmation. + * @param action The logic to execute upon confirming. + * @param prompt Question to ask sender to confirm. + * @param validDuration Duration in which the command is valid for confirm in seconds. + */ + public QueuedCommand(CommandSender sender, Runnable action, String prompt, int validDuration) { + this.sender = sender; + this.action = action; + this.prompt = prompt; + this.validDuration = validDuration; + } + + @NotNull + CommandSender getSender() { + return sender; + } + + @NotNull + String getPrompt() { + return prompt; + } + + int getValidDuration() { + return validDuration; + } + + @NotNull + Runnable getAction() { + return action; + } + + @NotNull + BukkitTask getExpireTask() { + return expireTask; + } + + void setExpireTask(@NotNull BukkitTask expireTask) { + if (this.expireTask != null) { + throw new IllegalStateException("This queue command already has an expire task. You can't register twice!"); + } + this.expireTask = expireTask; + } +} diff --git a/src/main/java/com/onarandombox/MultiverseCore/commandtools/queue/package-info.java b/src/main/java/com/onarandombox/MultiverseCore/commandtools/queue/package-info.java new file mode 100644 index 00000000..075ff0df --- /dev/null +++ b/src/main/java/com/onarandombox/MultiverseCore/commandtools/queue/package-info.java @@ -0,0 +1,4 @@ +/** + * Manager queuing of dangerous commands in need of confirmation. + */ +package com.onarandombox.MultiverseCore.commandtools.queue; \ No newline at end of file diff --git a/src/main/java/com/onarandombox/MultiverseCore/configuration/EntryFee.java b/src/main/java/com/onarandombox/MultiverseCore/configuration/EntryFee.java index be66fb3a..3fff6fe3 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/configuration/EntryFee.java +++ b/src/main/java/com/onarandombox/MultiverseCore/configuration/EntryFee.java @@ -22,6 +22,8 @@ public class EntryFee extends SerializationConfig { @Nullable private Material currency; + private final Material DISABLED_MATERIAL = Material.AIR; + public EntryFee() { super(); } @@ -51,6 +53,9 @@ public class EntryFee extends SerializationConfig { */ @Nullable public Material getCurrency() { + if (currency == null || currency.equals(DISABLED_MATERIAL)) { + return null; + } return currency; } diff --git a/src/main/java/com/onarandombox/MultiverseCore/destination/BedDestination.java b/src/main/java/com/onarandombox/MultiverseCore/destination/BedDestination.java index 70568cf5..19b6ad0a 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/destination/BedDestination.java +++ b/src/main/java/com/onarandombox/MultiverseCore/destination/BedDestination.java @@ -23,7 +23,7 @@ import org.bukkit.util.Vector; * A bed-{@link MVDestination}. */ public class BedDestination implements MVDestination { - public static final String OLD_BED_STRING = "b:playerbed"; + public static final String OWN_BED_STRING = "playerbed"; private String playername = ""; private boolean isValid; private Location knownBedLoc; @@ -46,11 +46,11 @@ public class BedDestination implements MVDestination { boolean validFormat = split.length >= 1 && split.length <= 2 && split[0].equals(this.getIdentifier()); OfflinePlayer p = Bukkit.getOfflinePlayer(split[1]); - boolean validPlayer = (p != null); + boolean validPlayer = p.getName() != null && !p.getName().equals(OWN_BED_STRING); if (validFormat && validPlayer) this.playername = p.getName(); - this.isValid = destination.equals(OLD_BED_STRING) || (validFormat && validPlayer); + this.isValid = destination.equals("b:" + OWN_BED_STRING) || (validFormat && validPlayer); return this.isValid; } @@ -136,6 +136,6 @@ public class BedDestination implements MVDestination { @Override public String toString() { - return playername.isEmpty() ? OLD_BED_STRING : ("b:" + playername); + return "b:" + (playername.isEmpty() ? OWN_BED_STRING : playername); } } diff --git a/src/main/java/com/onarandombox/MultiverseCore/destination/DestinationFactory.java b/src/main/java/com/onarandombox/MultiverseCore/destination/DestinationFactory.java index 2b80e153..402342c3 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/destination/DestinationFactory.java +++ b/src/main/java/com/onarandombox/MultiverseCore/destination/DestinationFactory.java @@ -11,16 +11,29 @@ import com.onarandombox.MultiverseCore.MultiverseCore; import com.onarandombox.MultiverseCore.api.MVDestination; import com.onarandombox.MultiverseCore.commands.TeleportCommand; import com.onarandombox.MultiverseCore.utils.PermissionTools; +import com.onarandombox.MultiverseCore.utils.PlayerFinder; import com.pneumaticraft.commandhandler.Command; +import org.bukkit.Bukkit; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; import org.bukkit.permissions.Permission; import org.bukkit.permissions.PermissionDefault; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +import java.util.Collection; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.regex.Pattern; -/** A factory class that will create destinations from specific strings. */ +/** + * A factory class that will create destinations from specific strings. + */ public class DestinationFactory { + + private static final Pattern CANNON_PATTERN = Pattern.compile("(?i)cannon-[\\d]+(\\.[\\d]+)?"); + private MultiverseCore plugin; private Map> destList; private Command teleportCommand; @@ -36,6 +49,59 @@ public class DestinationFactory { } } + /** + * Parse a destination that has relation to sender, such as a cannon or player destination. + * + * @param teleportee The player that is going to be teleported. + * @param destinationName The destination to parse. + * @return A non-null MVDestination + */ + @NotNull + public MVDestination getPlayerAwareDestination(@NotNull Player teleportee, + @NotNull String destinationName) { + + // Prioritise world, in the event that a world is named after a player online. + if (Bukkit.getWorld(destinationName) != null) { + return getDestination(destinationName); + } + + Player targetPlayer = PlayerFinder.get(destinationName, teleportee); + if (targetPlayer != null) { + return getDestination("pl:" + targetPlayer.getName()); + } + + if (CANNON_PATTERN.matcher(destinationName).matches()) { + return getDestination(parseCannonDest(teleportee, destinationName)); + } + + return getDestination(destinationName); + } + + /** + * Parses a cannon destination. + * + * @param teleportee The player that is going to be teleported. + * @param destinationName The destination to parse. + * @return A destination string. + */ + @NotNull + private String parseCannonDest(@NotNull Player teleportee, + @NotNull String destinationName) { + + String[] cannonSpeed = destinationName.split("-"); + try { + double speed = Double.parseDouble(cannonSpeed[1]); + destinationName = "ca:" + teleportee.getWorld().getName() + ":" + teleportee.getLocation().getX() + + "," + teleportee.getLocation().getY() + "," + teleportee.getLocation().getZ() + ":" + + teleportee.getLocation().getPitch() + ":" + teleportee.getLocation().getYaw() + ":" + speed; + } + catch (Exception e) { + destinationName = "i:invalid"; + } + + return destinationName; + } + /** * Gets a new destination from a string. * Returns a new InvalidDestination if the string could not be parsed. @@ -101,4 +167,13 @@ public class DestinationFactory { this.teleportCommand.addAdditonalPermission(other); return true; } + + /** + * Gets all the {@link MVDestination} identifiers registered. + * + * @return A collection of destination identifiers. + */ + public Collection getRegisteredIdentifiers() { + return this.destList.keySet(); + } } diff --git a/src/main/java/com/onarandombox/MultiverseCore/destination/PlayerDestination.java b/src/main/java/com/onarandombox/MultiverseCore/destination/PlayerDestination.java index fb71c32c..0ca08f98 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/destination/PlayerDestination.java +++ b/src/main/java/com/onarandombox/MultiverseCore/destination/PlayerDestination.java @@ -50,7 +50,7 @@ public class PlayerDestination implements MVDestination { */ @Override public Location getLocation(Entity e) { - Player p = plugin.getServer().getPlayer(this.player); + Player p = plugin.getServer().getPlayerExact(this.player); Player plLoc = null; if (e instanceof Player) { plLoc = (Player) e; diff --git a/src/main/java/com/onarandombox/MultiverseCore/display/ColorAlternator.java b/src/main/java/com/onarandombox/MultiverseCore/display/ColorAlternator.java new file mode 100644 index 00000000..561394a9 --- /dev/null +++ b/src/main/java/com/onarandombox/MultiverseCore/display/ColorAlternator.java @@ -0,0 +1,62 @@ +package com.onarandombox.MultiverseCore.display; + +import org.bukkit.ChatColor; +import org.jetbrains.annotations.NotNull; + +/** + * Helper class to switch between 2 {@link ChatColor}. + */ +public class ColorAlternator implements ColorTool { + + /** + * Creates a new {@link ColorAlternator} with 2 {@link ChatColor}s. + * + * @param colorThis The first color. + * @param colorThat The second color. + * @return The {@link ColorAlternator} created for you. + */ + public static ColorAlternator with(@NotNull ChatColor colorThis, + @NotNull ChatColor colorThat) { + + return new ColorAlternator(colorThis, colorThat); + } + + private boolean switcher; + private final ChatColor thisColor; + private final ChatColor thatColor; + + /** + * @param colorThis The first color. + * @param colorThat The second color. + */ + public ColorAlternator(@NotNull ChatColor colorThis, + @NotNull ChatColor colorThat) { + + this.thisColor = colorThis; + this.thatColor = colorThat; + } + + /** + * Gets the color. Everytime this method is called, it swaps the color that it returns. + * + * @return The color. + */ + @Override + public ChatColor get() { + return (this.switcher ^= true) ? this.thisColor : this.thatColor; + } + + /** + * @return The first color. + */ + public ChatColor getThisColor() { + return thisColor; + } + + /** + * @return The second color. + */ + public ChatColor getThatColor() { + return thatColor; + } +} diff --git a/src/main/java/com/onarandombox/MultiverseCore/display/ColorTool.java b/src/main/java/com/onarandombox/MultiverseCore/display/ColorTool.java new file mode 100644 index 00000000..1c4fae67 --- /dev/null +++ b/src/main/java/com/onarandombox/MultiverseCore/display/ColorTool.java @@ -0,0 +1,22 @@ +package com.onarandombox.MultiverseCore.display; + +import org.bukkit.ChatColor; + +/** + * Tools to allow customisation. + */ +@FunctionalInterface +public interface ColorTool { + + /** + * Gets a chat color. + * + * @return The color. + */ + ChatColor get(); + + /** + * Default implementation of this interface. Returns a default white color. + */ + ColorTool DEFAULT = () -> ChatColor.WHITE; +} diff --git a/src/main/java/com/onarandombox/MultiverseCore/display/ContentDisplay.java b/src/main/java/com/onarandombox/MultiverseCore/display/ContentDisplay.java new file mode 100644 index 00000000..c5d9e39c --- /dev/null +++ b/src/main/java/com/onarandombox/MultiverseCore/display/ContentDisplay.java @@ -0,0 +1,268 @@ +package com.onarandombox.MultiverseCore.display; + +import com.onarandombox.MultiverseCore.display.settings.DisplaySetting; +import org.bukkit.ChatColor; +import org.bukkit.command.CommandSender; +import org.jetbrains.annotations.NotNull; + +import java.util.Collection; +import java.util.Map; +import java.util.Objects; +import java.util.WeakHashMap; + +/** + * Helps to display contents such as list and maps in a nicely formatted fashion. + * + * @param Type of content to display. + */ +public class ContentDisplay { + + public static final String LINE_BREAK = "%br%"; + + /** + * Creates a ContentDisplay.Builder for the given content. + * + * @param content The content to be displayed. + * @param The type of the content which can be inferred. + * @return A new Builder. + */ + public static Builder forContent(T content) { + return new Builder<>(content); + } + + /** + * Creates a ContentDisplay.Builder for the given collection of content. + * + * @param content The content to be displayed. + * @return A new Builder. + */ + public static Builder> forContent(Collection content) { + return new Builder<>(content).displayHandler(DisplayHandlers.LIST); + } + + /** + * Creates a ContentDisplay.Builder for the given map of content. + * + * @param content The content to be displayed. + * @return A new Builder. + */ + public static Builder> forContent(Map content) { + return new Builder<>(content).displayHandler(DisplayHandlers.INLINE_MAP); + } + + private final T contents; + + private String header; + private String emptyMessage = "No matching content to display."; + private DisplayHandler displayHandler; + private ColorTool colorTool = ColorTool.DEFAULT; + private ContentFilter filter = ContentFilter.DEFAULT; + private final Map, Object> settingsMap = new WeakHashMap<>(); + + private ContentDisplay(T contents) { + this.contents = contents; + } + + /** + * Do the actual displaying of contents to the sender. + * + * @param sender The CommandSender to show the display to. + */ + public void show(@NotNull CommandSender sender) { + Collection formattedContent; + try { + formattedContent = (this.contents == null) ? null : this.displayHandler.format(sender, this); + } catch (DisplayFormatException e) { + sender.sendMessage(String.format("%sError: %s", ChatColor.RED, e.getMessage())); + return; + } + this.displayHandler.sendHeader(sender, this); + this.displayHandler.sendSubHeader(sender, this); + this.displayHandler.sendBody(sender, this, formattedContent); + } + + /** + * @return Gets the header to display. + */ + public String getHeader() { + return header; + } + + /** + * Sets the header text. + */ + public void setHeader(@NotNull String header) { + this.header = header; + } + + /** + * @return Gets the contents to display. + */ + public T getContents() { + return contents; + } + + /** + * @return Gets the message to display when no content is shown. + */ + @NotNull + public String getEmptyMessage() { + return emptyMessage; + } + + /** + * @return Gets the display handler that formats and sends content to sender. + */ + @NotNull + public DisplayHandler getDisplayHandler() { + return displayHandler; + } + + /** + * @return Gets the color tool used. + */ + @NotNull + public ColorTool getColorTool() { + return colorTool; + } + + /** + * @return Gets the filter used. + */ + @NotNull + public ContentFilter getFilter() { + return filter; + } + + /** + * Gets the value for a given setting option. + * + * @param setting The setting option. + * @param The setting type. + * @return Value set for the given setting. + */ + public S getSetting(@NotNull DisplaySetting setting) { + return (S) settingsMap.getOrDefault(setting, setting.defaultValue()); + } + + /** + * Sets other specific settings that may be used by the {@link DisplayHandler}. + * + * @param setting The settings option. + * @param value The value to set. + * @param The type of setting. + */ + public void setSetting(@NotNull DisplaySetting setting, S value) { + this.settingsMap.put(setting, value); + } + + /** + * Builds a {@link ContentDisplay}. + * + * @param Type of content to display. + */ + public static class Builder { + + private final ContentDisplay display; + + private Builder(T content) { + this.display = new ContentDisplay<>(content); + } + + /** + * Sets header to be displayed. + * + * @param header The header text. + * @param replacements String formatting replacements. + * @return The builder. + */ + @NotNull + public Builder header(@NotNull String header, Object...replacements) { + this.display.header = String.format(header, replacements); + return this; + } + + /** + * Sets the message to show when no content is available for display. + * + * @param emptyMessage The message text. + * @param replacements String formatting replacements. + * @return The builder. + */ + @NotNull + public Builder emptyMessage(@NotNull String emptyMessage, Object...replacements) { + this.display.emptyMessage = String.format(emptyMessage, replacements); + return this; + } + + /** + * Sets the display handler that does the formatting and sending of content. Required. + * + * @param displayHandler The display handler for the given content type. + * @return The builder. + */ + @NotNull + public Builder displayHandler(@NotNull DisplayHandler displayHandler) { + this.display.displayHandler = displayHandler; + return this; + } + + /** + * Sets the color tool used to make messages more colourful. + * + * @param colorTool The color tool to use. + * @return The builder. + */ + @NotNull + public Builder colorTool(@NotNull ColorTool colorTool) { + this.display.colorTool = colorTool; + return this; + } + + /** + * Sets content filter used to match specific content to be displayed. + * + * @param filter The filter to use. + * @return The builder. + */ + @NotNull + public Builder filter(@NotNull ContentFilter filter) { + this.display.filter = filter; + return this; + } + + /** + * Sets other specific settings that may be used by the {@link DisplayHandler}. + * + * @param setting The settings option. + * @param value The value to set. + * @param The type of setting. + * @return The builder. + */ + @NotNull + public Builder setting(@NotNull DisplaySetting setting, S value) { + this.display.settingsMap.put(setting, value); + return this; + } + + /** + * Validates and build the content display. + * + * @return The content display. + */ + @NotNull + public ContentDisplay build() { + Objects.requireNonNull(this.display.displayHandler); + return this.display; + } + + /** + * Build and show the content to the sender. + * + * @param sender The CommandSender to show the display to. + */ + public void show(CommandSender sender) { + this.build().show(sender); + } + } +} diff --git a/src/main/java/com/onarandombox/MultiverseCore/display/ContentFilter.java b/src/main/java/com/onarandombox/MultiverseCore/display/ContentFilter.java new file mode 100644 index 00000000..cd0bed8c --- /dev/null +++ b/src/main/java/com/onarandombox/MultiverseCore/display/ContentFilter.java @@ -0,0 +1,153 @@ +package com.onarandombox.MultiverseCore.display; + +import com.dumptruckman.minecraft.util.Logging; +import org.bukkit.ChatColor; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.util.regex.Pattern; +import java.util.regex.PatternSyntaxException; + +/** + *

Filter content and text based on regex matching.

+ * + *

Compile regex pattern based on {@link ContentFilter#filterString}. When prefixed with 'r=', + * use {@link ContentFilter#filterString} as the full regex pattern. Else, set to any match that + * contains the {@link ContentFilter#filterString}.

+ */ +public class ContentFilter { + + public static final ContentFilter DEFAULT = new ContentFilter(); + private static final Pattern REGEX_SPECIAL_CHARS = Pattern.compile("[.+*?\\[^\\]$(){}=!<>|:-\\\\]"); + + private String filterString; + private Pattern filterPattern; + private boolean exactMatch; + + private ContentFilter() { + } + + /** + * @param filterString The text to do matching, either plaintext or regex. + */ + public ContentFilter(@NotNull String filterString) { + this(filterString, false); + } + + /** + * @param filterString The text to do matching, else plaintext or regex. + * @param exactMatch Should check for exact match when doing regex matching. + */ + public ContentFilter(@NotNull String filterString, + boolean exactMatch) { + + this.filterString = filterString; + this.exactMatch = exactMatch; + parseFilter(); + } + + private void parseFilter() { + if (filterString == null) { + return; + } + if (filterString.startsWith("r=")) { + convertToMatcher(filterString.substring(2)); + return; + } + String cleanedFilter = REGEX_SPECIAL_CHARS.matcher(filterString.toLowerCase()).replaceAll("\\\\$0"); + convertToMatcher("(?i).*" + cleanedFilter + ".*"); + } + + /** + * Compile and store the regex into a {@link Pattern}. + * + * @param regex The regex text. + */ + private void convertToMatcher(@NotNull String regex) { + try { + this.filterPattern = Pattern.compile(regex); + Logging.finest("Parsed regex pattern: %s", this.filterPattern.toString()); + } + catch (PatternSyntaxException ignored) { + Logging.warning("Error parsing regex: %s", filterString); + } + } + + /** + * Do regex matching. + * + * @param text String to check regex on. + * @return True of matches regex pattern, false otherwise. + */ + public boolean checkMatch(@Nullable Object text) { + if (!hasFilter()) { + return true; + } + if (text == null || !hasValidPattern()) { + return false; + } + text = ChatColor.stripColor(String.valueOf(text)); + return (exactMatch) + ? filterPattern.matcher((CharSequence) text).matches() + : filterPattern.matcher((CharSequence) text).find(); + } + + /** + * Checks if a filter string is present. + * + * @return True if there is a filter string, else false. + */ + public boolean hasFilter() { + return filterString != null; + } + + /** + * Checks if regex pattern syntax is valid. + * + * @return True if valid, else false. + */ + public boolean hasValidPattern() { + return filterPattern != null; + } + + /** + * @return The filter string. + */ + @Nullable + public String getString() { + return filterString; + } + + /** + * @return The regex pattern. + */ + @Nullable + public Pattern getPattern() { + return filterPattern; + } + + /** + * @return True if filter is set to do exact matching, else false. + */ + public boolean isExactMatch() { + return exactMatch; + } + + /** + * Nicely format the filter string to be used for showing the sender. + * + * @return The formatted filter string. + */ + public @NotNull String getFormattedString() { + return String.format("%sFilter: '%s'", ChatColor.ITALIC, filterString); + } + + @Override + public String toString() { + return "ContentFilter{" + + "filterString='" + filterString + '\'' + + ", filterPattern=" + filterPattern + + ", exactMatch=" + exactMatch + + '}'; + } +} \ No newline at end of file diff --git a/src/main/java/com/onarandombox/MultiverseCore/display/DisplayFormatException.java b/src/main/java/com/onarandombox/MultiverseCore/display/DisplayFormatException.java new file mode 100644 index 00000000..604d6acd --- /dev/null +++ b/src/main/java/com/onarandombox/MultiverseCore/display/DisplayFormatException.java @@ -0,0 +1,25 @@ +package com.onarandombox.MultiverseCore.display; + +/** + * Thrown when an issue occur while formatting content. + */ +public class DisplayFormatException extends Exception { + public DisplayFormatException() { + } + + public DisplayFormatException(String message) { + super(message); + } + + public DisplayFormatException(String message, Throwable cause) { + super(message, cause); + } + + public DisplayFormatException(Throwable cause) { + super(cause); + } + + public DisplayFormatException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { + super(message, cause, enableSuppression, writableStackTrace); + } +} diff --git a/src/main/java/com/onarandombox/MultiverseCore/display/DisplayHandler.java b/src/main/java/com/onarandombox/MultiverseCore/display/DisplayHandler.java new file mode 100644 index 00000000..7971a11f --- /dev/null +++ b/src/main/java/com/onarandombox/MultiverseCore/display/DisplayHandler.java @@ -0,0 +1,68 @@ +package com.onarandombox.MultiverseCore.display; + +import com.google.common.base.Strings; +import org.bukkit.ChatColor; +import org.bukkit.command.CommandSender; +import org.jetbrains.annotations.NotNull; + +import java.util.Collection; + +/** + * Handles the formatting and sending of all content by the {@link ContentDisplay}. + * + * @param Type of content to display. + */ +@FunctionalInterface +public interface DisplayHandler { + + /** + * Formats the raw content into a {@link Collection} for displaying to the given sender. + * + * @param sender The {@link CommandSender} who will the content will be displayed to. + * @param display The responsible {@link ContentDisplay}. + * @return The formatted content. + * @throws DisplayFormatException Issue occurred while formatting content. E.g. invalid page. + */ + Collection format(@NotNull CommandSender sender, @NotNull ContentDisplay display) + throws DisplayFormatException; + + /** + * Sends the header. + * + * @param sender The {@link CommandSender} who will the header will be displayed to. + * @param display The responsible {@link ContentDisplay}. + */ + default void sendHeader(@NotNull CommandSender sender, @NotNull ContentDisplay display) { + if (!Strings.isNullOrEmpty(display.getHeader())) { + sender.sendMessage(display.getHeader()); + } + } + + /** + * Sends info such as filter and page. + * + * @param sender The {@link CommandSender} who will the sub header will be displayed to. + * @param display The responsible {@link ContentDisplay}. + */ + default void sendSubHeader(@NotNull CommandSender sender, @NotNull ContentDisplay display) { + if (display.getFilter().hasFilter()) { + sender.sendMessage(String.format("%s[ %s ]", ChatColor.GRAY, display.getFilter().getFormattedString())); + } + } + + /** + * Sends the content. + * + * @param sender The {@link CommandSender} who will the body will be displayed to. + * @param display The responsible {@link ContentDisplay}. + * @param formattedContent The content after being formatted by {@link #format(CommandSender, ContentDisplay)} + */ + default void sendBody(@NotNull CommandSender sender, @NotNull ContentDisplay display, + Collection formattedContent) { + if (formattedContent == null || formattedContent.size() == 0) { + sender.sendMessage(display.getEmptyMessage()); + return; + } + sender.sendMessage(formattedContent.toArray(new String[0])); + } +} diff --git a/src/main/java/com/onarandombox/MultiverseCore/display/DisplayHandlers.java b/src/main/java/com/onarandombox/MultiverseCore/display/DisplayHandlers.java new file mode 100644 index 00000000..7343c6ad --- /dev/null +++ b/src/main/java/com/onarandombox/MultiverseCore/display/DisplayHandlers.java @@ -0,0 +1,47 @@ +package com.onarandombox.MultiverseCore.display; + +import com.onarandombox.MultiverseCore.display.handlers.InlineListDisplayHandler; +import com.onarandombox.MultiverseCore.display.handlers.InlineMapDisplayHandler; +import com.onarandombox.MultiverseCore.display.handlers.ListDisplayHandler; +import com.onarandombox.MultiverseCore.display.handlers.PagedListDisplayHandler; +import com.onarandombox.MultiverseCore.display.settings.InlineDisplaySettings; +import com.onarandombox.MultiverseCore.display.settings.PagedDisplaySettings; +import com.onarandombox.MultiverseCore.display.settings.MapDisplaySettings; + +import java.util.Collection; +import java.util.Map; + +/** + * Various implementations of {@link DisplayHandler}. + */ +public class DisplayHandlers { + + /** + * Standard list display. + * + * Supported settings: none. + */ + public static final DisplayHandler> LIST = new ListDisplayHandler(); + + /** + * List display with paging. + * + * Supported settings: {@link PagedDisplaySettings#SHOW_PAGE}, {@link PagedDisplaySettings#LINES_PER_PAGE}, + * {@link PagedDisplaySettings#PAGE_IN_CONSOLE}, {@link PagedDisplaySettings#DO_END_PADDING}. + */ + public static final DisplayHandler> PAGE_LIST = new PagedListDisplayHandler(); + + /** + * Display a list inline. + * + * Supported settings: {@link InlineDisplaySettings#SEPARATOR}. + */ + public static final DisplayHandler> INLINE_LIST = new InlineListDisplayHandler(); + + /** + * Display key value pair inline. + * + * Supported settings: {@link InlineDisplaySettings#SEPARATOR}, {@link MapDisplaySettings#OPERATOR}. + */ + public static final DisplayHandler> INLINE_MAP = new InlineMapDisplayHandler(); +} diff --git a/src/main/java/com/onarandombox/MultiverseCore/display/handlers/InlineListDisplayHandler.java b/src/main/java/com/onarandombox/MultiverseCore/display/handlers/InlineListDisplayHandler.java new file mode 100644 index 00000000..53aa196b --- /dev/null +++ b/src/main/java/com/onarandombox/MultiverseCore/display/handlers/InlineListDisplayHandler.java @@ -0,0 +1,36 @@ +package com.onarandombox.MultiverseCore.display.handlers; + +import com.onarandombox.MultiverseCore.display.ContentDisplay; +import com.onarandombox.MultiverseCore.display.DisplayFormatException; +import com.onarandombox.MultiverseCore.display.DisplayHandler; +import com.onarandombox.MultiverseCore.display.settings.InlineDisplaySettings; +import org.bukkit.command.CommandSender; +import org.jetbrains.annotations.NotNull; + +import java.util.Collection; +import java.util.Collections; +import java.util.Iterator; + +public class InlineListDisplayHandler implements DisplayHandler> { + + @Override + public Collection format(@NotNull CommandSender sender, @NotNull ContentDisplay> display) + throws DisplayFormatException { + StringBuilder builder = new StringBuilder(); + String separator = display.getSetting(InlineDisplaySettings.SEPARATOR); + + for (Iterator iterator = display.getContents().iterator(); iterator.hasNext(); ) { + String content = iterator.next(); + if (!display.getFilter().checkMatch(content)) { + continue; + } + builder.append(display.getColorTool().get()).append(content); + if (iterator.hasNext()) { + builder.append(separator); + } + } + return (builder.length() == 0) + ? Collections.singletonList(display.getEmptyMessage()) + : Collections.singleton(builder.toString()); + } +} diff --git a/src/main/java/com/onarandombox/MultiverseCore/display/handlers/InlineMapDisplayHandler.java b/src/main/java/com/onarandombox/MultiverseCore/display/handlers/InlineMapDisplayHandler.java new file mode 100644 index 00000000..312ccb86 --- /dev/null +++ b/src/main/java/com/onarandombox/MultiverseCore/display/handlers/InlineMapDisplayHandler.java @@ -0,0 +1,44 @@ +package com.onarandombox.MultiverseCore.display.handlers; + +import com.onarandombox.MultiverseCore.display.ContentDisplay; +import com.onarandombox.MultiverseCore.display.DisplayFormatException; +import com.onarandombox.MultiverseCore.display.DisplayHandler; +import com.onarandombox.MultiverseCore.display.settings.InlineDisplaySettings; +import com.onarandombox.MultiverseCore.display.settings.MapDisplaySettings; +import org.bukkit.command.CommandSender; +import org.jetbrains.annotations.NotNull; + +import java.util.Collection; +import java.util.Collections; +import java.util.Iterator; +import java.util.Map; + +public class InlineMapDisplayHandler implements DisplayHandler> { + + @Override + public Collection format(@NotNull CommandSender sender, + @NotNull ContentDisplay> display) + throws DisplayFormatException { + StringBuilder builder = new StringBuilder(); + String separator = display.getSetting(InlineDisplaySettings.SEPARATOR); + String operator = display.getSetting(MapDisplaySettings.OPERATOR); + + for (Iterator> iterator = display.getContents().entrySet().iterator(); iterator.hasNext(); ) { + Map.Entry entry = iterator.next(); + if (!display.getFilter().checkMatch(entry.getKey()) && !display.getFilter().checkMatch(entry.getValue())) { + continue; + } + builder.append(display.getColorTool().get()) + .append(entry.getKey()) + .append(operator) + .append(display.getColorTool().get()) + .append(entry.getValue()); + if (iterator.hasNext()) { + builder.append(separator); + } + } + return (builder.length() == 0) + ? Collections.singletonList(display.getEmptyMessage()) + : Collections.singleton(builder.toString()); + } +} diff --git a/src/main/java/com/onarandombox/MultiverseCore/display/handlers/ListDisplayHandler.java b/src/main/java/com/onarandombox/MultiverseCore/display/handlers/ListDisplayHandler.java new file mode 100644 index 00000000..c2b0979f --- /dev/null +++ b/src/main/java/com/onarandombox/MultiverseCore/display/handlers/ListDisplayHandler.java @@ -0,0 +1,22 @@ +package com.onarandombox.MultiverseCore.display.handlers; + +import com.onarandombox.MultiverseCore.display.ContentDisplay; +import com.onarandombox.MultiverseCore.display.DisplayFormatException; +import com.onarandombox.MultiverseCore.display.DisplayHandler; +import org.bukkit.command.CommandSender; +import org.jetbrains.annotations.NotNull; + +import java.util.Collection; +import java.util.stream.Collectors; + +public class ListDisplayHandler implements DisplayHandler> { + + @Override + public Collection format(@NotNull CommandSender sender, @NotNull ContentDisplay> display) + throws DisplayFormatException { + return display.getContents().stream() + .filter(display.getFilter()::checkMatch) + .map(s -> (ContentDisplay.LINE_BREAK.equals(s)) ? "" : display.getColorTool().get() + s) + .collect(Collectors.toList()); + } +} diff --git a/src/main/java/com/onarandombox/MultiverseCore/display/handlers/PagedListDisplayHandler.java b/src/main/java/com/onarandombox/MultiverseCore/display/handlers/PagedListDisplayHandler.java new file mode 100644 index 00000000..17f0825e --- /dev/null +++ b/src/main/java/com/onarandombox/MultiverseCore/display/handlers/PagedListDisplayHandler.java @@ -0,0 +1,105 @@ +package com.onarandombox.MultiverseCore.display.handlers; + +import com.onarandombox.MultiverseCore.display.ContentDisplay; +import com.onarandombox.MultiverseCore.display.DisplayFormatException; +import com.onarandombox.MultiverseCore.display.settings.PagedDisplaySettings; +import org.bukkit.ChatColor; +import org.bukkit.command.CommandSender; +import org.bukkit.command.ConsoleCommandSender; +import org.jetbrains.annotations.NotNull; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; +import java.util.stream.IntStream; + +public class PagedListDisplayHandler extends ListDisplayHandler { + + @Override + public Collection format(@NotNull CommandSender sender, @NotNull ContentDisplay> display) + throws DisplayFormatException { + if (dontNeedPaging(sender, display)) { + return super.format(sender, display); + } + + int pages = 1; + int currentLength = 0; + int targetPage = display.getSetting(PagedDisplaySettings.SHOW_PAGE); + int linesPerPage = display.getSetting(PagedDisplaySettings.LINES_PER_PAGE); + List content = new ArrayList<>(linesPerPage); + + // Calculate the paging. + for (String line : display.getContents()) { + if (!display.getFilter().checkMatch(line)) { + continue; + } + // When it's the next page. + boolean isLineBreak = ContentDisplay.LINE_BREAK.equals(line); + if (isLineBreak || ++currentLength > linesPerPage) { + pages++; + currentLength = 0; + if (isLineBreak) { + continue; + } + } + if (pages == targetPage) { + // Let first line be the header when no header is defined. + if (display.getHeader() == null) { + display.setHeader(line); + currentLength--; + continue; + } + content.add(display.getColorTool().get() + line); + } + } + + // Page out of range. + if (targetPage < 1 || targetPage > pages) { + if (pages == 1) { + throw new DisplayFormatException("There is only 1 page!"); + } + throw new DisplayFormatException("Please enter a page from 1 to " + pages + "."); + } + + // No content + if (content.size() == 0) { + content.add(display.getEmptyMessage()); + } + + // Add empty lines to make output length consistent. + if (display.getSetting(PagedDisplaySettings.DO_END_PADDING)) { + IntStream.range(0, linesPerPage - content.size()).forEach(i -> content.add("")); + } + display.setSetting(PagedDisplaySettings.TOTAL_PAGE, pages); + + return content; + } + + @Override + public void sendSubHeader(@NotNull CommandSender sender, @NotNull ContentDisplay> display) { + if (dontNeedPaging(sender, display)) { + super.sendSubHeader(sender, display); + return; + } + + if (display.getFilter().hasFilter()) { + sender.sendMessage(String.format("%s[ Page %s of %s, %s ]", + ChatColor.GRAY, + display.getSetting(PagedDisplaySettings.SHOW_PAGE), + display.getSetting(PagedDisplaySettings.TOTAL_PAGE), + display.getFilter().getFormattedString()) + ); + return; + } + sender.sendMessage(String.format("%s[ Page %s of %s ]", + ChatColor.GRAY, + display.getSetting(PagedDisplaySettings.SHOW_PAGE), + display.getSetting(PagedDisplaySettings.TOTAL_PAGE)) + ); + } + + private boolean dontNeedPaging(CommandSender sender, ContentDisplay> display) { + return sender instanceof ConsoleCommandSender + && !display.getSetting(PagedDisplaySettings.PAGE_IN_CONSOLE); + } +} diff --git a/src/main/java/com/onarandombox/MultiverseCore/display/settings/DisplaySetting.java b/src/main/java/com/onarandombox/MultiverseCore/display/settings/DisplaySetting.java new file mode 100644 index 00000000..232e432b --- /dev/null +++ b/src/main/java/com/onarandombox/MultiverseCore/display/settings/DisplaySetting.java @@ -0,0 +1,19 @@ +package com.onarandombox.MultiverseCore.display.settings; + +import com.onarandombox.MultiverseCore.display.DisplayHandler; + +/** + * Represents a setting option that can be used by {@link DisplayHandler}. + * + * @param + */ +@FunctionalInterface +public interface DisplaySetting { + + /** + * Gets the default value of this Display Setting. + * + * @return The default value. + */ + T defaultValue(); +} diff --git a/src/main/java/com/onarandombox/MultiverseCore/display/settings/InlineDisplaySettings.java b/src/main/java/com/onarandombox/MultiverseCore/display/settings/InlineDisplaySettings.java new file mode 100644 index 00000000..25661d12 --- /dev/null +++ b/src/main/java/com/onarandombox/MultiverseCore/display/settings/InlineDisplaySettings.java @@ -0,0 +1,15 @@ +package com.onarandombox.MultiverseCore.display.settings; + +import com.onarandombox.MultiverseCore.display.DisplayHandler; +import org.bukkit.ChatColor; + +/** + * Collection of {@link DisplaySetting} that are used by various {@link DisplayHandler}. + */ +public class InlineDisplaySettings { + + /** + * Inline separator. E.g. '1, 2, 3' + */ + public static final DisplaySetting SEPARATOR = () -> ChatColor.WHITE + ", "; +} diff --git a/src/main/java/com/onarandombox/MultiverseCore/display/settings/MapDisplaySettings.java b/src/main/java/com/onarandombox/MultiverseCore/display/settings/MapDisplaySettings.java new file mode 100644 index 00000000..a7847395 --- /dev/null +++ b/src/main/java/com/onarandombox/MultiverseCore/display/settings/MapDisplaySettings.java @@ -0,0 +1,15 @@ +package com.onarandombox.MultiverseCore.display.settings; + +import com.onarandombox.MultiverseCore.display.DisplayHandler; +import org.bukkit.ChatColor; + +/** + * Collection of {@link DisplaySetting} that are used by various {@link DisplayHandler}. + */ +public class MapDisplaySettings { + + /** + * The thing between a key value pair. E.g. 'Me = Smart' + */ + public static final DisplaySetting OPERATOR = () -> ChatColor.WHITE + " = "; +} diff --git a/src/main/java/com/onarandombox/MultiverseCore/display/settings/PagedDisplaySettings.java b/src/main/java/com/onarandombox/MultiverseCore/display/settings/PagedDisplaySettings.java new file mode 100644 index 00000000..bdc12f9f --- /dev/null +++ b/src/main/java/com/onarandombox/MultiverseCore/display/settings/PagedDisplaySettings.java @@ -0,0 +1,30 @@ +package com.onarandombox.MultiverseCore.display.settings; + +public class PagedDisplaySettings { + + /** + * Page to display. + */ + public static final DisplaySetting SHOW_PAGE = () -> 1; + + /** + * Total pages available to display. + */ + public static final DisplaySetting TOTAL_PAGE = () -> 1; + + /** + * The max number of lines per page. This excludes header. + */ + public static final DisplaySetting LINES_PER_PAGE = () -> 8; + + /** + * Should add empty lines if content lines shown is less that {@link #LINES_PER_PAGE}. + */ + public static final DisplaySetting DO_END_PADDING = () -> true; + + /** + * Should display with paging when it's sent to console. + */ + public static final DisplaySetting PAGE_IN_CONSOLE = () -> false; + +} diff --git a/src/main/java/com/onarandombox/MultiverseCore/enums/RespawnType.java b/src/main/java/com/onarandombox/MultiverseCore/enums/RespawnType.java new file mode 100644 index 00000000..889f0e43 --- /dev/null +++ b/src/main/java/com/onarandombox/MultiverseCore/enums/RespawnType.java @@ -0,0 +1,7 @@ +package com.onarandombox.MultiverseCore.enums; + +public enum RespawnType { + BED, + ANCHOR, + OTHER +} diff --git a/src/main/java/com/onarandombox/MultiverseCore/event/MVVersionEvent.java b/src/main/java/com/onarandombox/MultiverseCore/event/MVVersionEvent.java index 43c1ddfc..9b1bc632 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/event/MVVersionEvent.java +++ b/src/main/java/com/onarandombox/MultiverseCore/event/MVVersionEvent.java @@ -1,8 +1,16 @@ package com.onarandombox.MultiverseCore.event; +import com.dumptruckman.minecraft.util.Logging; import org.bukkit.event.Event; import org.bukkit.event.HandlerList; +import java.io.BufferedReader; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileReader; +import java.io.IOException; +import java.util.Collections; +import java.util.HashMap; import java.util.Map; /** @@ -13,9 +21,9 @@ public class MVVersionEvent extends Event { private final StringBuilder versionInfoBuilder; private final Map detailedVersionInfo; - public MVVersionEvent(String legacyVersionInfo, Map files) { - this.versionInfoBuilder = new StringBuilder(legacyVersionInfo); - this.detailedVersionInfo = files; + public MVVersionEvent() { + versionInfoBuilder = new StringBuilder(); + detailedVersionInfo = new HashMap<>(); } private static final HandlerList HANDLERS = new HandlerList(); @@ -49,14 +57,14 @@ public class MVVersionEvent extends Event { * * This information is used for advanced paste services that would prefer * to get the information as several files. Examples include config.yml or - * portals.yml. + * portals.yml. Note that the map returned is immutable. * * The keys are filenames, the values are the contents of the files. * - * @return The key value mapping of files and the contents of those files. + * @return The immutable key value mapping of files and the contents of those files. */ public Map getDetailedVersionInfo() { - return this.detailedVersionInfo; + return Collections.unmodifiableMap(this.detailedVersionInfo); } /** @@ -66,4 +74,46 @@ public class MVVersionEvent extends Event { public void appendVersionInfo(String moreVersionInfo) { this.versionInfoBuilder.append(moreVersionInfo); } + + private String readFile(final String filename) { + StringBuilder result; + + try { + FileReader reader = new FileReader(filename); + BufferedReader bufferedReader = new BufferedReader(reader); + String line; + result = new StringBuilder(); + while ((line = bufferedReader.readLine()) != null) { + result.append(line).append("\n"); + } + } catch (FileNotFoundException e) { + Logging.severe("Unable to find %s. Here's the traceback: %s", filename, e.getMessage()); + e.printStackTrace(); + result = new StringBuilder(String.format("ERROR: Could not load: %s", filename)); + } catch (IOException e) { + Logging.severe("Something bad happend when reading %s. Here's the traceback: %s", filename, e.getMessage()); + e.printStackTrace(); + result = new StringBuilder(String.format("ERROR: Could not load: %s", filename)); + } + + return result.toString(); + } + + /** + * Adds a file to to the detailed version-info currently saved in this event. + * @param fileName The name of the file. + * @param contents The contents of the file. + */ + public void putDetailedVersionInfo(String fileName, String contents) { + this.detailedVersionInfo.put(fileName, contents); + } + + /** + * Adds a file to to the detailed version-info currently saved in this event. + * @param filename The name of the file. + * @param file The file. + */ + public void putDetailedVersionInfo(String filename, File file) { + this.putDetailedVersionInfo(filename, readFile(file.getAbsolutePath())); + } } diff --git a/src/main/java/com/onarandombox/MultiverseCore/listeners/MVAsyncPlayerChatListener.java b/src/main/java/com/onarandombox/MultiverseCore/listeners/MVAsyncPlayerChatListener.java index eae4df20..c6210253 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/listeners/MVAsyncPlayerChatListener.java +++ b/src/main/java/com/onarandombox/MultiverseCore/listeners/MVAsyncPlayerChatListener.java @@ -9,6 +9,7 @@ package com.onarandombox.MultiverseCore.listeners; import java.util.logging.Level; +import com.dumptruckman.minecraft.util.Logging; import org.bukkit.event.EventHandler; import org.bukkit.event.player.AsyncPlayerChatEvent; @@ -20,7 +21,7 @@ import com.onarandombox.MultiverseCore.MultiverseCore; public class MVAsyncPlayerChatListener extends MVChatListener { public MVAsyncPlayerChatListener(MultiverseCore plugin, MVPlayerListener playerListener) { super(plugin, playerListener); - plugin.log(Level.FINE, "Created AsyncPlayerChatEvent listener."); + Logging.fine("Created AsyncPlayerChatEvent listener."); } /** diff --git a/src/main/java/com/onarandombox/MultiverseCore/listeners/MVEntityListener.java b/src/main/java/com/onarandombox/MultiverseCore/listeners/MVEntityListener.java index 4aa34ccd..fd81e694 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/listeners/MVEntityListener.java +++ b/src/main/java/com/onarandombox/MultiverseCore/listeners/MVEntityListener.java @@ -7,9 +7,11 @@ package com.onarandombox.MultiverseCore.listeners; +import com.dumptruckman.minecraft.util.Logging; import com.onarandombox.MultiverseCore.MultiverseCore; import com.onarandombox.MultiverseCore.api.MVWorldManager; import com.onarandombox.MultiverseCore.api.MultiverseWorld; +import com.onarandombox.MultiverseCore.utils.CompatibilityLayer; import org.bukkit.World; import org.bukkit.entity.EntityType; import org.bukkit.entity.Player; @@ -17,6 +19,7 @@ import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; import org.bukkit.event.entity.CreatureSpawnEvent; import org.bukkit.event.entity.CreatureSpawnEvent.SpawnReason; +import org.bukkit.event.entity.EntityPortalEvent; import org.bukkit.event.entity.EntityRegainHealthEvent; import org.bukkit.event.entity.EntityRegainHealthEvent.RegainReason; import org.bukkit.event.entity.FoodLevelChangeEvent; @@ -98,7 +101,7 @@ public class MVEntityListener implements Listener { * Handle people with non-standard animals: ie a patched craftbukkit. */ if (type == null || type.getName() == null) { - this.plugin.log(Level.FINER, "Found a null typed creature."); + Logging.finer("Found a null typed creature."); return; } @@ -106,4 +109,17 @@ public class MVEntityListener implements Listener { event.setCancelled(this.plugin.getMVWorldManager().getTheWorldPurger().shouldWeKillThisCreature(mvworld, event.getEntity())); } + /** + * Handles portal search radius adjustment. + * @param event The Event that was fired. + */ + @EventHandler + public void entityPortal(EntityPortalEvent event) { + if (event.isCancelled() || event.getTo() == null) { + return; + } + if (!this.plugin.getMVConfig().isUsingDefaultPortalSearch()) { + CompatibilityLayer.setPortalSearchRadius(event, this.plugin.getMVConfig().getPortalSearchRadius()); + } + } } diff --git a/src/main/java/com/onarandombox/MultiverseCore/listeners/MVPlayerChatListener.java b/src/main/java/com/onarandombox/MultiverseCore/listeners/MVPlayerChatListener.java index fc110522..fff535da 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/listeners/MVPlayerChatListener.java +++ b/src/main/java/com/onarandombox/MultiverseCore/listeners/MVPlayerChatListener.java @@ -9,6 +9,7 @@ package com.onarandombox.MultiverseCore.listeners; import java.util.logging.Level; +import com.dumptruckman.minecraft.util.Logging; import org.bukkit.event.EventHandler; import org.bukkit.event.player.PlayerChatEvent; @@ -21,7 +22,7 @@ import com.onarandombox.MultiverseCore.MultiverseCore; public class MVPlayerChatListener extends MVChatListener { public MVPlayerChatListener(MultiverseCore plugin, MVPlayerListener playerListener) { super(plugin, playerListener); - plugin.log(Level.FINE, "Registered PlayerChatEvent listener."); + Logging.fine("Registered PlayerChatEvent listener."); } /** diff --git a/src/main/java/com/onarandombox/MultiverseCore/listeners/MVPlayerListener.java b/src/main/java/com/onarandombox/MultiverseCore/listeners/MVPlayerListener.java index 40c1f51b..2921f9c5 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/listeners/MVPlayerListener.java +++ b/src/main/java/com/onarandombox/MultiverseCore/listeners/MVPlayerListener.java @@ -7,15 +7,13 @@ package com.onarandombox.MultiverseCore.listeners; -import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; -import java.util.logging.Level; - import com.dumptruckman.minecraft.util.Logging; import com.onarandombox.MultiverseCore.MultiverseCore; import com.onarandombox.MultiverseCore.api.MVWorldManager; import com.onarandombox.MultiverseCore.api.MultiverseWorld; +import com.onarandombox.MultiverseCore.enums.RespawnType; import com.onarandombox.MultiverseCore.event.MVRespawnEvent; +import com.onarandombox.MultiverseCore.utils.CompatibilityLayer; import com.onarandombox.MultiverseCore.utils.PermissionTools; import org.bukkit.GameMode; import org.bukkit.Location; @@ -33,6 +31,9 @@ import org.bukkit.event.player.PlayerQuitEvent; import org.bukkit.event.player.PlayerRespawnEvent; import org.bukkit.event.player.PlayerTeleportEvent; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + /** * Multiverse's {@link Listener} for players. */ @@ -69,9 +70,16 @@ public class MVPlayerListener implements Listener { return; } + RespawnType respawnType = RespawnType.OTHER; + if (event.isBedSpawn()) { + respawnType = RespawnType.BED; + } + if (CompatibilityLayer.isAnchorSpawn(event)) { + respawnType = RespawnType.ANCHOR; + } - if (mvWorld.getBedRespawn() && event.isBedSpawn()) { - this.plugin.log(Level.FINE, "Spawning " + event.getPlayer().getName() + " at their bed"); + if (mvWorld.getBedRespawn() && (respawnType == RespawnType.BED || respawnType == RespawnType.ANCHOR)) { + Logging.fine("Spawning %s at their %s", event.getPlayer().getName(), respawnType); return; } @@ -110,15 +118,15 @@ public class MVPlayerListener implements Listener { public void playerJoin(PlayerJoinEvent event) { Player p = event.getPlayer(); if (!p.hasPlayedBefore()) { - this.plugin.log(Level.FINER, "Player joined for the FIRST time!"); + Logging.finer("Player joined for the FIRST time!"); if (plugin.getMVConfig().getFirstSpawnOverride()) { - this.plugin.log(Level.FINE, "Moving NEW player to(firstspawnoverride): " + Logging.fine("Moving NEW player to(firstspawnoverride): " + worldManager.getFirstSpawnWorld().getSpawnLocation()); this.sendPlayerToDefaultWorld(p); } return; } else { - this.plugin.log(Level.FINER, "Player joined AGAIN!"); + Logging.finer("Player joined AGAIN!"); if (this.plugin.getMVConfig().getEnforceAccess() // check this only if we're enforcing access! && !this.plugin.getMVPerms().hasPermission(p, "multiverse.access." + p.getWorld().getName(), false)) { p.sendMessage("[MV] - Sorry you can't be in this world anymore!"); @@ -156,7 +164,7 @@ public class MVPlayerListener implements Listener { */ @EventHandler(priority = EventPriority.HIGHEST) public void playerTeleport(PlayerTeleportEvent event) { - this.plugin.log(Level.FINER, "Got teleport event for player '" + Logging.finer("Got teleport event for player '" + event.getPlayer().getName() + "' with cause '" + event.getCause() + "'"); if (event.isCancelled()) { return; @@ -166,25 +174,25 @@ public class MVPlayerListener implements Listener { String teleporterName = MultiverseCore.getPlayerTeleporter(teleportee.getName()); if (teleporterName != null) { if (teleporterName.equals("CONSOLE")) { - this.plugin.log(Level.FINER, "We know the teleporter is the console! Magical!"); + Logging.finer("We know the teleporter is the console! Magical!"); teleporter = this.plugin.getServer().getConsoleSender(); } else { - teleporter = this.plugin.getServer().getPlayer(teleporterName); + teleporter = this.plugin.getServer().getPlayerExact(teleporterName); } } - this.plugin.log(Level.FINER, "Inferred sender '" + teleporter + "' from name '" + Logging.finer("Inferred sender '" + teleporter + "' from name '" + teleporterName + "', fetched from name '" + teleportee.getName() + "'"); MultiverseWorld fromWorld = this.worldManager.getMVWorld(event.getFrom().getWorld().getName()); MultiverseWorld toWorld = this.worldManager.getMVWorld(event.getTo().getWorld().getName()); if (toWorld == null) { - this.plugin.log(Level.FINE, "Player '" + teleportee.getName() + "' is teleporting to world '" + Logging.fine("Player '" + teleportee.getName() + "' is teleporting to world '" + event.getTo().getWorld().getName() + "' which is not managed by Multiverse-Core. No further " + "actions will be taken by Multiverse-Core."); return; } if (event.getFrom().getWorld().equals(event.getTo().getWorld())) { // The player is Teleporting to the same world. - this.plugin.log(Level.FINER, "Player '" + teleportee.getName() + "' is teleporting to the same world."); + Logging.finer("Player '" + teleportee.getName() + "' is teleporting to the same world."); this.stateSuccess(teleportee.getName(), toWorld.getAlias()); return; } @@ -192,7 +200,7 @@ public class MVPlayerListener implements Listener { // Charge the teleporter event.setCancelled(!pt.playerHasMoneyToEnter(fromWorld, toWorld, teleporter, teleportee, true)); if (event.isCancelled() && teleporter != null) { - this.plugin.log(Level.FINE, "Player '" + teleportee.getName() + Logging.fine("Player '" + teleportee.getName() + "' was DENIED ACCESS to '" + toWorld.getAlias() + "' because '" + teleporter.getName() + "' don't have the FUNDS required to enter it."); @@ -203,14 +211,14 @@ public class MVPlayerListener implements Listener { if (plugin.getMVConfig().getEnforceAccess()) { event.setCancelled(!pt.playerCanGoFromTo(fromWorld, toWorld, teleporter, teleportee)); if (event.isCancelled() && teleporter != null) { - this.plugin.log(Level.FINE, "Player '" + teleportee.getName() + Logging.fine("Player '" + teleportee.getName() + "' was DENIED ACCESS to '" + toWorld.getAlias() + "' because '" + teleporter.getName() + "' don't have: multiverse.access." + event.getTo().getWorld().getName()); return; } } else { - this.plugin.log(Level.FINE, "Player '" + teleportee.getName() + Logging.fine("Player '" + teleportee.getName() + "' was allowed to go to '" + toWorld.getAlias() + "' because enforceaccess is off."); } @@ -220,7 +228,7 @@ public class MVPlayerListener implements Listener { if (toWorld.getCBWorld().getPlayers().size() >= toWorld.getPlayerLimit()) { // Ouch the world is full, lets see if the player can bypass that limitation if (!pt.playerCanBypassPlayerLimit(toWorld, teleporter, teleportee)) { - this.plugin.log(Level.FINE, "Player '" + teleportee.getName() + Logging.fine("Player '" + teleportee.getName() + "' was DENIED ACCESS to '" + toWorld.getAlias() + "' because the world is full and '" + teleporter.getName() + "' doesn't have: mv.bypass.playerlimit." + event.getTo().getWorld().getName()); @@ -235,7 +243,7 @@ public class MVPlayerListener implements Listener { } private void stateSuccess(String playerName, String worldName) { - this.plugin.log(Level.FINE, "MV-Core is allowing Player '" + playerName + Logging.fine("MV-Core is allowing Player '" + playerName + "' to go to '" + worldName + "'."); } @@ -282,12 +290,12 @@ public class MVPlayerListener implements Listener { MultiverseWorld toWorld = this.worldManager.getMVWorld(event.getTo().getWorld().getName()); if (event.getFrom().getWorld().equals(event.getTo().getWorld())) { // The player is Portaling to the same world. - this.plugin.log(Level.FINER, "Player '" + event.getPlayer().getName() + "' is portaling to the same world."); + Logging.finer("Player '" + event.getPlayer().getName() + "' is portaling to the same world."); return; } event.setCancelled(!pt.playerHasMoneyToEnter(fromWorld, toWorld, event.getPlayer(), event.getPlayer(), true)); if (event.isCancelled()) { - this.plugin.log(Level.FINE, "Player '" + event.getPlayer().getName() + Logging.fine("Player '" + event.getPlayer().getName() + "' was DENIED ACCESS to '" + event.getTo().getWorld().getName() + "' because they don't have the FUNDS required to enter."); return; @@ -295,25 +303,17 @@ public class MVPlayerListener implements Listener { if (plugin.getMVConfig().getEnforceAccess()) { event.setCancelled(!pt.playerCanGoFromTo(fromWorld, toWorld, event.getPlayer(), event.getPlayer())); if (event.isCancelled()) { - this.plugin.log(Level.FINE, "Player '" + event.getPlayer().getName() + Logging.fine("Player '" + event.getPlayer().getName() + "' was DENIED ACCESS to '" + event.getTo().getWorld().getName() + "' because they don't have: multiverse.access." + event.getTo().getWorld().getName()); } } else { - this.plugin.log(Level.FINE, "Player '" + event.getPlayer().getName() + Logging.fine("Player '" + event.getPlayer().getName() + "' was allowed to go to '" + event.getTo().getWorld().getName() + "' because enforceaccess is off."); } - if (!plugin.getMVConfig().isUsingDefaultPortalSearch()) { - try { - Class.forName("org.bukkit.TravelAgent"); - if (event.getPortalTravelAgent() != null) { - event.getPortalTravelAgent().setSearchRadius(plugin.getMVConfig().getPortalSearchRadius()); - } - } catch (ClassNotFoundException ignore) { - plugin.log(Level.FINE, "TravelAgent not available for PlayerPortalEvent for " + event.getPlayer().getName()); - } - + if (!this.plugin.getMVConfig().isUsingDefaultPortalSearch()) { + CompatibilityLayer.setPortalSearchRadius(event, this.plugin.getMVConfig().getPortalSearchRadius()); } } @@ -335,7 +335,7 @@ public class MVPlayerListener implements Listener { if (mvWorld != null) { this.handleGameModeAndFlight(player, mvWorld); } else { - this.plugin.log(Level.FINER, "Not handling gamemode and flight for world '" + world.getName() + Logging.finer("Not handling gamemode and flight for world '" + world.getName() + "' not managed by Multiverse."); } } @@ -376,7 +376,7 @@ public class MVPlayerListener implements Listener { player.getName(), player.getWorld().getName(), world.getName()); } } else { - MVPlayerListener.this.plugin.log(Level.FINE, "Player: " + player.getName() + " is IMMUNE to gamemode changes!"); + Logging.fine("Player: " + player.getName() + " is IMMUNE to gamemode changes!"); } } }, 1L); diff --git a/src/main/java/com/onarandombox/MultiverseCore/listeners/MVPortalListener.java b/src/main/java/com/onarandombox/MultiverseCore/listeners/MVPortalListener.java index 31d6f469..c348ac3d 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/listeners/MVPortalListener.java +++ b/src/main/java/com/onarandombox/MultiverseCore/listeners/MVPortalListener.java @@ -7,6 +7,7 @@ package com.onarandombox.MultiverseCore.listeners; +import com.dumptruckman.minecraft.util.Logging; import com.onarandombox.MultiverseCore.MultiverseCore; import com.onarandombox.MultiverseCore.api.MultiverseWorld; import org.bukkit.Material; @@ -56,7 +57,7 @@ public class MVPortalListener implements Listener { public void portalForm(PortalCreateEvent event) { MultiverseWorld world = this.plugin.getMVWorldManager().getMVWorld(event.getWorld()); if (world != null && !world.getAllowedPortals().isPortalAllowed(PortalType.NETHER)) { - plugin.log(Level.FINE, "Cancelling creation of nether portal because portalForm disallows."); + Logging.fine("Cancelling creation of nether portal because portalForm disallows."); event.setCancelled(true); } } @@ -79,7 +80,7 @@ public class MVPortalListener implements Listener { } MultiverseWorld world = this.plugin.getMVWorldManager().getMVWorld(event.getPlayer().getWorld()); if (world != null && !world.getAllowedPortals().isPortalAllowed(PortalType.ENDER)) { - plugin.log(Level.FINE, "Cancelling creation of ender portal because portalForm disallows."); + Logging.fine("Cancelling creation of ender portal because portalForm disallows."); event.setCancelled(true); } } diff --git a/src/main/java/com/onarandombox/MultiverseCore/utils/AnchorManager.java b/src/main/java/com/onarandombox/MultiverseCore/utils/AnchorManager.java index 6a874f1e..4cac8a1b 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/utils/AnchorManager.java +++ b/src/main/java/com/onarandombox/MultiverseCore/utils/AnchorManager.java @@ -74,7 +74,7 @@ public class AnchorManager { this.anchorConfig.save(new File(this.plugin.getDataFolder(), "anchors.yml")); return true; } catch (IOException e) { - this.plugin.log(Level.SEVERE, "Failed to save anchors.yml. Please check your file permissions."); + Logging.severe("Failed to save anchors.yml. Please check your file permissions."); return false; } } diff --git a/src/main/java/com/onarandombox/MultiverseCore/utils/BukkitTravelAgent.java b/src/main/java/com/onarandombox/MultiverseCore/utils/BukkitTravelAgent.java index fd68d2ea..e2f658fa 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/utils/BukkitTravelAgent.java +++ b/src/main/java/com/onarandombox/MultiverseCore/utils/BukkitTravelAgent.java @@ -2,6 +2,7 @@ package com.onarandombox.MultiverseCore.utils; import java.util.logging.Level; +import com.dumptruckman.minecraft.util.Logging; import com.onarandombox.MultiverseCore.api.SafeTTeleporter; import com.onarandombox.MultiverseCore.destination.CannonDestination; import org.bukkit.Location; @@ -89,7 +90,7 @@ public class BukkitTravelAgent implements TravelAgent { private Location getSafeLocation() { // At this time, these can never use the velocity. if (agent.destination instanceof CannonDestination) { - agent.core.log(Level.FINE, "Using Stock TP method. This cannon will have 0 velocity"); + Logging.fine("Using Stock TP method. This cannon will have 0 velocity"); } SafeTTeleporter teleporter = agent.core.getSafeTTeleporter(); Location newLoc = agent.destination.getLocation(agent.player); diff --git a/src/main/java/com/onarandombox/MultiverseCore/utils/CompatibilityLayer.java b/src/main/java/com/onarandombox/MultiverseCore/utils/CompatibilityLayer.java new file mode 100644 index 00000000..2a83a7e2 --- /dev/null +++ b/src/main/java/com/onarandombox/MultiverseCore/utils/CompatibilityLayer.java @@ -0,0 +1,106 @@ +package com.onarandombox.MultiverseCore.utils; + +import com.dumptruckman.minecraft.util.Logging; +import org.bukkit.event.entity.EntityPortalEvent; +import org.bukkit.event.player.PlayerPortalEvent; +import org.bukkit.event.player.PlayerRespawnEvent; + +import java.lang.reflect.Method; + +/** + * Utility class to enable version specific minecraft features. + */ +public class CompatibilityLayer { + + private static Method checkAnchorSpawn; + private static boolean useTravelAgent; + private static Method playerPortalSearchRadius; + private static Method entityPortalSearchRadius; + + /** + * Initialise the reflection class, methods and fields. + */ + public static void init() { + checkAnchorSpawn = ReflectHelper.getMethod(PlayerRespawnEvent.class, "isAnchorSpawn"); + useTravelAgent = ReflectHelper.hasClass("org.bukkit.TravelAgent"); + playerPortalSearchRadius = ReflectHelper.getMethod(PlayerPortalEvent.class, "setSearchRadius", int.class); + entityPortalSearchRadius = ReflectHelper.getMethod(EntityPortalEvent.class, "setSearchRadius", int.class); + } + + /** + *

Check if the respawn point is of respawn anchor type.

+ *

Introduced in minecraft 1.16

+ * + * @param event A player respawn event. + * @return If the respawn location is an anchor point. + */ + public static boolean isAnchorSpawn(PlayerRespawnEvent event) { + if (checkAnchorSpawn == null) { + return false; + } + Boolean result = ReflectHelper.invokeMethod(event, checkAnchorSpawn); + if (result == null) { + Logging.warning("Unable to check if spawning at respawn anchor!"); + return false; + } + return result; + } + + /** + *

Gets if Travel Agent is supported on the server's minecraft version.

+ *

Removed in minecraft 1.14

+ * + * @return True if Travel Agent is supported, else false. + */ + public static boolean isUseTravelAgent() { + return useTravelAgent; + } + + /** + *

Sets search radius for a PlayerPortalEvent.

+ * + *

Use travel agent if available, else using new PlayerPortalEvent.setSearchRadius(int) method + * introduced in minecraft 1.15

+ * + * @param event A Player Portal Event. + * @param searchRadius Target search radius to set to. + */ + public static void setPortalSearchRadius(PlayerPortalEvent event, int searchRadius) { + if (useTravelAgent) { + event.getPortalTravelAgent().setSearchRadius(searchRadius); + event.useTravelAgent(true); + Logging.finer("Used travel agent to set player portal search radius."); + return; + } + if (playerPortalSearchRadius == null) { + Logging.warning("Unable to set player portal search radius!"); + return; + } + ReflectHelper.invokeMethod(event, playerPortalSearchRadius, searchRadius); + Logging.finer("Used new method to set player portal search radius."); + } + + /** + *

Sets search radius for a EntityPortalEvent.

+ * + *

Use travel agent if available, else using new EntityPortalEvent.setSearchRadius(int) method + * introduced in minecraft 1.15

+ * + * @param event A Entity Portal Event. + * @param searchRadius Target search radius to set to. + */ + public static void setPortalSearchRadius(EntityPortalEvent event, int searchRadius) { + if (useTravelAgent) { + event.getPortalTravelAgent().setSearchRadius(searchRadius); + event.useTravelAgent(true); + Logging.finer("Used travel agent to set entity portal search radius."); + return; + } + if (entityPortalSearchRadius == null) { + Logging.warning("Unable to set entity portal search radius!"); + return; + } + ReflectHelper.invokeMethod(event, entityPortalSearchRadius, searchRadius); + Logging.finer("Used new method to set entity portal search radius."); + } +} diff --git a/src/main/java/com/onarandombox/MultiverseCore/utils/FileUtils.java b/src/main/java/com/onarandombox/MultiverseCore/utils/FileUtils.java index ce793306..571143bb 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/utils/FileUtils.java +++ b/src/main/java/com/onarandombox/MultiverseCore/utils/FileUtils.java @@ -16,7 +16,9 @@ import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.SimpleFileVisitor; import java.nio.file.attribute.BasicFileAttributes; +import java.util.Arrays; import java.util.Comparator; +import java.util.List; import java.util.logging.Level; import java.util.logging.Logger; import java.util.stream.Stream; @@ -70,19 +72,30 @@ public class FileUtils { * Helper method to copy the world-folder. * @param source Source-File * @param target Target-File - * @param log A logger that logs the operation * - * @return if it had success + * @return true if it had success */ - public static boolean copyFolder(File source, File target, Logger log) { + public static boolean copyFolder(File source, File target) { + return copyFolder(source, target, null); + } + + /** + * Helper method to copy the world-folder. + * @param source Source-File + * @param target Target-File + * @param excludeFiles files to ignore and not copy over to Target-File + * + * @return true if it had success + */ + public static boolean copyFolder(File source, File target, List excludeFiles) { Path sourceDir = source.toPath(); Path targetDir = target.toPath(); try { - Files.walkFileTree(sourceDir, new CopyDirFileVisitor(sourceDir, targetDir)); + Files.walkFileTree(sourceDir, new CopyDirFileVisitor(sourceDir, targetDir, excludeFiles)); return true; } catch (IOException e) { - log.log(Level.WARNING, "Unable to copy directory", e); + Logging.warning("Unable to copy directory", e); return false; } } @@ -91,10 +104,12 @@ public class FileUtils { private final Path sourceDir; private final Path targetDir; + private final List excludeFiles; - private CopyDirFileVisitor(Path sourceDir, Path targetDir) { + private CopyDirFileVisitor(Path sourceDir, Path targetDir, List excludeFiles) { this.sourceDir = sourceDir; this.targetDir = targetDir; + this.excludeFiles = excludeFiles; } @Override @@ -108,9 +123,13 @@ public class FileUtils { @Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { + // Pass files that are set to ignore + if (excludeFiles != null && excludeFiles.contains(file.getFileName().toString())) + return FileVisitResult.CONTINUE; + // Copy the files Path targetFile = targetDir.resolve(sourceDir.relativize(file)); Files.copy(file, targetFile, COPY_ATTRIBUTES); return FileVisitResult.CONTINUE; } } -} +} \ No newline at end of file diff --git a/src/main/java/com/onarandombox/MultiverseCore/utils/MVPermissions.java b/src/main/java/com/onarandombox/MultiverseCore/utils/MVPermissions.java index d1c078e7..24ca8692 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/utils/MVPermissions.java +++ b/src/main/java/com/onarandombox/MultiverseCore/utils/MVPermissions.java @@ -7,6 +7,7 @@ package com.onarandombox.MultiverseCore.utils; +import com.dumptruckman.minecraft.util.Logging; import com.onarandombox.MultiverseCore.MultiverseCore; import com.onarandombox.MultiverseCore.api.MVDestination; import com.onarandombox.MultiverseCore.api.MVWorldManager; @@ -100,7 +101,7 @@ public class MVPermissions implements PermissionsInterface { public boolean canEnterWorld(Player p, MultiverseWorld w) { // If we're not enforcing access, anyone can enter. if (!plugin.getMVConfig().getEnforceAccess()) { - this.plugin.log(Level.FINEST, "EnforceAccess is OFF. Player was allowed in " + w.getAlias()); + Logging.finest("EnforceAccess is OFF. Player was allowed in " + w.getAlias()); return true; } return this.hasPermission(p, "multiverse.access." + w.getName(), false); @@ -257,14 +258,14 @@ public class MVPermissions implements PermissionsInterface { boolean hasPermission = sender.hasPermission(node); if (!sender.isPermissionSet(node)) { - this.plugin.log(Level.FINER, String.format("The node [%s%s%s] was %sNOT%s set for [%s%s%s].", + Logging.finer(String.format("The node [%s%s%s] was %sNOT%s set for [%s%s%s].", ChatColor.RED, node, ChatColor.WHITE, ChatColor.RED, ChatColor.WHITE, ChatColor.AQUA, player.getDisplayName(), ChatColor.WHITE)); } if (hasPermission) { - this.plugin.log(Level.FINER, "Checking to see if player [" + player.getName() + "] has permission [" + node + "]... YES"); + Logging.finer("Checking to see if player [" + player.getName() + "] has permission [" + node + "]... YES"); } else { - this.plugin.log(Level.FINER, "Checking to see if player [" + player.getName() + "] has permission [" + node + "]... NO"); + Logging.finer("Checking to see if player [" + player.getName() + "] has permission [" + node + "]... NO"); } return hasPermission; } diff --git a/src/main/java/com/onarandombox/MultiverseCore/utils/PermissionTools.java b/src/main/java/com/onarandombox/MultiverseCore/utils/PermissionTools.java index a6e98820..ef5e08c4 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/utils/PermissionTools.java +++ b/src/main/java/com/onarandombox/MultiverseCore/utils/PermissionTools.java @@ -7,6 +7,7 @@ package com.onarandombox.MultiverseCore.utils; +import com.dumptruckman.minecraft.util.Logging; import com.onarandombox.MultiverseCore.MultiverseCore; import com.onarandombox.MultiverseCore.api.MultiverseWorld; import org.bukkit.Material; @@ -195,7 +196,7 @@ public class PermissionTools { * @return True if they can't go to the world, False if they can. */ public boolean playerCanGoFromTo(MultiverseWorld fromWorld, MultiverseWorld toWorld, CommandSender teleporter, Player teleportee) { - this.plugin.log(Level.FINEST, "Checking '" + teleporter + "' can send '" + teleportee + "' somewhere"); + Logging.finest("Checking '" + teleporter + "' can send '" + teleportee + "' somewhere"); Player teleporterPlayer; if (plugin.getMVConfig().getTeleportIntercept()) { diff --git a/src/main/java/com/onarandombox/MultiverseCore/utils/PlayerFinder.java b/src/main/java/com/onarandombox/MultiverseCore/utils/PlayerFinder.java new file mode 100644 index 00000000..31845008 --- /dev/null +++ b/src/main/java/com/onarandombox/MultiverseCore/utils/PlayerFinder.java @@ -0,0 +1,176 @@ +package com.onarandombox.MultiverseCore.utils; + +import com.dumptruckman.minecraft.util.Logging; +import org.bukkit.Bukkit; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.util.ArrayList; +import java.util.List; +import java.util.UUID; +import java.util.regex.Pattern; +import java.util.stream.Collectors; + +/** + * Helper class to get {@link Player} from name, UUID or Selectors. + */ +public class PlayerFinder { + + private static final Pattern UUID_REGEX = Pattern.compile("[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}"); + private static final Pattern COMMA_SPLIT = Pattern.compile(","); + + /** + * Get a {@link Player} based on an identifier of name UUID or selector. + * + * @param playerIdentifier An identifier of name UUID or selector. + * @param sender Target sender for selector. + * @return The player if found, else null. + */ + @Nullable + public static Player get(@NotNull String playerIdentifier, + @NotNull CommandSender sender) { + + Player targetPlayer = getByName(playerIdentifier); + if (targetPlayer != null) { + return targetPlayer; + } + targetPlayer = getByUuid(playerIdentifier); + if (targetPlayer != null) { + return targetPlayer; + } + return getBySelector(playerIdentifier, sender); + } + + /** + * Get multiple {@link Player} based on many identifiers of name UUID or selector. + * + * @param playerIdentifiers An identifier of multiple names, UUIDs or selectors, separated by comma. + * @param sender Target sender for selector. + * @return A list of all the {@link Player} found. + */ + @Nullable + public static List getMulti(@NotNull String playerIdentifiers, + @NotNull CommandSender sender) { + + String[] playerIdentifierArray = COMMA_SPLIT.split(playerIdentifiers); + if (playerIdentifierArray == null || playerIdentifierArray.length == 0) { + return null; + } + + List playerResults = new ArrayList<>(); + for (String playerIdentifier : playerIdentifierArray) { + Player targetPlayer = getByName(playerIdentifier); + if (targetPlayer != null) { + playerResults.add(targetPlayer); + continue; + } + targetPlayer = getByUuid(playerIdentifier); + if (targetPlayer != null) { + playerResults.add(targetPlayer); + continue; + } + List targetPlayers = getMultiBySelector(playerIdentifier, sender); + if (targetPlayers != null) { + playerResults.addAll(targetPlayers); + } + } + return playerResults; + } + + /** + * Get a {@link Player} based on player name. + * + * @param playerName Name of a {@link Player}. + * @return The player if found, else null. + */ + @Nullable + public static Player getByName(@NotNull String playerName) { + return Bukkit.getPlayerExact(playerName); + } + + /** + * Get a {@link Player} based on player UUID. + * + * @param playerUuid UUID of a player. + * @return The player if found, else null. + */ + @Nullable + public static Player getByUuid(@NotNull String playerUuid) { + if (!UUID_REGEX.matcher(playerUuid).matches()) { + return null; + } + UUID uuid; + try { + uuid = UUID.fromString(playerUuid); + } catch (Exception e) { + return null; + } + return getByUuid(uuid); + } + + /** + * Get a {@link Player} based on playerUUID. + * + * @param playerUuid UUID of a player. + * @return The player if found, else null. + */ + @Nullable + public static Player getByUuid(@NotNull UUID playerUuid) { + return Bukkit.getPlayer(playerUuid); + } + + /** + * Get a {@link Player} based on vanilla selectors. + * https://minecraft.gamepedia.com/Commands#Target_selectors + * + * @param playerSelector A target selector, usually starts with an '@'. + * @param sender Target sender for selector. + * @return The player if only one found, else null. + */ + @Nullable + public static Player getBySelector(@NotNull String playerSelector, + @NotNull CommandSender sender) { + + List matchedPlayers = getMultiBySelector(playerSelector, sender); + if (matchedPlayers == null || matchedPlayers.isEmpty()) { + Logging.fine("No player found with selector '%s' for %s.", playerSelector, sender.getName()); + return null; + } + if (matchedPlayers.size() > 1) { + Logging.warning("Ambiguous selector result '%s' for %s (more than one player matched) - %s", + playerSelector, sender.getName(), matchedPlayers.toString()); + return null; + } + return matchedPlayers.get(0); + } + + /** + * Get multiple {@link Player} based on selector. + * https://minecraft.gamepedia.com/Commands#Target_selectors + * + * @param playerSelector A target selector, usually starts with an '@'. + * @param sender Target sender for selector. + * @return A list of all the {@link Player} found. + */ + @Nullable + public static List getMultiBySelector(@NotNull String playerSelector, + @NotNull CommandSender sender) { + + if (playerSelector.charAt(0) != '@') { + return null; + } + try { + return Bukkit.selectEntities(sender, playerSelector).stream() + .filter(e -> e instanceof Player) + .map(e -> ((Player) e)) + .collect(Collectors.toList()); + } catch (IllegalArgumentException e) { + Logging.warning("An error occurred while parsing selector '%s' for %s. Is it is the correct format?", + playerSelector, sender.getName()); + e.printStackTrace(); + return null; + } + } +} diff --git a/src/main/java/com/onarandombox/MultiverseCore/utils/PurgeWorlds.java b/src/main/java/com/onarandombox/MultiverseCore/utils/PurgeWorlds.java index c932d2d8..dbd3bf87 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/utils/PurgeWorlds.java +++ b/src/main/java/com/onarandombox/MultiverseCore/utils/PurgeWorlds.java @@ -7,6 +7,7 @@ package com.onarandombox.MultiverseCore.utils; +import com.dumptruckman.minecraft.util.Logging; import com.onarandombox.MultiverseCore.MultiverseCore; import com.onarandombox.MultiverseCore.api.MultiverseWorld; @@ -86,7 +87,7 @@ public class PurgeWorlds { } int entitiesKilled = 0; for (Entity e : world.getEntities()) { - this.plugin.log(Level.FINEST, "Entity list (aval for purge) from WORLD < " + mvworld.getName() + " >: " + e.toString()); + Logging.finest("Entity list (aval for purge) from WORLD < " + mvworld.getName() + " >: " + e.toString()); // Check against Monsters if (killMonster(mvworld, e, thingsToKill, negateMonsters)) { @@ -133,16 +134,16 @@ public class PurgeWorlds { entityName = e.toString().replaceAll("Craft", "").toUpperCase(); } if (e instanceof Slime || e instanceof Monster || e instanceof Ghast || e instanceof EnderDragon) { - this.plugin.log(Level.FINEST, "Looking at a monster: " + e); + Logging.finest("Looking at a monster: " + e); if (creaturesToKill.contains(entityName) || creaturesToKill.contains("ALL") || creaturesToKill.contains("MONSTERS")) { if (!negate) { - this.plugin.log(Level.FINEST, "Removing a monster: " + e); + Logging.finest("Removing a monster: " + e); e.remove(); return true; } } else { if (negate) { - this.plugin.log(Level.FINEST, "Removing a monster: " + e); + Logging.finest("Removing a monster: " + e); e.remove(); return true; } diff --git a/src/main/java/com/onarandombox/MultiverseCore/utils/ReflectHelper.java b/src/main/java/com/onarandombox/MultiverseCore/utils/ReflectHelper.java new file mode 100644 index 00000000..9e81bfe2 --- /dev/null +++ b/src/main/java/com/onarandombox/MultiverseCore/utils/ReflectHelper.java @@ -0,0 +1,140 @@ +package com.onarandombox.MultiverseCore.utils; + +import org.jetbrains.annotations.Nullable; + +import java.lang.reflect.Field; +import java.lang.reflect.Method; + +/** + * Utility class used to help in doing various reflection actions. + */ +public class ReflectHelper { + + /** + * Try to get the {@link Class} based on its classpath. + * + * @param classPath The target classpath. + * @return A {@link Class} if found, else null. + */ + @Nullable + public static Class getClass(String classPath) { + try { + return Class.forName(classPath); + } catch (ClassNotFoundException e) { + return null; + } + } + + /** + * Check if the {@link Class} for a give classpath is present/valid. + * + * @param classPath Target classpath. + * @return True if class path is a valid class, else false. + */ + public static boolean hasClass(String classPath) { + return getClass(classPath) != null; + } + + /** + * Try to get a {@link Method} from a given class. + * + * @param clazz The class to search the method on. + * @param methodName Name of the method to get. + * @param parameterTypes Parameters present for that method. + * @param The class type. + * @return A {@link Method} if found, else null. + */ + @Nullable + public static Method getMethod(Class clazz, String methodName, Class... parameterTypes) { + try { + Method method = clazz.getDeclaredMethod(methodName, parameterTypes); + method.setAccessible(true); + return method; + } catch (NoSuchMethodException e) { + return null; + } + } + + /** + * Try to get a {@link Method} from a given class. + * + * @param classInstance Instance of the class to search the method on. + * @param methodName Name of the method to get. + * @param parameterTypes Parameters present for that method. + * @param The class type. + * @return A {@link Method} if found, else null. + */ + @Nullable + public static Method getMethod(C classInstance, String methodName, Class... parameterTypes) { + return getMethod(classInstance.getClass(), methodName, parameterTypes); + } + + /** + * Calls a {@link Method}. + * + * @param classInstance Instance of the class responsible for the method. + * @param method The method to call. + * @param parameters Parameters needed when calling the method. + * @param The class type. + * @param The return type. + * @return Return value of the method call if any, else null. + */ + @Nullable + public static R invokeMethod(C classInstance, Method method, Object...parameters) { + try { + return (R) method.invoke(classInstance, parameters); + } catch (Exception e) { + return null; + } + } + + /** + * Try to get a {@link Field} from a given class. + * + * @param clazz The class to search the field on. + * @param fieldName Name of the field to get. + * @param The class type. + * @return A {@link Field} if found, else null. + */ + @Nullable + public static Field getField(Class clazz, String fieldName) { + try { + Field field = clazz.getDeclaredField(fieldName); + field.setAccessible(true); + return field; + } catch (NoSuchFieldException e) { + return null; + } + } + + /** + * Try to get a {@link Field} from a given class. + * + * @param classInstance Instance of the class to search the field on. + * @param fieldName Name of the field to get. + * @param The class type. + * @return A {@link Field} if found, else null. + */ + @Nullable + public static Field getField(C classInstance, String fieldName) { + return getField(classInstance.getClass(), fieldName); + } + + /** + * Gets the value of an {@link Field} from an instance of the class responsible. + * + * @param classInstance Instance of the class to get the field value from. + * @param field The field to get value from. + * @param The class type. + * @param The field value type. + * @return The field value if any, else null. + */ + @Nullable + public static V getFieldValue(C classInstance, Field field) { + try { + return (V) field.get(classInstance); + } catch (IllegalAccessException e) { + return null; + } + } +} diff --git a/src/main/java/com/onarandombox/MultiverseCore/utils/SimpleLocationManipulation.java b/src/main/java/com/onarandombox/MultiverseCore/utils/SimpleLocationManipulation.java index 3d0436b1..92767d63 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/utils/SimpleLocationManipulation.java +++ b/src/main/java/com/onarandombox/MultiverseCore/utils/SimpleLocationManipulation.java @@ -19,6 +19,7 @@ import com.onarandombox.MultiverseCore.api.LocationManipulation; import java.text.DecimalFormat; import java.util.Collections; import java.util.HashMap; +import java.util.Locale; import java.util.Map; /** @@ -39,6 +40,15 @@ public class SimpleLocationManipulation implements LocationManipulation { orientationInts.put("w", 90); orientationInts.put("nw", 135); + orientationInts.put("north", 180); + orientationInts.put("northeast", 225); + orientationInts.put("east", 270); + orientationInts.put("southeast", 315); + orientationInts.put("south", 0); + orientationInts.put("southwest", 45); + orientationInts.put("west", 90); + orientationInts.put("northwest", 135); + // "freeze" the map: ORIENTATION_INTS = Collections.unmodifiableMap(orientationInts); // END CHECKSTYLE-SUPPRESSION: MagicNumberCheck @@ -52,7 +62,7 @@ public class SimpleLocationManipulation implements LocationManipulation { if (location == null) { return ""; } - return String.format("%s:%.2f,%.2f,%.2f:%.2f:%.2f", location.getWorld().getName(), + return String.format(Locale.ENGLISH, "%s:%.2f,%.2f,%.2f:%.2f:%.2f", location.getWorld().getName(), location.getX(), location.getY(), location.getZ(), location.getYaw(), location.getPitch()); } diff --git a/src/main/java/com/onarandombox/MultiverseCore/utils/SimpleSafeTTeleporter.java b/src/main/java/com/onarandombox/MultiverseCore/utils/SimpleSafeTTeleporter.java index baae83e5..4ce22348 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/utils/SimpleSafeTTeleporter.java +++ b/src/main/java/com/onarandombox/MultiverseCore/utils/SimpleSafeTTeleporter.java @@ -7,6 +7,7 @@ package com.onarandombox.MultiverseCore.utils; +import com.dumptruckman.minecraft.util.Logging; import com.onarandombox.MultiverseCore.MultiverseCore; import com.onarandombox.MultiverseCore.api.SafeTTeleporter; import com.onarandombox.MultiverseCore.api.MVDestination; @@ -58,9 +59,9 @@ public class SimpleSafeTTeleporter implements SafeTTeleporter { if (safe != null) { safe.setX(safe.getBlockX() + .5); // SUPPRESS CHECKSTYLE: MagicNumberCheck safe.setZ(safe.getBlockZ() + .5); // SUPPRESS CHECKSTYLE: MagicNumberCheck - this.plugin.log(Level.FINE, "Hey! I found one: " + plugin.getLocationManipulation().strCoordsRaw(safe)); + Logging.fine("Hey! I found one: " + plugin.getLocationManipulation().strCoordsRaw(safe)); } else { - this.plugin.log(Level.FINE, "Uh oh! No safe place found!"); + Logging.fine("Uh oh! No safe place found!"); } return safe; } @@ -72,8 +73,8 @@ public class SimpleSafeTTeleporter implements SafeTTeleporter { } // We want half of it, so we can go up and down tolerance /= 2; - this.plugin.log(Level.FINER, "Given Location of: " + plugin.getLocationManipulation().strCoordsRaw(l)); - this.plugin.log(Level.FINER, "Checking +-" + tolerance + " with a radius of " + radius); + Logging.finer("Given Location of: " + plugin.getLocationManipulation().strCoordsRaw(l)); + Logging.finer("Checking +-" + tolerance + " with a radius of " + radius); // For now this will just do a straight up block. Location locToCheck = l.clone(); @@ -191,7 +192,7 @@ public class SimpleSafeTTeleporter implements SafeTTeleporter { @Override public TeleportResult safelyTeleport(CommandSender teleporter, Entity teleportee, MVDestination d) { if (d instanceof InvalidDestination) { - this.plugin.log(Level.FINER, "Entity tried to teleport to an invalid destination"); + Logging.finer("Entity tried to teleport to an invalid destination"); return TeleportResult.FAIL_INVALID; } Player teleporteePlayer = null; @@ -248,7 +249,7 @@ public class SimpleSafeTTeleporter implements SafeTTeleporter { public Location getSafeLocation(Entity e, MVDestination d) { Location l = d.getLocation(e); if (plugin.getBlockSafety().playerCanSpawnHereSafely(l)) { - plugin.log(Level.FINE, "The first location you gave me was safe."); + Logging.fine("The first location you gave me was safe."); return l; } if (e instanceof Minecart) { @@ -267,21 +268,21 @@ public class SimpleSafeTTeleporter implements SafeTTeleporter { // Add offset to account for a vehicle on dry land! if (e instanceof Minecart && !plugin.getBlockSafety().isEntitiyOnTrack(safeLocation)) { safeLocation.setY(safeLocation.getBlockY() + .5); - this.plugin.log(Level.FINER, "Player was inside a minecart. Offsetting Y location."); + Logging.finer("Player was inside a minecart. Offsetting Y location."); } - this.plugin.log(Level.FINE, "Had to look for a bit, but I found a safe place for ya!"); + Logging.finer("Had to look for a bit, but I found a safe place for ya!"); return safeLocation; } if (e instanceof Player) { Player p = (Player) e; this.plugin.getMessaging().sendMessage(p, "No safe locations found!", false); - this.plugin.log(Level.FINER, "No safe location found for " + p.getName()); + Logging.finer("No safe location found for " + p.getName()); } else if (e.getPassenger() instanceof Player) { Player p = (Player) e.getPassenger(); this.plugin.getMessaging().sendMessage(p, "No safe locations found!", false); - this.plugin.log(Level.FINER, "No safe location found for " + p.getName()); + Logging.finer("No safe location found for " + p.getName()); } - this.plugin.log(Level.FINE, "Sorry champ, you're basically trying to teleport into a minefield. I should just kill you now."); + Logging.fine("Sorry champ, you're basically trying to teleport into a minefield. I should just kill you now."); return null; } diff --git a/src/main/java/com/onarandombox/MultiverseCore/utils/TestingMode.java b/src/main/java/com/onarandombox/MultiverseCore/utils/TestingMode.java new file mode 100644 index 00000000..81bb0f6e --- /dev/null +++ b/src/main/java/com/onarandombox/MultiverseCore/utils/TestingMode.java @@ -0,0 +1,18 @@ +package com.onarandombox.MultiverseCore.utils; + +/** + * A utility class that enables automated tests to flag Multiverse for testing. This allows Multiverse to not perform + * certain behaviors such as enabled stats uploads. + */ +public class TestingMode { + + private static boolean enabled = false; + + public static void enable() { + enabled = true; + } + + public static boolean isDisabled() { + return !enabled; + } +} diff --git a/src/main/java/com/onarandombox/MultiverseCore/utils/WorldManager.java b/src/main/java/com/onarandombox/MultiverseCore/utils/WorldManager.java index 41cd707e..0b2ab070 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/utils/WorldManager.java +++ b/src/main/java/com/onarandombox/MultiverseCore/utils/WorldManager.java @@ -18,6 +18,7 @@ import com.onarandombox.MultiverseCore.api.SafeTTeleporter; import com.onarandombox.MultiverseCore.api.WorldPurger; import com.onarandombox.MultiverseCore.event.MVWorldDeleteEvent; import org.bukkit.Bukkit; +import org.bukkit.GameRule; import org.bukkit.Location; import org.bukkit.World; import org.bukkit.World.Environment; @@ -36,7 +37,9 @@ import java.io.File; import java.io.FilenameFilter; import java.io.IOException; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collection; +import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -45,7 +48,8 @@ import java.util.Set; import java.util.Stack; import java.util.concurrent.Callable; import java.util.concurrent.ConcurrentHashMap; -import java.util.logging.Level; +import java.util.regex.Pattern; +import java.util.stream.Collectors; /** * Public facing API to add/remove Multiverse worlds. @@ -87,7 +91,7 @@ public class WorldManager implements MVWorldManager { } } } else { - this.plugin.log(Level.WARNING, "Could not read 'bukkit.yml'. Any Default worldgenerators will not be loaded!"); + Logging.warning("Could not read 'bukkit.yml'. Any Default worldgenerators will not be loaded!"); } } @@ -125,12 +129,18 @@ public class WorldManager implements MVWorldManager { return false; } + // Check for valid world name + if (!(WorldNameChecker.isValidWorldName(oldName) && WorldNameChecker.isValidWorldName(newName))) { + return false; + } + final File oldWorldFile = new File(this.plugin.getServer().getWorldContainer(), oldName); final File newWorldFile = new File(this.plugin.getServer().getWorldContainer(), newName); + final List ignoreFiles = new ArrayList<>(Arrays.asList("session.lock", "uid.dat")); // Make sure the new world doesn't exist outside of multiverse. if (newWorldFile.exists()) { - Logging.warning("File for new world '%s' already exists", newName); + Logging.warning("Folder for new world '%s' already exists", newName); return false; } @@ -152,14 +162,8 @@ public class WorldManager implements MVWorldManager { } // Grab a bit of metadata from the old world. - MVWorld oldWorld = (MVWorld) getMVWorld(oldName); - Environment environment = oldWorld.getEnvironment(); - String seedString = oldWorld.getSeed() + ""; - WorldType worldType = oldWorld.getWorldType(); - Boolean generateStructures = oldWorld.getCBWorld().canGenerateStructures(); - String generator = oldWorld.getGenerator(); - boolean useSpawnAdjust = oldWorld.getAdjustSpawn(); - + MultiverseWorld oldWorld = getMVWorld(oldName); + // Don't need the loaded world anymore. if (wasJustLoaded) { this.unloadWorld(oldName, true); @@ -177,32 +181,40 @@ public class WorldManager implements MVWorldManager { oldWorld.getCBWorld().save(); } Logging.config("Copying files for world '%s'", oldName); - if (!FileUtils.copyFolder(oldWorldFile, newWorldFile, Logging.getLogger())) { + if (!FileUtils.copyFolder(oldWorldFile, newWorldFile, ignoreFiles)) { Logging.warning("Failed to copy files for world '%s', see the log info", newName); return false; } if (oldWorld != null && wasAutoSave) { oldWorld.getCBWorld().setAutoSave(true); } + + if (newWorldFile.exists()) { + Logging.fine("Succeeded at copying files"); - File uidFile = new File(newWorldFile, "uid.dat"); - if (uidFile.exists() && !uidFile.delete()) { - Logging.warning("Failed to delete unique ID file for world '%s'", newName); + // initialize new properties with old ones + WorldProperties newProps = new WorldProperties(); + newProps.copyValues(this.worldsFromTheConfig.get(oldName)); + // don't keep the alias the same -- that would be useless + newProps.setAlias(""); + // store the new properties in worlds config map + this.worldsFromTheConfig.put(newName, newProps); + + // save the worlds config to disk (worlds.yml) + if (!saveWorldsConfig()) { + Logging.severe("Failed to save worlds.yml"); + return false; + } + + // actually load the world + if (doLoad(newName)) { + Logging.fine("Succeeded at loading cloned world '" + newName + "'"); + return true; + } + Logging.severe("Failed to load the cloned world '" + newName + "'"); return false; } - if (newWorldFile.exists()) { - Logging.fine("Succeeded at copying files"); - if (this.addWorld(newName, environment, seedString, worldType, generateStructures, generator, useSpawnAdjust)) { - // getMVWorld() doesn't actually return an MVWorld - Logging.fine("Succeeded at importing world"); - MVWorld newWorld = (MVWorld) this.getMVWorld(newName); - newWorld.copyValues(this.worldsFromTheConfig.get(oldName)); - // don't keep the alias the same -- that would be useless - newWorld.setAlias(null); - return true; - } - } Logging.warning("Failed to copy files for world '%s', see the log info", newName); return false; } @@ -225,6 +237,13 @@ public class WorldManager implements MVWorldManager { if (name.equalsIgnoreCase("plugins") || name.equalsIgnoreCase("logs")) { return false; } + + if (!WorldNameChecker.isValidWorldName(name)) { + Logging.warning("Invalid world name '" + name + "'"); + Logging.warning("World name should not contain spaces or special characters!"); + return false; + } + Long seed = null; WorldCreator c = new WorldCreator(name); if (seedString != null && seedString.length() > 0) { @@ -267,7 +286,7 @@ public class WorldManager implements MVWorldManager { Logging.info(builder.toString()); if (!doLoad(c, true)) { - this.plugin.log(Level.SEVERE, "Failed to Create/Load the world '" + name + "'"); + Logging.severe("Failed to Create/Load the world '" + name + "'"); return false; } @@ -340,7 +359,7 @@ public class WorldManager implements MVWorldManager { MultiverseWorld world = this.getMVWorld(this.firstSpawn); if (world == null) { // If the spawn world was unloaded, get the default world - this.plugin.log(Level.WARNING, "The world specified as the spawn world (" + this.firstSpawn + ") did not exist!!"); + Logging.warning("The world specified as the spawn world (" + this.firstSpawn + ") did not exist!!"); try { return this.getMVWorld(this.plugin.getServer().getWorlds().get(0)); } catch (IndexOutOfBoundsException e) { @@ -368,14 +387,14 @@ public class WorldManager implements MVWorldManager { this.worldsFromTheConfig.get(name).cacheVirtualProperties(); if (unloadBukkit && this.unloadWorldFromBukkit(name, true)) { this.worlds.remove(name); - Logging.info("World '%s' was unloaded from memory.", name); + Logging.info("World '%s' was unloaded from Bukkit.", name); return true; } else if (!unloadBukkit){ this.worlds.remove(name); - Logging.info("World '%s' was unloaded from memory.", name); + Logging.info("World '%s' was unloaded from Multiverse.", name); return true; } else { - Logging.warning("World '%s' could not be unloaded. Is it a default world?", name); + Logging.warning("World '%s' could not be unloaded from Bukkit. Is it a default world?", name); } } else if (this.plugin.getServer().getWorld(name) != null) { Logging.warning("Hmm Multiverse does not know about this world but it's loaded in memory."); @@ -408,15 +427,15 @@ public class WorldManager implements MVWorldManager { } private void brokenWorld(String name) { - this.plugin.log(Level.SEVERE, "The world '" + name + "' could NOT be loaded because it contains errors and is probably corrupt!"); - this.plugin.log(Level.SEVERE, "Try using Minecraft Region Fixer to repair your world! '" + name + "'"); - this.plugin.log(Level.SEVERE, "https://github.com/Fenixin/Minecraft-Region-Fixer"); + Logging.severe("The world '" + name + "' could NOT be loaded because it contains errors and is probably corrupt!"); + Logging.severe("Try using Minecraft Region Fixer to repair your world! '" + name + "'"); + Logging.severe("https://github.com/Fenixin/Minecraft-Region-Fixer"); } private void nullWorld(String name) { - this.plugin.log(Level.SEVERE, "The world '" + name + "' could NOT be loaded because the server didn't like it!"); - this.plugin.log(Level.SEVERE, "We don't really know why this is. Contact the developer of your server software!"); - this.plugin.log(Level.SEVERE, "Server version info: " + Bukkit.getServer().getVersion()); + Logging.severe("The world '" + name + "' could NOT be loaded because the server didn't like it!"); + Logging.severe("We don't really know why this is. Contact the developer of your server software!"); + Logging.severe("Server version info: " + Bukkit.getServer().getVersion()); } private boolean doLoad(String name) { @@ -456,8 +475,8 @@ public class WorldManager implements MVWorldManager { throw new IllegalArgumentException("That world is already loaded!"); if (!ignoreExists && !new File(this.plugin.getServer().getWorldContainer(), worldName).exists() && !new File(this.plugin.getServer().getWorldContainer().getParent(), worldName).exists()) { - this.plugin.log(Level.WARNING, "WorldManager: Can't load this world because the folder was deleted/moved: " + worldName); - this.plugin.log(Level.WARNING, "Use '/mv remove' to remove it from the config!"); + Logging.warning("WorldManager: Can't load this world because the folder was deleted/moved: " + worldName); + Logging.warning("Use '/mv remove' to remove it from the config!"); return false; } @@ -487,6 +506,13 @@ public class WorldManager implements MVWorldManager { */ @Override public boolean deleteWorld(String name, boolean removeFromConfig, boolean deleteWorldFolder) { + if (this.hasUnloadedWorld(name, false)) { + // Attempt to load if unloaded so we can actually delete the world + if (!this.doLoad(name)) { + return false; + } + } + World world = this.plugin.getServer().getWorld(name); if (world == null) { // We can only delete loaded worlds @@ -497,7 +523,7 @@ public class WorldManager implements MVWorldManager { MVWorldDeleteEvent mvwde = new MVWorldDeleteEvent(getMVWorld(name), removeFromConfig); this.plugin.getServer().getPluginManager().callEvent(mvwde); if (mvwde.isCancelled()) { - this.plugin.log(Level.FINE, "Tried to delete a world, but the event was cancelled!"); + Logging.fine("Tried to delete a world, but the event was cancelled!"); return false; } @@ -513,7 +539,7 @@ public class WorldManager implements MVWorldManager { try { File worldFile = world.getWorldFolder(); - plugin.log(Level.FINER, "deleteWorld(): worldFile: " + worldFile.getAbsolutePath()); + Logging.finer("deleteWorld(): worldFile: " + worldFile.getAbsolutePath()); if (deleteWorldFolder ? FileUtils.deleteFolder(worldFile) : FileUtils.deleteFolderContents(worldFile)) { Logging.info("World '%s' was DELETED.", name); return true; @@ -591,6 +617,14 @@ public class WorldManager implements MVWorldManager { */ @Override public MultiverseWorld getMVWorld(String name) { + return this.getMVWorld(name, true); + } + + /** + * {@inheritDoc} + */ + @Override + public MultiverseWorld getMVWorld(String name, boolean checkAliases) { if (name == null) { return null; } @@ -598,7 +632,7 @@ public class WorldManager implements MVWorldManager { if (world != null) { return world; } - return this.getMVWorldByAlias(name); + return (checkAliases) ? this.getMVWorldByAlias(name) : null; } /** @@ -607,7 +641,7 @@ public class WorldManager implements MVWorldManager { @Override public MultiverseWorld getMVWorld(World world) { if (world != null) { - return this.getMVWorld(world.getName()); + return this.getMVWorld(world.getName(), false); } return null; } @@ -632,7 +666,15 @@ public class WorldManager implements MVWorldManager { */ @Override public boolean isMVWorld(final String name) { - return (this.worlds.containsKey(name) || isMVWorldAlias(name)); + return this.isMVWorld(name, true); + } + + /** + * {@inheritDoc} + */ + @Override + public boolean isMVWorld(final String name, boolean checkAliases) { + return this.worlds.containsKey(name) || (checkAliases && this.isMVWorldAlias(name)); } /** @@ -827,7 +869,7 @@ public class WorldManager implements MVWorldManager { this.configWorlds.save(new File(this.plugin.getDataFolder(), "worlds.yml")); return true; } catch (IOException e) { - this.plugin.log(Level.SEVERE, "Could not save worlds.yml. Please check your settings."); + Logging.severe("Could not save worlds.yml. Please check your settings."); return false; } } @@ -855,12 +897,23 @@ public class WorldManager implements MVWorldManager { */ @Override public boolean regenWorld(String name, boolean useNewSeed, boolean randomSeed, String seed) { + return regenWorld(name, useNewSeed, randomSeed, seed, false); + } + + /** + * {@inheritDoc} + */ + @Override + public boolean regenWorld(String name, boolean useNewSeed, boolean randomSeed, String seed, boolean keepGameRules) { MultiverseWorld world = this.getMVWorld(name); - if (world == null) + if (world == null) { + Logging.warning("Unable to regen a world that does not exist!"); return false; + } List ps = world.getCBWorld().getPlayers(); + // Apply new seed if needed. if (useNewSeed) { long theSeed; @@ -876,19 +929,64 @@ public class WorldManager implements MVWorldManager { world.setSeed(theSeed); } + WorldType type = world.getWorldType(); - if (this.deleteWorld(name, false, false)) { - this.doLoad(name, true, type); - SafeTTeleporter teleporter = this.plugin.getSafeTTeleporter(); - Location newSpawn = world.getSpawnLocation(); - // Send all players that were in the old world, BACK to it! - for (Player p : ps) { - teleporter.safelyTeleport(null, p, newSpawn, true); + // Save current GameRules if needed. + Map, Object> gameRuleMap = null; + if (keepGameRules) { + gameRuleMap = new HashMap<>(GameRule.values().length); + World CBWorld = world.getCBWorld(); + for (GameRule gameRule : GameRule.values()) { + // Only save if not default value. + Object value = CBWorld.getGameRuleValue(gameRule); + if (value != CBWorld.getGameRuleDefault(gameRule)) { + gameRuleMap.put(gameRule, value); + } } - return true; } - return false; + + // Do the regen. + if (!this.deleteWorld(name, false, false)) { + Logging.severe("Unable to regen world as world cannot be deleted."); + return false; + } + if (!this.doLoad(name, true, type)) { + Logging.severe("Unable to regen world as world cannot be loaded."); + return false; + } + + // Get new MultiverseWorld reference. + world = this.getMVWorld(name); + + // Load back GameRules if needed. + if (keepGameRules) { + Logging.fine("Restoring previous world's GameRules..."); + World CBWorld = world.getCBWorld(); + for (Map.Entry, Object> gameRuleEntry : gameRuleMap.entrySet()) { + if (!setGameRuleValue(CBWorld, gameRuleEntry.getKey(), gameRuleEntry.getValue())) { + Logging.warning("Unable to set GameRule '%s' to '%s' on regen world.", + gameRuleEntry.getKey().getName(), gameRuleEntry.getValue()); + } + } + } + + // Send all players that were in the old world, BACK to it! + SafeTTeleporter teleporter = this.plugin.getSafeTTeleporter(); + Location newSpawn = world.getSpawnLocation(); + for (Player p : ps) { + teleporter.safelyTeleport(null, p, newSpawn, true); + } + + return true; + } + + private boolean setGameRuleValue(World world, GameRule gameRule, Object value) { + try { + return world.setGameRule(gameRule, (T) value); + } catch (Exception e) { + return false; + } } /** @@ -899,6 +997,9 @@ public class WorldManager implements MVWorldManager { return this.configWorlds; } + /** + * {@inheritDoc} + */ @Override public boolean hasUnloadedWorld(String name, boolean includeLoaded) { if (getMVWorld(name) != null) { @@ -911,4 +1012,21 @@ public class WorldManager implements MVWorldManager { } return false; } + + /** + * {@inheritDoc} + */ + @Override + public Collection getPotentialWorlds() { + File worldContainer = this.plugin.getServer().getWorldContainer(); + if (worldContainer == null) { + return Collections.emptyList(); + } + return Arrays.stream(worldContainer.listFiles()) + .filter(File::isDirectory) + .filter(folder -> !this.isMVWorld(folder.getName(), false)) + .filter(WorldNameChecker::isValidWorldFolder) + .map(File::getName) + .collect(Collectors.toList()); + } } diff --git a/src/main/java/com/onarandombox/MultiverseCore/utils/WorldNameChecker.java b/src/main/java/com/onarandombox/MultiverseCore/utils/WorldNameChecker.java new file mode 100644 index 00000000..7e484167 --- /dev/null +++ b/src/main/java/com/onarandombox/MultiverseCore/utils/WorldNameChecker.java @@ -0,0 +1,159 @@ +package com.onarandombox.MultiverseCore.utils; + +import org.bukkit.Bukkit; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.io.File; +import java.util.Collections; +import java.util.HashSet; +import java.util.Set; +import java.util.regex.Pattern; + +/** + *

Utility class in helping to check the status of a world name and it's associated world folder.

+ * + *

Note this is for preliminary checks and better command output. A valid result will suggest but not + * 100% determine that a world name can be created, loaded or imported.

+ */ +public class WorldNameChecker { + + private static final Pattern WORLD_NAME_PATTERN = Pattern.compile("[a-zA-Z0-9/._-]+"); + private static final Set BLACKLIST_NAMES = Collections.unmodifiableSet(new HashSet() {{ + add("plugins"); + add("logs"); + add("cache"); + add("crash-reports"); + }}); + + /** + * Checks if a world name is valid. + * + * @param worldName The world name to check on. + * @return True if check result is valid, else false. + */ + public static boolean isValidWorldName(@Nullable String worldName) { + return checkName(worldName) == NameStatus.VALID; + } + + /** + * Checks the current validity status of a world name. + * + * @param worldName The world name to check on. + * @return The resulting name status. + */ + @NotNull + public static NameStatus checkName(@Nullable String worldName) { + if (BLACKLIST_NAMES.contains(worldName)) { + return NameStatus.BLACKLISTED; + } + if (worldName == null || !WORLD_NAME_PATTERN.matcher(worldName).matches()) { + return NameStatus.INVALID_CHARS; + } + return NameStatus.VALID; + } + + /** + * Checks if a world name has a valid world folder. + * + * @param worldName The world name to check on. + * @return True if check result is valid, else false. + */ + public static boolean isValidWorldFolder(@Nullable String worldName) { + return checkFolder(worldName) == FolderStatus.VALID; + } + + /** + * Checks if a world folder is valid. + * + * @param worldFolder The world folder to check on. + * @return True if check result is valid, else false. + */ + public static boolean isValidWorldFolder(@Nullable File worldFolder) { + return checkFolder(worldFolder) == FolderStatus.VALID; + } + + /** + * Checks the current folder status for a world name. + * + * @param worldName The world name to check on. + * @return The resulting folder status. + */ + @NotNull + public static FolderStatus checkFolder(@Nullable String worldName) { + if (worldName == null) { + return FolderStatus.DOES_NOT_EXIST; + } + File worldFolder = new File(Bukkit.getWorldContainer(), worldName); + return checkFolder(worldFolder); + } + + /** + * Checks the current folder status. + * + * @param worldFolder The world folder to check on. + * @return The resulting folder status. + */ + @NotNull + public static FolderStatus checkFolder(@Nullable File worldFolder) { + if (worldFolder == null || !worldFolder.exists() || !worldFolder.isDirectory()) { + return FolderStatus.DOES_NOT_EXIST; + } + if (!folderHasDat(worldFolder)) { + return FolderStatus.NOT_A_WORLD; + } + return FolderStatus.VALID; + } + + /** + * A very basic check to see if a folder has a level.dat file. If it does, we can safely assume + * it's a world folder. + * + * @param worldFolder The File that may be a world. + * @return True if it looks like a world, else false. + */ + private static boolean folderHasDat(@NotNull File worldFolder) { + File[] files = worldFolder.listFiles((file, name) -> name.toLowerCase().endsWith(".dat")); + return files != null && files.length > 0; + } + + /** + * Result after checking validity of world name. + */ + public enum NameStatus { + /** + * Name is valid. + */ + VALID, + + /** + * Name not valid as it contains invalid characters. + */ + INVALID_CHARS, + + /** + * Name not valid as it is deemed blacklisted. + */ + BLACKLISTED + } + + /** + * Result after checking validity of world folder. + */ + public enum FolderStatus { + /** + * Folder is valid. + */ + VALID, + + /** + * Folder exist, but contents in it doesnt look like a world. + */ + NOT_A_WORLD, + + /** + * Folder does not exist. + */ + DOES_NOT_EXIST + } +} diff --git a/src/main/java/com/onarandombox/MultiverseCore/utils/metrics/MetricsConfigurator.java b/src/main/java/com/onarandombox/MultiverseCore/utils/metrics/MetricsConfigurator.java new file mode 100644 index 00000000..5b34acad --- /dev/null +++ b/src/main/java/com/onarandombox/MultiverseCore/utils/metrics/MetricsConfigurator.java @@ -0,0 +1,95 @@ +package com.onarandombox.MultiverseCore.utils.metrics; + +import java.util.Collection; +import java.util.Map; +import java.util.function.Consumer; + +import com.dumptruckman.minecraft.util.Logging; +import com.onarandombox.MultiverseCore.MultiverseCore; +import com.onarandombox.MultiverseCore.api.MVWorldManager; +import com.onarandombox.MultiverseCore.api.MultiverseWorld; +import org.apache.commons.lang.WordUtils; +import org.bstats.bukkit.Metrics; +import org.bukkit.World; + +public class MetricsConfigurator { + + private static final int PLUGIN_ID = 7765; + private static final String NO_GENERATOR_NAME = "N/A"; + + public static void configureMetrics(MultiverseCore plugin) { + MetricsConfigurator configurator = new MetricsConfigurator(plugin); + configurator.initMetrics(); + } + + private final MultiverseCore plugin; + private final Metrics metrics; + + private MetricsConfigurator(MultiverseCore plugin) { + this.plugin = plugin; + this.metrics = new Metrics(plugin, PLUGIN_ID); + } + + private MVWorldManager getWorldManager() { + return plugin.getMVWorldManager(); + } + + private Collection getMVWorlds() { + return getWorldManager().getMVWorlds(); + } + + private void initMetrics() { + try { + addCustomGeneratorsMetric(); + addEnvironmentsMetric(); + addWorldCountMetric(); + + Logging.fine("Metrics enabled."); + } catch (Exception e) { + Logging.warning("There was an issue while enabling metrics:"); + e.printStackTrace(); + } + } + + private void addCustomGeneratorsMetric() { + addAdvancedPieMetric("custom_generators", map -> { + for (MultiverseWorld w : getMVWorlds()) { + MetricsHelper.incrementCount(map, getGeneratorName(w)); + } + }); + } + + private String getGeneratorName(MultiverseWorld world) { + String gen = world.getGenerator(); + return (gen != null && !gen.equalsIgnoreCase("null")) ? gen.split(":")[0] : NO_GENERATOR_NAME; + } + + private void addEnvironmentsMetric() { + addAdvancedPieMetric("environments", map -> { + for (MultiverseWorld w : getMVWorlds()) { + MetricsHelper.incrementCount(map, titleCaseEnv(w.getEnvironment())); + } + }); + } + + private String titleCaseEnv(World.Environment env) { + String envName = env.name().replaceAll("_+", " "); + return WordUtils.capitalizeFully(envName); + } + + private void addWorldCountMetric() { + addMultiLineMetric("world_count", map -> { + int loadedWorldsCount = getMVWorlds().size(); + map.put("Loaded worlds", loadedWorldsCount); + map.put("Total number of worlds", loadedWorldsCount + getWorldManager().getUnloadedWorlds().size()); + }); + } + + private void addAdvancedPieMetric(String chartId, Consumer> metricsFunc) { + metrics.addCustomChart(MetricsHelper.createAdvancedPieChart(chartId, metricsFunc)); + } + + private void addMultiLineMetric(String chartId, Consumer> metricsFunc) { + metrics.addCustomChart(MetricsHelper.createMultiLineChart(chartId, metricsFunc)); + } +} diff --git a/src/main/java/com/onarandombox/MultiverseCore/utils/metrics/MetricsHelper.java b/src/main/java/com/onarandombox/MultiverseCore/utils/metrics/MetricsHelper.java new file mode 100644 index 00000000..8fa8da45 --- /dev/null +++ b/src/main/java/com/onarandombox/MultiverseCore/utils/metrics/MetricsHelper.java @@ -0,0 +1,33 @@ +package com.onarandombox.MultiverseCore.utils.metrics; + +import org.bstats.charts.AdvancedPie; +import org.bstats.charts.MultiLineChart; + +import java.util.HashMap; +import java.util.Map; +import java.util.function.Consumer; + +enum MetricsHelper { + ; + + /** + * Adds one to the value in the given map with the given key. If the key does not exist in the map, it will be added with a value of 1. + */ + static void incrementCount(Map map, String key) { + Integer count = map.getOrDefault(key, 0); + map.put(key, count + 1); + } + + static AdvancedPie createAdvancedPieChart(String chartId, Consumer> metricsFunc) { + Map map = new HashMap<>(); + metricsFunc.accept(map); + return new AdvancedPie(chartId, () -> map); + } + + static MultiLineChart createMultiLineChart(String chartId, Consumer> metricsFunc) { + Map map = new HashMap<>(); + metricsFunc.accept(map); + return new MultiLineChart(chartId, () -> map); + } + +} diff --git a/src/main/java/com/onarandombox/MultiverseCore/utils/webpaste/BitlyURLShortener.java b/src/main/java/com/onarandombox/MultiverseCore/utils/webpaste/BitlyURLShortener.java index e700c2e3..aa7d29d7 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/utils/webpaste/BitlyURLShortener.java +++ b/src/main/java/com/onarandombox/MultiverseCore/utils/webpaste/BitlyURLShortener.java @@ -1,19 +1,43 @@ package com.onarandombox.MultiverseCore.utils.webpaste; +import org.json.simple.JSONObject; +import org.json.simple.parser.JSONParser; +import org.json.simple.parser.ParseException; + import java.io.IOException; +import java.util.Map; /** - * An {@link URLShortener} using {@code bit.ly}. + * A {@link URLShortener} using {@code bit.ly}. Requires an access token. */ -public class BitlyURLShortener extends HttpAPIClient implements URLShortener { - private static final String GENERIC_BITLY_REQUEST_FORMAT = "https://api-ssl.bitly.com/v3/shorten?format=txt&apiKey=%s&login=%s&longUrl=%s"; +class BitlyURLShortener extends URLShortener { + private static final String ACCESS_TOKEN = "Bearer bitly-access-token"; + private static final String BITLY_POST_REQUEST = "https://api-ssl.bitly.com/v4/shorten"; - // I think it's no problem that these are public - private static final String USERNAME = "multiverse2"; - private static final String API_KEY = "R_9dbff4862a3bc0c4218a7d78cc10d0e0"; + BitlyURLShortener() { + super(BITLY_POST_REQUEST, ACCESS_TOKEN); + if (ACCESS_TOKEN.endsWith("access-token")) { + throw new UnsupportedOperationException(); + } + } - public BitlyURLShortener() { - super(String.format(GENERIC_BITLY_REQUEST_FORMAT, API_KEY, USERNAME, "%s")); + /** + * {@inheritDoc} + */ + @Override + String encodeData(String data) { + JSONObject json = new JSONObject(); + json.put("domain", "j.mp"); + json.put("long_url", data); + return json.toJSONString(); + } + + /** + * {@inheritDoc} + */ + @Override + String encodeData(Map data) { + throw new UnsupportedOperationException(); } /** @@ -22,13 +46,11 @@ public class BitlyURLShortener extends HttpAPIClient implements URLShortener { @Override public String shorten(String longUrl) { try { - String result = this.exec(longUrl); - if (!result.startsWith("http://j.mp/")) // ... then it's failed :/ - throw new IOException(result); - return result; - } catch (IOException e) { + String stringJSON = this.exec(encodeData(longUrl), ContentType.JSON); + return (String) ((JSONObject) new JSONParser().parse(stringJSON)).get("link"); + } catch (IOException | ParseException e) { e.printStackTrace(); - return longUrl; // sorry ... + return longUrl; } } } diff --git a/src/main/java/com/onarandombox/MultiverseCore/utils/webpaste/GitHubPasteService.java b/src/main/java/com/onarandombox/MultiverseCore/utils/webpaste/GitHubPasteService.java new file mode 100644 index 00000000..bee098df --- /dev/null +++ b/src/main/java/com/onarandombox/MultiverseCore/utils/webpaste/GitHubPasteService.java @@ -0,0 +1,90 @@ +package com.onarandombox.MultiverseCore.utils.webpaste; + +import org.json.simple.JSONObject; +import org.json.simple.parser.JSONParser; +import org.json.simple.parser.ParseException; + +import java.io.IOException; +import java.util.HashMap; +import java.util.Map; + +/** + * Pastes to {@code gist.github.com}. Requires an access token with the {@code gist} scope. + */ +class GitHubPasteService extends PasteService { + private final boolean isPrivate; + // this access token must have the "gist" scope + private static final String ACCESS_TOKEN = "token github-access-token"; + private static final String GITHUB_POST_REQUEST = "https://api.github.com/gists"; + + GitHubPasteService(boolean isPrivate) { + super(GITHUB_POST_REQUEST, ACCESS_TOKEN); + this.isPrivate = isPrivate; + if (ACCESS_TOKEN.endsWith("access-token")) { + throw new UnsupportedOperationException(); + } + } + + /** + * {@inheritDoc} + */ + @Override + String encodeData(String data) { + Map mapData = new HashMap(); + mapData.put("multiverse.txt", data); + return this.encodeData(mapData); + } + + /** + * {@inheritDoc} + */ + @Override + String encodeData(Map files) { + JSONObject root = new JSONObject(); + root.put("description", "Multiverse-Core Debug Info"); + root.put("public", !this.isPrivate); + JSONObject fileList = new JSONObject(); + for (Map.Entry entry : files.entrySet()) { + JSONObject fileObject = new JSONObject(); + fileObject.put("content", entry.getValue()); + fileList.put(entry.getKey(), fileObject); + } + + root.put("files", fileList); + return root.toJSONString(); + } + + /** + * {@inheritDoc} + */ + @Override + public String postData(String data) throws PasteFailedException { + try { + String stringJSON = this.exec(encodeData(data), ContentType.JSON); + return (String) ((JSONObject) new JSONParser().parse(stringJSON)).get("html_url"); + } catch (IOException | ParseException e) { + throw new PasteFailedException(e); + } + } + + /** + * {@inheritDoc} + */ + @Override + public String postData(Map data) throws PasteFailedException { + try { + String stringJSON = this.exec(encodeData(data), ContentType.JSON); + return (String) ((JSONObject) new JSONParser().parse(stringJSON)).get("html_url"); + } catch (IOException | ParseException e) { + throw new PasteFailedException(e); + } + } + + /** + * {@inheritDoc} + */ + @Override + public boolean supportsMultiFile() { + return true; + } +} diff --git a/src/main/java/com/onarandombox/MultiverseCore/utils/webpaste/GithubPasteService.java b/src/main/java/com/onarandombox/MultiverseCore/utils/webpaste/GithubPasteService.java deleted file mode 100644 index 6d840969..00000000 --- a/src/main/java/com/onarandombox/MultiverseCore/utils/webpaste/GithubPasteService.java +++ /dev/null @@ -1,99 +0,0 @@ -package com.onarandombox.MultiverseCore.utils.webpaste; - -import com.google.gson.JsonObject; -import com.google.gson.JsonParser; -import com.google.gson.JsonPrimitive; - -import java.io.BufferedReader; -import java.io.IOException; -import java.io.InputStreamReader; -import java.io.OutputStreamWriter; -import java.net.MalformedURLException; -import java.net.URL; -import java.net.URLConnection; -import java.util.HashMap; -import java.util.Map; - -public class GithubPasteService implements PasteService { - - private final boolean isPrivate; - - public GithubPasteService(boolean isPrivate) { - this.isPrivate = isPrivate; - } - - @Override - public String encodeData(String data) { - Map mapData = new HashMap(); - mapData.put("multiverse.txt", data); - return this.encodeData(mapData); - } - - @Override - public String encodeData(Map files) { - JsonObject root = new JsonObject(); - root.add("description", new JsonPrimitive("Multiverse-Core Debug Info")); - root.add("public", new JsonPrimitive(!this.isPrivate)); - JsonObject fileList = new JsonObject(); - for (Map.Entry entry : files.entrySet()) - { - JsonObject fileObject = new JsonObject(); - fileObject.add("content", new JsonPrimitive(entry.getValue())); - fileList.add(entry.getKey(), fileObject); - } - root.add("files", fileList); - return root.toString(); - } - - @Override - public URL getPostURL() { - try { - return new URL("https://api.github.com/gists"); - //return new URL("http://jsonplaceholder.typicode.com/posts"); - } catch (MalformedURLException e) { - return null; // should never hit here - } - } - - @Override - public String postData(String encodedData, URL url) throws PasteFailedException { - OutputStreamWriter wr = null; - BufferedReader rd = null; - try { - URLConnection conn = url.openConnection(); - conn.setDoOutput(true); - wr = new OutputStreamWriter(conn.getOutputStream()); - wr.write(encodedData); - wr.flush(); - - rd = new BufferedReader(new InputStreamReader(conn.getInputStream())); - String line; - String pastieUrl = ""; - //Pattern pastiePattern = this.getURLMatchingPattern(); - StringBuilder responseString = new StringBuilder(); - - while ((line = rd.readLine()) != null) { - responseString.append(line); - } - return new JsonParser().parse(responseString.toString()).getAsJsonObject().get("html_url").getAsString(); - } catch (Exception e) { - throw new PasteFailedException(e); - } finally { - if (wr != null) { - try { - wr.close(); - } catch (IOException ignore) { } - } - if (rd != null) { - try { - rd.close(); - } catch (IOException ignore) { } - } - } - } - - @Override - public boolean supportsMultiFile() { - return true; - } -} diff --git a/src/main/java/com/onarandombox/MultiverseCore/utils/webpaste/HastebinPasteService.java b/src/main/java/com/onarandombox/MultiverseCore/utils/webpaste/HastebinPasteService.java index 69438cfd..e1fa7272 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/utils/webpaste/HastebinPasteService.java +++ b/src/main/java/com/onarandombox/MultiverseCore/utils/webpaste/HastebinPasteService.java @@ -1,79 +1,67 @@ package com.onarandombox.MultiverseCore.utils.webpaste; -import com.google.gson.JsonElement; -import com.google.gson.JsonParser; +import org.json.simple.JSONObject; +import org.json.simple.parser.JSONParser; +import org.json.simple.parser.ParseException; -import java.io.BufferedReader; import java.io.IOException; -import java.io.InputStreamReader; -import java.io.OutputStreamWriter; -import java.net.MalformedURLException; -import java.net.URL; -import java.net.URLConnection; import java.util.Map; /** * Pastes to {@code hastebin.com}. */ -public class HastebinPasteService implements PasteService { +class HastebinPasteService extends PasteService { + private static final String HASTEBIN_POST_REQUEST = "https://hastebin.com/documents"; + HastebinPasteService() { + super(HASTEBIN_POST_REQUEST); + } + + /** + * {@inheritDoc} + */ @Override - public String encodeData(String data) { + String encodeData(String data) { return data; } + /** + * {@inheritDoc} + */ @Override - public String encodeData(Map data) { + String encodeData(Map data) { throw new UnsupportedOperationException(); } + /** + * {@inheritDoc} + */ @Override - public URL getPostURL() { + public String postData(String data) throws PasteFailedException { try { - return new URL("https://hastebin.com/documents"); - } catch (MalformedURLException e) { - return null; // should never hit here - } - } - - @Override - public String postData(String encodedData, URL url) throws PasteFailedException { - OutputStreamWriter wr = null; - BufferedReader rd = null; - try { - URLConnection conn = url.openConnection(); - conn.setDoOutput(true); - - wr = new OutputStreamWriter(conn.getOutputStream()); - rd = new BufferedReader(new InputStreamReader(conn.getInputStream())); - - wr.write(encodedData); - wr.flush(); - - String line; - StringBuilder responseString = new StringBuilder(); - while ((line = rd.readLine()) != null) { - responseString.append(line); - } - String key = new JsonParser().parse(responseString.toString()).getAsJsonObject().get("key").getAsString(); - - return "https://hastebin.com/" + key; - } catch (Exception e) { + String stringJSON = this.exec(encodeData(data), ContentType.PLAINTEXT); + return "https://hastebin.com/" + ((JSONObject) new JSONParser().parse(stringJSON)).get("key"); + } catch (IOException | ParseException e) { throw new PasteFailedException(e); - } finally { - if (wr != null) { - try { - wr.close(); - } catch (IOException ignore) { } - } - if (rd != null) { - try { - rd.close(); - } catch (IOException ignore) { } - } } } + /** + * {@inheritDoc} + */ + @Override + public String postData(Map data) throws PasteFailedException { + try { + String stringJSON = this.exec(encodeData(data), ContentType.PLAINTEXT); + return "https://hastebin.com/" + ((JSONObject) new JSONParser().parse(stringJSON)).get("key"); + } catch (IOException | ParseException e) { + throw new PasteFailedException(e); + } + } + + /** + * {@inheritDoc} + */ @Override public boolean supportsMultiFile() { return false; diff --git a/src/main/java/com/onarandombox/MultiverseCore/utils/webpaste/HttpAPIClient.java b/src/main/java/com/onarandombox/MultiverseCore/utils/webpaste/HttpAPIClient.java index 55d69c5c..676ef7cd 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/utils/webpaste/HttpAPIClient.java +++ b/src/main/java/com/onarandombox/MultiverseCore/utils/webpaste/HttpAPIClient.java @@ -1,50 +1,129 @@ package com.onarandombox.MultiverseCore.utils.webpaste; +import javax.net.ssl.HttpsURLConnection; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; +import java.io.OutputStreamWriter; import java.net.URL; -import java.net.URLConnection; +import java.nio.charset.StandardCharsets; +import java.util.Map; /** * HTTP API-client. */ -public abstract class HttpAPIClient { +abstract class HttpAPIClient { /** - * The URL for this API-request. + * The URL for this API-request, and if necessary, the access token. + * If an access token is not necessary, it should be set to null. */ - protected final String urlFormat; + private final String url; + private final String accessToken; - public HttpAPIClient(String urlFormat) { - this.urlFormat = urlFormat; + /** + * Types of data that can be sent. + */ + enum ContentType { + JSON, + PLAINTEXT, + URLENCODED } + HttpAPIClient(String url) { + this(url, null); + } + + HttpAPIClient(String url, String accessToken) { + this.url = url; + this.accessToken = accessToken; + } + + /** + * Returns the HTTP Content-Type header that corresponds with each ContentType. + * @param type The type of data. + * @return The HTTP Content-Type header that corresponds with the type of data. + */ + private String getContentHeader(ContentType type) { + switch (type) { + case JSON: + return "application/json; charset=utf-8"; + case PLAINTEXT: + return "text/plain; charset=utf-8"; + case URLENCODED: + return "application/x-www-form-urlencoded; charset=utf-8"; + default: + throw new IllegalArgumentException("Unexpected value: " + type); + } + } + + /** + * Encode the given String data into a format suitable for transmission in an HTTP request. + * + * @param data The raw data to encode. + * @return A URL-encoded string. + */ + abstract String encodeData(String data); + + /** + * Encode the given Map data into a format suitable for transmission in an HTTP request. + * + * @param data The raw data to encode. + * @return A URL-encoded string. + */ + abstract String encodeData(Map data); + /** * Executes this API-Request. - * @param args Format-args. + * @param payload The data that will be sent. + * @param type The type of data that will be sent. * @return The result (as text). * @throws IOException When the I/O-operation failed. */ - protected final String exec(Object... args) throws IOException { + final String exec(String payload, ContentType type) throws IOException { + BufferedReader rd = null; + OutputStreamWriter wr = null; - URLConnection conn = new URL(String.format(this.urlFormat, args)).openConnection(); - conn.connect(); - StringBuilder ret = new StringBuilder(); - BufferedReader reader = null; try { - reader = new BufferedReader(new InputStreamReader(conn.getInputStream())); - while (!reader.ready()); // wait until reader is ready, may not be necessary, SUPPRESS CHECKSTYLE: EmptyStatement + HttpsURLConnection conn = (HttpsURLConnection) new URL(this.url).openConnection(); + conn.setRequestMethod("POST"); + conn.setDoOutput(true); - while (reader.ready()) { - ret.append(reader.readLine()).append('\n'); + // we can receive anything! + conn.addRequestProperty("Accept", "*/*"); + // set a dummy User-Agent + conn.addRequestProperty("User-Agent", "placeholder"); + // this isn't required, but is technically correct + conn.addRequestProperty("Content-Type", getContentHeader(type)); + // only some API requests require an access token + if (this.accessToken != null) { + conn.addRequestProperty("Authorization", this.accessToken); } + + wr = new OutputStreamWriter(conn.getOutputStream(), StandardCharsets.UTF_8.newEncoder()); + wr.write(payload); + wr.flush(); + + String line; + StringBuilder responseString = new StringBuilder(); + // this has to be initialized AFTER the data has been flushed! + rd = new BufferedReader(new InputStreamReader(conn.getInputStream(), StandardCharsets.UTF_8)); + + while ((line = rd.readLine()) != null) { + responseString.append(line); + } + + return responseString.toString(); } finally { - if (reader != null) { + if (wr != null) { try { - reader.close(); + wr.close(); + } catch (IOException ignore) { } + } + if (rd != null) { + try { + rd.close(); } catch (IOException ignore) { } } } - return ret.toString(); } } diff --git a/src/main/java/com/onarandombox/MultiverseCore/utils/webpaste/PasteFailedException.java b/src/main/java/com/onarandombox/MultiverseCore/utils/webpaste/PasteFailedException.java index 85a803a4..82792f49 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/utils/webpaste/PasteFailedException.java +++ b/src/main/java/com/onarandombox/MultiverseCore/utils/webpaste/PasteFailedException.java @@ -1,7 +1,7 @@ package com.onarandombox.MultiverseCore.utils.webpaste; /** - * Thrown when pasting failed. + * Thrown when pasting fails. */ public class PasteFailedException extends Exception { public PasteFailedException() { diff --git a/src/main/java/com/onarandombox/MultiverseCore/utils/webpaste/PasteGGPasteService.java b/src/main/java/com/onarandombox/MultiverseCore/utils/webpaste/PasteGGPasteService.java new file mode 100644 index 00000000..956d71c1 --- /dev/null +++ b/src/main/java/com/onarandombox/MultiverseCore/utils/webpaste/PasteGGPasteService.java @@ -0,0 +1,90 @@ +package com.onarandombox.MultiverseCore.utils.webpaste; + +import org.json.simple.JSONArray; +import org.json.simple.JSONObject; +import org.json.simple.parser.JSONParser; +import org.json.simple.parser.ParseException; + +import java.io.IOException; +import java.util.HashMap; +import java.util.Map; + +/** + * Pastes to {@code paste.gg}. + */ +class PasteGGPasteService extends PasteService { + private final boolean isPrivate; + private static final String PASTEGG_POST_REQUEST = "https://api.paste.gg/v1/pastes"; + + PasteGGPasteService(boolean isPrivate) { + super(PASTEGG_POST_REQUEST); + this.isPrivate = isPrivate; + } + + /** + * {@inheritDoc} + */ + @Override + String encodeData(String data) { + Map mapData = new HashMap(); + mapData.put("multiverse.txt", data); + return this.encodeData(mapData); + } + + /** + * {@inheritDoc} + */ + @Override + String encodeData(Map files) { + JSONObject root = new JSONObject(); + root.put("name", "Multiverse-Core Debug Info"); + root.put("visibility", this.isPrivate ? "unlisted" : "public"); + JSONArray fileList = new JSONArray(); + for (Map.Entry entry : files.entrySet()) { + JSONObject fileObject = new JSONObject(); + JSONObject contentObject = new JSONObject(); + fileObject.put("name", entry.getKey()); + fileObject.put("content", contentObject); + contentObject.put("format", "text"); + contentObject.put("value", entry.getValue()); + fileList.add(fileObject); + } + + root.put("files", fileList); + return root.toJSONString(); + } + + /** + * {@inheritDoc} + */ + @Override + public String postData(String data) throws PasteFailedException { + try { + String stringJSON = this.exec(encodeData(data), ContentType.JSON); + return (String) ((JSONObject) ((JSONObject) new JSONParser().parse(stringJSON)).get("result")).get("id"); + } catch (IOException | ParseException e) { + throw new PasteFailedException(e); + } + } + + /** + * {@inheritDoc} + */ + @Override + public String postData(Map data) throws PasteFailedException { + try { + String stringJSON = this.exec(encodeData(data), ContentType.JSON); + return "https://paste.gg/" + ((JSONObject) ((JSONObject) new JSONParser().parse(stringJSON)).get("result")).get("id"); + } catch (IOException | ParseException e) { + throw new PasteFailedException(e); + } + } + + /** + * {@inheritDoc} + */ + @Override + public boolean supportsMultiFile() { + return true; + } +} diff --git a/src/main/java/com/onarandombox/MultiverseCore/utils/webpaste/PasteService.java b/src/main/java/com/onarandombox/MultiverseCore/utils/webpaste/PasteService.java index 403f5ab8..a6bc15c4 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/utils/webpaste/PasteService.java +++ b/src/main/java/com/onarandombox/MultiverseCore/utils/webpaste/PasteService.java @@ -1,63 +1,52 @@ package com.onarandombox.MultiverseCore.utils.webpaste; -import java.net.URL; import java.util.Map; /** - * An interface to a web-based text-pasting service. Classes implementing this - * interface should implement its methods to send data to an online text-sharing - * service, such as pastebin.com. Conventionally, a paste is accomplished by (given - * some PasteService instance ps): + * An interface to a web-based text-pasting service. Classes extending this + * should implement its methods to send data to an online text-sharing service, + * such as pastebin.com. Given some PasteService instance ps, a paste is accomplished by: * - * {@code ps.postData(ps.encodeData(someString), ps.getPostURL());} + * {@code ps.postData(someString);} * * Services that provide a distinction between "public" and "private" pastes - * should implement a custom constructor that specifies which kind the PasteService + * should implement a constructor that specifies which kind the PasteService * instance is submitting; an example of this is the PastebinPasteService class. */ -public interface PasteService { +public abstract class PasteService extends HttpAPIClient { + PasteService(String url) { + super(url); + } + + PasteService(String url, String accessToken) { + super(url, accessToken); + } /** - * Encode the given String data into a format suitable for transmission in an HTTP request. + * Post data to the Web. * - * @param data The raw data to encode. - * @return A URL-encoded string. - */ - String encodeData(String data); - - /** - * Encode the given Map data into a format suitable for transmission in an HTTP request. - * - * @param data The raw data to encode. - * @return A URL-encoded string. - */ - String encodeData(Map data); - - /** - * Get the URL to which this paste service sends new pastes. - * - * @return The URL that will be accessed to complete the paste. - */ - URL getPostURL(); - - /** - * Post encoded data to the Web. - * - * @param encodedData A URL-encoded String containing the full request to post to - * the given URL. Can be the result of calling #encodeData(). - * @param url The URL to which to paste. Can be the result of calling #getPostURL(). + * @param data A String to post to the web. * @throws PasteFailedException When pasting/posting the data failed. * @return The URL at which the new paste is visible. */ - String postData(String encodedData, URL url) throws PasteFailedException; + public abstract String postData(String data) throws PasteFailedException; + + /** + * Post data to the Web. + * + * @param data A Map to post to the web. + * @throws PasteFailedException When pasting/posting the data failed. + * @return The URL at which the new paste is visible. + */ + public abstract String postData(Map data) throws PasteFailedException; /** * Does this service support uploading multiple files. * - * Newer services like gist support multi-file which allows us to upload configs - * in addition to the standard logs. + * Newer services like GitHub's Gist support multi-file pastes, + * which allows us to upload configs in addition to the standard logs. * * @return True if this service supports multiple file upload. */ - boolean supportsMultiFile(); + public abstract boolean supportsMultiFile(); } diff --git a/src/main/java/com/onarandombox/MultiverseCore/utils/webpaste/PasteServiceFactory.java b/src/main/java/com/onarandombox/MultiverseCore/utils/webpaste/PasteServiceFactory.java index df4fb831..f6f63a1c 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/utils/webpaste/PasteServiceFactory.java +++ b/src/main/java/com/onarandombox/MultiverseCore/utils/webpaste/PasteServiceFactory.java @@ -14,12 +14,14 @@ public class PasteServiceFactory { */ public static PasteService getService(PasteServiceType type, boolean isPrivate) { switch(type) { + case PASTEGG: + return new PasteGGPasteService(isPrivate); case PASTEBIN: return new PastebinPasteService(isPrivate); case HASTEBIN: return new HastebinPasteService(); case GITHUB: - return new GithubPasteService(isPrivate); + return new GitHubPasteService(isPrivate); default: return null; } diff --git a/src/main/java/com/onarandombox/MultiverseCore/utils/webpaste/PasteServiceType.java b/src/main/java/com/onarandombox/MultiverseCore/utils/webpaste/PasteServiceType.java index 0bd93e63..09424c0b 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/utils/webpaste/PasteServiceType.java +++ b/src/main/java/com/onarandombox/MultiverseCore/utils/webpaste/PasteServiceType.java @@ -7,6 +7,10 @@ package com.onarandombox.MultiverseCore.utils.webpaste; * @see PasteServiceFactory */ public enum PasteServiceType { + /** + * @see PasteGGPasteService + */ + PASTEGG, /** * @see PastebinPasteService */ @@ -16,7 +20,7 @@ public enum PasteServiceType { */ HASTEBIN, /** - * @see GithubPasteService + * @see GitHubPasteService */ GITHUB } diff --git a/src/main/java/com/onarandombox/MultiverseCore/utils/webpaste/PastebinPasteService.java b/src/main/java/com/onarandombox/MultiverseCore/utils/webpaste/PastebinPasteService.java index 3f06f612..eff40193 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/utils/webpaste/PastebinPasteService.java +++ b/src/main/java/com/onarandombox/MultiverseCore/utils/webpaste/PastebinPasteService.java @@ -1,24 +1,19 @@ package com.onarandombox.MultiverseCore.utils.webpaste; -import java.io.BufferedReader; import java.io.IOException; -import java.io.InputStreamReader; -import java.io.OutputStreamWriter; import java.io.UnsupportedEncodingException; -import java.net.MalformedURLException; -import java.net.URL; -import java.net.URLConnection; import java.net.URLEncoder; import java.util.Map; /** * Pastes to {@code pastebin.com}. */ -public class PastebinPasteService implements PasteService { +class PastebinPasteService extends PasteService { + private final boolean isPrivate; + private static final String PASTEBIN_POST_REQUEST = "https://pastebin.com/api/api_post.php"; - private boolean isPrivate; - - public PastebinPasteService(boolean isPrivate) { + PastebinPasteService(boolean isPrivate) { + super(PASTEBIN_POST_REQUEST); this.isPrivate = isPrivate; } @@ -26,73 +21,54 @@ public class PastebinPasteService implements PasteService { * {@inheritDoc} */ @Override - public URL getPostURL() { + String encodeData(String data) { try { - return new URL("http://pastebin.com/api/api_post.php"); - } catch (MalformedURLException e) { - return null; // should never hit here - } - } - - /** - * {@inheritDoc} - */ - @Override - public String encodeData(String data) { - try { - String encData = URLEncoder.encode("api_dev_key", "UTF-8") + "=" + URLEncoder.encode("d61d68d31e8e0392b59b50b277411c71", "UTF-8"); - encData += "&" + URLEncoder.encode("api_option", "UTF-8") + "=" + URLEncoder.encode("paste", "UTF-8"); - encData += "&" + URLEncoder.encode("api_paste_code", "UTF-8") + "=" + URLEncoder.encode(data, "UTF-8"); - encData += "&" + URLEncoder.encode("api_paste_private", "UTF-8") + "=" + URLEncoder.encode(this.isPrivate ? "1" : "0", "UTF-8"); - encData += "&" + URLEncoder.encode("api_paste_format", "UTF-8") + "=" + URLEncoder.encode("yaml", "UTF-8"); - return encData; + return URLEncoder.encode("api_dev_key", "UTF-8") + "=" + URLEncoder.encode("d61d68d31e8e0392b59b50b277411c71", "UTF-8") + + "&" + URLEncoder.encode("api_option", "UTF-8") + "=" + URLEncoder.encode("paste", "UTF-8") + + "&" + URLEncoder.encode("api_paste_code", "UTF-8") + "=" + URLEncoder.encode(data, "UTF-8") + + "&" + URLEncoder.encode("api_paste_private", "UTF-8") + "=" + URLEncoder.encode(this.isPrivate ? "1" : "0", "UTF-8") + + "&" + URLEncoder.encode("api_paste_format", "UTF-8") + "=" + URLEncoder.encode("yaml", "UTF-8") + + "&" + URLEncoder.encode("api_paste_name", "UTF-8") + "=" + URLEncoder.encode("Multiverse-Core Debug Info", "UTF-8"); } catch (UnsupportedEncodingException e) { return ""; // should never hit here } } + /** + * {@inheritDoc} + */ @Override - public String encodeData(Map data) { - return null; + String encodeData(Map data) { + throw new UnsupportedOperationException(); } /** * {@inheritDoc} */ @Override - public String postData(String encodedData, URL url) throws PasteFailedException { - OutputStreamWriter wr = null; - BufferedReader rd = null; + public String postData(String data) throws PasteFailedException { try { - URLConnection conn = url.openConnection(); - conn.setDoOutput(true); - wr = new OutputStreamWriter(conn.getOutputStream()); - wr.write(encodedData); - wr.flush(); - - rd = new BufferedReader(new InputStreamReader(conn.getInputStream())); - String line; - String pastebinUrl = ""; - while ((line = rd.readLine()) != null) { - pastebinUrl = line; - } - return pastebinUrl; - } catch (Exception e) { + return this.exec(encodeData(data), ContentType.URLENCODED); + } catch (IOException e) { throw new PasteFailedException(e); - } finally { - if (wr != null) { - try { - wr.close(); - } catch (IOException ignore) { } - } - if (rd != null) { - try { - rd.close(); - } catch (IOException ignore) { } - } } } + /** + * {@inheritDoc} + */ + @Override + public String postData(Map data) throws PasteFailedException { + try { + return this.exec(encodeData(data), ContentType.URLENCODED); + } catch (IOException e) { + throw new PasteFailedException(e); + } + } + + /** + * {@inheritDoc} + */ @Override public boolean supportsMultiFile() { return false; diff --git a/src/main/java/com/onarandombox/MultiverseCore/utils/webpaste/URLShortener.java b/src/main/java/com/onarandombox/MultiverseCore/utils/webpaste/URLShortener.java index 75d50f63..bde8ff1d 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/utils/webpaste/URLShortener.java +++ b/src/main/java/com/onarandombox/MultiverseCore/utils/webpaste/URLShortener.java @@ -1,13 +1,27 @@ package com.onarandombox.MultiverseCore.utils.webpaste; /** - * URL-Shortener. + * An interface to a web-based URL Shortener. Classes extending this should + * implement its methods to shorten links using the service. Given some + * URLShortener instance us, a URL is shortened by: + * + * {@code us.shorten(longUrl);} + * + * An example of this, is the BitlyURLShortener. */ -public interface URLShortener { +public abstract class URLShortener extends HttpAPIClient { + URLShortener(String url) { + super(url); + } + + URLShortener(String url, String accessToken) { + super(url, accessToken); + } + /** - * Shorten an URL. + * Shorten a URL. * @param longUrl The long form. * @return The shortened URL. */ - String shorten(String longUrl); + public abstract String shorten(String longUrl); } diff --git a/src/main/java/com/onarandombox/MultiverseCore/utils/webpaste/URLShortenerFactory.java b/src/main/java/com/onarandombox/MultiverseCore/utils/webpaste/URLShortenerFactory.java new file mode 100644 index 00000000..c0f3cafa --- /dev/null +++ b/src/main/java/com/onarandombox/MultiverseCore/utils/webpaste/URLShortenerFactory.java @@ -0,0 +1,23 @@ +package com.onarandombox.MultiverseCore.utils.webpaste; + +/** + * Used to construct {@link URLShortener}s. + */ +public class URLShortenerFactory { + private URLShortenerFactory() { } + + /** + * Constructs a new {@link URLShortener}. + * @param type The {@link URLShortenerType}. + * @return The newly created {@link URLShortener}. + */ + public static URLShortener getService(URLShortenerType type) { + if (type == URLShortenerType.BITLY) { + try { + return new BitlyURLShortener(); + } catch (UnsupportedOperationException ignored) {} + } + + return null; + } +} \ No newline at end of file diff --git a/src/main/java/com/onarandombox/MultiverseCore/utils/webpaste/URLShortenerType.java b/src/main/java/com/onarandombox/MultiverseCore/utils/webpaste/URLShortenerType.java new file mode 100644 index 00000000..d2c809f5 --- /dev/null +++ b/src/main/java/com/onarandombox/MultiverseCore/utils/webpaste/URLShortenerType.java @@ -0,0 +1,14 @@ +package com.onarandombox.MultiverseCore.utils.webpaste; + +/** + * An enum containing all known {@link URLShortener}s. + * + * @see URLShortener + * @see URLShortenerFactory + */ +public enum URLShortenerType { + /** + * @see BitlyURLShortener + */ + BITLY +} diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index d65a3779..d46f0fd7 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -1,7 +1,8 @@ name: Multiverse-Core main: com.onarandombox.MultiverseCore.MultiverseCore -authors: ['Rigby', 'fernferret', 'lithium3141', 'main--', 'dumptruckman'] +authors: ['dumptruckman', 'Rigby', 'fernferret', 'lithium3141', 'main--'] website: 'https://dev.bukkit.org/projects/multiverse-core' +softdepend: ['Vault'] api-version: 1.13 version: maven-version-number commands: @@ -10,34 +11,24 @@ commands: usage: / mvcreate: description: World create command - usage: | - / - / creative normal -- Creates a world called 'creative' with a NORMAL environment. - / hellworld nether -- Creates a world called 'hellworld' with a NETHER environment. - mvc: - description: World create command + aliases: [mvc] usage: | / / creative normal -- Creates a world called 'creative' with a NORMAL environment. / hellworld nether -- Creates a world called 'hellworld' with a NETHER environment. mvimport: description: World import command + aliases: [mvim] usage: | - / - / creative normal -- Imports an existing world called 'creative' with a NORMAL environment. - / hellworld nether -- Imports an existing world called 'hellworld' with a NETHER environment. - mvim: - description: World import command - usage: | - / - / creative normal -- Imports an existing world called 'creative' with a NORMAL environment. - / hellworld nether -- Imports an existing world called 'hellworld' with a NETHER environment. + / [-g generator[:id]] [-n] + / creative normal -- Imports an existing world called 'creative' with a NORMAL environment. + / hellworld nether -- Imports an existing world called 'hellworld' with a NETHER environment. mvremove: - description: World remove command + description: Remove world from multiverse command usage: | / mvdelete: - description: World delete command + description: Delete world from server folders command usage: | / mvunload: @@ -46,103 +37,75 @@ commands: / mvmodify: description: Modify the settings of an existing world + aliases: [mvm] usage: | - /