Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package cokoc.flatten;
- import org.bukkit.Chunk;
- import org.bukkit.Location;
- import org.bukkit.Material;
- import org.bukkit.World;
- import org.bukkit.block.Block;
- import org.bukkit.command.Command;
- import org.bukkit.command.CommandSender;
- import org.bukkit.entity.Player;
- import org.bukkit.plugin.java.JavaPlugin;
- public class Flatten extends JavaPlugin {
- public boolean onCommand(CommandSender commandSender, Command command, String label, String[] args) {
- if(!(commandSender instanceof Player)) {
- commandSender.sendMessage("You need to be a player to use this command!");
- return true;
- }
- Player senderPlayer = (Player) commandSender;
- if(!(senderPlayer.isOp())) {
- senderPlayer.sendMessage("You need to be OP to use this command!");
- return true;
- } else if(!(senderPlayer.hasPermission("flatten.*"))) {
- senderPlayer.sendMessage("You don't have enough permissions to use this command!");
- return true;
- }
- if(command.getName().equalsIgnoreCase("flatten")) {
- String mode = new String();
- int radius = 0;
- int height = 0;
- Material groundBlock = null;
- String direction = new String();
- if(args.length < 2) {
- senderPlayer.sendMessage("Not enough arguments");
- return false;
- }
- mode = args[0];
- if(Material.getMaterial(Integer.parseInt(args[1])) != null)
- groundBlock = Material.getMaterial(Integer.parseInt(args[1]));
- else {
- senderPlayer.sendMessage("Error: undefined material.");
- return false;
- }
- direction = args[2];
- if(mode.equalsIgnoreCase("chunk")) {
- Location playerLocation = senderPlayer.getLocation();
- Chunk currentChunk = playerLocation.getWorld().getChunkAt(playerLocation);
- if(direction.equalsIgnoreCase("down")) {
- for(int x = 0; x < 16; x++) {
- for(int y = playerLocation.getBlockY(); y > 0; y--) {
- for(int z = 0; z < 16; z++) {
- currentChunk.getBlock(x, y, z).setType(Material.AIR);
- }
- }
- }
- for(int x = 0; x < 16; x++) {
- for(int z = 0; z < 16; z++) {
- currentChunk.getBlock(x, 0, z).setType(groundBlock);
- }
- }
- } else {
- for(int x = 0; x < 16; x++) {
- for(int y = playerLocation.getBlockY(); y < 255; y++) {
- for(int z = 0; z < 16; z++) {
- currentChunk.getBlock(x, y, z).setType(Material.AIR);
- }
- }
- }
- for(int x = 0; x < 16; x++) {
- for(int z = 0; z < 16; z++) {
- currentChunk.getBlock(x, playerLocation.getBlockY() - 1, z).setType(groundBlock);
- }
- }
- }
- } else if(mode.equalsIgnoreCase("rectangle") || mode.equalsIgnoreCase("r")) {
- if(args.length < 5) {
- senderPlayer.sendMessage("Not enough arguments");
- return false;
- }
- radius = Integer.parseInt(args[3]);
- height = Integer.parseInt(args[4]);
- for(int currentHeight = 0; currentHeight <= height; currentHeight++) {
- Location playerLocation = senderPlayer.getLocation();
- Location centerLocation = null;
- if(direction.equalsIgnoreCase("down")) {
- centerLocation = new Location(playerLocation.getWorld(), playerLocation.getBlockX(), playerLocation.getBlockY() - currentHeight, playerLocation.getBlockZ());
- if(currentHeight == height)
- clearRectangle(centerLocation, radius, groundBlock);
- else
- clearRectangle(centerLocation, radius, Material.AIR);
- } else {
- centerLocation = new Location(playerLocation.getWorld(), playerLocation.getBlockX(), playerLocation.getBlockY() + currentHeight - 1, playerLocation.getBlockZ());
- if(currentHeight == 0)
- clearRectangle(centerLocation, radius, groundBlock);
- else
- clearRectangle(centerLocation, radius, Material.AIR);
- }
- }
- } else if(mode.equalsIgnoreCase("circle") || mode.equalsIgnoreCase("c")) {
- if(args.length < 5) {
- senderPlayer.sendMessage("Not enough arguments");
- return false;
- }
- radius = Integer.parseInt(args[3]);
- height = Integer.parseInt(args[4]);
- for(int currentHeight = 0; currentHeight <= height; currentHeight++) {
- for(int layer = radius; layer >= 0; layer--) {
- Location playerLocation = senderPlayer.getLocation();
- Location centerLocation = null;
- if(direction.equalsIgnoreCase("down")) {
- centerLocation = new Location(playerLocation.getWorld(), playerLocation.getBlockX(), playerLocation.getBlockY() - currentHeight, playerLocation.getBlockZ());
- if(currentHeight == height)
- changeCircleExterior(centerLocation, layer, groundBlock);
- else
- changeCircleExterior(centerLocation, layer, Material.AIR);
- } else {
- centerLocation = new Location(playerLocation.getWorld(), playerLocation.getBlockX(), playerLocation.getBlockY() + currentHeight - 1, playerLocation.getBlockZ());
- if(currentHeight == 0) {
- changeCircleExterior(centerLocation, layer, groundBlock);
- } else
- changeCircleExterior(centerLocation, layer, Material.AIR);
- }
- }
- }
- senderPlayer.sendMessage("Cleared cylinder.");
- } else {
- senderPlayer.sendMessage("Error: Unknown mode.");
- }
- }
- return true;
- }
- void clearRectangle(Location center, int radius, Material type) {
- Location startingLocation = new Location(center.getWorld(), center.getBlockX() - radius, center.getBlockY(), center.getBlockZ() - radius);
- int offSet = 2 * radius + 1;
- for(int x = 0; x < offSet; x++) {
- for(int z = 0; z < offSet; z++) {
- Location currentBlockLocation = new Location(startingLocation.getWorld(), startingLocation.getX() + x, startingLocation.getY(), startingLocation.getZ() + z);
- Block currentBlock = startingLocation.getWorld().getBlockAt(currentBlockLocation);
- currentBlock.setType(type);
- }
- }
- }
- // Circle tracing algorithm, taken from
- // http://fr.wikipedia.org/wiki/Algorithme_de_trac%C3%A9_de_cercle_d%27Andres
- void changeCircleExterior(Location center, int radius, Material type) {
- World targetWorld = center.getWorld();
- int x = 0;
- int z = radius;
- int d = radius - 1;
- int centerX = center.getBlockX();
- int centerY = center.getBlockY();
- int centerZ = center.getBlockZ();
- while(z >= x) {
- targetWorld.getBlockAt(x + centerX, centerY, z + centerZ).setType(type);
- targetWorld.getBlockAt(z + centerX, centerY, x + centerZ).setType(type);
- targetWorld.getBlockAt(-x + centerX, centerY, z + centerZ).setType(type);
- targetWorld.getBlockAt(-z + centerX, centerY, x + centerZ).setType(type);
- targetWorld.getBlockAt(x + centerX, centerY, -z + centerZ).setType(type);
- targetWorld.getBlockAt(z + centerX, centerY, -x + centerZ).setType(type);
- targetWorld.getBlockAt(-x + centerX, centerY, -z + centerZ).setType(type);
- targetWorld.getBlockAt(-z + centerX, centerY, -x + centerZ).setType(type);
- if(d >= 2*x) {
- d = d - 2*x - 1;
- x = x+1;
- } else if(d <= 2*(radius - z)) {
- d = d + 2*z - 1;
- z = z - 1;
- } else {
- d = d + 2*(z - x - 1);
- z = z -1;
- x = x+1;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement