From a5409445eafdc2352e09fcbfe848814f6febc33b Mon Sep 17 00:00:00 2001 From: Andrew121410 Date: Tue, 10 Sep 2024 20:16:03 -0400 Subject: [PATCH] Instead of constantly calling .runTaskLater in ElevatorRunnable just use one. --- .../mc/world16elevators/Elevator.java | 4 +- .../mc/world16elevators/ElevatorRunnable.java | 98 +++++++++---------- 2 files changed, 49 insertions(+), 53 deletions(-) diff --git a/src/main/java/com/andrew121410/mc/world16elevators/Elevator.java b/src/main/java/com/andrew121410/mc/world16elevators/Elevator.java index b37370b..12f28ec 100644 --- a/src/main/java/com/andrew121410/mc/world16elevators/Elevator.java +++ b/src/main/java/com/andrew121410/mc/world16elevators/Elevator.java @@ -222,8 +222,8 @@ public void goToFloor(int floorNumber, ElevatorStatus elevatorStatus, ElevatorWh int minFloorY = getFloor(topBottomFloor).getBlockUnderMainDoor().getBlockY() - 1; int maxFloorY = getFloor(topFloor).getBlockUnderMainDoor().getBlockY() + 1; - //Start ticking the elevator. - new ElevatorRunnable(plugin, this, goUp, elevatorFloor, elevatorStatus, minFloorY, maxFloorY).runTask(plugin); + // Start ticking the elevator. + new ElevatorRunnable(plugin, this, goUp, elevatorFloor, elevatorStatus, minFloorY, maxFloorY).startElevator(); } protected void move(int howManyY, boolean goUP) { diff --git a/src/main/java/com/andrew121410/mc/world16elevators/ElevatorRunnable.java b/src/main/java/com/andrew121410/mc/world16elevators/ElevatorRunnable.java index 7adaa5a..86d18ac 100644 --- a/src/main/java/com/andrew121410/mc/world16elevators/ElevatorRunnable.java +++ b/src/main/java/com/andrew121410/mc/world16elevators/ElevatorRunnable.java @@ -10,59 +10,53 @@ public class ElevatorRunnable extends BukkitRunnable { private final World16Elevators plugin; private final Elevator elevator; - private final boolean goingUp; - private final ElevatorFloor elevatorFloor; + private final ElevatorFloor targetFloor; private final ElevatorStatus elevatorStatus; - private int counter; - private ElevatorFloor floorThatWeAreGoingToPass; + private final int maxFloorY; + private final int minFloorY; - private int maxFloorY; - private int minFloorY; - - public ElevatorRunnable(World16Elevators plugin, - Elevator elevator, - boolean goingUp, - ElevatorFloor elevatorFloor, - ElevatorStatus elevatorStatus, - int counter, - ElevatorFloor floorThatWeAreGoingToPass, - int minFloorY, - int maxFloorY) { + private int ticksPerMove; + private int tickCounter; + + public ElevatorRunnable(World16Elevators plugin, Elevator elevator, boolean goingUp, ElevatorFloor targetFloor, ElevatorStatus elevatorStatus, int minFloorY, int maxFloorY) { this.plugin = plugin; this.elevator = elevator; this.goingUp = goingUp; - this.elevatorFloor = elevatorFloor; + this.targetFloor = targetFloor; this.elevatorStatus = elevatorStatus; - this.counter = counter; - this.floorThatWeAreGoingToPass = floorThatWeAreGoingToPass; this.minFloorY = minFloorY; this.maxFloorY = maxFloorY; + + this.ticksPerMove = (int) elevator.getElevatorSettings().getTicksPerSecond(); + this.tickCounter = 0; } - public ElevatorRunnable(World16Elevators plugin, - Elevator elevator, - boolean goingUp, - ElevatorFloor elevatorFloor, - ElevatorStatus elevatorStatus, - int minFloorY, - int maxFloorY) { - this(plugin, - elevator, - goingUp, - elevatorFloor, - elevatorStatus, - (int) elevator.getElevatorSettings().getTicksPerSecond(), - null, - minFloorY, - maxFloorY); + public void startElevator() { + this.runTaskTimer(plugin, 20L, 1L); } @Override public void run() { - if (elevator.isIdling()) return; + if (elevator.isIdling()) { + this.cancel(); + return; + } + + // Increment the tick counter + tickCounter++; + + // Increment the tick counter and move the elevator only if the counter reaches the ticksPerMove threshold + // For example, if ticksPerMove is 6, the elevator will move every 6 ticks + if (tickCounter < ticksPerMove) { + return; // If it's not time yet, return early and wait for the next tick + } + + // Reset the tick counter after a move + tickCounter = 0; + elevator.reCalculateFloorBuffer(goingUp); ElevatorFloor stopByFloor = !elevator.getStopBy().getPriorityQueue().isEmpty() ? elevator.getFloor(elevator.getStopBy().getPriorityQueue().peek()) : null; @@ -80,11 +74,13 @@ public void run() { } // Check's if at the floor if so then stop the elevator. - if (elevator.getElevatorMovement().getAtDoor().getBlockY() == elevatorFloor.getBlockUnderMainDoor().getBlockY()) { - elevator.floorStop(elevatorFloor, elevatorStatus); + if (elevator.getElevatorMovement().getAtDoor().getBlockY() == targetFloor.getBlockUnderMainDoor().getBlockY()) { + elevator.floorStop(targetFloor, elevatorStatus); + this.cancel(); return; } else if (stopByFloor != null && elevator.getElevatorMovement().getAtDoor().getY() == stopByFloor.getBlockUnderMainDoor().getY()) { - elevator.floorStop(elevatorFloor, elevatorStatus, elevator.getStopBy(), stopByFloor); + elevator.floorStop(targetFloor, elevatorStatus, elevator.getStopBy(), stopByFloor); + this.cancel(); return; } @@ -98,6 +94,7 @@ public void run() { elevator.setIdling(false); elevator.setGoing(false); elevator.setEmergencyStop(false); + this.cancel(); return; } @@ -109,22 +106,21 @@ public void run() { PlayerUtils.smoothTeleport(player, player.getLocation().add(0, goingUp ? 1 : -1, 0)); } - // Elevator leveling - int x = elevator.getElevatorMovement().getAtDoor().getBlockY(); - int z = elevatorFloor.getBlockUnderMainDoor().getBlockY(); + // Elevator leveling (slows down the elevator as it approaches the target floor) + int currentY = elevator.getElevatorMovement().getAtDoor().getBlockY(); + int targetY = targetFloor.getBlockUnderMainDoor().getBlockY(); if (elevator.getElevatorSettings().isDoElevatorLeveling()) { if (goingUp) { - x += 5; - if (x >= z) counter += 1; + currentY += 5; + if (currentY >= targetY) { + ticksPerMove += 1; + } } else { - x -= 5; - if (x <= z) counter += 1; + currentY -= 5; + if (currentY <= targetY) { + ticksPerMove += 1; + } } } - - // Don't try to register another task if the plugin is disabled. - if (!this.plugin.isEnabled()) return; - - new ElevatorRunnable(plugin, elevator, goingUp, elevatorFloor, elevatorStatus, counter, floorThatWeAreGoingToPass, minFloorY, maxFloorY).runTaskLater(plugin, counter); } }