diff --git a/.classpath b/.classpath new file mode 100644 index 0000000..fceb480 --- /dev/null +++ b/.classpath @@ -0,0 +1,6 @@ + + + + + + diff --git a/.gitignore b/.gitignore index 6143e53..b18c726 100644 --- a/.gitignore +++ b/.gitignore @@ -20,3 +20,6 @@ # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml hs_err_pid* + +bin/ +target/ diff --git a/.project b/.project new file mode 100644 index 0000000..3ada7bc --- /dev/null +++ b/.project @@ -0,0 +1,17 @@ + + + RandomTeleport_ + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/.settings/org.eclipse.jdt.core.prefs b/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000..3a21537 --- /dev/null +++ b/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,11 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 +org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve +org.eclipse.jdt.core.compiler.compliance=1.8 +org.eclipse.jdt.core.compiler.debug.lineNumber=generate +org.eclipse.jdt.core.compiler.debug.localVariable=generate +org.eclipse.jdt.core.compiler.debug.sourceFile=generate +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error +org.eclipse.jdt.core.compiler.source=1.8 diff --git a/src/randomTP/Main.java b/src/randomTP/Main.java new file mode 100644 index 0000000..769dcd7 --- /dev/null +++ b/src/randomTP/Main.java @@ -0,0 +1,141 @@ +package randomTP; + +import java.util.Random; + +import org.bukkit.Bukkit; +import org.bukkit.Location; +import org.bukkit.Material; +import org.bukkit.World; +import org.bukkit.command.Command; +import org.bukkit.command.CommandSender; +import org.bukkit.craftbukkit.v1_11_R1.CraftWorld; +import org.bukkit.entity.Player; +import org.bukkit.plugin.java.JavaPlugin; + +import net.minecraft.server.v1_11_R1.WorldBorder; + +public class Main extends JavaPlugin +{ + private static final Random random = new Random(); + + private static World world; + private static WorldBorder border; + private static double size, + borderCenterX, + borderCenterZ; + + private static int x,z; + private static final int radius = 70, + diameter = radius * 2; + + private static Location center, + north, + south, + east, + west; + + private static boolean centerUsed, + northUsed, + southUsed, + eastUsed, + westUsed; + + private static StringBuffer availableDirections = new StringBuffer(5); + private static int dir; + + /*================================================================================================*/ + + public void onEnable() + { + world = Bukkit.getWorld("World"); + border = ((CraftWorld) world).getHandle().getWorldBorder(); + + size = border.getSize(); + borderCenterX = border.getCenterX(); + borderCenterZ = border.getCenterZ(); + + getCommand("randomtp").setExecutor(this); + } + + /*================================================================================================*/ + + public boolean onCommand(CommandSender sender, Command label, String command, String[] args) + { + if (sender.isOp()) return rtp(Bukkit.getPlayer(args[0])); + + else return false; + } + + /*================================================================================================*/ + + public synchronized boolean newLocation() + { + //MAXIMUM TEN THOUSAND ATTEMPTS + for (int i = 0; i < 10000; i++) + { + //CHOOSE A RANDOM LOCATION WITHIN THE WORLDBORDER, ALLOWING SPACE FOR OUTER POSITIONS + x = (int) Math.floor((random.nextDouble() - 0.5) * (size - diameter)) + center.getBlockX(); + z = (int) Math.floor((random.nextDouble() - 0.5) * (size - diameter)) + center.getBlockY(); + + //CHECK THAT CENTER AND OUTER POSITIONS DO NOT HAVE WATER AT THEIR HIGHEST BLOCKS + if (world.getHighestBlockAt( x , z ).getType() != Material.WATER && + world.getHighestBlockAt( x , z - radius ).getType() != Material.WATER && + world.getHighestBlockAt( x , z + radius ).getType() != Material.WATER && + world.getHighestBlockAt( x - radius , z ).getType() != Material.WATER && + world.getHighestBlockAt( x + radius , z ).getType() != Material.WATER) + { + //IF NEW LOCATION CHECKS OUT, RESET VALUES + availableDirections.setCharAt(0, (char) 1); + availableDirections.setCharAt(1, (char) 2); + availableDirections.setCharAt(2, (char) 3); + availableDirections.setCharAt(3, (char) 4); + availableDirections.setCharAt(4, (char) 5); + + center = world.getHighestBlockAt( x , z ).getLocation(); + north = world.getHighestBlockAt( x , z - radius ).getLocation(); + south = world.getHighestBlockAt( x , z + radius ).getLocation(); + east = world.getHighestBlockAt( x + radius , z ).getLocation(); + west = world.getHighestBlockAt( x - radius , z ).getLocation(); + + centerUsed = northUsed = southUsed = eastUsed = westUsed = false; + + return true; + } + } + return false; + } + + + public synchronized boolean rtp(Player player) + { + if (player == null) + return false; + + //IF ALL POSITIONS USED, CHOOSE NEW LOCATION, AND IF NEW LOCATION FAILS RETURN FALSE + if (centerUsed && northUsed && southUsed && eastUsed && westUsed && !newLocation()) + return false; + + //IF BORDER HAS CHANGED, CHOOSE NEW LOCATION, AND IF NEW LOCATION FAILS RETURN FALSE + if ((borderCenterX != (borderCenterX = border.getCenterX()) || + borderCenterZ != (borderCenterZ = border.getCenterZ()) || + size != (size = border.getSize())) && !newLocation()) + return false; + + //CHOOSE ONE OF THE FIVE POSITIONS RANDOMLY AND TELEPORT THE PLAYER THERE, THEN REMOVE THAT POSITION + switch(dir = availableDirections.charAt((int) Math.floor(random.nextDouble() * availableDirections.length()))) + { + case (char) 1: player.teleport(center); centerUsed = true; break; + case (char) 2: player.teleport(north ); northUsed = true; break; + case (char) 3: player.teleport(south ); southUsed = true; break; + case (char) 4: player.teleport(east ); eastUsed = true; break; + case (char) 5: player.teleport(west ); westUsed = true; break; + } + availableDirections.deleteCharAt(dir); + + //IF ALL POSITIONS USED, CHOOSE A NEW LOCATION FOR NEXT ROUND + if (centerUsed && northUsed && southUsed && eastUsed && westUsed) + newLocation(); + + return true; + } +} diff --git a/src/randomTP/plugin.yml b/src/randomTP/plugin.yml new file mode 100644 index 0000000..4061ef4 --- /dev/null +++ b/src/randomTP/plugin.yml @@ -0,0 +1,3 @@ +main: randomTP.Main +version: 1.0.0 +name: RandomTP \ No newline at end of file