TextComponents will no longer use a cache

This will increase the cost of TextComponents by a negligible amount but in exchange I will be able to put player-specific data into them.
This commit is contained in:
nossr50 2019-01-12 19:29:06 -08:00
parent 85fd0a79bc
commit c8ee5099e0
2 changed files with 39 additions and 97 deletions

View File

@ -15,6 +15,7 @@ Version 2.1.0
+ Added links to mcMMO related websites to various commands + Added links to mcMMO related websites to various commands
+ Certain elements of mcMMO's UI have been restyled + Certain elements of mcMMO's UI have been restyled
+ Added the tagline "Overhaul Era" to various locations until 3.0.0 comes out + Added the tagline "Overhaul Era" to various locations until 3.0.0 comes out
+ (Sound) Volume and Pitch of sounds can now be configured in the new sounds.yml file
+ (MySQL) Added support for SSL for MySQL/MariaDB (On by default) + (MySQL) Added support for SSL for MySQL/MariaDB (On by default)
+ (Skills) Tool alerts now are sent to the Action Bar + (Skills) Tool alerts now are sent to the Action Bar
+ (Skills) Super Ability activation alerts are now sent to the Action Bar + (Skills) Super Ability activation alerts are now sent to the Action Bar

View File

@ -19,19 +19,22 @@ import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
/**
* This class handles many of the JSON components that mcMMO makes and uses
*/
public class TextComponentFactory { public class TextComponentFactory {
public static HashMap<String, TextComponent> subSkillTextComponents; //public static HashMap<String, TextComponent> subSkillTextComponents;
//Yeah there's probably a better way to do this //Yeah there's probably a better way to do this
public static HashMap<String, BaseComponent[]> lockedComponentMap; //public static HashMap<String, BaseComponent[]> lockedComponentMap;
public static BaseComponent[] webComponents; //public static BaseComponent[] webComponents;
/** /**
* This one is a bit simple * Makes a text component using strings from a locale and supports passing an undefined number of variables to the LocaleLoader
* @param localeKey * @param localeKey target locale string address
* @param notificationType * @param notificationType type of notification
* @param values * @param values vars to be passed to the locale loader
* @return * @return
*/ */
public static TextComponent getNotificationMultipleValues(String localeKey, NotificationType notificationType, String... values) public static TextComponent getNotificationMultipleValues(String localeKey, NotificationType notificationType, String... values)
@ -69,11 +72,11 @@ public class TextComponentFactory {
public static TextComponent getNotificationTextComponent(String text, NotificationType notificationType) public static TextComponent getNotificationTextComponent(String text, NotificationType notificationType)
{ {
TextComponent textComponent = new TextComponent(text); TextComponent textComponent = new TextComponent(text);
textComponent.setColor(getNotificationColor(notificationType)); //textComponent.setColor(getNotificationColor(notificationType));
return textComponent; return textComponent;
} }
public static ChatColor getNotificationColor(NotificationType notificationType) /*public static ChatColor getNotificationColor(NotificationType notificationType)
{ {
ChatColor color = ChatColor.WHITE; ChatColor color = ChatColor.WHITE;
switch(notificationType) switch(notificationType)
@ -93,7 +96,7 @@ public class TextComponentFactory {
} }
return color; return color;
} }*/
public static void sendPlayerUrlHeader(Player player) { public static void sendPlayerUrlHeader(Player player) {
if(!Config.getInstance().getUrlLinksEnabled()) if(!Config.getInstance().getUrlLinksEnabled())
@ -101,11 +104,11 @@ public class TextComponentFactory {
Player.Spigot spigotPlayer = player.spigot(); Player.Spigot spigotPlayer = player.spigot();
if(webComponents != null) /*if(webComponents != null)
{ {
player.spigot().sendMessage(webComponents); player.spigot().sendMessage(webComponents);
return; return;
} }*/
TextComponent prefix = new TextComponent("[| "); TextComponent prefix = new TextComponent("[| ");
prefix.setColor(ChatColor.DARK_AQUA); prefix.setColor(ChatColor.DARK_AQUA);
@ -128,9 +131,7 @@ public class TextComponentFactory {
getWebLinkTextComponent(McMMOWebLinks.HELP_TRANSLATE), getWebLinkTextComponent(McMMOWebLinks.HELP_TRANSLATE),
new TextComponent(suffix)}; new TextComponent(suffix)};
//Cache into memory since the links wont change spigotPlayer.sendMessage(baseComponents);
webComponents = baseComponents;
spigotPlayer.sendMessage(webComponents);
} }
public static void sendPlayerSubSkillList(Player player, List<TextComponent> textComponents) public static void sendPlayerSubSkillList(Player player, List<TextComponent> textComponents)
@ -296,20 +297,11 @@ public class TextComponentFactory {
public static TextComponent getSubSkillTextComponent(Player player, SubSkillType subSkillType) public static TextComponent getSubSkillTextComponent(Player player, SubSkillType subSkillType)
{ {
//Init our maps
if (subSkillTextComponents == null)
{
subSkillTextComponents = new HashMap<>();
lockedComponentMap = new HashMap<>();
//hoverComponentOuterMap = new HashMap<>();
}
//Get skill name & description from our locale file //Get skill name & description from our locale file
String key = subSkillType.toString(); //String key = subSkillType.toString();
String skillName = subSkillType.getLocaleName(); String skillName = subSkillType.getLocaleName();
if(subSkillTextComponents.get(key) == null)
{
//Setup Text Component //Setup Text Component
TextComponent textComponent = new TextComponent(skillName); TextComponent textComponent = new TextComponent(skillName);
//textComponent.setColor(ChatColor.DARK_AQUA); //textComponent.setColor(ChatColor.DARK_AQUA);
@ -321,29 +313,14 @@ public class TextComponentFactory {
textComponent.setInsertion(skillName); textComponent.setInsertion(skillName);
textComponent.setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/mmoinfo "+subSkillType.getNiceNameNoSpaces(subSkillType))); textComponent.setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/mmoinfo "+subSkillType.getNiceNameNoSpaces(subSkillType)));
subSkillTextComponents.put(key, textComponent); return textComponent;
return subSkillTextComponents.get(key);
} else {
return subSkillTextComponents.get(key);
}
} }
public static TextComponent getSubSkillTextComponent(Player player, AbstractSubSkill abstractSubSkill) public static TextComponent getSubSkillTextComponent(Player player, AbstractSubSkill abstractSubSkill)
{ {
//Init our maps //String key = abstractSubSkill.getConfigKeyName();
if (subSkillTextComponents == null)
{
subSkillTextComponents = new HashMap<>();
lockedComponentMap = new HashMap<>();
//hoverComponentOuterMap = new HashMap<>();
}
//Get skill name & description from our locale file
String key = abstractSubSkill.getConfigKeyName();
String skillName = abstractSubSkill.getNiceName(); String skillName = abstractSubSkill.getNiceName();
if(subSkillTextComponents.get(key) == null)
{
//Setup Text Component //Setup Text Component
TextComponent textComponent = new TextComponent(skillName); TextComponent textComponent = new TextComponent(skillName);
textComponent.setColor(ChatColor.DARK_AQUA); textComponent.setColor(ChatColor.DARK_AQUA);
@ -355,11 +332,7 @@ public class TextComponentFactory {
textComponent.setInsertion(skillName); textComponent.setInsertion(skillName);
textComponent.setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/mmoinfo "+abstractSubSkill.getConfigKeyName())); textComponent.setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/mmoinfo "+abstractSubSkill.getConfigKeyName()));
subSkillTextComponents.put(key, textComponent); return textComponent;
return subSkillTextComponents.get(key);
} else {
return subSkillTextComponents.get(key);
}
} }
private static BaseComponent[] getBaseComponent(Player player, AbstractSubSkill abstractSubSkill) private static BaseComponent[] getBaseComponent(Player player, AbstractSubSkill abstractSubSkill)
@ -370,25 +343,10 @@ public class TextComponentFactory {
//If the player hasn't unlocked this skill yet we use a different JSON template //If the player hasn't unlocked this skill yet we use a different JSON template
if(abstractSubSkill.getNumRanks() > 0 && curRank == 0) if(abstractSubSkill.getNumRanks() > 0 && curRank == 0)
{ {
//If the JSON component already exists
if(lockedComponentMap.get(key) != null)
return lockedComponentMap.get(key);
BaseComponent[] newComponents = getSubSkillHoverEventJSON(abstractSubSkill, player, curRank); BaseComponent[] newComponents = getSubSkillHoverEventJSON(abstractSubSkill, player, curRank);
lockedComponentMap.put(key, newComponents); return newComponents;
return lockedComponentMap.get(key);
} }
//If the inner hashmap for this rank isn't made yet
/*if(hoverComponentOuterMap.get(curRank) == null)
hoverComponentOuterMap.put(curRank, new HashMap<>());*/
//Inner Hashmap for current rank
//HashMap<String, BaseComponent[]> innerMap = hoverComponentOuterMap.get(curRank);
/*if(innerMap.get(key) == null)
innerMap.put(key, getSubSkillHoverEventJSON(abstractSubSkill, player, curRank));*/
return getSubSkillHoverEventJSON(abstractSubSkill, player, curRank); return getSubSkillHoverEventJSON(abstractSubSkill, player, curRank);
} }
@ -400,27 +358,10 @@ public class TextComponentFactory {
//If the player hasn't unlocked this skill yet we use a different JSON template //If the player hasn't unlocked this skill yet we use a different JSON template
if(subSkillType.getNumRanks() > 0 && curRank == 0) if(subSkillType.getNumRanks() > 0 && curRank == 0)
{ {
//If the JSON component already exists
if(lockedComponentMap.get(key) != null)
return lockedComponentMap.get(key);
BaseComponent[] newComponents = getSubSkillHoverEventJSON(subSkillType, player, curRank); BaseComponent[] newComponents = getSubSkillHoverEventJSON(subSkillType, player, curRank);
lockedComponentMap.put(key, newComponents); return newComponents;
return lockedComponentMap.get(key);
} }
//If the inner hashmap for this rank isn't made yet
/*if(hoverComponentOuterMap.get(curRank) == null)
hoverComponentOuterMap.put(curRank, new HashMap<>());
//Inner Hashmap for current rank
HashMap<String, BaseComponent[]> innerMap = hoverComponentOuterMap.get(curRank);*/
/*if(innerMap.get(key) == null)
innerMap.put(key, getSubSkillHoverEventJSON(subSkillType, player, curRank));
return innerMap.get(key);*/
return getSubSkillHoverEventJSON(subSkillType, player, curRank); return getSubSkillHoverEventJSON(subSkillType, player, curRank);
} }