Skip to content
Open
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
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>

</repositories>
<dependencies>
<!--Spigot API and NMS-->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import nl.martenm.servertutorialplus.helpers.dataholders.OldValuesPlayer;
import nl.martenm.servertutorialplus.points.IPlayPoint;
import org.bukkit.Bukkit;
import org.bukkit.NamespacedKey;
import org.bukkit.boss.BossBar;
import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitRunnable;

Expand Down Expand Up @@ -105,6 +107,11 @@ private void restorePlayer(boolean originalLocation){
if (originalLocation) {
player.teleport(oldValuesPlayer.getLoc());
}

BossBar bossBar = Bukkit.getBossBar(new NamespacedKey(plugin, "bossbar"));
if (bossBar != null) {
bossBar.removeAll();
}
// });
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@
import net.md_5.bungee.api.chat.TextComponent;
import nl.martenm.servertutorialplus.points.editor.PointArg;
import nl.martenm.servertutorialplus.points.editor.args.*;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Sound;
import org.bukkit.*;
import org.bukkit.boss.BarColor;
import org.bukkit.boss.BarStyle;
import org.bukkit.boss.BossBar;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Firework;
import org.bukkit.entity.Player;
Expand All @@ -37,11 +38,19 @@ public abstract class ServerTutorialPoint{
protected ServerTutorialPlus plugin;
protected PointType type;

protected BukkitTask bossbarRunnable = null;

protected Location loc;
protected List<String> message_chat;
protected List<String> commands;
protected List<FireWorkInfo> fireworks;
protected String message_actionBar;
protected String bossBarTitle;
protected double bossBarProgress;
protected BarColor bossBarColor;
protected BarStyle bossBarStyle;
protected double bossBarShowAfter;
protected double bossBarHideAfter;
protected PlayerTitle titleInfo;
protected PlayerSound soundInfo;
protected List<PotionEffect> pointionEffects;
Expand Down Expand Up @@ -89,6 +98,7 @@ public void run() {
@Override
public void stop() {
if(timerTask != null) timerTask.cancel();
if (bossbarRunnable != null) bossbarRunnable.cancel();
}
};
}
Expand Down Expand Up @@ -157,6 +167,42 @@ protected void playDefault(Player player, OldValuesPlayer oldValuesPlayer, boole
}
//endregion

if (bossBarTitle != null && bossBarHideAfter > bossBarShowAfter) {

BossBar oldBar = Bukkit.getBossBar(new NamespacedKey(plugin, "bossbar"));
if (oldBar != null) {
oldBar.removeAll();
}

bossbarRunnable = new BukkitRunnable() {
final BossBar bossBar = Bukkit.getServer().createBossBar(new NamespacedKey(plugin, "bossbar"),
ChatColor.translateAlternateColorCodes('&', bossBarTitle), bossBarColor, bossBarStyle);
final int showAfterTicks = (int) (bossBarShowAfter * 20);
final int hideAfterTicks = (int) (bossBarHideAfter * 20);
int ticksPassed = 0;
{
if (bossBarProgress > 1.0) {
bossBarProgress = 1.0;
}
if (bossBarProgress < 0.0) {
bossBarProgress = 0.0;
}
bossBar.setProgress(bossBarProgress);
}
@Override
public void run() {
if (ticksPassed >= showAfterTicks && !bossBar.getPlayers().contains(player)) {
bossBar.addPlayer(player);
}
if (ticksPassed >= hideAfterTicks || ticksPassed > time * 20) {
bossBar.removePlayer(player);
this.cancel();
}
ticksPassed += 2;
}
}.runTaskTimer(plugin, 0, 2);
}

//region commands
for (String command : commands) {
Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(), PluginUtils.replaceVariables(plugin.placeholderAPI, player, command));
Expand Down Expand Up @@ -198,9 +244,28 @@ public void readSaveData(Config tutorialSaves, String ID, String i) {
commands = tutorialSaves.getStringList("tutorials." + ID + ".points." + i + ".commands");

message_actionBar = tutorialSaves.getString("tutorials." + ID + ".points." + i + ".actionbar");
bossBarTitle = tutorialSaves.getString("tutorials." + ID + ".points." + i + ".bossbar.title");
bossBarProgress = tutorialSaves.getDouble("tutorials." + ID + ".points." + i + ".bossbar.progress", 1.0);
bossBarShowAfter = tutorialSaves.getDouble("tutorials." + ID + ".points." + i + ".bossbar.show-after", 0.0);
bossBarHideAfter = tutorialSaves.getDouble("tutorials." + ID + ".points." + i + ".bossbar.hide-after", time);
lockPlayer = tutorialSaves.getBoolean("tutorials." + ID + ".points." + i + ".locplayer");
lockView = tutorialSaves.getBoolean("tutorials." + ID + ".points." + i + ".locview");
flying = tutorialSaves.getBoolean("tutorials." + ID + ".points." + i + ".setFly");

try {
String bossBarStyleString = tutorialSaves.getString("tutorials." + ID + ".points." + i + ".bossbar.style", "SOLID");
bossBarStyle = BarStyle.valueOf(bossBarStyleString.toUpperCase());
} catch (IllegalArgumentException e) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reason for the 'try-catch' blocks here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, the exception IllegalArgumentException is caught when the valueOf method is called on the BarStyle enum with a string that doesn't match any of the enum constants.

bossBarStyle = BarStyle.SOLID;
}

try {
String bossBarColorString = tutorialSaves.getString("tutorials." + ID + ".points." + i + ".bossbar.color", "WHITE");
bossBarColor = BarColor.valueOf(bossBarColorString.toUpperCase());
} catch (IllegalArgumentException e) {
bossBarColor = BarColor.WHITE;
}

/*
Fire work meta!
*/
Expand Down Expand Up @@ -267,6 +332,15 @@ public void saveData(Config tutorialSaves, String key, String i){
tutorialSaves.set("tutorials." + key + ".points." + i + ".commands", commands);
if(flying) tutorialSaves.set("tutorials." + key + ".points." + i + ".setFly", flying);

if (bossBarTitle != null) {
tutorialSaves.set("tutorials." + key + ".points." + i + ".bossbar.title", bossBarTitle);
tutorialSaves.set("tutorials." + key + ".points." + i + ".bossbar.color", bossBarColor.name());
tutorialSaves.set("tutorials." + key + ".points." + i + ".bossbar.style", bossBarStyle.name());
tutorialSaves.set("tutorials." + key + ".points." + i + ".bossbar.progress", bossBarProgress);
tutorialSaves.set("tutorials." + key + ".points." + i + ".bossbar.show-after", bossBarShowAfter);
tutorialSaves.set("tutorials." + key + ".points." + i + ".bossbar.hide-after", bossBarHideAfter);
}

if(titleInfo != null){
tutorialSaves.set("tutorials." + key + ".points." + i + ".title.title", titleInfo.title);
tutorialSaves.set("tutorials." + key + ".points." + i + ".title.subtitle", titleInfo.subtitle);
Expand Down Expand Up @@ -317,6 +391,7 @@ public List<PointArg> getArgs(){
args.add(new MessagesArg());
args.add(new CommandsArg());
args.add(new ActionbarArg());
args.add(new BossBarArg());
args.add(new FireworkArg());
args.add(new PotionEffectArg());
args.add(new SoundArg());
Expand Down Expand Up @@ -439,4 +514,28 @@ public boolean isSetFlying() {
public void setFlying(boolean setFlying) {
this.flying = setFlying;
}

public void setBossBarTitle(String bossBarTitle) {
this.bossBarTitle = bossBarTitle;
}

public void setBossBarProgress(double bossBarProgress) {
this.bossBarProgress = bossBarProgress;
}

public void setBossBarColor(BarColor bossBarColor) {
this.bossBarColor = bossBarColor;
}

public void setBossBarStyle(BarStyle bossBarStyle) {
this.bossBarStyle = bossBarStyle;
}

public void setBossBarShowAfter(double bossBarShowAfter) {
this.bossBarShowAfter = bossBarShowAfter;
}

public void setBossBarHideAfter(double bossBarHideAfter) {
this.bossBarHideAfter = bossBarHideAfter;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package nl.martenm.servertutorialplus.points.editor.args;

import nl.martenm.servertutorialplus.language.Lang;
import nl.martenm.servertutorialplus.objects.ServerTutorial;
import nl.martenm.servertutorialplus.points.ServerTutorialPoint;
import nl.martenm.servertutorialplus.points.editor.PointArg;
import org.apache.commons.lang3.StringUtils;
import org.bukkit.boss.BarColor;
import org.bukkit.boss.BarStyle;
import org.bukkit.command.CommandSender;

public class BossBarArg extends PointArg {

public BossBarArg() {
super("bossbar");
}

@Override
public boolean run(ServerTutorial serverTutorial, ServerTutorialPoint point, CommandSender sender, String[] args) {
if(args.length < 1){
sender.sendMessage(Lang.WRONG_COMMAND_FORMAT + "/st editpoint <t> <p> bossbar <title/progress/color/style/show-after/hide-after> <args>");
return false;
}

if (args.length < 2) {
sender.sendMessage(Lang.ERROR_ATLEAST_ONE_WORD.toString());
return false;
}

switch (args[0]) {
case "title":
String title = StringUtils.join(args, ' ', 1, args.length);
point.setBossBarTitle(title);
break;
case "progress":
double progress;
try {
progress = Double.parseDouble(args[1]);
if (progress > 1.0 || progress < 0.0) {
sender.sendMessage(Lang.ERROR_INVALID_NUMBNER.toString());
return false;
}
} catch (NumberFormatException e) {
sender.sendMessage(Lang.ERROR_INVALID_NUMBNER.toString());
return false;
}
point.setBossBarProgress(progress);
break;
case "color":
BarColor color;
try {
color = BarColor.valueOf(args[1]);
} catch (IllegalArgumentException e) {
sender.sendMessage(Lang.INVALID_ARGS.toString());
return false;
}
point.setBossBarColor(color);
break;
case "style":
BarStyle style;
try {
style = BarStyle.valueOf(args[1]);
} catch (IllegalArgumentException e) {
sender.sendMessage(Lang.INVALID_ARGS.toString());
return false;
}
point.setBossBarStyle(style);
break;
case "show-after":
double showAfter;
try {
showAfter = Double.parseDouble(args[1]);
} catch (NumberFormatException e) {
sender.sendMessage(Lang.ERROR_INVALID_NUMBNER.toString());
return false;
}
point.setBossBarShowAfter(showAfter);
break;
case "hide-after":
double hideAfter;
try {
hideAfter = Double.parseDouble(args[1]);
} catch (NumberFormatException e) {
sender.sendMessage(Lang.ERROR_INVALID_NUMBNER.toString());
return false;
}
point.setBossBarHideAfter(hideAfter);
break;
default:
sender.sendMessage(Lang.WRONG_COMMAND_FORMAT + "/st editpoint <t> <p> bossbar <title/progress/color/style/show-after/hide-after> <args>");
return false;
}
return true;
}
}