Refactor player status handling and improve flag logic
Rename OnPlayerJoin to OnPlayerOnlineStatus, adding logic for player disconnects. Enhance OnPlayerDeath and Flag classes to handle flag reset and notifications when a flag carrier dies or disconnects.
This commit is contained in:
parent
7070165a94
commit
dbcbb10079
|
|
@ -6,7 +6,7 @@ import com.alttd.ctf.config.GameConfig;
|
|||
import com.alttd.ctf.config.Messages;
|
||||
import com.alttd.ctf.events.InventoryItemInteractionEvent;
|
||||
import com.alttd.ctf.events.OnPlayerDeath;
|
||||
import com.alttd.ctf.events.OnPlayerJoin;
|
||||
import com.alttd.ctf.events.OnPlayerOnlineStatus;
|
||||
import com.alttd.ctf.events.SnowballEvent;
|
||||
import com.alttd.ctf.flag.Flag;
|
||||
import com.alttd.ctf.flag.FlagTryCaptureEvent;
|
||||
|
|
@ -87,9 +87,9 @@ public class Main extends JavaPlugin {
|
|||
PluginManager pluginManager = getServer().getPluginManager();
|
||||
pluginManager.registerEvents(new SnowballEvent(gameManager), this);
|
||||
pluginManager.registerEvents(new FlagTryCaptureEvent(flag), this);
|
||||
pluginManager.registerEvents(new OnPlayerDeath(gameManager, worldBorderApi, this), this);
|
||||
pluginManager.registerEvents(new OnPlayerDeath(gameManager, worldBorderApi, this, flag), this);
|
||||
pluginManager.registerEvents(new InventoryItemInteractionEvent(), this);
|
||||
pluginManager.registerEvents(new OnPlayerJoin(gameManager, flag), this);
|
||||
pluginManager.registerEvents(new OnPlayerOnlineStatus(gameManager, flag), this);
|
||||
pluginManager.registerEvents(new GUIListener(), this);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,8 +1,8 @@
|
|||
package com.alttd.ctf.events;
|
||||
|
||||
import com.alttd.ctf.Main;
|
||||
import com.alttd.ctf.config.Config;
|
||||
import com.alttd.ctf.config.GameConfig;
|
||||
import com.alttd.ctf.flag.Flag;
|
||||
import com.alttd.ctf.game.GameManager;
|
||||
import com.alttd.ctf.game.GamePhase;
|
||||
import com.alttd.ctf.team.TeamPlayer;
|
||||
|
|
@ -24,11 +24,13 @@ public class OnPlayerDeath implements Listener {
|
|||
private final GameManager gameManager;
|
||||
private final WorldBorderApi worldBorderApi;
|
||||
private final Main main;
|
||||
private final Flag flag;
|
||||
|
||||
public OnPlayerDeath(GameManager gameManager, WorldBorderApi worldBorderApi, Main main) {
|
||||
public OnPlayerDeath(GameManager gameManager, WorldBorderApi worldBorderApi, Main main, Flag flag) {
|
||||
this.gameManager = gameManager;
|
||||
this.worldBorderApi = worldBorderApi;
|
||||
this.main = main;
|
||||
this.flag = flag;
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
|
|
@ -41,6 +43,7 @@ public class OnPlayerDeath implements Listener {
|
|||
Player player = event.getPlayer();
|
||||
player.getInventory().clear();
|
||||
player.updateInventory();
|
||||
flag.handleCarrierDeathOrDisconnect(player);
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
|
|
|
|||
|
|
@ -12,17 +12,19 @@ import org.bukkit.entity.Player;
|
|||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.player.PlayerJoinEvent;
|
||||
import org.bukkit.event.player.PlayerQuitEvent;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.Comparator;
|
||||
import java.util.Optional;
|
||||
|
||||
@Slf4j
|
||||
public class OnPlayerJoin implements Listener {
|
||||
public class OnPlayerOnlineStatus implements Listener {
|
||||
|
||||
private final GameManager gameManager;
|
||||
private final Flag flag;
|
||||
|
||||
public OnPlayerJoin(GameManager gameManager, Flag flag) {
|
||||
public OnPlayerOnlineStatus(GameManager gameManager, Flag flag) {
|
||||
this.gameManager = gameManager;
|
||||
this.flag = flag;
|
||||
}
|
||||
|
|
@ -64,4 +66,9 @@ public class OnPlayerJoin implements Listener {
|
|||
player.teleportAsync(teamPlayer.getTeam().getSpawnLocation());
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onPlayerJoin(@NotNull PlayerQuitEvent event) {
|
||||
flag.handleCarrierDeathOrDisconnect(event.getPlayer());
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -84,6 +84,9 @@ public class Flag implements Runnable {
|
|||
)));
|
||||
flagCarrier = player;
|
||||
teamFlagPointCount.clear();
|
||||
winningTeam = null;
|
||||
lastWinningTeamId = -1;
|
||||
bossBar.setProgress(0);
|
||||
}
|
||||
|
||||
public void spawnFlag() {
|
||||
|
|
@ -310,4 +313,21 @@ public class Flag implements Runnable {
|
|||
wins.clear();
|
||||
lastWinningTeamId = -1;
|
||||
}
|
||||
|
||||
public void handleCarrierDeathOrDisconnect(Player player) {
|
||||
if (flagCarrier == null) {
|
||||
return;
|
||||
}
|
||||
if (!flagCarrier.getUniqueId().equals(player.getUniqueId())) {
|
||||
return;
|
||||
}
|
||||
flagCarrier = null;
|
||||
particleTrail.clear();
|
||||
spawnFlag();
|
||||
gameManager.getTeam(player.getUniqueId())
|
||||
.ifPresentOrElse(team -> Bukkit.broadcast(MiniMessage.miniMessage()
|
||||
.deserialize("<red><team>'s flag carrier died! The flag has respawned",
|
||||
Placeholder.component("team", team.getName()))),
|
||||
() -> log.warn("A flag carrier died who was not part of a team"));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user