Advertisement
misdocumeno

Untitled

Dec 16th, 2020
626
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.47 KB | None | 0 0
  1. public class CopyBlock implements CommandExecutor {
  2.  
  3.     // it copies starting from the minimum block on x, y and z
  4.     @Override
  5.     public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
  6.  
  7.         if (sender instanceof Player) {
  8.  
  9.             Player player = (Player) sender;
  10.  
  11.             // parse arguments
  12.             double[] pos = new double[9];
  13.  
  14.             if (args.length != 9) {
  15.                 player.sendMessage("Invalid arguments amount.");
  16.             }
  17.             else {
  18.                 for (int i = 0; i < pos.length; i++) {
  19.                     pos[i] = Double.parseDouble(args[i]);
  20.                 }
  21.             }
  22.  
  23.             // create the cube to be copied
  24.             BoundingBox box = new BoundingBox(pos[0], pos[1], pos[2], pos[3], pos[4], pos[5]);
  25.  
  26.             // get the world
  27.             World world = player.getWorld();
  28.  
  29.             // start copying
  30.             for (double x = box.getMinX(); x <= box.getMaxX(); x++) {
  31.                 for (double y = box.getMinY(); y <= box.getMaxY(); y++) {
  32.                     for (double z = box.getMinZ(); z <= box.getMaxZ(); z++) {
  33.  
  34.                         //----------------
  35.                         // copy the block
  36.                         //----------------
  37.  
  38.                         // point to that block
  39.                         Location location = new Location(world, x, y, z);
  40.  
  41.                         // get the block at that location
  42.                         Block block = location.getBlock();
  43.  
  44.                         // get block type and some other stuff
  45.                         BlockData blockData = block.getBlockData();
  46.                         String blockString = blockData.getAsString();
  47.  
  48.                         //-----------------
  49.                         // paste the block
  50.                         //-----------------
  51.  
  52.                         // point to the destiny block
  53.                         Location newLocation = new Location(world, pos[6] + (x - box.getMinX()), pos[7] + (y - box.getMinY()), pos[8] + (z - box.getMinZ()));
  54.  
  55.                         // get the block at the new location
  56.                         Block newBlock = newLocation.getBlock();
  57.  
  58.                         // set the new block data
  59.                         BlockData newBlockData = Bukkit.createBlockData(blockString);
  60.                         newBlock.setBlockData(newBlockData);
  61.  
  62.                         //-------------------
  63.                         // handle containers
  64.                         //-------------------
  65.  
  66.                         // get the old block state
  67.                         BlockState blockState = block.getState();
  68.  
  69.                         // check if it's a container
  70.                         if (blockState instanceof Container) {
  71.  
  72.                             // get the contents of the container
  73.                             Inventory inventory = ((Container)blockState).getInventory();
  74.  
  75.                             // get the inventory of the destiny block
  76.                             Inventory newInventory = ((Container)newBlock.getState()).getInventory();
  77.  
  78.                             // put items on the new block
  79.                             for (ItemStack item : inventory) {
  80.                                 if (item != null)
  81.                                     newInventory.addItem(item);
  82.                             }
  83.                         }
  84.                     }
  85.                 }
  86.             }
  87.         }
  88.  
  89.         return true;
  90.     }
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement