BossBar API update + add equals/hashcode to Chat class

This commit is contained in:
Marc Baloup 2021-07-21 23:50:43 +02:00
parent bdb944f961
commit 94ba023fc8
Signed by: marcbal
GPG Key ID: BBC0FE3ABC30B893
2 changed files with 41 additions and 15 deletions

View File

@ -304,6 +304,20 @@ public abstract class Chat extends ChatStatic implements HoverEventSource<Compon
@Override
public boolean equals(Object obj) {
if (obj == null || !(obj instanceof Chat))
return false;
return getAdv().equals(((Chat)obj).getAdv());
}
@Override
public int hashCode() {
return getAdv().hashCode();
}

View File

@ -1,14 +1,10 @@
package fr.pandacube.lib.paper.util; package fr.pandacube.lib.paper.util;
import java.util.Objects;
import java.util.Timer; import java.util.Timer;
import java.util.TimerTask; import java.util.TimerTask;
import java.util.function.Predicate; import java.util.function.Predicate;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.boss.BarColor;
import org.bukkit.boss.BossBar;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority; import org.bukkit.event.EventPriority;
@ -18,8 +14,11 @@ import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.scheduler.BukkitScheduler; import org.bukkit.scheduler.BukkitScheduler;
import org.bukkit.scheduler.BukkitTask; import org.bukkit.scheduler.BukkitTask;
import fr.pandacube.lib.core.chat.Chat;
import fr.pandacube.lib.core.util.Log; import fr.pandacube.lib.core.util.Log;
import fr.pandacube.lib.paper.PandaLibPaper; import fr.pandacube.lib.paper.PandaLibPaper;
import net.kyori.adventure.bossbar.BossBar;
import net.kyori.adventure.bossbar.BossBar.Color;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
public class AutoUpdatedBossBar implements Listener { public class AutoUpdatedBossBar implements Listener {
@ -53,7 +52,7 @@ public class AutoUpdatedBossBar implements Listener {
throw new IllegalStateException("Can't schedule an already scheduled bossbar update"); throw new IllegalStateException("Can't schedule an already scheduled bossbar update");
scheduled = true; scheduled = true;
timer = new Timer("Panda BossBarUpdater - " + ChatColor.stripColor(bar.getTitle())); timer = new Timer("Panda BossBarUpdater - " + Chat.chatComponent(bar.name()).getPlainText());
timer.schedule(new TimerTask() { timer.schedule(new TimerTask() {
@Override @Override
public void run() { public void run() {
@ -121,7 +120,7 @@ public class AutoUpdatedBossBar implements Listener {
if (playerCondition != null && !playerCondition.test(event.getPlayer())) if (playerCondition != null && !playerCondition.test(event.getPlayer()))
return; return;
synchronized (bar) { synchronized (bar) {
bar.addPlayer(event.getPlayer()); event.getPlayer().showBossBar(bar);
} }
} }
@ -130,7 +129,18 @@ public class AutoUpdatedBossBar implements Listener {
if (followPlayerList == false) if (followPlayerList == false)
return; return;
synchronized (bar) { synchronized (bar) {
bar.removePlayer(event.getPlayer()); event.getPlayer().hideBossBar(bar);
}
}
/**
* Utility method to update the progress of the bossbar without unnecessary packet.
* @param title
*/
public void removeAll() {
synchronized (bar) {
for (Player p : Bukkit.getOnlinePlayers())
p.hideBossBar(bar);
} }
} }
@ -163,21 +173,24 @@ public class AutoUpdatedBossBar implements Listener {
* Utility method to update the title of the bossbar without unnecessary packet. * Utility method to update the title of the bossbar without unnecessary packet.
* @param title * @param title
*/ */
public void setTitle(String title) { public void setTitle(Chat title) {
synchronized (bar) { synchronized (bar) {
if (!Objects.equals(title, bar.getTitle())) bar.name(title); // already check if the title is the same
bar.setTitle(title);
} }
} }
@Deprecated
public void setTitle(String title) {
setTitle(Chat.legacyText(title));
}
/** /**
* Utility method to update the color of the bossbar without unnecessary packet. * Utility method to update the color of the bossbar without unnecessary packet.
* @param title * @param title
*/ */
public void setColor(BarColor color) { public void setColor(Color color) {
synchronized (bar) { synchronized (bar) {
if (color != bar.getColor()) bar.color(color);
bar.setColor(color);
} }
} }
@ -187,8 +200,7 @@ public class AutoUpdatedBossBar implements Listener {
*/ */
public void setProgress(double progress) { public void setProgress(double progress) {
synchronized (bar) { synchronized (bar) {
if (progress != bar.getProgress()) bar.progress((float) progress);
bar.setProgress(progress);
} }
} }