Advertisement
Guest User

Untitled

a guest
Mar 11th, 2012
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.95 KB | None | 0 0
  1. package cokoc.flatten;
  2.  
  3. import org.bukkit.Chunk;
  4. import org.bukkit.Location;
  5. import org.bukkit.Material;
  6. import org.bukkit.World;
  7. import org.bukkit.block.Block;
  8. import org.bukkit.command.Command;
  9. import org.bukkit.command.CommandSender;
  10. import org.bukkit.entity.Player;
  11. import org.bukkit.plugin.java.JavaPlugin;
  12.  
  13. public class Flatten extends JavaPlugin {
  14.     public boolean onCommand(CommandSender commandSender, Command command, String label, String[] args) {
  15.         if(!(commandSender instanceof Player)) {
  16.             commandSender.sendMessage("You need to be a player to use this command!");
  17.             return true;
  18.         }
  19.         Player senderPlayer = (Player) commandSender;
  20.         if(!(senderPlayer.isOp())) {
  21.             senderPlayer.sendMessage("You need to be OP to use this command!");
  22.             return true;
  23.         } else if(!(senderPlayer.hasPermission("flatten.*"))) {
  24.             senderPlayer.sendMessage("You don't have enough permissions to use this command!");
  25.             return true;
  26.         }
  27.  
  28.         if(command.getName().equalsIgnoreCase("flatten")) {
  29.             String mode = new String();
  30.             int radius = 0;
  31.             int height = 0;
  32.             Material groundBlock = null;
  33.             String direction = new String();
  34.             if(args.length < 2) {
  35.                 senderPlayer.sendMessage("Not enough arguments");
  36.                 return false;
  37.             }
  38.  
  39.             mode = args[0];
  40.             if(Material.getMaterial(Integer.parseInt(args[1])) != null)
  41.                 groundBlock = Material.getMaterial(Integer.parseInt(args[1]));
  42.             else {
  43.                 senderPlayer.sendMessage("Error: undefined material.");
  44.                 return false;
  45.             }
  46.             direction = args[2];
  47.  
  48.             if(mode.equalsIgnoreCase("chunk")) {
  49.                 Location playerLocation = senderPlayer.getLocation();
  50.                 Chunk currentChunk = playerLocation.getWorld().getChunkAt(playerLocation);
  51.                
  52.                 if(direction.equalsIgnoreCase("down")) {
  53.                     for(int x = 0; x < 16; x++) {
  54.                         for(int y = playerLocation.getBlockY(); y > 0; y--) {
  55.                             for(int z = 0; z < 16; z++) {
  56.                                 currentChunk.getBlock(x, y, z).setType(Material.AIR);
  57.                             }
  58.                         }
  59.                     }
  60.                     for(int x = 0; x < 16; x++) {
  61.                         for(int z = 0; z < 16; z++) {
  62.                             currentChunk.getBlock(x, 0, z).setType(groundBlock);
  63.                         }
  64.                     }
  65.                 } else {
  66.                     for(int x = 0; x < 16; x++) {
  67.                         for(int y = playerLocation.getBlockY(); y < 255; y++) {
  68.                             for(int z = 0; z < 16; z++) {
  69.                                 currentChunk.getBlock(x, y, z).setType(Material.AIR);
  70.                             }
  71.                         }
  72.                     }
  73.                     for(int x = 0; x < 16; x++) {
  74.                         for(int z = 0; z < 16; z++) {
  75.                             currentChunk.getBlock(x, playerLocation.getBlockY() - 1, z).setType(groundBlock);
  76.                         }
  77.                     }
  78.                 }
  79.             } else if(mode.equalsIgnoreCase("rectangle") || mode.equalsIgnoreCase("r")) {
  80.                 if(args.length < 5) {
  81.                     senderPlayer.sendMessage("Not enough arguments");
  82.                     return false;
  83.                 }
  84.  
  85.                 radius = Integer.parseInt(args[3]);
  86.                 height = Integer.parseInt(args[4]);
  87.                
  88.                 for(int currentHeight = 0; currentHeight <= height; currentHeight++) {
  89.                     Location playerLocation = senderPlayer.getLocation();
  90.                     Location centerLocation = null;
  91.                     if(direction.equalsIgnoreCase("down")) {
  92.                         centerLocation = new Location(playerLocation.getWorld(), playerLocation.getBlockX(), playerLocation.getBlockY() - currentHeight, playerLocation.getBlockZ());
  93.                         if(currentHeight == height)
  94.                             clearRectangle(centerLocation, radius, groundBlock);
  95.                         else
  96.                             clearRectangle(centerLocation, radius, Material.AIR);
  97.                     } else {
  98.                         centerLocation = new Location(playerLocation.getWorld(), playerLocation.getBlockX(), playerLocation.getBlockY() + currentHeight - 1, playerLocation.getBlockZ());
  99.                         if(currentHeight == 0)
  100.                             clearRectangle(centerLocation, radius, groundBlock);
  101.                         else
  102.                             clearRectangle(centerLocation, radius, Material.AIR);
  103.                     }
  104.                 }
  105.             } else if(mode.equalsIgnoreCase("circle") || mode.equalsIgnoreCase("c")) {
  106.                 if(args.length < 5) {
  107.                     senderPlayer.sendMessage("Not enough arguments");
  108.                     return false;
  109.                 }
  110.  
  111.                 radius = Integer.parseInt(args[3]);
  112.                 height = Integer.parseInt(args[4]);
  113.  
  114.                 for(int currentHeight = 0; currentHeight <= height; currentHeight++) {
  115.                     for(int layer = radius; layer >= 0; layer--) {
  116.                         Location playerLocation = senderPlayer.getLocation();
  117.                         Location centerLocation = null;
  118.                         if(direction.equalsIgnoreCase("down")) {
  119.                             centerLocation = new Location(playerLocation.getWorld(), playerLocation.getBlockX(), playerLocation.getBlockY() - currentHeight, playerLocation.getBlockZ());
  120.                             if(currentHeight == height)
  121.                                 changeCircleExterior(centerLocation, layer, groundBlock);
  122.                             else
  123.                                 changeCircleExterior(centerLocation, layer, Material.AIR);
  124.                         } else {
  125.                             centerLocation = new Location(playerLocation.getWorld(), playerLocation.getBlockX(), playerLocation.getBlockY() + currentHeight - 1, playerLocation.getBlockZ());
  126.                             if(currentHeight == 0) {
  127.                                 changeCircleExterior(centerLocation, layer, groundBlock);
  128.                             } else
  129.                                 changeCircleExterior(centerLocation, layer, Material.AIR);
  130.                         }
  131.                     }
  132.                 }
  133.                 senderPlayer.sendMessage("Cleared cylinder.");
  134.             } else {
  135.                 senderPlayer.sendMessage("Error: Unknown mode.");
  136.             }
  137.         }
  138.         return true;
  139.     }
  140.  
  141.     void clearRectangle(Location center, int radius, Material type) {
  142.         Location startingLocation = new Location(center.getWorld(), center.getBlockX() - radius, center.getBlockY(), center.getBlockZ() - radius);
  143.         int offSet = 2 * radius + 1;
  144.         for(int x = 0; x < offSet; x++) {
  145.             for(int z = 0; z < offSet; z++) {
  146.                 Location currentBlockLocation = new Location(startingLocation.getWorld(), startingLocation.getX() + x, startingLocation.getY(), startingLocation.getZ() + z);
  147.                 Block currentBlock = startingLocation.getWorld().getBlockAt(currentBlockLocation);
  148.                 currentBlock.setType(type);
  149.             }
  150.         }
  151.     }
  152.  
  153.     // Circle tracing algorithm, taken from
  154.     // http://fr.wikipedia.org/wiki/Algorithme_de_trac%C3%A9_de_cercle_d%27Andres
  155.     void changeCircleExterior(Location center, int radius, Material type) {
  156.         World targetWorld = center.getWorld();
  157.  
  158.         int x = 0;
  159.         int z = radius;
  160.         int d = radius - 1;
  161.         int centerX = center.getBlockX();
  162.         int centerY = center.getBlockY();
  163.         int centerZ = center.getBlockZ();
  164.  
  165.         while(z >= x) {
  166.             targetWorld.getBlockAt(x + centerX, centerY, z + centerZ).setType(type);
  167.             targetWorld.getBlockAt(z + centerX, centerY, x + centerZ).setType(type);
  168.             targetWorld.getBlockAt(-x + centerX, centerY, z + centerZ).setType(type);
  169.             targetWorld.getBlockAt(-z + centerX, centerY, x + centerZ).setType(type);
  170.             targetWorld.getBlockAt(x + centerX, centerY, -z + centerZ).setType(type);
  171.             targetWorld.getBlockAt(z + centerX, centerY, -x + centerZ).setType(type);
  172.             targetWorld.getBlockAt(-x + centerX, centerY, -z + centerZ).setType(type);
  173.             targetWorld.getBlockAt(-z + centerX, centerY, -x + centerZ).setType(type);
  174.             if(d >= 2*x) {
  175.                 d = d - 2*x - 1;
  176.                 x = x+1;
  177.             } else if(d <= 2*(radius - z)) {
  178.                 d = d + 2*z - 1;
  179.                 z = z - 1;
  180.             } else {
  181.                 d = d + 2*(z - x - 1);
  182.                 z = z -1;
  183.                 x = x+1;
  184.             }
  185.         }
  186.     }
  187. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement