Moved the enderpearl cooldown and combat tag tasks to 2 new classes, fixes fo FFAListener, changes to lang.yml and Language.java, and some minimal stuff that is not worth talking about

This commit is contained in:
Matheus 2024-03-19 08:50:22 -04:00
parent 6e4f5dbd0f
commit 3e10a8cac9
11 changed files with 115 additions and 83 deletions

View File

@ -11,7 +11,10 @@ import java.util.List;
public enum Language { public enum Language {
PEARL_COOLDOWN("COOLDOWN.PEARL"), PEARL_COOLDOWN("COOLDOWN.PEARL"),
REKIT_COOLDOWN("COOLDOWN.REKIT"); REKIT_COOLDOWN("COOLDOWN.REKIT"),
COMBAT_COOLDOWN("COOLDOWN.COMBAT"),
NO_PEARL_COOLDOWN("NO-COOLDOWN.PEARL"),
NO_COMBAT_COOLDOWN("NO-COOLDOWN.COMBAT");
private String path; private String path;

View File

@ -58,9 +58,9 @@ public class Main extends JavaPlugin {
saveMethodType = SaveMethodType.defaultTo(); saveMethodType = SaveMethodType.defaultTo();
} }
combatManager = new CombatManager(); combatManager = new CombatManager();
combatManager.runTaskTimer(this, 0, 2); combatManager.getTask().runTaskTimer(this, 0, 2);
enderpearlManager = new EnderpearlManager(); enderpearlManager = new EnderpearlManager();
enderpearlManager.runTaskTimer(this, 0, 2); enderpearlManager.getTask().runTaskTimer(this, 0, 2);
if (saveMethodType == SaveMethodType.MONGO) { if (saveMethodType == SaveMethodType.MONGO) {
loadMongo(); loadMongo();
} }

View File

@ -7,7 +7,7 @@ import org.bukkit.entity.Player;
@CommandMeta(label = "leaderboards") @CommandMeta(label = "leaderboards")
public class LeaderboardsCommand { public class LeaderboardsCommand {
public void exec(Player player) { public void execute(Player player) {
new LeaderboardsMenu().openMenu(player); new LeaderboardsMenu().openMenu(player);
} }

View File

@ -81,7 +81,8 @@ public class FFAListener implements Listener {
public void quitFFA(FFAQuitEvent event) { public void quitFFA(FFAQuitEvent event) {
Profile profile = Profile.getOrCreate(event.getPlayer()); Profile profile = Profile.getOrCreate(event.getPlayer());
if (Main.getInstance().getCombatManager().isInCombat(event.getPlayer())) { if (Main.getInstance().getCombatManager().isInCombat(event.getPlayer())) {
event.getPlayer().sendMessage(CC.RED + "You're still in combat!" + CC.GRAY + " (Remaining: " + Main.getInstance().getCombatManager().getCombatTime(event.getPlayer()) + "s)"); event.getPlayer().sendMessage(CC.translate(Language.COMBAT_COOLDOWN.format(Main.getInstance().getCombatManager().getCombatTime(event.getPlayer()))));
//event.getPlayer().sendMessage(CC.RED + "You're still in combat!" + CC.GRAY + " (Remaining: " + Main.getInstance().getCombatManager().getCombatTime(event.getPlayer()) + "s)");
} else { } else {
FFACache.getFFAList().get(profile.getFFA()).remove(event.getPlayer()); FFACache.getFFAList().get(profile.getFFA()).remove(event.getPlayer());
if (Hotbar.setup(event.getPlayer()) && Main.getInstance().getEssentials().getSpawn() != null) { if (Hotbar.setup(event.getPlayer()) && Main.getInstance().getEssentials().getSpawn() != null) {

View File

@ -41,20 +41,14 @@ public class Profile {
collection = Main.getInstance().getMongoDatabase().getCollection("profiles"); collection = Main.getInstance().getMongoDatabase().getCollection("profiles");
} }
// Players might have joined before the plugin finished loading
for (Player player : Bukkit.getOnlinePlayers()) { for (Player player : Bukkit.getOnlinePlayers()) {
Profile profile = new Profile(player.getUniqueId());
try { try {
profile.load(); new Profile(player.getUniqueId());
} catch (Exception e) { } catch (Exception e) {
player.kickPlayer(CC.RED + "The server is loading..."); player.kickPlayer(CC.RED + "The server is loading...");
continue;
} }
new Profile(player.getUniqueId());
} }
// Save every 5 minutes to prevent data loss
new BukkitRunnable() { new BukkitRunnable() {
@Override @Override
public void run() { public void run() {
@ -117,7 +111,7 @@ public class Profile {
return null; return null;
} }
void load() { public void load() {
if (Main.getInstance().getSaveMethodType() == SaveMethodType.MONGO && collection != null) { if (Main.getInstance().getSaveMethodType() == SaveMethodType.MONGO && collection != null) {
Document document = collection.find(Filters.eq("uuid", player.getUniqueId().toString())).first(); Document document = collection.find(Filters.eq("uuid", player.getUniqueId().toString())).first();

View File

@ -0,0 +1,33 @@
package dev.lugami.otaku.tasks;
import dev.lugami.otaku.Language;
import dev.lugami.otaku.Main;
import dev.lugami.otaku.utils.CC;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitRunnable;
public class CombatTask extends BukkitRunnable {
@Override
public void run() {
Main.getInstance().getCombatManager().count++;
for (Player player : Bukkit.getOnlinePlayers()) {
if (Main.getInstance().getCombatManager().isInCombat(player)) {
double remainingTime = Main.getInstance().getCombatManager().getCombatTime(player) - 0.1; // Adjust the decrement value as needed
Main.getInstance().getCombatManager().setCombatTime(player, remainingTime);
if (remainingTime <= 0) {
player.sendMessage(CC.translate(Language.NO_COMBAT_COOLDOWN.format()));
Main.getInstance().getCombatManager().setInCombat(player, false);
}
}
}
if (Main.getInstance().getCombatManager().count == 160) {
Main.getInstance().getCombatManager().count = 0;
}
}
}

View File

@ -0,0 +1,32 @@
package dev.lugami.otaku.tasks;
import dev.lugami.otaku.Language;
import dev.lugami.otaku.Main;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitRunnable;
public class EnderpearlTask extends BukkitRunnable {
@Override
public void run() {
Main.getInstance().getEnderpearlManager().count++;
for (Player player : Bukkit.getOnlinePlayers()) {
if (Main.getInstance().getEnderpearlManager().isPearlCooldown(player)) {
double remainingTime = Main.getInstance().getEnderpearlManager().getPearlCooldown(player) - 0.1; // Adjust the decrement value as needed
Main.getInstance().getEnderpearlManager().setCooldownTime(player, remainingTime);
if (remainingTime <= 0) {
player.sendMessage(Language.NO_PEARL_COOLDOWN.format());
Main.getInstance().getEnderpearlManager().setPearlCooldown(player, false);
}
}
}
if (Main.getInstance().getEnderpearlManager().count == 160) {
Main.getInstance().getEnderpearlManager().count = 0;
}
}
}

View File

@ -1,19 +1,20 @@
package dev.lugami.otaku.utils; package dev.lugami.otaku.utils;
import org.bukkit.Bukkit; import dev.lugami.otaku.tasks.CombatTask;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitRunnable;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
public class CombatManager extends BukkitRunnable { public class CombatManager {
private CombatTask task = new CombatTask();
private final Set<Player> combatSet = new HashSet<>(); private final Set<Player> combatSet = new HashSet<>();
private final Map<Player, Double> timeMap = new HashMap<>(); private final Map<Player, Double> timeMap = new HashMap<>();
private int count = 0; public int count = 0;
public void setInCombat(Player player, boolean inCombat) { public void setInCombat(Player player, boolean inCombat) {
if (inCombat) { if (inCombat) {
@ -37,24 +38,10 @@ public class CombatManager extends BukkitRunnable {
return Math.round(timeMap.getOrDefault(player, 0.0) * 10.0) / 10.0; return Math.round(timeMap.getOrDefault(player, 0.0) * 10.0) / 10.0;
} }
@Override public CombatTask getTask() {
public void run() { if (task == null) {
count++; task = new CombatTask();
}
for (Player player : Bukkit.getOnlinePlayers()) { return task;
if (isInCombat(player)) {
double remainingTime = getCombatTime(player) - 0.1; // Adjust the decrement value as needed
setCombatTime(player, remainingTime);
if (remainingTime <= 0) {
player.sendMessage(CC.translate("&aYou are no longer in combat!"));
setInCombat(player, false);
}
}
}
if (count == 160) {
count = 0;
}
} }
} }

View File

@ -1,8 +1,7 @@
package dev.lugami.otaku.utils; package dev.lugami.otaku.utils;
import org.bukkit.Bukkit; import dev.lugami.otaku.tasks.EnderpearlTask;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitRunnable;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
@ -12,11 +11,13 @@ import java.util.Set;
/** /**
* same thing as dev.lugami.otaku.utils.CombatManager but for pearls * same thing as dev.lugami.otaku.utils.CombatManager but for pearls
*/ */
public class EnderpearlManager extends BukkitRunnable { public class EnderpearlManager {
private EnderpearlTask task = new EnderpearlTask();
private final Set<Player> pearl = new HashSet<>(); private final Set<Player> pearl = new HashSet<>();
private final Map<Player, Double> timeMap = new HashMap<>(); private final Map<Player, Double> timeMap = new HashMap<>();
private int count = 0; public int count = 0;
public void setPearlCooldown(Player player, boolean pearlCooldown) { public void setPearlCooldown(Player player, boolean pearlCooldown) {
if (pearlCooldown) { if (pearlCooldown) {
@ -40,25 +41,11 @@ public class EnderpearlManager extends BukkitRunnable {
return Math.round(timeMap.getOrDefault(player, 0.0) * 10.0) / 10.0; return Math.round(timeMap.getOrDefault(player, 0.0) * 10.0) / 10.0;
} }
@Override public EnderpearlTask getTask() {
public void run() { if (task == null) {
count++; task = new EnderpearlTask();
for (Player player : Bukkit.getOnlinePlayers()) {
if (isPearlCooldown(player)) {
double remainingTime = getPearlCooldown(player) - 0.1; // Adjust the decrement value as needed
setCooldownTime(player, remainingTime);
if (remainingTime <= 0) {
player.sendMessage(CC.translate("&aYour enderpearl cooldown ended!"));
setPearlCooldown(player, false);
}
}
}
if (count == 160) {
count = 0;
} }
return task;
} }
} }

View File

@ -3,6 +3,7 @@ package dev.lugami.otaku.utils;
import dev.lugami.otaku.profile.Profile; import dev.lugami.otaku.profile.Profile;
import dev.lugami.otaku.profile.data.ProfileState; import dev.lugami.otaku.profile.data.ProfileState;
import lombok.Getter; import lombok.Getter;
import lombok.NonNull;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import java.util.HashMap; import java.util.HashMap;
@ -14,12 +15,8 @@ public class PlayerCache {
@Getter @Getter
private static final Map<String, UUID> playerCache = new HashMap<>(); private static final Map<String, UUID> playerCache = new HashMap<>();
public static UUID getUUID(String name) { public static UUID getUUID(@NonNull String name) {
UUID uuid = null; return playerCache.get(name);
if (getPlayerCache().containsKey(name)) {
uuid = getPlayerCache().get(name);
}
return uuid;
} }
public static int getInFFA() { public static int getInFFA() {
@ -28,11 +25,8 @@ public class PlayerCache {
return i; return i;
} }
public static String getName(UUID uuid) { public static String getName(@NonNull UUID uuid) {
for (UUID cachedUUID : playerCache.values()) { String name = playerCache.entrySet().stream()
if (cachedUUID.equals(uuid)) {
String name = playerCache.entrySet()
.stream()
.filter(entry -> entry.getValue().equals(uuid)) .filter(entry -> entry.getValue().equals(uuid))
.map(Map.Entry::getKey) .map(Map.Entry::getKey)
.findFirst() .findFirst()
@ -47,9 +41,5 @@ public class PlayerCache {
return name; return name;
} }
}
return null;
}
} }

View File

@ -1,3 +1,8 @@
COOLDOWN: COOLDOWN:
REKIT: "You're still on re-kit cooldown. &7[{0}s]" REKIT: "&cYou''re still on re-kit cooldown. &7[{0}s]"
PEARL: "You're still on pearl cooldown. &7[{0}s]" PEARL: "&cYou''re still on pearl cooldown. &7[{0}s]"
COMBAT: "&cYou're still on combat! &7[{0}s]"
NO-COOLDOWN:
PEARL: "&aYour pearl cooldown expired!"
COMBAT: "&aYou're no longer in combat!"