Dateien nach "src/main/java/de/nexuslobby/modules/bossbar" hochladen
This commit is contained in:
127
src/main/java/de/nexuslobby/modules/bossbar/BossBarModule.java
Normal file
127
src/main/java/de/nexuslobby/modules/bossbar/BossBarModule.java
Normal file
@@ -0,0 +1,127 @@
|
|||||||
|
package de.nexuslobby.modules.bossbar;
|
||||||
|
|
||||||
|
import de.nexuslobby.NexusLobby;
|
||||||
|
import de.nexuslobby.api.Module;
|
||||||
|
import me.clip.placeholderapi.PlaceholderAPI;
|
||||||
|
import org.bukkit.Bukkit;
|
||||||
|
import org.bukkit.boss.BarColor;
|
||||||
|
import org.bukkit.boss.BarStyle;
|
||||||
|
import org.bukkit.boss.BossBar;
|
||||||
|
import org.bukkit.configuration.file.FileConfiguration;
|
||||||
|
import org.bukkit.entity.Player;
|
||||||
|
import org.bukkit.scheduler.BukkitRunnable;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class BossBarModule implements Module {
|
||||||
|
|
||||||
|
private BossBar bossBar;
|
||||||
|
private int messageIndex = 0;
|
||||||
|
private int charIndex = 0;
|
||||||
|
private int pauseCounter = 0;
|
||||||
|
private boolean deleting = false;
|
||||||
|
private BukkitRunnable task;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getName() { return "BossBar"; }
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onEnable() {
|
||||||
|
FileConfiguration vConfig = NexusLobby.getInstance().getVisualsConfig();
|
||||||
|
if (!vConfig.getBoolean("bossbar.enabled", true)) return;
|
||||||
|
|
||||||
|
List<String> messages = vConfig.getStringList("bossbar.messages");
|
||||||
|
if (messages.isEmpty()) return;
|
||||||
|
|
||||||
|
String configColor = vConfig.getString("bossbar.color", "YELLOW").toUpperCase();
|
||||||
|
String configStyle = vConfig.getString("bossbar.style", "SOLID").toUpperCase();
|
||||||
|
|
||||||
|
BarColor barColor;
|
||||||
|
try {
|
||||||
|
if (configColor.equals("GOLD")) configColor = "YELLOW";
|
||||||
|
barColor = BarColor.valueOf(configColor);
|
||||||
|
} catch (IllegalArgumentException e) {
|
||||||
|
barColor = BarColor.YELLOW;
|
||||||
|
}
|
||||||
|
|
||||||
|
BarStyle barStyle;
|
||||||
|
try {
|
||||||
|
barStyle = BarStyle.valueOf(configStyle);
|
||||||
|
} catch (IllegalArgumentException e) {
|
||||||
|
barStyle = BarStyle.SOLID;
|
||||||
|
}
|
||||||
|
|
||||||
|
bossBar = Bukkit.createBossBar("", barColor, barStyle);
|
||||||
|
bossBar.setVisible(true);
|
||||||
|
|
||||||
|
startTypewriterTask(messages, vConfig);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void startTypewriterTask(List<String> messages, FileConfiguration vConfig) {
|
||||||
|
task = new BukkitRunnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
for (Player player : Bukkit.getOnlinePlayers()) {
|
||||||
|
if (!bossBar.getPlayers().contains(player)) {
|
||||||
|
bossBar.addPlayer(player);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
String currentFullMessage = messages.get(messageIndex);
|
||||||
|
currentFullMessage = replacePlaceholders(currentFullMessage);
|
||||||
|
|
||||||
|
if (!deleting) {
|
||||||
|
if (charIndex < currentFullMessage.length()) {
|
||||||
|
charIndex++;
|
||||||
|
} else {
|
||||||
|
pauseCounter++;
|
||||||
|
// Nutzt den Wert aus visuals.yml
|
||||||
|
int pauseTime = vConfig.getInt("bossbar.pause-seconds", 4) * 7;
|
||||||
|
if (pauseCounter > pauseTime) {
|
||||||
|
deleting = true;
|
||||||
|
pauseCounter = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (charIndex > 0) {
|
||||||
|
charIndex--;
|
||||||
|
} else {
|
||||||
|
deleting = false;
|
||||||
|
messageIndex = (messageIndex + 1) % messages.size();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (charIndex > 0 && charIndex <= currentFullMessage.length()) {
|
||||||
|
String displayedText = currentFullMessage.substring(0, charIndex);
|
||||||
|
bossBar.setTitle(colorize(displayedText));
|
||||||
|
} else if (charIndex == 0) {
|
||||||
|
bossBar.setTitle("");
|
||||||
|
}
|
||||||
|
|
||||||
|
double progress = (double) charIndex / Math.max(1, currentFullMessage.length());
|
||||||
|
bossBar.setProgress(Math.min(1.0, progress));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
task.runTaskTimer(NexusLobby.getInstance(), 0L, 3L);
|
||||||
|
}
|
||||||
|
|
||||||
|
private String replacePlaceholders(String text) {
|
||||||
|
text = text.replace("{online}", String.valueOf(Bukkit.getOnlinePlayers().size()));
|
||||||
|
if (Bukkit.getPluginManager().getPlugin("PlaceholderAPI") != null) {
|
||||||
|
text = PlaceholderAPI.setPlaceholders(null, text);
|
||||||
|
}
|
||||||
|
return text;
|
||||||
|
}
|
||||||
|
|
||||||
|
private String colorize(String s) {
|
||||||
|
return s == null ? "" : s.replace("&", "§");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onDisable() {
|
||||||
|
if (task != null) task.cancel();
|
||||||
|
if (bossBar != null) {
|
||||||
|
bossBar.removeAll();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user