From 2abe10bcde59461c3b9dceee16f350f0d7eeadde Mon Sep 17 00:00:00 2001 From: EpicKnarvik97 Date: Sun, 10 Oct 2021 15:10:36 +0200 Subject: [PATCH] Improves the way chunks are loaded, and decreases the wait time before players are put into minecarts --- .../net/knarcraft/stargate/portal/Portal.java | 72 ++++++++++++------- 1 file changed, 46 insertions(+), 26 deletions(-) diff --git a/src/main/java/net/knarcraft/stargate/portal/Portal.java b/src/main/java/net/knarcraft/stargate/portal/Portal.java index 072dc7d..5e6a906 100644 --- a/src/main/java/net/knarcraft/stargate/portal/Portal.java +++ b/src/main/java/net/knarcraft/stargate/portal/Portal.java @@ -501,6 +501,8 @@ public class Portal { //Load chunks to make sure not to teleport to the void loadChunks(); + Stargate.server.getScheduler().scheduleSyncDelayedTask(Stargate.stargate, + this::unloadChunks, 4000); //If no event is passed in, assume it's a teleport, and act as such if (event == null) { @@ -549,6 +551,8 @@ public class Portal { //Load chunks to make sure not to teleport to the void loadChunks(); + Stargate.server.getScheduler().scheduleSyncDelayedTask(Stargate.stargate, + this::unloadChunks, 4000); if (!passengers.isEmpty()) { if (vehicle instanceof RideableMinecart || vehicle instanceof Boat) { @@ -579,7 +583,7 @@ public class Portal { private void teleportLivingVehicle(Vehicle vehicle, Location exit, List passengers) { vehicle.eject(); vehicle.teleport(exit); - handleVehiclePassengers(passengers, vehicle); + handleVehiclePassengers(passengers, vehicle, 6); } /** @@ -597,7 +601,7 @@ public class Portal { vehicle.eject(); vehicle.remove(); newVehicle.setRotation(exit.getYaw(), exit.getPitch()); - handleVehiclePassengers(passengers, newVehicle); + handleVehiclePassengers(passengers, newVehicle, 1); Stargate.server.getScheduler().scheduleSyncDelayedTask(Stargate.stargate, () -> newVehicle.setVelocity(newVelocity), 1); } @@ -607,8 +611,9 @@ public class Portal { * * @param passengers

The passengers to handle

* @param targetVehicle

The vehicle the passengers should be put into

+ * @param delay

The amount of milliseconds to wait before adding the vehicle passengers

*/ - private void handleVehiclePassengers(List passengers, Vehicle targetVehicle) { + private void handleVehiclePassengers(List passengers, Vehicle targetVehicle, long delay) { for (Entity passenger : passengers) { passenger.eject(); //TODO: Fix random java.lang.IllegalStateException: Removing entity while ticking! @@ -616,7 +621,11 @@ public class Portal { Stargate.debug("handleVehiclePassengers", "Failed to teleport passenger"); } Stargate.server.getScheduler().scheduleSyncDelayedTask(Stargate.stargate, - () -> targetVehicle.addPassenger(passenger), 6); + () -> { + if (!targetVehicle.addPassenger(passenger)) { + Stargate.debug("handleVehiclePassengers", "Failed to add passenger"); + } + }, delay); } } @@ -752,36 +761,47 @@ public class Portal { } /** - * Loads the chunks at the portal's corners + * Unloads the chunks outside the portal's entrance */ - public void loadChunks() { - for (RelativeBlockVector vector : gate.getLayout().getCorners()) { - Chunk chunk = getBlockAt(vector).getChunk(); - - //Get the chunk in front of the gate corner - Location cornerLocation = getBlockAt(vector).getLocation(); - int blockOffset = options.isBackwards() ? -5 : 5; - Location fiveBlocksForward = DirectionHelper.moveLocation(cornerLocation, 0, 0, blockOffset, - getYaw()); - Chunk forwardChunk = fiveBlocksForward.getChunk(); - - //Load the chunks - loadOneChunk(chunk); - loadOneChunk(forwardChunk); + private void unloadChunks() { + for (Chunk chunk : getChunksToLoad()) { + chunk.removePluginChunkTicket(Stargate.stargate); } } /** - * Loads one chunk - * - * @param chunk

The chunk to load

+ * Loads the chunks outside the portal's entrance */ - private void loadOneChunk(Chunk chunk) { - if (!getWorld().isChunkLoaded(chunk)) { - if (!chunk.load()) { - Stargate.debug("loadChunks", "Failed to load chunk " + chunk); + private void loadChunks() { + for (Chunk chunk : getChunksToLoad()) { + chunk.addPluginChunkTicket(Stargate.stargate); + } + } + + /** + * Gets all relevant chunks near this portal's entrance which need to be loaded before teleportation + * + * @return

A list of chunks to load

+ */ + private List getChunksToLoad() { + List chunksToLoad = new ArrayList<>(); + for (RelativeBlockVector vector : gate.getLayout().getEntrances()) { + BlockLocation entranceLocation = getBlockAt(vector); + Chunk chunk = entranceLocation.getChunk(); + if (!chunksToLoad.contains(chunk)) { + chunksToLoad.add(chunk); + } + + //Get the chunk in front of the gate corner + int blockOffset = options.isBackwards() ? -5 : 5; + Location fiveBlocksForward = DirectionHelper.moveLocation(entranceLocation, 0, 0, blockOffset, + getYaw()); + Chunk forwardChunk = fiveBlocksForward.getChunk(); + if (!chunksToLoad.contains(forwardChunk)) { + chunksToLoad.add(forwardChunk); } } + return chunksToLoad; } /**