diff --git a/nms/interface/src/main/java/de/epiceric/shopchest/nms/TextComponentHelper.java b/nms/interface/src/main/java/de/epiceric/shopchest/nms/TextComponentHelper.java index 0078122..8a5012f 100644 --- a/nms/interface/src/main/java/de/epiceric/shopchest/nms/TextComponentHelper.java +++ b/nms/interface/src/main/java/de/epiceric/shopchest/nms/TextComponentHelper.java @@ -45,12 +45,15 @@ public interface TextComponentHelper { if (!pre.isEmpty()) { extras.addAll(Arrays.asList(TextComponent.fromLegacyText(pre))); } - extras.add(replacement); + extras.add(copyPreviousFormatting(extras, replacement)); cursor = matcher.end(); } while (matcher.find()); final String end = message.substring(cursor); 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 { @@ -61,4 +64,38 @@ public interface TextComponentHelper { return player -> player.spigot().sendMessage(baseComponent); } + static TextComponent copyPreviousFormatting(List 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; + } + }