11package eu .pb4 .placeholders ;
22
3- import eu .pb4 .placeholders .builtin .PlayerPlaceholders ;
4- import eu .pb4 .placeholders .builtin .ServerPlaceholders ;
5- import net .fabricmc .api .ModInitializer ;
63import net .minecraft .server .MinecraftServer ;
74import net .minecraft .server .network .ServerPlayerEntity ;
85import net .minecraft .text .Text ;
96import net .minecraft .util .Identifier ;
107
118import java .util .HashMap ;
9+ import java .util .Map ;
1210import java .util .regex .Pattern ;
1311
12+ public class PlaceholderAPI {
13+ public static final Pattern PLACEHOLDER_PATTERN = Pattern .compile ("[%](?<id>[^%]+:[^%]+)[%]" );
14+ public static final Pattern ALT_PLACEHOLDER_PATTERN = Pattern .compile ("[{](?<id>[^{}]+:[^{}]+)[}]" );
1415
15- public class PlaceholderAPI implements ModInitializer {
16- public static final Pattern PLACEHOLDER_PATTERN = Pattern .compile ("[%]([^%]+:[^%]+)[%]" );
17- public static final Pattern ALT_PLACEHOLDER_PATTERN = Pattern .compile ("[{]([^{}]+:[^{}]+)[}]" );
16+ public static final Pattern PLACEHOLDER_PATTERN_CUSTOM = Pattern .compile ("[%](?<id>[^%]+)[%]" );
17+ public static final Pattern ALT_PLACEHOLDER_PATTERN_CUSTOM = Pattern .compile ("[{](?<id>[^{}]+)[}]" );
1818
1919 private static final HashMap <Identifier , PlaceholderHandler > PLACEHOLDERS = new HashMap <>();
2020
21+ /**
22+ * Parses PlaceholderContext, can be used for parsing by hand
23+ *
24+ * @return PlaceholderResult
25+ */
2126 public static PlaceholderResult parsePlaceholder (PlaceholderContext context ) {
2227 if (PLACEHOLDERS .containsKey (context .getIdentifier ())) {
2328 return PLACEHOLDERS .get (context .getIdentifier ()).PlaceholderHandler (context );
@@ -26,45 +31,131 @@ public static PlaceholderResult parsePlaceholder(PlaceholderContext context) {
2631 }
2732 }
2833
34+ /**
35+ * Parses placeholders (without formatting) within String for player
36+ * Placeholders have format of {@code %namespace:placeholder/argument%}
37+ *
38+ * @return String
39+ */
2940 public static String parseString (String text , ServerPlayerEntity player ) {
30- return Helpers .parseString (text , player , PLACEHOLDER_PATTERN );
41+ return Helpers .parseString (text , player , PLACEHOLDER_PATTERN , PLACEHOLDERS );
3142 }
3243
44+ /**
45+ * Parses placeholders (without formatting) within String
46+ * Placeholders have format of {@code %namespace:placeholder/argument%}
47+ *
48+ * @return String
49+ */
3350 public static String parseString (String text , MinecraftServer server ) {
34- return Helpers .parseString (text , server , PLACEHOLDER_PATTERN );
51+ return Helpers .parseString (text , server , PLACEHOLDER_PATTERN , PLACEHOLDERS );
3552 }
3653
54+ /**
55+ * Parses placeholders for player
56+ * Placeholders have format of {@code %namespace:placeholder/argument%}
57+ *
58+ * @return Text
59+ */
3760 public static Text parseText (Text text , ServerPlayerEntity player ) {
38- return Helpers .recursivePlaceholderParsing (text , player , PLACEHOLDER_PATTERN );
61+ return Helpers .recursivePlaceholderParsing (text , player , PLACEHOLDER_PATTERN , PLACEHOLDERS );
3962 }
4063
64+ /**
65+ * Parses placeholders
66+ * Placeholders have format of {@code %namespace:placeholder/argument%}
67+ *
68+ * @return Text
69+ */
4170 public static Text parseText (Text text , MinecraftServer server ) {
42- return Helpers .recursivePlaceholderParsing (text , server , PLACEHOLDER_PATTERN );
71+ return Helpers .recursivePlaceholderParsing (text , server , PLACEHOLDER_PATTERN , PLACEHOLDERS );
4372 }
4473
74+ /**
75+ * Parses placeholders (without formatting) within String for player
76+ * Placeholders have format of {@code \{namespace:placeholder/argument\}}
77+ *
78+ * @return String
79+ */
4580 public static String parseStringAlt (String text , ServerPlayerEntity player ) {
46- return Helpers .parseString (text , player , ALT_PLACEHOLDER_PATTERN );
81+ return Helpers .parseString (text , player , ALT_PLACEHOLDER_PATTERN , PLACEHOLDERS );
4782 }
4883
84+ /**
85+ * Parses placeholders (without formatting) within String
86+ * Placeholders have format of {@code \{namespace:placeholder/argument\}}
87+ *
88+ * @return String
89+ */
4990 public static String parseStringAlt (String text , MinecraftServer server ) {
50- return Helpers .parseString (text , server , ALT_PLACEHOLDER_PATTERN );
91+ return Helpers .parseString (text , server , ALT_PLACEHOLDER_PATTERN , PLACEHOLDERS );
5192 }
5293
94+ /**
95+ * Parses placeholders for player
96+ * Placeholders have format of {@code \{namespace:placeholder/argument\}}
97+ *
98+ * @return Text
99+ */
53100 public static Text parseTextAlt (Text text , ServerPlayerEntity player ) {
54- return Helpers .recursivePlaceholderParsing (text , player , ALT_PLACEHOLDER_PATTERN );
101+ return Helpers .recursivePlaceholderParsing (text , player , ALT_PLACEHOLDER_PATTERN , PLACEHOLDERS );
55102 }
56103
104+ /**
105+ * Parses placeholders
106+ * Placeholders have format of {@code \{namespace:placeholder/argument\}}
107+ *
108+ * @return Text
109+ */
57110 public static Text parseTextAlt (Text text , MinecraftServer server ) {
58- return Helpers .recursivePlaceholderParsing (text , server , ALT_PLACEHOLDER_PATTERN );
111+ return Helpers .recursivePlaceholderParsing (text , server , ALT_PLACEHOLDER_PATTERN , PLACEHOLDERS );
59112 }
60113
61- public static void register (Identifier identifier , PlaceholderHandler handler ) {
62- PLACEHOLDERS .put (identifier , handler );
114+ /**
115+ * Parses placeholders (without formatting) within String for player
116+ * Placeholders have format of {@code %namespace:placeholder/argument%}
117+ *
118+ * @return String
119+ */
120+ public static String parseStringCustom (String text , ServerPlayerEntity player , HashMap <Identifier , PlaceholderHandler > placeholders , Pattern pattern ) {
121+ return Helpers .parseString (text , player , pattern , placeholders );
122+ }
123+
124+ /**
125+ * Parses custom placeholders (without formatting) within String
126+ * Placeholders can have custom format
127+ *
128+ * @return String
129+ */
130+ public static String parseStringCustom (String text , MinecraftServer server , HashMap <Identifier , PlaceholderHandler > placeholders , Pattern pattern ) {
131+ return Helpers .parseString (text , server , pattern , placeholders );
132+ }
133+
134+ /**
135+ * Parses custom placeholders for player
136+ * Placeholders can have custom format
137+ *
138+ * @return Text
139+ */
140+ public static Text parseTextCustom (Text text , ServerPlayerEntity player , Map <Identifier , PlaceholderHandler > placeholders , Pattern pattern ) {
141+ return Helpers .recursivePlaceholderParsing (text , player , pattern , placeholders );
63142 }
64143
65- @ Override
66- public void onInitialize () {
67- ServerPlaceholders .register ();
68- PlayerPlaceholders .register ();
144+ /**
145+ * Parses custom placeholders
146+ * Placeholders can have custom format
147+ *
148+ * @return Text
149+ */
150+ public static Text parseTextCustom (Text text , MinecraftServer server , Map <Identifier , PlaceholderHandler > placeholders , Pattern pattern ) {
151+ return Helpers .recursivePlaceholderParsing (text , server , pattern , placeholders );
152+ }
153+
154+
155+ /**
156+ * Registers new placeholder for identifier
157+ */
158+ public static void register (Identifier identifier , PlaceholderHandler handler ) {
159+ PLACEHOLDERS .put (identifier , handler );
69160 }
70161}
0 commit comments