Skip to content

Commit

Permalink
ElementTag.parse/to_minimessage tags (#2391)
Browse files Browse the repository at this point in the history
* WIP MiniMessage tags

* No need for a dedicated method

* MiniMessage was only added to Paper on 1.18

* Link MiniMessage docs
  • Loading branch information
tal5 committed Oct 19, 2022
1 parent 3e545c2 commit b80bc3a
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 0 deletions.
Expand Up @@ -76,6 +76,7 @@ public static void init() {
PropertyParser.registerProperty(PaperItemTagProperties.class, ItemTag.class);
PropertyParser.registerProperty(PaperWorldProperties.class, WorldTag.class);
PaperPlayerExtensions.register();
PaperElementExtensions.register();

// Paper Tags
new PaperTagBase();
Expand Down
@@ -0,0 +1,43 @@
package com.denizenscript.denizen.paper.properties;

import com.denizenscript.denizen.nms.NMSHandler;
import com.denizenscript.denizen.nms.NMSVersion;
import com.denizenscript.denizen.paper.PaperModule;
import com.denizenscript.denizen.utilities.FormattedTextHelper;
import com.denizenscript.denizencore.objects.core.ElementTag;
import net.kyori.adventure.text.minimessage.MiniMessage;
import net.md_5.bungee.api.ChatColor;
import net.md_5.bungee.chat.ComponentSerializer;

public class PaperElementExtensions {


public static void register() {
if (NMSHandler.getVersion().isAtLeast(NMSVersion.v1_18)) {

// <--[tag]
// @attribute <ElementTag.parse_minimessage>
// @returns ElementTag
// @Plugin Paper
// @description
// Returns the element with all MiniMessage tags parsed, see <@link url https://docs.adventure.kyori.net/minimessage/format.html> for more information.
// -->
ElementTag.tagProcessor.registerTag(ElementTag.class, "parse_minimessage", (attribute, object) -> {
return new ElementTag(PaperModule.stringifyComponent(MiniMessage.miniMessage().deserialize(object.asString())));
});

// <--[tag]
// @attribute <ElementTag.to_minimessage>
// @returns ElementTag
// @Plugin Paper
// @description
// Returns the element with all text formatting parsed into MiniMessage format.
// -->
ElementTag.tagProcessor.registerTag(ElementTag.class, "to_minimessage", (attribute, object) -> {
return new ElementTag(MiniMessage.miniMessage().serialize(
PaperModule.jsonToComponent(ComponentSerializer.toString(FormattedTextHelper.parse(object.asString(), ChatColor.WHITE, false)))
));
});
}
}
}
Expand Up @@ -364,6 +364,7 @@ public static void register() {
// Returns the element with all color codes parsed.
// Optionally, specify a character to prefix the color ids. Defaults to '&' if not specified.
// This allows old-style colors like '&b', or Essentials-style hex codes like '&#ff00ff'
// See also <@link tag ElementTag.parse_minimessage>
// -->
ElementTag.tagProcessor.registerStaticTag(ElementTag.class, "parse_color", (attribute, object) -> {
char prefix = '&';
Expand Down

0 comments on commit b80bc3a

Please sign in to comment.