Skip to content

Commit

Permalink
Fix for multiverse teleportation problems on a multiworld server (als…
Browse files Browse the repository at this point in the history
…o see thread in Discord server) (#53)
  • Loading branch information
Waterman1001 authored Jun 26, 2024
1 parent 21e5bf5 commit ca0a187
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 81 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
* <p>Created at 01.11.2021
Expand Down Expand Up @@ -71,52 +73,53 @@ public static void hidePlayersOutsideTheGame(Player player, PluginArena arena) {
VersionUtils.hidePlayer(plugin, players, player);
}
}

public static void preparePlayerForGame(
public static CompletableFuture<Void> 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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<Boolean> 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) {
Expand All @@ -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) {
Expand Down

0 comments on commit ca0a187

Please sign in to comment.