33
44import net .md_5 .bungee .api .ChatColor ;
55import org .broken .arrow .library .color .TextTranslator ;
6- import org .broken .arrow .library .menu .utility .DuplicateMessage ;
6+ import org .broken .arrow .library .menu .utility .message .BlacklistItemWrapper ;
7+ import org .broken .arrow .library .menu .utility .message .BlacklistMessage ;
8+ import org .broken .arrow .library .menu .utility .message .DuplicateMessage ;
9+ import org .broken .arrow .library .menu .utility .message .DuplicatedItemWrapper ;
710import org .broken .arrow .library .serialize .utility .converters .PlaceholderTranslator ;
811import org .bukkit .entity .Player ;
912import org .bukkit .inventory .ItemStack ;
1518 * Set messages when player add duplicated items or items you have blacklisted.
1619 */
1720public class SendMsgDuplicatedItems {
18- private Function < ItemStack , String > blacklistMessage ;
21+ private BlacklistMessage blacklistMessage ;
1922 private DuplicateMessage duplicatedMessage ;
2023 private boolean notFoundTextTranslator ;
2124
@@ -35,10 +38,12 @@ public SendMsgDuplicatedItems() {
3538 * Set message for when player have added item some are blacklisted.
3639 * Support both hex and & color codes.
3740 *
38- * <p><b>Supported color formats :</b></p>
41+ * <p><b>Formatting support :</b></p>
3942 * <ul>
40- * <li>Hex colors: {@code <#8000ff>} or gradients like {@code <#8000ff:#ff0080>} (requires color conversion module).</li>
41- * <li>Fallback to Spigot's legacy format: {@code &x&6&6&6&6&6&6}, or for white: {@code &x&F&F&F&F&F&F}, if color conversion is not enabled.</li>
43+ * <li>Legacy color codes: {@code &a}, {@code &f}, etc.</li>
44+ * <li>Legacy hex format (Spigot-compatible): {@code &x&R&R&G&G&B&B}</li>
45+ * <li>Hex colors: {@code <#RRGGBB>} and gradients like {@code <#RRGGBB:#RRGGBB>}
46+ * (supported when the internal formatter is available)</li>
4247 * </ul>
4348 *
4449 * <p> </p>
@@ -48,7 +53,7 @@ public SendMsgDuplicatedItems() {
4853 * @param blacklistMessage set a message.
4954 */
5055 public void setBlacklistMessage (final String blacklistMessage ) {
51- this .blacklistMessage = itemStack -> blacklistMessage ;
56+ this .blacklistMessage = ( blacklistItemWrapper ) -> blacklistMessage ;
5257 }
5358
5459 /**
@@ -60,10 +65,12 @@ public void setBlacklistMessage(final String blacklistMessage) {
6065 * if you're using a localization file or want the text to be updated externally with minimal code changes.
6166 * </p>
6267 *
63- * <p><b>Supported color formats :</b></p>
68+ * <p><b>Formatting support :</b></p>
6469 * <ul>
65- * <li>Hex colors: {@code <#8000ff>} or gradients like {@code <#8000ff:#ff0080>} (requires the color conversion module).</li>
66- * <li>Fallback to Spigot's legacy format: {@code &x&6&6&6&6&6&6}, or for white: {@code &x&F&F&F&F&F&F}, if color conversion is not enabled.</li>
70+ * <li>Legacy color codes: {@code &a}, {@code &f}, etc.</li>
71+ * <li>Legacy hex format (Spigot-compatible): {@code &x&R&R&G&G&B&B}</li>
72+ * <li>Hex colors: {@code <#RRGGBB>} and gradients like {@code <#RRGGBB:#RRGGBB>}
73+ * (supported when the internal formatter is available)</li>
6774 * </ul>
6875 *
6976 * <p><b>Available placeholders:</b></p>
@@ -85,7 +92,7 @@ public void setBlacklistMessage(final String blacklistMessage) {
8592 *
8693 * @param blacklistMessage a function that receives the {@link ItemStack} and returns the base message string to process.
8794 */
88- public void setBlacklistMessage (final Function < ItemStack , String > blacklistMessage ) {
95+ public void setBlacklistMessage (final BlacklistMessage blacklistMessage ) {
8996 this .blacklistMessage = blacklistMessage ;
9097 }
9198
@@ -175,23 +182,30 @@ public void sendMessage(Player player, String msg) {
175182 * The message supports color codes and placeholder replacement.
176183 *
177184 * @param player the player to send the message to
178- * @param itemStack the blacklisted item stack triggering the message
185+ * @param blacklistItemWrapper the blacklist wrapper for the item stack triggering the message
179186 */
180- public void sendBlacklistMessage (Player player , ItemStack itemStack ) {
187+ public void sendBlacklistMessage (final Player player ,@ Nonnull final BlacklistItemWrapper blacklistItemWrapper ) {
181188 String message ;
182189 if (blacklistMessage == null ) {
183190 message = "&fThis item&6 {0}&f are blacklisted and you get the items back." ;
184191 } else {
185- message = blacklistMessage .apply (itemStack . clone () );
192+ message = blacklistMessage .apply (blacklistItemWrapper );
186193 }
187194
188195 if (message == null || message .isEmpty ())
189196 return ;
190197
191- String itemName = itemStack .getType ().name ().toLowerCase ();
198+ final PlaceholderTranslator .PlaceholderWrapper wrapper = blacklistItemWrapper .getPlaceholderWrapper ();
199+ if (!wrapper .getPlaceholders ().isEmpty ())
200+ message = PlaceholderTranslator .translateText (message , wrapper );
201+ else {
202+ message = PlaceholderTranslator .translateText (message , blacklistItemWrapper .retrieveAsPlaceholderData ());
203+ }
204+
192205 if (notFoundTextTranslator )
193- player .sendMessage (ChatColor .translateAlternateColorCodes ('&' , (translatePlaceholders (message , itemName ))));
194- else player .sendMessage (TextTranslator .toSpigotFormat (translatePlaceholders (message , itemName )));
206+ player .sendMessage (ChatColor .translateAlternateColorCodes ('&' , message ));
207+ else
208+ player .sendMessage (TextTranslator .toSpigotFormat (message ));
195209 }
196210
197211 /**
@@ -240,98 +254,5 @@ public String translatePlaceholders(String rawText, Object... placeholders) {
240254 return rawText ;
241255 }
242256
243- /**
244- * Wrapper class holding information about duplicated items for placeholder substitution.
245- */
246- public static class DuplicatedItemWrapper {
247- private final PlaceholderTranslator .PlaceholderWrapper placeholderWrapper ;
248- private final ItemStack itemStack ;
249- private final int size ;
250- private final int itemAmount ;
251-
252- /**
253- * Constructs a new wrapper containing duplicated item data.
254- *
255- * @param itemStack the duplicated item stack
256- * @param size the total number of duplicated stacks
257- * @param itemAmount the total number of duplicated items
258- */
259- public DuplicatedItemWrapper (final ItemStack itemStack , final int size , final int itemAmount ) {
260- this .placeholderWrapper = new PlaceholderTranslator .PlaceholderWrapper ();
261- this .itemStack = itemStack ;
262- this .size = size ;
263- this .itemAmount = itemAmount ;
264- }
265-
266- /**
267- * Returns the duplicated item stack.
268- *
269- * @return the item stack
270- */
271- public ItemStack getItemStack () {
272- return itemStack .clone ();
273- }
274-
275- /**
276- * Returns the total number of duplicated stacks.
277- *
278- * @return duplicated stacks count
279- */
280- public int getSize () {
281- return size ;
282- }
283-
284- /**
285- * Returns the total number of duplicated items.
286- *
287- * @return duplicated item count
288- */
289- public int getItemAmount () {
290- return itemAmount ;
291- }
292-
293- /**
294- * Returns a {@link PlaceholderTranslator.PlaceholderWrapper} for defining custom
295- * key-based placeholders instead of using the default indexed placeholder system.
296- *
297- * <p>If no custom placeholders are provided, the system falls back to
298- * {@link #retrieveAsPlaceholderData()}, which uses ordered placeholders such as
299- * {@code {0}}, {@code {1}}, {@code {2}}.</p>
300- *
301- * <p>When this wrapper contains entries, it is used instead and allows named
302- * placeholders.</p>
303- *
304- * <p><b>Example (default indexed placeholders):</b></p>
305- * <pre>
306- * {@code "&fYou can't add more if this &6 {0} &ftype, you get back &6 {2} &fitems. You have added totally &4 {1} &fextra itemstacks"}
307- * </pre>
308- *
309- * <p><b>Example (custom named placeholders):</b></p>
310- * <pre>
311- * {@code
312- * wrapper.put("{type}", itemStack.getType())
313- .put("{addedStacks}", size)
314- .put("{returnedItems}", itemAmount);
315- }
316- *
317- * "&fYou can't add more if this &6 {type} &ftype, you get back &6 {returnedItems} &fitems.
318- * You have added totally &4 {addedStacks} &fextra itemstacks"
319- * </pre>
320- *
321- * @return the {@link PlaceholderTranslator.PlaceholderWrapper} for custom placeholders
322- */
323- public PlaceholderTranslator .PlaceholderWrapper getPlaceholderWrapper () {
324- return placeholderWrapper ;
325- }
326257
327- /**
328- * Returns an array of objects to be used as placeholders in messages:
329- * item type, duplicated stacks count, and duplicated items count.
330- *
331- * @return an array of placeholder data objects
332- */
333- public Object [] retrieveAsPlaceholderData () {
334- return new Object []{itemStack .getType (), size , itemAmount };
335- }
336- }
337258}
0 commit comments