diff --git a/.github/ISSUE_TEMPLATE/bug-report.yml b/.github/ISSUE_TEMPLATE/bug-report.yml new file mode 100644 index 00000000..b4635048 --- /dev/null +++ b/.github/ISSUE_TEMPLATE/bug-report.yml @@ -0,0 +1,77 @@ +name: 🐛 Report a Bug +description: Report a problem with Multiverse-Core. +title: "[Bug]: " +labels: ["Bug: Unconfirmed"] +body: + - type: markdown + attributes: + value: | + Thanks for taking the time to fill out this bug report! + + If you are having issues with other submodules of Multiverse, please go to their respective git repo: + Multiverse-Portals: https://github.com/Multiverse/Multiverse-Portals/issues/new/choose + Multiverse-NetherPortals: https://github.com/Multiverse/Multiverse-NetherPortals/issues/new/choose + Multiverse-Inventories: https://github.com/Multiverse/Multiverse-Inventories/issues/new/choose + Multiverse-SignPortals: https://github.com/Multiverse/Multiverse-SignPortals/issues/new/choose + + Not sure if this is a bug with Multiverse-Core? Feel free to ask on our Discord server: https://discord.gg/NZtfKky + + When completing the report, please ensure all fields are filled with the correct details requested. Failure to do so will result in your issue being closed. + - type: input + id: mutliverse-version-info + attributes: + label: '`/mv version -p` output' + description: Run `/mv version -p` in the console, then copy and paste the link from the output of the command into this box. + validations: + required: true + - type: input + id: server-logs + attributes: + label: Server logs + description: > + Include full startup log and console error (if any). upload all relevant logs to either https://paste.gg + or https://gist.github.com, save and the paste the link in this box. + validations: + required: true + - type: textarea + id: server-version + attributes: + label: Server Version + description: Output of `/version` command. + placeholder: server version info + render: shell + validations: + required: true + - type: textarea + id: bug-description + attributes: + label: Bug Description + description: Describe in detail what the bug is about. Include the expected behaviour and actual behaviour. + placeholder: "Example: When running `/mv` it cause the server to crash..." + validations: + required: true + - type: textarea + id: reproduce-steps + attributes: + label: Steps to reproduce + description: Provide detailed intructions to trigger the bug. + placeholder: | + Example: + 1. Start a server with only multiverse installed. + 2. Run `/mv` command. + 3. Players all get kicked with a "server closed" message. + ... + validations: + required: true + - type: checkboxes + id: agreements + attributes: + label: Agreements + description: Ensure you have done the following being submitting this issue. + options: + - label: I have searched for and ensured there isn't already an open or resolved issue(s) regarding this. + required: true + - label: > + I was able 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. + required: true diff --git a/.github/ISSUE_TEMPLATE/config.yml b/.github/ISSUE_TEMPLATE/config.yml new file mode 100644 index 00000000..9ae1a0fc --- /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/feature-request.yml b/.github/ISSUE_TEMPLATE/feature-request.yml new file mode 100644 index 00000000..d51ef22d --- /dev/null +++ b/.github/ISSUE_TEMPLATE/feature-request.yml @@ -0,0 +1,41 @@ +name: 💡 Request a Feature +description: Suggest a feature you want to see in Multiverse-Core! +title: "[Idea]: " +labels: ["Type: Idea"] +body: + - type: markdown + attributes: + value: | + Thanks for taking the time to share your ideas! + + If you are look for other submodules of Multiverse, go to their respective git repo: + Multiverse-Portals: https://github.com/Multiverse/Multiverse-Portals/issues/new/choose + Multiverse-NetherPortals: https://github.com/Multiverse/Multiverse-NetherPortals/issues/new/choose + Multiverse-Inventories: https://github.com/Multiverse/Multiverse-Inventories/issues/new/choose + Multiverse-SignPortals: https://github.com/Multiverse/Multiverse-SignPortals/issues/new/choose + + Not sure if your feature fits in Multiverse-Core? Feel free to ask on our Discord server: https://discord.gg/NZtfKky + - type: textarea + id: feature-description + attributes: + label: Describe the feature + description: What feature are you suggesting? + placeholder: "Example: I hope multiverse can implement random teleport feature..." + validations: + required: true + - type: textarea + id: feature-reasoning + attributes: + label: How is the feature useful to all Multiverse users? + description: How is the feature useful to all players, server owners and/or developers? + placeholder: "Example: Random teleporting can be integrated into many multiverse modules such as mv-portals..." + validations: + required: true + - type: checkboxes + id: agreements + attributes: + label: Agreements + description: Ensure you have done the following being submitting this issue. + options: + - label: I have searched for and ensured there isn't already an open issue regarding this. + required: true 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 2e7dbb32..00000000 --- a/.travis.yml +++ /dev/null @@ -1,6 +0,0 @@ -language: java -jdk: - - oraclejdk8 -notifications: - email: false -dist: trusty 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..f163c5e6 100644 --- a/README.md +++ b/README.md @@ -1,16 +1,44 @@ +![Multiverse 2](config/multiverse2-long.png) + +[![Maven CI/CD](https://github.com/Multiverse/Multiverse-Core/actions/workflows/build.yml/badge.svg)](https://github.com/Multiverse/Multiverse-Core/actions/workflows/build.yml) +[![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) +[![License](https://img.shields.io/github/license/Multiverse/Multiverse-Core)](LICENSE.md) + +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. - -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. - +Multiverse-Core is licensed under BSD-3-Clause License. Please see [LICENSE.md](LICENSE.md) for more info. diff --git a/config/mv_checks.xml b/config/mv_checks.xml index 0c660d8d..2048eff5 100644 --- a/config/mv_checks.xml +++ b/config/mv_checks.xml @@ -69,7 +69,7 @@ - + diff --git a/pom.xml b/pom.xml index 0949061f..2628624a 100644 --- a/pom.xml +++ b/pom.xml @@ -3,12 +3,13 @@ 4.0.0 com.onarandombox.multiversecore Multiverse-Core - 4.1.1-SNAPSHOT + 5.0.0-SNAPSHOT Multiverse-Core World Management Plugin UTF-8 UNKNOWN + bitly-access-token @@ -19,23 +20,23 @@ onarandombox - http://repo.onarandombox.com/content/groups/public + https://repo.onarandombox.com/content/groups/public spigot https://hub.spigotmc.org/nexus/content/groups/public/ - vault-repo - http://nexus.hc.to/content/repositories/pub_releases + jitpack.io + https://jitpack.io minebench-repo https://repo.minebench.de/ - CodeMC - https://repo.codemc.org/repository/maven-public + aikar + https://repo.aikar.co/content/groups/aikar/ @@ -43,7 +44,7 @@ doodleproject-repo DoodleProject Maven 2 Repository - http://doodleproject.sourceforge.net/maven2/release + https://doodleproject.sourceforge.net/maven2/release true @@ -52,7 +53,7 @@ jenkins - http://ci.onarandombox.com + https://ci.onarandombox.com @@ -123,8 +158,7 @@ maven-surefire-plugin 3.0.0-M3 - methods - 10 + true **/TestCommandSender.java **/TestInstanceCreator.java @@ -180,7 +214,7 @@ org.apache.maven.plugins maven-shade-plugin - 3.1.1 + 3.2.4 package @@ -188,6 +222,7 @@ shade + true me.main__.util @@ -221,6 +256,14 @@ de.themoep.idconverter com.onarandombox.idconverter + + co.aikar.commands + com.onarandombox.acf + + + co.aikar.locales + com.onarandombox.locales + @@ -229,13 +272,19 @@ se.eris notnull-instrumenter-maven-plugin - 0.6.8 + 1.1.1 instrument tests-instrument + + + org.jetbrains.annotations.NotNull + javax.validation.constraints.NotNull + + @@ -244,10 +293,10 @@ - org.bukkit - bukkit - 1.13.2-R0.1-SNAPSHOT - provided + org.spigotmc + spigot-api + 1.16.5-R0.1-SNAPSHOT + provided @@ -264,18 +313,29 @@ - net.milkbowl.vault + com.github.MilkBowl VaultAPI 1.7 provided + + co.aikar + acf-paper + 0.5.1-SNAPSHOT + com.pneumaticraft.commandhandler CommandHandler 11 + + + net.minidev + json-smart + 2.4.1 + com.dumptruckman.minecraft buscript @@ -284,13 +344,7 @@ org.bstats bstats-bukkit - 1.7 - - - org.bukkit - bukkit - - + 2.2.1 com.dumptruckman.minecraft @@ -316,30 +370,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 @@ -352,11 +385,11 @@ OnARandomBox - http://repo.onarandombox.com/content/repositories/multiverse + https://repo.onarandombox.com/content/repositories/multiverse OnARandomBox - http://repo.onarandombox.com/content/repositories/multiverse-snapshots/ + https://repo.onarandombox.com/content/repositories/multiverse-snapshots/ diff --git a/src/main/java/com/onarandombox/MultiverseCore/MVWorld.java b/src/main/java/com/onarandombox/MultiverseCore/MVWorld.java index 485a75ae..50a3f415 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/MVWorld.java +++ b/src/main/java/com/onarandombox/MultiverseCore/MVWorld.java @@ -213,7 +213,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); @@ -296,7 +296,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); } @@ -318,16 +318,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); @@ -355,7 +355,7 @@ public class MVWorld implements MultiverseWorld { @Override public Map serialize() { - return Collections.EMPTY_MAP; + return Collections.emptyMap(); } /** @@ -410,7 +410,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); } } @@ -421,16 +421,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. @@ -449,7 +449,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 43820a5a..a4e44b6a 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/MultiverseCore.java +++ b/src/main/java/com/onarandombox/MultiverseCore/MultiverseCore.java @@ -7,21 +7,6 @@ package com.onarandombox.MultiverseCore; -import java.io.BufferedReader; -import java.io.File; -import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.io.UnsupportedEncodingException; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collections; -import java.util.HashMap; -import java.util.LinkedHashMap; -import java.util.List; -import java.util.Map; -import java.util.logging.Level; - import buscript.Buscript; import com.dumptruckman.minecraft.util.Logging; import com.onarandombox.MultiverseCore.MVWorld.NullLocation; @@ -33,41 +18,43 @@ import com.onarandombox.MultiverseCore.api.MVWorldManager; import com.onarandombox.MultiverseCore.api.MultiverseCoreConfig; import com.onarandombox.MultiverseCore.api.MultiverseMessaging; import com.onarandombox.MultiverseCore.api.SafeTTeleporter; -import com.onarandombox.MultiverseCore.commands.AnchorCommand; -import com.onarandombox.MultiverseCore.commands.CheckCommand; -import com.onarandombox.MultiverseCore.commands.CloneCommand; -import com.onarandombox.MultiverseCore.commands.ConfigCommand; -import com.onarandombox.MultiverseCore.commands.ConfirmCommand; -import com.onarandombox.MultiverseCore.commands.CoordCommand; -import com.onarandombox.MultiverseCore.commands.CreateCommand; import com.onarandombox.MultiverseCore.commands.DebugCommand; -import com.onarandombox.MultiverseCore.commands.DeleteCommand; -import com.onarandombox.MultiverseCore.commands.EnvironmentCommand; -import com.onarandombox.MultiverseCore.commands.GameruleCommand; -import com.onarandombox.MultiverseCore.commands.GamerulesCommand; -import com.onarandombox.MultiverseCore.commands.GeneratorCommand; -import com.onarandombox.MultiverseCore.commands.HelpCommand; -import com.onarandombox.MultiverseCore.commands.ImportCommand; -import com.onarandombox.MultiverseCore.commands.InfoCommand; -import com.onarandombox.MultiverseCore.commands.ListCommand; -import com.onarandombox.MultiverseCore.commands.LoadCommand; -import com.onarandombox.MultiverseCore.commands.ModifyAddCommand; -import com.onarandombox.MultiverseCore.commands.ModifyClearCommand; -import com.onarandombox.MultiverseCore.commands.ModifyCommand; -import com.onarandombox.MultiverseCore.commands.ModifyRemoveCommand; -import com.onarandombox.MultiverseCore.commands.ModifySetCommand; -import com.onarandombox.MultiverseCore.commands.PurgeCommand; -import com.onarandombox.MultiverseCore.commands.RegenCommand; -import com.onarandombox.MultiverseCore.commands.ReloadCommand; -import com.onarandombox.MultiverseCore.commands.RemoveCommand; -import com.onarandombox.MultiverseCore.commands.ScriptCommand; -import com.onarandombox.MultiverseCore.commands.SetSpawnCommand; -import com.onarandombox.MultiverseCore.commands.SilentCommand; -import com.onarandombox.MultiverseCore.commands.SpawnCommand; -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.commandsold.AnchorCommand; +import com.onarandombox.MultiverseCore.commandsold.CheckCommand; +import com.onarandombox.MultiverseCore.commandsold.CloneCommand; +import com.onarandombox.MultiverseCore.commandsold.ConfigCommand; +import com.onarandombox.MultiverseCore.commandsold.ConfirmCommand; +import com.onarandombox.MultiverseCore.commandsold.CoordCommand; +import com.onarandombox.MultiverseCore.commandsold.CreateCommand; +import com.onarandombox.MultiverseCore.commandsold.DeleteCommand; +import com.onarandombox.MultiverseCore.commandsold.EnvironmentCommand; +import com.onarandombox.MultiverseCore.commandsold.GameruleCommand; +import com.onarandombox.MultiverseCore.commandsold.GamerulesCommand; +import com.onarandombox.MultiverseCore.commandsold.GeneratorCommand; +import com.onarandombox.MultiverseCore.commandsold.HelpCommand; +import com.onarandombox.MultiverseCore.commandsold.ImportCommand; +import com.onarandombox.MultiverseCore.commandsold.InfoCommand; +import com.onarandombox.MultiverseCore.commandsold.ListCommand; +import com.onarandombox.MultiverseCore.commandsold.LoadCommand; +import com.onarandombox.MultiverseCore.commandsold.ModifyAddCommand; +import com.onarandombox.MultiverseCore.commandsold.ModifyClearCommand; +import com.onarandombox.MultiverseCore.commandsold.ModifyCommand; +import com.onarandombox.MultiverseCore.commandsold.ModifyRemoveCommand; +import com.onarandombox.MultiverseCore.commandsold.ModifySetCommand; +import com.onarandombox.MultiverseCore.commandsold.PurgeCommand; +import com.onarandombox.MultiverseCore.commandsold.RegenCommand; +import com.onarandombox.MultiverseCore.commandsold.ReloadCommand; +import com.onarandombox.MultiverseCore.commandsold.RemoveCommand; +import com.onarandombox.MultiverseCore.commandsold.ScriptCommand; +import com.onarandombox.MultiverseCore.commandsold.SetSpawnCommand; +import com.onarandombox.MultiverseCore.commandsold.SilentCommand; +import com.onarandombox.MultiverseCore.commandsold.SpawnCommand; +import com.onarandombox.MultiverseCore.commandsold.TeleportCommand; +import com.onarandombox.MultiverseCore.commandsold.UnloadCommand; +import com.onarandombox.MultiverseCore.commandsold.VersionCommand; +import com.onarandombox.MultiverseCore.commandsold.WhoCommand; +import com.onarandombox.MultiverseCore.commandtools.MVCommandManager; +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; @@ -77,11 +64,8 @@ import com.onarandombox.MultiverseCore.destination.PlayerDestination; import com.onarandombox.MultiverseCore.destination.WorldDestination; import com.onarandombox.MultiverseCore.event.MVDebugModeEvent; import com.onarandombox.MultiverseCore.event.MVVersionEvent; -import com.onarandombox.MultiverseCore.listeners.MVAsyncPlayerChatListener; import com.onarandombox.MultiverseCore.listeners.MVChatListener; import com.onarandombox.MultiverseCore.listeners.MVEntityListener; -import com.onarandombox.MultiverseCore.listeners.MVMapListener; -import com.onarandombox.MultiverseCore.listeners.MVPlayerChatListener; import com.onarandombox.MultiverseCore.listeners.MVPlayerListener; import com.onarandombox.MultiverseCore.listeners.MVPortalListener; import com.onarandombox.MultiverseCore.listeners.MVWeatherListener; @@ -93,13 +77,14 @@ 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.metrics.MetricsConfigurator; import com.onarandombox.MultiverseCore.utils.SimpleBlockSafety; import com.onarandombox.MultiverseCore.utils.SimpleLocationManipulation; import com.onarandombox.MultiverseCore.utils.SimpleSafeTTeleporter; +import com.onarandombox.MultiverseCore.utils.TestingMode; import com.onarandombox.MultiverseCore.utils.UnsafeCallWrapper; import com.onarandombox.MultiverseCore.utils.VaultHandler; import com.onarandombox.MultiverseCore.utils.WorldManager; +import com.onarandombox.MultiverseCore.utils.metrics.MetricsConfigurator; import com.pneumaticraft.commandhandler.CommandHandler; import me.main__.util.SerializationConfig.NoSuchPropertyException; import me.main__.util.SerializationConfig.SerializationConfig; @@ -120,6 +105,20 @@ import org.bukkit.plugin.PluginManager; import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPluginLoader; +import java.io.BufferedReader; +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.UnsupportedEncodingException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.HashMap; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; + /** * The implementation of the Multiverse-{@link Core}. */ @@ -202,6 +201,8 @@ public class MultiverseCore extends JavaPlugin implements MVPlugin, Core { // Setup our Map for our Commands using the CommandHandler. private CommandHandler commandHandler; + private MVCommandManager commandManager; + private CommandQueueManager commandQueueManager; private static final String LOG_TAG = "[Multiverse-Core]"; @@ -284,6 +285,8 @@ public class MultiverseCore extends JavaPlugin implements MVPlugin, Core { // Setup the command manager this.commandHandler = new CommandHandler(this, this.ph); + this.commandManager = new MVCommandManager(this); + this.commandQueueManager = new CommandQueueManager(this); // Call the Function to assign all the Commands to their Class. this.registerCommands(); @@ -305,7 +308,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(); @@ -317,17 +320,8 @@ public class MultiverseCore extends JavaPlugin implements MVPlugin, Core { // A test that had no worlds loaded was being run. This should never happen in production } this.saveMVConfig(); - // Register async or sync player chat according to config - try { - Class.forName("org.bukkit.event.player.AsyncPlayerChatEvent"); - } catch (ClassNotFoundException e) { - getMVConfig().setUseAsyncChat(false); - } - if (getMVConfig().getUseAsyncChat()) { - this.chatListener = new MVAsyncPlayerChatListener(this, this.playerListener); - } else { - this.chatListener = new MVPlayerChatListener(this, this.playerListener); - } + + this.chatListener = new MVChatListener(this, this.playerListener); getServer().getPluginManager().registerEvents(this.chatListener, this); this.initializeBuscript(); @@ -343,16 +337,28 @@ public class MultiverseCore extends JavaPlugin implements MVPlugin, Core { } private void setupMetrics() { - MetricsConfigurator.configureMetrics(this); + 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; + + 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."); + } + } } private void initializeDestinationFactory() { @@ -375,9 +381,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."); pm.registerEvents(this.worldListener, this); - pm.registerEvents(new MVMapListener(this), this); } /** @@ -494,13 +498,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; } @@ -513,9 +517,9 @@ 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); + WorldProperties world = new WorldProperties(Collections.emptyMap()); ConfigurationSection section = (ConfigurationSection) entry.getValue(); // migrate animals and monsters @@ -679,8 +683,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) { @@ -697,7 +701,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; } @@ -766,13 +770,16 @@ public class MultiverseCore extends JavaPlugin implements MVPlugin, Core { this.commandHandler.registerCommand(new AnchorCommand(this)); // Misc Commands this.commandHandler.registerCommand(new EnvironmentCommand(this)); - this.commandHandler.registerCommand(new DebugCommand(this)); + // this.commandHandler.registerCommand(new DebugCommand(this)); this.commandHandler.registerCommand(new SilentCommand(this)); this.commandHandler.registerCommand(new GeneratorCommand(this)); this.commandHandler.registerCommand(new CheckCommand(this)); this.commandHandler.registerCommand(new ScriptCommand(this)); this.commandHandler.registerCommand(new GameruleCommand(this)); this.commandHandler.registerCommand(new GamerulesCommand(this)); + + //**NEW ACF COMMAND HANDLER** + this.commandManager.registerCommand(new DebugCommand(this)); } /** @@ -829,43 +836,6 @@ public class MultiverseCore extends JavaPlugin implements MVPlugin, Core { } } - /** - * {@inheritDoc} - */ - @Override - public void log(Level level, String msg) { - Logging.log(level, msg); - } - - /** - * Logs a message at the specified level. - * - * @param level The Log-{@link Level}. - * @param msg The message to log. - * - * @deprecated Replaced by {@link Logging}. Please refrain from using this from a third party plugin as the - * messages will appear to originate from Multiverse-Core. - */ - @Deprecated - public static void staticLog(Level level, String msg) { - Logging.log(level, msg); - } - - /** - * Print messages to the Debug Log, if the servers in Debug Mode then we also want to print the messages to the - * standard Server Console. - * - * @param level The Log-{@link Level} - * @param msg The message - * - * @deprecated Replaced by {@link Logging}. Please refrain from using this from a third party plugin as the - * messages will appear to originate from Multiverse-Core. - */ - @Deprecated - public static void staticDebugLog(Level level, String msg) { - Logging.log(level, msg); - } - /** * {@inheritDoc} */ @@ -900,13 +870,20 @@ public class MultiverseCore extends JavaPlugin implements MVPlugin, Core { } /** - * Gets the log-tag. - * - * @return The log-tag + * {@inheritDoc} */ - // TODO this should be static! - public String getTag() { - return MultiverseCore.LOG_TAG; + @Override + public MVCommandManager getCommandManager() { + return this.commandManager; + } + + /** + * {@inheritDoc} + */ + @Override + @Deprecated + public CommandQueueManager getCommandQueueManager() { + return commandQueueManager; } /** @@ -1051,7 +1028,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; } } @@ -1094,17 +1071,27 @@ public class MultiverseCore extends JavaPlugin implements MVPlugin, Core { * @return True if success, false if fail. */ public Boolean cloneWorld(String oldName, String newName, String generator) { - return this.worldManager.cloneWorld(oldName, newName, generator); + return this.worldManager.cloneWorld(oldName, newName); } /** * {@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); } /** @@ -1174,17 +1161,6 @@ public class MultiverseCore extends JavaPlugin implements MVPlugin, Core { return config; } - /** - * This method is currently used by other plugins. - * It will be removed in 2.4 - * @return The Multiverse config. - * @deprecated This is deprecated. - */ - @Deprecated - public static MultiverseCoreConfiguration getStaticConfig() { - return MultiverseCoreConfiguration.getInstance(); - } - @Override public Buscript getScriptAPI() { return buscript; diff --git a/src/main/java/com/onarandombox/MultiverseCore/MultiverseCoreConfiguration.java b/src/main/java/com/onarandombox/MultiverseCore/MultiverseCoreConfiguration.java index a04a6651..cc9b5fce 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/MultiverseCoreConfiguration.java +++ b/src/main/java/com/onarandombox/MultiverseCore/MultiverseCoreConfiguration.java @@ -48,14 +48,14 @@ public class MultiverseCoreConfiguration extends SerializationConfig implements @Property private volatile String prefixchatformat; @Property - private volatile boolean useasyncchat; - @Property private volatile boolean teleportintercept; @Property private volatile boolean firstspawnoverride; @Property private volatile boolean displaypermerrors; @Property + private volatile boolean enablebuscript; + @Property private volatile int globaldebug; @Property private volatile boolean silentstart; @@ -93,18 +93,18 @@ public class MultiverseCoreConfiguration extends SerializationConfig implements protected void setDefaults() { // 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 +213,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} */ @@ -303,16 +319,6 @@ public class MultiverseCoreConfiguration extends SerializationConfig implements this.teleportcooldown = teleportCooldown; } - @Override - public void setUseAsyncChat(boolean useAsyncChat) { - this.useasyncchat = useAsyncChat; - } - - @Override - public boolean getUseAsyncChat() { - return this.useasyncchat; - } - @Override public void setSilentStart(boolean silentStart) { Logging.setShowingConfig(!silentStart); diff --git a/src/main/java/com/onarandombox/MultiverseCore/api/Core.java b/src/main/java/com/onarandombox/MultiverseCore/api/Core.java index ff8513d4..d1d4e9e8 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/api/Core.java +++ b/src/main/java/com/onarandombox/MultiverseCore/api/Core.java @@ -8,6 +8,8 @@ package com.onarandombox.MultiverseCore.api; import buscript.Buscript; +import com.onarandombox.MultiverseCore.commandtools.MVCommandManager; +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 +88,23 @@ public interface Core { */ CommandHandler getCommandHandler(); + /** + * Multiverse uses {@link MVCommandManager} to make adding and using commands + * a piece of cake. + * + * @return A non-null {@link MVCommandManager}. + */ + MVCommandManager getCommandManager(); + + /** + * 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 +135,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 deleted file mode 100644 index f37ef788..00000000 --- a/src/main/java/com/onarandombox/MultiverseCore/api/LoggablePlugin.java +++ /dev/null @@ -1,30 +0,0 @@ -/****************************************************************************** - * Multiverse 2 Copyright (c) the Multiverse Team 2011. * - * 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.api; - -import org.bukkit.Server; - -import java.util.logging.Level; - -/** A simple API to require plugins to have a log method. */ -public interface LoggablePlugin { - /** - * Logs a message at the specified level. - * - * @param level The Log-{@link Level}. - * @param msg The message to log. - */ - void log(Level level, String msg); - - /** - * Gets the server instance that this plugin is attached to. - * - * @return A {@link Server} instance. - */ - Server getServer(); -} diff --git a/src/main/java/com/onarandombox/MultiverseCore/api/MVPlugin.java b/src/main/java/com/onarandombox/MultiverseCore/api/MVPlugin.java index e70cd35d..a9582edf 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/api/MVPlugin.java +++ b/src/main/java/com/onarandombox/MultiverseCore/api/MVPlugin.java @@ -13,7 +13,7 @@ import com.onarandombox.MultiverseCore.event.MVVersionEvent; /** * This interface is implemented by every official Multiverse-plugin. */ -public interface MVPlugin extends LoggablePlugin { +public interface MVPlugin { /** * Adds This plugin's version information to the buffer and returns the new string. * diff --git a/src/main/java/com/onarandombox/MultiverseCore/api/MVWorldManager.java b/src/main/java/com/onarandombox/MultiverseCore/api/MVWorldManager.java index 3eb96e2d..3c835c8d 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/api/MVWorldManager.java +++ b/src/main/java/com/onarandombox/MultiverseCore/api/MVWorldManager.java @@ -59,18 +59,6 @@ public interface MVWorldManager { boolean addWorld(String name, Environment env, String seedString, WorldType type, Boolean generateStructures, String generator, boolean useSpawnAdjust); - /** - * Make a copy of a world. - * - * @param oldName Name of world to be copied - * @param newName Name of world to be created - * @param generator The Custom generator plugin to use. Ignored. - * @return True if the world is copied successfully, false if not. - * @deprecated Use {@link #cloneWorld(String, String)} instead. - */ - @Deprecated - boolean cloneWorld(String oldName, String newName, String generator); - /** * Make a copy of a world. * @@ -174,6 +162,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 +181,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}. * @@ -213,19 +222,9 @@ public interface MVWorldManager { */ void loadDefaultWorlds(); - /** - * Return the World Purger. - * - * @return A valid {@link PurgeWorlds}. - * @deprecated {@link PurgeWorlds} is deprecated! - */ - @Deprecated - PurgeWorlds getWorldPurger(); - /** * Gets the {@link WorldPurger}. *

