Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package net.ddns.misplease.testplugin2;
- import org.bukkit.Bukkit;
- import org.bukkit.Location;
- import org.bukkit.World;
- import org.bukkit.block.Block;
- import org.bukkit.block.BlockState;
- import org.bukkit.block.Container;
- import org.bukkit.block.data.BlockData;
- import org.bukkit.command.Command;
- import org.bukkit.command.CommandExecutor;
- import org.bukkit.command.CommandSender;
- import org.bukkit.entity.Player;
- import org.bukkit.inventory.Inventory;
- import org.bukkit.inventory.ItemStack;
- import org.bukkit.util.BoundingBox;
- public class CopyBlock implements CommandExecutor {
- // it copies starting from the minimum block on x, y and z
- @Override
- public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
- if (sender instanceof Player) {
- Player player = (Player) sender;
- // parse arguments
- double[] pos = new double[9];
- if (args.length != 9) {
- player.sendMessage("Invalid arguments amount.");
- }
- else {
- for (int i = 0; i < pos.length; i++) {
- pos[i] = Double.parseDouble(args[i]);
- }
- }
- // create the cube to be copied
- BoundingBox box = new BoundingBox(pos[0], pos[1], pos[2], pos[3], pos[4], pos[5]);
- // get the world
- World world = player.getWorld();
- // start copying
- for (double x = box.getMinX(); x <= box.getMaxX(); x++) {
- for (double y = box.getMinY(); y <= box.getMaxY(); y++) {
- for (double z = box.getMinZ(); z <= box.getMaxZ(); z++) {
- //----------------
- // copy the block
- //----------------
- // point to that block
- Location location = new Location(world, x, y, z);
- // get the block at that location
- Block block = location.getBlock();
- // get block type and some other stuff
- BlockData blockData = block.getBlockData();
- String blockString = blockData.getAsString();
- //-----------------
- // paste the block
- //-----------------
- // point to the destiny block
- Location newLocation = new Location(world, pos[6] + (x - box.getMinX()), pos[7] + (y - box.getMinY()), pos[8] + (z - box.getMinZ()));
- // get the block at the new location
- Block newBlock = newLocation.getBlock();
- // set the new block data
- BlockData newBlockData = Bukkit.createBlockData(blockString);
- newBlock.setBlockData(newBlockData);
- //-------------------
- // handle containers
- //-------------------
- // get the old block state
- BlockState blockState = block.getState();
- // check if it's a container
- if (blockState instanceof Container) {
- // get the contents of the container
- Inventory inventory = ((Container)blockState).getInventory();
- // get the inventory of the destiny block
- Inventory newInventory = ((Container)newBlock.getState()).getInventory();
- // put items on the new block
- ItemStack[] contents = inventory.getContents().clone();
- newInventory.setContents(contents);
- }
- }
- }
- }
- }
- return true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement