To enhance your updateActionBar method by integrating the Kyori Adventure API for customizing the heart symbol's font, you'll need to transition from using StringBuilder and ChatColor to constructing Component objects. This approach allows for more advanced text styling, including custom fonts.
Below is the revised version of your updateActionBar method incorporating Kyori’s Adventure API:
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.TextComponent;
import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.text.format.TextDecoration;
import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitRunnable;
// Ensure you have the necessary imports for Adventure API
public void updateActionBar(Player player) {
new BukkitRunnable() {
@Override
public void run() {
if (!player.isOnline()) {
this.cancel();
return;
}
PlayerStats stats = PLmmorpgIFR.getScoreboard().playerStatsMap.get(player.getName());
if (stats == null) {
this.cancel();
return;
}
Component actionBar;
if (!skillModeActive.getOrDefault(player, false)) {
String health = getHealthInNumbers(player);
String healthMax = getMaxHealthInNumber(player);
String negativeSpaces = createNegativeSpaces(20 - health.length());
String downSpaces = createDownSpaces(1);
// Build the base health component
TextComponent baseHealth = Component.text(downSpaces + negativeSpaces + health + " / " + healthMax + " ")
.color(NamedTextColor.WHITE);
// Create the heart symbol with a custom font and color
TextComponent heart = Component.text("❤")
.color(NamedTextColor.RED)
.decoration(TextDecoration.NONE, true)
.font("custom:hearts"); // Replace with your actual font key
// Combine the base health and heart components
actionBar = baseHealth.append(heart);
} else {
String health = getHealthInNumbers(player);
String healthMax = getMaxHealthInNumber(player);
String negativeSpaces = createNegativeSpaces(20 - health.length());
// Build the base health component
TextComponent baseHealth = Component.text(negativeSpaces + health + " / " + healthMax + " ")
.color(NamedTextColor.WHITE);
// Create the heart symbol with a custom font and color
TextComponent heart = Component.text("❤")
.color(NamedTextColor.RED)
.decoration(TextDecoration.NONE, true)
.font("custom:hearts"); // Replace with your actual font key
// Start building the action bar with health and heart
actionBar = baseHealth.append(heart).append(Component.space());
String currentSelectedSkill = selectedSkillName.getOrDefault(player, "");
for (int i = 0; i < 8; i++) {
Skills skill = stats.getSkillAtSlot(i);
if (skill != null) {
long remainingCooldown = stats.getRemainingCooldown(skill);
NamedTextColor color;
if (skill.getName().equals(currentSelectedSkill)) {
color = NamedTextColor.YELLOW;
} else if (remainingCooldown > 0) {
color = NamedTextColor.RED;
} else {
color = NamedTextColor.GREEN;
}
// Create the skill name component with appropriate color
TextComponent skillComponent = Component.text(skill.getName())
.color(color);
// Append cooldown if applicable
if (remainingCooldown > 0) {
skillComponent = skillComponent.append(
Component.text("(" + (remainingCooldown / 1000) + "s)")
.color(NamedTextColor.GRAY)
);
}
actionBar = actionBar.append(skillComponent).append(Component.space());
} else {
// Append a gray dash for empty skill slots
TextComponent emptySlot = Component.text("-")
.color(NamedTextColor.GRAY)
.append(Component.space());
actionBar = actionBar.append(emptySlot);
}
}
}
// Send the assembled action bar to the player
player.sendActionBar(actionBar);
}
}.runTaskTimer(PLmmorpgIFR.getInstance(), 0L, 20L); // Update every second (20 ticks)
}
// Helper methods remain unchanged
private static String getHealthInNumbers(Player player) {
double health = player.getHealth();
return String.valueOf((int) health);
}
private static String getMaxHealthInNumber(Player player) {
double healthMax = player.getMaxHealth();
return String.valueOf((int) healthMax);
}
private static String createDownSpaces(int count) {
return new String(new char[count]).replace('\0', ' '); // Assuming ' ' is intended
}
private static String createNegativeSpaces(int count) {
return new String(new char[count]).replace('\0', '\uF805');
}
Key Changes and Explanations
-
Using Kyori's Component System:
- Replaced
StringBuilder with Component objects to leverage Adventure's rich text capabilities.
- This allows for more precise styling, such as applying different fonts to specific text segments.
-
Custom Font for Hearts:
- The heart symbol (
❤) is encapsulated within a TextComponent with a custom font applied via .font("custom:hearts").
- Ensure that
"custom:hearts" corresponds to a valid font key defined in your resource pack. You'll need to include this font in your plugin's resource assets and reference it correctly.
-
Color Management:
- Utilized
NamedTextColor instead of ChatColor for consistent color handling with Adventure API.
- Applied colors directly to
TextComponent instances for both health values and skill names.
-
Appending Components:
- Used
.append() to concatenate different Component parts, such as the health display and the heart symbol.
- For skills, each skill’s name and its cooldown (if any) are appended as separate components with appropriate styling.
-
Negative Spaces Preservation:
- The
createNegativeSpaces method remains unchanged, ensuring that the negative spaces are preserved as per your requirement.
- These spaces are included directly in the text components without additional styling, keeping them unaffected by the font changes applied to the heart symbol.
-
Action Bar Update Frequency:
- Adjusted the
runTaskTimer delay and period to 0L and 20L respectively, updating the action bar every second. Adjust this as needed based on your desired refresh rate.
Additional Considerations
-
Resource Pack Configuration:
- To use custom fonts with the Adventure API, you must define the font in your plugin’s resource pack. Ensure that the font file is correctly referenced and that the font key (
"custom:hearts") matches the one you use in the .font() method.
-
Adventure API Integration:
- Make sure your plugin is correctly set up to use the Adventure API. This typically involves including the necessary dependencies and initializing the Adventure platform within your plugin.
-
Performance Optimization:
- Continuously creating new
Component instances can be resource-intensive. Ensure that the frequency of updates is optimized to balance performance with responsiveness.
-
Error Handling:
- Consider adding more robust error handling, especially around font loading and component assembly, to prevent potential runtime issues.
By implementing these changes, your action bar will now display the heart symbol with a customized font while maintaining the integrity of the negative spaces and overall action bar layout.