Fix format breaking with replacement in /shop info

This commit is contained in:
Flowsqy 2021-12-30 01:39:20 +01:00
parent 551b50b41b
commit d4cddcab37

View File

@ -45,12 +45,15 @@ public interface TextComponentHelper {
if (!pre.isEmpty()) { if (!pre.isEmpty()) {
extras.addAll(Arrays.asList(TextComponent.fromLegacyText(pre))); extras.addAll(Arrays.asList(TextComponent.fromLegacyText(pre)));
} }
extras.add(replacement); extras.add(copyPreviousFormatting(extras, replacement));
cursor = matcher.end(); cursor = matcher.end();
} while (matcher.find()); } while (matcher.find());
final String end = message.substring(cursor); final String end = message.substring(cursor);
if (!end.isEmpty()) { if (!end.isEmpty()) {
extras.addAll(Arrays.asList(TextComponent.fromLegacyText(end))); TextComponent endBaseComponent = new TextComponent();
endBaseComponent = copyPreviousFormatting(extras, endBaseComponent);
endBaseComponent.setExtra(Arrays.asList(TextComponent.fromLegacyText(end)));
extras.add(endBaseComponent);
} }
} }
else { else {
@ -61,4 +64,38 @@ public interface TextComponentHelper {
return player -> player.spigot().sendMessage(baseComponent); return player -> player.spigot().sendMessage(baseComponent);
} }
static TextComponent copyPreviousFormatting(List<BaseComponent> extras, TextComponent replacement){
TextComponent formattedReplacement = replacement;
if(!extras.isEmpty()) {
formattedReplacement = replacement.duplicate();
final BaseComponent previousComponent = extras.get(extras.size() - 1);
// Check parent also (not done in copyFormatting)
if (formattedReplacement.getColorRaw() == null) {
formattedReplacement.setColor(previousComponent.getColor());
}
if (formattedReplacement.getFontRaw() == null) {
formattedReplacement.setFont(previousComponent.getFont());
}
if (formattedReplacement.isBoldRaw() == null) {
formattedReplacement.setBold(previousComponent.isBold());
}
if (formattedReplacement.isItalicRaw() == null) {
formattedReplacement.setItalic(previousComponent.isItalic());
}
if (formattedReplacement.isUnderlinedRaw() == null) {
formattedReplacement.setUnderlined(previousComponent.isUnderlined());
}
if (formattedReplacement.isStrikethroughRaw() == null) {
formattedReplacement.setStrikethrough(previousComponent.isStrikethrough());
}
if (formattedReplacement.isObfuscatedRaw() == null) {
formattedReplacement.setObfuscated(previousComponent.isObfuscated());
}
if (formattedReplacement.getInsertion() == null) {
formattedReplacement.setInsertion(previousComponent.getInsertion());
}
}
return formattedReplacement;
}
} }