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.Chunk;
- 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.*;
- import org.bukkit.inventory.AbstractHorseInventory;
- import org.bukkit.inventory.EntityEquipment;
- import org.bukkit.inventory.Inventory;
- import org.bukkit.inventory.ItemStack;
- import org.bukkit.util.BoundingBox;
- import java.util.HashSet;
- import java.util.Set;
- public class CopyBlock implements CommandExecutor {
- private static Location getNewPosition(Location selectionStart, Location destinationStart, Location oldPosition, World world) {
- double offsetX = oldPosition.getX() - selectionStart.getX();
- double offsetY = oldPosition.getY() - selectionStart.getY();
- double offsetZ = oldPosition.getZ() - selectionStart.getZ();
- final double x = destinationStart.getX() + offsetX;
- final double y = destinationStart.getY() + offsetY;
- final double z = destinationStart.getZ() + offsetZ;
- return new Location(world, x, y, z, oldPosition.getYaw(), oldPosition.getPitch());
- }
- // 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();
- World destinationWorld = player.getWorld(); // for now
- // chunks set (we will use it later to copy entities)
- Set<Chunk> chunks = new HashSet<>();
- // starting positions
- Location startPos1 = new Location(world, box.getMinX(), box.getMinY(), box.getMinZ());
- Location startPos2 = new Location(destinationWorld, pos[6], pos[7], pos[8]);
- // start copying
- for (double x = box.getMinX(); x <= box.getMaxX(); x++) {
- for (double z = box.getMinZ(); z <= box.getMaxZ(); z++) {
- for (double y = box.getMinY(); y <= box.getMaxY(); y++) { // we will mainly iterate over the Y axis so we reload chunks as little as possible
- //----------------
- // 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();
- // add current chunk to the set
- chunks.add(block.getChunk());
- //-----------------
- // 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);
- }
- }
- }
- }
- //-----------------
- // handle entities
- //-----------------
- // we have to do this to include the entities of the last upper block, otherwise they will not be copied
- box.expand(0.0, 1.0, 0.0);
- // iterate over each chunk
- for (Chunk chunk : chunks) {
- Entity[] entities = chunk.getEntities();
- for (Entity entity : entities) {
- // only copy entities inside the selected box
- if (!box.contains(entity.getLocation().toVector()))
- continue;
- // calculate the new position
- Location newEntPos = getNewPosition(startPos1, startPos2, entity.getLocation(), destinationWorld);
- // handle item frames
- if (entity instanceof ItemFrame) {
- try {
- newEntPos.setX(newEntPos.getBlockX()); // we need to floor with item frames (for some reason)
- newEntPos.setY(newEntPos.getBlockY());
- newEntPos.setZ(newEntPos.getBlockZ());
- ItemFrame newItemFrame = (ItemFrame) world.spawnEntity(newEntPos, EntityType.ITEM_FRAME);
- newItemFrame.setItem(((ItemFrame) entity).getItem());
- newItemFrame.setRotation(((ItemFrame) entity).getRotation());
- newItemFrame.setFacingDirection(entity.getFacing());
- }
- catch (IllegalArgumentException ie) {
- Bukkit.getServer().getConsoleSender().sendMessage("warning: entity " + entity + "could not be copied.");
- }
- }
- // handle armor stands
- else if (entity instanceof ArmorStand) {
- ArmorStand newArmorStand = (ArmorStand)world.spawnEntity(newEntPos, EntityType.ARMOR_STAND);
- EntityEquipment equipment = ((ArmorStand)entity).getEquipment();
- EntityEquipment newEquipment = newArmorStand.getEquipment();
- if (equipment != null && newEquipment != null) {
- newEquipment.setArmorContents(equipment.getArmorContents());
- }
- }
- // handle peaceful mobs
- else if (entity instanceof LivingEntity) {
- // to make changes later
- Entity newEnt = null;
- if (entity instanceof Chicken)
- newEnt = world.spawnEntity(newEntPos, EntityType.CHICKEN);
- else if (entity instanceof Cow)
- newEnt = world.spawnEntity(newEntPos, EntityType.COW);
- else if (entity instanceof Pig)
- newEnt = world.spawnEntity(newEntPos, EntityType.PIG);
- else if (entity instanceof Squid)
- newEnt = world.spawnEntity(newEntPos, EntityType.SQUID);
- else if (entity instanceof Turtle)
- newEnt = world.spawnEntity(newEntPos, EntityType.TURTLE);
- else if (entity instanceof Dolphin)
- newEnt = world.spawnEntity(newEntPos, EntityType.DOLPHIN);
- else if (entity instanceof MushroomCow) {
- MushroomCow newMushroomCow = (MushroomCow)world.spawnEntity(newEntPos, EntityType.MUSHROOM_COW);
- newMushroomCow.setVariant(((MushroomCow)entity).getVariant());
- }
- else if (entity instanceof Rabbit) {
- Rabbit newRabbit = (Rabbit)world.spawnEntity(newEntPos, EntityType.RABBIT);
- newRabbit.setRabbitType(((Rabbit)entity).getRabbitType());
- newEnt = newRabbit;
- }
- else if (entity instanceof Sheep) {
- Sheep newSheep = (Sheep)world.spawnEntity(newEntPos, EntityType.SHEEP);
- newSheep.setSheared(((Sheep)entity).isSheared());
- newSheep.setColor(((Sheep)entity).getColor());
- newEnt = newSheep;
- }
- else if (entity instanceof AbstractHorse) {
- AbstractHorse newHorse = null;
- if (entity instanceof Horse)
- newHorse = (AbstractHorse)world.spawnEntity(newEntPos, EntityType.HORSE);
- else if (entity instanceof Donkey)
- newHorse = (AbstractHorse)world.spawnEntity(newEntPos, EntityType.DONKEY);
- else if (entity instanceof Llama)
- newHorse = (AbstractHorse)world.spawnEntity(newEntPos, EntityType.LLAMA);
- else if (entity instanceof Mule)
- newHorse = (AbstractHorse)world.spawnEntity(newEntPos, EntityType.MULE);
- else if (entity instanceof SkeletonHorse)
- newHorse = (AbstractHorse)world.spawnEntity(newEntPos, EntityType.SKELETON_HORSE);
- if (newHorse != null) {
- newHorse.setDomestication(((AbstractHorse)entity).getDomestication());
- newHorse.setJumpStrength(((AbstractHorse)entity).getJumpStrength());
- newHorse.setMaxDomestication(((AbstractHorse)entity).getMaxDomestication());
- AbstractHorseInventory inventory = ((AbstractHorse)entity).getInventory();
- AbstractHorseInventory newInventory = newHorse.getInventory();
- ItemStack[] contents = inventory.getContents();
- newInventory.setContents(contents);
- if (inventory.getSaddle() != null)
- newInventory.setSaddle(inventory.getSaddle());
- }
- newEnt = newHorse;
- }
- else if (entity instanceof Bee) {
- Bee newBee = (Bee)world.spawnEntity(newEntPos, EntityType.BEE);
- newBee.setAnger(((Bee)entity).getAnger());
- newBee.setCannotEnterHiveTicks(((Bee)entity).getCannotEnterHiveTicks());
- newBee.setHasNectar(((Bee)entity).hasNectar());
- newBee.setHasStung(((Bee)entity).hasStung());
- Location flowerPos = ((Bee)entity).getFlower();
- if (flowerPos != null) {
- Location newFlowerPos = getNewPosition(startPos1, startPos2, flowerPos, destinationWorld);
- newBee.setFlower(newFlowerPos);
- }
- Location hivePos = ((Bee)entity).getHive();
- if (hivePos != null) {
- Location newHivePos = getNewPosition(startPos1, startPos2, hivePos, destinationWorld);
- newBee.setFlower(newHivePos);
- }
- newEnt = newBee;
- }
- else if (entity instanceof Cat) {
- Cat newCat = (Cat)world.spawnEntity(newEntPos, EntityType.CAT);
- newCat.setCatType(((Cat) entity).getCatType());
- newCat.setCollarColor(((Cat) entity).getCollarColor());
- newEnt = newCat;
- }
- else if (entity instanceof Wolf) {
- Wolf newWolf = (Wolf)world.spawnEntity(newEntPos, EntityType.WOLF);
- newWolf.setAngry(((Wolf)entity).isAngry());
- newWolf.setCollarColor(((Wolf)entity).getCollarColor());
- newEnt = newWolf;
- }
- else if (entity instanceof Fox) {
- Fox newFox = (Fox)world.spawnEntity(newEntPos, EntityType.FOX);
- newFox.setFirstTrustedPlayer(((Fox)entity).getFirstTrustedPlayer());
- newFox.setFoxType(((Fox)entity).getFoxType());
- newFox.setSecondTrustedPlayer(((Fox)entity).getSecondTrustedPlayer());
- newFox.setCrouching(((Fox)entity).isCrouching());
- newFox.setSleeping(((Fox)entity).isSleeping());
- }
- else if (entity instanceof Villager) { // are villagers peaceful mobs?? maybe
- Villager newVillager = (Villager)world.spawnEntity(newEntPos, EntityType.VILLAGER);
- newVillager.setProfession(((Villager)entity).getProfession());
- newVillager.setVillagerType(((Villager)entity).getVillagerType());
- newVillager.setVillagerExperience(((Villager)entity).getVillagerExperience());
- newVillager.setVillagerLevel(((Villager)entity).getVillagerLevel());
- newEnt = newVillager;
- }
- else if (entity instanceof IronGolem) {
- IronGolem newIronGolem = (IronGolem)world.spawnEntity(newEntPos, EntityType.IRON_GOLEM);
- newIronGolem.setPlayerCreated(((IronGolem)entity).isPlayerCreated());
- newEnt = newIronGolem;
- }
- // fish
- else if (entity instanceof Cod)
- newEnt = world.spawnEntity(newEntPos, EntityType.COD);
- else if (entity instanceof Salmon)
- newEnt = world.spawnEntity(newEntPos, EntityType.SALMON);
- else if (entity instanceof PufferFish) {
- PufferFish newPufferFish = (PufferFish)world.spawnEntity(newEntPos, EntityType.PUFFERFISH);
- newPufferFish.setPuffState(((PufferFish)entity).getPuffState());
- newEnt = newPufferFish;
- }
- else if (entity instanceof TropicalFish) {
- TropicalFish newTropicalFish = (TropicalFish)world.spawnEntity(newEntPos, EntityType.TROPICAL_FISH);
- newTropicalFish.setBodyColor(((TropicalFish)entity).getBodyColor());
- newTropicalFish.setPattern(((TropicalFish)entity).getPattern());
- newTropicalFish.setPatternColor(((TropicalFish)entity).getPatternColor());
- newEnt = newTropicalFish;
- }
- // final adjustments
- if (newEnt != null) {
- // set age
- if (newEnt instanceof Ageable) {
- if (((Ageable) entity).isAdult())
- ((Ageable) entity).setAdult();
- else
- ((Ageable) entity).setBaby();
- }
- // set name tag
- String customName = entity.getCustomName();
- if (customName != null) {
- newEnt.setCustomName(customName);
- newEnt.setCustomNameVisible(entity.isCustomNameVisible());
- }
- // set pet owner
- if (newEnt instanceof Tameable) {
- if (((Tameable) entity).isTamed()) {
- ((Tameable) newEnt).setTamed(true);
- ((Tameable) newEnt).setOwner(((Tameable) entity).getOwner());
- }
- }
- // sit if pet
- if (newEnt instanceof Sittable)
- ((Sittable) newEnt).setSitting(((Sittable) entity).isSitting());
- // leash entities to fences
- Entity holder;
- try {
- holder = ((LivingEntity) entity).getLeashHolder();
- } catch (IllegalStateException ie){
- holder = null;
- }
- if (holder != null) {
- Location newHitchPos = getNewPosition(startPos1, startPos2, holder.getLocation(), destinationWorld);
- LeashHitch newHitch = (LeashHitch) world.spawnEntity(newHitchPos, EntityType.LEASH_HITCH);
- ((LivingEntity)newEnt).setLeashHolder(newHitch);
- }
- }
- }
- }
- }
- }
- return true;
- }
- }
RAW Paste Data