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

Spectator break fix #119

Merged
merged 4 commits into from
Nov 29, 2021
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -485,4 +485,6 @@ default boolean isRespawning(Player p) {
ITeamAssigner getTeamAssigner();

void setTeamAssigner(ITeamAssigner teamAssigner);

List<Player> getLeavingPlayers();
}
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,8 @@ public class Arena implements IArena {
private List<Region> regionsList = new ArrayList<>();
private int renderDistance;

private final List<Player> leaving = new ArrayList<>();

/**
* Current event, used at scoreboard
*/
Expand Down Expand Up @@ -454,6 +456,8 @@ public boolean addPlayer(Player p, boolean skipOwnerCheck) {
}
}

leaving.remove(p);

if (status == GameState.waiting || (status == GameState.starting && (startingTask != null && startingTask.getCountdown() > 1))) {
if (players.size() >= maxPlayers && !isVip(p)) {
TextComponent text = new TextComponent(getMsg(p, Messages.COMMAND_JOIN_DENIED_IS_FULL));
Expand Down Expand Up @@ -643,14 +647,16 @@ public boolean addSpectator(@NotNull Player p, boolean playerBefore, Location st
p.setGameMode(GameMode.ADVENTURE);

Bukkit.getScheduler().runTaskLater(plugin, () -> {
if(leaving.contains(p)) return;
p.setAllowFlight(true);
p.setFlying(true);
}, 5L);

if (p.getPassenger() != null && p.getPassenger().getType() == EntityType.ARMOR_STAND)
p.getPassenger().remove();

Bukkit.getScheduler().runTaskLater(plugin, () -> {
Bukkit.getScheduler().runTask(plugin, () -> {
if(leaving.contains(p)) return;
for (Player on : Bukkit.getOnlinePlayers()) {
if (on == p) continue;
if (getSpectators().contains(on)) {
Expand Down Expand Up @@ -685,7 +691,9 @@ public boolean addSpectator(@NotNull Player p, boolean playerBefore, Location st
p.addPotionEffect(new PotionEffect(PotionEffectType.INVISIBILITY, Integer.MAX_VALUE, 1, false));

p.getInventory().setArmorContents(null);
}, 25L);
});

leaving.remove(p);

p.sendMessage(getMsg(p, Messages.COMMAND_JOIN_SPECTATOR_MSG).replace("{arena}", this.getDisplayName()));

Expand Down Expand Up @@ -723,6 +731,11 @@ public boolean addSpectator(@NotNull Player p, boolean playerBefore, Location st
* @param disconnect True if the player was disconnected
*/
public void removePlayer(@NotNull Player p, boolean disconnect) {
if(leaving.contains(p)) {
return;
} else {
leaving.add(p);
}
debug("Player removed: " + p.getName() + " arena: " + getArenaName());
respawnSessions.remove(p);

Expand Down Expand Up @@ -976,6 +989,13 @@ public void removePlayer(@NotNull Player p, boolean disconnect) {
*/
public void removeSpectator(@NotNull Player p, boolean disconnect) {
debug("Spectator removed: " + p.getName() + " arena: " + getArenaName());

if(leaving.contains(p)) {
return;
} else {
leaving.add(p);
}

Bukkit.getPluginManager().callEvent(new PlayerLeaveArenaEvent(p, this, null));
spectators.remove(p);
removeArenaByPlayer(p, this);
Expand Down Expand Up @@ -1017,7 +1037,7 @@ public void removeSpectator(@NotNull Player p, boolean disconnect) {
}
playerLocation.remove(p);

Bukkit.getScheduler().runTaskLater(plugin, () -> {
Bukkit.getScheduler().runTask(plugin, () -> {
for (Player on : Bukkit.getOnlinePlayers()) {
if (on.equals(p)) continue;
if (getArenaByPlayer(on) == null) {
Expand All @@ -1029,7 +1049,7 @@ public void removeSpectator(@NotNull Player p, boolean disconnect) {
}
}
if (!disconnect) BedWarsScoreboard.giveScoreboard(p, null, true);
}, 10L);
});

/* Remove also the party */
if (getParty().hasParty(p)) {
Expand Down Expand Up @@ -2360,6 +2380,7 @@ public void destroyData() {
oreGenerators = null;
perMinuteTask = null;
moneyperMinuteTask = null;
leaving.clear();
}

/**
Expand Down Expand Up @@ -2562,6 +2583,11 @@ public void setTeamAssigner(ITeamAssigner teamAssigner) {
}
}

@Override
public List<Player> getLeavingPlayers() {
return leaving;
}

/**
* Remove player from world.
* Contains fall-backs.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,8 @@ public void onWorldChange(PlayerChangedWorldEvent e) {
//}, 2L);
}
}
if (Arena.isInArena(e.getPlayer())) {
IArena a = Arena.getArenaByPlayer(e.getPlayer());
IArena a = Arena.getArenaByPlayer(e.getPlayer());
if (a != null) {
if (a.isPlayer(e.getPlayer())) {
if (a.getStatus() == GameState.waiting || a.getStatus() == GameState.starting) return;
if (!e.getPlayer().getWorld().getName().equalsIgnoreCase(a.getWorld().getName())) {
Expand Down