- * TODO: Remove {@link #getWorldPurger()} and replace it with this method. * @return The {@link WorldPurger} this {@link MVWorldManager} is using. * @see WorldPurger * @see SimpleWorldPurger @@ -292,15 +291,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 +326,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..ee8d0b16 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. @@ -146,18 +158,6 @@ public interface MultiverseCoreConfig extends ConfigurationSerializable { */ boolean getEnforceAccess(); - /** - * Sets useasyncchat. - * @param useAsyncChat The new value. - */ - void setUseAsyncChat(boolean useAsyncChat); - - /** - * Gets useasyncchat. - * @return useasyncchat. - */ - boolean getUseAsyncChat(); - /** * Sets whether to suppress startup messages. * diff --git a/src/main/java/com/onarandombox/MultiverseCore/api/MultiversePlugin.java b/src/main/java/com/onarandombox/MultiverseCore/api/MultiversePlugin.java index a6a064b8..28bbc919 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/api/MultiversePlugin.java +++ b/src/main/java/com/onarandombox/MultiverseCore/api/MultiversePlugin.java @@ -115,33 +115,6 @@ public abstract class MultiversePlugin extends JavaPlugin implements MVPlugin { return this.getCore().getCommandHandler().locateAndRunCommand(sender, allArgs); } - @Override - public void log(Level level, String msg) { - int debugLevel = this.getCore().getMVConfig().getGlobalDebug(); - if ((level == Level.FINE && debugLevel >= 1) || (level == Level.FINER && debugLevel >= 2) - || (level == Level.FINEST && debugLevel >= 3)) { - debugLog.log(level, msg); - } else if (level != Level.FINE && level != Level.FINER && level != Level.FINEST) { - String message = new StringBuilder(getLogTag()).append(msg).toString(); - this.getServer().getLogger().log(level, message); - debugLog.log(level, message); - } - } - - private String getLogTag() { - if (logTag == null) - logTag = String.format("[%s]", this.getDescription().getName()); - return logTag; - } - - /** - * Sets the debug log-tag. - * @param tag The new tag. - */ - protected final void setDebugLogTag(String tag) { - this.debugLog.setTag(tag); - } - @Override public final String dumpVersionInfo(String buffer) { throw new UnsupportedOperationException("This is gone."); diff --git a/src/main/java/com/onarandombox/MultiverseCore/commands/DebugCommand.java b/src/main/java/com/onarandombox/MultiverseCore/commands/DebugCommand.java index 4f6ec40c..e4cabe2a 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/commands/DebugCommand.java +++ b/src/main/java/com/onarandombox/MultiverseCore/commands/DebugCommand.java @@ -1,66 +1,57 @@ -/****************************************************************************** - * Multiverse 2 Copyright (c) the Multiverse Team 2011. * - * 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.commands; +import co.aikar.commands.CommandIssuer; +import co.aikar.commands.annotation.CommandAlias; +import co.aikar.commands.annotation.CommandCompletion; +import co.aikar.commands.annotation.CommandPermission; +import co.aikar.commands.annotation.Conditions; +import co.aikar.commands.annotation.Description; +import co.aikar.commands.annotation.Subcommand; +import co.aikar.commands.annotation.Syntax; +import com.dumptruckman.minecraft.util.Logging; import com.onarandombox.MultiverseCore.MultiverseCore; -import org.bukkit.ChatColor; -import org.bukkit.command.CommandSender; -import org.bukkit.permissions.PermissionDefault; +import com.onarandombox.MultiverseCore.locale.MVCorei18n; +import org.jetbrains.annotations.NotNull; -import java.util.List; -import java.util.logging.Level; - -/** - * Enables debug-information. - */ +@CommandAlias("mv") public class DebugCommand extends MultiverseCommand { public DebugCommand(MultiverseCore plugin) { super(plugin); - this.setName("Turn Debug on/off?"); - this.setCommandUsage("/mv debug" + ChatColor.GOLD + " [1|2|3|off|silent]"); - this.setArgRange(0, 1); - this.addKey("mv debug"); - this.addKey("mv d"); - this.addKey("mvdebug"); - this.addCommandExample("/mv debug " + ChatColor.GOLD + "2"); - this.setPermission("multiverse.core.debug", "Spams the console a bunch.", PermissionDefault.OP); } - @Override - public void runCommand(CommandSender sender, List args) { - if (args.size() == 1) { - if (args.get(0).equalsIgnoreCase("off")) { - plugin.getMVConfig().setGlobalDebug(0); - } else { - try { - int debugLevel = Integer.parseInt(args.get(0)); - if (debugLevel > 3 || debugLevel < 0) { - throw new NumberFormatException(); - } - plugin.getMVConfig().setGlobalDebug(debugLevel); - } catch (NumberFormatException e) { - sender.sendMessage(ChatColor.RED + "Error" + ChatColor.WHITE - + " setting debug level. Please use a number 0-3 " + ChatColor.AQUA + "(3 being many many messages!)"); - } - } - plugin.saveMVConfigs(); - } - this.displayDebugMode(sender); + @Subcommand("debug") + @CommandPermission("multiverse.core.debug") + @Description("{@@mv-core.debug_info_description}") + public void onShowDebugCommand(@NotNull CommandIssuer issuer) { + this.displayDebugMode(issuer); } - private void displayDebugMode(CommandSender sender) { - final int debugLevel = plugin.getMVConfig().getGlobalDebug(); + + @Subcommand("debug") + @CommandPermission("multiverse.core.debug") + @Syntax("<{@@mv-core.debug_change_syntax}>") + @CommandCompletion("@range:3") + @Description("{@@mv-core.debug_change_description}") + public void onChangeDebugCommand(@NotNull CommandIssuer issuer, + + @Conditions("debuglevel") + @Syntax("<{@@mv-core.debug_change_syntax}>") + @Description("{@@mv-core.debug_change_level_description}") + int level) { + + this.plugin.getMVConfig().setGlobalDebug(level); + this.plugin.saveMVConfigs(); + this.displayDebugMode(issuer); + } + + private void displayDebugMode(@NotNull CommandIssuer issuer) { + final int debugLevel = this.plugin.getMVConfig().getGlobalDebug(); if (debugLevel == 0) { - 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"); + issuer.sendInfo(MVCorei18n.DEBUG_INFO_OFF); + return; } + issuer.sendInfo(MVCorei18n.DEBUG_INFO_ON, "{level}", String.valueOf(debugLevel)); + Logging.fine("Multiverse Debug ENABLED."); } } diff --git a/src/main/java/com/onarandombox/MultiverseCore/commands/GamerulesCommand.java b/src/main/java/com/onarandombox/MultiverseCore/commands/GamerulesCommand.java deleted file mode 100644 index 321ac090..00000000 --- a/src/main/java/com/onarandombox/MultiverseCore/commands/GamerulesCommand.java +++ /dev/null @@ -1,76 +0,0 @@ -/****************************************************************************** - * Multiverse 2 Copyright (c) the Multiverse Team 2011. * - * 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.commands; - -import com.onarandombox.MultiverseCore.MultiverseCore; -import org.bukkit.Bukkit; -import org.bukkit.ChatColor; -import org.bukkit.World; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; -import org.bukkit.permissions.PermissionDefault; - -import java.util.List; - -/** - * Allows management of Anchor Destinations. - */ -public class GamerulesCommand extends MultiverseCommand { - - public GamerulesCommand(MultiverseCore plugin) { - super(plugin); - this.setName("List the Minecraft Game Rules for a World."); - this.setCommandUsage("/mv gamerules" + ChatColor.GOLD + " [WORLD]"); - this.setArgRange(0, 1); - this.addKey("mv gamerules"); - this.addKey("mv rules"); - this.addKey("mvgamerules"); - this.addKey("mvrules"); - this.addCommandExample("/mv gamerules"); - this.addCommandExample("/mvrules " + ChatColor.RED + "world_nether"); - this.setPermission("multiverse.core.gamerule.list", "Allows a player to list gamerules.", PermissionDefault.OP); - } - - - @Override - public void runCommand(CommandSender sender, List args) { - // We NEED a world from the command line - final Player p; - if (sender instanceof Player) { - p = (Player) sender; - } else { - p = null; - } - - if (args.size() == 0 && p == null) { - sender.sendMessage("From the command line, WORLD is required."); - sender.sendMessage(this.getCommandDesc()); - sender.sendMessage(this.getCommandUsage()); - sender.sendMessage("Nothing changed."); - return; - } - - final World world; - if (args.size() == 0) { - world = p.getWorld(); - } else { - world = Bukkit.getWorld(args.get(0)); - } - - final StringBuilder gameRules = new StringBuilder(); - for (final String gameRule : world.getGameRules()) { - if (gameRules.length() != 0) { - gameRules.append(ChatColor.WHITE).append(", "); - } - gameRules.append(ChatColor.AQUA).append(gameRule).append(ChatColor.WHITE).append(": "); - gameRules.append(ChatColor.GREEN).append(world.getGameRuleValue(gameRule)); - } - sender.sendMessage("=== Gamerules for " + ChatColor.AQUA + world.getName() + ChatColor.WHITE + " ==="); - sender.sendMessage(gameRules.toString()); - } -} diff --git a/src/main/java/com/onarandombox/MultiverseCore/commands/ListCommand.java b/src/main/java/com/onarandombox/MultiverseCore/commands/ListCommand.java deleted file mode 100644 index e63ea4f2..00000000 --- a/src/main/java/com/onarandombox/MultiverseCore/commands/ListCommand.java +++ /dev/null @@ -1,130 +0,0 @@ -/****************************************************************************** - * Multiverse 2 Copyright (c) the Multiverse Team 2011. * - * 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.commands; - -import com.onarandombox.MultiverseCore.MultiverseCore; -import com.onarandombox.MultiverseCore.api.MultiverseWorld; -import org.bukkit.ChatColor; -import org.bukkit.World.Environment; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; -import org.bukkit.permissions.PermissionDefault; - -import java.util.ArrayList; -import java.util.List; - -/** - * Displays a listing of all worlds that a player can enter. - */ -public class ListCommand extends PaginatedCoreCommand { - - public ListCommand(MultiverseCore plugin) { - super(plugin); - this.setName("World Listing"); - this.setCommandUsage("/mv list"); - 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; - } - - - 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; - } - } - - if (!(sender instanceof Player)) { - for (String c : availableWorlds) { - sender.sendMessage(c); - } - return; - } - - int totalPages = (int) Math.ceil(availableWorlds.size() / (this.itemsPerPage + 0.0)); - - if (filterObject.getPage() > totalPages) { - filterObject.setPage(totalPages); - } - - sender.sendMessage(ChatColor.AQUA + " Page " + filterObject.getPage() + " of " + totalPages); - - this.showPage(filterObject.getPage(), sender, availableWorlds); - } -} diff --git a/src/main/java/com/onarandombox/MultiverseCore/commands/MultiverseCommand.java b/src/main/java/com/onarandombox/MultiverseCore/commands/MultiverseCommand.java index ae54a9b8..42a65604 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/commands/MultiverseCommand.java +++ b/src/main/java/com/onarandombox/MultiverseCore/commands/MultiverseCommand.java @@ -1,40 +1,13 @@ -/****************************************************************************** - * Multiverse 2 Copyright (c) the Multiverse Team 2011. * - * 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.commands; +import co.aikar.commands.BaseCommand; import com.onarandombox.MultiverseCore.MultiverseCore; -import com.onarandombox.MultiverseCore.api.MultiverseMessaging; -import com.pneumaticraft.commandhandler.Command; -import org.bukkit.command.CommandSender; -import java.util.List; +public class MultiverseCommand extends BaseCommand { -/** - * A generic Multiverse-command. - */ -public abstract class MultiverseCommand extends Command { + protected final MultiverseCore plugin; - /** - * The reference to the core. - */ - protected MultiverseCore plugin; - /** - * The reference to {@link MultiverseMessaging}. - */ - protected MultiverseMessaging messaging; - - public MultiverseCommand(MultiverseCore plugin) { - super(plugin); + protected MultiverseCommand(MultiverseCore plugin) { this.plugin = plugin; - this.messaging = this.plugin.getMessaging(); } - - @Override - public abstract void runCommand(CommandSender sender, List args); - } diff --git a/src/main/java/com/onarandombox/MultiverseCore/commands/VersionCommand.java b/src/main/java/com/onarandombox/MultiverseCore/commands/VersionCommand.java deleted file mode 100644 index b2ac51e4..00000000 --- a/src/main/java/com/onarandombox/MultiverseCore/commands/VersionCommand.java +++ /dev/null @@ -1,227 +0,0 @@ -/****************************************************************************** - * Multiverse 2 Copyright (c) the Multiverse Team 2011. * - * 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.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.pneumaticraft.commandhandler.CommandHandler; -import org.apache.commons.lang.StringUtils; -import org.bukkit.ChatColor; -import org.bukkit.command.CommandSender; -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.util.List; -import java.util.Map; - -/** - * Dumps version info to the console. - */ -public class VersionCommand extends MultiverseCommand { - private static final URLShortener SHORTENER = new BitlyURLShortener(); - - public VersionCommand(MultiverseCore plugin) { - super(plugin); - this.setName("Multiverse Version"); - this.setCommandUsage("/mv version " + ChatColor.GOLD + "-[bh] [--include-plugin-list]"); - this.setArgRange(0, 2); - this.addKey("mv version"); - 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); - } - - 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(); - } - - 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(); - } - - 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 Map getVersionFiles() { - Map files = new HashMap(); - - // Add the legacy file, but as markdown so it's readable - files.put("version.md", this.getMarkdownString()); - - // Add the config.yml - File configFile = new File(this.plugin.getDataFolder(), "config.yml"); - files.put(configFile.getName(), this.readFile(configFile.getAbsolutePath())); - - // Add the config.yml - File worldConfig = new File(this.plugin.getDataFolder(), "worlds.yml"); - files.put(worldConfig.getName(), this.readFile(worldConfig.getAbsolutePath())); - return files; - } - - @Override - public void runCommand(final CommandSender sender, final List args) { - // Check if the command was sent from a Player. - if (sender instanceof Player) { - 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(); - this.plugin.getServer().getPluginManager().callEvent(versionEvent); - - String versionInfo = versionEvent.getVersionInfo(); - - if (CommandHandler.hasFlag("--include-plugin-list", args)) { - versionInfo = versionInfo + "\nPlugins: " + getPluginList(); - } - - final String data = versionInfo; - - // log to console - String[] lines = data.split("\n"); - for (String line : lines) { - if (!line.isEmpty()) { - Logging.info(line); - } - } - - BukkitRunnable logPoster = new BukkitRunnable() { - @Override - public void run() { - if (args.size() > 0) { - String pasteUrl; - if (CommandHandler.hasFlag("-b", args)) { - // private post to pastebin - pasteUrl = postToService(PasteServiceType.PASTEBIN, true, data, files); - } else if (CommandHandler.hasFlag("-h", args)) { - // private post to pastebin - pasteUrl = postToService(PasteServiceType.HASTEBIN, true, data, files); - } else { - return; - } - - if (!(sender instanceof ConsoleCommandSender)) { - sender.sendMessage("Version info dumped here: " + ChatColor.GREEN + pasteUrl); - } - Logging.info("Version info dumped here: %s", pasteUrl); - } - } - }; - - // Run the log posting operation asynchronously, since we don't know how long it will take. - logPoster.runTaskAsynchronously(this.plugin); - } - - /** - * Send the current contents of this.pasteBinBuffer to a web service. - * - * @param type Service type to send paste data to. - * @param isPrivate Should the paste be marked as private. - * @param pasteData Legacy string only data to post to a service. - * @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) { - PasteService ps = PasteServiceFactory.getService(type, isPrivate); - try { - String result; - if (ps.supportsMultiFile()) { - result = ps.postData(ps.encodeData(pasteFiles), ps.getPostURL()); - } else { - result = ps.postData(ps.encodeData(pasteData), ps.getPostURL()); - } - return SHORTENER.shorten(result); - } catch (PasteFailedException e) { - System.out.print(e); - return "Error posting to service"; - } - } - - private String getPluginList() { - return StringUtils.join(plugin.getServer().getPluginManager().getPlugins(), ", "); - } -} diff --git a/src/main/java/com/onarandombox/MultiverseCore/commands/package-info.java b/src/main/java/com/onarandombox/MultiverseCore/commands/package-info.java deleted file mode 100644 index 53d7f9b3..00000000 --- a/src/main/java/com/onarandombox/MultiverseCore/commands/package-info.java +++ /dev/null @@ -1,4 +0,0 @@ -/** - * This package contains all Commands. - */ -package com.onarandombox.MultiverseCore.commands; diff --git a/src/main/java/com/onarandombox/MultiverseCore/commands/AnchorCommand.java b/src/main/java/com/onarandombox/MultiverseCore/commandsold/AnchorCommand.java similarity index 99% rename from src/main/java/com/onarandombox/MultiverseCore/commands/AnchorCommand.java rename to src/main/java/com/onarandombox/MultiverseCore/commandsold/AnchorCommand.java index 55d0b2cf..5009c270 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/commands/AnchorCommand.java +++ b/src/main/java/com/onarandombox/MultiverseCore/commandsold/AnchorCommand.java @@ -5,7 +5,7 @@ * with this project. * ******************************************************************************/ -package com.onarandombox.MultiverseCore.commands; +package com.onarandombox.MultiverseCore.commandsold; import com.onarandombox.MultiverseCore.MultiverseCore; import org.bukkit.ChatColor; diff --git a/src/main/java/com/onarandombox/MultiverseCore/commands/CheckCommand.java b/src/main/java/com/onarandombox/MultiverseCore/commandsold/CheckCommand.java similarity index 94% rename from src/main/java/com/onarandombox/MultiverseCore/commands/CheckCommand.java rename to src/main/java/com/onarandombox/MultiverseCore/commandsold/CheckCommand.java index 474308c6..519dfa21 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/commands/CheckCommand.java +++ b/src/main/java/com/onarandombox/MultiverseCore/commandsold/CheckCommand.java @@ -5,12 +5,13 @@ * with this project. * ******************************************************************************/ -package com.onarandombox.MultiverseCore.commands; +package com.onarandombox.MultiverseCore.commandsold; 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/CloneCommand.java b/src/main/java/com/onarandombox/MultiverseCore/commandsold/CloneCommand.java similarity index 97% rename from src/main/java/com/onarandombox/MultiverseCore/commands/CloneCommand.java rename to src/main/java/com/onarandombox/MultiverseCore/commandsold/CloneCommand.java index 95a0bf14..7f2a2efe 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/commands/CloneCommand.java +++ b/src/main/java/com/onarandombox/MultiverseCore/commandsold/CloneCommand.java @@ -5,7 +5,7 @@ * with this project. * ******************************************************************************/ -package com.onarandombox.MultiverseCore.commands; +package com.onarandombox.MultiverseCore.commandsold; import java.util.List; diff --git a/src/main/java/com/onarandombox/MultiverseCore/commands/ConfigCommand.java b/src/main/java/com/onarandombox/MultiverseCore/commandsold/ConfigCommand.java similarity index 98% rename from src/main/java/com/onarandombox/MultiverseCore/commands/ConfigCommand.java rename to src/main/java/com/onarandombox/MultiverseCore/commandsold/ConfigCommand.java index 55424a94..e454dd95 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/commands/ConfigCommand.java +++ b/src/main/java/com/onarandombox/MultiverseCore/commandsold/ConfigCommand.java @@ -5,7 +5,7 @@ * with this project. * ******************************************************************************/ -package com.onarandombox.MultiverseCore.commands; +package com.onarandombox.MultiverseCore.commandsold; import com.onarandombox.MultiverseCore.MultiverseCore; import org.bukkit.ChatColor; diff --git a/src/main/java/com/onarandombox/MultiverseCore/commands/ConfirmCommand.java b/src/main/java/com/onarandombox/MultiverseCore/commandsold/ConfirmCommand.java similarity index 91% rename from src/main/java/com/onarandombox/MultiverseCore/commands/ConfirmCommand.java rename to src/main/java/com/onarandombox/MultiverseCore/commandsold/ConfirmCommand.java index 2a68f353..c216ca37 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/commands/ConfirmCommand.java +++ b/src/main/java/com/onarandombox/MultiverseCore/commandsold/ConfirmCommand.java @@ -5,7 +5,7 @@ * with this project. * ******************************************************************************/ -package com.onarandombox.MultiverseCore.commands; +package com.onarandombox.MultiverseCore.commandsold; import com.onarandombox.MultiverseCore.MultiverseCore; import org.bukkit.command.CommandSender; @@ -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/CoordCommand.java b/src/main/java/com/onarandombox/MultiverseCore/commandsold/CoordCommand.java similarity index 97% rename from src/main/java/com/onarandombox/MultiverseCore/commands/CoordCommand.java rename to src/main/java/com/onarandombox/MultiverseCore/commandsold/CoordCommand.java index 485cc370..b7ce8553 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/commands/CoordCommand.java +++ b/src/main/java/com/onarandombox/MultiverseCore/commandsold/CoordCommand.java @@ -5,13 +5,12 @@ * with this project. * ******************************************************************************/ -package com.onarandombox.MultiverseCore.commands; +package com.onarandombox.MultiverseCore.commandsold; import com.onarandombox.MultiverseCore.MultiverseCore; import com.onarandombox.MultiverseCore.api.MVWorldManager; import com.onarandombox.MultiverseCore.api.MultiverseWorld; import org.bukkit.ChatColor; -import org.bukkit.Material; import org.bukkit.World; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; diff --git a/src/main/java/com/onarandombox/MultiverseCore/commands/CreateCommand.java b/src/main/java/com/onarandombox/MultiverseCore/commandsold/CreateCommand.java similarity index 91% rename from src/main/java/com/onarandombox/MultiverseCore/commands/CreateCommand.java rename to src/main/java/com/onarandombox/MultiverseCore/commandsold/CreateCommand.java index 87bdacab..750c9e62 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/commands/CreateCommand.java +++ b/src/main/java/com/onarandombox/MultiverseCore/commandsold/CreateCommand.java @@ -5,7 +5,7 @@ * with this project. * ******************************************************************************/ -package com.onarandombox.MultiverseCore.commands; +package com.onarandombox.MultiverseCore.commandsold; import com.onarandombox.MultiverseCore.MultiverseCore; import com.onarandombox.MultiverseCore.api.MVWorldManager; @@ -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/commandsold/DebugCommand.java b/src/main/java/com/onarandombox/MultiverseCore/commandsold/DebugCommand.java new file mode 100644 index 00000000..81a7f74f --- /dev/null +++ b/src/main/java/com/onarandombox/MultiverseCore/commandsold/DebugCommand.java @@ -0,0 +1,66 @@ +/****************************************************************************** + * Multiverse 2 Copyright (c) the Multiverse Team 2011. * + * 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.commandsold; + +import com.dumptruckman.minecraft.util.Logging; +import com.onarandombox.MultiverseCore.MultiverseCore; +import org.bukkit.ChatColor; +import org.bukkit.command.CommandSender; +import org.bukkit.permissions.PermissionDefault; + +import java.util.List; + +/** + * Enables debug-information. + */ +public class DebugCommand extends MultiverseCommand { + + public DebugCommand(MultiverseCore plugin) { + super(plugin); + this.setName("Turn Debug on/off?"); + this.setCommandUsage("/mv debug" + ChatColor.GOLD + " [1|2|3|off|silent]"); + this.setArgRange(0, 1); + this.addKey("mv debug"); + this.addKey("mv d"); + this.addKey("mvdebug"); + this.addCommandExample("/mv debug " + ChatColor.GOLD + "2"); + this.setPermission("multiverse.core.debug", "Spams the console a bunch.", PermissionDefault.OP); + } + + @Override + public void runCommand(CommandSender sender, List args) { + if (args.size() == 1) { + if (args.get(0).equalsIgnoreCase("off")) { + plugin.getMVConfig().setGlobalDebug(0); + } else { + try { + int debugLevel = Integer.parseInt(args.get(0)); + if (debugLevel > 3 || debugLevel < 0) { + throw new NumberFormatException(); + } + plugin.getMVConfig().setGlobalDebug(debugLevel); + } catch (NumberFormatException e) { + sender.sendMessage(ChatColor.RED + "Error" + ChatColor.WHITE + + " setting debug level. Please use a number 0-3 " + ChatColor.AQUA + "(3 being many many messages!)"); + } + } + plugin.saveMVConfigs(); + } + this.displayDebugMode(sender); + } + + private void displayDebugMode(CommandSender sender) { + final int debugLevel = plugin.getMVConfig().getGlobalDebug(); + if (debugLevel == 0) { + sender.sendMessage("Multiverse Debug mode is " + ChatColor.RED + "OFF"); + } else { + sender.sendMessage("Multiverse Debug mode is " + ChatColor.GREEN + debugLevel); + Logging.fine("Multiverse Debug ENABLED"); + } + } +} diff --git a/src/main/java/com/onarandombox/MultiverseCore/commands/DeleteCommand.java b/src/main/java/com/onarandombox/MultiverseCore/commandsold/DeleteCommand.java similarity index 56% rename from src/main/java/com/onarandombox/MultiverseCore/commands/DeleteCommand.java rename to src/main/java/com/onarandombox/MultiverseCore/commandsold/DeleteCommand.java index 2e7b3c8b..85157d16 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/commands/DeleteCommand.java +++ b/src/main/java/com/onarandombox/MultiverseCore/commandsold/DeleteCommand.java @@ -5,14 +5,15 @@ * with this project. * ******************************************************************************/ -package com.onarandombox.MultiverseCore.commands; +package com.onarandombox.MultiverseCore.commandsold; 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 +36,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/EnvironmentCommand.java b/src/main/java/com/onarandombox/MultiverseCore/commandsold/EnvironmentCommand.java similarity index 98% rename from src/main/java/com/onarandombox/MultiverseCore/commands/EnvironmentCommand.java rename to src/main/java/com/onarandombox/MultiverseCore/commandsold/EnvironmentCommand.java index f9ac8860..9fc85cd4 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/commands/EnvironmentCommand.java +++ b/src/main/java/com/onarandombox/MultiverseCore/commandsold/EnvironmentCommand.java @@ -5,7 +5,7 @@ * with this project. * ******************************************************************************/ -package com.onarandombox.MultiverseCore.commands; +package com.onarandombox.MultiverseCore.commandsold; import com.onarandombox.MultiverseCore.MultiverseCore; import org.bukkit.ChatColor; diff --git a/src/main/java/com/onarandombox/MultiverseCore/commands/GameruleCommand.java b/src/main/java/com/onarandombox/MultiverseCore/commandsold/GameruleCommand.java similarity index 55% rename from src/main/java/com/onarandombox/MultiverseCore/commands/GameruleCommand.java rename to src/main/java/com/onarandombox/MultiverseCore/commandsold/GameruleCommand.java index 01eddae6..053c9f23 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/commands/GameruleCommand.java +++ b/src/main/java/com/onarandombox/MultiverseCore/commandsold/GameruleCommand.java @@ -5,11 +5,12 @@ * with this project. * ******************************************************************************/ -package com.onarandombox.MultiverseCore.commands; +package com.onarandombox.MultiverseCore.commandsold; 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,7 +56,7 @@ 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) { @@ -64,17 +65,52 @@ public class GameruleCommand extends MultiverseCommand { 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."); + + 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/commandsold/GamerulesCommand.java b/src/main/java/com/onarandombox/MultiverseCore/commandsold/GamerulesCommand.java new file mode 100644 index 00000000..4a8726df --- /dev/null +++ b/src/main/java/com/onarandombox/MultiverseCore/commandsold/GamerulesCommand.java @@ -0,0 +1,111 @@ +/****************************************************************************** + * Multiverse 2 Copyright (c) the Multiverse Team 2011. * + * 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.commandsold; + +import com.onarandombox.MultiverseCore.MultiverseCore; +import com.onarandombox.MultiverseCore.display.ContentDisplay; +import com.onarandombox.MultiverseCore.display.filters.ContentFilter; +import com.onarandombox.MultiverseCore.display.filters.DefaultContentFilter; +import com.onarandombox.MultiverseCore.display.filters.RegexContentFilter; +import com.onarandombox.MultiverseCore.display.handlers.InlineSendHandler; +import com.onarandombox.MultiverseCore.display.parsers.MapContentParser; +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. + */ +public class GamerulesCommand extends MultiverseCommand { + + public GamerulesCommand(MultiverseCore plugin) { + super(plugin); + this.setName("List the Minecraft Game Rules for a World."); + this.setCommandUsage("/mv gamerules" + ChatColor.GOLD + " [WORLD] [FILTER]"); + this.setArgRange(0, 2); + this.addKey("mv gamerules"); + this.addKey("mv rules"); + this.addKey("mvgamerules"); + this.addKey("mvrules"); + this.addCommandExample("/mv gamerules"); + this.addCommandExample("/mvrules " + ChatColor.RED + "world_nether"); + this.setPermission("multiverse.core.gamerule.list", "Allows a player to list gamerules.", PermissionDefault.OP); + } + + @Override + public void runCommand(CommandSender sender, List args) { + // We NEED a world from the command line + Player p; + World world; + ContentFilter filter = DefaultContentFilter.getInstance(); + + if (sender instanceof Player) { + p = (Player) sender; + } else { + p = null; + } + + if (args.size() == 0 && p == null) { + sender.sendMessage("From the command line, WORLD is required."); + sender.sendMessage(this.getCommandDesc()); + sender.sendMessage(this.getCommandUsage()); + sender.sendMessage("Nothing changed."); + return; + } + + // Not the best way, need to fix with ACF soon... + if (args.size() == 0) { + world = p.getWorld(); + } else if (args.size() == 1) { + world = Bukkit.getWorld(args.get(0)); + if (world == null) { + if (p == null) { + sender.sendMessage(ChatColor.RED + "Failure!" + ChatColor.WHITE + " World " + ChatColor.AQUA + args.get(0) + + ChatColor.WHITE + " does not exist."); + return; + } + world = p.getWorld(); + filter = RegexContentFilter.fromString(args.get(0)); + } + } 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; + } + filter = RegexContentFilter.fromString(args.get(1)); + } + + ContentDisplay.create() + .addContentParser(MapContentParser.forContent(getGameRuleMap(world)) + .withKeyColor(ChatColor.GREEN) + .withValueColor(ChatColor.YELLOW)) + .withSendHandler(InlineSendHandler.create() + .withHeader("====[ Gamerules for %s%s%s ]====", ChatColor.AQUA, world.getName(), ChatColor.WHITE) + .withFilter(filter)) + .send(sender); + } + + private Map getGameRuleMap(World world) { + Map gameRuleMap = new HashMap<>(); + for (GameRule rule : GameRule.values()) { + Object value = world.getGameRuleValue(rule); + gameRuleMap.put(rule.getName(), value); + } + return gameRuleMap; + } +} diff --git a/src/main/java/com/onarandombox/MultiverseCore/commands/GeneratorCommand.java b/src/main/java/com/onarandombox/MultiverseCore/commandsold/GeneratorCommand.java similarity index 97% rename from src/main/java/com/onarandombox/MultiverseCore/commands/GeneratorCommand.java rename to src/main/java/com/onarandombox/MultiverseCore/commandsold/GeneratorCommand.java index 72d4379b..342d71a1 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/commands/GeneratorCommand.java +++ b/src/main/java/com/onarandombox/MultiverseCore/commandsold/GeneratorCommand.java @@ -5,7 +5,7 @@ * with this project. * ******************************************************************************/ -package com.onarandombox.MultiverseCore.commands; +package com.onarandombox.MultiverseCore.commandsold; import com.dumptruckman.minecraft.util.Logging; import com.onarandombox.MultiverseCore.MultiverseCore; diff --git a/src/main/java/com/onarandombox/MultiverseCore/commands/HelpCommand.java b/src/main/java/com/onarandombox/MultiverseCore/commandsold/HelpCommand.java similarity index 83% rename from src/main/java/com/onarandombox/MultiverseCore/commands/HelpCommand.java rename to src/main/java/com/onarandombox/MultiverseCore/commandsold/HelpCommand.java index 90913026..42cbf978 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/commands/HelpCommand.java +++ b/src/main/java/com/onarandombox/MultiverseCore/commandsold/HelpCommand.java @@ -5,7 +5,7 @@ * with this project. * ******************************************************************************/ -package com.onarandombox.MultiverseCore.commands; +package com.onarandombox.MultiverseCore.commandsold; import com.onarandombox.MultiverseCore.MultiverseCore; import com.pneumaticraft.commandhandler.Command; @@ -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/commandsold/ImportCommand.java similarity index 71% rename from src/main/java/com/onarandombox/MultiverseCore/commands/ImportCommand.java rename to src/main/java/com/onarandombox/MultiverseCore/commandsold/ImportCommand.java index b16bf239..9d44dbcf 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/commands/ImportCommand.java +++ b/src/main/java/com/onarandombox/MultiverseCore/commandsold/ImportCommand.java @@ -5,11 +5,11 @@ * with this project. * ******************************************************************************/ -package com.onarandombox.MultiverseCore.commands; +package com.onarandombox.MultiverseCore.commandsold; 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/commandsold/InfoCommand.java similarity index 98% rename from src/main/java/com/onarandombox/MultiverseCore/commands/InfoCommand.java rename to src/main/java/com/onarandombox/MultiverseCore/commandsold/InfoCommand.java index ed7eb415..579f271d 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/commands/InfoCommand.java +++ b/src/main/java/com/onarandombox/MultiverseCore/commandsold/InfoCommand.java @@ -5,7 +5,7 @@ * with this project. * ******************************************************************************/ -package com.onarandombox.MultiverseCore.commands; +package com.onarandombox.MultiverseCore.commandsold; import com.onarandombox.MultiverseCore.MultiverseCore; import com.onarandombox.MultiverseCore.api.FancyText; @@ -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/commandsold/ListCommand.java b/src/main/java/com/onarandombox/MultiverseCore/commandsold/ListCommand.java new file mode 100644 index 00000000..9b6c1a73 --- /dev/null +++ b/src/main/java/com/onarandombox/MultiverseCore/commandsold/ListCommand.java @@ -0,0 +1,120 @@ +/****************************************************************************** + * Multiverse 2 Copyright (c) the Multiverse Team 2011. * + * 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.commandsold; + +import com.onarandombox.MultiverseCore.MultiverseCore; +import com.onarandombox.MultiverseCore.api.MultiverseWorld; +import com.onarandombox.MultiverseCore.display.ContentDisplay; +import com.onarandombox.MultiverseCore.display.filters.ContentFilter; +import com.onarandombox.MultiverseCore.display.filters.DefaultContentFilter; +import com.onarandombox.MultiverseCore.display.filters.RegexContentFilter; +import com.onarandombox.MultiverseCore.display.handlers.PagedSendHandler; +import com.onarandombox.MultiverseCore.display.parsers.ContentParser; +import org.bukkit.ChatColor; +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.List; + +/** + * Displays a listing of all worlds that a player can enter. + */ +public class ListCommand extends MultiverseCommand { + + public ListCommand(MultiverseCore plugin) { + super(plugin); + this.setName("World Listing"); + 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); + } + + @Override + public void runCommand(CommandSender sender, List args) { + ContentFilter filter = DefaultContentFilter.getInstance(); + int page = 1; + + // Either page or filter. + if (args.size() == 1) { + try { + page = Integer.parseInt(args.get(0)); + } catch (NumberFormatException ignore) { + filter = RegexContentFilter.fromString(args.get(0)); + } + } + + // Filter then page. + if (args.size() == 2) { + filter = RegexContentFilter.fromString(args.get(0)); + try { + page = Integer.parseInt(args.get(1)); + } catch (NumberFormatException ignore) { + sender.sendMessage(ChatColor.RED + args.get(1) + " is not valid number!"); + } + } + + ContentDisplay.create() + .addContentParser(new WorldListContentParser()) + .withSendHandler(PagedSendHandler.create() + .withHeader("%s====[ Multiverse World List ]====", ChatColor.GOLD) + .withFilter(filter) + .withTargetPage(page)) + .send(sender); + } + + private class WorldListContentParser implements ContentParser { + + @Override + public void parse(@NotNull CommandSender sender, @NotNull List content) { + Player player = (sender instanceof Player) ? (Player) sender : null; + + 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())) + .forEach(content::add); + + plugin.getMVWorldManager().getUnloadedWorlds().stream() + .filter(world -> plugin.getMVPerms().hasPermission(sender, "multiverse.access." + world, true)) + .map(world -> ChatColor.GRAY + world + " - UNLOADED") + .forEach(content::add); + } + + private boolean canSeeWorld(Player player, MultiverseWorld world) { + return !world.isHidden() + || player == null + || 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; + } + return color + env.toString(); + } + } +} diff --git a/src/main/java/com/onarandombox/MultiverseCore/commands/LoadCommand.java b/src/main/java/com/onarandombox/MultiverseCore/commandsold/LoadCommand.java similarity index 96% rename from src/main/java/com/onarandombox/MultiverseCore/commands/LoadCommand.java rename to src/main/java/com/onarandombox/MultiverseCore/commandsold/LoadCommand.java index 4a96ef36..2b60a8da 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/commands/LoadCommand.java +++ b/src/main/java/com/onarandombox/MultiverseCore/commandsold/LoadCommand.java @@ -5,7 +5,7 @@ * with this project. * ******************************************************************************/ -package com.onarandombox.MultiverseCore.commands; +package com.onarandombox.MultiverseCore.commandsold; import com.onarandombox.MultiverseCore.MultiverseCore; import org.bukkit.ChatColor; diff --git a/src/main/java/com/onarandombox/MultiverseCore/commands/ModifyAddCommand.java b/src/main/java/com/onarandombox/MultiverseCore/commandsold/ModifyAddCommand.java similarity index 98% rename from src/main/java/com/onarandombox/MultiverseCore/commands/ModifyAddCommand.java rename to src/main/java/com/onarandombox/MultiverseCore/commandsold/ModifyAddCommand.java index 1175d340..93229d32 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/commands/ModifyAddCommand.java +++ b/src/main/java/com/onarandombox/MultiverseCore/commandsold/ModifyAddCommand.java @@ -5,7 +5,7 @@ * with this project. * ******************************************************************************/ -package com.onarandombox.MultiverseCore.commands; +package com.onarandombox.MultiverseCore.commandsold; import com.onarandombox.MultiverseCore.MultiverseCore; import com.onarandombox.MultiverseCore.api.MVWorldManager; diff --git a/src/main/java/com/onarandombox/MultiverseCore/commands/ModifyClearCommand.java b/src/main/java/com/onarandombox/MultiverseCore/commandsold/ModifyClearCommand.java similarity index 98% rename from src/main/java/com/onarandombox/MultiverseCore/commands/ModifyClearCommand.java rename to src/main/java/com/onarandombox/MultiverseCore/commandsold/ModifyClearCommand.java index 788b7452..93e57663 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/commands/ModifyClearCommand.java +++ b/src/main/java/com/onarandombox/MultiverseCore/commandsold/ModifyClearCommand.java @@ -5,7 +5,7 @@ * with this project. * ******************************************************************************/ -package com.onarandombox.MultiverseCore.commands; +package com.onarandombox.MultiverseCore.commandsold; import com.onarandombox.MultiverseCore.MultiverseCore; import com.onarandombox.MultiverseCore.api.MVWorldManager; diff --git a/src/main/java/com/onarandombox/MultiverseCore/commands/ModifyCommand.java b/src/main/java/com/onarandombox/MultiverseCore/commandsold/ModifyCommand.java similarity index 98% rename from src/main/java/com/onarandombox/MultiverseCore/commands/ModifyCommand.java rename to src/main/java/com/onarandombox/MultiverseCore/commandsold/ModifyCommand.java index d8c74ac3..c87a0761 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/commands/ModifyCommand.java +++ b/src/main/java/com/onarandombox/MultiverseCore/commandsold/ModifyCommand.java @@ -5,7 +5,7 @@ * with this project. * ******************************************************************************/ -package com.onarandombox.MultiverseCore.commands; +package com.onarandombox.MultiverseCore.commandsold; import com.onarandombox.MultiverseCore.MultiverseCore; import com.onarandombox.MultiverseCore.enums.Action; diff --git a/src/main/java/com/onarandombox/MultiverseCore/commands/ModifyRemoveCommand.java b/src/main/java/com/onarandombox/MultiverseCore/commandsold/ModifyRemoveCommand.java similarity index 98% rename from src/main/java/com/onarandombox/MultiverseCore/commands/ModifyRemoveCommand.java rename to src/main/java/com/onarandombox/MultiverseCore/commandsold/ModifyRemoveCommand.java index f714be76..51792d99 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/commands/ModifyRemoveCommand.java +++ b/src/main/java/com/onarandombox/MultiverseCore/commandsold/ModifyRemoveCommand.java @@ -5,7 +5,7 @@ * with this project. * ******************************************************************************/ -package com.onarandombox.MultiverseCore.commands; +package com.onarandombox.MultiverseCore.commandsold; import com.onarandombox.MultiverseCore.MultiverseCore; import com.onarandombox.MultiverseCore.api.MVWorldManager; diff --git a/src/main/java/com/onarandombox/MultiverseCore/commands/ModifySetCommand.java b/src/main/java/com/onarandombox/MultiverseCore/commandsold/ModifySetCommand.java similarity index 99% rename from src/main/java/com/onarandombox/MultiverseCore/commands/ModifySetCommand.java rename to src/main/java/com/onarandombox/MultiverseCore/commandsold/ModifySetCommand.java index 54bf8a11..57a48768 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/commands/ModifySetCommand.java +++ b/src/main/java/com/onarandombox/MultiverseCore/commandsold/ModifySetCommand.java @@ -5,7 +5,7 @@ * with this project. * ******************************************************************************/ -package com.onarandombox.MultiverseCore.commands; +package com.onarandombox.MultiverseCore.commandsold; import com.onarandombox.MultiverseCore.MultiverseCore; import com.onarandombox.MultiverseCore.api.MVWorldManager; diff --git a/src/main/java/com/onarandombox/MultiverseCore/commandsold/MultiverseCommand.java b/src/main/java/com/onarandombox/MultiverseCore/commandsold/MultiverseCommand.java new file mode 100644 index 00000000..ac0aa7ba --- /dev/null +++ b/src/main/java/com/onarandombox/MultiverseCore/commandsold/MultiverseCommand.java @@ -0,0 +1,40 @@ +/****************************************************************************** + * Multiverse 2 Copyright (c) the Multiverse Team 2011. * + * 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.commandsold; + +import java.util.List; + +import com.onarandombox.MultiverseCore.MultiverseCore; +import com.onarandombox.MultiverseCore.api.MultiverseMessaging; +import com.pneumaticraft.commandhandler.Command; +import org.bukkit.command.CommandSender; + +/** + * A generic Multiverse-command. + */ +public abstract class MultiverseCommand extends Command { + + /** + * The reference to the core. + */ + protected MultiverseCore plugin; + /** + * The reference to {@link MultiverseMessaging}. + */ + protected MultiverseMessaging messaging; + + public MultiverseCommand(MultiverseCore plugin) { + super(plugin); + this.plugin = plugin; + this.messaging = this.plugin.getMessaging(); + } + + @Override + public abstract void runCommand(CommandSender sender, List args); + +} diff --git a/src/main/java/com/onarandombox/MultiverseCore/commands/PaginatedCommand.java b/src/main/java/com/onarandombox/MultiverseCore/commandsold/PaginatedCommand.java similarity index 90% rename from src/main/java/com/onarandombox/MultiverseCore/commands/PaginatedCommand.java rename to src/main/java/com/onarandombox/MultiverseCore/commandsold/PaginatedCommand.java index c3ddf09b..3e603e87 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/commands/PaginatedCommand.java +++ b/src/main/java/com/onarandombox/MultiverseCore/commandsold/PaginatedCommand.java @@ -5,7 +5,7 @@ * with this project. * ******************************************************************************/ -package com.onarandombox.MultiverseCore.commands; +package com.onarandombox.MultiverseCore.commandsold; import com.pneumaticraft.commandhandler.Command; import org.bukkit.command.CommandSender; @@ -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/PaginatedCoreCommand.java b/src/main/java/com/onarandombox/MultiverseCore/commandsold/PaginatedCoreCommand.java similarity index 94% rename from src/main/java/com/onarandombox/MultiverseCore/commands/PaginatedCoreCommand.java rename to src/main/java/com/onarandombox/MultiverseCore/commandsold/PaginatedCoreCommand.java index 7071311a..ebd407b7 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/commands/PaginatedCoreCommand.java +++ b/src/main/java/com/onarandombox/MultiverseCore/commandsold/PaginatedCoreCommand.java @@ -5,7 +5,7 @@ * with this project. * ******************************************************************************/ -package com.onarandombox.MultiverseCore.commands; +package com.onarandombox.MultiverseCore.commandsold; import com.onarandombox.MultiverseCore.MultiverseCore; diff --git a/src/main/java/com/onarandombox/MultiverseCore/commands/PurgeCommand.java b/src/main/java/com/onarandombox/MultiverseCore/commandsold/PurgeCommand.java similarity index 98% rename from src/main/java/com/onarandombox/MultiverseCore/commands/PurgeCommand.java rename to src/main/java/com/onarandombox/MultiverseCore/commandsold/PurgeCommand.java index 8b7c4a26..38f32ca6 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/commands/PurgeCommand.java +++ b/src/main/java/com/onarandombox/MultiverseCore/commandsold/PurgeCommand.java @@ -5,7 +5,7 @@ * with this project. * ******************************************************************************/ -package com.onarandombox.MultiverseCore.commands; +package com.onarandombox.MultiverseCore.commandsold; import com.onarandombox.MultiverseCore.MultiverseCore; import com.onarandombox.MultiverseCore.api.MVWorldManager; diff --git a/src/main/java/com/onarandombox/MultiverseCore/commands/RegenCommand.java b/src/main/java/com/onarandombox/MultiverseCore/commandsold/RegenCommand.java similarity index 53% rename from src/main/java/com/onarandombox/MultiverseCore/commands/RegenCommand.java rename to src/main/java/com/onarandombox/MultiverseCore/commandsold/RegenCommand.java index c93e36a7..2d249842 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/commands/RegenCommand.java +++ b/src/main/java/com/onarandombox/MultiverseCore/commandsold/RegenCommand.java @@ -5,14 +5,16 @@ * with this project. * ******************************************************************************/ -package com.onarandombox.MultiverseCore.commands; +package com.onarandombox.MultiverseCore.commandsold; 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/commandsold/ReloadCommand.java similarity index 96% rename from src/main/java/com/onarandombox/MultiverseCore/commands/ReloadCommand.java rename to src/main/java/com/onarandombox/MultiverseCore/commandsold/ReloadCommand.java index 3481a4ea..680de34a 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/commands/ReloadCommand.java +++ b/src/main/java/com/onarandombox/MultiverseCore/commandsold/ReloadCommand.java @@ -5,7 +5,7 @@ * with this project. * ******************************************************************************/ -package com.onarandombox.MultiverseCore.commands; +package com.onarandombox.MultiverseCore.commandsold; import com.onarandombox.MultiverseCore.MultiverseCore; import com.onarandombox.MultiverseCore.event.MVConfigReloadEvent; @@ -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/RemoveCommand.java b/src/main/java/com/onarandombox/MultiverseCore/commandsold/RemoveCommand.java similarity index 96% rename from src/main/java/com/onarandombox/MultiverseCore/commands/RemoveCommand.java rename to src/main/java/com/onarandombox/MultiverseCore/commandsold/RemoveCommand.java index ff24e740..aa821b33 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/commands/RemoveCommand.java +++ b/src/main/java/com/onarandombox/MultiverseCore/commandsold/RemoveCommand.java @@ -5,7 +5,7 @@ * with this project. * ******************************************************************************/ -package com.onarandombox.MultiverseCore.commands; +package com.onarandombox.MultiverseCore.commandsold; import com.onarandombox.MultiverseCore.MultiverseCore; import org.bukkit.ChatColor; diff --git a/src/main/java/com/onarandombox/MultiverseCore/commands/ScriptCommand.java b/src/main/java/com/onarandombox/MultiverseCore/commandsold/ScriptCommand.java similarity index 90% rename from src/main/java/com/onarandombox/MultiverseCore/commands/ScriptCommand.java rename to src/main/java/com/onarandombox/MultiverseCore/commandsold/ScriptCommand.java index 2896a8e7..baec84fb 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/commands/ScriptCommand.java +++ b/src/main/java/com/onarandombox/MultiverseCore/commandsold/ScriptCommand.java @@ -5,7 +5,7 @@ * with this project. * ******************************************************************************/ -package com.onarandombox.MultiverseCore.commands; +package com.onarandombox.MultiverseCore.commandsold; import com.onarandombox.MultiverseCore.MultiverseCore; import org.bukkit.ChatColor; @@ -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/commandsold/SetSpawnCommand.java similarity index 98% rename from src/main/java/com/onarandombox/MultiverseCore/commands/SetSpawnCommand.java rename to src/main/java/com/onarandombox/MultiverseCore/commandsold/SetSpawnCommand.java index 2c3c25a8..5e9fa3c6 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/commands/SetSpawnCommand.java +++ b/src/main/java/com/onarandombox/MultiverseCore/commandsold/SetSpawnCommand.java @@ -5,7 +5,7 @@ * with this project. * ******************************************************************************/ -package com.onarandombox.MultiverseCore.commands; +package com.onarandombox.MultiverseCore.commandsold; import com.onarandombox.MultiverseCore.MultiverseCore; import com.onarandombox.MultiverseCore.api.BlockSafety; @@ -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/SilentCommand.java b/src/main/java/com/onarandombox/MultiverseCore/commandsold/SilentCommand.java similarity index 97% rename from src/main/java/com/onarandombox/MultiverseCore/commands/SilentCommand.java rename to src/main/java/com/onarandombox/MultiverseCore/commandsold/SilentCommand.java index 4d0bfd73..45502296 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/commands/SilentCommand.java +++ b/src/main/java/com/onarandombox/MultiverseCore/commandsold/SilentCommand.java @@ -5,7 +5,7 @@ * with this project. * ******************************************************************************/ -package com.onarandombox.MultiverseCore.commands; +package com.onarandombox.MultiverseCore.commandsold; import com.onarandombox.MultiverseCore.MultiverseCore; import org.bukkit.ChatColor; diff --git a/src/main/java/com/onarandombox/MultiverseCore/commands/SleepCommand.java b/src/main/java/com/onarandombox/MultiverseCore/commandsold/SleepCommand.java similarity index 96% rename from src/main/java/com/onarandombox/MultiverseCore/commands/SleepCommand.java rename to src/main/java/com/onarandombox/MultiverseCore/commandsold/SleepCommand.java index 08156d20..c258b8f0 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/commands/SleepCommand.java +++ b/src/main/java/com/onarandombox/MultiverseCore/commandsold/SleepCommand.java @@ -5,7 +5,7 @@ * with this project. * ******************************************************************************/ -package com.onarandombox.MultiverseCore.commands; +package com.onarandombox.MultiverseCore.commandsold; import com.onarandombox.MultiverseCore.MultiverseCore; import org.bukkit.command.CommandSender; diff --git a/src/main/java/com/onarandombox/MultiverseCore/commands/SpawnCommand.java b/src/main/java/com/onarandombox/MultiverseCore/commandsold/SpawnCommand.java similarity index 95% rename from src/main/java/com/onarandombox/MultiverseCore/commands/SpawnCommand.java rename to src/main/java/com/onarandombox/MultiverseCore/commandsold/SpawnCommand.java index 5925d887..ae6c97dd 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/commands/SpawnCommand.java +++ b/src/main/java/com/onarandombox/MultiverseCore/commandsold/SpawnCommand.java @@ -5,10 +5,11 @@ * with this project. * ******************************************************************************/ -package com.onarandombox.MultiverseCore.commands; +package com.onarandombox.MultiverseCore.commandsold; 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/commandsold/TeleportCommand.java similarity index 81% rename from src/main/java/com/onarandombox/MultiverseCore/commands/TeleportCommand.java rename to src/main/java/com/onarandombox/MultiverseCore/commandsold/TeleportCommand.java index 847aa6d4..15571fd4 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/commands/TeleportCommand.java +++ b/src/main/java/com/onarandombox/MultiverseCore/commandsold/TeleportCommand.java @@ -5,11 +5,13 @@ * with this project. * ******************************************************************************/ -package com.onarandombox.MultiverseCore.commands; +package com.onarandombox.MultiverseCore.commandsold; +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; @@ -25,9 +28,7 @@ import org.bukkit.entity.Player; import org.bukkit.permissions.Permission; import org.bukkit.permissions.PermissionDefault; -import java.util.ArrayList; import java.util.List; -import java.util.logging.Level; /** * Used to teleport players. @@ -40,7 +41,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 +59,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 +75,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 +150,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/UnloadCommand.java b/src/main/java/com/onarandombox/MultiverseCore/commandsold/UnloadCommand.java similarity index 96% rename from src/main/java/com/onarandombox/MultiverseCore/commands/UnloadCommand.java rename to src/main/java/com/onarandombox/MultiverseCore/commandsold/UnloadCommand.java index 52f92c6c..04a35e17 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/commands/UnloadCommand.java +++ b/src/main/java/com/onarandombox/MultiverseCore/commandsold/UnloadCommand.java @@ -5,7 +5,7 @@ * with this project. * ******************************************************************************/ -package com.onarandombox.MultiverseCore.commands; +package com.onarandombox.MultiverseCore.commandsold; import com.onarandombox.MultiverseCore.MultiverseCore; import org.bukkit.ChatColor; diff --git a/src/main/java/com/onarandombox/MultiverseCore/commandsold/VersionCommand.java b/src/main/java/com/onarandombox/MultiverseCore/commandsold/VersionCommand.java new file mode 100644 index 00000000..941e4713 --- /dev/null +++ b/src/main/java/com/onarandombox/MultiverseCore/commandsold/VersionCommand.java @@ -0,0 +1,224 @@ +/****************************************************************************** + * Multiverse 2 Copyright (c) the Multiverse Team 2011. * + * 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.commandsold; + +import java.io.File; +import java.util.List; +import java.util.Map; + +import com.dumptruckman.minecraft.util.Logging; +import com.onarandombox.MultiverseCore.MultiverseCore; +import com.onarandombox.MultiverseCore.event.MVVersionEvent; +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; +import org.bukkit.command.CommandSender; +import org.bukkit.command.ConsoleCommandSender; +import org.bukkit.entity.Player; +import org.bukkit.permissions.PermissionDefault; +import org.bukkit.scheduler.BukkitRunnable; + +/** + * Dumps version info to the console. + */ +public class VersionCommand extends MultiverseCommand { + 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 + "[-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 pastebin.com with -b, to hastebin.com using -h, or to paste.gg with -p.", + PermissionDefault.OP) + ; + } + + private String getLegacyString() { + 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] 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() { + 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 void addVersionInfoToEvent(MVVersionEvent event) { + // add the legacy version info + event.appendVersionInfo(this.getLegacyString()); + + // 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 config.yml + File configFile = new File(this.plugin.getDataFolder(), "config.yml"); + event.putDetailedVersionInfo("multiverse-core/config.yml", configFile); + + // add worlds.yml + File worldsFile = new File(this.plugin.getDataFolder(), "worlds.yml"); + event.putDetailedVersionInfo("multiverse-core/worlds.yml", worldsFile); + } + + @Override + public void runCommand(final CommandSender sender, final List args) { + // Check if the command was sent from a Player. + if (sender instanceof Player) { + sender.sendMessage("Version info dumped to console. Please check your server logs."); + } + + MVVersionEvent versionEvent = new MVVersionEvent(); + + this.addVersionInfoToEvent(versionEvent); + this.plugin.getServer().getPluginManager().callEvent(versionEvent); + + if (CommandHandler.hasFlag("--include-plugin-list", args)) { + versionEvent.appendVersionInfo('\n' + "Plugins: " + getPluginList()); + versionEvent.putDetailedVersionInfo("plugins.txt", "Plugins: " + getPluginList()); + } + + final String versionInfo = versionEvent.getVersionInfo(); + versionEvent.putDetailedVersionInfo("version.txt", versionInfo); + + final Map files = versionEvent.getDetailedVersionInfo(); + + // log to console + String[] lines = versionInfo.split("\\r?\\n"); + for (String line : lines) { + if (!line.isEmpty()) { + this.plugin.getServer().getLogger().info(line); + } + } + + BukkitRunnable logPoster = new BukkitRunnable() { + @Override + public void run() { + if (args.size() > 0) { + String pasteUrl; + if (CommandHandler.hasFlag("-b", args)) { + // private post to pastebin + 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 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; + } + + if (!(sender instanceof ConsoleCommandSender)) { + sender.sendMessage("Version info dumped here: " + ChatColor.GREEN + pasteUrl); + } + Logging.info("Version info dumped here: %s", pasteUrl); + } + } + }; + + // Run the log posting operation asynchronously, since we don't know how long it will take. + logPoster.runTaskAsynchronously(this.plugin); + } + + /** + * Send the current contents of this.pasteBinBuffer to a web service. + * + * @param type Service type to send paste data to. + * @param isPrivate Should the paste be marked as private. + * @param pasteData Legacy string only data to post to a service. + * @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) { + PasteService ps = PasteServiceFactory.getService(type, isPrivate); + + try { + String result; + if (ps.supportsMultiFile()) { + result = ps.postData(pasteFiles); + } else { + result = ps.postData(pasteData); + } + + if (SHORTENER != null) return SHORTENER.shorten(result); + return result; + } catch (PasteFailedException e) { + e.printStackTrace(); + return "Error posting to service."; + } catch (NullPointerException e) { + e.printStackTrace(); + return "That service isn't supported yet."; + } + } + + private String getPluginList() { + return StringUtils.join(plugin.getServer().getPluginManager().getPlugins(), ", "); + } +} diff --git a/src/main/java/com/onarandombox/MultiverseCore/commands/WhoCommand.java b/src/main/java/com/onarandombox/MultiverseCore/commandsold/WhoCommand.java similarity index 99% rename from src/main/java/com/onarandombox/MultiverseCore/commands/WhoCommand.java rename to src/main/java/com/onarandombox/MultiverseCore/commandsold/WhoCommand.java index 1a03df28..fa9ab935 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/commands/WhoCommand.java +++ b/src/main/java/com/onarandombox/MultiverseCore/commandsold/WhoCommand.java @@ -5,7 +5,7 @@ * with this project. * ******************************************************************************/ -package com.onarandombox.MultiverseCore.commands; +package com.onarandombox.MultiverseCore.commandsold; import com.onarandombox.MultiverseCore.MultiverseCore; import com.onarandombox.MultiverseCore.api.MVWorldManager; diff --git a/src/main/java/com/onarandombox/MultiverseCore/commandsold/package-info.java b/src/main/java/com/onarandombox/MultiverseCore/commandsold/package-info.java new file mode 100644 index 00000000..a7559b95 --- /dev/null +++ b/src/main/java/com/onarandombox/MultiverseCore/commandsold/package-info.java @@ -0,0 +1,4 @@ +/** + * This package contains all Commands. + */ +package com.onarandombox.MultiverseCore.commandsold; diff --git a/src/main/java/com/onarandombox/MultiverseCore/commandtools/MVCommandCompletions.java b/src/main/java/com/onarandombox/MultiverseCore/commandtools/MVCommandCompletions.java new file mode 100644 index 00000000..7b5cbd9f --- /dev/null +++ b/src/main/java/com/onarandombox/MultiverseCore/commandtools/MVCommandCompletions.java @@ -0,0 +1,9 @@ +package com.onarandombox.MultiverseCore.commandtools; + +import co.aikar.commands.PaperCommandCompletions; + +public class MVCommandCompletions extends PaperCommandCompletions { + public MVCommandCompletions(MVCommandManager mvCommandManager) { + super(mvCommandManager); + } +} diff --git a/src/main/java/com/onarandombox/MultiverseCore/commandtools/MVCommandContexts.java b/src/main/java/com/onarandombox/MultiverseCore/commandtools/MVCommandContexts.java new file mode 100644 index 00000000..0ed2ac90 --- /dev/null +++ b/src/main/java/com/onarandombox/MultiverseCore/commandtools/MVCommandContexts.java @@ -0,0 +1,9 @@ +package com.onarandombox.MultiverseCore.commandtools; + +import co.aikar.commands.PaperCommandContexts; + +public class MVCommandContexts extends PaperCommandContexts { + public MVCommandContexts(MVCommandManager mvCommandManager) { + super(mvCommandManager); + } +} diff --git a/src/main/java/com/onarandombox/MultiverseCore/commandtools/MVCommandManager.java b/src/main/java/com/onarandombox/MultiverseCore/commandtools/MVCommandManager.java new file mode 100644 index 00000000..d84689f2 --- /dev/null +++ b/src/main/java/com/onarandombox/MultiverseCore/commandtools/MVCommandManager.java @@ -0,0 +1,54 @@ +package com.onarandombox.MultiverseCore.commandtools; + +import java.util.Locale; + +import co.aikar.commands.BukkitCommandCompletionContext; +import co.aikar.commands.BukkitCommandExecutionContext; +import co.aikar.commands.CommandCompletions; +import co.aikar.commands.CommandContexts; +import co.aikar.commands.PaperCommandManager; +import com.onarandombox.MultiverseCore.MultiverseCore; + +/** + * Main class to manage permissions. + */ +public class MVCommandManager extends PaperCommandManager { + + private final MultiverseCore plugin; + + public MVCommandManager(MultiverseCore plugin) { + super(plugin); + this.plugin = plugin; + + // Setup locale + this.addSupportedLanguage(Locale.ENGLISH); + this.locales.addMessageBundles("multiverse-core"); + this.locales.loadLanguages(); + } + + /** + * Gets class responsible for parsing string args into objects. + * + * @return A not-null {@link CommandContexts}. + */ + @Override + public synchronized CommandContexts getCommandContexts() { + if (this.contexts == null) { + this.contexts = new MVCommandContexts(this); + } + return this.contexts; + } + + /** + * Gets class responsible for tab-completion handling. + * + * @return A not-null {@link CommandCompletions}. + */ + @Override + public synchronized CommandCompletions getCommandCompletions() { + if (this.completions == null) { + this.completions = new MVCommandCompletions(this); + } + return this.completions; + } +} 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..181e8e50 --- /dev/null +++ b/src/main/java/com/onarandombox/MultiverseCore/commandtools/queue/DummyCommandBlockSender.java @@ -0,0 +1,121 @@ +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; +import java.util.UUID; + +/** + * 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 void sendMessage(@Nullable UUID uuid, @NotNull String s) { + throw new UnsupportedOperationException(); + } + + @Override + public void sendMessage(@Nullable UUID uuid, @NotNull String[] strings) { + throw new UnsupportedOperationException(); + } + + @Override + public @NotNull Server getServer() { + return Bukkit.getServer(); + } + + @Override + public @NotNull String getName() { + return "DummyCommandBlockSender"; + } + + @NotNull + @Override + public Spigot spigot() { + throw new UnsupportedOperationException(); + } + + @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..7f00248c 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/destination/DestinationFactory.java +++ b/src/main/java/com/onarandombox/MultiverseCore/destination/DestinationFactory.java @@ -9,18 +9,29 @@ package com.onarandombox.MultiverseCore.destination; import com.onarandombox.MultiverseCore.MultiverseCore; import com.onarandombox.MultiverseCore.api.MVDestination; -import com.onarandombox.MultiverseCore.commands.TeleportCommand; +import com.onarandombox.MultiverseCore.commandsold.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.entity.Player; import org.bukkit.permissions.Permission; import org.bukkit.permissions.PermissionDefault; +import org.jetbrains.annotations.NotNull; +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 +47,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 +165,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/ContentDisplay.java b/src/main/java/com/onarandombox/MultiverseCore/display/ContentDisplay.java new file mode 100644 index 00000000..b2303c54 --- /dev/null +++ b/src/main/java/com/onarandombox/MultiverseCore/display/ContentDisplay.java @@ -0,0 +1,69 @@ +package com.onarandombox.MultiverseCore.display; + +import com.onarandombox.MultiverseCore.display.handlers.DefaultSendHandler; +import com.onarandombox.MultiverseCore.display.handlers.SendHandler; +import com.onarandombox.MultiverseCore.display.parsers.ContentParser; +import org.bukkit.command.CommandSender; +import org.jetbrains.annotations.NotNull; + +import java.util.ArrayList; +import java.util.List; +import java.util.Objects; + +/** + * Helps to display contents such as list and maps in a nicely formatted fashion. + */ +public class ContentDisplay { + + /** + * Makes a new {@link ContentDisplay} instance to use. + * + * @return New {@link ContentDisplay} instance. + */ + @NotNull + public static ContentDisplay create() { + return new ContentDisplay(); + } + + private final List contentParsers = new ArrayList<>(); + private SendHandler sendHandler = DefaultSendHandler.getInstance(); + + public ContentDisplay() { + } + + /** + * Adds content to be displayed. + * + * @param parser The content parser to add. + * @return Same {@link ContentDisplay} for method chaining. + */ + @NotNull + public ContentDisplay addContentParser(@NotNull ContentParser parser) { + contentParsers.add(parser); + return this; + } + + /** + * Sets the handler for displaying the message to command sender. + * + * @param handler The send handler to use. + * @return Same {@link ContentDisplay} for method chaining. + */ + @NotNull + public ContentDisplay withSendHandler(@NotNull SendHandler handler) { + sendHandler = handler; + return this; + } + + /** + * Format and display the message to command sender. + * + * @param sender The target command sender to show the display to. + */ + public void send(@NotNull CommandSender sender) { + Objects.requireNonNull(sendHandler, "No send handler set for content display"); + List parsedContent = new ArrayList<>(); + contentParsers.forEach(parser -> parser.parse(sender, parsedContent)); + sendHandler.send(sender, parsedContent); + } +} diff --git a/src/main/java/com/onarandombox/MultiverseCore/display/filters/ContentFilter.java b/src/main/java/com/onarandombox/MultiverseCore/display/filters/ContentFilter.java new file mode 100644 index 00000000..240d7d92 --- /dev/null +++ b/src/main/java/com/onarandombox/MultiverseCore/display/filters/ContentFilter.java @@ -0,0 +1,21 @@ +package com.onarandombox.MultiverseCore.display.filters; + +/** + * Filter display content for it show only certain string. + */ +public interface ContentFilter { + /** + * Checks if a particular string should be displayed. + * + * @param value String to check on. + * @return True if should be display, false otherwise. + */ + boolean checkMatch(String value); + + /** + * Gets whether content needs to be filtered by this filter. + * + * @return True if content should be filtered, false otherwise. + */ + boolean needToFilter(); +} diff --git a/src/main/java/com/onarandombox/MultiverseCore/display/filters/DefaultContentFilter.java b/src/main/java/com/onarandombox/MultiverseCore/display/filters/DefaultContentFilter.java new file mode 100644 index 00000000..5070d647 --- /dev/null +++ b/src/main/java/com/onarandombox/MultiverseCore/display/filters/DefaultContentFilter.java @@ -0,0 +1,35 @@ +package com.onarandombox.MultiverseCore.display.filters; + +/** + * Default implementation of {@link ContentFilter} that doesn't filter anything. + */ +public class DefaultContentFilter implements ContentFilter { + + public static DefaultContentFilter instance; + + public static DefaultContentFilter getInstance() { + if (instance == null) { + instance = new DefaultContentFilter(); + } + return instance; + } + + private DefaultContentFilter() { + } + + /** + * {@inheritDoc} + */ + @Override + public boolean checkMatch(String value) { + return true; + } + + /** + * {@inheritDoc} + */ + @Override + public boolean needToFilter() { + return false; + } +} diff --git a/src/main/java/com/onarandombox/MultiverseCore/display/filters/RegexContentFilter.java b/src/main/java/com/onarandombox/MultiverseCore/display/filters/RegexContentFilter.java new file mode 100644 index 00000000..e83611e9 --- /dev/null +++ b/src/main/java/com/onarandombox/MultiverseCore/display/filters/RegexContentFilter.java @@ -0,0 +1,99 @@ +package com.onarandombox.MultiverseCore.display.filters; + +import com.dumptruckman.minecraft.util.Logging; +import com.google.common.base.Strings; +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. + */ +public class RegexContentFilter implements ContentFilter { + + private static final Pattern REGEX_SPECIAL_CHARS = Pattern.compile("[.+*?\\[^\\]$(){}=!<>|:-\\\\]"); + + /** + * Compile regex pattern to create a regex filter. + * + * When prefixed with 'r=', filter string is used as the full regex pattern. + * Else, set to regex that contains the filterString. + * + * @param filterString The target string to create filter. + * @return A new instance of {@link RegexContentFilter} with filter applied. + */ + @NotNull + public static RegexContentFilter fromString(@Nullable String filterString) { + if (filterString == null) { + return new RegexContentFilter(null); + } + if (filterString.startsWith("r=")) { + return new RegexContentFilter(filterString.substring(2)); + } + String cleanedFilter = REGEX_SPECIAL_CHARS.matcher(filterString.toLowerCase()).replaceAll("\\\\$0"); + return new RegexContentFilter("(?i).*" + cleanedFilter + ".*"); + } + + private final String regexString; + private Pattern regexPattern; + + public RegexContentFilter(@Nullable String regexString) { + this.regexString = regexString; + convertToPattern(); + } + + /** + * Try to compile and store the regex into a {@link Pattern}. + */ + private void convertToPattern() { + if (Strings.isNullOrEmpty(regexString)) { + return; + } + try { + regexPattern = Pattern.compile(regexString); + } catch (PatternSyntaxException ignored) { + regexPattern = null; + Logging.fine("Error parsing regex: %s", regexString); + } + } + + /** + * {@inheritDoc} + */ + @Override + public boolean checkMatch(String value) { + if (!hasValidRegex()) { + return false; + } + String text = ChatColor.stripColor(String.valueOf(value)); + return regexPattern.matcher(text).find(); + } + + /** + * {@inheritDoc} + */ + @Override + public boolean needToFilter() { + return hasValidRegex(); + } + + public boolean hasValidRegex() { + return regexPattern != null; + } + + public String getRegexString() { + return regexString; + } + + public Pattern getRegexPattern() { + return regexPattern; + } + + @Override + public String toString() { + return regexString; + } +} diff --git a/src/main/java/com/onarandombox/MultiverseCore/display/handlers/BaseSendHandler.java b/src/main/java/com/onarandombox/MultiverseCore/display/handlers/BaseSendHandler.java new file mode 100644 index 00000000..46e11b29 --- /dev/null +++ b/src/main/java/com/onarandombox/MultiverseCore/display/handlers/BaseSendHandler.java @@ -0,0 +1,99 @@ +package com.onarandombox.MultiverseCore.display.handlers; + +import com.google.common.base.Strings; +import com.onarandombox.MultiverseCore.display.filters.ContentFilter; +import com.onarandombox.MultiverseCore.display.filters.DefaultContentFilter; +import org.bukkit.ChatColor; +import org.bukkit.command.CommandSender; +import org.jetbrains.annotations.NotNull; + +import java.util.List; +import java.util.stream.Collectors; + +/** + * Base implementation of {@link SendHandler} with some common parameters. + * + * @param The subclass that inherited this baseclass. + */ +public abstract class BaseSendHandler> implements SendHandler { + + protected String header = ""; + protected ContentFilter filter = DefaultContentFilter.getInstance(); + + /** + * {@inheritDoc} + */ + @Override + public void send(@NotNull CommandSender sender, @NotNull List content) { + sendHeader(sender); + List filteredContent = filterContent(content); + if (filteredContent.isEmpty()) { + sender.sendMessage(String.format("%sThere is no content to display.", ChatColor.RED)); + return; + } + sendContent(sender, filteredContent); + } + + /** + * Sends the header if header is present. + * + * @param sender The target which the header will be displayed to. + */ + protected void sendHeader(CommandSender sender) { + if (!Strings.isNullOrEmpty(header)) { + sender.sendMessage(header); + } + } + + /** + * Filter to keep only contents that matches the filter. + * + * @param content The content to filter on. + * @return The filtered list of content. + */ + protected List filterContent(@NotNull List content) { + if (filter.needToFilter()) { + return content.stream().filter(filter::checkMatch).collect(Collectors.toList()); + } + return content; + } + + /** + * Display the contents. + * + * @param sender The target which the content will be displayed to. + * @param content The content to display. + */ + protected abstract void sendContent(@NotNull CommandSender sender, @NotNull List content); + + /** + * Sets header to be displayed. + * + * @param header The header text. + * @param replacements String formatting replacements. + * @return Same {@link T} for method chaining. + */ + public T withHeader(@NotNull String header, @NotNull Object...replacements) { + this.header = String.format(header, replacements); + return (T) this; + } + + /** + * Sets content filter used to match specific content to be displayed. + * + * @param filter The filter to use. + * @return Same {@link T} for method chaining. + */ + public T withFilter(@NotNull ContentFilter filter) { + this.filter = filter; + return (T) this; + } + + public String getHeader() { + return header; + } + + public ContentFilter getFilter() { + return filter; + } +} diff --git a/src/main/java/com/onarandombox/MultiverseCore/display/handlers/DefaultSendHandler.java b/src/main/java/com/onarandombox/MultiverseCore/display/handlers/DefaultSendHandler.java new file mode 100644 index 00000000..e0299785 --- /dev/null +++ b/src/main/java/com/onarandombox/MultiverseCore/display/handlers/DefaultSendHandler.java @@ -0,0 +1,29 @@ +package com.onarandombox.MultiverseCore.display.handlers; + +import org.bukkit.command.CommandSender; +import org.jetbrains.annotations.NotNull; + +import java.util.List; + +public class DefaultSendHandler implements SendHandler { + + private static DefaultSendHandler instance; + + public static DefaultSendHandler getInstance() { + if (instance == null) { + instance = new DefaultSendHandler(); + } + return instance; + } + + private DefaultSendHandler() { + } + + /** + * {@inheritDoc} + */ + @Override + public void send(@NotNull CommandSender sender, @NotNull List content) { + sender.sendMessage(content.toArray(new String[0])); + } +} diff --git a/src/main/java/com/onarandombox/MultiverseCore/display/handlers/InlineSendHandler.java b/src/main/java/com/onarandombox/MultiverseCore/display/handlers/InlineSendHandler.java new file mode 100644 index 00000000..5d8a1d10 --- /dev/null +++ b/src/main/java/com/onarandombox/MultiverseCore/display/handlers/InlineSendHandler.java @@ -0,0 +1,53 @@ +package com.onarandombox.MultiverseCore.display.handlers; + +import org.bukkit.ChatColor; +import org.bukkit.command.CommandSender; +import org.jetbrains.annotations.NotNull; + +import java.util.List; + +/** + * Display the contents in a single line. + */ +public class InlineSendHandler extends BaseSendHandler { + + /** + * Makes a new {@link InlineSendHandler} instance to use. + * + * @return New {@link InlineSendHandler} instance. + */ + public static InlineSendHandler create() { + return new InlineSendHandler(); + } + + private String delimiter = ChatColor.WHITE + ", "; + + public InlineSendHandler() { + } + + /** + * {@inheritDoc} + */ + @Override + public void sendContent(@NotNull CommandSender sender, @NotNull List content) { + if (filter.needToFilter()) { + sender.sendMessage(String.format("%s[Filter '%s']", ChatColor.GRAY, filter)); + } + sender.sendMessage(String.join(delimiter, content)); + } + + /** + * Sets the delimiter. A sequence of characters that is used to separate each of the elements in content. + * + * @param delimiter The delimiter to use. + * @return Same {@link InlineSendHandler} for method chaining. + */ + public InlineSendHandler withDelimiter(String delimiter) { + this.delimiter = delimiter; + return this; + } + + public String getDelimiter() { + return delimiter; + } +} diff --git a/src/main/java/com/onarandombox/MultiverseCore/display/handlers/PagedSendHandler.java b/src/main/java/com/onarandombox/MultiverseCore/display/handlers/PagedSendHandler.java new file mode 100644 index 00000000..62208b5c --- /dev/null +++ b/src/main/java/com/onarandombox/MultiverseCore/display/handlers/PagedSendHandler.java @@ -0,0 +1,170 @@ +package com.onarandombox.MultiverseCore.display.handlers; + +import org.bukkit.ChatColor; +import org.bukkit.command.CommandSender; +import org.bukkit.command.ConsoleCommandSender; +import org.jetbrains.annotations.NotNull; + +import java.util.List; + +/** + * Display content as a list with optional pagination. + */ +public class PagedSendHandler extends BaseSendHandler { + + /** + * Makes a new {@link PagedSendHandler} instance to use. + * + * @return New {@link PagedSendHandler} instance. + */ + public static PagedSendHandler create() { + return new PagedSendHandler(); + } + + private boolean paginate = true; + private boolean paginateInConsole = false; + private boolean padEnd = true; + private int linesPerPage = 8; + private int targetPage = 1; + + public PagedSendHandler() { + } + + /** + * {@inheritDoc} + */ + @Override + public void sendContent(@NotNull CommandSender sender, + @NotNull List content) { + + if (!paginate || (sender instanceof ConsoleCommandSender && !paginateInConsole)) { + sendNormal(sender, content); + return; + } + sendPaged(sender, content); + } + + /** + * Send content list without pagination. + * + * @param sender The target which the content will be displayed to. + * @param content The content to display. + */ + private void sendNormal(@NotNull CommandSender sender, + @NotNull List content) { + + if (filter.needToFilter()) { + sender.sendMessage(String.format("%s[Filter '%s']", ChatColor.GRAY, filter)); + } + sender.sendMessage(content.toArray(new String[0])); + } + + /** + * Send content list with pagination. + * + * @param sender The target which the content will be displayed to. + * @param content The content to display. + */ + private void sendPaged(@NotNull CommandSender sender, + @NotNull List content) { + + int totalPages = (content.size() + linesPerPage - 1) / linesPerPage; // Basically just divide round up + if (targetPage < 1 || targetPage > totalPages) { + sender.sendMessage(String.format("%sInvalid page number. Please enter a page number between 1 and %s", ChatColor.RED, totalPages)); + return; + } + + if (filter.needToFilter()) { + sender.sendMessage(String.format("%s[Page %s of %s] [Filter '%s']", ChatColor.GRAY, targetPage, totalPages, filter)); + } else { + sender.sendMessage(String.format("%s[Page %s of %s]", ChatColor.GRAY, targetPage, totalPages)); + } + + int startIndex = (targetPage - 1) * linesPerPage; + int pageEndIndex = startIndex + linesPerPage; + int endIndex = Math.min(pageEndIndex, content.size()); + List pageContent = content.subList(startIndex, endIndex); + if (padEnd) { + for (int i = 0; i < (pageEndIndex - endIndex); i++) { + pageContent.add(""); + } + } + sender.sendMessage(pageContent.toArray(new String[0])); + } + + /** + * Sets whether display output should be paginated. + * + * @param paginate State of doing pagination. + * @return Same {@link PagedSendHandler} for method chaining. + */ + public PagedSendHandler doPagination(boolean paginate) { + this.paginate = paginate; + return this; + } + + /** + * Sets whether display output should be paginated if is for console output. + * This option will be useless of {@link PagedSendHandler#paginate} is set to false. + * + * @param paginateInConsole State of doing pagination in console. + * @return Same {@link PagedSendHandler} for method chaining. + */ + public PagedSendHandler doPaginationInConsole(boolean paginateInConsole) { + this.paginateInConsole = paginateInConsole; + return this; + } + + /** + * Sets whether empty lines should be added if content lines shown is less that {@link PagedSendHandler#linesPerPage}. + * + * @param padEnd State of doing end padding. + * @return Same {@link PagedSendHandler} for method chaining. + */ + public PagedSendHandler doEndPadding(boolean padEnd) { + this.padEnd = padEnd; + return this; + } + + /** + * Sets the max number of lines per page. This excludes header. + * + * @param linesPerPage The number of lines per page. + * @return Same {@link PagedSendHandler} for method chaining. + */ + public PagedSendHandler withLinesPerPage(int linesPerPage) { + this.linesPerPage = linesPerPage; + return this; + } + + /** + * Sets the page number to display. + * + * @param targetPage The target page number to display. + * @return Same {@link PagedSendHandler} for method chaining. + */ + public PagedSendHandler withTargetPage(int targetPage) { + this.targetPage = targetPage; + return this; + } + + public boolean isPaginate() { + return paginate; + } + + public boolean isPaginateInConsole() { + return paginateInConsole; + } + + public boolean isPadEnd() { + return padEnd; + } + + public int getLinesPerPage() { + return linesPerPage; + } + + public int getTargetPage() { + return targetPage; + } +} diff --git a/src/main/java/com/onarandombox/MultiverseCore/display/handlers/SendHandler.java b/src/main/java/com/onarandombox/MultiverseCore/display/handlers/SendHandler.java new file mode 100644 index 00000000..6c826aa6 --- /dev/null +++ b/src/main/java/com/onarandombox/MultiverseCore/display/handlers/SendHandler.java @@ -0,0 +1,20 @@ +package com.onarandombox.MultiverseCore.display.handlers; + +import org.bukkit.command.CommandSender; +import org.jetbrains.annotations.NotNull; + +import java.util.List; + +/** + * Handles the sending of all content to the command sender. + */ +@FunctionalInterface +public interface SendHandler { + /** + * Sends all the content to the given command sender. + * + * @param sender The target which the content will be displayed to. + * @param content The content to display. + */ + void send(@NotNull CommandSender sender, @NotNull List content); +} diff --git a/src/main/java/com/onarandombox/MultiverseCore/display/parsers/ContentParser.java b/src/main/java/com/onarandombox/MultiverseCore/display/parsers/ContentParser.java new file mode 100644 index 00000000..b69f7a84 --- /dev/null +++ b/src/main/java/com/onarandombox/MultiverseCore/display/parsers/ContentParser.java @@ -0,0 +1,20 @@ +package com.onarandombox.MultiverseCore.display.parsers; + +import org.bukkit.command.CommandSender; +import org.jetbrains.annotations.NotNull; + +import java.util.List; + +/** + * Parse objects into string or list of strings. + */ +@FunctionalInterface +public interface ContentParser { + /** + * Parse the object to string(s) and add it to the content. + * + * @param sender The target which the content will be displayed to. + * @param content The content to display. + */ + void parse(@NotNull CommandSender sender, @NotNull List content); +} diff --git a/src/main/java/com/onarandombox/MultiverseCore/display/parsers/ListContentParser.java b/src/main/java/com/onarandombox/MultiverseCore/display/parsers/ListContentParser.java new file mode 100644 index 00000000..00a89061 --- /dev/null +++ b/src/main/java/com/onarandombox/MultiverseCore/display/parsers/ListContentParser.java @@ -0,0 +1,60 @@ +package com.onarandombox.MultiverseCore.display.parsers; + +import org.bukkit.command.CommandSender; +import org.jetbrains.annotations.NotNull; + +import java.util.List; + +/** + * Simple parser for list object. + * + * @param List element type. + */ +public class ListContentParser implements ContentParser { + + /** + * New list content parser for the given list. + * + * @param list The list object to parse. + * @param List element type. + * @return New {@link MapContentParser} instance. + */ + public static ListContentParser forContent(List list) { + return new ListContentParser<>(list); + } + + private final List list; + + private String format = "%s"; + + public ListContentParser(List list) { + this.list = list; + } + + /** + * {@inheritDoc} + */ + @Override + public void parse(@NotNull CommandSender sender, @NotNull List content) { + list.forEach(element -> content.add(String.format(format, element))); + } + + /** + * Sets the format that will be used to parse each list entry. Uses java string format pattern. + * + * @param format The format to use. + * @return Same {@link ListContentParser} for method chaining. + */ + public ListContentParser withFormat(String format) { + this.format = format; + return this; + } + + public List getList() { + return list; + } + + public String getFormat() { + return format; + } +} diff --git a/src/main/java/com/onarandombox/MultiverseCore/display/parsers/MapContentParser.java b/src/main/java/com/onarandombox/MultiverseCore/display/parsers/MapContentParser.java new file mode 100644 index 00000000..4aad97bc --- /dev/null +++ b/src/main/java/com/onarandombox/MultiverseCore/display/parsers/MapContentParser.java @@ -0,0 +1,112 @@ +package com.onarandombox.MultiverseCore.display.parsers; + +import org.bukkit.ChatColor; +import org.bukkit.command.CommandSender; +import org.jetbrains.annotations.NotNull; + +import java.util.List; +import java.util.Map; + +/** + * Simple parser for map object. + * + * @param Key type. + * @param Value type. + */ +public class MapContentParser implements ContentParser { + + /** + * New map content parser for the given map. + * + * @param map The map object to parse. + * @param Key type. + * @param Value type. + * @return New {@link MapContentParser} instance. + */ + public static MapContentParser forContent(Map map) { + return new MapContentParser<>(map); + } + + private final Map map; + + private String format = "%s%s%s%s%s"; + private ChatColor keyColor = ChatColor.WHITE; + private ChatColor valueColor = ChatColor.WHITE; + private String separator = ": "; + + public MapContentParser(Map map) { + this.map = map; + } + + /** + * {@inheritDoc} + */ + @Override + public void parse(@NotNull CommandSender sender, @NotNull List content) { + map.forEach((k, v) -> content.add(String.format(format, keyColor, k, separator, valueColor, v))); + } + + /** + * Sets the format that will be used to parse each map entry. Uses java string format pattern. + * + * @param format The format to use. + * @return Same {@link MapContentParser} for method chaining. + */ + public MapContentParser withFormat(String format) { + this.format = format; + return this; + } + + /** + * Sets the color for the key text. + * + * @param keyColor The color to use. + * @return Same {@link MapContentParser} for method chaining. + */ + public MapContentParser withKeyColor(ChatColor keyColor) { + this.keyColor = keyColor; + return this; + } + + /** + * Sets the color for the value text. + * + * @param valueColor The color to use. + * @return Same {@link MapContentParser} for method chaining. + */ + public MapContentParser withValueColor(ChatColor valueColor) { + this.valueColor = valueColor; + return this; + } + + /** + * Sets the separator between each key value pairing. + * + * @param separator The separator to use. + * @return Same {@link MapContentParser} for method chaining. + */ + public MapContentParser withSeparator(String separator) { + this.separator = separator; + return this; + } + + public Map getMap() { + return map; + } + + public String getFormat() { + return format; + } + + public ChatColor getKeyColor() { + return keyColor; + } + + public ChatColor getValueColor() { + return valueColor; + } + + public String getSeparator() { + return separator; + } +} 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/AsyncChatEvent.java b/src/main/java/com/onarandombox/MultiverseCore/listeners/AsyncChatEvent.java deleted file mode 100644 index 38d48bef..00000000 --- a/src/main/java/com/onarandombox/MultiverseCore/listeners/AsyncChatEvent.java +++ /dev/null @@ -1,35 +0,0 @@ -package com.onarandombox.MultiverseCore.listeners; - -import org.bukkit.entity.Player; -import org.bukkit.event.player.AsyncPlayerChatEvent; - -/** - * A wrapper for the {@link AsyncPlayerChatEvent}. - */ -public class AsyncChatEvent implements ChatEvent { - private final AsyncPlayerChatEvent event; - - public AsyncChatEvent(AsyncPlayerChatEvent event) { - this.event = event; - } - - @Override - public boolean isCancelled() { - return event.isCancelled(); - } - - @Override - public String getFormat() { - return event.getFormat(); - } - - @Override - public void setFormat(String s) { - event.setFormat(s); - } - - @Override - public Player getPlayer() { - return event.getPlayer(); - } -} diff --git a/src/main/java/com/onarandombox/MultiverseCore/listeners/ChatEvent.java b/src/main/java/com/onarandombox/MultiverseCore/listeners/ChatEvent.java deleted file mode 100644 index 16cafb81..00000000 --- a/src/main/java/com/onarandombox/MultiverseCore/listeners/ChatEvent.java +++ /dev/null @@ -1,29 +0,0 @@ -package com.onarandombox.MultiverseCore.listeners; - -import org.bukkit.entity.Player; - -/** - * A wrapper for the two chat-events in Bukkit. - */ -public interface ChatEvent { - /** - * @return Whether this event is cancelled. - */ - boolean isCancelled(); - - /** - * @return The format. - */ - String getFormat(); - - /** - * Sets the format. - * @param s The new format. - */ - void setFormat(String s); - - /** - * @return The player. - */ - Player getPlayer(); -} diff --git a/src/main/java/com/onarandombox/MultiverseCore/listeners/MVAsyncPlayerChatListener.java b/src/main/java/com/onarandombox/MultiverseCore/listeners/MVAsyncPlayerChatListener.java deleted file mode 100644 index eae4df20..00000000 --- a/src/main/java/com/onarandombox/MultiverseCore/listeners/MVAsyncPlayerChatListener.java +++ /dev/null @@ -1,34 +0,0 @@ -/****************************************************************************** - * Multiverse 2 Copyright (c) the Multiverse Team 2011. * - * 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.listeners; - -import java.util.logging.Level; - -import org.bukkit.event.EventHandler; -import org.bukkit.event.player.AsyncPlayerChatEvent; - -import com.onarandombox.MultiverseCore.MultiverseCore; - -/** - * Multiverse's {@link org.bukkit.event.Listener} for players. - */ -public class MVAsyncPlayerChatListener extends MVChatListener { - public MVAsyncPlayerChatListener(MultiverseCore plugin, MVPlayerListener playerListener) { - super(plugin, playerListener); - plugin.log(Level.FINE, "Created AsyncPlayerChatEvent listener."); - } - - /** - * This method is called when a player wants to chat. - * @param event The Event that was fired. - */ - @EventHandler - public void playerChat(AsyncPlayerChatEvent event) { - this.playerChat(new AsyncChatEvent(event)); - } -} diff --git a/src/main/java/com/onarandombox/MultiverseCore/listeners/MVChatListener.java b/src/main/java/com/onarandombox/MultiverseCore/listeners/MVChatListener.java index aaa05ab5..84fcb11f 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/listeners/MVChatListener.java +++ b/src/main/java/com/onarandombox/MultiverseCore/listeners/MVChatListener.java @@ -5,12 +5,14 @@ import com.onarandombox.MultiverseCore.api.MVWorldManager; import com.onarandombox.MultiverseCore.api.MultiverseWorld; import org.bukkit.ChatColor; +import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; +import org.bukkit.event.player.AsyncPlayerChatEvent; /** * Multiverse's {@link org.bukkit.event.Listener} for players. */ -public abstract class MVChatListener implements Listener { +public class MVChatListener implements Listener { private final MultiverseCore plugin; private final MVWorldManager worldManager; private final MVPlayerListener playerListener; @@ -22,10 +24,11 @@ public abstract class MVChatListener implements Listener { } /** - * This handles a {@link ChatEvent}. - * @param event The {@link ChatEvent}. + * This handles a {@link AsyncPlayerChatEvent}. + * @param event The {@link AsyncPlayerChatEvent}. */ - public void playerChat(ChatEvent event) { + @EventHandler + public void playerChat(AsyncPlayerChatEvent event) { if (event.isCancelled()) { return; } diff --git a/src/main/java/com/onarandombox/MultiverseCore/listeners/MVEntityListener.java b/src/main/java/com/onarandombox/MultiverseCore/listeners/MVEntityListener.java index 4aa34ccd..509f7bbd 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/listeners/MVEntityListener.java +++ b/src/main/java/com/onarandombox/MultiverseCore/listeners/MVEntityListener.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.MVWorldManager; import com.onarandombox.MultiverseCore.api.MultiverseWorld; @@ -17,12 +18,11 @@ 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; -import java.util.logging.Level; - /** * Multiverse's Entity {@link Listener}. */ @@ -98,7 +98,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 +106,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()) { + event.setSearchRadius(this.plugin.getMVConfig().getPortalSearchRadius()); + } + } } diff --git a/src/main/java/com/onarandombox/MultiverseCore/listeners/MVMapListener.java b/src/main/java/com/onarandombox/MultiverseCore/listeners/MVMapListener.java deleted file mode 100644 index 2443337a..00000000 --- a/src/main/java/com/onarandombox/MultiverseCore/listeners/MVMapListener.java +++ /dev/null @@ -1,43 +0,0 @@ -package com.onarandombox.MultiverseCore.listeners; - -import com.onarandombox.MultiverseCore.MultiverseCore; -import org.bukkit.Bukkit; -import org.bukkit.Location; -import org.bukkit.Material; -import org.bukkit.entity.Player; -import org.bukkit.event.EventHandler; -import org.bukkit.event.Listener; -import org.bukkit.event.server.MapInitializeEvent; -import org.bukkit.map.MapView; - -/** - * A listener for bukkit map events. - */ -public class MVMapListener implements Listener { - - private final MultiverseCore plugin; - - public MVMapListener(final MultiverseCore plugin) { - this.plugin = plugin; - } - - /** - * This method is called when a map is initialized. - * @param event The event that was fired. - */ - @EventHandler - public void mapInitialize(final MapInitializeEvent event) { - for (final Player player : Bukkit.getOnlinePlayers()) { - if ((player.getItemInHand().getType() == Material.MAP - || player.getItemInHand().getType() == Material.FILLED_MAP) - && player.getItemInHand().getDurability() == event.getMap().getId()) { - final Location playerLoc = player.getLocation(); - final MapView map = event.getMap(); - map.setCenterX(playerLoc.getBlockX()); - map.setCenterZ(playerLoc.getBlockZ()); - map.setWorld(playerLoc.getWorld()); - return; - } - } - } -} diff --git a/src/main/java/com/onarandombox/MultiverseCore/listeners/MVPlayerChatListener.java b/src/main/java/com/onarandombox/MultiverseCore/listeners/MVPlayerChatListener.java deleted file mode 100644 index fc110522..00000000 --- a/src/main/java/com/onarandombox/MultiverseCore/listeners/MVPlayerChatListener.java +++ /dev/null @@ -1,35 +0,0 @@ -/****************************************************************************** - * Multiverse 2 Copyright (c) the Multiverse Team 2011. * - * 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.listeners; - -import java.util.logging.Level; - -import org.bukkit.event.EventHandler; -import org.bukkit.event.player.PlayerChatEvent; - -import com.onarandombox.MultiverseCore.MultiverseCore; - -/** - * Multiverse's {@link org.bukkit.event.Listener} for players. - */ -@SuppressWarnings("deprecation") // this exists only for downwards compatibility -public class MVPlayerChatListener extends MVChatListener { - public MVPlayerChatListener(MultiverseCore plugin, MVPlayerListener playerListener) { - super(plugin, playerListener); - plugin.log(Level.FINE, "Registered PlayerChatEvent listener."); - } - - /** - * This method is called when a player wants to chat. - * @param event The Event that was fired. - */ - @EventHandler - public void playerChat(PlayerChatEvent event) { - this.playerChat(new NormalChatEvent(event)); - } -} diff --git a/src/main/java/com/onarandombox/MultiverseCore/listeners/MVPlayerListener.java b/src/main/java/com/onarandombox/MultiverseCore/listeners/MVPlayerListener.java index 40c1f51b..72600132 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/listeners/MVPlayerListener.java +++ b/src/main/java/com/onarandombox/MultiverseCore/listeners/MVPlayerListener.java @@ -9,7 +9,6 @@ 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; @@ -69,9 +68,8 @@ public class MVPlayerListener implements Listener { return; } - - if (mvWorld.getBedRespawn() && event.isBedSpawn()) { - this.plugin.log(Level.FINE, "Spawning " + event.getPlayer().getName() + " at their bed"); + if (mvWorld.getBedRespawn() && (event.isBedSpawn() || event.isAnchorSpawn())) { + Logging.fine("Spawning %s at their %s.", event.getPlayer().getName(), event.isBedSpawn() ? "BED" : "ANCHOR"); return; } @@ -110,15 +108,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 +154,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 +164,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 +190,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 +201,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 +218,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 +233,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 +280,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 +293,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()) { + event.setSearchRadius(this.plugin.getMVConfig().getPortalSearchRadius()); } } @@ -335,7 +325,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 +366,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..dee9f7b5 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/listeners/MVPortalListener.java +++ b/src/main/java/com/onarandombox/MultiverseCore/listeners/MVPortalListener.java @@ -7,19 +7,18 @@ 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; import org.bukkit.PortalType; +import org.bukkit.block.BlockState; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; import org.bukkit.event.block.Action; -import org.bukkit.event.entity.EntityCreatePortalEvent; import org.bukkit.event.player.PlayerInteractEvent; import org.bukkit.event.world.PortalCreateEvent; -import java.util.logging.Level; - /** * A custom listener for portal related events. */ @@ -32,31 +31,48 @@ public class MVPortalListener implements Listener { } /** - * This is called when an entity creates a portal. - * - * @param event The event where an entity created a portal. - */ - @EventHandler - public void entityPortalCreate(EntityCreatePortalEvent event) { - if (event.isCancelled() || event.getBlocks().size() == 0) { - return; - } - MultiverseWorld world = this.plugin.getMVWorldManager().getMVWorld(event.getEntity().getWorld()); - // We have to do it like this due to a bug in 1.1-R3 - if (world != null && !world.getAllowedPortals().isPortalAllowed(event.getPortalType())) { - event.setCancelled(true); - } - } - - /** - * This is called when a portal is created as the result of another world being linked. - * @param event The event where a portal was formed due to a world link + * This is called when a portal is formed. + * @param event The event where a portal was created or formed due to a world link */ @EventHandler(ignoreCancelled = true) public void portalForm(PortalCreateEvent event) { + Logging.fine("Attempting to create portal at '%s' with reason: %s", event.getWorld().getName(), event.getReason()); + 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."); + if (world == null) { + Logging.fine("World '%s' is not managed by Multiverse! Ignoring at PortalCreateEvent.", event.getWorld().getName()); + return; + } + + PortalType targetType; + switch (event.getReason()) { + case FIRE: + // Ensure portal by flint and steel actually creates nether + boolean isNether = false; + for (BlockState block : event.getBlocks()) { + if (block.getType() == Material.NETHER_PORTAL) { + isNether = true; + break; + } + } + if (!isNether) { + return; + } + targetType = PortalType.NETHER; + break; + case NETHER_PAIR: + targetType = PortalType.NETHER; + break; + case END_PLATFORM: + targetType = PortalType.ENDER; + break; + default: + Logging.fine("Portal created is not NETHER or ENDER type. Ignoring..."); + return; + } + + if (!world.getAllowedPortals().isPortalAllowed(targetType)) { + Logging.fine("Cancelling creation of %s portal because portalForm disallows.", targetType); event.setCancelled(true); } } @@ -71,15 +87,22 @@ public class MVPortalListener implements Listener { if (event.getAction() != Action.RIGHT_CLICK_BLOCK) { return; } - if (event.getClickedBlock().getType() != Material.END_PORTAL_FRAME) { + if (event.getClickedBlock() == null || event.getClickedBlock().getType() != Material.END_PORTAL_FRAME) { return; } if (event.getItem() == null || event.getItem().getType() != Material.ENDER_EYE) { return; } + 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."); + if (world == null) { + Logging.fine("World '%s' is not managed by Multiverse! Ignoring at PlayerInteractEvent.", + event.getPlayer().getWorld().getName()); + return; + } + + if (!world.getAllowedPortals().isPortalAllowed(PortalType.ENDER)) { + Logging.fine("Cancelling creation of ENDER portal because portalForm disallows."); event.setCancelled(true); } } diff --git a/src/main/java/com/onarandombox/MultiverseCore/listeners/NormalChatEvent.java b/src/main/java/com/onarandombox/MultiverseCore/listeners/NormalChatEvent.java deleted file mode 100644 index a712c627..00000000 --- a/src/main/java/com/onarandombox/MultiverseCore/listeners/NormalChatEvent.java +++ /dev/null @@ -1,37 +0,0 @@ -package com.onarandombox.MultiverseCore.listeners; - -import org.bukkit.entity.Player; -import org.bukkit.event.player.PlayerChatEvent; - -/** - * A wrapper for the {@link PlayerChatEvent}. - * @deprecated This is deprecated like the {@link PlayerChatEvent}. - */ -@Deprecated -public class NormalChatEvent implements ChatEvent { - private final PlayerChatEvent event; - - public NormalChatEvent(PlayerChatEvent event) { - this.event = event; - } - - @Override - public boolean isCancelled() { - return event.isCancelled(); - } - - @Override - public String getFormat() { - return event.getFormat(); - } - - @Override - public void setFormat(String s) { - event.setFormat(s); - } - - @Override - public Player getPlayer() { - return event.getPlayer(); - } -} diff --git a/src/main/java/com/onarandombox/MultiverseCore/locale/MVCorei18n.java b/src/main/java/com/onarandombox/MultiverseCore/locale/MVCorei18n.java new file mode 100644 index 00000000..f91073d0 --- /dev/null +++ b/src/main/java/com/onarandombox/MultiverseCore/locale/MVCorei18n.java @@ -0,0 +1,18 @@ +package com.onarandombox.MultiverseCore.locale; + +import co.aikar.locales.MessageKey; +import co.aikar.locales.MessageKeyProvider; + +public enum MVCorei18n implements MessageKeyProvider { + + CONFIG_SAVE_FAILED, + DEBUG_INFO_OFF, + DEBUG_INFO_ON; + + private final MessageKey key = MessageKey.of("mv-core." + this.name().toLowerCase()); + + @Override + public MessageKey getMessageKey() { + return this.key; + } + } 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 deleted file mode 100644 index fd68d2ea..00000000 --- a/src/main/java/com/onarandombox/MultiverseCore/utils/BukkitTravelAgent.java +++ /dev/null @@ -1,110 +0,0 @@ -package com.onarandombox.MultiverseCore.utils; - -import java.util.logging.Level; - -import com.onarandombox.MultiverseCore.api.SafeTTeleporter; -import com.onarandombox.MultiverseCore.destination.CannonDestination; -import org.bukkit.Location; -import org.bukkit.TravelAgent; -import org.bukkit.event.player.PlayerPortalEvent; - -public class BukkitTravelAgent implements TravelAgent { - private final MVTravelAgent agent; - - public BukkitTravelAgent(MVTravelAgent agent) { - this.agent = agent; - } - - /** - * {@inheritDoc} - */ - @Override - public BukkitTravelAgent setSearchRadius(int radius) { - return this; - } - - /** - * {@inheritDoc} - */ - @Override - public int getSearchRadius() { - return 0; - } - - /** - * {@inheritDoc} - */ - @Override - public BukkitTravelAgent setCreationRadius(int radius) { - return this; - } - - /** - * {@inheritDoc} - */ - @Override - public int getCreationRadius() { - return 0; - } - - /** - * {@inheritDoc} - */ - @Override - public boolean getCanCreatePortal() { - return false; - } - - /** - * {@inheritDoc} - */ - @Override - public void setCanCreatePortal(boolean create) { - } - - /** - * {@inheritDoc} - */ - @Override - public Location findOrCreate(Location location) { - return this.getSafeLocation(); - } - - /** - * {@inheritDoc} - */ - @Override - public Location findPortal(Location location) { - return this.getSafeLocation(); - } - - /** - * {@inheritDoc} - */ - @Override - public boolean createPortal(Location location) { - return false; - } - - 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"); - } - SafeTTeleporter teleporter = agent.core.getSafeTTeleporter(); - Location newLoc = agent.destination.getLocation(agent.player); - if (agent.destination.useSafeTeleporter()) { - newLoc = teleporter.getSafeLocation(agent.player, agent.destination); - } - if (newLoc == null) { - return agent.player.getLocation(); - } - return newLoc; - - } - - public void setPortalEventTravelAgent(PlayerPortalEvent event) { - event.setPortalTravelAgent(this); - event.useTravelAgent(true); - } -} 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/LocationManipulation.java b/src/main/java/com/onarandombox/MultiverseCore/utils/LocationManipulation.java deleted file mode 100644 index b0bce924..00000000 --- a/src/main/java/com/onarandombox/MultiverseCore/utils/LocationManipulation.java +++ /dev/null @@ -1,285 +0,0 @@ -/****************************************************************************** - * Multiverse 2 Copyright (c) the Multiverse Team 2011. * - * 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.utils; - -import org.bukkit.Bukkit; -import org.bukkit.ChatColor; -import org.bukkit.Location; -import org.bukkit.World; -import org.bukkit.entity.Vehicle; -import org.bukkit.util.Vector; - -import java.text.DecimalFormat; -import java.util.Collections; -import java.util.HashMap; -import java.util.Locale; -import java.util.Map; - -/** - * Utility class to manipulate locations. - * - * @deprecated Use instead: {@link com.onarandombox.MultiverseCore.api.LocationManipulation} and {@link SimpleLocationManipulation}. - */ -@Deprecated -public class LocationManipulation { - private LocationManipulation() { } - - private static Map orientationInts = new HashMap(); - - static { - // BEGIN CHECKSTYLE-SUPPRESSION: MagicNumberCheck - orientationInts.put("n", 180); - orientationInts.put("ne", 225); - orientationInts.put("e", 270); - orientationInts.put("se", 315); - orientationInts.put("s", 0); - orientationInts.put("sw", 45); - orientationInts.put("w", 90); - orientationInts.put("nw", 135); - - // "freeze" the map: - orientationInts = Collections.unmodifiableMap(orientationInts); - // END CHECKSTYLE-SUPPRESSION: MagicNumberCheck - } - - /** - * Convert a Location into a Colon separated string to allow us to store it in text. - *

- * WORLD:X,Y,Z:yaw:pitch - *

- * The corresponding String2Loc function is {@link #stringToLocation} - * - * @param location The Location to save. - * @return The location as a string in this format: WORLD:x,y,z:yaw:pitch - */ - public static String locationToString(Location location) { - if (location == null) { - return ""; - } - // We set the locale to ENGLISH here so we always save with the format: - // world:1.2,5.4,3.6:1.8:21.3 - // Otherwise we blow up when parsing! - return String.format(Locale.ENGLISH, "%s:%.2f,%.2f,%.2f:%.2f:%.2f", location.getWorld().getName(), - location.getX(), location.getY(), location.getZ(), location.getYaw(), location.getPitch()); - } - - /** - * This method simply does some rounding, rather than forcing a call to the server to get the blockdata. - * - * @param l The location to round to the block location - * @return A rounded location. - */ - public static Location getBlockLocation(Location l) { - l.setX(l.getBlockX()); - l.setY(l.getBlockY()); - l.setZ(l.getBlockZ()); - return l; - } - - /** - * Returns a new location from a given string. The format is as follows: - *

- * WORLD:X,Y,Z:yaw:pitch - *

- * The corresponding Location2String function is {@link #stringToLocation} - * - * @param locationString The location represented as a string (WORLD:X,Y,Z:yaw:pitch) - * @return A new location defined by the string or null if the string was invalid. - */ - public static Location stringToLocation(String locationString) { - //format: - //world:x,y,z:pitch:yaw - if (locationString == null) { - return null; - } - - // Split the whole string, format is: - // {'world', 'x,y,z'[, 'pitch', 'yaw']} - String[] split = locationString.split(":"); - if (split.length < 2 || split.length > 4) { // SUPPRESS CHECKSTYLE: MagicNumberCheck - return null; - } - // Split the xyz string, format is: - // {'x', 'y', 'z'} - String[] xyzsplit = split[1].split(","); - if (xyzsplit.length != 3) { - return null; - } - - // Verify the world is valid - World w = Bukkit.getWorld(split[0]); - if (w == null) { - return null; - } - - try { - float pitch = 0; - float yaw = 0; - if (split.length >= 3) { - yaw = (float) Double.parseDouble(split[2]); - } - if (split.length == 4) { // SUPPRESS CHECKSTYLE: MagicNumberCheck - pitch = (float) Double.parseDouble(split[3]); - } - return new Location(w, Double.parseDouble(xyzsplit[0]), Double.parseDouble(xyzsplit[1]), Double.parseDouble(xyzsplit[2]), yaw, pitch); - } catch (NumberFormatException e) { - return null; - } - } - - /** - * Returns a colored string with the coords. - * - * @param l The {@link Location} - * @return The {@link String} - */ - public static String strCoords(Location l) { - String result = ""; - DecimalFormat df = new DecimalFormat(); - df.setMinimumFractionDigits(0); - df.setMaximumFractionDigits(2); - result += ChatColor.WHITE + "X: " + ChatColor.AQUA + df.format(l.getX()) + " "; - result += ChatColor.WHITE + "Y: " + ChatColor.AQUA + df.format(l.getY()) + " "; - result += ChatColor.WHITE + "Z: " + ChatColor.AQUA + df.format(l.getZ()) + " "; - result += ChatColor.WHITE + "P: " + ChatColor.GOLD + df.format(l.getPitch()) + " "; - result += ChatColor.WHITE + "Y: " + ChatColor.GOLD + df.format(l.getYaw()) + " "; - return result; - } - - /** - * Converts a location to a printable readable formatted string including pitch/yaw. - * - * @param l The {@link Location} - * @return The {@link String} - */ - public static String strCoordsRaw(Location l) { - if (l == null) { - return "null"; - } - String result = ""; - DecimalFormat df = new DecimalFormat(); - df.setMinimumFractionDigits(0); - df.setMaximumFractionDigits(2); - result += "X: " + df.format(l.getX()) + " "; - result += "Y: " + df.format(l.getY()) + " "; - result += "Z: " + df.format(l.getZ()) + " "; - result += "P: " + df.format(l.getPitch()) + " "; - result += "Y: " + df.format(l.getYaw()) + " "; - return result; - } - - /** - * Return the NESW Direction a Location is facing. - * - * @param location The {@link Location} - * @return The NESW Direction - */ - public static String getDirection(Location location) { - // BEGIN CHECKSTYLE-SUPPRESSION: MagicNumberCheck - double r = (location.getYaw() % 360) + 180; - // Remember, these numbers are every 45 degrees with a 22.5 offset, to detect boundaries. - String dir; - if (r < 22.5) - dir = "n"; - else if (r < 67.5) - dir = "ne"; - else if (r < 112.5) - dir = "e"; - else if (r < 157.5) - dir = "se"; - else if (r < 202.5) - dir = "s"; - else if (r < 247.5) - dir = "sw"; - else if (r < 292.5) - dir = "w"; - else if (r < 337.5) - dir = "nw"; - else - dir = "n"; - // END CHECKSTYLE-SUPPRESSION: MagicNumberCheck - - return dir; - } - - /** - * Returns the float yaw position for the given cardinal direction. - * - * @param orientation The cardinal direction - * @return The yaw - */ - public static float getYaw(String orientation) { - if (orientation == null) { - return 0; - } - if (orientationInts.containsKey(orientation.toLowerCase())) { - return orientationInts.get(orientation.toLowerCase()); - } - return 0; - } - - /** - * Returns a speed float from a given vector. - * - * @param v The {@link Vector} - * @return The speed - */ - public static float getSpeed(Vector v) { - return (float) Math.sqrt(v.getX() * v.getX() + v.getZ() * v.getZ()); - } - - // X, Y, Z - // -W/+E,0, -N/+S - - /** - * Returns a translated vector from the given direction. - * - * @param v The old {@link Vector} - * @param direction The new direction - * @return The translated {@link Vector} - */ - public static Vector getTranslatedVector(Vector v, String direction) { - if (direction == null) { - return v; - } - float speed = getSpeed(v); - float halfSpeed = (float) (speed / 2.0); - if (direction.equalsIgnoreCase("n")) { - return new Vector(0, 0, -1 * speed); - } else if (direction.equalsIgnoreCase("ne")) { - return new Vector(halfSpeed, 0, -1 * halfSpeed); - } else if (direction.equalsIgnoreCase("e")) { - return new Vector(speed, 0, 0); - } else if (direction.equalsIgnoreCase("se")) { - return new Vector(halfSpeed, 0, halfSpeed); - } else if (direction.equalsIgnoreCase("s")) { - return new Vector(0, 0, speed); - } else if (direction.equalsIgnoreCase("sw")) { - return new Vector(-1 * halfSpeed, 0, halfSpeed); - } else if (direction.equalsIgnoreCase("w")) { - return new Vector(-1 * speed, 0, 0); - } else if (direction.equalsIgnoreCase("nw")) { - return new Vector(-1 * halfSpeed, 0, -1 * halfSpeed); - } - return v; - } - - /** - * Returns the next Location that a {@link Vehicle} is traveling at. - * - * @param v The {@link Vehicle} - * @return The {@link Location} - */ - public static Location getNextBlock(Vehicle v) { - Vector vector = v.getVelocity(); - Location location = v.getLocation(); - int x = vector.getX() < 0 ? vector.getX() == 0 ? 0 : -1 : 1; - int z = vector.getZ() < 0 ? vector.getZ() == 0 ? 0 : -1 : 1; - return location.add(x, 0, z); - } -} 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/MVTravelAgent.java b/src/main/java/com/onarandombox/MultiverseCore/utils/MVTravelAgent.java deleted file mode 100644 index a86def8a..00000000 --- a/src/main/java/com/onarandombox/MultiverseCore/utils/MVTravelAgent.java +++ /dev/null @@ -1,27 +0,0 @@ -/****************************************************************************** - * Multiverse 2 Copyright (c) the Multiverse Team 2011. * - * 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.utils; - -import com.onarandombox.MultiverseCore.MultiverseCore; -import com.onarandombox.MultiverseCore.api.MVDestination; -import org.bukkit.entity.Player; - -/** - * The Multiverse TravelAgent. - */ -public class MVTravelAgent { - protected MVDestination destination; - protected MultiverseCore core; - protected Player player; - - public MVTravelAgent(MultiverseCore multiverseCore, MVDestination d, Player p) { - this.destination = d; - this.core = multiverseCore; - this.player = p; - } -} 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..93367ae5 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/utils/SimpleSafeTTeleporter.java +++ b/src/main/java/com/onarandombox/MultiverseCore/utils/SimpleSafeTTeleporter.java @@ -7,12 +7,13 @@ 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; +import com.onarandombox.MultiverseCore.api.SafeTTeleporter; import com.onarandombox.MultiverseCore.destination.InvalidDestination; import com.onarandombox.MultiverseCore.enums.TeleportResult; - +import org.bukkit.Bukkit; import org.bukkit.Location; import org.bukkit.Material; import org.bukkit.block.Block; @@ -24,8 +25,6 @@ import org.bukkit.entity.Player; import org.bukkit.entity.Vehicle; import org.bukkit.util.Vector; -import java.util.logging.Level; - /** * The default-implementation of {@link SafeTTeleporter}. */ @@ -36,6 +35,7 @@ public class SimpleSafeTTeleporter implements SafeTTeleporter { this.plugin = plugin; } + private static final Vector DEFAULT_VECTOR = new Vector(); private static final int DEFAULT_TOLERANCE = 6; private static final int DEFAULT_RADIUS = 9; @@ -58,9 +58,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 +72,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 +191,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; @@ -213,8 +213,11 @@ public class SimpleSafeTTeleporter implements SafeTTeleporter { if (safeLoc != null) { if (teleportee.teleport(safeLoc)) { - if (!d.getVelocity().equals(new Vector(0, 0, 0))) { - teleportee.setVelocity(d.getVelocity()); + Vector v = d.getVelocity(); + if (v != null && !DEFAULT_VECTOR.equals(v)) { + Bukkit.getScheduler().runTaskLater(this.plugin, () -> { + teleportee.setVelocity(d.getVelocity()); + }, 1); } return TeleportResult.SUCCESS; } @@ -248,7 +251,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 +270,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 d3fcb8c6..d50c2a09 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,20 +91,10 @@ 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!"); } } - /** - * {@inheritDoc} - * @deprecated Use {@link #cloneWorld(String, String)} instead. - */ - @Override - @Deprecated - public boolean cloneWorld(String oldName, String newName, String generator) { - return this.cloneWorld(oldName, newName); - } - /** * {@inheritDoc} */ @@ -125,12 +119,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 +152,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 +171,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(""); - return true; - } - } Logging.warning("Failed to copy files for world '%s', see the log info", newName); return false; } @@ -225,6 +227,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 +276,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 +349,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 +377,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 +417,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 +465,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 +496,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 +513,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 +529,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 +607,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 +622,7 @@ public class WorldManager implements MVWorldManager { if (world != null) { return world; } - return this.getMVWorldByAlias(name); + return (checkAliases) ? this.getMVWorldByAlias(name) : null; } /** @@ -607,7 +631,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 +656,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)); } /** @@ -742,16 +774,6 @@ public class WorldManager implements MVWorldManager { } } - /** - * {@inheritDoc} - * @deprecated This is deprecated! - */ - @Override - @Deprecated - public PurgeWorlds getWorldPurger() { - return new PurgeWorlds(plugin); - } - /** * {@inheritDoc} */ @@ -827,7 +849,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 +877,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 +909,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 +977,9 @@ public class WorldManager implements MVWorldManager { return this.configWorlds; } + /** + * {@inheritDoc} + */ @Override public boolean hasUnloadedWorld(String name, boolean includeLoaded) { if (getMVWorld(name) != null) { @@ -911,4 +992,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 index fd3470a6..5b34acad 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/utils/metrics/MetricsConfigurator.java +++ b/src/main/java/com/onarandombox/MultiverseCore/utils/metrics/MetricsConfigurator.java @@ -61,7 +61,7 @@ public class MetricsConfigurator { private String getGeneratorName(MultiverseWorld world) { String gen = world.getGenerator(); - return (gen != null && !gen.equalsIgnoreCase("null")) ? gen : NO_GENERATOR_NAME; + return (gen != null && !gen.equalsIgnoreCase("null")) ? gen.split(":")[0] : NO_GENERATOR_NAME; } private void addEnvironmentsMetric() { diff --git a/src/main/java/com/onarandombox/MultiverseCore/utils/metrics/MetricsHelper.java b/src/main/java/com/onarandombox/MultiverseCore/utils/metrics/MetricsHelper.java index 23fb6eba..8fa8da45 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/utils/metrics/MetricsHelper.java +++ b/src/main/java/com/onarandombox/MultiverseCore/utils/metrics/MetricsHelper.java @@ -1,11 +1,12 @@ 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; -import org.bstats.bukkit.Metrics; - enum MetricsHelper { ; @@ -17,16 +18,16 @@ enum MetricsHelper { map.put(key, count + 1); } - static Metrics.AdvancedPie createAdvancedPieChart(String chartId, Consumer> metricsFunc) { + static AdvancedPie createAdvancedPieChart(String chartId, Consumer> metricsFunc) { Map map = new HashMap<>(); metricsFunc.accept(map); - return new Metrics.AdvancedPie(chartId, () -> map); + return new AdvancedPie(chartId, () -> map); } - static Metrics.MultiLineChart createMultiLineChart(String chartId, Consumer> metricsFunc) { + static MultiLineChart createMultiLineChart(String chartId, Consumer> metricsFunc) { Map map = new HashMap<>(); metricsFunc.accept(map); - return new Metrics.MultiLineChart(chartId, () -> 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..3c1e7234 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,42 @@ package com.onarandombox.MultiverseCore.utils.webpaste; import java.io.IOException; +import java.util.Map; + +import net.minidev.json.JSONObject; +import net.minidev.json.parser.JSONParser; +import net.minidev.json.parser.ParseException; /** - * 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("long_url", data); + return json.toJSONString(); + } + + /** + * {@inheritDoc} + */ + @Override + String encodeData(Map data) { + throw new UnsupportedOperationException(); } /** @@ -22,13 +45,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/multiverse-core_en.properties b/src/main/resources/multiverse-core_en.properties new file mode 100644 index 00000000..be09fb1f --- /dev/null +++ b/src/main/resources/multiverse-core_en.properties @@ -0,0 +1,7 @@ +mv-core.config_save_failed=§cUnable to save Multiverse-Core config.yml. Your changes will be temporary! +mv-core.debug_info_description=Show the current debug level. +mv-core.debug_info_off=§fMultiverse Debug mode is §cOFF§f. +mv-core.debug_info_on=§fMultiverse Debug mode is at §alevel {level}§f. +mv-core.debug_change_description=Change debug level. +mv-core.debug_change_syntax=level +mv-core.debug_change_level_description=Debug level to set to. diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index 5359af21..d46f0fd7 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -11,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: @@ -47,103 +37,75 @@ commands: / mvmodify: description: Modify the settings of an existing world + aliases: [mvm] usage: | - /