Advertisement
misdocumeno

Untitled

Dec 17th, 2020
816
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 18.53 KB | None | 0 0
  1. package net.ddns.misplease.testplugin2;
  2.  
  3. import org.bukkit.Bukkit;
  4. import org.bukkit.Chunk;
  5. import org.bukkit.Location;
  6. import org.bukkit.World;
  7. import org.bukkit.block.Block;
  8. import org.bukkit.block.BlockState;
  9. import org.bukkit.block.Container;
  10. import org.bukkit.block.data.BlockData;
  11. import org.bukkit.command.Command;
  12. import org.bukkit.command.CommandExecutor;
  13. import org.bukkit.command.CommandSender;
  14. import org.bukkit.entity.*;
  15. import org.bukkit.inventory.AbstractHorseInventory;
  16. import org.bukkit.inventory.EntityEquipment;
  17. import org.bukkit.inventory.Inventory;
  18. import org.bukkit.inventory.ItemStack;
  19. import org.bukkit.util.BoundingBox;
  20.  
  21. import java.util.HashSet;
  22. import java.util.Set;
  23.  
  24. public class CopyBlock implements CommandExecutor {
  25.  
  26.     private static Location getNewPosition(Location selectionStart, Location destinationStart, Location oldPosition, World world) {
  27.         double offsetX = oldPosition.getX() - selectionStart.getX();
  28.         double offsetY = oldPosition.getY() - selectionStart.getY();
  29.         double offsetZ = oldPosition.getZ() - selectionStart.getZ();
  30.         final double x = destinationStart.getX() + offsetX;
  31.         final double y = destinationStart.getY() + offsetY;
  32.         final double z = destinationStart.getZ() + offsetZ;
  33.         return new Location(world, x, y, z, oldPosition.getYaw(), oldPosition.getPitch());
  34.     }
  35.  
  36.     // it copies starting from the minimum block on x, y and z
  37.     @Override
  38.     public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
  39.  
  40.         if (sender instanceof Player) {
  41.  
  42.             Player player = (Player) sender;
  43.  
  44.             // parse arguments
  45.             double[] pos = new double[9];
  46.  
  47.             if (args.length != 9) {
  48.                 player.sendMessage("Invalid arguments amount.");
  49.             }
  50.             else {
  51.                 for (int i = 0; i < pos.length; i++) {
  52.                     pos[i] = Double.parseDouble(args[i]);
  53.                 }
  54.             }
  55.  
  56.             // create the cube to be copied
  57.             BoundingBox box = new BoundingBox(pos[0], pos[1], pos[2], pos[3], pos[4], pos[5]);
  58.  
  59.             // get the world
  60.             World world = player.getWorld();
  61.             World destinationWorld = player.getWorld(); // for now
  62.  
  63.             // chunks set (we will use it later to copy entities)
  64.             Set<Chunk> chunks = new HashSet<>();
  65.  
  66.             // starting positions
  67.             Location startPos1 = new Location(world, box.getMinX(), box.getMinY(), box.getMinZ());
  68.             Location startPos2 = new Location(destinationWorld, pos[6], pos[7], pos[8]);
  69.  
  70.             // start copying
  71.             for (double x = box.getMinX(); x <= box.getMaxX(); x++) {
  72.                 for (double z = box.getMinZ(); z <= box.getMaxZ(); z++) {
  73.                     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
  74.  
  75.                         //----------------
  76.                         // copy the block
  77.                         //----------------
  78.  
  79.                         // point to that block
  80.                         Location location = new Location(world, x, y, z);
  81.  
  82.                         // get the block at that location
  83.                         Block block = location.getBlock();
  84.  
  85.                         // get block type and some other stuff
  86.                         BlockData blockData = block.getBlockData();
  87.                         String blockString = blockData.getAsString();
  88.  
  89.                         // add current chunk to the set
  90.                         chunks.add(block.getChunk());
  91.  
  92.                         //-----------------
  93.                         // paste the block
  94.                         //-----------------
  95.  
  96.                         // point to the destiny block
  97.                         Location newLocation = new Location(world, pos[6] + (x - box.getMinX()), pos[7] + (y - box.getMinY()), pos[8] + (z - box.getMinZ()));
  98.  
  99.                         // get the block at the new location
  100.                         Block newBlock = newLocation.getBlock();
  101.  
  102.                         // set the new block data
  103.                         BlockData newBlockData = Bukkit.createBlockData(blockString);
  104.                         newBlock.setBlockData(newBlockData);
  105.  
  106.                         //-------------------
  107.                         // handle containers
  108.                         //-------------------
  109.  
  110.                         // get the old block state
  111.                         BlockState blockState = block.getState();
  112.  
  113.                         // check if it's a container
  114.                         if (blockState instanceof Container) {
  115.  
  116.                             // get the contents of the container
  117.                             Inventory inventory = ((Container)blockState).getInventory();
  118.  
  119.                             // get the inventory of the destiny block
  120.                             Inventory newInventory = ((Container)newBlock.getState()).getInventory();
  121.  
  122.                             // put items on the new block
  123.                             ItemStack[] contents = inventory.getContents().clone();
  124.                             newInventory.setContents(contents);
  125.                         }
  126.                     }
  127.                 }
  128.             }
  129.  
  130.             //-----------------
  131.             // handle entities
  132.             //-----------------
  133.  
  134.             // we have to do this to include the entities of the last upper block, otherwise they will not be copied
  135.             box.expand(0.0, 1.0, 0.0);
  136.  
  137.             // iterate over each chunk
  138.             for (Chunk chunk : chunks) {
  139.  
  140.                 Entity[] entities = chunk.getEntities();
  141.  
  142.                 for (Entity entity : entities) {
  143.  
  144.                     // only copy entities inside the selected box
  145.                     if (!box.contains(entity.getLocation().toVector()))
  146.                         continue;
  147.  
  148.                     // calculate the new position
  149.                     Location newEntPos = getNewPosition(startPos1, startPos2, entity.getLocation(), destinationWorld);
  150.  
  151.                     // handle item frames
  152.                     if (entity instanceof ItemFrame) {
  153.                         try {
  154.                             newEntPos.setX(newEntPos.getBlockX()); // we need to floor with item frames (for some reason)
  155.                             newEntPos.setY(newEntPos.getBlockY());
  156.                             newEntPos.setZ(newEntPos.getBlockZ());
  157.                             ItemFrame newItemFrame = (ItemFrame) world.spawnEntity(newEntPos, EntityType.ITEM_FRAME);
  158.                             newItemFrame.setItem(((ItemFrame) entity).getItem());
  159.                             newItemFrame.setRotation(((ItemFrame) entity).getRotation());
  160.                             newItemFrame.setFacingDirection(entity.getFacing());
  161.                         }
  162.                         catch (IllegalArgumentException ie) {
  163.                             Bukkit.getServer().getConsoleSender().sendMessage("warning: entity " + entity + "could not be copied.");
  164.                         }
  165.                     }
  166.  
  167.                     // handle armor stands
  168.                     else if (entity instanceof ArmorStand) {
  169.  
  170.                         ArmorStand newArmorStand = (ArmorStand)world.spawnEntity(newEntPos, EntityType.ARMOR_STAND);
  171.                         EntityEquipment equipment = ((ArmorStand)entity).getEquipment();
  172.                         EntityEquipment newEquipment = newArmorStand.getEquipment();
  173.  
  174.                         if (equipment != null && newEquipment != null) {
  175.                             newEquipment.setArmorContents(equipment.getArmorContents());
  176.                         }
  177.                     }
  178.  
  179.                     // handle peaceful mobs
  180.                     else if (entity instanceof LivingEntity) {
  181.  
  182.                         // to make changes later
  183.                         Entity newEnt = null;
  184.  
  185.                         if (entity instanceof Chicken)
  186.                             newEnt = world.spawnEntity(newEntPos, EntityType.CHICKEN);
  187.                         else if (entity instanceof Cow)
  188.                             newEnt = world.spawnEntity(newEntPos, EntityType.COW);
  189.                         else if (entity instanceof Pig)
  190.                             newEnt = world.spawnEntity(newEntPos, EntityType.PIG);
  191.                         else if (entity instanceof Squid)
  192.                             newEnt = world.spawnEntity(newEntPos, EntityType.SQUID);
  193.                         else if (entity instanceof Turtle)
  194.                             newEnt = world.spawnEntity(newEntPos, EntityType.TURTLE);
  195.                         else if (entity instanceof Dolphin)
  196.                             newEnt = world.spawnEntity(newEntPos, EntityType.DOLPHIN);
  197.                         else if (entity instanceof Rabbit) {
  198.                             Rabbit newRabbit = (Rabbit)world.spawnEntity(newEntPos, EntityType.RABBIT);
  199.                             newRabbit.setRabbitType(((Rabbit)entity).getRabbitType());
  200.                             newEnt = newRabbit;
  201.                         }
  202.                         else if (entity instanceof Sheep) {
  203.                             Sheep newSheep = (Sheep)world.spawnEntity(newEntPos, EntityType.SHEEP);
  204.                             newSheep.setSheared(((Sheep)entity).isSheared());
  205.                             newSheep.setColor(((Sheep)entity).getColor());
  206.                             newEnt = newSheep;
  207.                         }
  208.                         else if (entity instanceof AbstractHorse) {
  209.                             AbstractHorse newHorse = null;
  210.                             if (entity instanceof Horse)
  211.                                 newHorse = (AbstractHorse)world.spawnEntity(newEntPos, EntityType.HORSE);
  212.                             else if (entity instanceof Donkey)
  213.                                 newHorse = (AbstractHorse)world.spawnEntity(newEntPos, EntityType.DONKEY);
  214.                             else if (entity instanceof Llama)
  215.                                 newHorse = (AbstractHorse)world.spawnEntity(newEntPos, EntityType.LLAMA);
  216.                             else if (entity instanceof Mule)
  217.                                 newHorse = (AbstractHorse)world.spawnEntity(newEntPos, EntityType.MULE);
  218.                             else if (entity instanceof SkeletonHorse)
  219.                                 newHorse = (AbstractHorse)world.spawnEntity(newEntPos, EntityType.SKELETON_HORSE);
  220.  
  221.                             if (newHorse != null) {
  222.                                 newHorse.setDomestication(((AbstractHorse)entity).getDomestication());
  223.                                 newHorse.setJumpStrength(((AbstractHorse)entity).getJumpStrength());
  224.                                 newHorse.setMaxDomestication(((AbstractHorse)entity).getMaxDomestication());
  225.  
  226.                                 AbstractHorseInventory inventory = ((AbstractHorse)entity).getInventory();
  227.                                 AbstractHorseInventory newInventory = newHorse.getInventory();
  228.  
  229.                                 ItemStack[] contents = inventory.getContents();
  230.                                 newInventory.setContents(contents);
  231.  
  232.                                 if (inventory.getSaddle() != null)
  233.                                     newInventory.setSaddle(inventory.getSaddle());
  234.                             }
  235.  
  236.                             newEnt = newHorse;
  237.                         }
  238.                         else if (entity instanceof Bee) {
  239.                             Bee newBee = (Bee)world.spawnEntity(newEntPos, EntityType.BEE);
  240.                             newBee.setAnger(((Bee)entity).getAnger());
  241.                             newBee.setCannotEnterHiveTicks(((Bee)entity).getCannotEnterHiveTicks());
  242.                             newBee.setHasNectar(((Bee)entity).hasNectar());
  243.                             newBee.setHasStung(((Bee)entity).hasStung());
  244.  
  245.                             Location flowerPos = ((Bee)entity).getFlower();
  246.                             if (flowerPos != null) {
  247.                                 Location newFlowerPos = getNewPosition(startPos1, startPos2, flowerPos, destinationWorld);
  248.                                 newBee.setFlower(newFlowerPos);
  249.                             }
  250.  
  251.                             Location hivePos = ((Bee)entity).getHive();
  252.                             if (hivePos != null) {
  253.                                 Location newHivePos = getNewPosition(startPos1, startPos2, hivePos, destinationWorld);
  254.                                 newBee.setFlower(newHivePos);
  255.                             }
  256.  
  257.                             newEnt = newBee;
  258.                         }
  259.                         else if (entity instanceof Cat) {
  260.                             Cat newCat = (Cat)world.spawnEntity(newEntPos, EntityType.CAT);
  261.                             newCat.setCatType(((Cat) entity).getCatType());
  262.                             newCat.setCollarColor(((Cat) entity).getCollarColor());
  263.                             newEnt = newCat;
  264.                         }
  265.                         else if (entity instanceof Wolf) {
  266.                             Wolf newWolf = (Wolf)world.spawnEntity(newEntPos, EntityType.WOLF);
  267.                             newWolf.setAngry(((Wolf)entity).isAngry());
  268.                             newWolf.setCollarColor(((Wolf)entity).getCollarColor());
  269.                             newEnt = newWolf;
  270.                         }
  271.                         else if (entity instanceof Fox) {
  272.                             Fox newFox = (Fox)world.spawnEntity(newEntPos, EntityType.FOX);
  273.                             newFox.setFirstTrustedPlayer(((Fox)entity).getFirstTrustedPlayer());
  274.                             newFox.setFoxType(((Fox)entity).getFoxType());
  275.                             newFox.setSecondTrustedPlayer(((Fox)entity).getSecondTrustedPlayer());
  276.                             newFox.setCrouching(((Fox)entity).isCrouching());
  277.                             newFox.setSleeping(((Fox)entity).isSleeping());
  278.                         }
  279.                         else if (entity instanceof Villager) { // are villagers peaceful mobs?? maybe
  280.                             Villager newVillager = (Villager)world.spawnEntity(newEntPos, EntityType.VILLAGER);
  281.                             newVillager.setProfession(((Villager)entity).getProfession());
  282.                             newVillager.setVillagerType(((Villager)entity).getVillagerType());
  283.                             newVillager.setVillagerExperience(((Villager)entity).getVillagerExperience());
  284.                             newVillager.setVillagerLevel(((Villager)entity).getVillagerLevel());
  285.                             newEnt = newVillager;
  286.                         }
  287.                         else if (entity instanceof IronGolem) {
  288.                             IronGolem newIronGolem = (IronGolem)world.spawnEntity(newEntPos, EntityType.IRON_GOLEM);
  289.                             newIronGolem.setPlayerCreated(((IronGolem)entity).isPlayerCreated());
  290.                             newEnt = newIronGolem;
  291.                         }
  292.                         // fish
  293.                         else if (entity instanceof Cod)
  294.                             newEnt = world.spawnEntity(newEntPos, EntityType.COD);
  295.                         else if (entity instanceof Salmon)
  296.                             newEnt = world.spawnEntity(newEntPos, EntityType.SALMON);
  297.                         else if (entity instanceof PufferFish) {
  298.                             PufferFish newPufferFish = (PufferFish)world.spawnEntity(newEntPos, EntityType.PUFFERFISH);
  299.                             newPufferFish.setPuffState(((PufferFish)entity).getPuffState());
  300.                             newEnt = newPufferFish;
  301.                         }
  302.                         else if (entity instanceof TropicalFish) {
  303.                             TropicalFish newTropicalFish = (TropicalFish)world.spawnEntity(newEntPos, EntityType.TROPICAL_FISH);
  304.                             newTropicalFish.setBodyColor(((TropicalFish)entity).getBodyColor());
  305.                             newTropicalFish.setPattern(((TropicalFish)entity).getPattern());
  306.                             newTropicalFish.setPatternColor(((TropicalFish)entity).getPatternColor());
  307.                             newEnt = newTropicalFish;
  308.                         }
  309.  
  310.                         // final adjustments
  311.                         if (newEnt != null) {
  312.  
  313.                             // set age
  314.                             if (newEnt instanceof Ageable) {
  315.                                 if (((Ageable) entity).isAdult())
  316.                                     ((Ageable) entity).setAdult();
  317.                                 else
  318.                                     ((Ageable) entity).setBaby();
  319.                             }
  320.  
  321.                             // set name tag
  322.                             String customName = entity.getCustomName();
  323.                             if (customName != null) {
  324.                                 newEnt.setCustomName(customName);
  325.                                 newEnt.setCustomNameVisible(entity.isCustomNameVisible());
  326.                             }
  327.  
  328.                             // set pet owner
  329.                             if (newEnt instanceof Tameable) {
  330.                                 if (((Tameable) entity).isTamed()) {
  331.                                     ((Tameable) newEnt).setTamed(true);
  332.                                     ((Tameable) newEnt).setOwner(((Tameable) entity).getOwner());
  333.                                 }
  334.                             }
  335.  
  336.                             // sit if pet
  337.                             if (newEnt instanceof Sittable)
  338.                                 ((Sittable) newEnt).setSitting(((Sittable) entity).isSitting());
  339.  
  340.                             // leash entities to fences
  341.                             Entity holder;
  342.                             try {
  343.                                 holder = ((LivingEntity) entity).getLeashHolder();
  344.                             } catch (IllegalStateException ie){
  345.                                 holder = null;
  346.                             }
  347.  
  348.                             if (holder != null) {
  349.                                 Location newHitchPos = getNewPosition(startPos1, startPos2, holder.getLocation(), destinationWorld);
  350.                                 LeashHitch newHitch = (LeashHitch) world.spawnEntity(newHitchPos, EntityType.LEASH_HITCH);
  351.                                 ((LivingEntity)newEnt).setLeashHolder(newHitch);
  352.                             }
  353.                         }
  354.                     }
  355.                 }
  356.             }
  357.         }
  358.  
  359.         return true;
  360.     }
  361. }
  362.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement