Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package net.voigon.elevators;
- import java.util.AbstractMap;
- import java.util.HashMap;
- import java.util.HashSet;
- import java.util.Map;
- import java.util.Map.Entry;
- import java.util.Set;
- import java.util.UUID;
- import org.bukkit.Bukkit;
- import org.bukkit.GameMode;
- import org.bukkit.Location;
- import org.bukkit.Material;
- import org.bukkit.block.Block;
- import org.bukkit.entity.FallingBlock;
- import org.bukkit.entity.Player;
- import org.bukkit.scheduler.BukkitRunnable;
- import org.bukkit.util.Vector;
- import lombok.Getter;
- import lombok.RequiredArgsConstructor;
- import lombok.Setter;
- @RequiredArgsConstructor
- public class ElevatorOperationTask extends BukkitRunnable {
- Elevators plugin = Elevators.getInstance();
- final Elevator
- elevator;
- final int
- sourceFloor;
- final Set<Block>
- blocks;
- final double
- abs = .2;
- Set<FallingBlock>
- fallingBlocks = new HashSet<>();
- Set<Block>
- blocksToUpdate = new HashSet<>();
- double lastY = -1;
- int floor = 1;
- Map<Location, Entry<Material, Byte>>
- blocksToRevert = new HashMap<>();
- @Getter @Setter
- boolean
- cancelled;
- Map<Player, Vector>
- vectors = new HashMap<>();
- @SuppressWarnings("deprecation")
- public void init() {
- blocksToRevert.clear();
- for (Block block : Utils.getBlocks(elevator.getLevel2Minimum(), elevator.getLevel2Maximum(),
- elevator.getLevel2Maximum().getBlockY())) {
- blocksToRevert.put(block.getLocation(), new AbstractMap.SimpleEntry<>(block.getType(), block.getData()));
- block.setType(Material.AIR);
- block.getState().update();
- }
- for (Block block : Utils.getBlocks(elevator.getLevel1Minimum(), elevator.getLevel1Maximum(),
- elevator.getLevel1Maximum().getBlockY())) {
- FallingBlock fallingBlock = block.getWorld().spawnFallingBlock(block.getLocation(), block.getType(),
- block.getData());
- fallingBlock.setHurtEntities(false);
- fallingBlock.setDropItem(false);
- System.out.println(fallingBlock.getVelocity());
- plugin.getFallingBlocks().put(fallingBlock, elevator);
- plugin.getFallingBlocksWithOrigin().put(fallingBlock, block.getLocation());
- plugin.getFallingBlocksWithFloor().put(fallingBlock, sourceFloor);
- blocksToUpdate.add(block);
- fallingBlocks.add(fallingBlock);
- }
- /*for (UUID uuid : elevator.getPlayersInFloor(sourceFloor)) {
- //Player player = Bukkit.getPlayer(uuid);
- //Location to = Utils.getBlockLocation(player.getLocation());
- //to.setY(elevator.getLevel2Maximum().getY());
- //Vector vector = to.toVector().subtract(Utils.getBlockLocation(player.getLocation()).toVector());
- //vectors.put(player, vector);
- //player.setVelocity(vector.multiply(0.1));
- } */
- }
- @Override
- public void run() {
- double targetY = elevator.getLevel2Maximum().getY();
- if (lastY == -1) {
- init();
- lastY = elevator.getLevel1Maximum().getBlockY();
- }
- if (isCancelled()) return;
- System.out.println("ElevatorOperationTask lastY = " + lastY);
- System.out.println("ElevatorOperationTask targetY = " + targetY);
- if (lastY == targetY + 1) {
- Bukkit.broadcastMessage("ElevatorOperationTask cancel");
- cancel();
- return;
- }
- /*if (fallingBlocks.size() == 0) {
- for (Block block : blocks) {
- FallingBlock fallingBlock = block.getWorld().spawnFallingBlock(block.getLocation(), block.getType(),
- block.getData());
- fallingBlock.setHurtEntities(false);
- fallingBlock.setDropItem(false);
- fallingBlocks.add(fallingBlock);
- }
- } */
- for (UUID uuid : elevator.getPlayersInFloor(sourceFloor)) {
- Player player = Bukkit.getPlayer(uuid);
- if (player == null) continue;
- player.playSound(player.getLocation(), elevator.getTransportSound(), elevator.getTransportVolume(),
- elevator.getTransportPitch());
- player.setAllowFlight(true);
- //player.setVelocity(new Vector(0, abs, 0));
- player.teleport(player.getLocation().add(0, abs, 0));
- }
- for (FallingBlock block : fallingBlocks) {
- //block.setVelocity(new Vector(0, 0, 0));
- block.setVelocity(block.getVelocity().setY(abs));
- //block.teleport(block.getLocation().add(0, abs, 0));
- }
- lastY = lastY + abs;
- }
- @Override
- public synchronized void cancel() throws IllegalStateException {
- super.cancel();
- setCancelled(true);
- elevator.setOperating(false);
- for (UUID uuid : elevator.getPlayersInFloor(sourceFloor)) {
- Player player = Bukkit.getPlayer(uuid);
- elevator.getPlayersInFloor(floor).add(uuid);
- if (player.getAllowFlight() && player.getGameMode() != GameMode.CREATIVE)
- player.setAllowFlight(false);
- player.playSound(player.getLocation(), elevator.getReachSound(), elevator.getReachVolume(),
- elevator.getReachPitch());
- player.setVelocity(new Vector(0, 0, 0));
- //player.teleport(player.getLocation().add(0, abs, 0));
- }
- for (FallingBlock block : fallingBlocks)
- block.remove();
- for (Entry<Location, Entry<Material, Byte>> entry : blocksToRevert.entrySet()) {
- Block block = entry.getKey().getBlock();
- block.setType(entry.getValue().getKey());
- block.setData(entry.getValue().getValue());
- block.getState().update();
- }
- for (Block block : blocksToUpdate)
- block.getState().update();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement