Advertisement
misdocumeno

Untitled

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