diff --git a/.github/ISSUE_TEMPLATE/config.yml b/.github/ISSUE_TEMPLATE/config.yml new file mode 100644 index 000000000..7beb1efdb --- /dev/null +++ b/.github/ISSUE_TEMPLATE/config.yml @@ -0,0 +1,5 @@ +blank_issues_enabled: false +contact_links: + - name: 💬 ​Multiverse Discord Server + url: https://discord.gg/NZtfKky + about: Need help with using Multiverse-Core or just want to chat with the devs? Join the Multiverse Discord Server for help! diff --git a/.github/ISSUE_TEMPLATE/help.md b/.github/ISSUE_TEMPLATE/help.md new file mode 100644 index 000000000..e7a615ff3 --- /dev/null +++ b/.github/ISSUE_TEMPLATE/help.md @@ -0,0 +1,56 @@ +--- +name: ❓ Help! +about: Encountered a problem with Multiverse-Core? Not sure how to fix it? +title: '' +labels: 'Assistance' +assignees: '' + +--- + + + +### Information + +* **Server version:** + +* **Full output of `/mv version -b`:** + +* **Server log:** + +### Help request + +**Problem** + + +**What I have tried** + + +**Screenshots** + \ No newline at end of file diff --git a/.github/ISSUE_TEMPLATE/report-a-bug.md b/.github/ISSUE_TEMPLATE/report-a-bug.md new file mode 100644 index 000000000..37143b42c --- /dev/null +++ b/.github/ISSUE_TEMPLATE/report-a-bug.md @@ -0,0 +1,59 @@ +--- +name: 🐛 Report a Bug +about: Report a Multiverse-Core bug. Only use this if you're 100% sure it's something wrong with Multiverse-Core - otherwise, try "Help!". +title: '' +labels: 'Bug' +assignees: '' + +--- + + + +### Information + +* **Server version:** + +* **Full output of `/mv version -b`:** + +* **Server log:** + +### Details +I was **``** to reproduce my issue on a freshly setup and up-to-date server with the latest version of Multiverse plugins with no other plugins and with no kinds of other server or client mods. + +**Description** + + +**Steps to reproduce** + + +**Expected behavior** + + +**Screenshots** + diff --git a/.github/ISSUE_TEMPLATE/request-a-feature.md b/.github/ISSUE_TEMPLATE/request-a-feature.md new file mode 100644 index 000000000..60447ab43 --- /dev/null +++ b/.github/ISSUE_TEMPLATE/request-a-feature.md @@ -0,0 +1,54 @@ +--- +name: 💡 Request a Feature +about: Suggest a feature you want to see in Multiverse-Core! +title: '' +labels: 'Suggestion' +assignees: '' + +--- + + + +### Feature request + +**Feature description** + + +**How the feature is useful** + \ No newline at end of file diff --git a/.gitignore b/.gitignore index 3fd14a371..4a3f59823 100644 --- a/.gitignore +++ b/.gitignore @@ -12,6 +12,7 @@ # maven /target +dependency-reduced-pom.xml # vim .*.sw[a-p] diff --git a/.travis.yml b/.travis.yml index b94030c85..2e7dbb32c 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,7 +1,6 @@ -language: java -jdk: - - openjdk6 -notifications: - email: false -before_install: - - sed -i.bak -e 's|https://nexus.codehaus.org/snapshots/|https://oss.sonatype.org/content/repositories/codehaus-snapshots/|g' ~/.m2/settings.xml +language: java +jdk: + - oraclejdk8 +notifications: + email: false +dist: trusty diff --git a/README.md b/README.md index 41d090ee0..3d4af1fc2 100644 --- a/README.md +++ b/README.md @@ -13,3 +13,4 @@ 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/config/mv_checks.xml b/config/mv_checks.xml index f28ddaf91..0c660d8d9 100644 --- a/config/mv_checks.xml +++ b/config/mv_checks.xml @@ -115,7 +115,6 @@ - - elmakers-repo - http://maven.elmakers.com/repository/ + minebench-repo + https://repo.minebench.de/ + + + CodeMC + https://repo.codemc.org/repository/maven-public @@ -66,6 +70,17 @@ ${env.BUILD_NUMBER} + + bitly + + + env.BITLY_ACCESS_TOKEN + + + + ${env.BITLY_ACCESS_TOKEN} + + @@ -75,39 +90,62 @@ org.apache.maven.plugins maven-compiler-plugin - 2.3.2 + 3.7.0 - 1.6 - 1.6 + 1.8 + 1.8 com.google.code.maven-replacer-plugin maven-replacer-plugin - 1.3.8 + 1.4.1 + replace-bitly-access-token + generate-sources + + replace + + + ${project.build.sourceDirectory} + + com/onarandombox/MultiverseCore/utils/webpaste/BitlyURLShortener.java + + + + bitly-access-token + ${project.bitly-access-token} + + + + + + replace-maven-version-number prepare-package replace + + ${project.build.directory}/classes + + plugin.yml + + + + maven-version-number + ${project.version}-b${project.build.number} + + + - - target/classes/plugin.yml - - - maven-version-number - ${project.version}-b${project.build.number} - - - org.apache.maven.plugins maven-jar-plugin - 2.3.1 + 3.0.2 @@ -118,7 +156,7 @@ org.apache.maven.plugins maven-surefire-plugin - 2.11 + 3.0.0-M3 methods 10 @@ -131,14 +169,14 @@ org.apache.maven.surefire surefire-junit47 - 2.11 + 3.0.0-M3 org.apache.maven.plugins maven-checkstyle-plugin - 2.10 + 2.17 true ${project.basedir}/config/mv_checks.xml @@ -147,7 +185,7 @@ org.apache.maven.plugins maven-source-plugin - 2.1.2 + 3.0.1 attach-sources @@ -160,7 +198,7 @@ maven-javadoc-plugin - 2.8.1 + 2.10.4 javadoc-jar @@ -168,13 +206,16 @@ jar + + false + org.apache.maven.plugins maven-shade-plugin - 1.5 + 3.1.1 package @@ -182,32 +223,22 @@ shade - - - me.main__.util:SerializationConfig - com.pneumaticraft.commandhandler:CommandHandler - com.dumptruckman.minecraft:buscript - org.mcstats.bukkit:metrics - com.dumptruckman.minecraft:Logging - org.codehaus.jettison:jettison - - me.main__.util - me.main__.util.multiverse + com.onarandombox.serializationconfig com.pneumaticraft.commandhandler - com.pneumaticraft.commandhandler.multiverse + com.onarandombox.commandhandler buscript - buscript.multiverse + com.onarandombox.buscript - org.mcstats - org.mcstats.multiverse + org.bstats + com.onarandombox.bstats com.dumptruckman.minecraft.util.Logging @@ -218,32 +249,28 @@ com.onarandombox.MultiverseCore.utils.DebugFileLogger - org.codehaus.jettison.json - org.codehaus.jettison.json.multiverse + org.codehaus.jettison + com.onarandombox.jettison + + + de.themoep.idconverter + com.onarandombox.idconverter + + + + se.eris + notnull-instrumenter-maven-plugin + 0.6.8 + - apache-commons-io - package - shade + instrument + tests-instrument - - - - commons-io:commons-io - - - - - org.apache.commons - org.apache.commons.multiverse - - - true - @@ -252,70 +279,47 @@ - commons-io - commons-io - 2.4 - jar - compile + org.bukkit + bukkit + 1.13.2-R0.1-SNAPSHOT + provided - - - org.spigotmc - spigot-api - 1.9-R0.1-SNAPSHOT - compile - - - me.main__.util SerializationConfig 1.7 - jar - compile + + + org.bukkit + bukkit + + net.milkbowl.vault VaultAPI - 1.5 - jar - compile + 1.7 + provided com.pneumaticraft.commandhandler CommandHandler - 8 - jar - compile + 11 - - com.dumptruckman.minecraft buscript - 1.0 - jar - compile + 2.0-SNAPSHOT - - - org.mcstats.bukkit - metrics - R8-SNAPSHOT - jar - compile + org.bstats + bstats-bukkit + 1.7 org.bukkit @@ -323,63 +327,62 @@ - - com.dumptruckman.minecraft Logging - 1.0.9 - jar - compile - - - org.bukkit - craftbukkit - - + 1.1.1 + + + org.jetbrains + annotations + 16.0.2 - - - org.codehaus.jettison - jettison - 1.3.7 + de.themoep.idconverter + mappings + 1.2-SNAPSHOT - + junit junit - 4.8.2 + 4.12 + test org.powermock powermock-module-junit4 - 1.4.9 + 2.0.0 jar test org.powermock powermock-api-easymock - 1.4.9 + 2.0.0 jar test org.powermock - powermock-api-mockito - 1.4.9 + powermock-api-mockito2 + 2.0.0 jar test org.easymock easymock - 3.0 + 4.0.2 + test + + + commons-io + commons-io + 2.4 test - diff --git a/src/main/java/com/onarandombox/MultiverseCore/MVWorld.java b/src/main/java/com/onarandombox/MultiverseCore/MVWorld.java index ce8770cd5..d8f64e972 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/MVWorld.java +++ b/src/main/java/com/onarandombox/MultiverseCore/MVWorld.java @@ -24,6 +24,7 @@ import org.bukkit.Difficulty; import org.bukkit.GameMode; import org.bukkit.Location; +import org.bukkit.Material; import org.bukkit.World; import org.bukkit.World.Environment; import org.bukkit.WorldType; @@ -33,6 +34,7 @@ import org.bukkit.permissions.Permission; import org.bukkit.permissions.PermissionDefault; import org.bukkit.util.Vector; +import org.jetbrains.annotations.Nullable; import org.json.simple.JSONObject; import java.util.Collections; @@ -280,7 +282,9 @@ public SpawnSettings validateChange(String property, SpawnSettings newValue, Spa } world.setSpawnFlags(allowMonsters, allowAnimals); } - plugin.getMVWorldManager().getTheWorldPurger().purgeWorld(MVWorld.this); + if (MultiverseCoreConfiguration.getInstance().isAutoPurgeEnabled()) { + plugin.getMVWorldManager().getTheWorldPurger().purgeWorld(MVWorld.this); + } return super.validateChange(property, newValue, oldValue, object); } } @@ -601,29 +605,6 @@ else if (property.equalsIgnoreCase("monsters")) return null; } - /** - * {@inheritDoc} - * - * @deprecated This is deprecated. - */ - @Override - @Deprecated - public com.onarandombox.MultiverseCore.configuration.MVConfigProperty getProperty(String property, - Class expected) throws PropertyDoesNotExistException { - throw new UnsupportedOperationException("'MVConfigProperty getProperty(String,Class)' is no longer supported!"); - } - - /** - * {@inheritDoc} - * - * @deprecated This is deprecated. - */ - @Override - @Deprecated - public boolean setProperty(String name, String value, CommandSender sender) throws PropertyDoesNotExistException { - return this.setPropertyValue(name, value); - } - /** * {@inheritDoc} */ @@ -943,7 +924,7 @@ public Permission getAccessPermission() { * {@inheritDoc} */ @Override - public int getCurrency() { + public Material getCurrency() { return this.props.getCurrency(); } @@ -951,7 +932,7 @@ public int getCurrency() { * {@inheritDoc} */ @Override - public void setCurrency(int currency) { + public void setCurrency(@Nullable Material currency) { this.props.setCurrency(currency); } diff --git a/src/main/java/com/onarandombox/MultiverseCore/MultiverseCore.java b/src/main/java/com/onarandombox/MultiverseCore/MultiverseCore.java index 8bf674498..b1d316cbe 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/MultiverseCore.java +++ b/src/main/java/com/onarandombox/MultiverseCore/MultiverseCore.java @@ -7,6 +7,21 @@ 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; @@ -17,7 +32,6 @@ import com.onarandombox.MultiverseCore.api.MVWorldManager; import com.onarandombox.MultiverseCore.api.MultiverseCoreConfig; import com.onarandombox.MultiverseCore.api.MultiverseMessaging; -import com.onarandombox.MultiverseCore.api.MultiverseWorld; import com.onarandombox.MultiverseCore.api.SafeTTeleporter; import com.onarandombox.MultiverseCore.commands.AnchorCommand; import com.onarandombox.MultiverseCore.commands.CheckCommand; @@ -61,6 +75,7 @@ import com.onarandombox.MultiverseCore.destination.ExactDestination; 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; @@ -77,6 +92,9 @@ import com.onarandombox.MultiverseCore.utils.MVMessaging; import com.onarandombox.MultiverseCore.utils.MVPermissions; import com.onarandombox.MultiverseCore.utils.MVPlayerSession; +import com.onarandombox.MultiverseCore.utils.MaterialConverter; +import com.onarandombox.MultiverseCore.utils.TestingMode; +import com.onarandombox.MultiverseCore.utils.metrics.MetricsConfigurator; import com.onarandombox.MultiverseCore.utils.SimpleBlockSafety; import com.onarandombox.MultiverseCore.utils.SimpleLocationManipulation; import com.onarandombox.MultiverseCore.utils.SimpleSafeTTeleporter; @@ -90,8 +108,6 @@ import org.bukkit.Difficulty; import org.bukkit.GameMode; import org.bukkit.Location; -import org.bukkit.Server; -import org.bukkit.World.Environment; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; import org.bukkit.configuration.Configuration; @@ -101,28 +117,15 @@ import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.entity.Player; import org.bukkit.plugin.PluginDescriptionFile; -import org.bukkit.plugin.PluginLoader; import org.bukkit.plugin.PluginManager; import org.bukkit.plugin.java.JavaPlugin; -import org.mcstats.Metrics; - -import java.io.*; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collections; -import java.util.HashMap; -import java.util.HashSet; -import java.util.LinkedHashMap; -import java.util.List; -import java.util.Map; -import java.util.Set; -import java.util.logging.Level; +import org.bukkit.plugin.java.JavaPluginLoader; /** * The implementation of the Multiverse-{@link Core}. */ public class MultiverseCore extends JavaPlugin implements MVPlugin, Core { - private static final int PROTOCOL = 20; + private static final int PROTOCOL = 24; // TODO: Investigate if this one is really needed to be static. // Doubt it. -- FernFerret private static Map teleportQueue = new HashMap(); @@ -137,11 +140,13 @@ public MultiverseCore() { /** * This is for unit testing. - * @deprecated + * @param loader The PluginLoader to use. + * @param description The Description file to use. + * @param dataFolder The folder that other datafiles can be found in. + * @param file The location of the plugin. */ - @Deprecated - public MultiverseCore(PluginLoader loader, Server server, PluginDescriptionFile description, File dataFolder, File file) { - super(loader, server, description, dataFolder, file); + public MultiverseCore(JavaPluginLoader loader, PluginDescriptionFile description, File dataFolder, File file) { + super(loader, description, dataFolder, file); } /** @@ -169,11 +174,6 @@ public static void addPlayerToTeleportQueue(String teleporter, String teleportee teleportQueue.put(teleportee, teleporter); } - @Override - public String toString() { - return "The Multiverse-Core Plugin"; - } - /** * {@inheritDoc} * @deprecated This is now deprecated, nobody needs it any longer. @@ -228,7 +228,6 @@ public int getProtocolVersion() { private Buscript buscript; private int pluginCount; private DestinationFactory destFactory; - //private SpoutInterface spoutInterface = null; private MultiverseMessaging messaging; private BlockSafety blockSafety; private LocationManipulation locationManipulation; @@ -303,7 +302,6 @@ public void onEnable() { // Setup & Load our Configuration files. loadConfigs(); if (this.multiverseConfig != null) { - Logging.setDebugLevel(getMVConfig().getGlobalDebug()); Logging.setShowingConfig(!getMVConfig().getSilentStart()); this.worldManager.loadDefaultWorlds(); this.worldManager.loadWorlds(true); @@ -332,119 +330,36 @@ public void onEnable() { this.chatListener = new MVPlayerChatListener(this, this.playerListener); } getServer().getPluginManager().registerEvents(this.chatListener, this); - /* - // Check to see if spout was already loaded (most likely): - if (this.getServer().getPluginManager().getPlugin("Spout") != null) { - this.setSpout(); - this.log(Level.INFO, "Spout integration enabled."); - } - */ this.initializeBuscript(); this.setupMetrics(); // Output a little snippet to show it's enabled. Logging.config("Version %s (API v%s) Enabled - By %s", this.getDescription().getVersion(), PROTOCOL, getAuthors()); - } - /** - * Initializes the buscript javascript library. - */ - private void initializeBuscript() { - buscript = new Buscript(this); - // Add global variable "multiverse" to javascript environment - buscript.getGlobalScope().put("multiverse", buscript.getGlobalScope(), this); - } - - /** - * Plotter for Environment-Values. - */ - private static final class EnvironmentPlotter extends Metrics.Plotter { - private MultiverseCore core; - private final Environment env; - - public EnvironmentPlotter(MultiverseCore core, Environment env) { - super(envToString(env)); - this.core = core; - this.env = env; - } - - private static String envToString(Environment env) { - return new StringBuilder().append(env.name().toUpperCase().charAt(0)) - .append(env.name().toLowerCase().substring(1)).toString(); + if (getMVConfig().isShowingDonateMessage()) { + getLogger().config("Help dumptruckman keep this project alive. Become a patron! https://www.patreon.com/dumptruckman"); + getLogger().config("One time donations are also appreciated: https://www.paypal.me/dumptruckman"); } + } - @Override - public int getValue() { - int count = 0; - for (MultiverseWorld w : core.getMVWorldManager().getMVWorlds()) - if (w.getEnvironment() == env) - count++; - core.log(Level.FINE, String.format("Tracking %d worlds of type %s", count, env)); - return count; + private void setupMetrics() { + if (TestingMode.isDisabled()) { + MetricsConfigurator.configureMetrics(this); } } /** - * Plotter for Generator-Values. + * Initializes the buscript javascript library. */ - private static final class GeneratorPlotter extends Metrics.Plotter { - private MultiverseCore core; - private final String gen; - - public GeneratorPlotter(MultiverseCore core, String gen) { - super(gen); - this.core = core; - this.gen = gen; - } - - @Override - public int getValue() { - int count = 0; - for (MultiverseWorld w : core.getMVWorldManager().getMVWorlds()) - if (gen.equals(w.getGenerator())) - count++; - core.log(Level.FINE, String.format("Tracking %d worlds of type %s", count, gen)); - return count; - } - } - - private void setupMetrics() { + private void initializeBuscript() { try { - Metrics m = new Metrics(this); - - Metrics.Graph envGraph = m.createGraph("Worlds by environment"); - for (Environment env : Environment.values()) - envGraph.addPlotter(new EnvironmentPlotter(this, env)); - - Metrics.Graph loadedWorldsGraph = m.createGraph("Worlds by environment"); - loadedWorldsGraph.addPlotter(new Metrics.Plotter("Loaded worlds") { - @Override - public int getValue() { - return getMVWorldManager().getMVWorlds().size(); - } - }); - loadedWorldsGraph.addPlotter(new Metrics.Plotter("Total number of worlds") { - @Override - public int getValue() { - return getMVWorldManager().getMVWorlds().size() - + getMVWorldManager().getUnloadedWorlds().size(); - } - }); - - Set gens = new HashSet(); - for (MultiverseWorld w : this.getMVWorldManager().getMVWorlds()) - gens.add(w.getGenerator()); - gens.remove(null); - gens.remove("null"); - Metrics.Graph genGraph = m.createGraph("Custom Generators"); - for (String gen : gens) - genGraph.addPlotter(new GeneratorPlotter(this, gen)); - - m.start(); - log(Level.FINE, "Metrics have run!"); - } catch (Exception e) { - log(Level.WARNING, "There was an issue while enabling metrics: " + e.getMessage()); + buscript = new Buscript(this); + // Add global variable "multiverse" to javascript environment + buscript.setScriptVariable("multiverse", this); + } catch (NullPointerException e) { + buscript = null; + Logging.warning("Buscript failed to load! The script command will be disabled!"); } } @@ -468,6 +383,7 @@ private void registerEvents() { pm.registerEvents(this.entityListener, this); pm.registerEvents(this.weatherListener, this); pm.registerEvents(this.portalListener, this); + log(Level.INFO, ChatColor.GREEN + "We are aware of the warning about the deprecated event. There is no alternative that allows us to do what we need to do and performance impact is negligible. It is safe to ignore."); pm.registerEvents(this.worldListener, this); pm.registerEvents(new MVMapListener(this), this); } @@ -515,6 +431,12 @@ public void loadConfigs() { // Old Config Format this.migrate22Values(); this.saveMVConfigs(); + + int level = Logging.getDebugLevel(); + Logging.setDebugLevel(getMVConfig().getGlobalDebug()); + if (level != Logging.getDebugLevel()) { + getServer().getPluginManager().callEvent(new MVDebugModeEvent(level)); + } } /** @@ -635,8 +557,9 @@ private void migrateWorldConfig() { // SUPPRESS CHECKSTYLE: MethodLength // migrate entryfee if (section.isConfigurationSection("entryfee")) { ConfigurationSection feeSection = section.getConfigurationSection("entryfee"); - if (feeSection.isInt("currency")) - world.setCurrency(feeSection.getInt("currency")); + if (feeSection.isInt("currency")) { + world.setCurrency(MaterialConverter.convertConfigType(feeSection, "currency")); + } if (feeSection.isDouble("amount")) world.setPrice(feeSection.getDouble("amount")); @@ -882,17 +805,6 @@ public MVPlayerSession getPlayerSession(Player player) { } } - /** - * {@inheritDoc} - * - * @deprecated This is deprecated. - */ - @Override - @Deprecated - public com.onarandombox.MultiverseCore.utils.SafeTTeleporter getTeleporter() { - return new com.onarandombox.MultiverseCore.utils.SafeTTeleporter(this); - } - /** * {@inheritDoc} */ @@ -1092,25 +1004,6 @@ public void setServerFolder(File newServerFolder) { this.serverFolder = newServerFolder; } - /* - /** - * Initializes Spout. - * / - public void setSpout() { - this.spoutInterface = new SpoutInterface(); - this.commandHandler.registerCommand(new SpoutCommand(this)); - } - - /** - * Gets our {@link SpoutInterface}. - * - * @return The {@link SpoutInterface} we're using. - * / - public SpoutInterface getSpout() { - return this.spoutInterface; - } - */ - /** * {@inheritDoc} */ diff --git a/src/main/java/com/onarandombox/MultiverseCore/MultiverseCoreConfiguration.java b/src/main/java/com/onarandombox/MultiverseCore/MultiverseCoreConfiguration.java index 4ffadbb7c..a04a6651a 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/MultiverseCoreConfiguration.java +++ b/src/main/java/com/onarandombox/MultiverseCore/MultiverseCoreConfiguration.java @@ -1,10 +1,14 @@ package com.onarandombox.MultiverseCore; -import com.dumptruckman.minecraft.util.*; -import com.onarandombox.MultiverseCore.api.*; -import me.main__.util.SerializationConfig.*; +import java.util.Map; -import java.util.*; +import com.dumptruckman.minecraft.util.Logging; +import com.onarandombox.MultiverseCore.api.MultiverseCoreConfig; +import com.onarandombox.MultiverseCore.event.MVDebugModeEvent; +import me.main__.util.SerializationConfig.NoSuchPropertyException; +import me.main__.util.SerializationConfig.Property; +import me.main__.util.SerializationConfig.SerializationConfig; +import org.bukkit.Bukkit; /** * Our configuration. @@ -67,6 +71,10 @@ public static MultiverseCoreConfiguration getInstance() { private volatile boolean defaultportalsearch; @Property private volatile int portalsearchradius; + @Property + private volatile boolean autopurge; + @Property + private volatile boolean idonotwanttodonate; public MultiverseCoreConfiguration() { super(); @@ -98,6 +106,8 @@ protected void setDefaults() { silentstart = false; defaultportalsearch = false; portalsearchradius = 128; + autopurge = true; + idonotwanttodonate = false; // END CHECKSTYLE-SUPPRESSION: MagicNumberCheck } @@ -226,6 +236,7 @@ public int getGlobalDebug() { public void setGlobalDebug(int globalDebug) { this.globaldebug = globalDebug; Logging.setDebugLevel(globalDebug); + Bukkit.getPluginManager().callEvent(new MVDebugModeEvent(globalDebug)); } /** @@ -332,4 +343,24 @@ public void setPortalSearchRadius(int searchRadius) { public int getPortalSearchRadius() { return portalsearchradius; } + + @Override + public boolean isAutoPurgeEnabled() { + return autopurge; + } + + @Override + public void setAutoPurgeEnabled(boolean autopurge) { + this.autopurge = autopurge; + } + + @Override + public boolean isShowingDonateMessage() { + return !idonotwanttodonate; + } + + @Override + public void setShowDonateMessage(boolean showDonateMessage) { + this.idonotwanttodonate = !showDonateMessage; + } } diff --git a/src/main/java/com/onarandombox/MultiverseCore/WorldProperties.java b/src/main/java/com/onarandombox/MultiverseCore/WorldProperties.java index 89ad4e815..3dd208b86 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/WorldProperties.java +++ b/src/main/java/com/onarandombox/MultiverseCore/WorldProperties.java @@ -18,8 +18,10 @@ import org.bukkit.Difficulty; import org.bukkit.GameMode; import org.bukkit.Location; +import org.bukkit.Material; import org.bukkit.World.Environment; import org.bukkit.configuration.serialization.SerializableAs; +import org.jetbrains.annotations.Nullable; import java.util.ArrayList; import java.util.Collections; @@ -261,7 +263,7 @@ public Long deserialize(String serialized, Class wanted) throws IllegalPro @Property(description = "Sorry, 'animals' must either be: true or false.") private volatile SpawnSettings spawning; @Property - private volatile EntryFee entryfee; + volatile EntryFee entryfee; @Property(description = "Sorry, 'hunger' must either be: true or false.") private volatile boolean hunger; @Property(description = "Sorry, 'autoheal' must either be: true or false.") @@ -496,11 +498,11 @@ public boolean setRespawnToWorld(String respawnToWorld) { return this.setPropertyValueUnchecked("respawnWorld", respawnToWorld); } - public int getCurrency() { + public Material getCurrency() { return this.entryfee.getCurrency(); } - public void setCurrency(int currency) { + public void setCurrency(@Nullable Material currency) { this.setPropertyValueUnchecked("entryfee.currency", currency); } diff --git a/src/main/java/com/onarandombox/MultiverseCore/api/Core.java b/src/main/java/com/onarandombox/MultiverseCore/api/Core.java index 907d033bc..ff8513d4c 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/api/Core.java +++ b/src/main/java/com/onarandombox/MultiverseCore/api/Core.java @@ -70,17 +70,6 @@ public interface Core { */ MVPlayerSession getPlayerSession(Player player); - /** - * Gets the instantiated Safe-T-Teleporter for performing - * safe teleports. - * - * @return A non-null {@link SafeTTeleporter}. - * - * @deprecated Use {@link #getSafeTTeleporter()} instead. - */ - @Deprecated - com.onarandombox.MultiverseCore.utils.SafeTTeleporter getTeleporter(); - /** * Multiverse uses an advanced permissions setup, this object * simplifies getting/setting permissions. diff --git a/src/main/java/com/onarandombox/MultiverseCore/api/MVWorldManager.java b/src/main/java/com/onarandombox/MultiverseCore/api/MVWorldManager.java index 8015f507c..3eb96e2d5 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/api/MVWorldManager.java +++ b/src/main/java/com/onarandombox/MultiverseCore/api/MVWorldManager.java @@ -199,7 +199,7 @@ boolean addWorld(String name, Environment env, String seedString, WorldType type boolean isMVWorld(World world); /** - * Load the Worlds & Settings from the configuration file. + * Load the Worlds & Settings from the configuration file. * * @param forceLoad If set to true, this will perform a total * reset and not just load new worlds. @@ -207,7 +207,7 @@ boolean addWorld(String name, Environment env, String seedString, WorldType type void loadWorlds(boolean forceLoad); /** - * Loads the Worlds & Settings for any worlds that bukkit loaded before us. + * Loads the Worlds & Settings for any worlds that bukkit loaded before us. *

* This way people will _always_ have some worlds in the list. */ diff --git a/src/main/java/com/onarandombox/MultiverseCore/api/MultiverseCoreConfig.java b/src/main/java/com/onarandombox/MultiverseCore/api/MultiverseCoreConfig.java index bb49048fb..b5574ff26 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/api/MultiverseCoreConfig.java +++ b/src/main/java/com/onarandombox/MultiverseCore/api/MultiverseCoreConfig.java @@ -199,4 +199,32 @@ public interface MultiverseCoreConfig extends ConfigurationSerializable { * @return The portal search radius. */ int getPortalSearchRadius(); + + /** + * Gets whether or not the automatic purge of entities is enabled. + * + * @return True if automatic purge is enabled. + */ + boolean isAutoPurgeEnabled(); + + /** + * Sets whether or not the automatic purge of entities is enabled. + * + * @param autopurge True if automatic purge should be enabled. + */ + void setAutoPurgeEnabled(boolean autopurge); + + /** + * Gets whether or not the donation/patreon messages are shown. + * + * @return True if donation/patreon messages should be shown. + */ + boolean isShowingDonateMessage(); + + /** + * Sets whether or not the donation/patreon messages are shown. + * + * @param idonotwanttodonate True if donation/patreon messages should be shown. + */ + void setShowDonateMessage(boolean idonotwanttodonate); } diff --git a/src/main/java/com/onarandombox/MultiverseCore/api/MultiverseWorld.java b/src/main/java/com/onarandombox/MultiverseCore/api/MultiverseWorld.java index 5ddffafba..07ba347f3 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/api/MultiverseWorld.java +++ b/src/main/java/com/onarandombox/MultiverseCore/api/MultiverseWorld.java @@ -13,10 +13,12 @@ import org.bukkit.Difficulty; import org.bukkit.GameMode; import org.bukkit.Location; +import org.bukkit.Material; import org.bukkit.World; import org.bukkit.WorldType; import org.bukkit.command.CommandSender; import org.bukkit.permissions.Permission; +import org.jetbrains.annotations.Nullable; import java.util.List; @@ -153,37 +155,6 @@ public interface MultiverseWorld { */ boolean setPropertyValue(String property, String value) throws PropertyDoesNotExistException; - /** - * Gets the actual MVConfigProperty from this world. - * It will throw a PropertyDoesNotExistException if the property is not found. - * - * @param property The name of a world property to get. - * @param expected The type of the expected property. Use Object.class if this doesn't matter for you. - * @param The type of the expected property. - * - * @return A valid MVWorldProperty. - * - * @throws PropertyDoesNotExistException Thrown if the property was not found in the world. - * @deprecated We don't use {@link com.onarandombox.MultiverseCore.configuration.MVConfigProperty} any longer! - */ - @Deprecated - com.onarandombox.MultiverseCore.configuration.MVConfigProperty getProperty(String property, Class expected) throws PropertyDoesNotExistException; - - // old config - /** - * Adds the property to the given value. - * It will throw a PropertyDoesNotExistException if the property is not found. - * - * @param property The name of a world property to set. - * @param value A value in string representation, it will be parsed to the correct type. - * @param sender The sender who wants this value to be set. - * @return True if the value was set, false if not. - * @throws PropertyDoesNotExistException Thrown if the property was not found in the world. - * @deprecated Use {@link #setPropertyValue(String, String)} instead. - */ - @Deprecated - boolean setProperty(String property, String value, CommandSender sender) throws PropertyDoesNotExistException; - /** * Adds a value to the given property. The property must be a {@link com.onarandombox.MultiverseCore.enums.AddProperties}. * @@ -504,26 +475,28 @@ public interface MultiverseWorld { /** * Sets the price for entry to this world. * You can think of this like an amount. - * The type can be set with {@link #setCurrency(int)} + * The type can be set with {@link #setCurrency(Material)} * * @param price The Amount of money/item to enter the world. */ void setPrice(double price); /** - * Gets the Type of currency that will be used when users enter this world. + * Gets the type of currency that will be used when users enter this world. A value of null indicates a non-item + * based currency is used. * - * @return The Type of currency that will be used when users enter this world. + * @return The type of currency that will be used when users enter this world. */ - int getCurrency(); + @Nullable + Material getCurrency(); /** * Sets the type of item that will be required given the price is not 0. - * Use -1 to use an AllPay economy, or any valid itemid + * Use a value of null to specify a non-item based currency. * * @param item The Type of currency that will be used when users enter this world. */ - void setCurrency(int item); + void setCurrency(@Nullable Material item); /** * Gets the world players will respawn in if they die in this one. diff --git a/src/main/java/com/onarandombox/MultiverseCore/api/WorldPurger.java b/src/main/java/com/onarandombox/MultiverseCore/api/WorldPurger.java index a52b73f30..b0ccad276 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/api/WorldPurger.java +++ b/src/main/java/com/onarandombox/MultiverseCore/api/WorldPurger.java @@ -17,7 +17,7 @@ public interface WorldPurger { void purgeWorlds(List worlds); /** - * Convenience method for {@link #purgeWorld(CommandSender, MultiverseWorld, List, boolean, boolean)} that takes the settings from the world-config. + * Convenience method for {@link #purgeWorld(MultiverseWorld, java.util.List, boolean, boolean)} that takes the settings from the world-config. * * @param world The {@link MultiverseWorld}. */ diff --git a/src/main/java/com/onarandombox/MultiverseCore/commands/CoordCommand.java b/src/main/java/com/onarandombox/MultiverseCore/commands/CoordCommand.java index 944d8029c..485cc3701 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/commands/CoordCommand.java +++ b/src/main/java/com/onarandombox/MultiverseCore/commands/CoordCommand.java @@ -62,7 +62,7 @@ public void runCommand(CommandSender sender, List args) { df.setMaximumFractionDigits(2); p.sendMessage(ChatColor.AQUA + "Coordinates: " + ChatColor.WHITE + plugin.getLocationManipulation().strCoords(p.getLocation())); p.sendMessage(ChatColor.AQUA + "Direction: " + ChatColor.WHITE + plugin.getLocationManipulation().getDirection(p.getLocation())); - p.sendMessage(ChatColor.AQUA + "Block: " + ChatColor.WHITE + Material.getMaterial(world.getBlockTypeIdAt(p.getLocation()))); + p.sendMessage(ChatColor.AQUA + "Block: " + ChatColor.WHITE + world.getBlockAt(p.getLocation()).getType()); } else { sender.sendMessage("This command needs to be used from a Player."); } diff --git a/src/main/java/com/onarandombox/MultiverseCore/commands/DeleteCommand.java b/src/main/java/com/onarandombox/MultiverseCore/commands/DeleteCommand.java index a37bcd116..2e7b3c8b0 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/commands/DeleteCommand.java +++ b/src/main/java/com/onarandombox/MultiverseCore/commands/DeleteCommand.java @@ -33,9 +33,13 @@ public DeleteCommand(MultiverseCore plugin) { @Override 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 Deleted!", ChatColor.RED + "World could NOT be deleted!"); + this.plugin.getCommandHandler() + .queueCommand(sender, "mvdelete", "deleteWorld", objectArgs, + paramTypes, ChatColor.GREEN + "World '" + worldName + "' Deleted!", + ChatColor.RED + "World '" + worldName + "' could NOT be deleted!"); } } diff --git a/src/main/java/com/onarandombox/MultiverseCore/commands/GameruleCommand.java b/src/main/java/com/onarandombox/MultiverseCore/commands/GameruleCommand.java index 5cc91d9fa..ca0e1e076 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/commands/GameruleCommand.java +++ b/src/main/java/com/onarandombox/MultiverseCore/commands/GameruleCommand.java @@ -10,6 +10,7 @@ import com.onarandombox.MultiverseCore.MultiverseCore; import org.bukkit.Bukkit; import org.bukkit.ChatColor; +import org.bukkit.GameRule; import org.bukkit.World; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; @@ -55,21 +56,61 @@ public void runCommand(CommandSender sender, List args) { return; } - final String gameRule = args.get(0); + final GameRule gameRule = GameRule.getByName(args.get(0)); final String value = args.get(1); final World world; if (args.size() == 2) { world = p.getWorld(); } else { world = Bukkit.getWorld(args.get(2)); + if (world == null) { + sender.sendMessage(ChatColor.RED + "Failure!" + ChatColor.WHITE + " World " + ChatColor.AQUA + args.get(2) + + ChatColor.WHITE + " does not exist."); + return; + } } - if (world.setGameRuleValue(gameRule, value)) { - sender.sendMessage(ChatColor.GREEN + "Success!" + ChatColor.WHITE + " Gamerule " + ChatColor.AQUA + gameRule - + ChatColor.WHITE + " was set to " + ChatColor.GREEN + value); + if (gameRule == null) { + sender.sendMessage(ChatColor.RED + "Failure! " + ChatColor.AQUA + args.get(0) + ChatColor.WHITE + + " is not a valid gamerule."); } else { - sender.sendMessage(ChatColor.RED + "Failure!" + ChatColor.WHITE + " Gamerule " + ChatColor.AQUA + gameRule - + ChatColor.WHITE + " cannot be set to " + ChatColor.RED + value); + if (gameRule.getType() == Boolean.class) { + boolean booleanValue; + if (value.equalsIgnoreCase("true")) { + booleanValue = true; + } else if (value.equalsIgnoreCase("false")) { + booleanValue = false; + } else { + sender.sendMessage(getErrorMessage(gameRule.getName(), value) + "it can only be set to true or false."); + return; + } + + if (!world.setGameRule(gameRule, booleanValue)) { + sender.sendMessage(getErrorMessage(gameRule.getName(), value) + "something went wrong."); + return; + } + } else if (gameRule.getType() == Integer.class) { + try { + if (!world.setGameRule(gameRule, Integer.parseInt(value))) { + throw new NumberFormatException(); + } + } catch (NumberFormatException e) { + sender.sendMessage(getErrorMessage(gameRule.getName(), value) + "it can only be set to a positive integer."); + return; + } + } else { + sender.sendMessage(ChatColor.RED + "Failure!" + ChatColor.WHITE + " Gamerule " + ChatColor.AQUA + gameRule.getName() + + ChatColor.WHITE + " isn't supported yet, please let us know about it."); + return; + } + + sender.sendMessage(ChatColor.GREEN + "Success!" + ChatColor.WHITE + " Gamerule " + ChatColor.AQUA + gameRule.getName() + + ChatColor.WHITE + " was set to " + ChatColor.GREEN + value + ChatColor.WHITE + "."); } } + + private String getErrorMessage(String gameRule, String value) { + return ChatColor.RED + "Failure!" + ChatColor.WHITE + " Gamerule " + ChatColor.AQUA + gameRule + + ChatColor.WHITE + " could not be set to " + ChatColor.RED + value + ChatColor.WHITE + ", "; + } } diff --git a/src/main/java/com/onarandombox/MultiverseCore/commands/GamerulesCommand.java b/src/main/java/com/onarandombox/MultiverseCore/commands/GamerulesCommand.java index 321ac0907..62b3e0607 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/commands/GamerulesCommand.java +++ b/src/main/java/com/onarandombox/MultiverseCore/commands/GamerulesCommand.java @@ -10,6 +10,7 @@ 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; @@ -60,6 +61,11 @@ public void runCommand(CommandSender sender, List args) { world = p.getWorld(); } else { world = Bukkit.getWorld(args.get(0)); + if (world == null) { + sender.sendMessage(ChatColor.RED + "Failure!" + ChatColor.WHITE + " World " + ChatColor.AQUA + args.get(0) + + ChatColor.WHITE + " does not exist."); + return; + } } final StringBuilder gameRules = new StringBuilder(); @@ -68,7 +74,7 @@ public void runCommand(CommandSender sender, List args) { gameRules.append(ChatColor.WHITE).append(", "); } gameRules.append(ChatColor.AQUA).append(gameRule).append(ChatColor.WHITE).append(": "); - gameRules.append(ChatColor.GREEN).append(world.getGameRuleValue(gameRule)); + gameRules.append(ChatColor.GREEN).append(world.getGameRuleValue(GameRule.getByName(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/ImportCommand.java b/src/main/java/com/onarandombox/MultiverseCore/commands/ImportCommand.java index e750cfde3..b16bf2398 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/commands/ImportCommand.java +++ b/src/main/java/com/onarandombox/MultiverseCore/commands/ImportCommand.java @@ -57,7 +57,7 @@ private static boolean checkIfIsWorld(File worldFolder) { File[] files = worldFolder.listFiles(new FilenameFilter() { @Override public boolean accept(File file, String name) { - return name.equalsIgnoreCase("level.dat"); + return name.toLowerCase().endsWith(".dat"); } }); if (files != null && files.length > 0) { @@ -95,10 +95,15 @@ private String getPotentialWorlds() { } return worldList; } + + 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)); if (worldName.toLowerCase().equals("--list") || worldName.toLowerCase().equals("-l")) { String worldList = this.getPotentialWorlds(); @@ -112,7 +117,7 @@ public void runCommand(CommandSender sender, List args) { } // Since we made an exception for the list, we have to make sure they have at least 2 params: // Note the exception is --list, which is covered above. - if (args.size() == 1) { + if (args.size() == 1 || worldName.length() < 1) { this.showHelp(sender); return; } @@ -142,21 +147,25 @@ public void runCommand(CommandSender sender, List args) { return; } - if (worldFile.exists() && env != null) { - Command.broadcastCommandMessage(sender, String.format("Starting import of world '%s'...", worldName)); - if (this.worldManager.addWorld(worldName, environment, null, null, null, generator, useSpawnAdjust)) - Command.broadcastCommandMessage(sender, ChatColor.GREEN + "Complete!"); - else - Command.broadcastCommandMessage(sender, ChatColor.RED + "Failed!"); + if (!worldFile.exists()) { + sender.sendMessage(ChatColor.RED + "FAILED."); + String worldList = this.getPotentialWorlds(); + sender.sendMessage("That world folder does not exist. These look like worlds to me:"); + sender.sendMessage(worldList); + } else if (!checkIfIsWorld(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)); } else if (env == null) { sender.sendMessage(ChatColor.RED + "FAILED."); sender.sendMessage("That world environment did not exist."); sender.sendMessage("For a list of available world types, type: " + ChatColor.AQUA + "/mvenv"); } else { - sender.sendMessage(ChatColor.RED + "FAILED."); - String worldList = this.getPotentialWorlds(); - sender.sendMessage("That world folder does not exist. These look like worlds to me:"); - sender.sendMessage(worldList); + Command.broadcastCommandMessage(sender, String.format("Starting import of world '%s'...", worldName)); + if (this.worldManager.addWorld(worldName, environment, null, null, null, generator, useSpawnAdjust)) + Command.broadcastCommandMessage(sender, ChatColor.GREEN + "Complete!"); + else + Command.broadcastCommandMessage(sender, ChatColor.RED + "Failed!"); } } } diff --git a/src/main/java/com/onarandombox/MultiverseCore/commands/InfoCommand.java b/src/main/java/com/onarandombox/MultiverseCore/commands/InfoCommand.java index ed7eb415b..701424e70 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/commands/InfoCommand.java +++ b/src/main/java/com/onarandombox/MultiverseCore/commands/InfoCommand.java @@ -115,6 +115,7 @@ private List> buildEntireCommand(MultiverseWorld world, Player p 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 @@ private List> buildEntireCommand(MultiverseWorld world, Player p message = new ArrayList(); message.add(new FancyHeader("More World Settings", colors)); message.add(new FancyMessage("World Type: ", world.getWorldType().toString(), colors)); + message.add(new FancyMessage("Generator: ", world.getGenerator(), colors)); message.add(new FancyMessage("Structures: ", world.getCBWorld().canGenerateStructures() + "", colors)); message.add(new FancyMessage("Weather: ", world.isWeatherEnabled() + "", colors)); message.add(new FancyMessage("Players will get hungry: ", world.getHunger() + "", colors)); diff --git a/src/main/java/com/onarandombox/MultiverseCore/commands/ListCommand.java b/src/main/java/com/onarandombox/MultiverseCore/commands/ListCommand.java index e63ea4f2f..d702bf93e 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/commands/ListCommand.java +++ b/src/main/java/com/onarandombox/MultiverseCore/commands/ListCommand.java @@ -26,7 +26,7 @@ public class ListCommand extends PaginatedCoreCommand { public ListCommand(MultiverseCore plugin) { super(plugin); this.setName("World Listing"); - this.setCommandUsage("/mv list"); + this.setCommandUsage("/mv list [page]"); this.setArgRange(0, 2); this.addKey("mvlist"); this.addKey("mvl"); diff --git a/src/main/java/com/onarandombox/MultiverseCore/commands/ReloadCommand.java b/src/main/java/com/onarandombox/MultiverseCore/commands/ReloadCommand.java index 3481a4ea8..abe2b0a64 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/commands/ReloadCommand.java +++ b/src/main/java/com/onarandombox/MultiverseCore/commands/ReloadCommand.java @@ -27,6 +27,7 @@ public ReloadCommand(MultiverseCore plugin) { this.setCommandUsage("/mv reload"); this.setArgRange(0, 0); this.addKey("mvreload"); + this.addKey("mvr"); this.addKey("mv reload"); this.addCommandExample("/mv reload"); this.setPermission("multiverse.core.reload", "Reloads worlds.yml and config.yml.", PermissionDefault.OP); diff --git a/src/main/java/com/onarandombox/MultiverseCore/commands/ScriptCommand.java b/src/main/java/com/onarandombox/MultiverseCore/commands/ScriptCommand.java index 2896a8e70..665f6aa71 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/commands/ScriptCommand.java +++ b/src/main/java/com/onarandombox/MultiverseCore/commands/ScriptCommand.java @@ -35,6 +35,10 @@ public ScriptCommand(MultiverseCore plugin) { @Override public void runCommand(CommandSender sender, List args) { + if (plugin.getScriptAPI() == null) { + sender.sendMessage("Buscript failed to load while the server was starting. Scripts cannot be run."); + return; + } File file = new File(plugin.getScriptAPI().getScriptFolder(), args.get(0)); if (!file.exists()) { sender.sendMessage("That script file does not exist in the Multiverse-Core scripts directory!"); diff --git a/src/main/java/com/onarandombox/MultiverseCore/commands/SetSpawnCommand.java b/src/main/java/com/onarandombox/MultiverseCore/commands/SetSpawnCommand.java index ef39e043a..26dad442c 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/commands/SetSpawnCommand.java +++ b/src/main/java/com/onarandombox/MultiverseCore/commands/SetSpawnCommand.java @@ -18,6 +18,7 @@ import org.bukkit.permissions.PermissionDefault; import java.util.List; +import org.bukkit.Bukkit; /** * Sets the spawn for a world. @@ -27,8 +28,9 @@ public SetSpawnCommand(MultiverseCore plugin) { super(plugin); this.setName("Set World Spawn"); this.setCommandUsage("/mv setspawn"); - this.setArgRange(0, 0); + this.setArgRange(0, 6); this.addKey("mvsetspawn"); + this.addKey("mvsets"); this.addKey("mvss"); this.addKey("mv set spawn"); this.addKey("mv setspawn"); @@ -37,43 +39,110 @@ public SetSpawnCommand(MultiverseCore plugin) { this.setPermission("multiverse.core.spawn.set", "Sets the spawn for the current world.", PermissionDefault.OP); } + /** + * Dispatches the user's command depending on the number of parameters + * @param sender The player who executes the command, may be console as well. + * @param args Command line parameters + */ @Override public void runCommand(CommandSender sender, List args) { - setWorldSpawn(sender); + if (args.isEmpty()) { + setWorldSpawn(sender); + } else if (args.size() == 4) { + setWorldSpawn(sender, args.get(0), args.get(1), args.get(2), args.get(3)); + } else if (args.size() == 6) { + setWorldSpawn(sender, args.get(0), args.get(1), args.get(2), args.get(3), args.get(4), args.get(5)); + } else { + sender.sendMessage("Use no arguments for your current location, or world/x/y/z, or world/x/y/z/yaw/pitch!"); + } } /** - * Does the actual spawn-setting-work. - * - * @param sender The {@link CommandSender} that's setting the spawn. + * Set the world spawn when no parameters are given + * @param sender The {@link CommandSender} who executes the command. + * Everything not a {@link Player}, e.g. console, gets rejected, as we can't get coordinates from there. */ protected void setWorldSpawn(CommandSender sender) { if (sender instanceof Player) { Player p = (Player) sender; Location l = p.getLocation(); World w = p.getWorld(); - MultiverseWorld foundWorld = this.plugin.getMVWorldManager().getMVWorld(w.getName()); - if (foundWorld != null) { - foundWorld.setSpawnLocation(p.getLocation()); - BlockSafety bs = this.plugin.getBlockSafety(); - if (!bs.playerCanSpawnHereSafely(p.getLocation()) && foundWorld.getAdjustSpawn()) { - sender.sendMessage("It looks like that location would normally be unsafe. But I trust you."); - sender.sendMessage("I'm turning off the Safe-T-Teleporter for spawns to this world."); - sender.sendMessage("If you want this turned back on just do:"); - sender.sendMessage(ChatColor.AQUA + "/mvm set adjustspawn true " + foundWorld.getAlias()); - foundWorld.setAdjustSpawn(false); - } - sender.sendMessage("Spawn was set to: " + plugin.getLocationManipulation().strCoords(p.getLocation())); - if (!plugin.saveWorldConfig()) { - sender.sendMessage(ChatColor.RED + "There was an issue saving worlds.yml! Your changes will only be temporary!"); - } - } else { - w.setSpawnLocation(l.getBlockX(), l.getBlockY(), l.getBlockZ()); - sender.sendMessage("Multiverse does not know about this world, only X,Y and Z set. Please import it to set the spawn fully (Pitch/Yaws)."); - } + setWorldSpawn(sender, w, l); + } else { + sender.sendMessage("You need to give coordinates to use this command from the console!"); + } + } + + /** + * Set the world spawn when 4 parameters are given + * @param sender The {@link CommandSender} who executes the command + * @param world The world to set the spawn in + * @param x X-coordinate to set the spawn to (as a {@link String} as it's from the command line, gets parsed into a double) + * @param y Y-coordinate to set the spawn to (as a {@link String} as it's from the command line, gets parsed into a double) + * @param z Z-coordinate to set the spawn to (as a {@link String} as it's from the command line, gets parsed into a double) + */ + protected void setWorldSpawn(CommandSender sender, String world, String x, String y, String z) { + setWorldSpawn(sender, world, x, y, z, "0", "0"); + } + /** + * Set the world spawn when 6 parameters are given + * @param sender The {@link CommandSender} who executes the command + * @param world The world to set the spawn in + * @param x X-coordinate to set the spawn to (as a {@link String} as it's from the command line, gets parsed into a double) + * @param y Y-coordinate to set the spawn to (as a {@link String} as it's from the command line, gets parsed into a double) + * @param z Z-coordinate to set the spawn to (as a {@link String} as it's from the command line, gets parsed into a double) + * @param yaw Yaw a newly spawned player should look at (as a {@link String} as it's from the command line, gets parsed into a float) + * @param pitch Pitch a newly spawned player should look at (as a {@link String} as it's from the command line, gets parsed into a float) + */ + protected void setWorldSpawn(CommandSender sender, String world, String x, String y, String z, String yaw, String pitch) { + double dx, dy, dz; + float fpitch, fyaw; + World bukkitWorld = Bukkit.getWorld(world); + if (bukkitWorld == null) { + sender.sendMessage("World " + world + " is unknown!"); + return; + } + try { + dx = Double.parseDouble(x); + dy = Double.parseDouble(y); + dz = Double.parseDouble(z); + fpitch = Float.parseFloat(pitch); + fyaw = Float.parseFloat(yaw); + } catch (NumberFormatException ex) { + sender.sendMessage("All coordinates must be numeric"); + return; + } + Location l = new Location(bukkitWorld, dx, dy, dz, fyaw, fpitch); + setWorldSpawn(sender, bukkitWorld, l); + } + + /** + * Does the actual spawn-setting-work. + * + * @param sender The {@link CommandSender} that's setting the spawn. + * @param w The {@link World} to set the spawn in + * @param l The {@link Location} to set the spawn to + */ + private void setWorldSpawn(CommandSender sender, World w, Location l) { + MultiverseWorld foundWorld = this.plugin.getMVWorldManager().getMVWorld(w.getName()); + if (foundWorld != null) { + foundWorld.setSpawnLocation(l); + BlockSafety bs = this.plugin.getBlockSafety(); + if (!bs.playerCanSpawnHereSafely(l) && foundWorld.getAdjustSpawn()) { + sender.sendMessage("It looks like that location would normally be unsafe. But I trust you."); + sender.sendMessage("I'm turning off the Safe-T-Teleporter for spawns to this world."); + sender.sendMessage("If you want this turned back on just do:"); + sender.sendMessage(ChatColor.AQUA + "/mvm set adjustspawn true " + foundWorld.getAlias()); + foundWorld.setAdjustSpawn(false); + } + sender.sendMessage("Spawn was set to: " + plugin.getLocationManipulation().strCoords(l)); + if (!plugin.saveWorldConfig()) { + sender.sendMessage(ChatColor.RED + "There was an issue saving worlds.yml! Your changes will only be temporary!"); + } } else { - sender.sendMessage("You cannot use this command from the console."); + w.setSpawnLocation(l.getBlockX(), l.getBlockY(), l.getBlockZ()); + sender.sendMessage("Multiverse does not know about this world, only X,Y and Z set. Please import it to set the spawn fully (Pitch/Yaws)."); } } } diff --git a/src/main/java/com/onarandombox/MultiverseCore/commands/SpoutCommand.java b/src/main/java/com/onarandombox/MultiverseCore/commands/SpoutCommand.java deleted file mode 100644 index 3683d9375..000000000 --- a/src/main/java/com/onarandombox/MultiverseCore/commands/SpoutCommand.java +++ /dev/null @@ -1,66 +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.ChatColor; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; -import org.bukkit.permissions.PermissionDefault; -import org.getspout.spoutapi.gui.GenericButton; -import org.getspout.spoutapi.gui.GenericPopup; -import org.getspout.spoutapi.gui.PopupScreen; -import org.getspout.spoutapi.player.SpoutPlayer; - -import java.util.List; - -/** - * Edit a world with spout. - * / -public class SpoutCommand extends MultiverseCommand { - - public SpoutCommand(MultiverseCore plugin) { - super(plugin); - this.setName("Edit World with Spout"); - this.setCommandUsage("/mv spout"); - this.setArgRange(0, 0); - this.addKey("mv spout"); - this.setPermission("multiverse.core.spout", "Edit a world with spout.", PermissionDefault.OP); - this.addCommandExample("/mv spout"); - } - - @Override - public void runCommand(CommandSender sender, List args) { - if (!(sender instanceof Player)) { - sender.sendMessage(ChatColor.RED + "This command must be run as a player!"); - return; - } - if (plugin.getSpout() == null) { - sender.sendMessage(ChatColor.RED + "You need spout installed on this server to use it with Multiverse!"); - return; - } - SpoutPlayer p = (SpoutPlayer) sender; - if (!p.isSpoutCraftEnabled()) { - sender.sendMessage(ChatColor.RED + p.getName() + "You need to be using the Spoutcraft client to run this command!"); - return; - } - PopupScreen pop = new GenericPopup(); - GenericButton button = new GenericButton("Fish"); - // TO-DO maybe use constants for these - // BEGIN CHECKSTYLE-SUPPRESSION: MagicNumberCheck - button.setX(50); - button.setY(50); - button.setWidth(100); - button.setHeight(40); - // END CHECKSTYLE-SUPPRESSION: MagicNumberCheck - pop.attachWidget(this.plugin, button); - sender.sendMessage(ChatColor.GREEN + "YAY!"); - p.getMainScreen().attachPopupScreen(pop); - } -} -*/ diff --git a/src/main/java/com/onarandombox/MultiverseCore/commands/TeleportCommand.java b/src/main/java/com/onarandombox/MultiverseCore/commands/TeleportCommand.java index 847aa6d47..6f8264f49 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/commands/TeleportCommand.java +++ b/src/main/java/com/onarandombox/MultiverseCore/commands/TeleportCommand.java @@ -40,7 +40,7 @@ public TeleportCommand(MultiverseCore plugin) { 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"); diff --git a/src/main/java/com/onarandombox/MultiverseCore/commands/VersionCommand.java b/src/main/java/com/onarandombox/MultiverseCore/commands/VersionCommand.java index f529c1591..403226bff 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/commands/VersionCommand.java +++ b/src/main/java/com/onarandombox/MultiverseCore/commands/VersionCommand.java @@ -10,20 +10,23 @@ import com.dumptruckman.minecraft.util.Logging; import com.onarandombox.MultiverseCore.MultiverseCore; import com.onarandombox.MultiverseCore.event.MVVersionEvent; -import com.onarandombox.MultiverseCore.utils.webpaste.BitlyURLShortener; import com.onarandombox.MultiverseCore.utils.webpaste.PasteFailedException; import com.onarandombox.MultiverseCore.utils.webpaste.PasteService; import com.onarandombox.MultiverseCore.utils.webpaste.PasteServiceFactory; import com.onarandombox.MultiverseCore.utils.webpaste.PasteServiceType; import com.onarandombox.MultiverseCore.utils.webpaste.URLShortener; +import com.onarandombox.MultiverseCore.utils.webpaste.URLShortenerFactory; +import com.onarandombox.MultiverseCore.utils.webpaste.URLShortenerType; +import com.pneumaticraft.commandhandler.CommandHandler; +import org.apache.commons.lang.StringUtils; import org.bukkit.ChatColor; 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 java.io.*; -import java.util.HashMap; +import java.io.File; import java.util.List; import java.util.Map; @@ -31,110 +34,91 @@ * Dumps version info to the console. */ public class VersionCommand extends MultiverseCommand { - private static final URLShortener SHORTENER = new BitlyURLShortener(); + private static final URLShortener SHORTENER = URLShortenerFactory.getService(URLShortenerType.BITLY); public VersionCommand(MultiverseCore plugin) { super(plugin); this.setName("Multiverse Version"); - this.setCommandUsage("/mv version " + ChatColor.GOLD + "-[pbg]"); - this.setArgRange(0, 1); + this.setCommandUsage("/mv version " + ChatColor.GOLD + "[-b|-h|-p] [--include-plugin-list]"); + this.setArgRange(0, 2); this.addKey("mv version"); + this.addKey("mvver"); this.addKey("mvv"); this.addKey("mvversion"); this.setPermission("multiverse.core.version", - "Dumps version info to the console, optionally to pastie.org with -p or pastebin.com with a -b.", PermissionDefault.TRUE); + "Dumps version info to the console, optionally to pastebin.com with -b, to hastebin.com using -h, or to paste.gg with -p.", 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(); + return "[Multiverse-Core] Multiverse-Core Version: " + this.plugin.getDescription().getVersion() + '\n' + + "[Multiverse-Core] Bukkit Version: " + this.plugin.getServer().getVersion() + '\n' + + "[Multiverse-Core] Loaded Worlds: " + this.plugin.getMVWorldManager().getMVWorlds() + '\n' + + "[Multiverse-Core] Multiverse Plugins Loaded: " + this.plugin.getPluginCount() + '\n' + +"[Multiverse-Core] Economy being used: " + plugin.getEconomist().getEconomyName() + '\n' + + "[Multiverse-Core] Permissions Plugin: " + this.plugin.getMVPerms().getType() + '\n' + + "[Multiverse-Core] Dumping Config Values: (version " + this.plugin.getMVConfig().getVersion() + ")" + '\n' + + "[Multiverse-Core] enforceaccess: " + plugin.getMVConfig().getEnforceAccess() + '\n' + + "[Multiverse-Core] prefixchat: " + plugin.getMVConfig().getPrefixChat() + '\n' + + "[Multiverse-Core] prefixchatformat: " + plugin.getMVConfig().getPrefixChatFormat() + '\n' + + "[Multiverse-Core] useasyncchat: " + plugin.getMVConfig().getUseAsyncChat() + '\n' + + "[Multiverse-Core] teleportintercept: " + plugin.getMVConfig().getTeleportIntercept() + '\n' + + "[Multiverse-Core] firstspawnoverride: " + plugin.getMVConfig().getFirstSpawnOverride() + '\n' + + "[Multiverse-Core] displaypermerrors: " + plugin.getMVConfig().getDisplayPermErrors() + '\n' + + "[Multiverse-Core] globaldebug: " + plugin.getMVConfig().getGlobalDebug() + '\n' + + "[Multiverse-Core] silentstart: " + plugin.getMVConfig().getSilentStart() + '\n' + + "[Multiverse-Core] messagecooldown: " + plugin.getMessaging().getCooldown() + '\n' + + "[Multiverse-Core] version: " + plugin.getMVConfig().getVersion() + '\n' + + "[Multiverse-Core] firstspawnworld: " + plugin.getMVConfig().getFirstSpawnWorld() + '\n' + + "[Multiverse-Core] teleportcooldown: " + plugin.getMVConfig().getTeleportCooldown() + '\n' + + "[Multiverse-Core] defaultportalsearch: " + plugin.getMVConfig().isUsingDefaultPortalSearch() + '\n' + + "[Multiverse-Core] portalsearchradius: " + plugin.getMVConfig().getPortalSearchRadius() + '\n' + + "[Multiverse-Core] autopurge: " + plugin.getMVConfig().isAutoPurgeEnabled() + '\n' + + "[Multiverse-Core] Special Code: FRN002" + '\n'; } private String getMarkdownString() { - StringBuilder markdownString = new StringBuilder(); - markdownString.append("# Multiverse-Core\n"); - markdownString.append("## Overview\n"); - markdownString.append("| Name | Value |\n"); - markdownString.append("| --- | --- |\n"); - markdownString.append("| Multiverse-Core Version | `").append(this.plugin.getDescription().getVersion()).append("` |\n"); - markdownString.append("| Bukkit Version | `").append(this.plugin.getServer().getVersion()).append("` |\n"); - //markdownString.append("| Loaded Worlds | `").append(this.plugin.getMVWorldManager().getMVWorlds()).append("` |\n"); - markdownString.append("| Multiverse Plugins Loaded | `").append(this.plugin.getPluginCount()).append("` |\n"); - markdownString.append("| Economy being used | `").append(plugin.getEconomist().getEconomyName()).append("` |\n"); - markdownString.append("| Permissions Plugin | `").append(this.plugin.getMVPerms().getType()).append("` |\n"); - markdownString.append("## Parsed Config\n"); - markdownString.append("These are what Multiverse thought the in-memory values of the config were.\n\n"); - markdownString.append("| Config Key | Value |\n"); - markdownString.append("| --- | --- |\n"); - markdownString.append("| version | `").append(this.plugin.getMVConfig().getVersion()).append("` |\n"); - markdownString.append("| messagecooldown | `").append(plugin.getMessaging().getCooldown()).append("` |\n"); - markdownString.append("| teleportcooldown | `").append(plugin.getMVConfig().getTeleportCooldown()).append("` |\n"); - markdownString.append("| worldnameprefix | `").append(plugin.getMVConfig().getPrefixChat()).append("` |\n"); - markdownString.append("| worldnameprefixFormat | `").append(plugin.getMVConfig().getPrefixChatFormat()).append("` |\n"); - markdownString.append("| enforceaccess | `").append(plugin.getMVConfig().getEnforceAccess()).append("` |\n"); - markdownString.append("| displaypermerrors | `").append(plugin.getMVConfig().getDisplayPermErrors()).append("` |\n"); - markdownString.append("| teleportintercept | `").append(plugin.getMVConfig().getTeleportIntercept()).append("` |\n"); - markdownString.append("| firstspawnoverride | `").append(plugin.getMVConfig().getFirstSpawnOverride()).append("` |\n"); - markdownString.append("| firstspawnworld | `").append(plugin.getMVConfig().getFirstSpawnWorld()).append("` |\n"); - markdownString.append("| debug | `").append(plugin.getMVConfig().getGlobalDebug()).append("` |\n"); - return markdownString.toString(); + return "# Multiverse-Core" + '\n' + + "## Overview" + '\n' + + "| Name | Value |" + '\n' + + "| --- | --- |" + '\n' + + "| Multiverse-Core Version | `" + this.plugin.getDescription().getVersion() + "` |" + '\n' + + "| Bukkit Version | `" + this.plugin.getServer().getVersion() + "` |" + '\n' + + "| Loaded Worlds | `" + this.plugin.getMVWorldManager().getMVWorlds() + "` |" + '\n' + + "| Multiverse Plugins Loaded | `" + this.plugin.getPluginCount() + "` |" + '\n' + + "| Economy being used | `" + plugin.getEconomist().getEconomyName() + "` |" + '\n' + + "| Permissions Plugin | `" + this.plugin.getMVPerms().getType() + "` |" + '\n' + + "## Parsed Config" + '\n' + + "These are what Multiverse thought the in-memory values of the config were." + "\n\n" + + "| Config Key | Value |" + '\n' + + "| --- | --- |" + '\n' + + "| version | `" + this.plugin.getMVConfig().getVersion() + "` |" + '\n' + + "| messagecooldown | `" + plugin.getMessaging().getCooldown() + "` |" + '\n' + + "| teleportcooldown | `" + plugin.getMVConfig().getTeleportCooldown() + "` |" + '\n' + + "| worldnameprefix | `" + plugin.getMVConfig().getPrefixChat() + "` |" + '\n' + + "| worldnameprefixFormat | `" + plugin.getMVConfig().getPrefixChatFormat() + "` |" + '\n' + + "| enforceaccess | `" + plugin.getMVConfig().getEnforceAccess() + "` |" + '\n' + + "| displaypermerrors | `" + plugin.getMVConfig().getDisplayPermErrors() + "` |" + '\n' + + "| teleportintercept | `" + plugin.getMVConfig().getTeleportIntercept() + "` |" + '\n' + + "| firstspawnoverride | `" + plugin.getMVConfig().getFirstSpawnOverride() + "` |" + '\n' + + "| firstspawnworld | `" + plugin.getMVConfig().getFirstSpawnWorld() + "` |" + '\n' + + "| debug | `" + plugin.getMVConfig().getGlobalDebug() + "` |" + '\n'; } - private String readFile(final String filename) { - String result; - try { - FileReader reader = new FileReader(filename); - BufferedReader bufferedReader = new BufferedReader(reader); - String line; - result = ""; - while ((line = bufferedReader.readLine()) != null) { - result += line + '\n'; - } - } catch (FileNotFoundException e) { - Logging.severe("Unable to find %s. Here's the traceback: %s", filename, e.getMessage()); - e.printStackTrace(); - result = String.format("ERROR: Could not load: %s", filename); - } catch (IOException e) { - Logging.severe("Something bad happend when reading %s. Here's the traceback: %s", filename, e.getMessage()); - e.printStackTrace(); - result = String.format("ERROR: Could not load: %s", filename); - } - return result; - } - - private Map getVersionFiles() { - Map files = new HashMap(); + 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 - files.put("version.md", this.getMarkdownString()); + // add the legacy file, but as markdown so it's readable + // TODO Readd this in 5.0.0 + // event.putDetailedVersionInfo("version.md", this.getMarkdownString()); - // Add the config.yml + // add config.yml File configFile = new File(this.plugin.getDataFolder(), "config.yml"); - files.put(configFile.getName(), this.readFile(configFile.getAbsolutePath())); + event.putDetailedVersionInfo("multiverse-core/config.yml", configFile); - // Add the config.yml - File worldConfig = new File(this.plugin.getDataFolder(), "worlds.yml"); - files.put(worldConfig.getName(), this.readFile(worldConfig.getAbsolutePath())); - return files; + // add worlds.yml + File worldsFile = new File(this.plugin.getDataFolder(), "worlds.yml"); + event.putDetailedVersionInfo("multiverse-core/worlds.yml", worldsFile); } @Override @@ -144,36 +128,53 @@ public void runCommand(final CommandSender sender, final List args) { sender.sendMessage("Version info dumped to console. Please check your server logs."); } - MVVersionEvent versionEvent = new MVVersionEvent(this.getLegacyString(), this.getVersionFiles()); - final Map files = this.getVersionFiles(); + MVVersionEvent versionEvent = new MVVersionEvent(); + + this.addVersionInfoToEvent(versionEvent); this.plugin.getServer().getPluginManager().callEvent(versionEvent); + 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 - final String data = versionEvent.getVersionInfo(); - String[] lines = data.split("\n"); + String[] lines = versionInfo.split("\\r?\\n"); for (String line : lines) { - Logging.info(line); + if (!line.isEmpty()) { + this.plugin.getServer().getLogger().info(line); + } } BukkitRunnable logPoster = new BukkitRunnable() { @Override public void run() { - if (args.size() == 1) { + if (args.size() > 0) { String pasteUrl; - if (args.get(0).equalsIgnoreCase("-p")) { - // private post to pastie - pasteUrl = postToService(PasteServiceType.PASTIE, true, data, files); - } else if (args.get(0).equalsIgnoreCase("-b")) { + if (CommandHandler.hasFlag("-b", args)) { // private post to pastebin - pasteUrl = postToService(PasteServiceType.PASTEBIN, true, data, files); - } else if (args.get(0).equalsIgnoreCase("-g")) { + pasteUrl = postToService(PasteServiceType.PASTEBIN, true, versionInfo, files); + } else if (CommandHandler.hasFlag("-g", args)) { // private post to github - pasteUrl = postToService(PasteServiceType.GITHUB, true, data, files); + 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; } - sender.sendMessage("Version info dumped here: " + ChatColor.GREEN + pasteUrl); + if (!(sender instanceof ConsoleCommandSender)) { + sender.sendMessage("Version info dumped here: " + ChatColor.GREEN + pasteUrl); + } Logging.info("Version info dumped here: %s", pasteUrl); } } @@ -192,20 +193,29 @@ public void run() { * @param pasteFiles Map of filenames/contents of debug info. * @return URL of visible paste */ - private static String postToService(PasteServiceType type, boolean isPrivate, String pasteData, - Map pasteFiles) { + private static String postToService(PasteServiceType type, boolean isPrivate, String pasteData, Map pasteFiles) { PasteService ps = PasteServiceFactory.getService(type, isPrivate); + try { String result; if (ps.supportsMultiFile()) { - result = ps.postData(ps.encodeData(pasteFiles), ps.getPostURL()); + result = ps.postData(pasteFiles); } else { - result = ps.postData(ps.encodeData(pasteData), ps.getPostURL()); + result = ps.postData(pasteData); } - return SHORTENER.shorten(result); + + if (SHORTENER != null) return SHORTENER.shorten(result); + return result; } catch (PasteFailedException e) { - System.out.print(e); - return "Error posting to service"; + e.printStackTrace(); + return "Error posting to service."; + } catch (NullPointerException e) { + e.printStackTrace(); + return "That service isn't supported yet."; } } + + private String getPluginList() { + return StringUtils.join(plugin.getServer().getPluginManager().getPlugins(), ", "); + } } diff --git a/src/main/java/com/onarandombox/MultiverseCore/configuration/EntryFee.java b/src/main/java/com/onarandombox/MultiverseCore/configuration/EntryFee.java index e754e6820..be66fb3ad 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/configuration/EntryFee.java +++ b/src/main/java/com/onarandombox/MultiverseCore/configuration/EntryFee.java @@ -2,10 +2,14 @@ import java.util.Map; +import com.onarandombox.MultiverseCore.utils.MaterialConverter; import me.main__.util.SerializationConfig.Property; import me.main__.util.SerializationConfig.SerializationConfig; +import me.main__.util.SerializationConfig.Serializor; +import org.bukkit.Material; import org.bukkit.configuration.serialization.SerializableAs; +import org.jetbrains.annotations.Nullable; /** * Entryfee-settings. @@ -14,8 +18,9 @@ public class EntryFee extends SerializationConfig { @Property private double amount; - @Property - private int currency; + @Property(serializor = EntryFeeCurrencySerializor.class) + @Nullable + private Material currency; public EntryFee() { super(); @@ -31,7 +36,7 @@ public EntryFee(Map values) { @Override protected void setDefaults() { amount = 0D; - currency = -1; + currency = null; } /** @@ -44,7 +49,8 @@ public double getAmount() { /** * @return the currency */ - public int getCurrency() { + @Nullable + public Material getCurrency() { return currency; } @@ -60,7 +66,19 @@ public void setAmount(double amount) { * Sets the currency. * @param currency The new value. */ - public void setCurrency(int currency) { + public void setCurrency(@Nullable Material currency) { this.currency = currency; } + + public static final class EntryFeeCurrencySerializor implements Serializor { + @Override + public String serialize(Material material) { + return material.toString(); + } + + @Override + public Material deserialize(Object o, Class aClass) { + return MaterialConverter.convertTypeString(o.toString()); + } + } } diff --git a/src/main/java/com/onarandombox/MultiverseCore/configuration/MVActiveConfigProperty.java b/src/main/java/com/onarandombox/MultiverseCore/configuration/MVActiveConfigProperty.java deleted file mode 100644 index 403d0026d..000000000 --- a/src/main/java/com/onarandombox/MultiverseCore/configuration/MVActiveConfigProperty.java +++ /dev/null @@ -1,37 +0,0 @@ -/****************************************************************************** - * Multiverse 2 Copyright (c) the Multiverse Team 2012. * - * 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.configuration; - -/** - * An "active" {@link MVConfigProperty} that uses the specified method to be "actually" set. - * @param The type of the config-property. - * @deprecated This is deprecated. - * @see MVConfigProperty - */ -@Deprecated -public interface MVActiveConfigProperty extends MVConfigProperty { - /** - * Gets the method that will be executed. - * - * @return The name of the method in MVWorld to be called. - */ - String getMethod(); - - /** - * Sets the method that will be executed. - * - * @param methodName The name of the method in MVWorld to be called. - */ - void setMethod(String methodName); - - /** - * Returns the class of the object we're looking at. - * @return the class of the object we're looking at. - */ - Class getPropertyClass(); -} diff --git a/src/main/java/com/onarandombox/MultiverseCore/configuration/MVConfigProperty.java b/src/main/java/com/onarandombox/MultiverseCore/configuration/MVConfigProperty.java deleted file mode 100644 index 537a46b57..000000000 --- a/src/main/java/com/onarandombox/MultiverseCore/configuration/MVConfigProperty.java +++ /dev/null @@ -1,69 +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.configuration; - -/** - * A generic config-property. - * - * @param The type of the config-property. - * @deprecated This is deprecated. - */ -@Deprecated -public interface MVConfigProperty { - /** - * Gets the name of this property. - * - * @return The name of this property. - */ - String getName(); - - /** - * Gets the value of this property. - * - * @return The value of this property. - */ - T getValue(); - - /** - * Gets the string representation of this value. - * - * @return The value of this property as a string. - */ - String toString(); - - /** - * Gets the help string for this. - * - * @return The value of this property as a string. - */ - String getHelp(); - - /** - * Sets the value of this property. - * - * @param value The T representation of this value. - * @return True the value was successfully set. - */ - boolean setValue(T value); - - /** - * This parseValue should be used with strings. - * - * @param value The string representation of the value to set. - * - * @return True if the value was set, false if not. - */ - boolean parseValue(String value); - - /** - * Gets the name of the config-node that this {@link MVConfigProperty} is saved as. - * - * @return The name of the config-node that this {@link MVConfigProperty} is saved as. - */ - String getConfigNode(); -} diff --git a/src/main/java/com/onarandombox/MultiverseCore/destination/ExactDestination.java b/src/main/java/com/onarandombox/MultiverseCore/destination/ExactDestination.java index fe46bbea7..081628933 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/destination/ExactDestination.java +++ b/src/main/java/com/onarandombox/MultiverseCore/destination/ExactDestination.java @@ -21,9 +21,10 @@ * An exact {@link MVDestination}. */ public class ExactDestination implements MVDestination { - private final String coordRegex = "(-?[\\d]+\\.?[\\d]*),(-?[\\d]+\\.?[\\d]*),(-?[\\d]+\\.?[\\d]*)"; + private final String coordRegex = "(-?[\\d]+\\.?[\\d]*|~-?[\\d]+\\.?[\\d]*|~),(-?[\\d]+\\.?[\\d]*|~-?[\\d]+\\.?[\\d]*|~),(-?[\\d]+\\.?[\\d]*|~-?[\\d]+\\.?[\\d]*|~)"; private boolean isValid; private Location location; + private boolean relativeX, relativeY, relativeZ; /** * {@inheritDoc} @@ -88,7 +89,20 @@ public boolean isThisType(JavaPlugin plugin, String destination) { */ @Override public Location getLocation(Entity e) { - return this.location; + Location loc = this.location.clone(); + if (relativeX || relativeY || relativeZ) { + Location eLoc = e.getLocation(); + loc.add(relativeX ? eLoc.getX() : 0, relativeY ? eLoc.getY() : 0, relativeZ ? eLoc.getZ() : 0); + // Since the location is relative, it makes sense to use the entity's pitch and yaw unless those were + // specified in the destination. + if (loc.getPitch() == 0) { + loc.setPitch(eLoc.getPitch()); + } + if (loc.getYaw() == 0) { + loc.setYaw(eLoc.getYaw()); + } + } + return loc; } /** @@ -134,11 +148,42 @@ public void setDestination(JavaPlugin plugin, String destination) { double[] coords = new double[3]; String[] coordString = parsed.get(2).split(","); for (int i = 0; i < 3; i++) { - try { - coords[i] = Double.parseDouble(coordString[i]); - } catch (NumberFormatException e) { - this.isValid = false; - return; + String[] relSplit = coordString[i].split("~"); + boolean relative = false; + if (relSplit.length == 0) { + // coord is "~" form + relative = true; + coords[i] = 0; + } else if (relSplit.length == 1) { + // coord is "123" form + try { + coords[i] = Double.parseDouble(relSplit[0]); + } catch (NumberFormatException e) { + this.isValid = false; + return; + } + } else { + // coord is "~123" form + relative = true; + try { + coords[i] = Double.parseDouble(relSplit[1]); + } catch (NumberFormatException e) { + this.isValid = false; + return; + } + } + if (relative) { + switch (i) { + case 0: + relativeX = true; + break; + case 1: + relativeY = true; + break; + case 2: + relativeZ = true; + break; + } } } this.location.setX(coords[0]); diff --git a/src/main/java/com/onarandombox/MultiverseCore/event/MVDebugModeEvent.java b/src/main/java/com/onarandombox/MultiverseCore/event/MVDebugModeEvent.java new file mode 100644 index 000000000..f3c3408b7 --- /dev/null +++ b/src/main/java/com/onarandombox/MultiverseCore/event/MVDebugModeEvent.java @@ -0,0 +1,43 @@ +package com.onarandombox.MultiverseCore.event; + +import org.bukkit.event.Event; +import org.bukkit.event.HandlerList; + +/** + * Called when Core's debug level is changed. + */ +public class MVDebugModeEvent extends Event { + + private static final HandlerList HANDLERS = new HandlerList(); + + private final int level; + + public MVDebugModeEvent(int level) { + this.level = level; + } + + /** + * {@inheritDoc} + */ + @Override + public HandlerList getHandlers() { + return HANDLERS; + } + + /** + * Gets the handler list. This is required by the event system. + * @return A list of HANDLERS. + */ + public static HandlerList getHandlerList() { + return HANDLERS; + } + + /** + * Returns the current debug level of Core. + * + * @return the current debug level of Core. + */ + public int getLevel() { + return level; + } +} diff --git a/src/main/java/com/onarandombox/MultiverseCore/event/MVVersionEvent.java b/src/main/java/com/onarandombox/MultiverseCore/event/MVVersionEvent.java index 43c1ddfc1..9b1bc6326 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 String getVersionInfo() { * * 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 Map getDetailedVersionInfo() { 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/MVMapListener.java b/src/main/java/com/onarandombox/MultiverseCore/listeners/MVMapListener.java index fe039a6c3..2443337a3 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/listeners/MVMapListener.java +++ b/src/main/java/com/onarandombox/MultiverseCore/listeners/MVMapListener.java @@ -29,7 +29,7 @@ public MVMapListener(final MultiverseCore plugin) { public void mapInitialize(final MapInitializeEvent event) { for (final Player player : Bukkit.getOnlinePlayers()) { if ((player.getItemInHand().getType() == Material.MAP - || player.getItemInHand().getType() == Material.EMPTY_MAP) + || player.getItemInHand().getType() == Material.FILLED_MAP) && player.getItemInHand().getDurability() == event.getMap().getId()) { final Location playerLoc = player.getLocation(); final MapView map = event.getMap(); diff --git a/src/main/java/com/onarandombox/MultiverseCore/listeners/MVPlayerListener.java b/src/main/java/com/onarandombox/MultiverseCore/listeners/MVPlayerListener.java index 190e494f0..40c1f51b1 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/listeners/MVPlayerListener.java +++ b/src/main/java/com/onarandombox/MultiverseCore/listeners/MVPlayerListener.java @@ -7,6 +7,10 @@ package com.onarandombox.MultiverseCore.listeners; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; +import java.util.logging.Level; + import com.dumptruckman.minecraft.util.Logging; import com.onarandombox.MultiverseCore.MultiverseCore; import com.onarandombox.MultiverseCore.api.MVWorldManager; @@ -29,10 +33,6 @@ import org.bukkit.event.player.PlayerRespawnEvent; import org.bukkit.event.player.PlayerTeleportEvent; -import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; -import java.util.logging.Level; - /** * Multiverse's {@link Listener} for players. */ @@ -252,7 +252,7 @@ public void playerPortalCheck(PlayerPortalEvent event) { // REMEMBER! getTo MAY be NULL HERE!!! // If the player was actually outside of the portal, adjust the from location - if (event.getFrom().getWorld().getBlockAt(event.getFrom()).getType() != Material.PORTAL) { + if (event.getFrom().getWorld().getBlockAt(event.getFrom()).getType() != Material.NETHER_PORTAL) { Location newloc = this.plugin.getSafeTTeleporter().findPortalBlockNextTo(event.getFrom()); // TODO: Fix this. Currently, we only check for PORTAL blocks. I'll have to figure out what // TODO: we want to do here. @@ -304,8 +304,16 @@ public void playerPortal(PlayerPortalEvent event) { + "' was allowed to go to '" + event.getTo().getWorld().getName() + "' because enforceaccess is off."); } - if (!plugin.getMVConfig().isUsingDefaultPortalSearch() && event.getPortalTravelAgent() != null) { - event.getPortalTravelAgent().setSearchRadius(plugin.getMVConfig().getPortalSearchRadius()); + 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()); + } + } } diff --git a/src/main/java/com/onarandombox/MultiverseCore/listeners/MVPortalListener.java b/src/main/java/com/onarandombox/MultiverseCore/listeners/MVPortalListener.java index 75b64df69..31d6f4693 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/listeners/MVPortalListener.java +++ b/src/main/java/com/onarandombox/MultiverseCore/listeners/MVPortalListener.java @@ -71,10 +71,10 @@ public void portalForm(PlayerInteractEvent event) { if (event.getAction() != Action.RIGHT_CLICK_BLOCK) { return; } - if (event.getClickedBlock().getType() != Material.ENDER_PORTAL_FRAME) { + if (event.getClickedBlock().getType() != Material.END_PORTAL_FRAME) { return; } - if (event.getItem() == null || event.getItem().getType() != Material.EYE_OF_ENDER) { + if (event.getItem() == null || event.getItem().getType() != Material.ENDER_EYE) { return; } MultiverseWorld world = this.plugin.getMVWorldManager().getMVWorld(event.getPlayer().getWorld()); diff --git a/src/main/java/com/onarandombox/MultiverseCore/utils/BlockSafety.java b/src/main/java/com/onarandombox/MultiverseCore/utils/BlockSafety.java deleted file mode 100644 index 562e70d05..000000000 --- a/src/main/java/com/onarandombox/MultiverseCore/utils/BlockSafety.java +++ /dev/null @@ -1,281 +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.dumptruckman.minecraft.util.Logging; -import org.bukkit.Location; -import org.bukkit.Material; -import org.bukkit.World; -import org.bukkit.entity.Minecart; -import org.bukkit.entity.Vehicle; - -/** - * Used to determine block/location-related facts. - * - * @deprecated Use instead: {@link com.onarandombox.MultiverseCore.api.BlockSafety} and {@link SimpleBlockSafety}. - */ -@Deprecated -public class BlockSafety { - - /** - * This function checks whether the block at the given coordinates are above air or not. - * @param l The {@link Location} of the block. - * @return True if the block at that {@link Location} is above air. - */ - public boolean isBlockAboveAir(Location l) { - Location downOne = l.clone(); - downOne.setY(downOne.getY() - 1); - return (downOne.getBlock().getType() == Material.AIR); - } - - // TODO maybe remove this? - private boolean blockIsNotSafe(World world, double x, double y, double z) { - return !playerCanSpawnHereSafely(world, x, y, z); - } - - /** - * Checks if a player can spawn safely at the given coordinates. - * @param world The {@link World}. - * @param x The x-coordinate. - * @param y The y-coordinate. - * @param z The z-coordinate. - * @return True if a player can spawn safely at the given coordinates. - */ - public boolean playerCanSpawnHereSafely(World world, double x, double y, double z) { - Location l = new Location(world, x, y, z); - return playerCanSpawnHereSafely(l); - } - - /** - * This function checks whether the block at the coordinates given is safe or not by checking for Lava/Fire/Air - * etc. This also ensures there is enough space for a player to spawn! - * - * @param l The {@link Location} - * @return Whether the player can spawn safely at the given {@link Location} - */ - public boolean playerCanSpawnHereSafely(Location l) { - if (l == null) { - // Can't safely spawn at a null location! - return false; - } - - World world = l.getWorld(); - Location actual = l.clone(); - Location upOne = l.clone(); - Location downOne = l.clone(); - upOne.setY(upOne.getY() + 1); - downOne.setY(downOne.getY() - 1); - - if (this.isSolidBlock(world.getBlockAt(actual).getType()) - || this.isSolidBlock(upOne.getBlock().getType())) { - Logging.finest("Error Here (Actual)? (%s)[%s]", actual.getBlock().getType(), - this.isSolidBlock(actual.getBlock().getType())); - Logging.finest("Error Here (upOne)? (%s)[%s]", upOne.getBlock().getType(), - this.isSolidBlock(upOne.getBlock().getType())); - return false; - } - - if (downOne.getBlock().getType() == Material.LAVA || downOne.getBlock().getType() == Material.STATIONARY_LAVA) { - Logging.finest("Error Here (downOne)? (%s)[%s]", downOne.getBlock().getType(), - this.isSolidBlock(downOne.getBlock().getType())); - return false; - } - - if (downOne.getBlock().getType() == Material.FIRE) { - Logging.finest("There's fire below! (%s)[%s]", actual.getBlock().getType(), - this.isSolidBlock(actual.getBlock().getType())); - return false; - } - - if (isBlockAboveAir(actual)) { - Logging.finest("Is block above air [%s]", isBlockAboveAir(actual)); - Logging.finest("Has 2 blocks of water below [%s]", this.hasTwoBlocksofWaterBelow(actual)); - return this.hasTwoBlocksofWaterBelow(actual); - } - return true; - } - - /** - * Gets the location of the top block at the specified {@link Location}. - * @param l Any {@link Location}. - * @return The {@link Location} of the top-block. - */ - public Location getTopBlock(Location l) { - Location check = l.clone(); - check.setY(127); // SUPPRESS CHECKSTYLE: MagicNumberCheck - while (check.getY() > 0) { - if (this.playerCanSpawnHereSafely(check)) { - return check; - } - check.setY(check.getY() - 1); - } - return null; - } - - /** - * Gets the location of the top block at the specified {@link Location}. - * @param l Any {@link Location}. - * @return The {@link Location} of the top-block. - */ - public Location getBottomBlock(Location l) { - Location check = l.clone(); - check.setY(0); - while (check.getY() < 127) { // SUPPRESS CHECKSTYLE: MagicNumberCheck - if (this.playerCanSpawnHereSafely(check)) { - return check; - } - check.setY(check.getY() + 1); - } - return null; - } - - /* - * If someone has a better way of this... Please either tell us, or submit a pull request! - */ - private boolean isSolidBlock(Material type) { - switch (type) { - case AIR: - return false; - case SNOW: - return false; - case TRAP_DOOR: - return false; - case TORCH: - return false; - case YELLOW_FLOWER: - return false; - case RED_ROSE: - return false; - case RED_MUSHROOM: - return false; - case BROWN_MUSHROOM: - return false; - case REDSTONE: - return false; - case REDSTONE_WIRE: - return false; - case RAILS: - return false; - case POWERED_RAIL: - return false; - case REDSTONE_TORCH_ON: - return false; - case REDSTONE_TORCH_OFF: - return false; - case DEAD_BUSH: - return false; - case SAPLING: - return false; - case STONE_BUTTON: - return false; - case LEVER: - return false; - case LONG_GRASS: - return false; - case PORTAL: - return false; - case STONE_PLATE: - return false; - case WOOD_PLATE: - return false; - case SEEDS: - return false; - case SUGAR_CANE_BLOCK: - return false; - case WALL_SIGN: - return false; - case SIGN_POST: - return false; - case WOODEN_DOOR: - return false; - case STATIONARY_WATER: - return false; - case WATER: - return false; - default: - return true; - } - } - - /** - * Checks if an entity would be on track at the specified {@link Location}. - * @param l The {@link Location}. - * @return True if an entity would be on tracks at the specified {@link Location}. - */ - public boolean isEntitiyOnTrack(Location l) { - Material currentBlock = l.getBlock().getType(); - return (currentBlock == Material.POWERED_RAIL || currentBlock == Material.DETECTOR_RAIL || currentBlock == Material.RAILS); - } - - // TODO maybe remove this? - private void showDangers(Location l) { - Location actual = new Location(l.getWorld(), l.getX(), l.getY(), l.getZ()); - Location upOne = new Location(l.getWorld(), l.getX(), l.getY(), l.getZ()); - Location downOne = new Location(l.getWorld(), l.getX(), l.getY(), l.getZ()); - upOne.setY(upOne.getY() + 1); - downOne.setY(downOne.getY() - 1); - - System.out.print("Location Up: " + upOne.getBlock().getType()); - System.out.print(" " + upOne); - System.out.print("Location: " + actual.getBlock().getType()); - System.out.print(" " + actual); - System.out.print("Location Down: " + downOne.getBlock().getType()); - System.out.print(" " + downOne); - } - - /** - * Checks recursively below a {@link Location} for 2 blocks of water. - * - * @param l The {@link Location} - * @return Whether there are 2 blocks of water - */ - private boolean hasTwoBlocksofWaterBelow(Location l) { - if (l.getBlockY() < 0) { - return false; - } - Location oneBelow = l.clone(); - oneBelow.subtract(0, 1, 0); - if (oneBelow.getBlock().getType() == Material.WATER || oneBelow.getBlock().getType() == Material.STATIONARY_WATER) { - Location twoBelow = oneBelow.clone(); - twoBelow.subtract(0, 1, 0); - return (oneBelow.getBlock().getType() == Material.WATER || oneBelow.getBlock().getType() == Material.STATIONARY_WATER); - } - if (oneBelow.getBlock().getType() != Material.AIR) { - return false; - } - return hasTwoBlocksofWaterBelow(oneBelow); - } - - /** - * Checks if the specified {@link Minecart} can spawn safely. - * @param cart The {@link Minecart}. - * @return True if the minecart can spawn safely. - */ - public boolean canSpawnCartSafely(Minecart cart) { - if (this.isBlockAboveAir(cart.getLocation())) { - return true; - } - if (this.isEntitiyOnTrack(LocationManipulation.getNextBlock(cart))) { - return true; - } - return false; - } - - /** - * Checks if the specified {@link Vehicle} can spawn safely. - * @param vehicle The {@link Vehicle}. - * @return True if the vehicle can spawn safely. - */ - public boolean canSpawnVehicleSafely(Vehicle vehicle) { - if (this.isBlockAboveAir(vehicle.getLocation())) { - return true; - } - return false; - } - -} diff --git a/src/main/java/com/onarandombox/MultiverseCore/utils/BukkitTravelAgent.java b/src/main/java/com/onarandombox/MultiverseCore/utils/BukkitTravelAgent.java new file mode 100644 index 000000000..fd68d2ea4 --- /dev/null +++ b/src/main/java/com/onarandombox/MultiverseCore/utils/BukkitTravelAgent.java @@ -0,0 +1,110 @@ +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/FancyMessage.java b/src/main/java/com/onarandombox/MultiverseCore/utils/FancyMessage.java index e02c010e0..7775e9661 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/utils/FancyMessage.java +++ b/src/main/java/com/onarandombox/MultiverseCore/utils/FancyMessage.java @@ -22,9 +22,9 @@ public class FancyMessage implements FancyText { * Allows easy creation of an alternating colored list. * TODO: Documentation! Why does CheckStyle just ignore this? * - * @param title - * @param message - * @param scheme + * @param title The title. + * @param message The body of the message. + * @param scheme The color scheme to use for easy styling. */ public FancyMessage(String title, String message, FancyColorScheme scheme) { this.title = title; diff --git a/src/main/java/com/onarandombox/MultiverseCore/utils/FileUtils.java b/src/main/java/com/onarandombox/MultiverseCore/utils/FileUtils.java index 4bc4a18ab..47fa80ed7 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/utils/FileUtils.java +++ b/src/main/java/com/onarandombox/MultiverseCore/utils/FileUtils.java @@ -7,11 +7,23 @@ package com.onarandombox.MultiverseCore.utils; -import com.dumptruckman.minecraft.util.Logging; +import static java.nio.file.StandardCopyOption.COPY_ATTRIBUTES; import java.io.File; import java.io.IOException; +import java.nio.file.FileVisitResult; +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; + +import com.dumptruckman.minecraft.util.Logging; /** * File-utilities. @@ -28,8 +40,8 @@ protected FileUtils() { * @return true if the folder was successfully deleted. */ public static boolean deleteFolder(File file) { - try { - org.apache.commons.io.FileUtils.deleteDirectory(file); + try (Stream files = Files.walk(file.toPath())) { + files.sorted(Comparator.reverseOrder()).map(Path::toFile).forEach(File::delete); return true; } catch (IOException e) { Logging.warning(e.getMessage()); @@ -44,8 +56,11 @@ public static boolean deleteFolder(File file) { * @return true if the contents were successfully deleted */ public static boolean deleteFolderContents(File file) { - try { - org.apache.commons.io.FileUtils.cleanDirectory(file); + try (Stream files = Files.walk(file.toPath())){ + files.sorted(Comparator.reverseOrder()) + .map(Path::toFile) + .filter(f -> !f.equals(file)) + .forEach(File::delete); return true; } catch (IOException e) { Logging.warning(e.getMessage()); @@ -53,23 +68,70 @@ public static boolean deleteFolderContents(File file) { } } - private static final int COPY_BLOCK_SIZE = 1024; - /** * 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) { + return copyFolder(source, target, null, log); + } + + /** + * 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 + * @param log A logger that logs the operation + * + * @return true if it had success + */ + public static boolean copyFolder(File source, File target, List excludeFiles, Logger log) { + Path sourceDir = source.toPath(); + Path targetDir = target.toPath(); + try { - org.apache.commons.io.FileUtils.copyDirectory(source, target); + Files.walkFileTree(sourceDir, new CopyDirFileVisitor(sourceDir, targetDir, excludeFiles)); return true; } catch (IOException e) { - log.warning(e.getMessage()); + log.log(Level.WARNING, "Unable to copy directory", e); return false; } } -} + + private static class CopyDirFileVisitor extends SimpleFileVisitor { + + private final Path sourceDir; + private final Path targetDir; + private final List excludeFiles; + + private CopyDirFileVisitor(Path sourceDir, Path targetDir, List excludeFiles) { + this.sourceDir = sourceDir; + this.targetDir = targetDir; + this.excludeFiles = excludeFiles; + } + + @Override + public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException { + Path newDir = targetDir.resolve(sourceDir.relativize(dir)); + if (!Files.isDirectory(newDir)) { + Files.createDirectory(newDir); + } + return FileVisitResult.CONTINUE; + } + + @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/MVEconomist.java b/src/main/java/com/onarandombox/MultiverseCore/utils/MVEconomist.java index 702428e17..04353649a 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/utils/MVEconomist.java +++ b/src/main/java/com/onarandombox/MultiverseCore/utils/MVEconomist.java @@ -1,284 +1,285 @@ -package com.onarandombox.MultiverseCore.utils; - -import org.bukkit.ChatColor; -import org.bukkit.Material; -import org.bukkit.World; -import org.bukkit.entity.Player; -import org.bukkit.inventory.ItemStack; -import org.bukkit.plugin.Plugin; - -import java.util.HashMap; - -/** - * Multiverse's Friendly Economist. This is used to deal with external economies and also item costs for stuff in MV. - */ -public class MVEconomist { - - private final VaultHandler vaultHandler; - - public MVEconomist(Plugin plugin) { - vaultHandler = new VaultHandler(plugin); - } - - private boolean isUsingVault(int currency) { - return !isItemCurrency(currency) && getVaultHandler().hasEconomy(); - } - - /** - * Checks if an economy plugin is in use. - * - * @return true if an economy plugin is detected by Vault. - */ - public boolean isUsingEconomyPlugin() { - return getVaultHandler().hasEconomy(); - } - - /** - * Formats the amount to a human readable currency string. - * - * @param amount the amount of currency. - * @param currency the type of currency. A value greater than -1 indicates the material type used for currency. - * @return the human readable currency string. - */ - public String formatPrice(double amount, int currency) { - if (isUsingVault(currency)) { - return getVaultHandler().getEconomy().format(amount); - } else { - return ItemEconomy.getFormattedPrice(amount, currency); - } - } - - /** - * Returns the name of the economy in use. - * - * @return the name of the economy in use. - */ - public String getEconomyName() { - if (getVaultHandler().hasEconomy()) { - return getVaultHandler().getEconomy().getName(); - } else { - return ItemEconomy.getName(); - } - } - - /** - * Determines if a player has enough of a given currency. - * @param player the player to check for currency. - * @param amount the amount of currency. - * @param currency the type of currency. A value greater than -1 indicates the material type used for currency. - * @return true if the player has enough of the given currency or the amount is 0 or less. - */ - public boolean isPlayerWealthyEnough(Player player, double amount, int currency) { - if (amount <= 0D) { - return true; - } else if (isUsingVault(currency)) { - return getVaultHandler().getEconomy().has(player, amount); - } else { - return ItemEconomy.hasEnough(player, amount, currency); - } - } - - /** - * Formats a message for a player indicating they don't have enough currency. - * - * @param currency the type of currency. A value greater than -1 indicates the material type used for currency. - * @param message The more specific message to append to the generic message of not having enough. - * @return the formatted insufficient funds message. - */ - public String getNSFMessage(int currency, String message) { - return "Sorry, you don't have enough " + (isItemCurrency(currency) ? "items" : "funds") + ". " + message; - } - - /** - * Deposits a given amount of currency either into the player's economy account or inventory if the currency - * represents an item. - * - * @param player the player to give currency to. - * @param amount the amount to give. - * @param currency the type of currency. A value greater than -1 indicates the material type used for currency. - */ - public void deposit(Player player, double amount, int currency) { - if (isUsingVault(currency)) { - getVaultHandler().getEconomy().depositPlayer(player, amount); - } else { - ItemEconomy.deposit(player, amount, currency); - } - } - - /** - * Withdraws a given amount of currency either from the player's economy account or inventory if the currency - * represents an item. - * - * @param player the player to take currency from. - * @param amount the amount to take. - * @param currency the type of currency. A value greater than -1 indicates the material type used for currency. - */ - public void withdraw(Player player, double amount, int currency) { - if (isUsingVault(currency)) { - getVaultHandler().getEconomy().withdrawPlayer(player, amount); - } else { - ItemEconomy.withdraw(player, amount, currency); - } - } - - /** - * Returns the economy balance of the given player. - * - * @param player the player to get the balance for. - * @return the economy balance of the given player. - * @throws IllegalStateException thrown if this is used when no economy plugin is available. - */ - public double getBalance(Player player) throws IllegalStateException { - return getBalance(player, null); - } - - /** - * Returns the economy balance of the given player in the given world. If the economy plugin does not have world - * specific balances then the global balance will be returned. - * - * @param player the player to get the balance for. - * @param world the world to get the balance for. - * @return the economy balance of the given player in the given world. - * @throws IllegalStateException thrown if this is used when no economy plugin is available. - */ - public double getBalance(Player player, World world) throws IllegalStateException { - if (!isUsingEconomyPlugin()) { - throw new IllegalStateException("getBalance is only available when using an economy plugin with Vault"); - } - if (world != null) { - return getVaultHandler().getEconomy().getBalance(player, world.getName()); - } else { - return getVaultHandler().getEconomy().getBalance(player); - } - } - - /** - * Sets the economy balance for the given player. - * - * @param player the player to set the balance for. - * @param amount the amount to set the player's balance to. - * @throws IllegalStateException thrown if this is used when no economy plugin is available. - */ - public void setBalance(Player player, double amount) throws IllegalStateException { - setBalance(player, null, amount); - } - - /** - * Sets the economy balance for the given player in the given world. If the economy plugin does not have world - * specific balances then the global balance will be set. - * - * @param player the player to set the balance for. - * @param world the world to get the balance for. - * @param amount the amount to set the player's balance to. - * @throws IllegalStateException thrown if this is used when no economy plugin is available. - */ - public void setBalance(Player player, World world, double amount) throws IllegalStateException { - if (!isUsingEconomyPlugin()) { - throw new IllegalStateException("getBalance is only available when using an economy plugin with Vault"); - } - if (world != null) { - getVaultHandler().getEconomy().withdrawPlayer(player, world.getName(), getBalance(player, world)); - getVaultHandler().getEconomy().depositPlayer(player, world.getName(), amount); - } else { - getVaultHandler().getEconomy().withdrawPlayer(player, getBalance(player)); - getVaultHandler().getEconomy().depositPlayer(player, amount); - } - } - - /** - * This method is public for backwards compatibility. - * - * @return the old VaultHandler. - * @deprecated just use the other methods in this class for economy stuff. - */ - // TODO make private - @Deprecated - public VaultHandler getVaultHandler() { - return vaultHandler; - } - - /** - * Determines if the currency type given represents an item currency. - * - * @param currency the type of currency. A value greater than -1 indicates the material type used for currency. - * @return true if currency is greater than -1. - */ - public static boolean isItemCurrency(int currency) { - return currency >= 0; - } - - private static class ItemEconomy { - - private static final String ECONOMY_NAME = "Simple Item Economy"; - - private static String getFormattedPrice(double amount, int currency) { - if (isItemCurrency(currency)) { - Material m = Material.getMaterial(currency); - return m != null ? amount + " " + m.toString() : "NO ITEM FOUND"; - } else { - return ""; - } - } - - private static String getName() { - return ECONOMY_NAME; - } - - private static boolean hasEnough(Player player, double amount, int currency) { - if (isItemCurrency(currency)) { - return player.getInventory().contains(currency, (int) amount); - } else { - return true; - } - } - - private static void deposit(Player player, double amount, int currency) { - if (isItemCurrency(currency)) { - giveItem(player, amount, currency); - } - } - - private static void withdraw(Player player, double amount, int currency) { - if (isItemCurrency(currency)) { - takeItem(player, amount, currency); - } - } - - private static void giveItem(Player player, double amount, int type) { - ItemStack item = new ItemStack(type, (int) amount); - player.getInventory().addItem(item); - showReceipt(player, (amount * -1), type); - } - - private static void takeItem(Player player, double amount, int type) { - int removed = 0; - HashMap items = (HashMap) player.getInventory().all(type); - for (int i : items.keySet()) { - if (removed >= amount) { - break; - } - int diff = (int) (amount - removed); - int amt = player.getInventory().getItem(i).getAmount(); - if (amt - diff > 0) { - player.getInventory().getItem(i).setAmount(amt - diff); - break; - } else { - removed += amt; - player.getInventory().clear(i); - } - } - showReceipt(player, amount, type); - } - - private static void showReceipt(Player player, double price, int item) { - if (price > 0D) { - player.sendMessage(String.format("%s%s%s %s", - ChatColor.WHITE, "You have been charged", ChatColor.GREEN, getFormattedPrice(price, item))); - } else if (price < 0D) { - player.sendMessage(String.format("%s%s%s %s", - ChatColor.DARK_GREEN, getFormattedPrice((price * -1), item), - ChatColor.WHITE, "has been added to your inventory.")); - } - } - } -} +package com.onarandombox.MultiverseCore.utils; + +import org.bukkit.ChatColor; +import org.bukkit.Material; +import org.bukkit.World; +import org.bukkit.entity.Player; +import org.bukkit.inventory.ItemStack; +import org.bukkit.plugin.Plugin; +import org.jetbrains.annotations.Nullable; + +import java.util.HashMap; + +/** + * Multiverse's Friendly Economist. This is used to deal with external economies and also item costs for stuff in MV. + */ +public class MVEconomist { + + private final VaultHandler vaultHandler; + + public MVEconomist(Plugin plugin) { + vaultHandler = new VaultHandler(plugin); + } + + private boolean isUsingVault(Material currency) { + return !isItemCurrency(currency) && getVaultHandler().hasEconomy(); + } + + /** + * Checks if an economy plugin is in use. + * + * @return true if an economy plugin is detected by Vault. + */ + public boolean isUsingEconomyPlugin() { + return getVaultHandler().hasEconomy(); + } + + /** + * Formats the amount to a human readable currency string. + * + * @param amount the amount of currency. + * @param currency the type of currency. Null indicates a non-item currency is used. + * @return the human readable currency string. + */ + public String formatPrice(double amount, @Nullable Material currency) { + if (isUsingVault(currency)) { + return getVaultHandler().getEconomy().format(amount); + } else { + return ItemEconomy.getFormattedPrice(amount, currency); + } + } + + /** + * Returns the name of the economy in use. + * + * @return the name of the economy in use. + */ + public String getEconomyName() { + if (getVaultHandler().hasEconomy()) { + return getVaultHandler().getEconomy().getName(); + } else { + return ItemEconomy.getName(); + } + } + + /** + * Determines if a player has enough of a given currency. + * + * @param player the player to check for currency. + * @param amount the amount of currency. + * @param currency the type of currency. Null indicates non-item currency is used. + * @return true if the player has enough of the given currency or the amount is 0 or less. + */ + public boolean isPlayerWealthyEnough(Player player, double amount, Material currency) { + if (amount <= 0D) { + return true; + } else if (isUsingVault(currency)) { + return getVaultHandler().getEconomy().has(player, amount); + } else { + return ItemEconomy.hasEnough(player, amount, currency); + } + } + + /** + * Formats a message for a player indicating they don't have enough currency. + * + * @param currency the type of currency. Null indicates a non-item currency is used. + * @param message The more specific message to append to the generic message of not having enough. + * @return the formatted insufficient funds message. + */ + public String getNSFMessage(Material currency, String message) { + return "Sorry, you don't have enough " + (isItemCurrency(currency) ? "items" : "funds") + ". " + message; + } + + /** + * Deposits a given amount of currency either into the player's economy account or inventory if the currency + * is not null. + * + * @param player the player to give currency to. + * @param amount the amount to give. + * @param currency the type of currency. + */ + public void deposit(Player player, double amount, @Nullable Material currency) { + if (isUsingVault(currency)) { + getVaultHandler().getEconomy().depositPlayer(player, amount); + } else { + ItemEconomy.deposit(player, amount, currency); + } + } + + /** + * Withdraws a given amount of currency either from the player's economy account or inventory if the currency + * is not null. + * + * @param player the player to take currency from. + * @param amount the amount to take. + * @param currency the type of currency. + */ + public void withdraw(Player player, double amount, @Nullable Material currency) { + if (isUsingVault(currency)) { + getVaultHandler().getEconomy().withdrawPlayer(player, amount); + } else { + ItemEconomy.withdraw(player, amount, currency); + } + } + + /** + * Returns the economy balance of the given player. + * + * @param player the player to get the balance for. + * @return the economy balance of the given player. + * @throws IllegalStateException thrown if this is used when no economy plugin is available. + */ + public double getBalance(Player player) throws IllegalStateException { + return getBalance(player, null); + } + + /** + * Returns the economy balance of the given player in the given world. If the economy plugin does not have world + * specific balances then the global balance will be returned. + * + * @param player the player to get the balance for. + * @param world the world to get the balance for. + * @return the economy balance of the given player in the given world. + * @throws IllegalStateException thrown if this is used when no economy plugin is available. + */ + public double getBalance(Player player, World world) throws IllegalStateException { + if (!isUsingEconomyPlugin()) { + throw new IllegalStateException("getBalance is only available when using an economy plugin with Vault"); + } + if (world != null) { + return getVaultHandler().getEconomy().getBalance(player, world.getName()); + } else { + return getVaultHandler().getEconomy().getBalance(player); + } + } + + /** + * Sets the economy balance for the given player. + * + * @param player the player to set the balance for. + * @param amount the amount to set the player's balance to. + * @throws IllegalStateException thrown if this is used when no economy plugin is available. + */ + public void setBalance(Player player, double amount) throws IllegalStateException { + setBalance(player, null, amount); + } + + /** + * Sets the economy balance for the given player in the given world. If the economy plugin does not have world + * specific balances then the global balance will be set. + * + * @param player the player to set the balance for. + * @param world the world to get the balance for. + * @param amount the amount to set the player's balance to. + * @throws IllegalStateException thrown if this is used when no economy plugin is available. + */ + public void setBalance(Player player, World world, double amount) throws IllegalStateException { + if (!isUsingEconomyPlugin()) { + throw new IllegalStateException("getBalance is only available when using an economy plugin with Vault"); + } + if (world != null) { + getVaultHandler().getEconomy().withdrawPlayer(player, world.getName(), getBalance(player, world)); + getVaultHandler().getEconomy().depositPlayer(player, world.getName(), amount); + } else { + getVaultHandler().getEconomy().withdrawPlayer(player, getBalance(player)); + getVaultHandler().getEconomy().depositPlayer(player, amount); + } + } + + /** + * This method is public for backwards compatibility. + * + * @return the old VaultHandler. + * @deprecated just use the other methods in this class for economy stuff. + */ + // TODO make private + @Deprecated + public VaultHandler getVaultHandler() { + return vaultHandler; + } + + /** + * Determines if the currency type string given represents an item currency. + * + * @param currency the type of currency. + * @return true if currency string matches a valid material. + */ + public static boolean isItemCurrency(Material currency) { + return currency != null; + } + + private static class ItemEconomy { + + private static final String ECONOMY_NAME = "Simple Item Economy"; + + private static String getFormattedPrice(double amount, Material currency) { + if (isItemCurrency(currency)) { + return amount + " " + currency.toString(); + } else { + return ""; + } + } + + private static String getName() { + return ECONOMY_NAME; + } + + private static boolean hasEnough(Player player, double amount, Material currency) { + if (currency != null) { + return player.getInventory().contains(currency, (int) amount); + } else { + return true; + } + } + + private static void deposit(Player player, double amount, Material currency) { + if (isItemCurrency(currency)) { + giveItem(player, amount, currency); + } + } + + private static void withdraw(Player player, double amount, Material currency) { + if (isItemCurrency(currency)) { + takeItem(player, amount, currency); + } + } + + private static void giveItem(Player player, double amount, Material type) { + ItemStack item = new ItemStack(type, (int) amount); + player.getInventory().addItem(item); + showReceipt(player, (amount * -1), type); + } + + private static void takeItem(Player player, double amount, Material type) { + int removed = 0; + HashMap items = (HashMap) player.getInventory().all(type); + for (int i : items.keySet()) { + if (removed >= amount) { + break; + } + int diff = (int) (amount - removed); + int amt = player.getInventory().getItem(i).getAmount(); + if (amt - diff > 0) { + player.getInventory().getItem(i).setAmount(amt - diff); + break; + } else { + removed += amt; + player.getInventory().clear(i); + } + } + showReceipt(player, amount, type); + } + + private static void showReceipt(Player player, double price, Material item) { + if (price > 0D) { + player.sendMessage(String.format("%s%s%s %s", + ChatColor.WHITE, "You have been charged", ChatColor.GREEN, getFormattedPrice(price, item))); + } else if (price < 0D) { + player.sendMessage(String.format("%s%s%s %s", + ChatColor.DARK_GREEN, getFormattedPrice((price * -1), item), + ChatColor.WHITE, "has been added to your inventory.")); + } + } + } +} diff --git a/src/main/java/com/onarandombox/MultiverseCore/utils/MVPermissions.java b/src/main/java/com/onarandombox/MultiverseCore/utils/MVPermissions.java index 2ea793578..d1c078e72 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/utils/MVPermissions.java +++ b/src/main/java/com/onarandombox/MultiverseCore/utils/MVPermissions.java @@ -230,7 +230,7 @@ public void tellMeWhyICantDoThis(CommandSender asker, CommandSender playerInQues * * @param sender Who is requesting the permission. * @param node The permission node in string format; multiverse.core.list.worlds for example. - * @param isOpRequired @Deprecated. This is not used for anything anymore. + * @param isOpRequired deprecated This is not used for anything anymore. * @return True if they have that permission or any parent. */ @Override diff --git a/src/main/java/com/onarandombox/MultiverseCore/utils/MVTravelAgent.java b/src/main/java/com/onarandombox/MultiverseCore/utils/MVTravelAgent.java index 0f3ada975..a86def8a5 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/utils/MVTravelAgent.java +++ b/src/main/java/com/onarandombox/MultiverseCore/utils/MVTravelAgent.java @@ -9,114 +9,19 @@ import com.onarandombox.MultiverseCore.MultiverseCore; import com.onarandombox.MultiverseCore.api.MVDestination; -import com.onarandombox.MultiverseCore.api.SafeTTeleporter; -import com.onarandombox.MultiverseCore.destination.CannonDestination; -import org.bukkit.Location; -import org.bukkit.TravelAgent; import org.bukkit.entity.Player; -import java.util.logging.Level; - /** - * The Multiverse-{@link TravelAgent}. + * The Multiverse TravelAgent. */ -public class MVTravelAgent implements TravelAgent { - private MVDestination destination; - private MultiverseCore core; - private Player player; +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; } - - /** - * {@inheritDoc} - */ - @Override - public TravelAgent setSearchRadius(int radius) { - return this; - } - - /** - * {@inheritDoc} - */ - @Override - public int getSearchRadius() { - return 0; - } - - /** - * {@inheritDoc} - */ - @Override - public TravelAgent 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 (this.destination instanceof CannonDestination) { - this.core.log(Level.FINE, "Using Stock TP method. This cannon will have 0 velocity"); - } - SafeTTeleporter teleporter = this.core.getSafeTTeleporter(); - Location newLoc = this.destination.getLocation(this.player); - if (this.destination.useSafeTeleporter()) { - newLoc = teleporter.getSafeLocation(this.player, this.destination); - } - if (newLoc == null) { - return this.player.getLocation(); - } - return newLoc; - - } - } diff --git a/src/main/java/com/onarandombox/MultiverseCore/utils/MaterialConverter.java b/src/main/java/com/onarandombox/MultiverseCore/utils/MaterialConverter.java new file mode 100644 index 000000000..0fd19457e --- /dev/null +++ b/src/main/java/com/onarandombox/MultiverseCore/utils/MaterialConverter.java @@ -0,0 +1,42 @@ +package com.onarandombox.MultiverseCore.utils; + +import de.themoep.idconverter.IdMappings; +import org.bukkit.Material; +import org.bukkit.configuration.ConfigurationSection; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +/** + * A tool for converting values which may be an old type ID to a Material. + */ +public class MaterialConverter { + + /** + * Converts the value in the given config at the given path from a numeric id or flattened material name to a + * Material. + * + * @param config The config with the value to convert. + * @param path The path of the value in the config. + * @return The converted Material type or null if no matching type. + */ + @Nullable + public static Material convertConfigType(@NotNull ConfigurationSection config, @NotNull String path) { + return convertTypeString(config.getString(path)); + } + + /** + * Converts a string representing a numeric id or flattened material name to a Material. + * + * @param value The value to convert. + * @return The converted Material type or null if no matching type. + */ + @Nullable + public static Material convertTypeString(@Nullable String value) { + IdMappings.Mapping mapping = IdMappings.getById(value != null ? value : ""); + if (mapping != null) { + return Material.matchMaterial(mapping.getFlatteningType()); + } else { + return Material.matchMaterial(value != null ? value : ""); + } + } +} diff --git a/src/main/java/com/onarandombox/MultiverseCore/utils/PermissionTools.java b/src/main/java/com/onarandombox/MultiverseCore/utils/PermissionTools.java index 119d02cfd..a6e988205 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/utils/PermissionTools.java +++ b/src/main/java/com/onarandombox/MultiverseCore/utils/PermissionTools.java @@ -9,6 +9,7 @@ import com.onarandombox.MultiverseCore.MultiverseCore; import com.onarandombox.MultiverseCore.api.MultiverseWorld; +import org.bukkit.Material; import org.bukkit.command.CommandSender; import org.bukkit.command.ConsoleCommandSender; import org.bukkit.entity.Player; @@ -145,7 +146,7 @@ public boolean playerHasMoneyToEnter(MultiverseWorld fromWorld, MultiverseWorld } final MVEconomist economist = plugin.getEconomist(); - final int currency = toWorld.getCurrency(); + final Material currency = toWorld.getCurrency(); final String formattedAmount = economist.formatPrice(price, currency); if (economist.isPlayerWealthyEnough(teleporterPlayer, price, currency)) { @@ -171,7 +172,7 @@ public boolean playerHasMoneyToEnter(MultiverseWorld fromWorld, MultiverseWorld return true; } - private void sendTeleportPaymentMessage (MVEconomist economist, Player teleporterPlayer, Player teleportee, String toWorld, double price, int currency) { + private void sendTeleportPaymentMessage (MVEconomist economist, Player teleporterPlayer, Player teleportee, String toWorld, double price, Material currency) { price = Math.abs(price); if (teleporterPlayer.equals(teleportee)) { teleporterPlayer.sendMessage("You were " + (price > 0D ? "charged " : "given ") + economist.formatPrice(price, currency) + " for teleporting to " + toWorld); diff --git a/src/main/java/com/onarandombox/MultiverseCore/utils/PurgeWorlds.java b/src/main/java/com/onarandombox/MultiverseCore/utils/PurgeWorlds.java index 48ed4a2af..c932d2d88 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/utils/PurgeWorlds.java +++ b/src/main/java/com/onarandombox/MultiverseCore/utils/PurgeWorlds.java @@ -27,7 +27,7 @@ /** * Utility class that removes animals from worlds that don't belong there. * - * @deprecated Use instead: {@link WorldPurger} and {@link SimpleWorldPurger}. + * @deprecated Use instead: {@link com.onarandombox.MultiverseCore.api.WorldPurger} and {@link SimpleWorldPurger}. */ @Deprecated public class PurgeWorlds { diff --git a/src/main/java/com/onarandombox/MultiverseCore/utils/SafeTTeleporter.java b/src/main/java/com/onarandombox/MultiverseCore/utils/SafeTTeleporter.java deleted file mode 100644 index 0984c437e..000000000 --- a/src/main/java/com/onarandombox/MultiverseCore/utils/SafeTTeleporter.java +++ /dev/null @@ -1,358 +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.BlockSafety; -import com.onarandombox.MultiverseCore.api.MVDestination; -import com.onarandombox.MultiverseCore.destination.InvalidDestination; -import com.onarandombox.MultiverseCore.enums.TeleportResult; - -import org.bukkit.Location; -import org.bukkit.Material; -import org.bukkit.block.Block; -import org.bukkit.block.BlockFace; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Entity; -import org.bukkit.entity.Minecart; -import org.bukkit.entity.Player; -import org.bukkit.entity.Vehicle; -import org.bukkit.util.Vector; - -import java.util.logging.Level; - -/** - * The {@link SafeTTeleporter}. - * - * @deprecated Use instead: {@link com.onarandombox.MultiverseCore.api.SafeTTeleporter} and {@link SimpleSafeTTeleporter}. - */ -@Deprecated -public class SafeTTeleporter { - - private MultiverseCore plugin; - private BlockSafety bs; - - public SafeTTeleporter(MultiverseCore plugin) { - this.plugin = plugin; - this.bs = plugin.getBlockSafety(); - } - - private static final int DEFAULT_TOLERANCE = 6; - private static final int DEFAULT_RADIUS = 9; - - /** - * Gets the next safe location around the given location. - * @param l A {@link Location}. - * @return A safe {@link Location}. - */ - public Location getSafeLocation(Location l) { - return this.getSafeLocation(l, DEFAULT_TOLERANCE, DEFAULT_RADIUS); - } - - /** - * Gets the next safe location around the given location. - * @param l A {@link Location}. - * @param tolerance The tolerance. - * @param radius The radius. - * @return A safe {@link Location}. - */ - public Location getSafeLocation(Location l, int tolerance, int radius) { - // Check around the player first in a configurable radius: - Location safe = checkAboveAndBelowLocation(l, tolerance, radius); - 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)); - } else { - this.plugin.log(Level.FINE, "Uh oh! No safe place found!"); - } - return safe; - } - - private Location checkAboveAndBelowLocation(Location l, int tolerance, int radius) { - // Tolerance must be an even number: - if (tolerance % 2 != 0) { - tolerance += 1; - } - // 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); - - // For now this will just do a straight up block. - Location locToCheck = l.clone(); - // Check the main level - Location safe = this.checkAroundLocation(locToCheck, radius); - if (safe != null) { - return safe; - } - // We've already checked zero right above this. - int currentLevel = 1; - while (currentLevel <= tolerance) { - // Check above - locToCheck = l.clone(); - locToCheck.add(0, currentLevel, 0); - safe = this.checkAroundLocation(locToCheck, radius); - if (safe != null) { - return safe; - } - - // Check below - locToCheck = l.clone(); - locToCheck.subtract(0, currentLevel, 0); - safe = this.checkAroundLocation(locToCheck, radius); - if (safe != null) { - return safe; - } - currentLevel++; - } - - return null; - } - - /* - * For my crappy algorithm, radius MUST be odd. - */ - private Location checkAroundLocation(Location l, int diameter) { - if (diameter % 2 == 0) { - diameter += 1; - } - Location checkLoc = l.clone(); - - // Start at 3, the min diameter around a block - int loopcounter = 3; - while (loopcounter <= diameter) { - boolean foundSafeArea = checkAroundSpecificDiameter(checkLoc, loopcounter); - // If a safe area was found: - if (foundSafeArea) { - // Return the checkLoc, it is the safe location. - return checkLoc; - } - // Otherwise, let's reset our location - checkLoc = l.clone(); - // And increment the radius - loopcounter += 2; - } - return null; - } - - private boolean checkAroundSpecificDiameter(Location checkLoc, int circle) { - // Adjust the circle to get how many blocks to step out. - // A radius of 3 makes the block step 1 - // A radius of 5 makes the block step 2 - // A radius of 7 makes the block step 3 - // ... - int adjustedCircle = ((circle - 1) / 2); - checkLoc.add(adjustedCircle, 0, 0); - if (this.bs.playerCanSpawnHereSafely(checkLoc)) { - return true; - } - // Now we go to the right that adjustedCircle many - for (int i = 0; i < adjustedCircle; i++) { - checkLoc.add(0, 0, 1); - if (this.bs.playerCanSpawnHereSafely(checkLoc)) { - return true; - } - } - - // Then down adjustedCircle *2 - for (int i = 0; i < adjustedCircle * 2; i++) { - checkLoc.add(-1, 0, 0); - if (this.bs.playerCanSpawnHereSafely(checkLoc)) { - return true; - } - } - - // Then left adjustedCircle *2 - for (int i = 0; i < adjustedCircle * 2; i++) { - checkLoc.add(0, 0, -1); - if (this.bs.playerCanSpawnHereSafely(checkLoc)) { - return true; - } - } - - // Then up Then left adjustedCircle *2 - for (int i = 0; i < adjustedCircle * 2; i++) { - checkLoc.add(1, 0, 0); - if (this.bs.playerCanSpawnHereSafely(checkLoc)) { - return true; - } - } - - // Then finish up by doing adjustedCircle - 1 - for (int i = 0; i < adjustedCircle - 1; i++) { - checkLoc.add(0, 0, 1); - if (this.bs.playerCanSpawnHereSafely(checkLoc)) { - return true; - } - } - return false; - } - - /** - * Safely teleport the entity to the MVDestination. This will perform checks to see if the place is safe, and if - * it's not, will adjust the final destination accordingly. - * - * @param teleporter Person who performed the teleport command. - * @param teleportee Entity to teleport - * @param d Destination to teleport them to - * @return true for success, false for failure - */ - 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"); - return TeleportResult.FAIL_INVALID; - } - Player teleporteePlayer = null; - if (teleportee instanceof Player) { - teleporteePlayer = ((Player) teleportee); - } else if (teleportee.getPassenger() instanceof Player) { - teleporteePlayer = ((Player) teleportee.getPassenger()); - } - - if (teleporteePlayer == null) { - return TeleportResult.FAIL_INVALID; - } - MultiverseCore.addPlayerToTeleportQueue(teleporter.getName(), teleporteePlayer.getName()); - - Location safeLoc = d.getLocation(teleportee); - if (d.useSafeTeleporter()) { - safeLoc = this.getSafeLocation(teleportee, d); - } - - if (safeLoc != null) { - if (teleportee.teleport(safeLoc)) { - if (!d.getVelocity().equals(new Vector(0, 0, 0))) { - teleportee.setVelocity(d.getVelocity()); - } - return TeleportResult.SUCCESS; - } - return TeleportResult.FAIL_OTHER; - } - return TeleportResult.FAIL_UNSAFE; - } - - /** - * Safely teleport the entity to the Location. This may perform checks to - * see if the place is safe, and if - * it's not, will adjust the final destination accordingly. - * - * @param teleporter Person who issued the teleport command. - * @param teleportee Entity to teleport. - * @param location Location to teleport them to. - * @param safely Should the destination be checked for safety before teleport? - * @return true for success, false for failure. - */ - public TeleportResult safelyTeleport(CommandSender teleporter, Entity teleportee, Location location, boolean safely) { - if (safely) { - location = this.getSafeLocation(location); - } - - if (location != null) { - if (teleportee.teleport(location)) { - return TeleportResult.SUCCESS; - } - return TeleportResult.FAIL_OTHER; - } - return TeleportResult.FAIL_UNSAFE; - } - - /** - * Returns a safe location for the entity to spawn at. - * - * @param e The entity to spawn - * @param d The MVDestination to take the entity to. - * @return A new location to spawn the entity at. - */ - public Location getSafeLocation(Entity e, MVDestination d) { - Location l = d.getLocation(e); - if (this.bs.playerCanSpawnHereSafely(l)) { - plugin.log(Level.FINE, "The first location you gave me was safe."); - return l; - } - if (e instanceof Minecart) { - Minecart m = (Minecart) e; - if (!this.bs.canSpawnCartSafely(m)) { - return null; - } - } else if (e instanceof Vehicle) { - Vehicle v = (Vehicle) e; - if (!this.bs.canSpawnVehicleSafely(v)) { - return null; - } - } - Location safeLocation = this.getSafeLocation(l); - if (safeLocation != null) { - // Add offset to account for a vehicle on dry land! - if (e instanceof Minecart && !this.bs.isEntitiyOnTrack(safeLocation)) { - safeLocation.setY(safeLocation.getBlockY() + .5); - this.plugin.log(Level.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!"); - 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()); - } 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()); - } - this.plugin.log(Level.FINE, "Sorry champ, you're basically trying to teleport into a minefield. I should just kill you now."); - return null; - } - - /** - * Finds a portal-block next to the specified {@link Location}. - * @param l The {@link Location} - * @return The next portal-block's {@link Location}. - */ - public static Location findPortalBlockNextTo(Location l) { - Block b = l.getWorld().getBlockAt(l); - Location foundLocation = null; - if (b.getType() == Material.PORTAL) { - return l; - } - if (b.getRelative(BlockFace.NORTH).getType() == Material.PORTAL) { - foundLocation = getCloserBlock(l, b.getRelative(BlockFace.NORTH).getLocation(), foundLocation); - } - if (b.getRelative(BlockFace.SOUTH).getType() == Material.PORTAL) { - foundLocation = getCloserBlock(l, b.getRelative(BlockFace.SOUTH).getLocation(), foundLocation); - } - if (b.getRelative(BlockFace.EAST).getType() == Material.PORTAL) { - foundLocation = getCloserBlock(l, b.getRelative(BlockFace.EAST).getLocation(), foundLocation); - } - if (b.getRelative(BlockFace.WEST).getType() == Material.PORTAL) { - foundLocation = getCloserBlock(l, b.getRelative(BlockFace.WEST).getLocation(), foundLocation); - } - return foundLocation; - } - - private static Location getCloserBlock(Location source, Location blockA, Location blockB) { - // If B wasn't given, return a. - if (blockB == null) { - return blockA; - } - // Center our calculations - blockA.add(.5, 0, .5); - blockB.add(.5, 0, .5); - - // Retrieve the distance to the normalized blocks - double testA = source.distance(blockA); - double testB = source.distance(blockB); - - // Compare and return - if (testA <= testB) { - return blockA; - } - return blockB; - } - -} diff --git a/src/main/java/com/onarandombox/MultiverseCore/utils/SimpleBlockSafety.java b/src/main/java/com/onarandombox/MultiverseCore/utils/SimpleBlockSafety.java index a75673801..3072d8a8d 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/utils/SimpleBlockSafety.java +++ b/src/main/java/com/onarandombox/MultiverseCore/utils/SimpleBlockSafety.java @@ -14,9 +14,10 @@ import org.bukkit.Material; import org.bukkit.World; import org.bukkit.block.BlockFace; +import org.bukkit.block.data.BlockData; +import org.bukkit.block.data.type.Bed; import org.bukkit.entity.Minecart; import org.bukkit.entity.Vehicle; -import org.bukkit.material.Bed; import java.util.EnumSet; import java.util.Iterator; @@ -89,7 +90,7 @@ public boolean playerCanSpawnHereSafely(Location l) { return false; } - if (downOne.getBlock().getType() == Material.LAVA || downOne.getBlock().getType() == Material.STATIONARY_LAVA) { + if (downOne.getBlock().getType() == Material.LAVA) { Logging.finer("Error Here (downOne)? (%s)[%s]", downOne.getBlock().getType(), isSolidBlock(downOne.getBlock().getType())); return false; } @@ -151,12 +152,13 @@ private Location getSafeSpawnAroundABlock(Location l) { * @return The location of the other bed piece, or null if it was a jacked up bed. */ private Location findOtherBedPiece(Location checkLoc) { - if (checkLoc.getBlock().getType() != Material.BED_BLOCK) { + BlockData data = checkLoc.getBlock().getBlockData(); + if (!(data instanceof Bed)) { return null; } - // Construct a bed object at this location - final Bed b = new Bed(Material.BED_BLOCK, checkLoc.getBlock().getData()); - if (b.isHeadOfBed()) { + Bed b = (Bed) data; + + if (b.getPart() == Bed.Part.HEAD) { return checkLoc.getBlock().getRelative(b.getFacing().getOppositeFace()).getLocation(); } // We shouldn't ever be looking at the foot, but here's the code for it. @@ -199,69 +201,8 @@ public Location getBottomBlock(Location l) { /* * If someone has a better way of this... Please either tell us, or submit a pull request! */ - private static boolean isSolidBlock(Material type) { - switch (type) { - case AIR: - return false; - case SNOW: - return false; - case TRAP_DOOR: - return false; - case TORCH: - return false; - case YELLOW_FLOWER: - return false; - case RED_ROSE: - return false; - case RED_MUSHROOM: - return false; - case BROWN_MUSHROOM: - return false; - case REDSTONE: - return false; - case REDSTONE_WIRE: - return false; - case RAILS: - return false; - case POWERED_RAIL: - return false; - case REDSTONE_TORCH_ON: - return false; - case REDSTONE_TORCH_OFF: - return false; - case DEAD_BUSH: - return false; - case SAPLING: - return false; - case STONE_BUTTON: - return false; - case LEVER: - return false; - case LONG_GRASS: - return false; - case PORTAL: - return false; - case STONE_PLATE: - return false; - case WOOD_PLATE: - return false; - case SEEDS: - return false; - case SUGAR_CANE_BLOCK: - return false; - case WALL_SIGN: - return false; - case SIGN_POST: - return false; - case WOODEN_DOOR: - return false; - case STATIONARY_WATER: - return false; - case WATER: - return false; - default: - return true; - } + public static boolean isSolidBlock(Material type) { + return type.isSolid(); } /** @@ -270,7 +211,10 @@ private static boolean isSolidBlock(Material type) { @Override public boolean isEntitiyOnTrack(Location l) { Material currentBlock = l.getBlock().getType(); - return (currentBlock == Material.POWERED_RAIL || currentBlock == Material.DETECTOR_RAIL || currentBlock == Material.RAILS); + return (currentBlock == Material.POWERED_RAIL + || currentBlock == Material.DETECTOR_RAIL + || currentBlock == Material.RAIL + || currentBlock == Material.ACTIVATOR_RAIL); } /** @@ -285,10 +229,10 @@ private boolean hasTwoBlocksofWaterBelow(Location l) { } Location oneBelow = l.clone(); oneBelow.subtract(0, 1, 0); - if (oneBelow.getBlock().getType() == Material.WATER || oneBelow.getBlock().getType() == Material.STATIONARY_WATER) { + if (oneBelow.getBlock().getType() == Material.WATER) { Location twoBelow = oneBelow.clone(); twoBelow.subtract(0, 1, 0); - return (oneBelow.getBlock().getType() == Material.WATER || oneBelow.getBlock().getType() == Material.STATIONARY_WATER); + return (oneBelow.getBlock().getType() == Material.WATER); } if (oneBelow.getBlock().getType() != Material.AIR) { return false; diff --git a/src/main/java/com/onarandombox/MultiverseCore/utils/SimpleLocationManipulation.java b/src/main/java/com/onarandombox/MultiverseCore/utils/SimpleLocationManipulation.java index 3d0436b1e..17f770e17 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 java.text.DecimalFormat; import java.util.Collections; import java.util.HashMap; +import java.util.Locale; import java.util.Map; /** @@ -52,7 +53,7 @@ public String locationToString(Location location) { 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 0608dc150..baae83e59 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/utils/SimpleSafeTTeleporter.java +++ b/src/main/java/com/onarandombox/MultiverseCore/utils/SimpleSafeTTeleporter.java @@ -292,19 +292,19 @@ public Location getSafeLocation(Entity e, MVDestination d) { public Location findPortalBlockNextTo(Location l) { Block b = l.getWorld().getBlockAt(l); Location foundLocation = null; - if (b.getType() == Material.PORTAL) { + if (b.getType() == Material.NETHER_PORTAL) { return l; } - if (b.getRelative(BlockFace.NORTH).getType() == Material.PORTAL) { + if (b.getRelative(BlockFace.NORTH).getType() == Material.NETHER_PORTAL) { foundLocation = getCloserBlock(l, b.getRelative(BlockFace.NORTH).getLocation(), foundLocation); } - if (b.getRelative(BlockFace.SOUTH).getType() == Material.PORTAL) { + if (b.getRelative(BlockFace.SOUTH).getType() == Material.NETHER_PORTAL) { foundLocation = getCloserBlock(l, b.getRelative(BlockFace.SOUTH).getLocation(), foundLocation); } - if (b.getRelative(BlockFace.EAST).getType() == Material.PORTAL) { + if (b.getRelative(BlockFace.EAST).getType() == Material.NETHER_PORTAL) { foundLocation = getCloserBlock(l, b.getRelative(BlockFace.EAST).getLocation(), foundLocation); } - if (b.getRelative(BlockFace.WEST).getType() == Material.PORTAL) { + if (b.getRelative(BlockFace.WEST).getType() == Material.NETHER_PORTAL) { foundLocation = getCloserBlock(l, b.getRelative(BlockFace.WEST).getLocation(), foundLocation); } return foundLocation; diff --git a/src/main/java/com/onarandombox/MultiverseCore/utils/SimpleWorldPurger.java b/src/main/java/com/onarandombox/MultiverseCore/utils/SimpleWorldPurger.java index 9c3456c7b..bfaa4df99 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/utils/SimpleWorldPurger.java +++ b/src/main/java/com/onarandombox/MultiverseCore/utils/SimpleWorldPurger.java @@ -20,6 +20,7 @@ import org.bukkit.entity.Golem; import org.bukkit.entity.LivingEntity; import org.bukkit.entity.Monster; +import org.bukkit.entity.Phantom; import org.bukkit.entity.Projectile; import org.bukkit.entity.Slime; import org.bukkit.entity.Squid; @@ -148,7 +149,7 @@ private boolean killDecision(Entity e, List thingsToKill, boolean negate if (specifiedAnimals) specified = true; negate = negateAnimals; - } else if (e instanceof Monster || e instanceof Ghast || e instanceof Slime) { + } else if (e instanceof Monster || e instanceof Ghast || e instanceof Slime || e instanceof Phantom) { // it's a monster if (specifiedMonsters && !negateMonsters) { Logging.finest("Removing an entity because I was told to remove all monsters in world %s: %s", e.getWorld().getName(), e); diff --git a/src/main/java/com/onarandombox/MultiverseCore/utils/SpoutInterface.java b/src/main/java/com/onarandombox/MultiverseCore/utils/SpoutInterface.java deleted file mode 100644 index 192215084..000000000 --- a/src/main/java/com/onarandombox/MultiverseCore/utils/SpoutInterface.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.utils; - -import org.getspout.spoutapi.SpoutManager; - -/** - * A helper-class holding the {@link SpoutManager}. - * / -public class SpoutInterface { - private SpoutManager spoutManager; - - public SpoutInterface() { - this.spoutManager = SpoutManager.getInstance(); - } - - /** - * Gets the {@link SpoutManager}. - * @return The {@link SpoutManager}. - * / - public SpoutManager getManager() { - return this.spoutManager; - } -} -*/ 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 000000000..81bb0f6e3 --- /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 681bfd1d3..015a4d8e5 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/utils/WorldManager.java +++ b/src/main/java/com/onarandombox/MultiverseCore/utils/WorldManager.java @@ -10,6 +10,7 @@ import com.dumptruckman.minecraft.util.Logging; import com.onarandombox.MultiverseCore.MVWorld; import com.onarandombox.MultiverseCore.MultiverseCore; +import com.onarandombox.MultiverseCore.MultiverseCoreConfiguration; import com.onarandombox.MultiverseCore.WorldProperties; import com.onarandombox.MultiverseCore.api.MVWorldManager; import com.onarandombox.MultiverseCore.api.MultiverseWorld; @@ -42,15 +43,19 @@ import java.util.Random; import java.util.Set; import java.util.Stack; +import java.util.Arrays; import java.util.concurrent.Callable; import java.util.concurrent.ConcurrentHashMap; import java.util.logging.Level; +import java.util.regex.Matcher; +import java.util.regex.Pattern; /** * Public facing API to add/remove Multiverse worlds. */ public class WorldManager implements MVWorldManager { private final MultiverseCore plugin; + private final Pattern worldNamePattern = Pattern.compile("[a-zA-Z0-9/._-]+"); private final WorldPurger worldPurger; private final Map worlds; private Map worldsFromTheConfig; @@ -124,12 +129,18 @@ public boolean cloneWorld(String oldName, String newName) { return false; } + // Check for valid world name + if (!(isValidWorldName(oldName) && 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; } @@ -151,14 +162,8 @@ public boolean cloneWorld(String oldName, String newName) { } // 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); @@ -176,32 +181,40 @@ public boolean cloneWorld(String oldName, String newName) { 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.getLogger())) { Logging.warning("Failed to copy files for world '%s', see the log info", newName); return false; } if (oldWorld != null && wasAutoSave) { oldWorld.getCBWorld().setAutoSave(true); } - - File uidFile = new File(newWorldFile, "uid.dat"); - if (uidFile.exists() && !uidFile.delete()) { - Logging.warning("Failed to delete unique ID file for world '%s'", newName); - return false; - } - + if (newWorldFile.exists()) { Logging.fine("Succeeded at copying files"); - if (this.addWorld(newName, environment, seedString, worldType, generateStructures, generator, useSpawnAdjust)) { - // getMVWorld() doesn't actually return an MVWorld - Logging.fine("Succeeded at importing world"); - MVWorld newWorld = (MVWorld) this.getMVWorld(newName); - newWorld.copyValues(this.worldsFromTheConfig.get(oldName)); - // don't keep the alias the same -- that would be useless - newWorld.setAlias(null); - return true; + + // 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()) { + this.plugin.log(Level.SEVERE, "Failed to save worlds.yml"); + return false; + } + + // actually load the world + if (doLoad(newName)) { + this.plugin.log(Level.FINE, "Succeeded at loading cloned world '" + newName + "'"); + return true; } + this.plugin.log(Level.SEVERE, "Failed to load the cloned world '" + newName + "'"); + return false; } + Logging.warning("Failed to copy files for world '%s', see the log info", newName); return false; } @@ -224,6 +237,11 @@ public boolean addWorld(String name, Environment env, String seedString, WorldTy if (name.equalsIgnoreCase("plugins") || name.equalsIgnoreCase("logs")) { return false; } + + if (!isValidWorldName(name)) { + return false; + } + Long seed = null; WorldCreator c = new WorldCreator(name); if (seedString != null && seedString.length() > 0) { @@ -406,6 +424,21 @@ public boolean loadWorld(String name) { } } + /** + * Check if the world name is allowed + * + * @param name Name of the world + * @return True if the world world name is valid based on regex + */ + private boolean isValidWorldName(String name) { + if (!worldNamePattern.matcher(name).matches()) { + Logging.warning("Invalid world name '" + name + "'"); + Logging.warning("World name should not contain spaces or special characters!"); + return false; + } + return true; + } + 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 + "'"); @@ -474,7 +507,9 @@ private boolean doLoad(WorldCreator creator, boolean ignoreExists) { return false; } MVWorld world = new MVWorld(plugin, cbworld, mvworld); - this.worldPurger.purgeWorld(world); + if (MultiverseCoreConfiguration.getInstance().isAutoPurgeEnabled()) { + this.worldPurger.purgeWorld(world); + } this.worlds.put(worldName, world); return true; } @@ -484,6 +519,13 @@ private boolean doLoad(WorldCreator creator, boolean ignoreExists) { */ @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 @@ -896,6 +938,9 @@ public FileConfiguration getConfigWorlds() { return this.configWorlds; } + /** + * {@inheritDoc} + */ @Override public boolean hasUnloadedWorld(String name, boolean includeLoaded) { if (getMVWorld(name) != null) { diff --git a/src/main/java/com/onarandombox/MultiverseCore/utils/metrics/MetricsConfigurator.java b/src/main/java/com/onarandombox/MultiverseCore/utils/metrics/MetricsConfigurator.java new file mode 100644 index 000000000..5b34acad2 --- /dev/null +++ b/src/main/java/com/onarandombox/MultiverseCore/utils/metrics/MetricsConfigurator.java @@ -0,0 +1,95 @@ +package com.onarandombox.MultiverseCore.utils.metrics; + +import java.util.Collection; +import java.util.Map; +import java.util.function.Consumer; + +import com.dumptruckman.minecraft.util.Logging; +import com.onarandombox.MultiverseCore.MultiverseCore; +import com.onarandombox.MultiverseCore.api.MVWorldManager; +import com.onarandombox.MultiverseCore.api.MultiverseWorld; +import org.apache.commons.lang.WordUtils; +import org.bstats.bukkit.Metrics; +import org.bukkit.World; + +public class MetricsConfigurator { + + private static final int PLUGIN_ID = 7765; + private static final String NO_GENERATOR_NAME = "N/A"; + + public static void configureMetrics(MultiverseCore plugin) { + MetricsConfigurator configurator = new MetricsConfigurator(plugin); + configurator.initMetrics(); + } + + private final MultiverseCore plugin; + private final Metrics metrics; + + private MetricsConfigurator(MultiverseCore plugin) { + this.plugin = plugin; + this.metrics = new Metrics(plugin, PLUGIN_ID); + } + + private MVWorldManager getWorldManager() { + return plugin.getMVWorldManager(); + } + + private Collection getMVWorlds() { + return getWorldManager().getMVWorlds(); + } + + private void initMetrics() { + try { + addCustomGeneratorsMetric(); + addEnvironmentsMetric(); + addWorldCountMetric(); + + Logging.fine("Metrics enabled."); + } catch (Exception e) { + Logging.warning("There was an issue while enabling metrics:"); + e.printStackTrace(); + } + } + + private void addCustomGeneratorsMetric() { + addAdvancedPieMetric("custom_generators", map -> { + for (MultiverseWorld w : getMVWorlds()) { + MetricsHelper.incrementCount(map, getGeneratorName(w)); + } + }); + } + + private String getGeneratorName(MultiverseWorld world) { + String gen = world.getGenerator(); + return (gen != null && !gen.equalsIgnoreCase("null")) ? gen.split(":")[0] : NO_GENERATOR_NAME; + } + + private void addEnvironmentsMetric() { + addAdvancedPieMetric("environments", map -> { + for (MultiverseWorld w : getMVWorlds()) { + MetricsHelper.incrementCount(map, titleCaseEnv(w.getEnvironment())); + } + }); + } + + private String titleCaseEnv(World.Environment env) { + String envName = env.name().replaceAll("_+", " "); + return WordUtils.capitalizeFully(envName); + } + + private void addWorldCountMetric() { + addMultiLineMetric("world_count", map -> { + int loadedWorldsCount = getMVWorlds().size(); + map.put("Loaded worlds", loadedWorldsCount); + map.put("Total number of worlds", loadedWorldsCount + getWorldManager().getUnloadedWorlds().size()); + }); + } + + private void addAdvancedPieMetric(String chartId, Consumer> metricsFunc) { + metrics.addCustomChart(MetricsHelper.createAdvancedPieChart(chartId, metricsFunc)); + } + + private void addMultiLineMetric(String chartId, Consumer> metricsFunc) { + metrics.addCustomChart(MetricsHelper.createMultiLineChart(chartId, metricsFunc)); + } +} diff --git a/src/main/java/com/onarandombox/MultiverseCore/utils/metrics/MetricsHelper.java b/src/main/java/com/onarandombox/MultiverseCore/utils/metrics/MetricsHelper.java new file mode 100644 index 000000000..23fb6eba6 --- /dev/null +++ b/src/main/java/com/onarandombox/MultiverseCore/utils/metrics/MetricsHelper.java @@ -0,0 +1,32 @@ +package com.onarandombox.MultiverseCore.utils.metrics; + +import java.util.HashMap; +import java.util.Map; +import java.util.function.Consumer; + +import org.bstats.bukkit.Metrics; + +enum MetricsHelper { + ; + + /** + * Adds one to the value in the given map with the given key. If the key does not exist in the map, it will be added with a value of 1. + */ + static void incrementCount(Map map, String key) { + Integer count = map.getOrDefault(key, 0); + map.put(key, count + 1); + } + + static Metrics.AdvancedPie createAdvancedPieChart(String chartId, Consumer> metricsFunc) { + Map map = new HashMap<>(); + metricsFunc.accept(map); + return new Metrics.AdvancedPie(chartId, () -> map); + } + + static Metrics.MultiLineChart createMultiLineChart(String chartId, Consumer> metricsFunc) { + Map map = new HashMap<>(); + metricsFunc.accept(map); + return new Metrics.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 e700c2e32..aa7d29d75 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/utils/webpaste/BitlyURLShortener.java +++ b/src/main/java/com/onarandombox/MultiverseCore/utils/webpaste/BitlyURLShortener.java @@ -1,19 +1,43 @@ package com.onarandombox.MultiverseCore.utils.webpaste; +import org.json.simple.JSONObject; +import org.json.simple.parser.JSONParser; +import org.json.simple.parser.ParseException; + import java.io.IOException; +import java.util.Map; /** - * An {@link URLShortener} using {@code bit.ly}. + * A {@link URLShortener} using {@code bit.ly}. Requires an access token. */ -public class BitlyURLShortener extends HttpAPIClient implements URLShortener { - private static final String GENERIC_BITLY_REQUEST_FORMAT = "https://api-ssl.bitly.com/v3/shorten?format=txt&apiKey=%s&login=%s&longUrl=%s"; +class BitlyURLShortener extends URLShortener { + private static final String ACCESS_TOKEN = "Bearer bitly-access-token"; + private static final String BITLY_POST_REQUEST = "https://api-ssl.bitly.com/v4/shorten"; - // I think it's no problem that these are public - private static final String USERNAME = "multiverse2"; - private static final String API_KEY = "R_9dbff4862a3bc0c4218a7d78cc10d0e0"; + BitlyURLShortener() { + super(BITLY_POST_REQUEST, ACCESS_TOKEN); + if (ACCESS_TOKEN.endsWith("access-token")) { + throw new UnsupportedOperationException(); + } + } - public BitlyURLShortener() { - super(String.format(GENERIC_BITLY_REQUEST_FORMAT, API_KEY, USERNAME, "%s")); + /** + * {@inheritDoc} + */ + @Override + String encodeData(String data) { + JSONObject json = new JSONObject(); + json.put("domain", "j.mp"); + json.put("long_url", data); + return json.toJSONString(); + } + + /** + * {@inheritDoc} + */ + @Override + String encodeData(Map data) { + throw new UnsupportedOperationException(); } /** @@ -22,13 +46,11 @@ public BitlyURLShortener() { @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 000000000..bee098dfe --- /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 86f9888ed..000000000 --- a/src/main/java/com/onarandombox/MultiverseCore/utils/webpaste/GithubPasteService.java +++ /dev/null @@ -1,105 +0,0 @@ -package com.onarandombox.MultiverseCore.utils.webpaste; - -import org.codehaus.jettison.json.JSONException; -import org.codehaus.jettison.json.JSONObject; - -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(); - String result = ""; - try { - 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); - result = root.toString(); - } catch (JSONException e) { - e.printStackTrace(); - } - return result; - } - - @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); - } - JSONObject response = new JSONObject(responseString.toString()); - return response.get("html_url").toString(); - } 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 new file mode 100644 index 000000000..e1fa7272c --- /dev/null +++ b/src/main/java/com/onarandombox/MultiverseCore/utils/webpaste/HastebinPasteService.java @@ -0,0 +1,69 @@ +package com.onarandombox.MultiverseCore.utils.webpaste; + +import org.json.simple.JSONObject; +import org.json.simple.parser.JSONParser; +import org.json.simple.parser.ParseException; + +import java.io.IOException; +import java.util.Map; + +/** + * Pastes to {@code hastebin.com}. + */ +class HastebinPasteService extends PasteService { + private static final String HASTEBIN_POST_REQUEST = "https://hastebin.com/documents"; + + HastebinPasteService() { + super(HASTEBIN_POST_REQUEST); + } + + /** + * {@inheritDoc} + */ + @Override + String encodeData(String data) { + return data; + } + + /** + * {@inheritDoc} + */ + @Override + String encodeData(Map data) { + throw new UnsupportedOperationException(); + } + + /** + * {@inheritDoc} + */ + @Override + public String postData(String 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 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 55d69c5c0..676ef7cd8 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); + + // 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); + } - while (reader.ready()) { - ret.append(reader.readLine()).append('\n'); + 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 { + wr.close(); + } catch (IOException ignore) { } + } + if (rd != null) { try { - reader.close(); + 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 85a803a4b..82792f499 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 000000000..956d71c1c --- /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 71ba3a8e0..a6bc15c4f 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 - * instance is submitting; an example of this is the PastiePasteService class. + * 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); + } - /** - * 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. - */ - String encodeData(String data); + PasteService(String url, String accessToken) { + super(url, accessToken); + } /** - * Encode the given Map 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(Map data); - - /** - * Get the URL to which this paste service sends new pastes. - * - * @return The URL that will be accessed to complete the paste. + * @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. */ - URL getPostURL(); + public abstract String postData(String data) throws PasteFailedException; /** - * Post encoded data to the Web. + * Post 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 Map 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(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 1319ac20a..f6f63a1ce 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 @@ private PasteServiceFactory() { } */ public static PasteService getService(PasteServiceType type, boolean isPrivate) { switch(type) { + case PASTEGG: + return new PasteGGPasteService(isPrivate); case PASTEBIN: return new PastebinPasteService(isPrivate); - case PASTIE: - return new PastiePasteService(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 5954dd884..09424c0b5 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/utils/webpaste/PasteServiceType.java +++ b/src/main/java/com/onarandombox/MultiverseCore/utils/webpaste/PasteServiceType.java @@ -7,16 +7,20 @@ * @see PasteServiceFactory */ public enum PasteServiceType { + /** + * @see PasteGGPasteService + */ + PASTEGG, /** * @see PastebinPasteService */ PASTEBIN, /** - * @see PastiePasteService + * @see HastebinPasteService */ - PASTIE, + 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 3f06f612e..eff401933 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,11 +21,16 @@ public PastebinPasteService(boolean isPrivate) { * {@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 + 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 } } @@ -38,61 +38,37 @@ public URL getPostURL() { * {@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; - } catch (UnsupportedEncodingException e) { - return ""; // should never hit here - } + String encodeData(Map data) { + throw new UnsupportedOperationException(); } + /** + * {@inheritDoc} + */ @Override - public String encodeData(Map data) { - return null; + public String postData(String data) throws PasteFailedException { + try { + return this.exec(encodeData(data), ContentType.URLENCODED); + } catch (IOException e) { + throw new PasteFailedException(e); + } } /** * {@inheritDoc} */ @Override - public String postData(String encodedData, URL url) throws PasteFailedException { - OutputStreamWriter wr = null; - BufferedReader rd = null; + public String postData(Map 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 boolean supportsMultiFile() { return false; diff --git a/src/main/java/com/onarandombox/MultiverseCore/utils/webpaste/PastiePasteService.java b/src/main/java/com/onarandombox/MultiverseCore/utils/webpaste/PastiePasteService.java deleted file mode 100644 index 16c062a2f..000000000 --- a/src/main/java/com/onarandombox/MultiverseCore/utils/webpaste/PastiePasteService.java +++ /dev/null @@ -1,118 +0,0 @@ -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; -import java.util.regex.Matcher; -import java.util.regex.Pattern; - -/** - * Pastes to {@code pastie.org}. - */ -public class PastiePasteService implements PasteService { - - private boolean isPrivate; - - public PastiePasteService(boolean isPrivate) { - this.isPrivate = isPrivate; - } - - /** - * {@inheritDoc} - */ - @Override - public URL getPostURL() { - try { - return new URL("http://pastie.org/pastes"); - } catch (MalformedURLException e) { - return null; // should never hit here - } - } - - /** - * {@inheritDoc} - */ - @Override - public String encodeData(String data) { - try { - String encData = URLEncoder.encode("paste[authorization]", "UTF-8") + "=" + URLEncoder.encode("burger", "UTF-8"); // burger is magic - encData += "&" + URLEncoder.encode("paste[restricted]", "UTF-8") + "=" + URLEncoder.encode(this.isPrivate ? "1" : "0", "UTF-8"); - encData += "&" + URLEncoder.encode("paste[parser_id]", "UTF-8") + "=" + URLEncoder.encode("6", "UTF-8"); // 6 is plain text - encData += "&" + URLEncoder.encode("paste[body]", "UTF-8") + "=" + URLEncoder.encode(data, "UTF-8"); - return encData; - } catch (UnsupportedEncodingException e) { - return ""; // should never hit here - } - } - - @Override - public String encodeData(Map data) { - return null; - } - - /** - * {@inheritDoc} - */ - @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(); - while ((line = rd.readLine()) != null) { - Matcher m = pastiePattern.matcher(line); - if (m.matches()) { - String pastieID = m.group(1); - pastieUrl = this.formatURL(pastieID); - } - } - return pastieUrl; - } 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 false; - } - - private Pattern getURLMatchingPattern() { - if (this.isPrivate) { - return Pattern.compile(".*http://pastie.org/.*key=([0-9a-z]+).*"); - } else { - return Pattern.compile(".*http://pastie.org/([0-9]+).*"); - } - } - - private String formatURL(String pastieID) { - return "http://pastie.org/" + (this.isPrivate ? "private/" : "") + pastieID; - } -} 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 75d50f634..bde8ff1d8 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 000000000..c0f3cafac --- /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 000000000..d2c809f51 --- /dev/null +++ b/src/main/java/com/onarandombox/MultiverseCore/utils/webpaste/URLShortenerType.java @@ -0,0 +1,14 @@ +package com.onarandombox.MultiverseCore.utils.webpaste; + +/** + * An enum containing all known {@link URLShortener}s. + * + * @see URLShortener + * @see URLShortenerFactory + */ +public enum URLShortenerType { + /** + * @see BitlyURLShortener + */ + BITLY +} diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index 945bbbbec..d46f0fd7e 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -1,6 +1,9 @@ name: Multiverse-Core main: com.onarandombox.MultiverseCore.MultiverseCore -authors: ['Rigby', 'fernferret', 'lithium3141', 'main--'] +authors: ['dumptruckman', 'Rigby', 'fernferret', 'lithium3141', 'main--'] +website: 'https://dev.bukkit.org/projects/multiverse-core' +softdepend: ['Vault'] +api-version: 1.13 version: maven-version-number commands: mv: @@ -8,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: @@ -44,103 +37,75 @@ commands: / mvmodify: description: Modify the settings of an existing world + aliases: [mvm] usage: | - /