From ca0a187b0d6ebc18f28a43271ed756e778ae2f3d Mon Sep 17 00:00:00 2001 From: Onno Date: Wed, 26 Jun 2024 22:34:48 +0200 Subject: [PATCH] Fix for multiverse teleportation problems on a multiworld server (also see thread in Discord server) (#53) --- .../classic/arena/PluginArenaManager.java | 59 ++++++------ .../classic/arena/PluginArenaUtils.java | 93 ++++++++++--------- .../arena/states/PluginStartingState.java | 3 +- .../events/spectator/SpectatorEvents.java | 3 +- .../classic/utils/version/VersionUtils.java | 9 +- 5 files changed, 86 insertions(+), 81 deletions(-) diff --git a/MiniGamesBox Classic/src/main/java/plugily/projects/minigamesbox/classic/arena/PluginArenaManager.java b/MiniGamesBox Classic/src/main/java/plugily/projects/minigamesbox/classic/arena/PluginArenaManager.java index 0eb202c7..e494dd7e 100644 --- a/MiniGamesBox Classic/src/main/java/plugily/projects/minigamesbox/classic/arena/PluginArenaManager.java +++ b/MiniGamesBox Classic/src/main/java/plugily/projects/minigamesbox/classic/arena/PluginArenaManager.java @@ -81,42 +81,43 @@ public void joinAttempt(@NotNull Player player, @NotNull IPluginArena arena) { new MessageBuilder("IN_GAME_SPECTATOR_BLOCKED").asKey().player(player).arena(arena).sendPlayer(); return; } - PluginArenaUtils.preparePlayerForGame(arena, player, arena.getSpectatorLocation(), true); - new MessageBuilder("IN_GAME_SPECTATOR_YOU_ARE_SPECTATOR").asKey().player(player).arena(arena).sendPlayer(); - PluginArenaUtils.hidePlayer(player, arena); - for(Player spectator : arena.getPlayers()) { - if(plugin.getUserManager().getUser(spectator).isSpectator()) { - VersionUtils.hidePlayer(plugin, player, spectator); - } else { - VersionUtils.showPlayer(plugin, player, spectator); + PluginArenaUtils.preparePlayerForGame(arena, player, arena.getSpectatorLocation(), true).thenAccept(act -> { + new MessageBuilder("IN_GAME_SPECTATOR_YOU_ARE_SPECTATOR").asKey().player(player).arena(arena).sendPlayer(); + PluginArenaUtils.hidePlayer(player, arena); + for (Player spectator : arena.getPlayers()) { + if (plugin.getUserManager().getUser(spectator).isSpectator()) { + VersionUtils.hidePlayer(plugin, player, spectator); + } else { + VersionUtils.showPlayer(plugin, player, spectator); + } } - } - additionalSpectatorSettings(player, arena); - plugin.getDebugger().debug("[{0}] Final join attempt as spectator for {1} took {2}ms", arena.getId(), player.getName(), System.currentTimeMillis() - start); + additionalSpectatorSettings(player, arena); + plugin.getDebugger().debug("[{0}] Final join attempt as spectator for {1} took {2}ms", arena.getId(), player.getName(), System.currentTimeMillis() - start); + }); return; } - PluginArenaUtils.preparePlayerForGame(arena, player, arena.getLobbyLocation(), false); - - new MessageBuilder(MessageBuilder.ActionType.JOIN).arena(arena).player(player).sendArena(); - new TitleBuilder("IN_GAME_JOIN_TITLE").asKey().arena(arena).player(player).sendPlayer(); + PluginArenaUtils.preparePlayerForGame(arena, player, arena.getLobbyLocation(), false).thenAccept(act -> { + new MessageBuilder(MessageBuilder.ActionType.JOIN).arena(arena).player(player).sendArena(); + new TitleBuilder("IN_GAME_JOIN_TITLE").asKey().arena(arena).player(player).sendPlayer(); - if(plugin.getConfigPreferences().getOption("KITS")) { - plugin.getUserManager().getUser(player).setKit(plugin.getKitRegistry().getDefaultKit()); - } - plugin.getSpecialItemManager().addSpecialItemsOfStage(player, SpecialItem.DisplayStage.LOBBY); - if(arena.getArenaState() == IArenaState.WAITING_FOR_PLAYERS) { - plugin.getSpecialItemManager().addSpecialItemsOfStage(player, SpecialItem.DisplayStage.WAITING_FOR_PLAYERS); - } else if(ArenaState.isStartingStage(arena)) { - plugin.getSpecialItemManager().addSpecialItemsOfStage(player, SpecialItem.DisplayStage.ENOUGH_PLAYERS_TO_START); - } + if(plugin.getConfigPreferences().getOption("KITS")) { + plugin.getUserManager().getUser(player).setKit(plugin.getKitRegistry().getDefaultKit()); + } + plugin.getSpecialItemManager().addSpecialItemsOfStage(player, SpecialItem.DisplayStage.LOBBY); + if(arena.getArenaState() == IArenaState.WAITING_FOR_PLAYERS) { + plugin.getSpecialItemManager().addSpecialItemsOfStage(player, SpecialItem.DisplayStage.WAITING_FOR_PLAYERS); + } else if(ArenaState.isStartingStage(arena)) { + plugin.getSpecialItemManager().addSpecialItemsOfStage(player, SpecialItem.DisplayStage.ENOUGH_PLAYERS_TO_START); + } - for(Player arenaPlayer : arena.getPlayers()) { - PluginArenaUtils.showPlayer(arenaPlayer, arena); - } + for(Player arenaPlayer : arena.getPlayers()) { + PluginArenaUtils.showPlayer(arenaPlayer, arena); + } - plugin.getSignManager().updateSigns(); - plugin.getDebugger().debug("[{0}] Final join attempt as player for {1} took {2}ms", arena.getId(), player.getName(), System.currentTimeMillis() - start); + plugin.getSignManager().updateSigns(); + plugin.getDebugger().debug("[{0}] Final join attempt as player for {1} took {2}ms", arena.getId(), player.getName(), System.currentTimeMillis() - start); + }); } private boolean joinAsParty(@NotNull Player player, @NotNull IPluginArena arena) { diff --git a/MiniGamesBox Classic/src/main/java/plugily/projects/minigamesbox/classic/arena/PluginArenaUtils.java b/MiniGamesBox Classic/src/main/java/plugily/projects/minigamesbox/classic/arena/PluginArenaUtils.java index 0656b515..c1fb7a03 100644 --- a/MiniGamesBox Classic/src/main/java/plugily/projects/minigamesbox/classic/arena/PluginArenaUtils.java +++ b/MiniGamesBox Classic/src/main/java/plugily/projects/minigamesbox/classic/arena/PluginArenaUtils.java @@ -35,6 +35,8 @@ import plugily.projects.minigamesbox.classic.utils.serialization.InventorySerializer; import plugily.projects.minigamesbox.classic.utils.version.VersionUtils; +import java.util.concurrent.CompletableFuture; + /** * @author Tigerpanzer_02 *

Created at 01.11.2021 @@ -71,52 +73,53 @@ public static void hidePlayersOutsideTheGame(Player player, PluginArena arena) { VersionUtils.hidePlayer(plugin, players, player); } } - - public static void preparePlayerForGame( + + public static CompletableFuture preparePlayerForGame( IPluginArena arena, Player player, Location location, boolean spectator) { - IUser user = plugin.getUserManager().getUser(player); - if(plugin.getConfigPreferences().getOption("INVENTORY_MANAGER")) { - InventorySerializer.saveInventoryToFile(plugin, player); - } - VersionUtils.teleport(player, location); - player.getInventory().clear(); - player - .getActivePotionEffects() - .forEach(potionEffect -> player.removePotionEffect(potionEffect.getType())); - VersionUtils.setMaxHealth(player, VersionUtils.getMaxHealth(player)); - player.setHealth(VersionUtils.getMaxHealth(player)); - player.setFoodLevel(20); - player.setGameMode(GameMode.SURVIVAL); - player - .getInventory() - .setArmorContents( - new ItemStack[]{ - new ItemStack(Material.AIR), - new ItemStack(Material.AIR), - new ItemStack(Material.AIR), - new ItemStack(Material.AIR) - }); - player.setExp(1); - player.setLevel(0); - player.setWalkSpeed(0.2f); - player.setFlySpeed(0.1f); - - if(spectator) { - player.setAllowFlight(true); - player.setFlying(true); - user.setSpectator(true); - player.addPotionEffect(new PotionEffect(PotionEffectType.NIGHT_VISION, Integer.MAX_VALUE, 0)); - plugin - .getSpecialItemManager() - .addSpecialItemsOfStage(player, SpecialItem.DisplayStage.SPECTATOR); - } else { - player.setAllowFlight(false); - player.setFlying(false); - user.setSpectator(false); - } - player.updateInventory(); - arena.getBossbarManager().doBarAction(IPluginArena.IBarAction.ADD, player); - arena.getScoreboardManager().createScoreboard(user); + return VersionUtils.teleport(player, location).thenAccept(bo -> { + IUser user = plugin.getUserManager().getUser(player); + if (plugin.getConfigPreferences().getOption("INVENTORY_MANAGER")) { + InventorySerializer.saveInventoryToFile(plugin, player); + } + player.getInventory().clear(); + player + .getActivePotionEffects() + .forEach(potionEffect -> player.removePotionEffect(potionEffect.getType())); + VersionUtils.setMaxHealth(player, VersionUtils.getMaxHealth(player)); + player.setHealth(VersionUtils.getMaxHealth(player)); + player.setFoodLevel(20); + player.setGameMode(GameMode.SURVIVAL); + player + .getInventory() + .setArmorContents( + new ItemStack[]{ + new ItemStack(Material.AIR), + new ItemStack(Material.AIR), + new ItemStack(Material.AIR), + new ItemStack(Material.AIR) + }); + player.setExp(1); + player.setLevel(0); + player.setWalkSpeed(0.2f); + player.setFlySpeed(0.1f); + + if (spectator) { + player.setAllowFlight(true); + player.setFlying(true); + user.setSpectator(true); + player.addPotionEffect(new PotionEffect(PotionEffectType.NIGHT_VISION, Integer.MAX_VALUE, 0)); + plugin + .getSpecialItemManager() + .addSpecialItemsOfStage(player, SpecialItem.DisplayStage.SPECTATOR); + } else { + player.setAllowFlight(false); + player.setFlying(false); + user.setSpectator(false); + } + player.updateInventory(); + arena.getBossbarManager().doBarAction(IPluginArena.IBarAction.ADD, player); + arena.getScoreboardManager().createScoreboard(user); + }); } public static void resetPlayerAfterGame(IPluginArena arena, Player player) { diff --git a/MiniGamesBox Classic/src/main/java/plugily/projects/minigamesbox/classic/arena/states/PluginStartingState.java b/MiniGamesBox Classic/src/main/java/plugily/projects/minigamesbox/classic/arena/states/PluginStartingState.java index db6d2344..9fd15641 100644 --- a/MiniGamesBox Classic/src/main/java/plugily/projects/minigamesbox/classic/arena/states/PluginStartingState.java +++ b/MiniGamesBox Classic/src/main/java/plugily/projects/minigamesbox/classic/arena/states/PluginStartingState.java @@ -100,7 +100,7 @@ public void handleCall(PluginArena arena) { arena.getBossbarManager().setProgress(1.0); org.bukkit.Location arenaLoc = arena.getStartLocation(); for(Player player : arena.getPlayers()) { - VersionUtils.teleport(player, arenaLoc); + VersionUtils.teleport(player, arenaLoc).thenAccept(bol -> { PluginArenaUtils.hidePlayersOutsideTheGame(player, arena); player.setExp(0); player.setLevel(0); @@ -116,6 +116,7 @@ public void handleCall(PluginArena arena) { plugin.getSpecialItemManager().addSpecialItemsOfStage(player, SpecialItem.DisplayStage.IN_GAME); plugin.getRewardsHandler().performReward(player, arena, plugin.getRewardsHandler().getRewardType("START_GAME")); plugin.getUserManager().addStat(user, plugin.getStatsStorage().getStatisticType("GAMES_PLAYED")); + }); } arenaTimer = plugin.getConfig().getInt("Time-Manager.In-Game", 270); plugin.getDebugger().performance("ArenaUpdate", "Arena {0} current timer set to {1}", arena.getId(), arenaTimer); diff --git a/MiniGamesBox Classic/src/main/java/plugily/projects/minigamesbox/classic/events/spectator/SpectatorEvents.java b/MiniGamesBox Classic/src/main/java/plugily/projects/minigamesbox/classic/events/spectator/SpectatorEvents.java index 5363b760..a63be235 100644 --- a/MiniGamesBox Classic/src/main/java/plugily/projects/minigamesbox/classic/events/spectator/SpectatorEvents.java +++ b/MiniGamesBox Classic/src/main/java/plugily/projects/minigamesbox/classic/events/spectator/SpectatorEvents.java @@ -136,8 +136,7 @@ public void onDamage(EntityDamageEvent event) { return; } if(player.getLocation().getY() < VersionUtils.getWorldMinHeight(player.getWorld())) { - VersionUtils.teleport(player, arena.getStartLocation()); - event.setDamage(0); + VersionUtils.teleport(player, arena.getStartLocation()).thenAccept(bol -> event.setDamage(0)); } event.setCancelled(true); } diff --git a/MiniGamesBox Classic/src/main/java/plugily/projects/minigamesbox/classic/utils/version/VersionUtils.java b/MiniGamesBox Classic/src/main/java/plugily/projects/minigamesbox/classic/utils/version/VersionUtils.java index 203dcedf..b70c84f3 100644 --- a/MiniGamesBox Classic/src/main/java/plugily/projects/minigamesbox/classic/utils/version/VersionUtils.java +++ b/MiniGamesBox Classic/src/main/java/plugily/projects/minigamesbox/classic/utils/version/VersionUtils.java @@ -62,6 +62,7 @@ import java.util.Set; import java.util.UUID; import java.util.concurrent.CancellationException; +import java.util.concurrent.CompletableFuture; import java.util.concurrent.ExecutionException; import java.util.stream.Collectors; import java.util.stream.Stream; @@ -170,20 +171,19 @@ public static SkullMeta setPlayerHead(Player player, SkullMeta meta) { private static JavaPlugin plugin; - public static void teleport(Entity entity, Location location) { + public static CompletableFuture teleport(Entity entity, Location location) { if(isPaper) { // Avoid Future.get() method to be called from the main thread if(Bukkit.isPrimaryThread()) { // Checks if the current thread is not async - PaperLib.teleportAsync(entity, location); - return; + return PaperLib.teleportAsync(entity, location); } if(plugin == null) plugin = JavaPlugin.getPlugin(PluginMain.class); try { - Bukkit.getScheduler().callSyncMethod(plugin, () -> PaperLib.teleportAsync(entity, location)).get(); + return Bukkit.getScheduler().callSyncMethod(plugin, () -> PaperLib.teleportAsync(entity, location)).get(); } catch(InterruptedException | ExecutionException e) { e.printStackTrace(); } catch(CancellationException e) { @@ -192,6 +192,7 @@ public static void teleport(Entity entity, Location location) { } else { entity.teleport(location); } + return new CompletableFuture<>(); } public static void sendParticles(String particleName, Player player, Location location, int count) {