diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ba19071 --- /dev/null +++ b/.gitignore @@ -0,0 +1,18 @@ + +build/classes/ + +build/libs/ + +build/resources/ + +build/tmp/ + +\.gradle/ + +\.idea/ + +gradle/ + + + +build/\.DS_Store diff --git a/README.md b/README.md index bc0a908..c9cd9b4 100644 --- a/README.md +++ b/README.md @@ -1 +1,30 @@ -RandomTeleport +#RandomTeleport + +### Plugin for Bukkit/Spigot core. + +> Testing only on 1.13.2 + + +## Features +* Only sign +* Black list blocks available + + +```yaml +settings: + sign: + name: + pretty: §l§b[§4RandomTP§b] #Name after create sign for Random TP + raw: '[rtp]' # What need write for create RandomTP sign + teleport: + max: + x: 3000 # at -3000 to 3000 X Teleport. Point location sign + z: 3000 # at -3000 to 3000 Z Teleport. Point location sign + block: + blackList: + - LAVA + - WATER + - CACTUS + - CACTUS_GREEN + +``` diff --git a/build.gradle b/build.gradle new file mode 100644 index 0000000..b617290 --- /dev/null +++ b/build.gradle @@ -0,0 +1,28 @@ +apply plugin: 'java' + +group = pluginGroup +version = pluginVersion + +sourceCompatibility = 1.8 +targetCompatibility = 1.8 + +repositories { + mavenCentral() + maven { + name = 'spigotmc-repo' + url = 'https://hub.spigotmc.org/nexus/content/groups/public/' + } +} + +dependencies { + testCompile group: 'junit', name: 'junit', version: '4.12' + compile 'org.bukkit:bukkit:1.13.2-R0.1-SNAPSHOT' +} + +import org.apache.tools.ant.filters.ReplaceTokens + +processResources { + from(sourceSets.main.resources.srcDirs) { + filter ReplaceTokens, tokens: [version: version] + } +} diff --git a/build/version/1.0/RandomTeleport.jar b/build/version/1.0/RandomTeleport.jar new file mode 100644 index 0000000..04888b1 Binary files /dev/null and b/build/version/1.0/RandomTeleport.jar differ diff --git a/gradle.properties b/gradle.properties new file mode 100644 index 0000000..04f7d2e --- /dev/null +++ b/gradle.properties @@ -0,0 +1,2 @@ +pluginGroup=ru.zendal +pluginVersion=1.0-SNAPSHOT diff --git a/gradlew b/gradlew new file mode 100755 index 0000000..cccdd3d --- /dev/null +++ b/gradlew @@ -0,0 +1,172 @@ +#!/usr/bin/env sh + +############################################################################## +## +## Gradle start up script for UN*X +## +############################################################################## + +# Attempt to set APP_HOME +# Resolve links: $0 may be a link +PRG="$0" +# Need this for relative symlinks. +while [ -h "$PRG" ] ; do + ls=`ls -ld "$PRG"` + link=`expr "$ls" : '.*-> \(.*\)$'` + if expr "$link" : '/.*' > /dev/null; then + PRG="$link" + else + PRG=`dirname "$PRG"`"/$link" + fi +done +SAVED="`pwd`" +cd "`dirname \"$PRG\"`/" >/dev/null +APP_HOME="`pwd -P`" +cd "$SAVED" >/dev/null + +APP_NAME="Gradle" +APP_BASE_NAME=`basename "$0"` + +# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +DEFAULT_JVM_OPTS="" + +# Use the maximum available, or set MAX_FD != -1 to use that value. +MAX_FD="maximum" + +warn () { + echo "$*" +} + +die () { + echo + echo "$*" + echo + exit 1 +} + +# OS specific support (must be 'true' or 'false'). +cygwin=false +msys=false +darwin=false +nonstop=false +case "`uname`" in + CYGWIN* ) + cygwin=true + ;; + Darwin* ) + darwin=true + ;; + MINGW* ) + msys=true + ;; + NONSTOP* ) + nonstop=true + ;; +esac + +CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar + +# Determine the Java command to use to start the JVM. +if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD="$JAVA_HOME/jre/sh/java" + else + JAVACMD="$JAVA_HOME/bin/java" + fi + if [ ! -x "$JAVACMD" ] ; then + die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +else + JAVACMD="java" + which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." +fi + +# Increase the maximum file descriptors if we can. +if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then + MAX_FD_LIMIT=`ulimit -H -n` + if [ $? -eq 0 ] ; then + if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then + MAX_FD="$MAX_FD_LIMIT" + fi + ulimit -n $MAX_FD + if [ $? -ne 0 ] ; then + warn "Could not set maximum file descriptor limit: $MAX_FD" + fi + else + warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" + fi +fi + +# For Darwin, add options to specify how the application appears in the dock +if $darwin; then + GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" +fi + +# For Cygwin, switch paths to Windows format before running java +if $cygwin ; then + APP_HOME=`cygpath --path --mixed "$APP_HOME"` + CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` + JAVACMD=`cygpath --unix "$JAVACMD"` + + # We build the pattern for arguments to be converted via cygpath + ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` + SEP="" + for dir in $ROOTDIRSRAW ; do + ROOTDIRS="$ROOTDIRS$SEP$dir" + SEP="|" + done + OURCYGPATTERN="(^($ROOTDIRS))" + # Add a user-defined pattern to the cygpath arguments + if [ "$GRADLE_CYGPATTERN" != "" ] ; then + OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" + fi + # Now convert the arguments - kludge to limit ourselves to /bin/sh + i=0 + for arg in "$@" ; do + CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` + CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option + + if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition + eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` + else + eval `echo args$i`="\"$arg\"" + fi + i=$((i+1)) + done + case $i in + (0) set -- ;; + (1) set -- "$args0" ;; + (2) set -- "$args0" "$args1" ;; + (3) set -- "$args0" "$args1" "$args2" ;; + (4) set -- "$args0" "$args1" "$args2" "$args3" ;; + (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; + (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; + (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; + (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; + (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; + esac +fi + +# Escape application args +save () { + for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done + echo " " +} +APP_ARGS=$(save "$@") + +# Collect all arguments for the java command, following the shell quoting and substitution rules +eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" + +# by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong +if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then + cd "$(dirname "$0")" +fi + +exec "$JAVACMD" "$@" diff --git a/gradlew.bat b/gradlew.bat new file mode 100644 index 0000000..e95643d --- /dev/null +++ b/gradlew.bat @@ -0,0 +1,84 @@ +@if "%DEBUG%" == "" @echo off +@rem ########################################################################## +@rem +@rem Gradle startup script for Windows +@rem +@rem ########################################################################## + +@rem Set local scope for the variables with windows NT shell +if "%OS%"=="Windows_NT" setlocal + +set DIRNAME=%~dp0 +if "%DIRNAME%" == "" set DIRNAME=. +set APP_BASE_NAME=%~n0 +set APP_HOME=%DIRNAME% + +@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +set DEFAULT_JVM_OPTS= + +@rem Find java.exe +if defined JAVA_HOME goto findJavaFromJavaHome + +set JAVA_EXE=java.exe +%JAVA_EXE% -version >NUL 2>&1 +if "%ERRORLEVEL%" == "0" goto init + +echo. +echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:findJavaFromJavaHome +set JAVA_HOME=%JAVA_HOME:"=% +set JAVA_EXE=%JAVA_HOME%/bin/java.exe + +if exist "%JAVA_EXE%" goto init + +echo. +echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:init +@rem Get command-line arguments, handling Windows variants + +if not "%OS%" == "Windows_NT" goto win9xME_args + +:win9xME_args +@rem Slurp the command line arguments. +set CMD_LINE_ARGS= +set _SKIP=2 + +:win9xME_args_slurp +if "x%~1" == "x" goto execute + +set CMD_LINE_ARGS=%* + +:execute +@rem Setup the command line + +set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar + +@rem Execute Gradle +"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% + +:end +@rem End local scope for the variables with windows NT shell +if "%ERRORLEVEL%"=="0" goto mainEnd + +:fail +rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of +rem the _cmd.exe /c_ return code! +if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 +exit /b 1 + +:mainEnd +if "%OS%"=="Windows_NT" endlocal + +:omega diff --git a/src/main/java/ru/zendal/randomteleport/RandomTeleport.java b/src/main/java/ru/zendal/randomteleport/RandomTeleport.java new file mode 100644 index 0000000..a9554a2 --- /dev/null +++ b/src/main/java/ru/zendal/randomteleport/RandomTeleport.java @@ -0,0 +1,19 @@ +package ru.zendal.randomteleport; + +import org.bukkit.plugin.java.JavaPlugin; +import ru.zendal.randomteleport.config.FileConfigurationWrapper; +import ru.zendal.randomteleport.event.SignRandomTeleport; + +public final class RandomTeleport extends JavaPlugin { + + @Override + public void onEnable() { + this.getServer().getPluginManager().registerEvents(new SignRandomTeleport(new FileConfigurationWrapper(this.getConfig())), this); + this.saveConfig(); + + } + + @Override + public void onDisable() { + } +} diff --git a/src/main/java/ru/zendal/randomteleport/config/ConfigurationRandomTeleport.java b/src/main/java/ru/zendal/randomteleport/config/ConfigurationRandomTeleport.java new file mode 100644 index 0000000..e5b9b6c --- /dev/null +++ b/src/main/java/ru/zendal/randomteleport/config/ConfigurationRandomTeleport.java @@ -0,0 +1,19 @@ +package ru.zendal.randomteleport.config; + +import org.bukkit.Material; +import org.bukkit.block.Block; + +import java.util.List; + +public interface ConfigurationRandomTeleport { + + String getPrettySignTitle(); + + String getRawSignTitle(); + + Integer getMaxXAtLocation(); + + Integer getMaxZAtLocation(); + + List getBlockBlackList(); +} diff --git a/src/main/java/ru/zendal/randomteleport/config/FileConfigurationWrapper.java b/src/main/java/ru/zendal/randomteleport/config/FileConfigurationWrapper.java new file mode 100644 index 0000000..1ce624b --- /dev/null +++ b/src/main/java/ru/zendal/randomteleport/config/FileConfigurationWrapper.java @@ -0,0 +1,87 @@ +package ru.zendal.randomteleport.config; + +import org.bukkit.Material; +import org.bukkit.configuration.file.FileConfiguration; + +import java.util.ArrayList; +import java.util.List; + +/** + * Wrapper of FileConfiguration + */ +public class FileConfigurationWrapper implements ConfigurationRandomTeleport { + + + private FileConfiguration source; + + /** + * Instantiates a new File configuration wrapper. + * + * @param fileConfiguration the file configuration + */ + public FileConfigurationWrapper(FileConfiguration fileConfiguration) { + this.source = fileConfiguration; + this.initialize(); + } + + + private void initialize() { + + if (!this.source.contains("settings.sign.name.pretty")) { + this.source.set("settings.sign.name.pretty", "§l§b[§4RandomTP§b]"); + } + + if (!this.source.contains("settings.sign.name.raw")) { + this.source.set("settings.sign.name.raw", "[RandomTP]"); + } + + if (!this.source.contains("settings.teleport.max.x")) { + this.source.set("settings.teleport.max.x", 3000); + } + + if (!this.source.contains("settings.teleport.max.z")) { + this.source.set("settings.teleport.max.z", 3000); + } + + if (!this.source.contains("settings.teleport.block.blackList")) { + List blockBlackList = new ArrayList<>(); + blockBlackList.add("LAVA"); + blockBlackList.add("WATER"); + blockBlackList.add("CACTUS"); + blockBlackList.add("CACTUS_GREEN"); + this.source.set("settings.teleport.block.blackList", blockBlackList); + } + + } + + @Override + public String getPrettySignTitle() { + return this.source.getString("settings.sign.name.pretty"); + } + + @Override + public String getRawSignTitle() { + return this.source.getString("settings.sign.name.raw"); + } + + @Override + public Integer getMaxXAtLocation() { + return this.source.getInt("settings.teleport.max.x"); + } + + @Override + public Integer getMaxZAtLocation() { + return this.source.getInt("settings.teleport.max.z"); + } + + @Override + public List getBlockBlackList() { + List rawBlockBlackList = this.source.getStringList("settings.teleport.block.blackList"); + List blockBlackList = new ArrayList<>(); + + rawBlockBlackList.forEach(rawBlockBlack -> { + blockBlackList.add(Material.getMaterial(rawBlockBlack)); + }); + return blockBlackList; + } +} diff --git a/src/main/java/ru/zendal/randomteleport/event/SignRandomTeleport.java b/src/main/java/ru/zendal/randomteleport/event/SignRandomTeleport.java new file mode 100644 index 0000000..0489f24 --- /dev/null +++ b/src/main/java/ru/zendal/randomteleport/event/SignRandomTeleport.java @@ -0,0 +1,89 @@ +package ru.zendal.randomteleport.event; + +import org.bukkit.Material; +import org.bukkit.block.Block; +import org.bukkit.block.Sign; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.block.Action; +import org.bukkit.event.block.SignChangeEvent; +import org.bukkit.event.player.PlayerInteractEvent; +import ru.zendal.randomteleport.config.ConfigurationRandomTeleport; +import ru.zendal.randomteleport.util.LocationFinder; + +/** + * The type Sign random teleport. + */ +public class SignRandomTeleport implements Listener { + + /** + * Configuration of plugin + */ + private ConfigurationRandomTeleport configuration; + + /** + * Instantiates a new Sign random teleport. + * + * @param configuration the configuration + */ + public SignRandomTeleport(ConfigurationRandomTeleport configuration) { + this.configuration = configuration; + } + + /** + * On sign call. + * + * @param event the event + */ + @EventHandler + public void onSignCall(PlayerInteractEvent event) { + if (event.getAction() != Action.RIGHT_CLICK_BLOCK && event.getAction() != Action.LEFT_CLICK_BLOCK) { + return; + } + if (event.getClickedBlock() == null) { + return; + } + + Block block = event.getClickedBlock(); + if (block.getType() == Material.SIGN || block.getType() == Material.WALL_SIGN && block.getState() instanceof Sign) { + Sign sign = (Sign) block.getState(); + if (sign.getLine(0).equals(this.preparePrettySignTitle(configuration.getPrettySignTitle()))) { + event.getPlayer().teleport(LocationFinder.getRandomLocation(sign.getLocation(), configuration.getMaxXAtLocation(), configuration.getMaxZAtLocation(), configuration.getBlockBlackList())); + } + } + } + + /** + * Prepare title for compare + * + * @param prettySignTitle title in config + * @return prepare title + */ + private String preparePrettySignTitle(String prettySignTitle) { + return prettySignTitle.replaceAll("§l", ""); + } + + /** + * On sign create. + * + * @param event the event + */ + @EventHandler + public void onSignCreate(SignChangeEvent event) { + if (!this.isServiceSign(event) || !event.getPlayer().hasPermission("randomteleport.sign.create")) { + return; + } + event.setLine(0, configuration.getPrettySignTitle()); + } + + /** + * Check is sign service + * + * @param event sing change event + * @return {@code true} if service sign else {@code false} + */ + private boolean isServiceSign(SignChangeEvent event) { + return event.getLine(0).toLowerCase().equals(configuration.getRawSignTitle().toLowerCase()); + } + +} diff --git a/src/main/java/ru/zendal/randomteleport/util/LocationFinder.java b/src/main/java/ru/zendal/randomteleport/util/LocationFinder.java new file mode 100644 index 0000000..d6a5b15 --- /dev/null +++ b/src/main/java/ru/zendal/randomteleport/util/LocationFinder.java @@ -0,0 +1,59 @@ +package ru.zendal.randomteleport.util; + +import org.bukkit.Location; +import org.bukkit.Material; + +import java.util.Collection; +import java.util.Random; + +/** + * The type Location finder. + */ +public class LocationFinder { + + /** + * Gets random location. + * + * @param pointLocation the point location + * @param maxX the max x + * @param maxZ the max z + * @param blackListBlock the black list block + * @return the random location + */ + public static Location getRandomLocation(Location pointLocation, int maxX, int maxZ, Collection blackListBlock) { + Location result; + do { + Location randomLocation = LocationFinder.getRandomLocation(pointLocation, maxX, maxZ); + result = LocationFinder.getLocationForTeleport(randomLocation, blackListBlock); + } while (result == null); + return result; + } + + + private static Location getRandomLocation(Location center, int maxX, int maxZ) { + int newX = LocationFinder.getRandomInteger(maxX * -1, maxX); + int newZ = LocationFinder.getRandomInteger(maxZ * -1, maxZ); + return new Location(center.getWorld(), newX, center.getWorld().getMaxHeight(), newZ); + } + + + private static int getRandomInteger(int min, int max) { + Random random = new Random(); + return min + random.nextInt(max - min + 1); + } + + private static Location getLocationForTeleport(Location location, Collection blackListBlock) { + while (location.getBlockY() != 0) { + location.setY(location.getY() - 1); + if (location.getBlock().getType() == Material.AIR) { + continue; + } + if (blackListBlock.stream().anyMatch(material -> material == location.getBlock().getType())) { + return null; + } + location.setY(location.getY() + 2); + return location; + } + return null; + } +} diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml new file mode 100644 index 0000000..df20ba8 --- /dev/null +++ b/src/main/resources/plugin.yml @@ -0,0 +1,5 @@ +name: RandomTeleport +version: @version@ +main: ru.zendal.randomteleport.RandomTeleport +api-version: 1.13 +authors: [BigTows]