Skip to content

Commit

Permalink
Stop this elevator if it goes above the max floor or bottom floor.
Browse files Browse the repository at this point in the history
I don't think this has ever happened except when I was debuging the code, but in case
  • Loading branch information
andrew121410 committed Sep 9, 2024
1 parent 15e1fc4 commit 7f95670
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,10 @@ public Elevator(World16Elevators plugin, String name, String world, ElevatorMove
}
}

// This should be used for like teleporting entities up and down.
public Collection<Entity> getEntities() {
return getBukkitWorld().getNearbyEntities(this.getElevatorMovement().getTeleportingBoundingBox());
}

// This should be used for like teleporting entities up and down.
public Collection<Player> getPlayers() {
return getEntities().stream().filter(entity -> entity instanceof Player).map(entity -> (Player) entity).collect(Collectors.toList());
}
Expand Down Expand Up @@ -220,8 +218,12 @@ public void goToFloor(int floorNumber, ElevatorStatus elevatorStatus, ElevatorWh
}
}

// Safety
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).runTask(plugin);
new ElevatorRunnable(plugin, this, goUp, elevatorFloor, elevatorStatus, minFloorY, maxFloorY).runTask(plugin);
}

protected void move(int howManyY, boolean goUP) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,45 @@ public class ElevatorRunnable extends BukkitRunnable {

private ElevatorFloor floorThatWeAreGoingToPass;

public ElevatorRunnable(World16Elevators plugin, Elevator elevator, boolean goingUp, ElevatorFloor elevatorFloor, ElevatorStatus elevatorStatus, int counter, ElevatorFloor floorThatWeAreGoingToPass) {
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) {
this.plugin = plugin;
this.elevator = elevator;
this.goingUp = goingUp;
this.elevatorFloor = elevatorFloor;
this.elevatorStatus = elevatorStatus;
this.counter = counter;
this.floorThatWeAreGoingToPass = floorThatWeAreGoingToPass;
this.minFloorY = minFloorY;
this.maxFloorY = maxFloorY;
}

public ElevatorRunnable(World16Elevators plugin, Elevator elevator, boolean goingUp, ElevatorFloor elevatorFloor, ElevatorStatus elevatorStatus) {
this(plugin, elevator, goingUp, elevatorFloor, elevatorStatus, (int) elevator.getElevatorSettings().getTicksPerSecond(), null);
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);
}

@Override
Expand Down Expand Up @@ -61,6 +88,11 @@ public void run() {
return;
}

// Safety Check
if (elevator.getElevatorMovement().getAtDoor().getY() > maxFloorY || elevator.getElevatorMovement().getAtDoor().getY() < minFloorY) {
this.elevator.setEmergencyStop(true);
}

// Stop's the elevator if emergencyStop is on.
if (elevator.isEmergencyStop()) {
elevator.setIdling(false);
Expand Down Expand Up @@ -93,6 +125,6 @@ public void run() {
// 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).runTaskLater(plugin, counter);
new ElevatorRunnable(plugin, elevator, goingUp, elevatorFloor, elevatorStatus, counter, floorThatWeAreGoingToPass, minFloorY, maxFloorY).runTaskLater(plugin, counter);
}
}

0 comments on commit 7f95670

Please sign in to comment.