下面列出了怎么用org.bukkit.boss.BossBar的API类实例代码及写法,或者点击链接到github查看源代码。
private void add(Player player, LugolsIodineEffect.Effect effect) {
Objects.requireNonNull(player, "player");
Objects.requireNonNull(effect, "effect");
BossBar bossBar = this.displayMap.computeIfAbsent(player.getUniqueId(), playerId -> this.createBossBar());
bossBar.setProgress(effect.getSecondsLeft() / (double) effect.getInitialSeconds());
bossBar.addPlayer(player);
}
public static void hideDurabilityBossBar(Player player, EquipmentSlot slot) {
UUID uuid = player.getUniqueId();
BossBar bar;
HashMap<UUID, BossBar> playersBars;
if (slot.equals(EquipmentSlot.HAND)) {
playersBars = playersBarsMain;
} else if (slot.equals(EquipmentSlot.OFF_HAND)) {
playersBars = playersBarsOff;
} else {
return;
}
if (!playersBars.containsKey(uuid)) {
return;
} else {
bar = playersBars.get(uuid);
}
bar.setVisible(false);
bar.setProgress(1.0D);
bar.setColor(BarColor.GREEN);
}
public void disable() {
HandlerList.unregisterAll(this);
if (this.task != null) {
this.task.cancel();
}
this.displayMap.values().forEach(BossBar::removeAll);
this.displayMap.clear();
}
@EventHandler(priority = EventPriority.MONITOR)
public void onPlayerQuit(PlayerQuitEvent event) {
Player player = event.getPlayer();
BossBar bossBar = this.displayMap.get(player.getUniqueId());
if (bossBar != null) {
bossBar.removePlayer(player);
}
}
private void remove(Player player) {
Objects.requireNonNull(player, "player");
UUID playerId = player.getUniqueId();
BossBar bossBar = this.displayMap.get(playerId);
if (bossBar != null) {
bossBar.removePlayer(player);
this.displayMap.remove(playerId);
}
}
public static void sendBossBarMessage(final World world, final String message, final String color, final double percentage) {
if (!Config.getBoolean("messages.bossbar.enabled") || message.length() < 1) return;
BossBar bar = Bukkit.createBossBar(Messages.prepareMessage(world, message), getBarColor(color), BarStyle.SOLID);
bar.setProgress(percentage);
world.getPlayers().forEach(bar::addPlayer);
Bukkit.getScheduler().runTaskLater(Harbor.getHarbor(), bar::removeAll, Config.getInteger("interval") * 20);
}
@EventHandler(priority = EventPriority.HIGHEST)
public void join(final PlayerJoinEvent event) {
Player player = event.getPlayer();
resetPlayer(player);
event.getPlayer().addAttachment(lobby, Permissions.OBSERVER, true);
if (player.hasPermission("lobby.overhead-news")) {
final String datacenter = minecraftService.getLocalServer().datacenter();
final Component news = new Component(ChatColor.GREEN)
.extra(new TranslatableComponent(
"lobby.news",
new Component(ChatColor.GOLD, ChatColor.BOLD).extra(generalFormatter.publicHostname())
));
final BossBar bar = bossBarFactory.createBossBar(renderer.render(news, player), BarColor.BLUE, BarStyle.SOLID);
bar.setProgress(1);
bar.addPlayer(player);
bar.show();
}
if(!player.hasPermission("lobby.disabled-permissions-exempt")) {
for(PermissionAttachmentInfo attachment : player.getEffectivePermissions()) {
if(config.getDisabledPermissions().contains(attachment.getPermission())) {
attachment.getAttachment().setPermission(attachment.getPermission(), false);
}
}
}
int count = lobby.getServer().getOnlinePlayers().size();
if(!lobby.getServer().getOnlinePlayers().contains(event.getPlayer())) count++;
minecraftService.updateLocalServer(new SignUpdate(count));
}
@Override
public void addPlayer(Player player) {
if(!views.containsKey(player)) {
final BossBar view = bossBarFactory.createBossBar(renderer.render(title, player), color, style, flags.toArray(new BarFlag[flags.size()]));
view.setVisible(visibile);
view.addPlayer(player);
views.put(player, view);
}
}
@HandleModel
public void ticketUpdated(@Nullable Ticket before, @Nullable Ticket after, Ticket latest) {
final Arena arena = arenas.byId(latest.arena_id());
updateArena(arena);
final Player player = userStore.find(latest.user());
if(player != null) {
final BossBar bar = bars.getUnchecked(arena);
if(after != null && after.server_id() == null) {
bar.addPlayer(player);
} else {
bar.removePlayer(player);
}
}
}
private static void removeBar(final BossBar bar, final Player p) {
final int task = Bukkit.getScheduler().scheduleSyncRepeatingTask(RedProtect.get(), () -> {
double d = bar.getProgress();
if (d >= 0.2) {
bar.setProgress(d - 0.2);
}
}, 20, 20);
Bukkit.getScheduler().runTaskLater(RedProtect.get(), () -> {
bar.removePlayer(p);
Bukkit.getScheduler().cancelTask(task);
}, 120);
}
/**
* Creates a boss bar if doesn't exist
*
* @param player the player
* @return the boss bar
*/
private BossBar createIfNotExists(Player player) {
if(bossBars.containsKey(player.getUniqueId())) {
return getBossBar(player);
}
BossBar bossBar = Bukkit.getServer().createBossBar("", Config.BOSSBAR_RAIDBAR_COLOR.toEnum(BarColor.class), Config.BOSSBAR_RAIDBAR_STYLE.toEnum(BarStyle.class));
bossBar.addPlayer(player);
bossBars.put(player.getUniqueId(), bossBar);
return bossBar;
}
/**
* Saves a bossbar in the BossBarManager for future use.
*
* @param id The ID text for the bossbar, duplicate ID's will be over written.
* @param bossbar The bossbar object to be stored.
*/
void createBossBar(String id, BossBar bossbar) {
if (barMap.containsKey(id)) {
barMap.get(id).removeAll();
barMap.remove(id);
}
barMap.put(id, bossbar);
}
/**
* Add a flag to a bossbar in the BossBarManager through the stored ID.
*
* @param id The ID text for the bossbar.
* @param flag The BarFlag to be added.
*/
void addFlag(String id, BarFlag flag) {
BossBar bar = barMap.get(id);
if (bar != null) {
bar.addFlag(flag);
barMap.put(id, bar);
}
}
/**
* Remove a flag from a bossbar in the BossBarManager through the stored ID.
*
* @param id The ID text for the bossbar.
* @param flag The BarFlag to be added.
*/
void removeFlag(String id, BarFlag flag) {
BossBar bar = barMap.get(id);
if (bar != null) {
bar.removeFlag(flag);
barMap.put(id, bar);
}
}
/**
* Add an array of players to a bossbar in the BossBarManager through the stored ID.
*
* @param id The ID text for the bossbar.
* @param players Array of players to be added to the bossbar
*/
void addPlayers(String id, Player[] players) {
BossBar bar = barMap.get(id);
if (bar != null) {
for (Player p : players) {
bar.addPlayer(p);
}
barMap.put(id, bar);
}
}
/**
* Remove an array of players from a bossbar in the BossBarManager through the stored ID.
*
* @param id The ID text for the bossbar.
* @param players Array of players to be removed from the bossbar
*/
void removePlayers(String id, Player[] players) {
BossBar bar = barMap.get(id);
if (bar != null) {
for (Player p : players) {
bar.removePlayer(p);
}
barMap.put(id, bar);
}
}
/**
* Remove a bossbar from the BossBarManager and remove it from all players who were assigned it.
*
* @param id The ID text for the bossbar.
*/
void removeBar(String id) {
BossBar bar = barMap.get(id);
if (bar != null) {
bar.removeAll();
barMap.remove(id);
}
}
/**
* Changed the title of a bossbar from the BossBarManager through the stored ID.
*
* @param id The ID text for the bossbar.
* @param title The new title for the bossbar with color codes.
*/
void changeTitle(String id, String title) {
BossBar bar = barMap.get(id);
if (bar != null) {
bar.setTitle(title);
barMap.put(id, bar);
}
}
/**
* Changed the progress or fill of a bossbar from the BossBarManager through the stored ID.
*
* @param id The ID text for the bossbar.
* @param progress The progress or fill to be set from 0 - 100.
*/
void changeValue(String id, double progress) {
BossBar bar = barMap.get(id);
if (bar != null) {
if (progress > 100) {
progress = 100;
} else if (progress < 0) {
progress = 0;
}
bar.setProgress(progress / 100);
barMap.put(id, bar);
}
}
/**
* Changed the color of a bossbar from the BossBarManager through the stored ID.
*
* @param id The ID text for the bossbar.
* @param color The BarColor to be used.
*/
void changeColor(String id, BarColor color) {
BossBar bar = barMap.get(id);
if (bar != null) {
bar.setColor(color);
barMap.put(id, bar);
}
}
/**
* Changed the style of a bossbar from the BossBarManager through the stored ID.
*
* @param id The ID text for the bossbar.
* @param style The BarStyle to be used.
*/
void changeStyle(String id, BarStyle style) {
BossBar bar = barMap.get(id);
if (bar != null) {
bar.setStyle(style);
barMap.put(id, bar);
}
}
/**
* Hide a bossbar from the BossBarManager through the stored ID for all players
* who were assigned it.
*
* @param id The ID text for the bossbar.
*/
void hideBar(String id) {
BossBar bar = barMap.get(id);
if (bar != null) {
bar.setVisible(false);
barMap.put(id, bar);
}
}
/**
* Show a bossbar from the BossBarManager through the stored ID for all players
* who were assigned it.
*
* @param id The ID text for the bossbar.
*/
void showBar(String id) {
BossBar bar = barMap.get(id);
if (bar != null) {
bar.setVisible(true);
barMap.put(id, bar);
}
}
/**
* Get the title of a stored bossbar from the BossBarManager through the ID.
*
* @param id The ID text for the bossbar.
* @return Title of the bossbar
*/
String getBarTitle(String id) {
BossBar bar = barMap.get(id);
if (bar != null) {
return bar.getTitle();
}
return null;
}
/**
* Get the progress or fill of a stored bossbar from the BossBarManager through the ID.
*
* @param id The ID text for the bossbar.
* @return The progress or fill of the bossbar from 0 - 100.
*/
Number getBarProgress(String id) {
BossBar bar = barMap.get(id);
if (bar != null) {
return bar.getProgress();
}
return null;
}
@Override
protected void execute(Event evt) {
BarStyle barStyle = BarStyle.SOLID;
BarColor barColor = BarColor.PURPLE;
if (style != null) {
barStyle = style.getSingle(evt).getKey();
}
if (color != null) {
barColor = color.getSingle(evt).getKey();
}
BossBar bar;
if (flag != null) {
bar = Bukkit.createBossBar(title.getSingle(evt).replace("\"", ""), barColor, barStyle,
flag.getSingle(evt).getKey());
} else {
bar = Bukkit.createBossBar(title.getSingle(evt).replace("\"", ""), barColor, barStyle);
}
if (value != null) {
double vol = value.getSingle(evt).doubleValue();
if (vol > 100) {
vol = 100;
} else if (vol < 0) {
vol = 0;
}
bar.setProgress(vol / 100);
}
for (Player p : players.getAll(evt)) {
bar.addPlayer(p);
}
Core.bossbarManager.createBossBar(id.getSingle(evt).replace("\"", ""), bar);
}
private void remBossBar(Player player) {
BossBar bossBar = playerBars.getOrDefault(player.getUniqueId(), null);
if (bossBar != null) {
bossBar.removePlayer(player);
playerBars.remove(player.getUniqueId());
}
}
public void setColor(BarColor color) {
if (color != this.color) {
this.color = color;
for (BossBar bossbar : playerBossBars.values()) {
bossbar.setColor(color);
}
}
}
public void removeFlag(BarFlag flag) {
if (this.flags.contains(flag)) {
this.flags.remove(flag);
for (BossBar bossbar : playerBossBars.values()) {
bossbar.setFlags(this.flags);
}
}
}
public void addFlag(BarFlag flag) {
if (!this.flags.contains(flag)) {
this.flags.remove(flag);
for (BossBar bossbar : playerBossBars.values()) {
bossbar.setFlags(this.flags);
}
}
}