Skip to content

Instantly share code, notes, and snippets.

@theresajayne
Created May 26, 2014 08:31
Show Gist options
  • Save theresajayne/a8dad61b97ad54afea85 to your computer and use it in GitHub Desktop.
Save theresajayne/a8dad61b97ad54afea85 to your computer and use it in GitHub Desktop.
package com.mymod.slashchest;
import java.util.Date;
import java.util.Random;
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();
while (!chunk.isLoaded())
{
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
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);
}
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