Some refactoring

This commit is contained in:
nossr50
2020-11-04 12:12:51 -08:00
parent 329de942b4
commit 15578bb84e
62 changed files with 170 additions and 201 deletions

View File

@ -0,0 +1,23 @@
package com.gmail.nossr50.util.text;
import net.kyori.adventure.audience.Audience;
import net.kyori.adventure.audience.MessageType;
import net.kyori.adventure.identity.Identity;
import net.kyori.adventure.text.Component;
import java.util.function.BiConsumer;
public enum McMMOMessageType {
ACTION_BAR(Audience::sendActionBar),
SYSTEM((audience, message) -> audience.sendMessage(Identity.nil(), message, MessageType.SYSTEM));
private final BiConsumer<Audience, Component> sender;
McMMOMessageType(final BiConsumer<Audience, Component> sender) {
this.sender = sender;
}
public void send(final Audience audience, final Component message) {
this.sender.accept(audience, message);
}
}

View File

@ -0,0 +1,152 @@
package com.gmail.nossr50.util.text;
import com.gmail.nossr50.datatypes.party.PartyFeature;
import com.gmail.nossr50.datatypes.skills.SuperAbilityType;
import org.bukkit.Material;
import org.bukkit.block.data.Ageable;
import org.bukkit.block.data.BlockData;
import org.bukkit.entity.EntityType;
import org.jetbrains.annotations.NotNull;
import java.util.Locale;
public class StringUtils {
/**
* Gets a capitalized version of the target string.
*
* @param target
* String to capitalize
* @return the capitalized string
*/
public static String getCapitalized(String target) {
return target.substring(0, 1).toUpperCase() + target.substring(1).toLowerCase(Locale.ENGLISH);
}
/**
* Creates a string from an array skipping the first n elements
* @param args the array to iterate over when forming the string
* @param index the amount of elements to skip over
* @return the "trimmed" string
*/
public static String buildStringAfterNthElement(@NotNull String @NotNull []args, int index) {
StringBuilder trimMessage = new StringBuilder();
for (int i = index; i < args.length; i++) {
if(i + 1 >= args.length)
trimMessage.append(args[i]);
else
trimMessage.append(args[i]).append(" ");
}
return trimMessage.toString();
}
public static String getPrettyItemString(Material material) {
return createPrettyString(material.toString());
}
public static String getPrettyEntityTypeString(EntityType entity) {
return createPrettyString(entity.toString());
}
public static String getPrettyAbilityString(SuperAbilityType ability) {
return createPrettyString(ability.toString());
}
public static String getWildcardConfigBlockDataString(BlockData data) {
return getWildcardConfigMaterialString(data.getMaterial());
}
public static String getWildcardConfigMaterialString(Material data) {
return StringUtils.getPrettyItemString(data).replace(" ", "_") + "|*";
}
public static String getFriendlyConfigBlockDataString(BlockData data) {
switch(data.getMaterial()){
case CHORUS_FLOWER:
case COCOA:
case WHEAT:
case BEETROOTS:
case CARROTS:
case POTATOES:
case NETHER_WART: {
if (data instanceof Ageable) {
Ageable ageData = (Ageable) data;
if (ageData.getAge() == ageData.getMaximumAge()) {
return getPrettyItemString(data.getMaterial()).replace(" ", "_") + "_Ripe";
}
}
return getPrettyItemString(data.getMaterial()).replace(" ", "_") + "_Ungrown";
}
}
return getPrettyItemString(data.getMaterial()).replace(" ", "_");
}
public static String getFriendlyConfigMaterialString(Material data) {
return getPrettyItemString(data).replace(" ", "_");
}
public static String getExplicitConfigBlockDataString(BlockData data) {
return getExplicitConfigMaterialString(data.getMaterial());
}
public static String getExplicitConfigMaterialString(Material data) {
return StringUtils.getPrettyItemString(data).replace(" ", "_");
}
public static String getPrettyPartyFeatureString(PartyFeature partyFeature) {
return createPrettyString(partyFeature.toString());
}
private static String createPrettyString(String baseString) {
String[] substrings = baseString.split("_");
String prettyString = "";
int size = 1;
for (String string : substrings) {
prettyString = prettyString.concat(getCapitalized(string));
if (size < substrings.length) {
prettyString = prettyString.concat(" ");
}
size++;
}
return prettyString;
}
/**
* Determine if a string represents an Integer
*
* @param string
* String to check
* @return true if the string is an Integer, false otherwise
*/
public static boolean isInt(String string) {
try {
Integer.parseInt(string);
return true;
} catch (NumberFormatException nFE) {
return false;
}
}
/**
* Determine if a string represents a Double
*
* @param string
* String to check
* @return true if the string is a Double, false otherwise
*/
public static boolean isDouble(String string) {
try {
Double.parseDouble(string);
return true;
} catch (NumberFormatException nFE) {
return false;
}
}
}

View File

@ -0,0 +1,524 @@
package com.gmail.nossr50.util.text;
import com.gmail.nossr50.config.Config;
import com.gmail.nossr50.config.RankConfig;
import com.gmail.nossr50.datatypes.json.McMMOUrl;
import com.gmail.nossr50.datatypes.json.McMMOWebLinks;
import com.gmail.nossr50.datatypes.skills.PrimarySkillType;
import com.gmail.nossr50.datatypes.skills.SubSkillType;
import com.gmail.nossr50.datatypes.skills.subskills.AbstractSubSkill;
import com.gmail.nossr50.listeners.InteractionManager;
import com.gmail.nossr50.locale.LocaleLoader;
import com.gmail.nossr50.mcMMO;
import com.gmail.nossr50.util.Permissions;
import com.gmail.nossr50.util.skills.RankUtils;
import net.kyori.adventure.audience.Audience;
import net.kyori.adventure.audience.MessageType;
import net.kyori.adventure.identity.Identity;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.TextComponent;
import net.kyori.adventure.text.event.ClickEvent;
import net.kyori.adventure.text.event.HoverEvent;
import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.text.format.TextColor;
import net.kyori.adventure.text.format.TextDecoration;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
/**
* This class handles many of the JSON components that mcMMO makes and uses
*/
public class TextComponentFactory {
/**
* Makes a text component using strings from a locale and supports passing an undefined number of variables to the LocaleLoader
* @param localeKey target locale string address
* @param values vars to be passed to the locale loader
* @return
*/
public static TextComponent getNotificationMultipleValues(String localeKey, String... values)
{
String preColoredString = LocaleLoader.getString(localeKey, (Object[]) values);
return Component.text(preColoredString);
}
public static Component getNotificationTextComponentFromLocale(String localeKey)
{
return getNotificationTextComponent(LocaleLoader.getString(localeKey));
}
public static Component getNotificationLevelUpTextComponent(PrimarySkillType skill, int levelsGained, int currentLevel)
{
return Component.text(LocaleLoader.getString("Overhaul.Levelup", LocaleLoader.getString("Overhaul.Name."+ StringUtils.getCapitalized(skill.toString())), levelsGained, currentLevel));
}
private static TextComponent getNotificationTextComponent(String text)
{
//textComponent.setColor(getNotificationColor(notificationType));
return Component.text(text);
}
public static void sendPlayerSubSkillWikiLink(Player player, String subskillformatted)
{
if(!Config.getInstance().getUrlLinksEnabled())
return;
TextComponent.Builder wikiLinkComponent = Component.text().content(LocaleLoader.getString("Overhaul.mcMMO.MmoInfo.Wiki"));
wikiLinkComponent.decoration(TextDecoration.UNDERLINED, true);
String wikiUrl = "https://mcmmo.org/wiki/"+subskillformatted;
wikiLinkComponent.clickEvent(ClickEvent.openUrl(wikiUrl));
TextComponent.Builder componentBuilder = Component.text().content(subskillformatted).append(Component.newline()).append(Component.text(wikiUrl)).color(NamedTextColor.GRAY).decoration(TextDecoration.ITALIC, true);
wikiLinkComponent.hoverEvent(HoverEvent.showText(componentBuilder.build()));
mcMMO.getAudiences().player(player).sendMessage(Identity.nil(), wikiLinkComponent, MessageType.SYSTEM);
}
public static void sendPlayerUrlHeader(Player player) {
TextComponent prefix = Component.text(LocaleLoader.getString("Overhaul.mcMMO.Url.Wrap.Prefix") + " ");
/*prefix.setColor(ChatColor.DARK_AQUA);*/
TextComponent suffix = Component.text(" "+LocaleLoader.getString("Overhaul.mcMMO.Url.Wrap.Suffix"));
/*suffix.setColor(ChatColor.DARK_AQUA);*/
TextComponent emptySpace = Component.space();
mcMMO.getAudiences().player(player).sendMessage(Identity.nil(),TextComponent.ofChildren(
prefix,
getWebLinkTextComponent(McMMOWebLinks.WEBSITE),
emptySpace,
getWebLinkTextComponent(McMMOWebLinks.DISCORD),
emptySpace,
getWebLinkTextComponent(McMMOWebLinks.PATREON),
emptySpace,
getWebLinkTextComponent(McMMOWebLinks.WIKI),
emptySpace,
getWebLinkTextComponent(McMMOWebLinks.SPIGOT),
emptySpace,
getWebLinkTextComponent(McMMOWebLinks.HELP_TRANSLATE),
suffix
), MessageType.SYSTEM);
}
/**
* Sends a player a bunch of text components that represent a list of sub-skills
* Styling and formatting is applied before sending the messages
*
* @param player target player
* @param subSkillComponents the text components representing the sub-skills by name
*/
public static void sendPlayerSubSkillList(@NotNull Player player, @NotNull List<Component> subSkillComponents) {
final Audience audience = mcMMO.getAudiences().player(player);
//@ Signs, done for style
Component space = Component.space();
TextComponent atSignComponent = Component.text(LocaleLoader.getString("JSON.Hover.AtSymbolSkills"));
//Only send 3 sub-skills per line
Component[][] splitSubSkills = TextUtils.splitComponentsIntoGroups(subSkillComponents, 3);
ArrayList<Component> individualLinesToSend = new ArrayList<>();
//Create each line
for (Component[] componentArray : splitSubSkills) {
individualLinesToSend.add(TextUtils.fromArray(componentArray, atSignComponent, space));
}
//Send each group
for(Component curLine : individualLinesToSend) {
audience.sendMessage(Identity.nil(), curLine);
}
}
private static Component getWebLinkTextComponent(McMMOWebLinks webLinks)
{
TextComponent.Builder webTextComponent;
switch(webLinks)
{
case WEBSITE:
webTextComponent = Component.text().content(LocaleLoader.getString("JSON.Hover.AtSymbolURL"));
TextUtils.addChildWebComponent(webTextComponent, "Web");
webTextComponent.clickEvent(getUrlClickEvent(McMMOUrl.urlWebsite));
break;
case SPIGOT:
webTextComponent = Component.text().content(LocaleLoader.getString("JSON.Hover.AtSymbolURL"));
TextUtils.addChildWebComponent(webTextComponent, "Spigot");
webTextComponent.clickEvent(getUrlClickEvent(McMMOUrl.urlSpigot));
break;
case DISCORD:
webTextComponent = Component.text().content(LocaleLoader.getString("JSON.Hover.AtSymbolURL"));
TextUtils.addChildWebComponent(webTextComponent, "Discord");
webTextComponent.clickEvent(getUrlClickEvent(McMMOUrl.urlDiscord));
break;
case PATREON:
webTextComponent = Component.text().content(LocaleLoader.getString("JSON.Hover.AtSymbolURL"));
TextUtils.addChildWebComponent(webTextComponent, "Patreon");
webTextComponent.clickEvent(getUrlClickEvent(McMMOUrl.urlPatreon));
break;
case WIKI:
webTextComponent = Component.text().content(LocaleLoader.getString("JSON.Hover.AtSymbolURL"));
TextUtils.addChildWebComponent(webTextComponent, "Wiki");
webTextComponent.clickEvent(getUrlClickEvent(McMMOUrl.urlWiki));
break;
case HELP_TRANSLATE:
webTextComponent = Component.text().content(LocaleLoader.getString("JSON.Hover.AtSymbolURL"));
TextUtils.addChildWebComponent(webTextComponent, "Lang");
webTextComponent.clickEvent(getUrlClickEvent(McMMOUrl.urlTranslate));
break;
default:
webTextComponent = Component.text().content("NOT DEFINED");
}
TextUtils.addNewHoverComponentToTextComponent(webTextComponent, getUrlHoverEvent(webLinks));
webTextComponent.insertion(webLinks.getUrl());
return webTextComponent.build();
}
private static Component getUrlHoverEvent(McMMOWebLinks webLinks)
{
TextComponent.Builder componentBuilder = Component.text().content(webLinks.getNiceTitle());
switch(webLinks)
{
case WEBSITE:
addUrlHeaderHover(webLinks, componentBuilder);
componentBuilder.append(Component.newline()).append(Component.newline());
componentBuilder.append(Component.text(webLinks.getLocaleDescription(), NamedTextColor.GREEN));
componentBuilder.append(Component.text("\nDev Blogs, and information related to mcMMO can be found here", NamedTextColor.GRAY));
break;
case SPIGOT:
addUrlHeaderHover(webLinks, componentBuilder);
componentBuilder.append(Component.newline()).append(Component.newline());
componentBuilder.append(Component.text(webLinks.getLocaleDescription(), NamedTextColor.GREEN));
componentBuilder.append(Component.text("\nI post regularly in the discussion thread here!", NamedTextColor.GRAY));
break;
case PATREON:
addUrlHeaderHover(webLinks, componentBuilder);
componentBuilder.append(Component.newline()).append(Component.newline());
componentBuilder.append(Component.text(webLinks.getLocaleDescription(), NamedTextColor.GREEN));
componentBuilder.append(Component.newline());
componentBuilder.append(Component.text("Show support by buying me a coffee :)", NamedTextColor.GRAY));
break;
case WIKI:
addUrlHeaderHover(webLinks, componentBuilder);
componentBuilder.append(Component.newline()).append(Component.newline());
componentBuilder.append(Component.text(webLinks.getLocaleDescription(), NamedTextColor.GREEN));
componentBuilder.append(Component.newline());
componentBuilder.append(Component.text("I'm looking for more wiki staff, contact me on our discord!", NamedTextColor.DARK_GRAY));
break;
case DISCORD:
addUrlHeaderHover(webLinks, componentBuilder);
componentBuilder.append(Component.newline()).append(Component.newline());
componentBuilder.append(Component.text(webLinks.getLocaleDescription(), NamedTextColor.GREEN));
break;
case HELP_TRANSLATE:
addUrlHeaderHover(webLinks, componentBuilder);
componentBuilder.append(Component.newline()).append(Component.newline());
componentBuilder.append(Component.text(webLinks.getLocaleDescription(), NamedTextColor.GREEN));
componentBuilder.append(Component.newline());
componentBuilder.append(Component.text("You can use this website to help translate mcMMO into your language!" +
"\nIf you want to know more contact me in discord.", NamedTextColor.DARK_GRAY));
}
return componentBuilder.build();
}
private static void addUrlHeaderHover(McMMOWebLinks webLinks, TextComponent.Builder componentBuilder) {
componentBuilder.append(Component.newline());
componentBuilder.append(Component.text(webLinks.getUrl(), NamedTextColor.GRAY, TextDecoration.ITALIC));
}
private static ClickEvent getUrlClickEvent(String url)
{
return ClickEvent.openUrl(url);
}
private static Component getSubSkillTextComponent(Player player, SubSkillType subSkillType)
{
//Get skill name
String skillName = subSkillType.getLocaleName();
boolean skillUnlocked = RankUtils.hasUnlockedSubskill(player, subSkillType);
TextComponent.Builder textComponent = initNewSkillTextComponent(player, skillName, subSkillType, skillUnlocked);
//Hover Event
TextUtils.addNewHoverComponentToTextComponent(textComponent, getSubSkillHoverComponent(player, subSkillType));
//Insertion
textComponent.insertion(skillName);
return textComponent.build();
}
private static TextComponent getSubSkillTextComponent(Player player, AbstractSubSkill abstractSubSkill)
{
//String key = abstractSubSkill.getConfigKeyName();
String skillName = abstractSubSkill.getNiceName();
//Setup Text Component
SubSkillType subSkillType = abstractSubSkill.getSubSkillType();
boolean skillUnlocked = RankUtils.hasUnlockedSubskill(player, subSkillType);
TextComponent.Builder textComponent = initNewSkillTextComponent(player, skillName, subSkillType, skillUnlocked);
//Hover Event
TextUtils.addNewHoverComponentToTextComponent(textComponent, getSubSkillHoverComponent(player, abstractSubSkill));
//Insertion
textComponent.insertion(skillName);
return textComponent.build();
}
private static TextComponent.Builder initNewSkillTextComponent(Player player, String skillName, SubSkillType subSkillType, boolean skillUnlocked) {
TextComponent.Builder textComponent;
if (skillUnlocked) {
if (RankUtils.getHighestRank(subSkillType) == RankUtils.getRank(player, subSkillType) && subSkillType.getNumRanks() > 1)
textComponent = Component.text().content(LocaleLoader.getString("JSON.Hover.MaxRankSkillName", skillName));
else
textComponent = Component.text().content(LocaleLoader.getString("JSON.Hover.SkillName", skillName));
textComponent.clickEvent(ClickEvent.runCommand("/mmoinfo " + subSkillType.getNiceNameNoSpaces(subSkillType)));
} else {
textComponent = Component.text().content(LocaleLoader.getString("JSON.Hover.Mystery",
String.valueOf(RankUtils.getUnlockLevel(subSkillType))));
textComponent.clickEvent(ClickEvent.runCommand("/mmoinfo ???"));
}
return textComponent;
}
private static Component getSubSkillHoverComponent(Player player, AbstractSubSkill abstractSubSkill)
{
return getSubSkillHoverEventJSON(abstractSubSkill, player);
}
private static Component getSubSkillHoverComponent(Player player, SubSkillType subSkillType)
{
return getSubSkillHoverEventJSON(subSkillType, player);
}
/**
* Used for the skill in the new skill system (Deriving from AbstractSubSkill)
* @param abstractSubSkill this subskill
* @param player the player who owns this subskill
* @return the hover basecomponent object for this subskill
*/
private static Component getSubSkillHoverEventJSON(AbstractSubSkill abstractSubSkill, Player player)
{
String skillName = abstractSubSkill.getNiceName();
/*
* Hover Event BaseComponent color table
*/
TextColor ccSubSkillHeader = NamedTextColor.GOLD;
TextColor ccRank = NamedTextColor.BLUE;
TextColor ccCurRank = NamedTextColor.GREEN;
TextColor ccPossessive = NamedTextColor.WHITE;
//ChatColor ccDescriptionHeader = ChatColor.DARK_PURPLE;
//ChatColor ccDescription = ChatColor.WHITE;
TextColor ccLocked = NamedTextColor.DARK_GRAY;
TextColor ccLevelRequirement = NamedTextColor.BLUE;
TextColor ccLevelRequired = NamedTextColor.RED;
SubSkillType subSkillType = abstractSubSkill.getSubSkillType();
//SubSkillType Name
TextComponent.Builder componentBuilder = setupSkillComponentNameStyle(player, skillName, subSkillType, RankUtils.hasUnlockedSubskill(player, abstractSubSkill));
if(!RankUtils.hasUnlockedSubskill(player, abstractSubSkill))
{
//Skill is not unlocked yet
addLocked(abstractSubSkill, ccLocked, ccLevelRequirement, ccLevelRequired, componentBuilder);
} else {
addSubSkillTypeToHoverEventJSON(abstractSubSkill, componentBuilder);
//RANK
int curRank = RankUtils.getRank(player, abstractSubSkill);
int nextRank = 0;
if(curRank < abstractSubSkill.getNumRanks() && abstractSubSkill.getNumRanks() > 0)
{
nextRank = RankUtils.getRankUnlockLevel(abstractSubSkill, curRank+1);
}
addRanked(ccRank, ccCurRank, ccPossessive, ccCurRank, componentBuilder, abstractSubSkill.getNumRanks(), RankUtils.getRank(player, abstractSubSkill), nextRank);
componentBuilder.append(Component.text(LocaleLoader.getString("JSON.DescriptionHeader")));
componentBuilder.append(Component.newline()).append(Component.text(abstractSubSkill.getDescription())).append(Component.newline());
//Empty line
componentBuilder.append(Component.newline()).decoration(TextDecoration.BOLD, false);
componentBuilder.append(Component.newline());
//Finally, add details to the tooltip
abstractSubSkill.addStats(componentBuilder, player);
}
return componentBuilder.build();
}
private static TextComponent.Builder setupSkillComponentNameStyle(Player player, String skillName, SubSkillType subSkillType, boolean skillUnlocked) {
TextComponent.Builder componentBuilder;
if (skillUnlocked) {
if (RankUtils.getHighestRank(subSkillType) == RankUtils.getRank(player, subSkillType) && subSkillType.getNumRanks() > 1)
componentBuilder = getNewComponentBuilder(LocaleLoader.getString("JSON.Hover.MaxRankSkillName", skillName));
else
componentBuilder = getNewComponentBuilder(LocaleLoader.getString("JSON.Hover.SkillName", skillName));
} else
componentBuilder = getNewComponentBuilder(LocaleLoader.getString("JSON.Hover.Mystery",
String.valueOf(RankUtils.getUnlockLevel(subSkillType))));
return componentBuilder;
}
private static TextComponent.Builder getNewComponentBuilder(String skillName) {
TextComponent.Builder componentBuilder = Component.text().content(skillName);
componentBuilder.append(Component.newline());
return componentBuilder;
}
private static void addRanked(TextColor ccRank, TextColor ccCurRank, TextColor ccPossessive, TextColor ccNumRanks, TextComponent.Builder componentBuilder, int numRanks, int rank, int nextRank) {
if (numRanks > 0) {
//Rank: x
componentBuilder.append(Component.text(LocaleLoader.getString("JSON.Hover.Rank", String.valueOf(rank)))).append(Component.newline());
//Next Rank: x
if(nextRank > rank)
componentBuilder.append(Component.text(LocaleLoader.getString("JSON.Hover.NextRank", String.valueOf(nextRank)))).append(Component.newline());
/*componentBuilder.append(" " + LocaleLoader.getString("JSON.RankPossesive") + " ").color(ccPossessive);
componentBuilder.append(String.valueOf(numRanks)).color(ccNumRanks);*/
}
}
private static void addLocked(SubSkillType subSkillType, TextColor ccLocked, TextColor ccLevelRequirement, TextColor ccLevelRequired, TextComponent.Builder componentBuilder) {
addLocked(ccLocked, ccLevelRequirement, componentBuilder);
componentBuilder.append(Component.text(String.valueOf(RankConfig.getInstance().getSubSkillUnlockLevel(subSkillType, 1)), ccLevelRequired));
//componentBuilder.append(Component.newline());
}
private static void addLocked(AbstractSubSkill abstractSubSkill, TextColor ccLocked, TextColor ccLevelRequirement, TextColor ccLevelRequired, TextComponent.Builder componentBuilder) {
addLocked(ccLocked, ccLevelRequirement, componentBuilder);
componentBuilder.append(Component.text(String.valueOf(RankConfig.getInstance().getSubSkillUnlockLevel(abstractSubSkill, 1)), ccLevelRequired));
//componentBuilder.append(Component.newline());
}
private static void addLocked(TextColor ccLocked, TextColor ccLevelRequirement, TextComponent.Builder componentBuilder) {
componentBuilder.append(Component.text(LocaleLoader.getString("JSON.Locked"), ccLocked, TextDecoration.BOLD));
componentBuilder.append(Component.newline()).append(Component.newline());
componentBuilder.append(Component.text(LocaleLoader.getString("JSON.LevelRequirement") + ": ", ccLevelRequirement));
}
@Deprecated
private static Component getSubSkillHoverEventJSON(SubSkillType subSkillType, Player player)
{
String skillName = subSkillType.getLocaleName();
/*
* Hover Event BaseComponent color table
*/
TextColor ccSubSkillHeader = NamedTextColor.GOLD;
TextColor ccRank = NamedTextColor.BLUE;
TextColor ccCurRank = NamedTextColor.GREEN;
TextColor ccPossessive = NamedTextColor.WHITE;
TextColor ccDescriptionHeader = NamedTextColor.DARK_PURPLE;
TextColor ccDescription = NamedTextColor.DARK_GRAY;
TextColor ccLocked = NamedTextColor.DARK_GRAY;
TextColor ccLevelRequirement = NamedTextColor.BLUE;
TextColor ccLevelRequired = NamedTextColor.RED;
//SubSkillType Name
TextComponent.Builder componentBuilder = setupSkillComponentNameStyle(player, skillName, subSkillType, RankUtils.hasUnlockedSubskill(player, subSkillType));
if(!RankUtils.hasUnlockedSubskill(player, subSkillType))
{
//Skill is not unlocked yet
addLocked(subSkillType, ccLocked, ccLevelRequirement, ccLevelRequired, componentBuilder);
} else {
//addSubSkillTypeToHoverEventJSON(subSkillType, componentBuilder);
//RANK
if(subSkillType.getNumRanks() > 0)
{
int curRank = RankUtils.getRank(player, subSkillType);
int nextRank = 0;
if(curRank < subSkillType.getNumRanks())
{
nextRank = RankUtils.getRankUnlockLevel(subSkillType, curRank+1);
}
addRanked(ccRank, ccCurRank, ccPossessive, ccCurRank, componentBuilder, subSkillType.getNumRanks(), RankUtils.getRank(player, subSkillType), nextRank);
}
componentBuilder.append(Component.newline());
componentBuilder.append(Component.text(LocaleLoader.getString("JSON.DescriptionHeader")));
componentBuilder.color(ccDescriptionHeader);
componentBuilder.append(Component.newline());
componentBuilder.append(Component.text(subSkillType.getLocaleDescription()));
componentBuilder.color(ccDescription);
}
return componentBuilder.build();
}
private static void addSubSkillTypeToHoverEventJSON(AbstractSubSkill abstractSubSkill, TextComponent.Builder componentBuilder)
{
if(abstractSubSkill.isSuperAbility())
{
componentBuilder.append(Component.text(LocaleLoader.getString("JSON.Type.SuperAbility"), NamedTextColor.LIGHT_PURPLE, TextDecoration.BOLD));
} else if(abstractSubSkill.isActiveUse())
{
componentBuilder.append(Component.text(LocaleLoader.getString("JSON.Type.Active"), NamedTextColor.DARK_RED, TextDecoration.BOLD));
} else {
componentBuilder.append(Component.text(LocaleLoader.getString("JSON.Type.Passive"), NamedTextColor.GREEN, TextDecoration.BOLD));
}
componentBuilder.append(Component.newline());
}
public static void getSubSkillTextComponents(Player player, List<Component> textComponents, PrimarySkillType parentSkill) {
for(SubSkillType subSkillType : SubSkillType.values())
{
if(subSkillType.getParentSkill() == parentSkill)
{
if(Permissions.isSubSkillEnabled(player, subSkillType))
{
if(!InteractionManager.hasSubSkill(subSkillType))
textComponents.add(TextComponentFactory.getSubSkillTextComponent(player, subSkillType));
}
}
}
/* NEW SKILL SYSTEM */
for(AbstractSubSkill abstractSubSkill : InteractionManager.getSubSkillList())
{
if(abstractSubSkill.getPrimarySkill() == parentSkill)
{
if(Permissions.isSubSkillEnabled(player, abstractSubSkill))
textComponents.add(TextComponentFactory.getSubSkillTextComponent(player, abstractSubSkill));
}
}
}
public static TextComponent getSubSkillUnlockedNotificationComponents(Player player, SubSkillType subSkillType)
{
TextComponent.Builder unlockMessage = Component.text().content(LocaleLoader.getString("JSON.SkillUnlockMessage", subSkillType.getLocaleName(), RankUtils.getRank(player, subSkillType)));
unlockMessage.hoverEvent(HoverEvent.showText(getSubSkillHoverComponent(player, subSkillType)));
unlockMessage.clickEvent(ClickEvent.runCommand("/"+subSkillType.getParentSkill().toString().toLowerCase(Locale.ENGLISH)));
return unlockMessage.build();
}
}

View File

@ -0,0 +1,87 @@
package com.gmail.nossr50.util.text;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.ComponentBuilder;
import net.kyori.adventure.text.TextComponent;
import net.kyori.adventure.text.event.HoverEvent;
import net.kyori.adventure.text.format.NamedTextColor;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.List;
public class TextUtils {
/**
* Makes a single component from an array of components, can optionally add prefixes and suffixes to come before and after each component
* @param componentsArray target array
* @return a component with optional styling built from an array
*/
static @NotNull Component fromArray(@NotNull Component[] componentsArray, @Nullable Component prefixComponent, @Nullable Component suffixComponent) {
TextComponent.Builder componentBuilder = Component.text();
for(Component component : componentsArray) {
if(component == null) //Individual elements can be null
continue;
if(prefixComponent != null)
componentBuilder.append(prefixComponent);
componentBuilder.append(component);
if(suffixComponent != null)
componentBuilder.append(suffixComponent);
}
return componentBuilder.build();
}
/**
* Takes a list of components and splits them into arrays each with a maximum element limit
* Individual elements in [][X] may be null
*
* @param components target component list
* @param groupsSize maximum size per array
* @return a 2D array with components split into groups
*/
static @NotNull Component[][] splitComponentsIntoGroups(@NotNull List<Component> components, int groupsSize) {
int groupCount = (int) Math.ceil((double) components.size() / (double) groupsSize);
Component[][] splitGroups = new Component[groupCount][groupsSize];
int groupsFinished = 0;
while (groupsFinished < groupCount) {
//Fill group with members
for(int i = 0; i < groupsSize; i++) {
int indexOfPotentialMember = i + (groupsFinished * 3); //Groups don't always fill all members neatly
//Some groups won't have entirely non-null elements
if(indexOfPotentialMember > components.size()-1) {
break;
}
Component potentialMember = components.get(indexOfPotentialMember);
//Make sure the potential member exists because of rounding
if(potentialMember != null) {
splitGroups[groupsFinished][i] = potentialMember;
}
}
//Another group is finished
groupsFinished++;
}
return splitGroups;
}
static void addChildWebComponent(@NotNull ComponentBuilder<?, ?> webTextComponent, @NotNull String childName) {
TextComponent childComponent = Component.text(childName).color(NamedTextColor.BLUE);
webTextComponent.append(childComponent);
}
static void addNewHoverComponentToTextComponent(@NotNull TextComponent.Builder textComponent, @NotNull Component baseComponent) {
textComponent.hoverEvent(HoverEvent.showText(baseComponent));
}
}