Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
<!-- Do not change unless you want different name for local builds. -->
<build.number>-LOCAL</build.number>
<!-- This allows to change between versions. -->
<build.version>1.18.3</build.version>
<build.version>1.19.0</build.version>
<!-- SonarCloud -->
<sonar.projectKey>BentoBoxWorld_AOneBlock</sonar.projectKey>
<sonar.organization>bentobox-world</sonar.organization>
Expand Down
15 changes: 14 additions & 1 deletion src/main/java/world/bentobox/aoneblock/AOneBlock.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import org.bukkit.World;
import org.bukkit.World.Environment;
import org.bukkit.WorldCreator;
import org.bukkit.WorldType;
import org.bukkit.entity.SpawnCategory;
import org.bukkit.generator.ChunkGenerator;
import org.eclipse.jdt.annotation.NonNull;
Expand All @@ -24,6 +23,7 @@
import world.bentobox.aoneblock.listeners.InfoListener;
import world.bentobox.aoneblock.listeners.ItemsAdderListener;
import world.bentobox.aoneblock.listeners.JoinLeaveListener;
import world.bentobox.aoneblock.listeners.BossBarListener;
import world.bentobox.aoneblock.listeners.NoBlockHandler;
import world.bentobox.aoneblock.listeners.StartSafetyListener;
import world.bentobox.aoneblock.oneblocks.OneBlockCustomBlockCreator;
Expand Down Expand Up @@ -66,6 +66,9 @@ public class AOneBlock extends GameModeAddon {
.listener(new StartSafetyListener(this))
.defaultSetting(false)
.build();
private BossBarListener bossBar = new BossBarListener(this);
public final Flag BOSSBAR = new Flag.Builder("BOSSBAR", Material.DRAGON_HEAD).mode(Mode.BASIC)
.type(Type.SETTING).listener(bossBar).defaultSetting(true).build();

@Override
public void onLoad() {
Expand All @@ -89,6 +92,8 @@ public void onLoad() {
// Register flag with BentoBox
// Register protection flag with BentoBox
getPlugin().getFlagsManager().registerFlag(this, START_SAFETY);
// Bossbar
getPlugin().getFlagsManager().registerFlag(this, this.BOSSBAR);
}
}

Expand Down Expand Up @@ -120,6 +125,7 @@ public void onEnable() {
registerListener(new BlockProtect(this));
registerListener(new JoinLeaveListener(this));
registerListener(new InfoListener(this));
registerListener(bossBar);
// Register placeholders
phManager = new AOneBlockPlaceholders(this, getPlugin().getPlaceholdersManager());

Expand Down Expand Up @@ -335,4 +341,11 @@ public void setSettings(Settings settings) {
this.settings = settings;
}

/**
* @return the bossBar
*/
public BossBarListener getBossBar() {
return bossBar;
}

}
16 changes: 16 additions & 0 deletions src/main/java/world/bentobox/aoneblock/Settings.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ public class Settings implements WorldSettings {
@ConfigEntry(path = "aoneblock.command.set-count-command", since = "1.10.0")
private String setCountCommand = "setCount";

@ConfigComment("The command label that toggers the progress bar.")
@ConfigComment("By default it is 'bossbar'.")
@ConfigEntry(path = "aoneblock.command.bossbar-command", since = "1.19.0")
private String bossBarCommand = "bossbar";

@ConfigComment("How long a player must wait until they can use the setCount command again. In minutes.")
@ConfigComment("This is the command that is run from the phases panel.")
@ConfigEntry(path = "aoneblock.command.set-count-cooldown", since = "1.13.0")
Expand Down Expand Up @@ -2236,4 +2241,15 @@ public void setConcurrentIslands(int concurrentIslands) {
this.concurrentIslands = concurrentIslands;
}

public String getBossBarCommand() {
return bossBarCommand;
}

/**
* @param bossBarCommand the bossBarCommand to set
*/
public void setBossBarCommand(String bossBarCommand) {
this.bossBarCommand = bossBarCommand;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package world.bentobox.aoneblock.commands.island;

import java.util.List;

import world.bentobox.aoneblock.AOneBlock;
import world.bentobox.bentobox.api.commands.CompositeCommand;
import world.bentobox.bentobox.api.user.User;

public class IslandBossBarCommand extends CompositeCommand {

private AOneBlock addon;

public IslandBossBarCommand(CompositeCommand islandCommand, String label, String[] aliases)
{
super(islandCommand, label, aliases);
}

@Override
public void setup() {
setDescription("aoneblock.commands.island.bossbar.description");
setOnlyPlayer(true);
// Permission
setPermission("island.bossbar");
addon = getAddon();
}

@Override
public boolean execute(User user, String label, List<String> args) {
addon.getBossBar().toggleUser(user);
getIslands().getIslandAt(user.getLocation()).ifPresent(i -> {
if (!i.isAllowed(addon.BOSSBAR)) {
user.sendMessage("aoneblock.bossbar.not-active");
}
});
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,8 @@ public void setup() {
new IslandRespawnBlockCommand(this,
settings.getRespawnBlockCommand().split(" ")[0],
settings.getRespawnBlockCommand().split(" "));
// Boss bar
new IslandBossBarCommand(this, settings.getBossBarCommand().split(" ")[0],
settings.getBossBarCommand().split(" "));
}
}
189 changes: 189 additions & 0 deletions src/main/java/world/bentobox/aoneblock/listeners/BossBarListener.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
package world.bentobox.aoneblock.listeners;

import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.UUID;

import org.bukkit.Bukkit;
import org.bukkit.boss.BarColor;
import org.bukkit.boss.BarStyle;
import org.bukkit.boss.BossBar;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.eclipse.jdt.annotation.NonNull;

import world.bentobox.aoneblock.AOneBlock;
import world.bentobox.aoneblock.dataobjects.OneBlockIslands;
import world.bentobox.aoneblock.events.MagicBlockEvent;
import world.bentobox.bentobox.api.events.flags.FlagSettingChangeEvent;
import world.bentobox.bentobox.api.events.island.IslandEnterEvent;
import world.bentobox.bentobox.api.events.island.IslandExitEvent;
import world.bentobox.bentobox.api.metadata.MetaDataValue;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.database.objects.Island;

public class BossBarListener implements Listener {

private static final String AONEBLOCK_BOSSBAR = "aoneblock.bossbar";

public BossBarListener(AOneBlock addon) {
super();
this.addon = addon;
}

private AOneBlock addon;

// Store a boss bar for each player (using their UUID)
private final Map<Island, BossBar> islandBossBars = new HashMap<>();

@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
public void onBreakBlockEvent(MagicBlockEvent e) {
// Update boss bar
tryToShowBossBar(e.getPlayerUUID(), e.getIsland());
}

@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
public void onEnterIsland(IslandEnterEvent event) {
if (addon.inWorld(event.getIsland().getWorld())) {
tryToShowBossBar(event.getPlayerUUID(), event.getIsland());
}
}

@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
public void onFlagChange(FlagSettingChangeEvent e) {
if (e.getEditedFlag() == addon.BOSSBAR) {
// Show to players on island. If it isn't allowed then this will clean up the boss bar too
e.getIsland().getPlayersOnIsland().stream().map(Player::getUniqueId)
.forEach(uuid -> this.tryToShowBossBar(uuid, e.getIsland()));
}
}

/**
* Try to show the bossbar to the player
* @param uuid player's UUID
* @param island island they are on
*/
private void tryToShowBossBar(UUID uuid, Island island) {
User user = User.getInstance(uuid);

// Only show if enabled for island
if (!island.isAllowed(addon.BOSSBAR)) {
BossBar removed = islandBossBars.remove(island);
if (removed != null) {
// Remove all players from the boss bar
removed.removeAll();
}
return;
}
// Default to showing boss bar unless it is explicitly turned off
if (!user.getMetaData(AONEBLOCK_BOSSBAR).map(MetaDataValue::asBoolean).orElse(true)) {
// Remove any boss bar from user if they are in the world
removeBar(user, island);
// Do not show a boss bar
return;
}
// Prepare boss bar
String title = user.getTranslationOrNothing("aoneblock.bossbar.title");
BarColor c;
try {
c = BarColor.valueOf(user.getTranslation("aoneblock.bossbar.color").toUpperCase(Locale.ENGLISH));
} catch (Exception e) {
c = BarColor.RED;
addon.logError("Bossbar color unknown. Pick from RED, WHITE, PINK, BLUE, GREEN, YELLOW, or PURPLE");
}
BarStyle s = BarStyle.SOLID;
try {
s = BarStyle.valueOf(user.getTranslation("aoneblock.bossbar.style").toUpperCase(Locale.ENGLISH));
} catch (Exception e) {
s = BarStyle.SOLID;
addon.logError(
"Bossbar style unknow. Pick from SOLID, SEGMENTED_6, SEGMENTED_10, SEGMENTED_12, SEGMENTED_20");
}
// Get it or make it
BossBar bar = this.islandBossBars.getOrDefault(island,
Bukkit.createBossBar(title, c, s));
// Get the progress
@NonNull
OneBlockIslands obi = addon.getOneBlocksIsland(island);

// Set progress
bar.setProgress(addon.getOneBlockManager().getPercentageDone(obi) / 100);
int numBlocksToGo = addon.getOneBlockManager().getNextPhaseBlocks(obi);
int phaseBlocks = addon.getOneBlockManager().getPhaseBlocks(obi);
int done = phaseBlocks - numBlocksToGo;
String translation = user.getTranslationOrNothing("aoneblock.bossbar.status", "[togo]",
String.valueOf(numBlocksToGo), "[total]", String.valueOf(phaseBlocks), "[done]", String.valueOf(done));
bar.setTitle(translation);
// Add to user if they don't have it already
Player player = Bukkit.getPlayer(uuid);
if (!bar.getPlayers().contains(player)) {
bar.addPlayer(player);
}
// Save the boss bar for later reference (e.g., when updating or removing)
islandBossBars.put(island, bar);

}

private void removeBar(User user, Island island) {
if (!addon.inWorld(island.getWorld()) || !user.isPlayer() || !user.isOnline()) {
return;
}
BossBar bossBar = islandBossBars.get(island);
if (bossBar != null) {
bossBar.removePlayer(user.getPlayer());
if (bossBar.getPlayers().isEmpty()) {
// Clean up
islandBossBars.remove(island);
}
}

}

@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
public void onExitIsland(IslandExitEvent event) {
User user = User.getInstance(event.getPlayerUUID());
removeBar(user, event.getIsland());
}

@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
public void onJoin(PlayerJoinEvent e) {
// If the player is on an island then show the bar
if (!addon.inWorld(e.getPlayer().getLocation())) {
return;
}
addon.getIslands().getIslandAt(e.getPlayer().getLocation())
.ifPresent(is -> this.tryToShowBossBar(e.getPlayer().getUniqueId(), is));
}

@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
public void onQuit(PlayerQuitEvent e) {
// Clean up boss bars
islandBossBars.values().stream().forEach(bb -> bb.removePlayer(e.getPlayer()));
islandBossBars.values().removeIf(bb -> bb.getPlayers().isEmpty());
}

/**
* User-level boss bar control.
* @param user user to toggle
*/
public void toggleUser(User user) {
boolean newState = !user.getMetaData(AONEBLOCK_BOSSBAR).map(MetaDataValue::asBoolean).orElse(true);
user.putMetaData(AONEBLOCK_BOSSBAR, new MetaDataValue(newState));
if (newState) {
// If the player is on an island then show the bar
addon.getIslands().getIslandAt(user.getLocation()).filter(is -> addon.inWorld(is.getWorld()))
.ifPresent(is -> this.tryToShowBossBar(user.getUniqueId(), is));
user.sendMessage("aoneblock.commands.island.bossbar.status_on");
} else {
// Remove player from any boss bars. Adding happens automatically
islandBossBars.forEach((k, v) -> v.removePlayer(user.getPlayer()));
user.sendMessage("aoneblock.commands.island.bossbar.status_off");
}
}

}
3 changes: 3 additions & 0 deletions src/main/resources/addon.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ permissions:
aoneblock.island:
description: Allow use of '/ob' command - the main island command
default: TRUE
aoneblock.island.bossbar:
description: Allow use of '/ob bossbar' command - toggle the bossbar
default: FALSE
aoneblock.island.home:
description: Allow use of '/ob go' command - teleport you to your island
default: TRUE
Expand Down
Loading