From 219c07cf1df691fa08f4f8b3edf6484fd1dcf7e2 Mon Sep 17 00:00:00 2001 From: devlrxxh Date: Fri, 5 Apr 2024 12:38:06 +0200 Subject: [PATCH] Bug Fixes --- .../java/me/lrxh/practice/match/Match.java | 137 +++++++---------- .../me/lrxh/practice/match/MatchListener.java | 9 +- .../practice/profile/ProfileListener.java | 10 +- .../me/lrxh/practice/util/InventoryUtil.java | 2 +- .../me/lrxh/practice/util/MathHelper.java | 15 -- .../lrxh/practice/util/PlaceholderUtil.java | 6 +- .../me/lrxh/practice/util/PlayerUtil.java | 8 +- .../java/me/lrxh/practice/util/TimeUtil.java | 7 +- .../util/assemble/AssembleThread.java | 140 +++++++++--------- 9 files changed, 154 insertions(+), 180 deletions(-) delete mode 100644 src/main/java/me/lrxh/practice/util/MathHelper.java diff --git a/src/main/java/me/lrxh/practice/match/Match.java b/src/main/java/me/lrxh/practice/match/Match.java index 5c0e6c8..afc83f0 100644 --- a/src/main/java/me/lrxh/practice/match/Match.java +++ b/src/main/java/me/lrxh/practice/match/Match.java @@ -30,7 +30,6 @@ import org.bukkit.potion.PotionEffect; import org.bukkit.potion.PotionEffectType; import org.bukkit.scheduler.BukkitRunnable; -import org.bukkit.util.Vector; import java.util.ArrayList; import java.util.Collections; @@ -186,13 +185,15 @@ public void setupPlayer(Player player) { public Player getOpponent(UUID playerUUID) { GameParticipant playerParticipant = getParticipant(Bukkit.getPlayer(playerUUID)); - if (playerParticipant != null) { - for (GameParticipant gameParticipant : getParticipants()) { - if (!gameParticipant.equals(playerParticipant)) { - for (MatchGamePlayer gamePlayer : gameParticipant.getPlayers()) { - if (!gamePlayer.isDisconnected()) { - return gamePlayer.getPlayer(); - } + if (playerParticipant == null) { + return null; + } + + for (GameParticipant gameParticipant : getParticipants()) { + if (!gameParticipant.equals(playerParticipant)) { + for (MatchGamePlayer gamePlayer : gameParticipant.getPlayers()) { + if (!gamePlayer.isDisconnected()) { + return gamePlayer.getPlayer(); } } } @@ -250,19 +251,19 @@ public void end() { for (GameParticipant gameParticipant : getParticipants()) { for (MatchGamePlayer gamePlayer : gameParticipant.getPlayers()) { Player player = gamePlayer.getPlayer(); - if (player != null) { - player.setFireTicks(0); - player.updateInventory(); - - Profile profile = Profile.getByUuid(player.getUniqueId()); - profile.setState(ProfileState.LOBBY); - profile.setEnderpearlCooldown(new Cooldown(0)); - PlayerUtil.allowMovement(gamePlayer.getPlayer()); - VisibilityLogic.handle(player); - Practice.getInstance().getHotbar().giveHotbarItems(player); - PlayerUtil.teleportToSpawn(player); - PlayerUtil.allowMovement(gamePlayer.getPlayer()); - } + if (player != null) { + player.setFireTicks(0); + player.updateInventory(); + + Profile profile = Profile.getByUuid(player.getUniqueId()); + profile.setState(ProfileState.LOBBY); + profile.setEnderpearlCooldown(new Cooldown(0)); + PlayerUtil.allowMovement(gamePlayer.getPlayer()); + VisibilityLogic.handle(player); + Practice.getInstance().getHotbar().giveHotbarItems(player); + PlayerUtil.teleportToSpawn(player); + PlayerUtil.allowMovement(gamePlayer.getPlayer()); + } } } @@ -276,7 +277,6 @@ public void end() { arena.setActive(false); } - Practice.getInstance().getCache().getMatches().remove(this); } @@ -353,6 +353,14 @@ public void onRoundEnd() { removeSpectator(player); } + for (GameParticipant gameParticipant : getParticipants()) { + for (MatchGamePlayer gamePlayer : gameParticipant.getPlayers()) { + if (!gamePlayer.isDisconnected()) { + Profile profile = Profile.getByUuid(gamePlayer.getPlayer().getUniqueId()); + profile.setMatch(null); + } + } + } } public void respawn(UUID playerUUID) { @@ -478,7 +486,6 @@ public void onDeath(Player dead) { if (!(state == MatchState.STARTING_ROUND || state == MatchState.PLAYING_ROUND)) { return; } - dead.getInventory().setContents(new ItemStack[36]); PlayerUtil.animateDeath(dead); MatchGamePlayer deadGamePlayer = getGamePlayer(dead); @@ -520,6 +527,7 @@ public void onDeath(Player dead) { Bukkit.getScheduler().runTaskLater(Practice.getInstance(), firework::detonate, 5L); break; } + PlayerUtil.setLastAttacker(killer, null); killer.playSound(killer.getLocation(), Sound.EXPLODE, 1.0f, 1.0f); } @@ -536,11 +544,11 @@ public void onDeath(Player dead) { // Add snapshot to list snapshots.add(snapshot); - // Reset inventory - PlayerUtil.reset(dead); PlayerUtil.doVelocityChange(dead); + PlayerUtil.setLastAttacker(dead, null); + // Handle visibility for match players // Send death message for (GameParticipant gameParticipant : getParticipants()) { @@ -584,12 +592,10 @@ public void onDeath(Player dead) { logicTask.setNextAction(4); } } else { - dead.setAllowFlight(true); - dead.setFlying(true); - Practice.getInstance().getHotbar().giveHotbarItems(dead); } - + // Reset inventory + PlayerUtil.reset(dead); } public abstract boolean isOnSameTeam(Player first, Player second); @@ -757,71 +763,40 @@ protected List getSpectatorsAsPlayers() { public void sendDeathMessage(Player dead, Player killer, boolean finalKill) { String deathMessage; - if (finalKill) { - for (GameParticipant gameParticipant : getParticipants()) { - for (MatchGamePlayer gamePlayer : gameParticipant.getPlayers()) { - Player player = gamePlayer.getPlayer(); - if (killer == null) { - deathMessage = Locale.MATCH_PLAYER_DIED_FINAL.format(player, - getRelationColor(player, dead) + dead.getName() - ); - } else { - deathMessage = Locale.MATCH_PLAYER_FINAL_KILL.format(player, - getRelationColor(player, dead) + dead.getName(), - getRelationColor(player, killer) + killer.getName() - ); - } - player.sendMessage(deathMessage); - } - } + Locale deathLocale; - for (Player player : getSpectatorsAsPlayers()) { - if (killer == null) { - deathMessage = Locale.MATCH_PLAYER_DIED_FINAL.format(player, - getRelationColor(player, dead) + dead.getName() - ); - } else { - deathMessage = Locale.MATCH_PLAYER_FINAL_KILL.format(player, - getRelationColor(player, dead) + dead.getName(), - getRelationColor(player, killer) + killer.getName() - ); - } - player.sendMessage(deathMessage); - } - return; + if (finalKill) { + deathLocale = (killer == null) ? Locale.MATCH_PLAYER_DIED_FINAL : Locale.MATCH_PLAYER_FINAL_KILL; + } else { + deathLocale = (killer == null) ? Locale.MATCH_PLAYER_DIED : Locale.MATCH_PLAYER_KILLED; } for (GameParticipant gameParticipant : getParticipants()) { for (MatchGamePlayer gamePlayer : gameParticipant.getPlayers()) { Player player = gamePlayer.getPlayer(); - if (killer == null) { - deathMessage = Locale.MATCH_PLAYER_DIED.format(player, - getRelationColor(player, dead) + dead.getName() - ); - } else { - deathMessage = Locale.MATCH_PLAYER_KILLED.format(player, - getRelationColor(player, dead) + dead.getName(), - getRelationColor(player, killer) + killer.getName() - ); - } + deathMessage = formatDeathMessage(deathLocale, player, dead, killer); player.sendMessage(deathMessage); } } for (Player player : getSpectatorsAsPlayers()) { - if (killer == null) { - deathMessage = Locale.MATCH_PLAYER_DIED.format(player, - getRelationColor(player, dead) + dead.getName() - ); - } else { - deathMessage = Locale.MATCH_PLAYER_KILLED.format(player, - getRelationColor(player, dead) + dead.getName(), - getRelationColor(player, killer) + killer.getName() - ); - } + deathMessage = formatDeathMessage(deathLocale, player, dead, killer); player.sendMessage(deathMessage); } - } + private String formatDeathMessage(Locale deathLocale, Player player, Player dead, Player killer) { + String deathMessage; + if (killer == null) { + deathMessage = deathLocale.format(player, + getRelationColor(player, dead) + dead.getName() + ); + } else { + deathMessage = deathLocale.format(player, + getRelationColor(player, dead) + dead.getName(), + getRelationColor(player, killer) + killer.getName() + ); + } + return deathMessage; + } } diff --git a/src/main/java/me/lrxh/practice/match/MatchListener.java b/src/main/java/me/lrxh/practice/match/MatchListener.java index 8eea5ca..e70122a 100644 --- a/src/main/java/me/lrxh/practice/match/MatchListener.java +++ b/src/main/java/me/lrxh/practice/match/MatchListener.java @@ -60,7 +60,7 @@ public void onPlayerMoveEvent(PlayerMoveEvent event) { if (!(player.getLocation().getY() >= match.getArena().getDeathZone()) && !match.getGamePlayer(player).isRespawned()) { if (!bedGone) { - if(PlayerUtil.getLastAttacker(player) != null){ + if (PlayerUtil.getLastAttacker(player) != null) { player.playSound(player.getLocation(), Sound.NOTE_PLING, 1.0f, 1.0f); } @@ -335,6 +335,9 @@ public void onPlayerPickUpEvent(PlayerPickupItemEvent event) { if (profile.getMatch() != null && profile.getMatch().getState().equals(MatchState.ENDING_MATCH)) { event.setCancelled(true); } + if (event.getPlayer().isOp() || event.getPlayer().getGameMode() == GameMode.CREATIVE) { + event.setCancelled(false); + } } @EventHandler @@ -358,7 +361,7 @@ public void onPlayerDeathEvent(PlayerDeathEvent event) { event.getDrops().clear(); if (!bedGone) { - if(PlayerUtil.getLastAttacker(player) != null){ + if (PlayerUtil.getLastAttacker(player) != null) { player.playSound(player.getLocation(), Sound.NOTE_PLING, 1.0f, 1.0f); } @@ -739,7 +742,7 @@ public void onPlayerInteractEvent(PlayerInteractEvent event) { } if (kitLoadout != null) { - player.sendMessage(Locale.MATCH_GIVE_KIT.format(player, kitLoadout.getCustomName(), kitName)); + player.sendMessage(Locale.MATCH_GIVE_KIT.format(player, kitLoadout.getCustomName(), match.getKit().getName())); profile.getMatch().getGamePlayer(player).setKitLoadout(kitLoadout); GameParticipant participantA = match.getParticipantA(); player.getInventory().setArmorContents(InventoryUtil.color(kitLoadout.getArmor(), participantA.containsPlayer(player.getUniqueId()) ? Color.RED : Color.BLUE).toArray(new ItemStack[0])); diff --git a/src/main/java/me/lrxh/practice/profile/ProfileListener.java b/src/main/java/me/lrxh/practice/profile/ProfileListener.java index 1c34fe7..bbedd37 100644 --- a/src/main/java/me/lrxh/practice/profile/ProfileListener.java +++ b/src/main/java/me/lrxh/practice/profile/ProfileListener.java @@ -2,6 +2,7 @@ import me.lrxh.practice.Locale; import me.lrxh.practice.Practice; +import me.lrxh.practice.match.MatchState; import me.lrxh.practice.profile.hotbar.HotbarItem; import me.lrxh.practice.profile.meta.option.button.AllowSpectatorsOptionButton; import me.lrxh.practice.profile.meta.option.button.DuelRequestsOptionButton; @@ -190,10 +191,13 @@ public void onPlayerQuitEvent(PlayerQuitEvent event) { } if (profile.getMatch() != null) { - profile.getMatch().sendDeathMessage(event.getPlayer(), null, false); - profile.getMatch().end(); - profile.setMatch(null); + if(profile.getMatch().getState().equals(MatchState.PLAYING_ROUND) + || profile.getMatch().getState().equals(MatchState.ENDING_MATCH) + || profile.getMatch().getState().equals(MatchState.STARTING_ROUND)){ + profile.getMatch().sendDeathMessage(event.getPlayer(), null, false); + } + profile.getMatch().end(); } if (profile.getState().equals(ProfileState.QUEUEING)) { profile.getQueueProfile().getQueue().removeQueue(); diff --git a/src/main/java/me/lrxh/practice/util/InventoryUtil.java b/src/main/java/me/lrxh/practice/util/InventoryUtil.java index 5757d6c..97a1c34 100644 --- a/src/main/java/me/lrxh/practice/util/InventoryUtil.java +++ b/src/main/java/me/lrxh/practice/util/InventoryUtil.java @@ -251,7 +251,7 @@ public static void removeCrafting(Material material) { public static List color(ItemStack[] itemStackList, Color color) { List items = new ArrayList<>(); for (ItemStack itemStack : itemStackList) { - if(itemStack.getType()==null){ + if (itemStack == null || itemStack.getType() == null) { continue; } if (itemStack.getType() == Material.LEATHER_BOOTS || itemStack.getType() == Material.LEATHER_CHESTPLATE || itemStack.getType() == Material.LEATHER_HELMET diff --git a/src/main/java/me/lrxh/practice/util/MathHelper.java b/src/main/java/me/lrxh/practice/util/MathHelper.java deleted file mode 100644 index 620043e..0000000 --- a/src/main/java/me/lrxh/practice/util/MathHelper.java +++ /dev/null @@ -1,15 +0,0 @@ -package me.lrxh.practice.util; - -public class MathHelper { - - public static int floor(double x) { - int y = (int) x; - return x < (double) y ? y - 1 : y; - } - - public static int floor(float x) { - int y = (int) x; - return x < (float) y ? y - 1 : y; - } - -} diff --git a/src/main/java/me/lrxh/practice/util/PlaceholderUtil.java b/src/main/java/me/lrxh/practice/util/PlaceholderUtil.java index 91fe8ef..92bda29 100644 --- a/src/main/java/me/lrxh/practice/util/PlaceholderUtil.java +++ b/src/main/java/me/lrxh/practice/util/PlaceholderUtil.java @@ -58,7 +58,7 @@ public static List format(List lines, Player player) { line = line.replaceAll("", String.valueOf(profile.getParty().getListOfPlayers().size())); } Match match = profile.getMatch(); - if(match != null) { + if (match != null) { if (match.getOpponent(player.getUniqueId()) != null) { line = line.replaceAll("", match.getOpponent(player.getUniqueId()).getName()); line = line.replaceAll("", match.getDuration()); @@ -70,6 +70,10 @@ public static List format(List lines, Player player) { if (match.getKit().getGameRules().isBedwars()) { line = line.replaceAll("", match.isBedABroken() ? CC.RED + CC.X : CC.GREEN + CC.CHECKMARK); line = line.replaceAll("", match.isBedBBroken() ? CC.RED + CC.X : CC.GREEN + CC.CHECKMARK); + + boolean aTeam = match.getParticipantA().containsPlayer(player.getUniqueId()); + line = line.replaceAll("", aTeam ? "" : "&7YOU"); + line = line.replaceAll("", !aTeam ? "" : "&7YOU"); } } diff --git a/src/main/java/me/lrxh/practice/util/PlayerUtil.java b/src/main/java/me/lrxh/practice/util/PlayerUtil.java index d289fc4..5bf333e 100644 --- a/src/main/java/me/lrxh/practice/util/PlayerUtil.java +++ b/src/main/java/me/lrxh/practice/util/PlayerUtil.java @@ -46,7 +46,11 @@ public class PlayerUtil { } public void setLastAttacker(Player victim, Player attacker) { - victim.setMetadata("lastAttacker", new FixedMetadataValue(Practice.getInstance(), attacker.getUniqueId())); + if(attacker == null){ + victim.setMetadata("lastAttacker", new FixedMetadataValue(Practice.getInstance(), null)); + }else{ + victim.setMetadata("lastAttacker", new FixedMetadataValue(Practice.getInstance(), attacker.getUniqueId())); + } } public static void setImmune(Player player, int ticks) { @@ -111,7 +115,7 @@ public void applyFireballKnockback(Location location, List entities) { } } - public void doVelocityChange(Player player){ + public void doVelocityChange(Player player) { player.setVelocity(player.getVelocity().add(new Vector(0, 0.25, 0))); player.setAllowFlight(true); player.setFlying(true); diff --git a/src/main/java/me/lrxh/practice/util/TimeUtil.java b/src/main/java/me/lrxh/practice/util/TimeUtil.java index acf21ed..0277690 100644 --- a/src/main/java/me/lrxh/practice/util/TimeUtil.java +++ b/src/main/java/me/lrxh/practice/util/TimeUtil.java @@ -1,5 +1,7 @@ package me.lrxh.practice.util; +import lombok.experimental.UtilityClass; + import java.sql.Timestamp; import java.text.DecimalFormat; import java.text.SimpleDateFormat; @@ -8,15 +10,12 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; +@UtilityClass public final class TimeUtil { private static final String HOUR_FORMAT = "%02d:%02d:%02d"; private static final String MINUTE_FORMAT = "%02d:%02d"; - private TimeUtil() { - throw new RuntimeException("Cannot instantiate a utility class."); - } - public static String millisToTimer(long millis) { long seconds = millis / 1000L; diff --git a/src/main/java/me/lrxh/practice/util/assemble/AssembleThread.java b/src/main/java/me/lrxh/practice/util/assemble/AssembleThread.java index c19b300..4cc819d 100644 --- a/src/main/java/me/lrxh/practice/util/assemble/AssembleThread.java +++ b/src/main/java/me/lrxh/practice/util/assemble/AssembleThread.java @@ -43,90 +43,90 @@ private void tick() { for (Player player : this.assemble.getPlugin().getServer().getOnlinePlayers()) { Profile profile = Profile.getByUuid(player.getUniqueId()); try { - if (!profile.getOptions().showScoreboard()) { - return; + if (!profile.getOptions().showScoreboard()) { + return; + } + AssembleBoard board = this.assemble.getBoards().get(player.getUniqueId()); + + // This shouldn't happen, but just in case. + if (board == null) { + continue; + } + + Scoreboard scoreboard = board.getScoreboard(); + Objective objective = board.getObjective(); + + if (scoreboard == null || objective == null) { + continue; + } + + // Just make a variable so we don't have to + // process the same thing twice. + String title = ChatColor.translateAlternateColorCodes('&', this.assemble.getAdapter().getTitle(player)); + + // Update the title if needed. + if (!objective.getDisplayName().equals(title)) { + objective.setDisplayName(title); + } + + List newLines = this.assemble.getAdapter().getLines(player); + + // Allow adapter to return null/empty list to display nothing. + if (newLines == null || newLines.isEmpty()) { + board.getEntries().forEach(AssembleBoardEntry::remove); + board.getEntries().clear(); + } else { + if (newLines.size() > 15) { + newLines = newLines.subList(0, 15); } - AssembleBoard board = this.assemble.getBoards().get(player.getUniqueId()); - // This shouldn't happen, but just in case. - if (board == null) { - continue; + // Reverse the lines because scoreboard scores are in descending order. + if (!this.assemble.getAssembleStyle().isDescending()) { + Collections.reverse(newLines); } - Scoreboard scoreboard = board.getScoreboard(); - Objective objective = board.getObjective(); - - if (scoreboard == null || objective == null) { - continue; - } - - // Just make a variable so we don't have to - // process the same thing twice. - String title = ChatColor.translateAlternateColorCodes('&', this.assemble.getAdapter().getTitle(player)); - - // Update the title if needed. - if (!objective.getDisplayName().equals(title)) { - objective.setDisplayName(title); - } - - List newLines = this.assemble.getAdapter().getLines(player); - - // Allow adapter to return null/empty list to display nothing. - if (newLines == null || newLines.isEmpty()) { - board.getEntries().forEach(AssembleBoardEntry::remove); - board.getEntries().clear(); - } else { - if (newLines.size() > 15) { - newLines = newLines.subList(0, 15); - } - - // Reverse the lines because scoreboard scores are in descending order. - if (!this.assemble.getAssembleStyle().isDescending()) { - Collections.reverse(newLines); - } - - // Remove excessive amount of board entries. - if (board.getEntries().size() > newLines.size()) { - for (int i = newLines.size(); i < board.getEntries().size(); i++) { - AssembleBoardEntry entry = board.getEntryAtPosition(i); + // Remove excessive amount of board entries. + if (board.getEntries().size() > newLines.size()) { + for (int i = newLines.size(); i < board.getEntries().size(); i++) { + AssembleBoardEntry entry = board.getEntryAtPosition(i); - if (entry != null) { - entry.remove(); - } + if (entry != null) { + entry.remove(); } } + } - // Update existing entries / add new entries. - int cache = this.assemble.getAssembleStyle().getStartNumber(); - for (int i = 0; i < newLines.size(); i++) { - AssembleBoardEntry entry = board.getEntryAtPosition(i); + // Update existing entries / add new entries. + int cache = this.assemble.getAssembleStyle().getStartNumber(); + for (int i = 0; i < newLines.size(); i++) { + AssembleBoardEntry entry = board.getEntryAtPosition(i); - // Translate any colors. - String line = ChatColor.translateAlternateColorCodes('&', newLines.get(i)); + // Translate any colors. + String line = ChatColor.translateAlternateColorCodes('&', newLines.get(i)); - // If the entry is null, just create a new one. - // Creating a new AssembleBoardEntry instance will add - // itself to the provided board's entries list. - if (entry == null) { - entry = new AssembleBoardEntry(board, line, i); - } - - // Update text, setup the team, and update the display values. - entry.setText(line); - entry.setup(); - entry.send( - this.assemble.getAssembleStyle().isDescending() ? cache-- : cache++ - ); + // If the entry is null, just create a new one. + // Creating a new AssembleBoardEntry instance will add + // itself to the provided board's entries list. + if (entry == null) { + entry = new AssembleBoardEntry(board, line, i); } - } - if (player.getScoreboard() != scoreboard && !assemble.isHook()) { - player.setScoreboard(scoreboard); + // Update text, setup the team, and update the display values. + entry.setText(line); + entry.setup(); + entry.send( + this.assemble.getAssembleStyle().isDescending() ? cache-- : cache++ + ); } + } - } catch (Exception e) { - //Bukkit.getServer().getScheduler().runTask(assemble.getPlugin(), () -> player.kickPlayer(CC.translate("&cPlease rejoin."))); - Bukkit.getLogger().warning("Error updating scoreboard for player " + player.getName() + ": " + e.getMessage()); + if (player.getScoreboard() != scoreboard && !assemble.isHook()) { + player.setScoreboard(scoreboard); + } + + } catch (Exception e) { + //Bukkit.getServer().getScheduler().runTask(assemble.getPlugin(), () -> player.kickPlayer(CC.translate("&cPlease rejoin."))); + Bukkit.getLogger().warning("Error updating scoreboard for player " + player.getName() + ": " + e.getMessage()); } }