Last active
August 29, 2015 14:01
-
-
Save theresajayne/c0a5ff2ce0ddd266dec6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.mymod.slashchest; | |
import java.util.Date; | |
import java.util.Random; | |
import org.bukkit.ChatColor; | |
import org.bukkit.Chunk; | |
import org.bukkit.Location; | |
import org.bukkit.World; | |
import org.bukkit.block.Block; | |
import org.bukkit.command.Command; | |
import org.bukkit.command.CommandExecutor; | |
import org.bukkit.command.CommandSender; | |
import org.bukkit.entity.Player; | |
public class CommandTpRandom implements CommandExecutor | |
{ | |
private final SlashChest plugin; | |
private final Random random = new Random(new Date().getTime()); | |
public CommandTpRandom(SlashChest slashChest) | |
{ | |
// TODO Auto-generated constructor stub | |
super(); | |
this.plugin = slashChest; | |
SlashChest.logger.warning("adding tprandom"); | |
} | |
@Override | |
public boolean onCommand(final CommandSender sender, final Command command, final String label, final String[] args) { | |
if (!(sender instanceof Player)) { | |
sender.sendMessage("You can't use this from the console."); | |
return true; | |
} | |
Player player = (Player)sender; | |
int toX = getRandomCoord(SlashChest.tprandomMax); | |
int toZ = getRandomCoord(SlashChest.tprandomMax); | |
checkMin(toX); | |
checkMin(toZ); | |
Location location= player.getLocation(); | |
if (isInNether(location) || isInEnd(location) ) return true; | |
location.setX(toX); | |
location.setZ(toZ); | |
checkSafe(location); | |
Chunk chunk = location.getChunk(); | |
if(!chunk.isLoaded()) | |
chunk.load(); | |
sender.sendMessage(ChatColor.YELLOW + "Teleporting....."); | |
System.out.println(String.format("tprandom: Teleporting %s to %f %f %f",player.getDisplayName(),location.getX(),location.getY(),location.getZ())); | |
player.teleport(location); | |
return true; | |
} | |
private void checkSafe(Location location) { | |
Location topBlockLocation = location.getWorld().getHighestBlockAt(location).getLocation(); | |
topBlockLocation.setY(topBlockLocation.getY()-1); | |
Block block = location.getWorld().getBlockAt(topBlockLocation); | |
System.out.println("Location is "+block.toString()); | |
while(block.isLiquid()){ | |
location.setX(getRandomCoord(SlashChest.tprandomMax)); | |
location.setZ(getRandomCoord(SlashChest.tprandomMax)); | |
topBlockLocation = location.getWorld().getHighestBlockAt(location).getLocation(); | |
topBlockLocation.setY(topBlockLocation.getY()-1); | |
block = location.getWorld().getBlockAt(topBlockLocation); | |
System.out.println("Location is "+block.toString()); | |
} | |
location.setY(topBlockLocation.getY()+1.5); | |
location.setX(location.getX()+0.5); | |
location.setZ(location.getZ()+0.5); | |
} | |
private boolean isInNether(Location location) { | |
if (location.getWorld().getEnvironment() == World.Environment.NETHER) | |
{ | |
return true; | |
} | |
return false; | |
} | |
private boolean isInEnd(Location location) { | |
if (location.getWorld().getEnvironment() == World.Environment.THE_END) | |
{ | |
return true; | |
} | |
return false; | |
} | |
private void checkMin(int toX) { | |
if(toX > 0-SlashChest.tprandomMin && toX < SlashChest.tprandomMin) | |
{ | |
if(toX <0 ){ | |
toX = toX - SlashChest.tprandomMin; | |
} | |
else | |
{ | |
toX = toX + SlashChest.tprandomMin; | |
} | |
} | |
} | |
private int getRandomCoord(int maxCoord) | |
{ | |
return random.nextInt(maxCoord*2)-(maxCoord); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment