Advertisement
Voigon

Untitled

Jul 13th, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.29 KB | None | 0 0
  1. package net.voigon.elevators;
  2.  
  3. import java.util.AbstractMap;
  4. import java.util.HashMap;
  5. import java.util.HashSet;
  6. import java.util.Map;
  7. import java.util.Map.Entry;
  8. import java.util.Set;
  9. import java.util.UUID;
  10.  
  11. import org.bukkit.Bukkit;
  12. import org.bukkit.GameMode;
  13. import org.bukkit.Location;
  14. import org.bukkit.Material;
  15. import org.bukkit.block.Block;
  16. import org.bukkit.entity.FallingBlock;
  17. import org.bukkit.entity.Player;
  18. import org.bukkit.scheduler.BukkitRunnable;
  19. import org.bukkit.util.Vector;
  20.  
  21. import lombok.Getter;
  22. import lombok.RequiredArgsConstructor;
  23. import lombok.Setter;
  24.  
  25. @RequiredArgsConstructor
  26. public class ElevatorOperationTask extends BukkitRunnable {
  27.  
  28.     Elevators plugin = Elevators.getInstance();
  29.    
  30.     final Elevator
  31.             elevator;
  32.    
  33.     final int
  34.             sourceFloor;
  35.    
  36.     final Set<Block>
  37.             blocks;
  38.    
  39.     final double
  40.             abs = .2;
  41.    
  42.     Set<FallingBlock>
  43.             fallingBlocks = new HashSet<>();
  44.    
  45.     Set<Block>
  46.             blocksToUpdate = new HashSet<>();
  47.    
  48.     double lastY = -1;
  49.    
  50.     int floor = 1;
  51.    
  52.     Map<Location, Entry<Material, Byte>>
  53.             blocksToRevert = new HashMap<>();
  54.    
  55.     @Getter @Setter
  56.     boolean
  57.             cancelled;
  58.    
  59.     Map<Player, Vector>
  60.             vectors = new HashMap<>();
  61.    
  62.     @SuppressWarnings("deprecation")
  63.     public void init() {
  64.         blocksToRevert.clear();
  65.        
  66.         for (Block block : Utils.getBlocks(elevator.getLevel2Minimum(), elevator.getLevel2Maximum(),
  67.                 elevator.getLevel2Maximum().getBlockY())) {
  68.             blocksToRevert.put(block.getLocation(), new AbstractMap.SimpleEntry<>(block.getType(), block.getData()));
  69.            
  70.             block.setType(Material.AIR);
  71.             block.getState().update();
  72.         }
  73.        
  74.         for (Block block : Utils.getBlocks(elevator.getLevel1Minimum(), elevator.getLevel1Maximum(),
  75.                 elevator.getLevel1Maximum().getBlockY())) {
  76.             FallingBlock fallingBlock = block.getWorld().spawnFallingBlock(block.getLocation(), block.getType(),
  77.                     block.getData());
  78.             fallingBlock.setHurtEntities(false);
  79.             fallingBlock.setDropItem(false);
  80.             System.out.println(fallingBlock.getVelocity());
  81.  
  82.             plugin.getFallingBlocks().put(fallingBlock, elevator);
  83.             plugin.getFallingBlocksWithOrigin().put(fallingBlock, block.getLocation());
  84.             plugin.getFallingBlocksWithFloor().put(fallingBlock, sourceFloor);
  85.            
  86.             blocksToUpdate.add(block);
  87.            
  88.             fallingBlocks.add(fallingBlock);
  89.            
  90.         }
  91.  
  92.         /*for (UUID uuid : elevator.getPlayersInFloor(sourceFloor)) {
  93.             //Player player = Bukkit.getPlayer(uuid);
  94.             //Location to = Utils.getBlockLocation(player.getLocation());
  95.             //to.setY(elevator.getLevel2Maximum().getY());
  96.             //Vector vector = to.toVector().subtract(Utils.getBlockLocation(player.getLocation()).toVector());
  97.             //vectors.put(player, vector);
  98.            
  99.             //player.setVelocity(vector.multiply(0.1));
  100.         } */
  101.        
  102.     }
  103.  
  104.     @Override
  105.     public void run() {
  106.         double targetY = elevator.getLevel2Maximum().getY();
  107.  
  108.         if (lastY == -1) {
  109.             init();
  110.             lastY = elevator.getLevel1Maximum().getBlockY();
  111.         }
  112.        
  113.         if (isCancelled()) return;
  114.        
  115.         System.out.println("ElevatorOperationTask lastY = " + lastY);
  116.         System.out.println("ElevatorOperationTask targetY = " + targetY);
  117.  
  118.         if (lastY == targetY + 1) {
  119.             Bukkit.broadcastMessage("ElevatorOperationTask cancel");
  120.             cancel();
  121.             return;
  122.         }
  123.        
  124.         /*if (fallingBlocks.size() == 0) {
  125.             for (Block block : blocks) {
  126.                 FallingBlock fallingBlock = block.getWorld().spawnFallingBlock(block.getLocation(), block.getType(),
  127.                         block.getData());
  128.                 fallingBlock.setHurtEntities(false);
  129.                 fallingBlock.setDropItem(false);
  130.                 fallingBlocks.add(fallingBlock);
  131.             }
  132.  
  133.         } */
  134.         for (UUID uuid : elevator.getPlayersInFloor(sourceFloor)) {
  135.             Player player = Bukkit.getPlayer(uuid);
  136.             if (player == null) continue;
  137.            
  138.             player.playSound(player.getLocation(), elevator.getTransportSound(), elevator.getTransportVolume(),
  139.                     elevator.getTransportPitch());
  140.             player.setAllowFlight(true);
  141.            
  142.             //player.setVelocity(new Vector(0, abs, 0));
  143.             player.teleport(player.getLocation().add(0, abs, 0));
  144.  
  145.         }
  146.  
  147.         for (FallingBlock block : fallingBlocks) {
  148.             //block.setVelocity(new Vector(0, 0, 0));
  149.             block.setVelocity(block.getVelocity().setY(abs));
  150.             //block.teleport(block.getLocation().add(0, abs, 0));
  151.  
  152.         }
  153.        
  154.        
  155.         lastY = lastY + abs;
  156.  
  157.     }
  158.    
  159.     @Override
  160.     public synchronized void cancel() throws IllegalStateException {
  161.         super.cancel();
  162.        
  163.         setCancelled(true);
  164.         elevator.setOperating(false);
  165.        
  166.         for (UUID uuid : elevator.getPlayersInFloor(sourceFloor)) {
  167.             Player player = Bukkit.getPlayer(uuid);
  168.            
  169.             elevator.getPlayersInFloor(floor).add(uuid);
  170.            
  171.             if (player.getAllowFlight() && player.getGameMode() != GameMode.CREATIVE)
  172.                 player.setAllowFlight(false);
  173.            
  174.             player.playSound(player.getLocation(), elevator.getReachSound(), elevator.getReachVolume(),
  175.                     elevator.getReachPitch());
  176.             player.setVelocity(new Vector(0, 0, 0));
  177.            
  178.             //player.teleport(player.getLocation().add(0, abs, 0));
  179.            
  180.         }
  181.  
  182.         for (FallingBlock block : fallingBlocks)
  183.             block.remove();
  184.                
  185.         for (Entry<Location, Entry<Material, Byte>> entry : blocksToRevert.entrySet()) {
  186.             Block block = entry.getKey().getBlock();
  187.             block.setType(entry.getValue().getKey());
  188.             block.setData(entry.getValue().getValue());
  189.             block.getState().update();
  190.         }
  191.        
  192.         for (Block block : blocksToUpdate)
  193.             block.getState().update();
  194.        
  195.        
  196.     }
  197.  
  198. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement