From 86bbee0913a30cae49cf207c69ba915d27a332cb Mon Sep 17 00:00:00 2001 From: devlrxxh Date: Sat, 13 Jul 2024 13:17:45 +0200 Subject: [PATCH] Code improvements --- .../main/java/dev/lrxh/neptune/Neptune.java | 2 +- .../neptune/cosmetics/impl/KillEffect.java | 8 ++++++- .../main/java/dev/lrxh/neptune/kit/Kit.java | 23 ++++++++++--------- .../java/dev/lrxh/neptune/kit/KitManager.java | 2 +- .../lrxh/neptune/kit/command/KitCommand.java | 2 +- .../dev/lrxh/neptune/kit/impl/KitRule.java | 1 - .../match/impl/participant/Participant.java | 15 ++++++++++-- .../lrxh/neptune/profile/VisibilityLogic.java | 8 +++++-- .../providers/tasks/NeptuneRunnable.java | 4 ++++ .../providers/tasks/TaskScheduler.java | 11 ++++++--- .../lrxh/neptune/utils/PlaceholderUtil.java | 12 +--------- .../dev/lrxh/neptune/utils/menu/Button.java | 2 ++ .../utils/menu/listener/MenuListener.java | 2 +- 13 files changed, 57 insertions(+), 35 deletions(-) diff --git a/Plugin/src/main/java/dev/lrxh/neptune/Neptune.java b/Plugin/src/main/java/dev/lrxh/neptune/Neptune.java index b7ce32a..0dcbdd4 100644 --- a/Plugin/src/main/java/dev/lrxh/neptune/Neptune.java +++ b/Plugin/src/main/java/dev/lrxh/neptune/Neptune.java @@ -166,7 +166,7 @@ private void loadWorlds() { } private void loadTasks() { - taskScheduler = new TaskScheduler(); + taskScheduler = new TaskScheduler(this); new QueueCheckTask().start(SettingsLocale.QUEUE_UPDATE_TIME.getInt(), this); new LeaderboardTask().start(SettingsLocale.LEADERBOARD_UPDATE_TIME.getInt(), this); } diff --git a/Plugin/src/main/java/dev/lrxh/neptune/cosmetics/impl/KillEffect.java b/Plugin/src/main/java/dev/lrxh/neptune/cosmetics/impl/KillEffect.java index 2e9285d..da55f4e 100644 --- a/Plugin/src/main/java/dev/lrxh/neptune/cosmetics/impl/KillEffect.java +++ b/Plugin/src/main/java/dev/lrxh/neptune/cosmetics/impl/KillEffect.java @@ -2,6 +2,7 @@ import dev.lrxh.neptune.Neptune; import dev.lrxh.neptune.configs.impl.CosmeticsLocale; +import dev.lrxh.neptune.providers.tasks.NeptuneRunnable; import lombok.Getter; import org.bukkit.*; import org.bukkit.entity.Firework; @@ -40,7 +41,12 @@ public void execute(Player player) { meta.addEffect(builder.build()); meta.setPower(1); firework.setFireworkMeta(meta); - Bukkit.getScheduler().runTaskLater(Neptune.get(), firework::detonate, 5L); + new NeptuneRunnable() { + @Override + public void run() { + firework.detonate(); + } + }.startLater(5, Neptune.get()); } }; diff --git a/Plugin/src/main/java/dev/lrxh/neptune/kit/Kit.java b/Plugin/src/main/java/dev/lrxh/neptune/kit/Kit.java index 75f38cf..611db37 100644 --- a/Plugin/src/main/java/dev/lrxh/neptune/kit/Kit.java +++ b/Plugin/src/main/java/dev/lrxh/neptune/kit/Kit.java @@ -19,7 +19,7 @@ @Setter @AllArgsConstructor public class Kit { - private final Neptune plugin = Neptune.get(); + private Neptune plugin; private String name; private String displayName; private List items; @@ -28,7 +28,7 @@ public class Kit { private HashMap rules; private int queue, playing; - public Kit(String name, String displayName, List items, HashSet arenas, ItemStack icon, HashMap rules) { + public Kit(String name, String displayName, List items, HashSet arenas, ItemStack icon, HashMap rules, Neptune plugin) { this.name = name; this.displayName = displayName; this.items = items; @@ -37,17 +37,12 @@ public Kit(String name, String displayName, List items, HashSet()); - } - - if (plugin.getProfileManager() != null) { - addToProfiles(); - } + checkMissing(); } - public Kit(String name, List items, ItemStack icon) { + public Kit(String name, List items, ItemStack icon, Neptune plugin) { this.name = name; this.displayName = name; this.items = items; @@ -56,7 +51,13 @@ public Kit(String name, List items, ItemStack icon) { this.icon = icon.getType().equals(Material.AIR) ? new ItemStack(Material.BARRIER) : new ItemStack(icon); this.queue = 0; this.playing = 0; + this.plugin = plugin; + + checkMissing(); + } + + private void checkMissing() { if (plugin.getLeaderboardManager() != null) { plugin.getLeaderboardManager().getLeaderboards().put(this, new ArrayList<>()); } @@ -128,7 +129,7 @@ public void addPlaying() { } public void delete() { - Neptune.get().getKitManager().kits.remove(this); + plugin.getKitManager().kits.remove(this); } } diff --git a/Plugin/src/main/java/dev/lrxh/neptune/kit/KitManager.java b/Plugin/src/main/java/dev/lrxh/neptune/kit/KitManager.java index 110e54c..1e7d6b7 100644 --- a/Plugin/src/main/java/dev/lrxh/neptune/kit/KitManager.java +++ b/Plugin/src/main/java/dev/lrxh/neptune/kit/KitManager.java @@ -45,7 +45,7 @@ public void loadKits() { rules.put(kitRule, config.getBoolean(path + kitRule.getSaveName(), false)); } - kits.add(new Kit(kitName, displayName, items, arenas, icon, rules)); + kits.add(new Kit(kitName, displayName, items, arenas, icon, rules, plugin)); } } } diff --git a/Plugin/src/main/java/dev/lrxh/neptune/kit/command/KitCommand.java b/Plugin/src/main/java/dev/lrxh/neptune/kit/command/KitCommand.java index 524f8ca..6b04fba 100644 --- a/Plugin/src/main/java/dev/lrxh/neptune/kit/command/KitCommand.java +++ b/Plugin/src/main/java/dev/lrxh/neptune/kit/command/KitCommand.java @@ -63,7 +63,7 @@ public void create(Player player, String kitName) { return; } - Kit kit = new Kit(kitName, Arrays.asList(player.getInventory().getContents()), PlayerUtil.getItemInHand(player.getUniqueId())); + Kit kit = new Kit(kitName, Arrays.asList(player.getInventory().getContents()), PlayerUtil.getItemInHand(player.getUniqueId()), plugin); plugin.getKitManager().kits.add(kit); plugin.getKitManager().saveKits(); diff --git a/Plugin/src/main/java/dev/lrxh/neptune/kit/impl/KitRule.java b/Plugin/src/main/java/dev/lrxh/neptune/kit/impl/KitRule.java index aeba5f1..4a766c7 100644 --- a/Plugin/src/main/java/dev/lrxh/neptune/kit/impl/KitRule.java +++ b/Plugin/src/main/java/dev/lrxh/neptune/kit/impl/KitRule.java @@ -2,7 +2,6 @@ import lombok.Getter; -@SuppressWarnings("unused") @Getter public enum KitRule { BUILD("Allow/Deny Players to place blocks.", "Build", "build"), diff --git a/Plugin/src/main/java/dev/lrxh/neptune/match/impl/participant/Participant.java b/Plugin/src/main/java/dev/lrxh/neptune/match/impl/participant/Participant.java index 22686e5..4adad58 100644 --- a/Plugin/src/main/java/dev/lrxh/neptune/match/impl/participant/Participant.java +++ b/Plugin/src/main/java/dev/lrxh/neptune/match/impl/participant/Participant.java @@ -6,6 +6,7 @@ import dev.lrxh.neptune.match.Match; import dev.lrxh.neptune.match.impl.SoloFightMatch; import dev.lrxh.neptune.profile.Profile; +import dev.lrxh.neptune.utils.CC; import dev.lrxh.neptune.utils.PlayerUtil; import dev.lrxh.sounds.Sound; import lombok.Data; @@ -53,7 +54,7 @@ public void playSound(Sound sound) { Player player = Bukkit.getPlayer(playerUUID); if (player == null) return; player.playSound(player.getLocation(), - (org.bukkit.Sound) Neptune.get().getVersionHandler().getSound().getSound(sound), 1.0f, 1.0f); + (org.bukkit.Sound) plugin.getVersionHandler().getSound().getSound(sound), 1.0f, 1.0f); } public void playKillEffect() { @@ -100,7 +101,7 @@ public void handleHit(Participant opponent) { break; } } - Match match = Neptune.get().getProfileManager().getByUUID(playerUUID).getMatch(); + Match match = plugin.getProfileManager().getByUUID(playerUUID).getMatch(); if (match instanceof SoloFightMatch) { if (match.getKit().is(KitRule.BOXING)) { if (hits >= 100) { @@ -110,4 +111,14 @@ public void handleHit(Participant opponent) { } } } + + public String getHitsDifference(Participant otherParticipant) { + if (hits - otherParticipant.getHits() > 0) { + return CC.color("&a(+" + (hits - otherParticipant.getHits()) + ")"); + } else if (hits - otherParticipant.getHits() < 0) { + return CC.color("&c(" + (hits - otherParticipant.getHits()) + ")"); + } else { + return CC.color("&e(" + (hits - otherParticipant.getHits()) + ")"); + } + } } diff --git a/Plugin/src/main/java/dev/lrxh/neptune/profile/VisibilityLogic.java b/Plugin/src/main/java/dev/lrxh/neptune/profile/VisibilityLogic.java index 36f6c66..a50711e 100644 --- a/Plugin/src/main/java/dev/lrxh/neptune/profile/VisibilityLogic.java +++ b/Plugin/src/main/java/dev/lrxh/neptune/profile/VisibilityLogic.java @@ -37,13 +37,17 @@ public void handle(UUID playerUUID, UUID otherUUID) { return; } - if (viewerProfile.getSettingData().isPlayerVisibility() && - has(playerUUID, otherUUID, ProfileState.IN_LOBBY, ProfileState.IN_QUEUE, ProfileState.IN_PARTY)) { + if (has(playerUUID, otherUUID, ProfileState.IN_LOBBY, ProfileState.IN_QUEUE, ProfileState.IN_PARTY)) { viewerPlayer.showPlayer(plugin, otherPlayer); otherPlayer.showPlayer(plugin, viewerPlayer); return; } + if(viewerProfile.getSettingData().isPlayerVisibility()){ + viewerPlayer.showPlayer(plugin, otherPlayer); + return; + } + viewerPlayer.hidePlayer(plugin, otherPlayer); otherPlayer.hidePlayer(plugin, viewerPlayer); diff --git a/Plugin/src/main/java/dev/lrxh/neptune/providers/tasks/NeptuneRunnable.java b/Plugin/src/main/java/dev/lrxh/neptune/providers/tasks/NeptuneRunnable.java index ae86b26..843fb60 100644 --- a/Plugin/src/main/java/dev/lrxh/neptune/providers/tasks/NeptuneRunnable.java +++ b/Plugin/src/main/java/dev/lrxh/neptune/providers/tasks/NeptuneRunnable.java @@ -20,4 +20,8 @@ public void start(Neptune plugin) { public void stop(Neptune plugin) { plugin.getTaskScheduler().removeTask(this); } + + public void startLater(long delay, Neptune plugin) { + plugin.getTaskScheduler().startTaskLater(this, delay); + } } diff --git a/Plugin/src/main/java/dev/lrxh/neptune/providers/tasks/TaskScheduler.java b/Plugin/src/main/java/dev/lrxh/neptune/providers/tasks/TaskScheduler.java index 73d3dc3..8db76cf 100644 --- a/Plugin/src/main/java/dev/lrxh/neptune/providers/tasks/TaskScheduler.java +++ b/Plugin/src/main/java/dev/lrxh/neptune/providers/tasks/TaskScheduler.java @@ -7,20 +7,25 @@ public class TaskScheduler { private final List tasks = new CopyOnWriteArrayList<>(); + private final Neptune plugin; + + public TaskScheduler(Neptune plugin) { + this.plugin = plugin; + } public void startTask(NeptuneRunnable task, long delay, long period) { tasks.add(task); - task.runTaskTimer(Neptune.get(), delay, period); + task.runTaskTimer(plugin, delay, period); } public void startTask(NeptuneRunnable task) { tasks.add(task); - task.runTask(Neptune.get()); + task.runTask(plugin); } public void startTaskLater(NeptuneRunnable task, long delay) { tasks.add(task); - task.runTaskLater(Neptune.get(), delay); + task.runTaskLater(plugin, delay); } public void stopAllTasks(Neptune plugin) { diff --git a/Plugin/src/main/java/dev/lrxh/neptune/utils/PlaceholderUtil.java b/Plugin/src/main/java/dev/lrxh/neptune/utils/PlaceholderUtil.java index f92781c..6632b04 100644 --- a/Plugin/src/main/java/dev/lrxh/neptune/utils/PlaceholderUtil.java +++ b/Plugin/src/main/java/dev/lrxh/neptune/utils/PlaceholderUtil.java @@ -64,7 +64,7 @@ public List format(List lines, Player player) { line = line.replaceAll("", opponent.getCombo() > 1 ? "&e(" + opponent.getCombo() + " Combo)" : ""); line = line.replaceAll("", String.valueOf(participant.getHits())); line = line.replaceAll("", String.valueOf(opponent.getHits())); - line = line.replaceAll("", getDifference(participant, opponent)); + line = line.replaceAll("", participant.getHitsDifference(opponent)); if (match.getRounds() > 1) { @@ -122,14 +122,4 @@ public List format(List lines, Player player) { return formattedLines; } - - public String getDifference(Participant participant, Participant opponent) { - if (participant.getHits() - opponent.getHits() > 0) { - return CC.color("&a(+" + (participant.getHits() - opponent.getHits()) + ")"); - } else if (participant.getHits() - opponent.getHits() < 0) { - return CC.color("&c(" + (participant.getHits() - opponent.getHits()) + ")"); - } else { - return CC.color("&e(" + (participant.getHits() - opponent.getHits()) + ")"); - } - } } diff --git a/Plugin/src/main/java/dev/lrxh/neptune/utils/menu/Button.java b/Plugin/src/main/java/dev/lrxh/neptune/utils/menu/Button.java index f273592..8c150e3 100644 --- a/Plugin/src/main/java/dev/lrxh/neptune/utils/menu/Button.java +++ b/Plugin/src/main/java/dev/lrxh/neptune/utils/menu/Button.java @@ -13,8 +13,10 @@ @NoArgsConstructor public abstract class Button { public Neptune plugin = Neptune.get(); + private boolean sound = true; public void onClick(Player player, ClickType clickType) { + sound = false; } public abstract ItemStack getButtonItem(Player player); diff --git a/Plugin/src/main/java/dev/lrxh/neptune/utils/menu/listener/MenuListener.java b/Plugin/src/main/java/dev/lrxh/neptune/utils/menu/listener/MenuListener.java index 5e65c05..21b487c 100644 --- a/Plugin/src/main/java/dev/lrxh/neptune/utils/menu/listener/MenuListener.java +++ b/Plugin/src/main/java/dev/lrxh/neptune/utils/menu/listener/MenuListener.java @@ -42,7 +42,7 @@ public void onButtonPress(InventoryClickEvent event) { button.onClick(player, event.getClick()); Profile profile = plugin.getProfileManager().getByUUID(player.getUniqueId()); - if (profile != null && profile.getSettingData().isMenuSound()) { + if (profile != null && profile.getSettingData().isMenuSound() && button.isSound()) { player.playSound(player.getLocation(), Sound.UI_BUTTON_CLICK, 1, 1); }