Created
January 4, 2017 15:24
-
-
Save Minikloon/76ade074874134d499802bf9671bed6f to your computer and use it in GitHub Desktop.
This file contains 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 net.minikloon; | |
import org.bukkit.Chunk; | |
import org.bukkit.Location; | |
import org.bukkit.Material; | |
import org.bukkit.block.Block; | |
import org.bukkit.block.BlockFace; | |
import org.bukkit.command.Command; | |
import org.bukkit.command.CommandSender; | |
import org.bukkit.entity.Player; | |
import org.bukkit.plugin.java.JavaPlugin; | |
import org.bukkit.scheduler.BukkitScheduler; | |
import java.util.Random; | |
public class GeneratorBug extends JavaPlugin { | |
private final Random rand = new Random(); | |
private int runs = 0; | |
@Override | |
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { | |
Player player = (Player) sender; | |
BukkitScheduler scheduler = getServer().getScheduler(); | |
scheduler.runTaskTimer(this, () -> { | |
++runs; | |
Chunk playerChunk = player.getLocation().getChunk(); | |
Chunk chunkFarAway = playerChunk.getWorld().getChunkAt(playerChunk.getX() + 50, playerChunk.getZ()); | |
Location tpLoc = getSafeLocationInChunk(chunkFarAway, 10); | |
if(tpLoc == null) { | |
player.sendMessage(runs + " attempts limit reached"); | |
player.teleport(chunkFarAway.getBlock(0, 100, 0).getLocation()); | |
return; | |
} | |
player.teleport(tpLoc); | |
Block bottomBlockTped = player.getLocation().getBlock(); | |
Runnable sendBlockStatus = () -> { | |
String status = runs + " " + bottomBlockTped.getType() + " " + bottomBlockTped.getRelative(BlockFace.UP).getType(); | |
player.sendMessage(status); | |
}; | |
sendBlockStatus.run(); | |
scheduler.runTaskLater(this, sendBlockStatus, 2L); | |
}, 0, 20L); | |
return true; | |
} | |
public Location getSafeLocationInChunk(Chunk chunk, int attempts) { | |
for(int attempt = 0; attempt < attempts; ++attempt) { | |
Location attemptLoc = chunk.getBlock(rand.nextInt(16), 0, rand.nextInt(16)).getLocation(); | |
int y = findGroundWithTwoAir(attemptLoc); | |
if(y < 0) | |
continue; | |
attemptLoc.setX(attemptLoc.getBlockX() + 0.5); | |
attemptLoc.setY(y); | |
attemptLoc.setZ(attemptLoc.getBlockZ() + 0.5); | |
return attemptLoc; | |
} | |
return null; | |
} | |
public int findGroundWithTwoAir(Location loc) { | |
for(int y = 250; y > 0; --y) { | |
Block block = loc.getWorld().getBlockAt(loc.getBlockX(), y, loc.getBlockZ()); | |
Block oneOver = block.getRelative(0, 1, 0); | |
Block twoOver = block.getRelative(0, 2, 0); | |
if(block.getType().isSolid() && oneOver.getType() == Material.AIR && twoOver.getType() == Material.AIR) | |
return y+1; | |
} | |
return -1; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment