diff --git a/Plugin/src/main/java/dev/lrxh/neptune/feature/queue/QueueService.java b/Plugin/src/main/java/dev/lrxh/neptune/feature/queue/QueueService.java index a50aaa3d..58592146 100644 --- a/Plugin/src/main/java/dev/lrxh/neptune/feature/queue/QueueService.java +++ b/Plugin/src/main/java/dev/lrxh/neptune/feature/queue/QueueService.java @@ -86,6 +86,11 @@ public QueueEntry remove(UUID playerUUID) { entry.getKit().removeQueue(); } + Profile profile = API.getProfile(playerUUID); + if (profile != null && profile.hasState(ProfileState.IN_QUEUE)) { + profile.setState(ProfileState.IN_LOBBY); + } + return entry; } diff --git a/Plugin/src/main/java/dev/lrxh/neptune/game/duel/DuelRequest.java b/Plugin/src/main/java/dev/lrxh/neptune/game/duel/DuelRequest.java index c34483f9..d1b1771a 100644 --- a/Plugin/src/main/java/dev/lrxh/neptune/game/duel/DuelRequest.java +++ b/Plugin/src/main/java/dev/lrxh/neptune/game/duel/DuelRequest.java @@ -8,6 +8,8 @@ import dev.lrxh.neptune.game.match.MatchService; import dev.lrxh.neptune.game.match.impl.participant.Participant; import dev.lrxh.neptune.game.match.impl.team.MatchTeam; +import dev.lrxh.neptune.feature.queue.QueueService; +import dev.lrxh.neptune.profile.data.ProfileState; import dev.lrxh.neptune.profile.impl.Profile; import dev.lrxh.neptune.providers.request.Request; import dev.lrxh.neptune.utils.CC; @@ -48,16 +50,31 @@ public void start(UUID receiver) { public void normalDuel(UUID receiver) { Player sender = Bukkit.getPlayer(getSender()); - Player reciverPlayer = Bukkit.getPlayer(receiver); + Player receiverPlayer = Bukkit.getPlayer(receiver); - if (reciverPlayer == null || sender == null) { + if (receiverPlayer == null || sender == null) { if (arena != null) arena.remove(); return; } + Profile senderProfile = API.getProfile(sender.getUniqueId()); + Profile receiverProfile = API.getProfile(receiver); + if (senderProfile == null || receiverProfile == null + || senderProfile.getMatch() != null || receiverProfile.getMatch() != null + || senderProfile.hasState(ProfileState.IN_GAME, ProfileState.IN_SPECTATOR, ProfileState.IN_KIT_EDITOR) + || receiverProfile.hasState(ProfileState.IN_GAME, ProfileState.IN_SPECTATOR, ProfileState.IN_KIT_EDITOR)) { + if (arena != null) arena.remove(); + MessagesLocale.DUEL_REQUEST_COULDNT_BE_ACCEPTED.send(sender.getUniqueId()); + MessagesLocale.DUEL_REQUEST_COULDNT_BE_ACCEPTED.send(receiverPlayer.getUniqueId()); + return; + } + + QueueService.get().remove(sender.getUniqueId()); + QueueService.get().remove(receiver); + Participant participant1 = new Participant(sender); - Participant participant2 = new Participant(reciverPlayer); + Participant participant2 = new Participant(receiverPlayer); MatchService.get().startMatch(participant1, participant2, kit, arena, true, rounds); diff --git a/Plugin/src/main/java/dev/lrxh/neptune/game/duel/command/DuelCommand.java b/Plugin/src/main/java/dev/lrxh/neptune/game/duel/command/DuelCommand.java index 5bee17fe..a5405985 100644 --- a/Plugin/src/main/java/dev/lrxh/neptune/game/duel/command/DuelCommand.java +++ b/Plugin/src/main/java/dev/lrxh/neptune/game/duel/command/DuelCommand.java @@ -10,6 +10,7 @@ import dev.lrxh.neptune.profile.data.GameData; import dev.lrxh.neptune.profile.data.ProfileState; import dev.lrxh.neptune.profile.impl.Profile; +import dev.lrxh.neptune.providers.request.Request; import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder; import org.bukkit.Bukkit; @@ -74,8 +75,8 @@ public void acceptUUID(@Sender Player player, UUID uuid) { Profile profile = API.getProfile(player); GameData playerGameData = profile.getGameData(); - if (profile.getMatch() != null || profile.getState().equals(ProfileState.IN_SPECTATOR) - || profile.hasState(ProfileState.IN_KIT_EDITOR) || profile.hasState(ProfileState.IN_QUEUE)) { + if (profile.getMatch() != null + || profile.hasState(ProfileState.IN_SPECTATOR, ProfileState.IN_KIT_EDITOR)) { MessagesLocale.YOU_CANT_SEND_DUEL.send(player.getUniqueId()); return; } @@ -87,16 +88,21 @@ public void acceptUUID(@Sender Player player, UUID uuid) { } Profile targetProfile = API.getProfile(target); - DuelRequest duelRequest = (DuelRequest) playerGameData.getRequests().get(uuid); - - if (duelRequest == null) { + Request request = playerGameData.getRequests().get(uuid); + if (!(request instanceof DuelRequest duelRequest)) { MessagesLocale.YOU_DONT_HAVE_DUEL_REQUEST.send(player.getUniqueId()); return; } if (!duelRequest.isParty() && targetProfile.getState().equals(ProfileState.IN_PARTY) - || duelRequest.isParty() && !profile.getState().equals(ProfileState.IN_PARTY) - || duelRequest.isParty() && !targetProfile.getState().equals(ProfileState.IN_PARTY)) { + || (duelRequest.isParty() && !profile.getState().equals(ProfileState.IN_PARTY)) + || (duelRequest.isParty() && !targetProfile.getState().equals(ProfileState.IN_PARTY))) { + MessagesLocale.DUEL_REQUEST_COULDNT_BE_ACCEPTED.send(player.getUniqueId()); + return; + } + + if (targetProfile.getMatch() != null + || targetProfile.hasState(ProfileState.IN_SPECTATOR, ProfileState.IN_KIT_EDITOR, ProfileState.IN_GAME)) { MessagesLocale.DUEL_REQUEST_COULDNT_BE_ACCEPTED.send(player.getUniqueId()); return; } @@ -128,14 +134,18 @@ public void denyUUID(@Sender Player player, UUID uuid) { Profile profile = API.getProfile(player); GameData playerGameData = profile.getGameData(); - DuelRequest duelRequest = (DuelRequest) playerGameData.getRequests().get(uuid); - if (duelRequest == null) { + Request denyRequest = playerGameData.getRequests().get(uuid); + if (!(denyRequest instanceof DuelRequest duelRequest)) { MessagesLocale.YOU_DONT_HAVE_DUEL_REQUEST.send(player.getUniqueId()); return; } Player sender = Bukkit.getPlayer(uuid); - if (sender == null) return; + if (sender == null) { + duelRequest.getArena().remove(); + playerGameData.removeRequest(uuid); + return; + } MessagesLocale.DUEL_DENY_SENDER.send(player.getUniqueId(), Placeholder.unparsed("player", sender.getName())); MessagesLocale.DUEL_DENY_RECEIVER.send(uuid, Placeholder.unparsed("player", player.getName())); diff --git a/Plugin/src/main/java/dev/lrxh/neptune/game/match/Match.java b/Plugin/src/main/java/dev/lrxh/neptune/game/match/Match.java index 4c8877f8..984e2d01 100644 --- a/Plugin/src/main/java/dev/lrxh/neptune/game/match/Match.java +++ b/Plugin/src/main/java/dev/lrxh/neptune/game/match/Match.java @@ -37,6 +37,7 @@ import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver; import org.bukkit.*; import org.bukkit.attribute.Attribute; +import org.bukkit.attribute.AttributeInstance; import org.bukkit.block.data.BlockData; import org.bukkit.entity.Entity; import org.bukkit.entity.Item; @@ -48,6 +49,7 @@ import java.util.*; import java.util.function.Consumer; +import java.util.logging.Level; @AllArgsConstructor @Getter @@ -336,15 +338,21 @@ public void setupPlayer(UUID playerUUID) { if (player == null) return; Profile profile = API.getProfile(playerUUID); + Participant participant = getParticipant(playerUUID); + if (profile == null || participant == null) + return; profile.setMatch(this); profile.setState(ProfileState.IN_GAME); PlayerUtil.reset(player); player.setCollidable(true); - Participant participant = getParticipant(playerUUID); participant.setLastAttacker(null); kit.giveLoadout(participant); - player.getAttribute(Attribute.MAX_HEALTH).setBaseValue(kit.getHealth()); - player.setHealth(kit.getHealth()); + AttributeInstance maxHealthAttribute = player.getAttribute(Attribute.MAX_HEALTH); + if (maxHealthAttribute != null) { + maxHealthAttribute.setBaseValue(kit.getHealth()); + } + double maxHealth = maxHealthAttribute != null ? maxHealthAttribute.getValue() : kit.getHealth(); + player.setHealth(Math.min(kit.getHealth(), maxHealth)); player.sendHealthUpdate(); } @@ -362,14 +370,12 @@ public void broadcast(String message) { } public void checkRules() { + boolean freeze = state != null && state.equals(MatchState.STARTING) && kit.is(KitRule.DENY_MOVEMENT) + && !(this instanceof FfaFightMatch); forEachParticipant(participant -> { - if (!(this instanceof FfaFightMatch)) { - if (kit.is(KitRule.DENY_MOVEMENT)) { - participant.toggleFreeze(); - } - } + participant.setFrozen(freeze); if (kit.is(KitRule.SHOW_HP)) { - if (state.equals(MatchState.STARTING)) { + if (MatchState.STARTING.equals(state)) { showHealth(); } } @@ -464,7 +470,23 @@ public void removeEntities() { } public void setupParticipants() { - forEachPlayer(player -> setupPlayer(player.getUniqueId())); + for (Participant participant : new ArrayList<>(participants)) { + try { + Player player = participant.getPlayer(); + if (player == null) continue; + setupPlayer(player.getUniqueId()); + } catch (Exception e) { + Neptune.get().getLogger().log(Level.WARNING, "Failed to setup participant " + + participant.getPlayerUUID() + " for match " + uuid, e); + try { + Profile profile = API.getProfile(participant.getPlayerUUID()); + if (profile != null && profile.getMatch() == this) { + profile.setMatch(null); + } + } catch (Exception ignored) { + } + } + } forEachParticipant(par -> { par.reset(); showParticipant(par); diff --git a/Plugin/src/main/java/dev/lrxh/neptune/game/match/MatchService.java b/Plugin/src/main/java/dev/lrxh/neptune/game/match/MatchService.java index cad3cfdf..8b6d8963 100644 --- a/Plugin/src/main/java/dev/lrxh/neptune/game/match/MatchService.java +++ b/Plugin/src/main/java/dev/lrxh/neptune/game/match/MatchService.java @@ -147,21 +147,41 @@ public void startMatch(IMatch match, Player redPlayer, Player bluePlayer) { ? source.acquire() : ArenaService.get().copyFrom(match.getArena()).acquire(); - arenaFuture.thenAccept(arena -> { - if (arena == null) return; - Match neptuneMatch = new SoloFightMatch( - arena, - KitService.get().copyFrom(match.getKit()), - true, - new ArrayList<>(), - new Participant(redPlayer), - new Participant(bluePlayer), - 1 - ); - - matches.add(neptuneMatch); - new MatchStartRunnable(neptuneMatch).start(0L, 20L); - }); + arenaFuture.thenAccept(arena -> startApiMatch(match, redPlayer, bluePlayer, arena)); + } + + private void startApiMatch(IMatch match, Player redPlayer, Player bluePlayer, IArena arena) { + if (arena == null) return; + if (redPlayer == null || bluePlayer == null || !redPlayer.isOnline() || !bluePlayer.isOnline()) { + arena.remove(); + return; + } + if (match.getKit() == null) { + arena.remove(); + return; + } + Kit kit = KitService.get().copyFrom(match.getKit()); + kit.addPlaying(2); + Participant participantRed = new Participant(redPlayer); + Participant participantBlue = new Participant(bluePlayer); + participantRed.setOpponent(participantBlue); + participantRed.setColor(ParticipantColor.RED); + participantBlue.setOpponent(participantRed); + participantBlue.setColor(ParticipantColor.BLUE); + Match neptuneMatch = new SoloFightMatch( + arena, + kit, + match.isDuel(), + new ArrayList<>(List.of(participantRed, participantBlue)), + participantRed, + participantBlue, + Math.max(1, match.getRounds()) + ); + participantRed.setMatch(neptuneMatch); + participantBlue.setMatch(neptuneMatch); + + matches.add(neptuneMatch); + new MatchStartRunnable(neptuneMatch).start(0L, 20L); } @Override diff --git a/Plugin/src/main/java/dev/lrxh/neptune/profile/impl/Profile.java b/Plugin/src/main/java/dev/lrxh/neptune/profile/impl/Profile.java index 02fd5835..d694fd98 100644 --- a/Plugin/src/main/java/dev/lrxh/neptune/profile/impl/Profile.java +++ b/Plugin/src/main/java/dev/lrxh/neptune/profile/impl/Profile.java @@ -25,6 +25,7 @@ import dev.lrxh.neptune.game.match.Match; import dev.lrxh.neptune.profile.data.*; import dev.lrxh.neptune.providers.database.DatabaseService; +import dev.lrxh.neptune.providers.request.Request; import dev.lrxh.neptune.providers.database.impl.DataDocument; import dev.lrxh.neptune.utils.Cooldown; import dev.lrxh.neptune.utils.ItemUtils; @@ -407,7 +408,9 @@ public void disband() { } public void acceptDuel(UUID senderUUID) { - ((DuelRequest) gameData.getRequests().get(senderUUID)).start(playerUUID); + Request request = gameData.getRequests().get(senderUUID); + if (!(request instanceof DuelRequest duelRequest)) return; + duelRequest.start(playerUUID); gameData.removeRequest(senderUUID); }