Skip to content

Commit

Permalink
Code improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
Devlrxxh committed Jul 13, 2024
1 parent c4b7ddf commit 86bbee0
Show file tree
Hide file tree
Showing 13 changed files with 57 additions and 35 deletions.
2 changes: 1 addition & 1 deletion Plugin/src/main/java/dev/lrxh/neptune/Neptune.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
}
};

Expand Down
23 changes: 12 additions & 11 deletions Plugin/src/main/java/dev/lrxh/neptune/kit/Kit.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<ItemStack> items;
Expand All @@ -28,7 +28,7 @@ public class Kit {
private HashMap<KitRule, Boolean> rules;
private int queue, playing;

public Kit(String name, String displayName, List<ItemStack> items, HashSet<Arena> arenas, ItemStack icon, HashMap<KitRule, Boolean> rules) {
public Kit(String name, String displayName, List<ItemStack> items, HashSet<Arena> arenas, ItemStack icon, HashMap<KitRule, Boolean> rules, Neptune plugin) {
this.name = name;
this.displayName = displayName;
this.items = items;
Expand All @@ -37,17 +37,12 @@ public Kit(String name, String displayName, List<ItemStack> items, HashSet<Arena
this.rules = rules;
this.queue = 0;
this.playing = 0;
this.plugin = plugin;

if (plugin.getLeaderboardManager() != null) {
plugin.getLeaderboardManager().getLeaderboards().put(this, new ArrayList<>());
}

if (plugin.getProfileManager() != null) {
addToProfiles();
}
checkMissing();
}

public Kit(String name, List<ItemStack> items, ItemStack icon) {
public Kit(String name, List<ItemStack> items, ItemStack icon, Neptune plugin) {
this.name = name;
this.displayName = name;
this.items = items;
Expand All @@ -56,7 +51,13 @@ public Kit(String name, List<ItemStack> 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<>());
}
Expand Down Expand Up @@ -128,7 +129,7 @@ public void addPlaying() {
}

public void delete() {
Neptune.get().getKitManager().kits.remove(this);
plugin.getKitManager().kits.remove(this);
}
}

2 changes: 1 addition & 1 deletion Plugin/src/main/java/dev/lrxh/neptune/kit/KitManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import lombok.Getter;

@SuppressWarnings("unused")
@Getter
public enum KitRule {
BUILD("Allow/Deny Players to place blocks.", "Build", "build"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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) {
Expand All @@ -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()) + ")");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,25 @@

public class TaskScheduler {
private final List<NeptuneRunnable> 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) {
Expand Down
12 changes: 1 addition & 11 deletions Plugin/src/main/java/dev/lrxh/neptune/utils/PlaceholderUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public List<String> format(List<String> lines, Player player) {
line = line.replaceAll("<opponent-combo>", opponent.getCombo() > 1 ? "&e(" + opponent.getCombo() + " Combo)" : "");
line = line.replaceAll("<hits>", String.valueOf(participant.getHits()));
line = line.replaceAll("<opponent-hits>", String.valueOf(opponent.getHits()));
line = line.replaceAll("<diffrence>", getDifference(participant, opponent));
line = line.replaceAll("<diffrence>", participant.getHitsDifference(opponent));


if (match.getRounds() > 1) {
Expand Down Expand Up @@ -122,14 +122,4 @@ public List<String> format(List<String> 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()) + ")");
}
}
}
2 changes: 2 additions & 0 deletions Plugin/src/main/java/dev/lrxh/neptune/utils/menu/Button.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down

0 comments on commit 86bbee0

Please sign in to comment.