I'm working on a minecraft plugin for my Spigot Minecraft server.
I have a class which works as expected. I've done my best to hammer every single bug out (famous last words, I know) and am now taking a look at the way it's written. I've noticed that documentation definitely is not lacking, and looks over documented, to the point where it becomes unhelpful. My code is as follows:
package <redacted>;
import java.util.List;
import java.util.Random;
import net.md_5.bungee.api.chat.BaseComponent;
import net.md_5.bungee.api.chat.TextComponent;
import net.md_5.bungee.chat.ComponentSerializer;
import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitRunnable;
import org.inventivetalent.bossbar.BossBar;
import org.inventivetalent.bossbar.BossBarAPI;
/**
*
* @author Spotlight
*/
public class BossBarUpdate extends BukkitRunnable {
private Player p;
/**
* Set global variables.
*
* @param p Player
*/
public BossBarUpdate(Player p) {
this.p = p;
}
@Override
public void run() {
// Cancel task if player logged off or crashed
if (p == null || !p.isOnline()) {
p = null;
cancel();
return;
}
if (!Main.SHOW_BAR) {
// Do nothing, as AttentionManager or another class has taken over.
return;
}
// Get list of possible messages
List<String> messages = Main.barConfig.getStringList("messages");
// Create color instance
BossBarAPI.Color barColor;
// Get current (first) boss bar
BossBar firstBossBar = BossBarAPI.getBossBars(p).iterator().next();
// Create possible string instance
String possibleMessage;
// Make current message a String
BaseComponent[] components = ComponentSerializer.parse(firstBossBar.getMessage());
String currentMessage = new TextComponent(components).toLegacyText();
// Choose a random color + message
do {
// Get random color
barColor = new Utils(null).randomEnum(BossBarAPI.Color.class);
// Get random number between message size
int index = new Random().nextInt(messages.size());
// Get random message at random number index
possibleMessage = messages.get(index);
} while (!firstBossBar.getColor().equals(barColor) & currentMessage.equals(possibleMessage));
TextComponent message = new TextComponent(TextComponent.fromLegacyText(possibleMessage));
// Temporarily reset bars
BossBarAPI.removeAllBars(p);
// Choose a random color
BossBarAPI.addBar(p, // The receiver of the BossBar
message, // Displayed message
barColor, // Color of the bar
BossBarAPI.Style.PROGRESS, // Bar style
1.0F); // Fullness
}
}
I'm looking to increase the helpfulness of this code's documentation, so that it's not as verbose, but you can still tell what's going on. All comments are welcome, even if they aren't related to documentation.