Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Added bossbar implementations * Added bossbar component to the messages file * Added limit to the amount of active bossbars at a time for players * Added bossbar service to the API * Moved bossbar utility classes under the bossbar service package * Merge branch 'dev' into feature/player-roles-display-name # Conflicts: # src/main/java/com/bgsoftware/superiorskyblock/service/placeholders/PlaceholdersServiceImpl.java
- Loading branch information
1 parent
0a424be
commit afdc7b7
Showing
20 changed files
with
521 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
API/src/main/java/com/bgsoftware/superiorskyblock/api/service/bossbar/BossBar.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package com.bgsoftware.superiorskyblock.api.service.bossbar; | ||
|
||
import org.bukkit.entity.Player; | ||
|
||
public interface BossBar { | ||
|
||
/** | ||
* Display this boss-bar to a player. | ||
* | ||
* @param player The player to display the boss-bar to. | ||
*/ | ||
void addPlayer(Player player); | ||
|
||
/** | ||
* Stop displaying this boss-bar to all the players. | ||
*/ | ||
void removeAll(); | ||
|
||
/** | ||
* Set the progress bar of this boss-bar. | ||
* | ||
* @param progress The progress to set. | ||
*/ | ||
void setProgress(double progress); | ||
|
||
/** | ||
* Get the progress bar of this boss-bar. | ||
*/ | ||
double getProgress(); | ||
|
||
enum Color { | ||
|
||
PINK, | ||
BLUE, | ||
RED, | ||
GREEN, | ||
YELLOW, | ||
PURPLE, | ||
WHITE | ||
|
||
} | ||
|
||
} |
18 changes: 18 additions & 0 deletions
18
API/src/main/java/com/bgsoftware/superiorskyblock/api/service/bossbar/BossBarsService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.bgsoftware.superiorskyblock.api.service.bossbar; | ||
|
||
import org.bukkit.entity.Player; | ||
|
||
public interface BossBarsService { | ||
|
||
/** | ||
* Create a new boss-bar. | ||
* | ||
* @param player The player to create the boss-bar for. | ||
* @param message The message to display in the boss-bar. | ||
* @param color The color of the boss-bar. | ||
* @param ticksToRun The time to run the boss-bar. | ||
* If set to 0 or below, it will stay forever. | ||
*/ | ||
BossBar createBossBar(Player player, String message, BossBar.Color color, double ticksToRun); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
src/main/java/com/bgsoftware/superiorskyblock/lang/component/impl/BossBarComponent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
|
||
package com.bgsoftware.superiorskyblock.lang.component.impl; | ||
|
||
import com.bgsoftware.superiorskyblock.SuperiorSkyblockPlugin; | ||
import com.bgsoftware.superiorskyblock.api.service.bossbar.BossBar; | ||
import com.bgsoftware.superiorskyblock.lang.component.EmptyMessageComponent; | ||
import com.bgsoftware.superiorskyblock.lang.component.IMessageComponent; | ||
import org.apache.logging.log4j.util.Strings; | ||
import org.bukkit.command.CommandSender; | ||
import org.bukkit.entity.Player; | ||
|
||
import javax.annotation.Nullable; | ||
|
||
public final class BossBarComponent implements IMessageComponent { | ||
|
||
private static final SuperiorSkyblockPlugin plugin = SuperiorSkyblockPlugin.getPlugin(); | ||
|
||
private final String message; | ||
private final BossBar.Color color; | ||
private final int ticksToRun; | ||
|
||
public static IMessageComponent of(@Nullable String message, BossBar.Color color, int ticks) { | ||
return ticks <= 0 || Strings.isBlank(message) ? EmptyMessageComponent.getInstance() : new BossBarComponent(message, color, ticks); | ||
} | ||
|
||
private BossBarComponent(String message, BossBar.Color color, int ticks) { | ||
this.message = message; | ||
this.color = color; | ||
this.ticksToRun = ticks; | ||
} | ||
|
||
@Override | ||
public String getMessage() { | ||
return this.message; | ||
} | ||
|
||
@Override | ||
public void sendMessage(CommandSender sender, Object... objects) { | ||
if (sender instanceof Player) { | ||
IMessageComponent.replaceArgs(this.message, objects).ifPresent(message -> { | ||
plugin.getServices().getBossBarsService().createBossBar((Player) sender, message, this.color, this.ticksToRun); | ||
}); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
src/main/java/com/bgsoftware/superiorskyblock/service/bossbar/BossBarTask.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package com.bgsoftware.superiorskyblock.service.bossbar; | ||
|
||
import com.bgsoftware.superiorskyblock.SuperiorSkyblockPlugin; | ||
import com.bgsoftware.superiorskyblock.api.service.bossbar.BossBar; | ||
import org.bukkit.entity.Player; | ||
import org.bukkit.scheduler.BukkitRunnable; | ||
|
||
import java.util.HashMap; | ||
import java.util.LinkedList; | ||
import java.util.Map; | ||
import java.util.Queue; | ||
import java.util.UUID; | ||
|
||
public final class BossBarTask extends BukkitRunnable { | ||
|
||
private static final SuperiorSkyblockPlugin plugin = SuperiorSkyblockPlugin.getPlugin(); | ||
|
||
private static final BossBarTask EMPTY_TASK = new BossBarTask(EmptyBossBar.getInstance(), 0); | ||
|
||
private static final Map<UUID, Queue<BossBarTask>> PLAYERS_RUNNING_TASKS = new HashMap<>(); | ||
|
||
private final BossBar bossBar; | ||
private final double progressToRemovePerTick; | ||
private boolean reachedEndTask = false; | ||
|
||
public static BossBarTask create(BossBar bossBar, double ticksToRun) { | ||
return ticksToRun <= 0 ? EMPTY_TASK : new BossBarTask(bossBar, ticksToRun); | ||
} | ||
|
||
private BossBarTask(BossBar bossBar, double ticksToRun) { | ||
this.bossBar = bossBar; | ||
this.progressToRemovePerTick = this.bossBar.getProgress() / ticksToRun; | ||
if (progressToRemovePerTick > 0) { | ||
runTaskTimer(plugin, 1L, 1L); | ||
} | ||
} | ||
|
||
@Override | ||
public void run() { | ||
if (reachedEndTask) { | ||
cancel(); | ||
} else { | ||
this.bossBar.setProgress(Math.max(0D, this.bossBar.getProgress() - progressToRemovePerTick)); | ||
reachedEndTask = this.bossBar.getProgress() == 0D; | ||
} | ||
} | ||
|
||
@Override | ||
public synchronized void cancel() throws IllegalStateException { | ||
this.bossBar.removeAll(); | ||
super.cancel(); | ||
} | ||
|
||
public void registerTask(Player player) { | ||
Queue<BossBarTask> bossBarTasks = PLAYERS_RUNNING_TASKS.computeIfAbsent(player.getUniqueId(), s -> new LinkedList<>()); | ||
|
||
if (bossBarTasks.size() >= plugin.getSettings().getBossbarLimit()) { | ||
BossBarTask lastRunningTask = bossBarTasks.poll(); | ||
if (lastRunningTask != null) | ||
lastRunningTask.cancel(); | ||
} | ||
|
||
bossBarTasks.add(this); | ||
} | ||
|
||
public void unregisterTask(Player player) { | ||
Queue<BossBarTask> bossBarTasks = PLAYERS_RUNNING_TASKS.get(player.getUniqueId()); | ||
|
||
if (bossBarTasks == null) | ||
return; | ||
|
||
bossBarTasks.remove(this); | ||
} | ||
|
||
} |
21 changes: 21 additions & 0 deletions
21
src/main/java/com/bgsoftware/superiorskyblock/service/bossbar/BossBarsServiceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package com.bgsoftware.superiorskyblock.service.bossbar; | ||
|
||
import com.bgsoftware.superiorskyblock.SuperiorSkyblockPlugin; | ||
import com.bgsoftware.superiorskyblock.api.service.bossbar.BossBar; | ||
import com.bgsoftware.superiorskyblock.api.service.bossbar.BossBarsService; | ||
import org.bukkit.entity.Player; | ||
|
||
public final class BossBarsServiceImpl implements BossBarsService { | ||
|
||
private final SuperiorSkyblockPlugin plugin; | ||
|
||
public BossBarsServiceImpl(SuperiorSkyblockPlugin plugin) { | ||
this.plugin = plugin; | ||
} | ||
|
||
@Override | ||
public BossBar createBossBar(Player player, String message, BossBar.Color color, double ticksToRun) { | ||
return plugin.getNMSPlayers().createBossBar(player, message, color, ticksToRun); | ||
} | ||
|
||
} |
38 changes: 38 additions & 0 deletions
38
src/main/java/com/bgsoftware/superiorskyblock/service/bossbar/EmptyBossBar.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package com.bgsoftware.superiorskyblock.service.bossbar; | ||
|
||
import com.bgsoftware.superiorskyblock.api.service.bossbar.BossBar; | ||
import org.bukkit.entity.Player; | ||
|
||
public final class EmptyBossBar implements BossBar { | ||
|
||
private static final EmptyBossBar INSTANCE = new EmptyBossBar(); | ||
|
||
public static EmptyBossBar getInstance() { | ||
return INSTANCE; | ||
} | ||
|
||
private EmptyBossBar() { | ||
|
||
} | ||
|
||
@Override | ||
public void addPlayer(Player player) { | ||
|
||
} | ||
|
||
@Override | ||
public void removeAll() { | ||
|
||
} | ||
|
||
@Override | ||
public void setProgress(double progress) { | ||
|
||
} | ||
|
||
@Override | ||
public double getProgress() { | ||
return 0; | ||
} | ||
|
||
} |
Oops, something went wrong.