More async Paper support for chunk loading

This commit is contained in:
Fabrizio La Rosa 2020-06-25 05:08:35 +02:00
parent bd3ee69a9a
commit 62cd1ddbcb
5 changed files with 90 additions and 57 deletions

View File

@ -1138,10 +1138,13 @@ public class IslandManager {
}
}
Bukkit.getServer().getScheduler().runTask(skyblock, () -> {
Bukkit.getServer().getScheduler().runTaskAsynchronously(skyblock, () -> {
Location loc = island.getLocation(IslandWorld.Normal, IslandEnvironment.Visitor);
if (!player.getGameMode().equals(GameMode.CREATIVE) && !player.getGameMode().equals(GameMode.SPECTATOR)) {
loc = LocationUtil.getSafeLocation(loc);
CompletableFuture<Location> safeLoc = LocationUtil.getSafeLocation(loc);
if(safeLoc != null){
loc = safeLoc.join();
}
}
if(loc != null){
PaperLib.teleportAsync(player, loc);

View File

@ -29,6 +29,7 @@ import org.bukkit.potion.PotionEffect;
import java.io.File;
import java.util.Objects;
import java.util.concurrent.CompletableFuture;
public class Move implements Listener {
@ -193,30 +194,41 @@ public class Move implements Listener {
}
private void teleportPlayerToIslandSpawn(Player player, IslandWorld world, Island island) {
Location loc;
if (island.hasRole(IslandRole.Member, player.getUniqueId()) || island.hasRole(IslandRole.Operator, player.getUniqueId())
|| island.hasRole(IslandRole.Owner, player.getUniqueId())) {
if (!player.getGameMode().equals(GameMode.CREATIVE) && !player.getGameMode().equals(GameMode.SPECTATOR)) {
loc = LocationUtil.getSafeLocation(island.getLocation(world, IslandEnvironment.Main));
Bukkit.getScheduler().runTaskAsynchronously(skyblock, () -> {
Location loc = null;
if (island.hasRole(IslandRole.Member, player.getUniqueId()) || island.hasRole(IslandRole.Operator, player.getUniqueId())
|| island.hasRole(IslandRole.Owner, player.getUniqueId())) {
if (!player.getGameMode().equals(GameMode.CREATIVE) && !player.getGameMode().equals(GameMode.SPECTATOR)) {
CompletableFuture<Location> safeLoc = LocationUtil.getSafeLocation(island.getLocation(world, IslandEnvironment.Main));
if(safeLoc != null){
loc = safeLoc.join();
}
} else {
loc = island.getLocation(world, IslandEnvironment.Main);
LocationUtil.removeWaterFromLoc(skyblock, loc);
}
} else {
loc = island.getLocation(world, IslandEnvironment.Main);
LocationUtil.removeWaterFromLoc(skyblock, loc);
if (!player.getGameMode().equals(GameMode.CREATIVE) && !player.getGameMode().equals(GameMode.SPECTATOR)) {
CompletableFuture<Location> safeLoc = LocationUtil.getSafeLocation(island.getLocation(world, IslandEnvironment.Visitor));
if(safeLoc != null){
loc = safeLoc.join();
}
} else {
loc = island.getLocation(world, IslandEnvironment.Visitor);
}
}
} else {
if (!player.getGameMode().equals(GameMode.CREATIVE) && !player.getGameMode().equals(GameMode.SPECTATOR)) {
loc = LocationUtil.getSafeLocation(island.getLocation(world, IslandEnvironment.Visitor));
} else {
loc = island.getLocation(world, IslandEnvironment.Visitor);
}
}
if(loc != null){
PaperLib.teleportAsync(player, loc);
} else {
LocationUtil.teleportPlayerToSpawn(player);
player.sendMessage(ChatColor.translateAlternateColorCodes('&',
Objects.requireNonNull(skyblock.getFileManager().getConfig(new File(skyblock.getDataFolder(), "language.yml"))
.getFileConfiguration().getString("Command.Island.Teleport.Unsafe.Message"))));
}
Location finalLoc = loc;
Bukkit.getScheduler().runTask(skyblock, () -> {
if(finalLoc != null){
PaperLib.teleportAsync(player, finalLoc);
} else {
LocationUtil.teleportPlayerToSpawn(player);
player.sendMessage(ChatColor.translateAlternateColorCodes('&',
skyblock.getFileManager().getConfig(new File(skyblock.getDataFolder(), "language.yml"))
.getFileConfiguration().getString("Command.Island.Teleport.Unsafe.Message")));
}
});
});
}
private void teleportPlayerToIslandSpawn(Player player, Island island) {

View File

@ -31,6 +31,7 @@ import java.io.File;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
public class Portal implements Listener {
@ -163,13 +164,20 @@ public class Portal implements Listener {
private void teleportPlayerToWorld(Player player, SoundManager soundManager, Island island, IslandEnvironment spawnEnvironment, Tick tick, IslandWorld toWorld) {
IslandWorld toWorldF = toWorld;
Bukkit.getScheduler().scheduleSyncDelayedTask(skyblock, () -> {
Bukkit.getScheduler().runTaskLaterAsynchronously(skyblock, () -> {
Location loc = island.getLocation(toWorldF, spawnEnvironment);
Location safeLoc = LocationUtil.getSafeLocation(loc);
CompletableFuture<Location> tempSafeLoc = LocationUtil.getSafeLocation(loc);
Location safeLoc = null;
if(tempSafeLoc != null) {
safeLoc = tempSafeLoc.join();
}
if(safeLoc != null){
loc = safeLoc;
}
PaperLib.teleportAsync(player, loc);
Location finalLoc = loc;
Bukkit.getScheduler().runTask(skyblock, () -> {
PaperLib.teleportAsync(player, finalLoc);
});
}, 1L);
soundManager.playSound(player, CompatibleSound.ENTITY_ENDERMAN_TELEPORT.getSound(), 1.0F, 1.0F);
player.setFallDistance(0.0F);

View File

@ -17,6 +17,8 @@ import org.bukkit.entity.Player;
import org.bukkit.event.player.PlayerMoveEvent;
import org.bukkit.event.player.PlayerTeleportEvent;
import java.util.concurrent.CompletableFuture;
public class PortalPermission extends ListeningPermission {
private final SkyBlock plugin;
@ -68,17 +70,22 @@ public class PortalPermission extends ListeningPermission {
}
}
private Location getToLocation(Location from, Player player) {
IslandManager islandManager = plugin.getIslandManager();
Island island = islandManager.getIslandAtLocation(from);
Location to = island.getLocation(IslandWorld.Normal, IslandEnvironment.Main);
if(island.hasRole(IslandRole.Visitor, player.getUniqueId())){
to = LocationUtil.getSafeLocation(island.getLocation(IslandWorld.Normal, IslandEnvironment.Visitor));
if(to == null){
to = LocationUtil.getSpawnLocation();
private CompletableFuture<Location> getToLocation(Location from, Player player) {
return CompletableFuture.supplyAsync(() -> {
IslandManager islandManager = plugin.getIslandManager();
Island island = islandManager.getIslandAtLocation(from);
Location to = island.getLocation(IslandWorld.Normal, IslandEnvironment.Main);
if(island.hasRole(IslandRole.Visitor, player.getUniqueId())){
CompletableFuture<Location> safeLoc = LocationUtil.getSafeLocation(island.getLocation(IslandWorld.Normal, IslandEnvironment.Visitor));
if(safeLoc != null) {
to = safeLoc.join();
}
if(to == null){
to = LocationUtil.getSpawnLocation();
}
}
}
return to;
return to;
});
}
}

View File

@ -25,6 +25,7 @@ import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.CompletableFuture;
import java.util.logging.Level;
public final class LocationUtil {
@ -41,29 +42,31 @@ public final class LocationUtil {
}
}
public static @Nullable Location getSafeLocation(Location loc){
boolean found = false;
Location locChecked = null;
if(loc != null && loc.getWorld() != null){
locChecked = loc.clone();
loc.getWorld().loadChunk(loc.getWorld().getChunkAt(loc));
for(int i=loc.getBlockY(); i>=0 && !found; i--){
locChecked = locChecked.subtract(0d, 1d, 0d);
found = checkBlock(locChecked);
}
if(!found){
for(int i=loc.getBlockY(); i<256 && !found; i++){
locChecked = locChecked.add(0d, 1d, 0d);
public static @Nullable CompletableFuture<Location> getSafeLocation(Location loc){
return CompletableFuture.supplyAsync(() -> {
boolean found = false;
Location locChecked = null;
if(loc != null && loc.getWorld() != null){
locChecked = loc.clone();
loc.getWorld().loadChunk(loc.getWorld().getChunkAt(loc));
for(int i=loc.getBlockY(); i>=0 && !found; i--){
locChecked = locChecked.subtract(0d, 1d, 0d);
found = checkBlock(locChecked);
}
if(!found){
for(int i=loc.getBlockY(); i<256 && !found; i++){
locChecked = locChecked.add(0d, 1d, 0d);
found = checkBlock(locChecked);
}
}
if (found) {
locChecked = locChecked.add(0d,1d,0d);
} else {
locChecked = null;
}
}
if (found) {
locChecked = locChecked.add(0d,1d,0d);
} else {
locChecked = null;
}
}
return locChecked;
return locChecked;
});
}
public static @Nonnull Location getDefinitiveLocation(Location loc){