Improves the way chunks are loaded, and decreases the wait time before players are put into minecarts

This commit is contained in:
Kristian Knarvik 2021-10-10 15:10:36 +02:00
parent 7a9dbb8046
commit 2abe10bcde

View File

@ -501,6 +501,8 @@ public class Portal {
//Load chunks to make sure not to teleport to the void //Load chunks to make sure not to teleport to the void
loadChunks(); 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 no event is passed in, assume it's a teleport, and act as such
if (event == null) { if (event == null) {
@ -549,6 +551,8 @@ public class Portal {
//Load chunks to make sure not to teleport to the void //Load chunks to make sure not to teleport to the void
loadChunks(); loadChunks();
Stargate.server.getScheduler().scheduleSyncDelayedTask(Stargate.stargate,
this::unloadChunks, 4000);
if (!passengers.isEmpty()) { if (!passengers.isEmpty()) {
if (vehicle instanceof RideableMinecart || vehicle instanceof Boat) { if (vehicle instanceof RideableMinecart || vehicle instanceof Boat) {
@ -579,7 +583,7 @@ public class Portal {
private void teleportLivingVehicle(Vehicle vehicle, Location exit, List<Entity> passengers) { private void teleportLivingVehicle(Vehicle vehicle, Location exit, List<Entity> passengers) {
vehicle.eject(); vehicle.eject();
vehicle.teleport(exit); vehicle.teleport(exit);
handleVehiclePassengers(passengers, vehicle); handleVehiclePassengers(passengers, vehicle, 6);
} }
/** /**
@ -597,7 +601,7 @@ public class Portal {
vehicle.eject(); vehicle.eject();
vehicle.remove(); vehicle.remove();
newVehicle.setRotation(exit.getYaw(), exit.getPitch()); newVehicle.setRotation(exit.getYaw(), exit.getPitch());
handleVehiclePassengers(passengers, newVehicle); handleVehiclePassengers(passengers, newVehicle, 1);
Stargate.server.getScheduler().scheduleSyncDelayedTask(Stargate.stargate, Stargate.server.getScheduler().scheduleSyncDelayedTask(Stargate.stargate,
() -> newVehicle.setVelocity(newVelocity), 1); () -> newVehicle.setVelocity(newVelocity), 1);
} }
@ -607,8 +611,9 @@ public class Portal {
* *
* @param passengers <p>The passengers to handle</p> * @param passengers <p>The passengers to handle</p>
* @param targetVehicle <p>The vehicle the passengers should be put into</p> * @param targetVehicle <p>The vehicle the passengers should be put into</p>
* @param delay <p>The amount of milliseconds to wait before adding the vehicle passengers</p>
*/ */
private void handleVehiclePassengers(List<Entity> passengers, Vehicle targetVehicle) { private void handleVehiclePassengers(List<Entity> passengers, Vehicle targetVehicle, long delay) {
for (Entity passenger : passengers) { for (Entity passenger : passengers) {
passenger.eject(); passenger.eject();
//TODO: Fix random java.lang.IllegalStateException: Removing entity while ticking! //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.debug("handleVehiclePassengers", "Failed to teleport passenger");
} }
Stargate.server.getScheduler().scheduleSyncDelayedTask(Stargate.stargate, 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() { private void unloadChunks() {
for (RelativeBlockVector vector : gate.getLayout().getCorners()) { for (Chunk chunk : getChunksToLoad()) {
Chunk chunk = getBlockAt(vector).getChunk(); chunk.removePluginChunkTicket(Stargate.stargate);
//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);
} }
} }
/** /**
* Loads one chunk * Loads the chunks outside the portal's entrance
*
* @param chunk <p>The chunk to load</p>
*/ */
private void loadOneChunk(Chunk chunk) { private void loadChunks() {
if (!getWorld().isChunkLoaded(chunk)) { for (Chunk chunk : getChunksToLoad()) {
if (!chunk.load()) { chunk.addPluginChunkTicket(Stargate.stargate);
Stargate.debug("loadChunks", "Failed to load chunk " + chunk); }
}
/**
* Gets all relevant chunks near this portal's entrance which need to be loaded before teleportation
*
* @return <p>A list of chunks to load</p>
*/
private List<Chunk> getChunksToLoad() {
List<Chunk> 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;
} }
/** /**