Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed: Bug where invalid arena name on spectator command throws an error. #90

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions src/main/java/tk/shanebee/hg/commands/SpectateCmd.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,15 @@ public boolean run() {
Util.scm(player, lang.cmd_join_in_game);
} else {
Game game = gameManager.getGame(args[1]);
GamePlayerData gamePlayerData = game.getGamePlayerData();
if (game != null && !gamePlayerData.getPlayers().contains(player.getUniqueId()) && !gamePlayerData.getSpectators().contains(player.getUniqueId())) {
Status status = game.getGameArenaData().getStatus();
if (status == Status.RUNNING || status == Status.BEGINNING) {
gamePlayerData.spectate(player);
} else {
Util.scm(player, "This game is not running, status: " + status);
if (game != null) {
GamePlayerData gamePlayerData = game.getGamePlayerData();
if (!gamePlayerData.getPlayers().contains(player.getUniqueId()) && !gamePlayerData.getSpectators().contains(player.getUniqueId())) {
Status status = game.getGameArenaData().getStatus();
if (status == Status.RUNNING || status == Status.BEGINNING) {
gamePlayerData.spectate(player);
} else {
Util.scm(player, "This game is not running, status: " + status);
}
}
} else {
Util.scm(player, lang.cmd_delete_noexist);
Expand Down
21 changes: 19 additions & 2 deletions src/main/java/tk/shanebee/hg/game/GamePlayerData.java
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,9 @@ void heal(Player player) {
* @param player Player to freeze
*/
public void freeze(Player player) {
player.setGameMode(GameMode.SURVIVAL);
// Change to Adventure mode instead of Survival, to prevent moving by breaking blocks which get cancelled breaking before the game starts.
// In order to make the gamemode switch work with Multiverse as well, only change it 2 ticks later.
Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(plugin, () -> player.setGameMode(GameMode.ADVENTURE), 2l);
player.addPotionEffect(new PotionEffect(PotionEffectType.JUMP, 23423525, -10, false, false));
player.setWalkSpeed(0.0001F);
player.setFoodLevel(1);
Expand All @@ -153,6 +155,7 @@ public void freeze(Player player) {
* @param player Player to unfreeze
*/
public void unFreeze(Player player) {
player.setGameMode(GameMode.SURVIVAL); //Change back to Survival Mode.
player.removePotionEffect(PotionEffectType.JUMP);
player.setWalkSpeed(0.2F);
}
Expand Down Expand Up @@ -305,6 +308,16 @@ public void join(Player player, boolean command) {
freeze(player);
kills.put(player, 0);

// Sometimes, when joining from a different world or unloaded chunks using e.g. Multiverse plugins,
// after the first teleportation you are still able to make 1 last jump at the spawnpoint when joining via
// a Sign, from the velocity you have at the moment just before joining. This can happen because of server lag for example.
// You are then able to get closer to the middle while the game did not yet start and you are not placed
// properly on the spawn point. Therefore, I included an extra teleport to make sure everyone for sure is at the beginning before game start.
// I could not think of a smarter way to resolve this issue and I really needed it to be able to player HungerGames
// without any problems. There must however be smarter ways to resolve this issue. (Resetting player velocity did not work :c.)
// I might as well not fully understand the issue, I only know it occurs and how to reproduce it.
PaperLib.teleportAsync(player, loc);

if (players.size() == 1 && status == Status.READY)
gameArenaData.setStatus(Status.WAITING);
if (players.size() >= game.gameArenaData.minPlayers && (status == Status.WAITING || status == Status.READY)) {
Expand Down Expand Up @@ -394,7 +407,11 @@ void exit(Player player, @Nullable Location exitLocation) {
*/
public void spectate(Player spectator) {
UUID uuid = spectator.getUniqueId();
spectator.teleport(game.gameArenaData.getSpawns().get(0));
Location spectatorSpawn = game.gameArenaData.getSpawns().get(0);
//Make players join as flying in spectator mode, such that they directly see they can fly.
Location newSpectatorSpawn = spectatorSpawn.clone().add(0, 5, 0);
spectator.teleport(newSpectatorSpawn);
Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(plugin, () -> spectator.setFlying(true), 5l);
if (playerManager.hasPlayerData(uuid)) {
playerManager.transferPlayerDataToSpectator(uuid);
} else {
Expand Down