2012-06-08 15:28:36 +02:00
package com.Acrobot.Breeze.Utils ;
2018-03-12 21:01:28 +01:00
import com.Acrobot.Breeze.Collection.SimpleCache ;
2013-01-24 22:35:28 +01:00
import com.Acrobot.ChestShop.ChestShop ;
2020-06-28 19:00:09 +02:00
import com.Acrobot.ChestShop.Configuration.Messages ;
2018-03-12 21:01:28 +01:00
import com.Acrobot.ChestShop.Configuration.Properties ;
2019-01-17 19:50:47 +01:00
import com.Acrobot.ChestShop.Events.MaterialParseEvent ;
2021-01-03 01:55:01 +01:00
import com.Acrobot.ChestShop.Utils.ItemUtil ;
2018-01-06 17:44:35 +01:00
import de.themoep.ShowItem.api.ShowItem ;
2020-12-01 19:48:26 +01:00
import de.themoep.minedown.adventure.Replacer ;
import net.kyori.adventure.text.Component ;
2020-12-04 14:53:27 +01:00
import net.kyori.adventure.text.TextComponent ;
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer ;
2019-01-17 19:50:47 +01:00
import org.bukkit.Bukkit ;
2012-06-08 15:28:36 +02:00
import org.bukkit.Material ;
2019-07-08 19:05:50 +02:00
import org.bukkit.configuration.file.YamlConstructor ;
import org.bukkit.configuration.file.YamlRepresenter ;
2018-01-06 17:44:35 +01:00
import org.bukkit.entity.Player ;
2012-06-08 15:28:36 +02:00
import org.bukkit.inventory.ItemStack ;
2018-09-10 02:11:42 +02:00
import org.bukkit.inventory.meta.Damageable ;
2013-01-24 22:35:28 +01:00
import org.bukkit.inventory.meta.ItemMeta ;
2018-01-06 17:44:35 +01:00
import org.bukkit.plugin.Plugin ;
2019-07-08 19:05:50 +02:00
import org.yaml.snakeyaml.DumperOptions ;
import org.yaml.snakeyaml.Yaml ;
import org.yaml.snakeyaml.nodes.Tag ;
2012-06-08 15:28:36 +02:00
2018-01-06 17:44:35 +01:00
import java.util.ArrayList ;
2017-11-15 21:08:32 +01:00
import java.util.Collection ;
2020-06-28 19:00:09 +02:00
import java.util.LinkedHashMap ;
2018-01-06 17:44:35 +01:00
import java.util.List ;
2020-01-29 16:35:04 +01:00
import java.util.Locale ;
2019-07-08 19:05:50 +02:00
import java.util.Map ;
2018-01-06 17:44:35 +01:00
import java.util.logging.Level ;
2012-06-08 15:28:36 +02:00
import java.util.regex.Matcher ;
import java.util.regex.Pattern ;
2018-09-09 15:37:09 +02:00
import static com.Acrobot.Breeze.Utils.StringUtil.getMinecraftCharWidth ;
import static com.Acrobot.Breeze.Utils.StringUtil.getMinecraftStringWidth ;
2012-06-08 15:28:36 +02:00
/ * *
* @author Acrobot
* /
public class MaterialUtil {
2012-10-21 22:45:30 +02:00
public static final Pattern DURABILITY = Pattern . compile ( " :( \\ d)* " ) ;
2013-01-24 22:35:28 +01:00
public static final Pattern METADATA = Pattern . compile ( " #([0-9a-zA-Z])* " ) ;
2012-06-08 15:28:36 +02:00
2013-01-15 21:33:00 +01:00
public static final boolean LONG_NAME = true ;
public static final boolean SHORT_NAME = false ;
2018-09-09 15:37:09 +02:00
/ * *
* @deprecated Use { @link MaterialUtil # MAXIMUM_SIGN_WIDTH }
* /
@Deprecated
2018-03-12 21:01:28 +01:00
public static final short MAXIMUM_SIGN_LETTERS = 15 ;
2018-09-09 15:37:09 +02:00
// 15 dashes fit on one sign line with the default resource pack:
public static final int MAXIMUM_SIGN_WIDTH = ( short ) getMinecraftStringWidth ( " --------------- " ) ;
2018-05-07 16:32:51 +02:00
2018-03-12 21:01:28 +01:00
private static final SimpleCache < String , Material > MATERIAL_CACHE = new SimpleCache < > ( Properties . CACHE_SIZE ) ;
2013-01-15 21:33:00 +01:00
2019-07-08 19:05:50 +02:00
private static final Yaml YAML = new Yaml ( new YamlBukkitConstructor ( ) , new YamlRepresenter ( ) , new DumperOptions ( ) ) ;
private static class YamlBukkitConstructor extends YamlConstructor {
public YamlBukkitConstructor ( ) {
this . yamlConstructors . put ( new Tag ( Tag . PREFIX + " org.bukkit.inventory.ItemStack " ) , yamlConstructors . get ( Tag . MAP ) ) ;
}
}
2012-06-08 15:28:36 +02:00
/ * *
* Checks if the itemStack is empty or null
*
* @param item Item to check
* @return Is the itemStack empty ?
* /
public static boolean isEmpty ( ItemStack item ) {
return item = = null | | item . getType ( ) = = Material . AIR ;
}
/ * *
* Checks if the itemStacks are equal , ignoring their amount
*
* @param one first itemStack
* @param two second itemStack
* @return Are they equal ?
* /
public static boolean equals ( ItemStack one , ItemStack two ) {
2017-05-04 19:54:49 +02:00
if ( one = = null | | two = = null ) {
2018-01-08 23:56:58 +01:00
return one = = two ;
2017-05-04 19:54:49 +02:00
}
if ( one . isSimilar ( two ) ) {
return true ;
}
2018-05-07 16:32:51 +02:00
2019-03-19 19:08:53 +01:00
// Additional checks as serialisation and de-serialisation might lead to different item meta
// This would only be done if the items share the same item meta type so it shouldn't be too inefficient
2019-07-08 19:05:50 +02:00
// Special check for books as their pages might change when serialising (See SPIGOT-3206 and ChestShop#250)
2019-03-19 19:08:53 +01:00
// Special check for explorer maps/every item with a localised name (See SPIGOT-4672)
2019-09-06 14:23:49 +02:00
// Special check for legacy spawn eggs (See ChestShop#264)
2019-07-08 19:05:50 +02:00
if ( one . getType ( ) ! = two . getType ( )
| | one . getDurability ( ) ! = two . getDurability ( )
2019-09-06 14:23:49 +02:00
| | ( one . hasItemMeta ( ) & & two . hasItemMeta ( ) & & one . getItemMeta ( ) . getClass ( ) ! = two . getItemMeta ( ) . getClass ( ) ) ) {
2019-07-08 19:05:50 +02:00
return false ;
}
2019-09-06 14:23:49 +02:00
if ( ! one . hasItemMeta ( ) & & ! two . hasItemMeta ( ) ) {
return true ;
}
2019-07-08 19:05:50 +02:00
Map < String , Object > oneSerMeta = one . getItemMeta ( ) . serialize ( ) ;
Map < String , Object > twoSerMeta = two . getItemMeta ( ) . serialize ( ) ;
if ( oneSerMeta . equals ( twoSerMeta ) ) {
return true ;
}
// Try to use same parsing as the YAML dumper in the ItemDatabase when generating the code as the last resort
ItemStack oneDumped = YAML . loadAs ( YAML . dump ( one ) , ItemStack . class ) ;
if ( oneDumped . isSimilar ( two ) | | oneDumped . getItemMeta ( ) . serialize ( ) . equals ( twoSerMeta ) ) {
return true ;
}
ItemStack twoDumped = YAML . loadAs ( YAML . dump ( two ) , ItemStack . class ) ;
if ( oneDumped . isSimilar ( twoDumped ) | | oneDumped . getItemMeta ( ) . serialize ( ) . equals ( twoDumped . getItemMeta ( ) . serialize ( ) ) ) {
return true ;
}
return false ;
2012-06-08 15:28:36 +02:00
}
/ * *
* Gives you a Material from a String ( doesn ' t have to be fully typed in )
*
* @param name Name of the material
* @return Material found
* /
public static Material getMaterial ( String name ) {
2020-07-08 22:40:06 +02:00
String formatted = name . replaceAll ( " (?<!^)([A-Z1-9]) " , " _$1 " ) . replace ( ' ' , '_' ) . toUpperCase ( Locale . ROOT ) ;
2012-06-08 15:28:36 +02:00
2018-03-12 21:01:28 +01:00
Material material = MATERIAL_CACHE . get ( formatted ) ;
if ( material ! = null ) {
return material ;
2013-02-11 01:09:12 +01:00
}
2013-02-11 17:09:34 +01:00
2018-03-12 21:01:28 +01:00
material = Material . matchMaterial ( name ) ;
2013-02-11 17:09:34 +01:00
2012-06-08 15:28:36 +02:00
if ( material ! = null ) {
2013-02-11 17:09:34 +01:00
MATERIAL_CACHE . put ( formatted , material ) ;
2012-06-08 15:28:36 +02:00
return material ;
}
2018-03-16 18:30:51 +01:00
material = new EnumParser < Material > ( ) . parse ( name , Material . values ( ) ) ;
if ( material ! = null ) {
MATERIAL_CACHE . put ( formatted , material ) ;
}
2013-02-11 17:09:34 +01:00
2012-06-08 15:28:36 +02:00
return material ;
}
2018-08-23 23:10:24 +02:00
/ * *
* Get a list with item information
*
* @param items The items to get the information from
* @return The list , including the amount and names of the items
2021-01-03 01:55:01 +01:00
* @deprecated Use { @link ItemUtil # getItemList ( ItemStack [ ] ) } instead !
2018-08-23 23:10:24 +02:00
* /
2021-01-03 01:55:01 +01:00
@Deprecated
2018-08-23 23:10:24 +02:00
public static String getItemList ( ItemStack [ ] items ) {
ItemStack [ ] mergedItems = InventoryUtil . mergeSimilarStacks ( items ) ;
List < String > itemText = new ArrayList < > ( ) ;
for ( ItemStack item : mergedItems ) {
itemText . add ( item . getAmount ( ) + " " + getName ( item ) ) ;
}
return String . join ( " , " , itemText ) ;
}
2012-06-08 15:28:36 +02:00
/ * *
* Returns item ' s name
2021-01-03 01:55:01 +01:00
* Use { @link ItemUtil # getName ( ItemStack , int ) } if you want to get name aliases too !
2012-06-08 15:28:36 +02:00
*
* @param itemStack ItemStack to name
* @return ItemStack ' s name
* /
public static String getName ( ItemStack itemStack ) {
2018-03-12 21:01:28 +01:00
return getName ( itemStack , 0 ) ;
2012-06-08 15:28:36 +02:00
}
/ * *
* Returns item ' s name
*
* @param itemStack ItemStack to name
* @param showDataValue Should we also show the data value ?
* @return ItemStack ' s name
2018-03-12 21:01:28 +01:00
* @deprecated Use { @link # getName ( ItemStack , int ) }
2012-06-08 15:28:36 +02:00
* /
2018-03-12 21:01:28 +01:00
@Deprecated
2012-06-08 15:28:36 +02:00
public static String getName ( ItemStack itemStack , boolean showDataValue ) {
2018-07-26 01:13:06 +02:00
return getName ( itemStack , 0 ) ;
2012-06-08 15:28:36 +02:00
}
/ * *
* Returns item ' s name , just like on the sign
2021-01-03 01:55:01 +01:00
* Use { @link ItemUtil # getSignName ( ItemStack ) } if you want to get name aliases too !
2012-06-08 15:28:36 +02:00
*
* @param itemStack ItemStack to name
* @return ItemStack ' s name
* /
public static String getSignName ( ItemStack itemStack ) {
2018-09-09 15:37:09 +02:00
return getName ( itemStack , MAXIMUM_SIGN_WIDTH ) ;
2018-03-12 21:01:28 +01:00
}
2018-05-07 16:32:51 +02:00
2018-03-12 21:01:28 +01:00
/ * *
2021-01-03 01:55:01 +01:00
* Returns item ' s name , with a maximum width .
* Use { @link ItemUtil # getName ( ItemStack , int ) } if you want to get name aliases too !
2018-03-12 21:01:28 +01:00
*
* @param itemStack ItemStack to name
2018-09-09 15:37:09 +02:00
* @param maxWidth The max width that the name should have ; 0 or below if it should be unlimited
2018-03-12 21:01:28 +01:00
* @return ItemStack ' s name
* /
2018-09-09 15:37:09 +02:00
public static String getName ( ItemStack itemStack , int maxWidth ) {
2021-01-03 01:55:01 +01:00
String itemName = itemStack . getType ( ) . toString ( ) ;
2018-05-07 16:32:51 +02:00
2018-03-12 21:01:28 +01:00
String durability = " " ;
2018-07-26 01:13:06 +02:00
if ( itemStack . getDurability ( ) ! = 0 ) {
durability = " : " + itemStack . getDurability ( ) ;
2012-06-08 15:28:36 +02:00
}
2018-03-12 21:01:28 +01:00
String metaData = " " ;
2013-02-07 14:16:50 +01:00
if ( itemStack . hasItemMeta ( ) ) {
2018-03-12 21:01:28 +01:00
metaData = " # " + Metadata . getItemCode ( itemStack ) ;
}
2018-05-07 16:32:51 +02:00
2018-09-10 01:31:26 +02:00
String code = StringUtil . capitalizeFirstLetter ( itemName , '_' ) ;
2018-09-09 23:43:49 +02:00
int codeWidth = getMinecraftStringWidth ( code + durability + metaData ) ;
2018-09-09 15:37:09 +02:00
if ( maxWidth > 0 & & codeWidth > maxWidth ) {
int exceeding = codeWidth - maxWidth ;
code = getShortenedName ( code , getMinecraftStringWidth ( code ) - exceeding ) ;
2018-03-12 21:01:28 +01:00
}
2018-05-07 16:32:51 +02:00
2021-01-03 01:55:01 +01:00
return code + durability + metaData ;
2012-06-08 15:28:36 +02:00
}
2018-05-07 16:32:51 +02:00
2018-03-14 15:07:13 +01:00
/ * *
* Get an item name shortened to a max length that is still reversable by { @link # getMaterial ( String ) }
2018-05-07 16:32:51 +02:00
*
2018-03-14 15:07:13 +01:00
* @param itemName The name of the item
2018-09-09 15:37:09 +02:00
* @param maxWidth The max width
2018-03-14 15:07:13 +01:00
* @return The name shortened to the max length
* /
2018-09-09 15:37:09 +02:00
public static String getShortenedName ( String itemName , int maxWidth ) {
2018-09-10 00:08:49 +02:00
itemName = StringUtil . capitalizeFirstLetter ( itemName . replace ( '_' , ' ' ) , ' ' ) ;
2018-09-09 15:37:09 +02:00
int width = getMinecraftStringWidth ( itemName ) ;
if ( width < = maxWidth ) {
2018-03-14 15:07:13 +01:00
return itemName ;
}
2018-09-09 23:43:49 +02:00
String [ ] itemParts = itemName . split ( " " ) ;
2018-09-10 01:33:27 +02:00
itemName = String . join ( " " , itemParts ) ;
width = getMinecraftStringWidth ( itemName ) ;
if ( width < = maxWidth ) {
return itemName ;
}
int exceeding = width - maxWidth ;
2018-03-14 15:07:13 +01:00
int shortestIndex = 0 ;
int longestIndex = 0 ;
for ( int i = 0 ; i < itemParts . length ; i + + ) {
2018-09-09 15:37:09 +02:00
if ( getMinecraftStringWidth ( itemParts [ longestIndex ] ) < getMinecraftStringWidth ( itemParts [ i ] ) ) {
2018-03-14 15:07:13 +01:00
longestIndex = i ;
}
2018-09-09 15:37:09 +02:00
if ( getMinecraftStringWidth ( itemParts [ shortestIndex ] ) > getMinecraftStringWidth ( itemParts [ i ] ) ) {
2018-03-14 15:07:13 +01:00
shortestIndex = i ;
}
}
2018-09-09 23:43:49 +02:00
int shortestWidth = getMinecraftStringWidth ( itemParts [ shortestIndex ] ) ;
int longestWidth = getMinecraftStringWidth ( itemParts [ longestIndex ] ) ;
int remove = longestWidth - shortestWidth ;
while ( remove > 0 & & exceeding > 0 ) {
int endWidth = getMinecraftCharWidth ( itemParts [ longestIndex ] . charAt ( itemParts [ longestIndex ] . length ( ) - 1 ) ) ;
itemParts [ longestIndex ] = itemParts [ longestIndex ] . substring ( 0 , itemParts [ longestIndex ] . length ( ) - 1 ) ;
remove - = endWidth ;
exceeding - = endWidth ;
}
2018-07-26 01:13:06 +02:00
for ( int i = itemParts . length - 1 ; i > = 0 & & exceeding > 0 ; i - - ) {
2018-09-09 15:47:32 +02:00
int partWidth = getMinecraftStringWidth ( itemParts [ i ] ) ;
if ( partWidth > shortestWidth ) {
remove = partWidth - shortestWidth ;
2018-03-14 15:07:13 +01:00
}
2018-09-09 15:47:32 +02:00
2018-07-26 01:13:06 +02:00
if ( remove > exceeding ) {
remove = exceeding ;
2018-03-14 15:07:13 +01:00
}
2018-09-09 15:47:32 +02:00
2018-09-09 15:37:09 +02:00
while ( remove > 0 ) {
2018-09-09 15:47:32 +02:00
int endWidth = getMinecraftCharWidth ( itemParts [ i ] . charAt ( itemParts [ i ] . length ( ) - 1 ) ) ;
itemParts [ i ] = itemParts [ i ] . substring ( 0 , itemParts [ i ] . length ( ) - 1 ) ;
2018-09-09 15:37:09 +02:00
remove - = endWidth ;
2018-09-09 15:47:32 +02:00
exceeding - = endWidth ;
2018-09-09 15:37:09 +02:00
}
2018-03-14 15:07:13 +01:00
}
2018-09-09 15:37:09 +02:00
2018-07-26 01:13:06 +02:00
while ( exceeding > 0 ) {
for ( int i = itemParts . length - 1 ; i > = 0 & & exceeding > 0 ; i - - ) {
2018-09-09 15:47:32 +02:00
int endWidth = getMinecraftCharWidth ( itemParts [ i ] . charAt ( itemParts [ i ] . length ( ) - 1 ) ) ;
2018-07-26 01:13:06 +02:00
itemParts [ i ] = itemParts [ i ] . substring ( 0 , itemParts [ i ] . length ( ) - 1 ) ;
2018-09-09 15:47:32 +02:00
exceeding - = endWidth ;
2018-07-26 01:13:06 +02:00
}
}
2018-09-10 01:33:27 +02:00
return String . join ( " " , itemParts ) ;
2018-03-14 15:07:13 +01:00
}
2018-05-07 16:32:51 +02:00
2012-06-08 15:28:36 +02:00
/ * *
* Gives you an ItemStack from a String
*
* @param itemName Item name
* @return ItemStack
* /
public static ItemStack getItem ( String itemName ) {
2017-09-23 16:19:00 +02:00
String [ ] split = itemName . split ( " [: \\ -#] " ) ;
for ( int i = 0 ; i < split . length ; i + + ) {
split [ i ] = split [ i ] . trim ( ) ;
}
2012-06-08 15:28:36 +02:00
2013-02-08 19:29:13 +01:00
short durability = getDurability ( itemName ) ;
2019-01-17 19:50:47 +01:00
MaterialParseEvent parseEvent = new MaterialParseEvent ( split [ 0 ] , durability ) ;
Bukkit . getPluginManager ( ) . callEvent ( parseEvent ) ;
Material material = parseEvent . getMaterial ( ) ;
2012-06-08 15:28:36 +02:00
if ( material = = null ) {
2018-07-26 01:13:06 +02:00
return null ;
2012-06-08 15:28:36 +02:00
}
2021-01-03 01:55:01 +01:00
ItemStack itemStack = new ItemStack ( material ) ;
2012-06-08 15:28:36 +02:00
2013-01-24 22:35:28 +01:00
ItemMeta meta = getMetadata ( itemName ) ;
if ( meta ! = null ) {
2018-09-10 02:11:42 +02:00
if ( meta instanceof Damageable ) {
( ( Damageable ) meta ) . setDamage ( durability ) ;
}
2013-01-24 22:35:28 +01:00
itemStack . setItemMeta ( meta ) ;
}
2013-02-11 17:09:34 +01:00
2012-06-08 15:28:36 +02:00
return itemStack ;
}
/ * *
* Returns the durability from a string
*
* @param itemName Item name
* @return Durability found
* /
public static short getDurability ( String itemName ) {
Matcher m = DURABILITY . matcher ( itemName ) ;
if ( ! m . find ( ) ) {
return 0 ;
}
String data = m . group ( ) ;
if ( data = = null | | data . isEmpty ( ) ) {
return 0 ;
}
data = data . substring ( 1 ) ;
2012-10-21 22:45:30 +02:00
return NumberUtil . isShort ( data ) ? Short . valueOf ( data ) : 0 ;
2012-06-08 15:28:36 +02:00
}
2013-01-24 22:35:28 +01:00
/ * *
* Returns metadata from a string
*
* @param itemName Item name
* @return Metadata found
* /
public static ItemMeta getMetadata ( String itemName ) {
Matcher m = METADATA . matcher ( itemName ) ;
if ( ! m . find ( ) ) {
return null ;
}
String group = m . group ( ) . substring ( 1 ) ;
return Metadata . getFromCode ( group ) ;
}
2018-03-16 18:30:51 +01:00
private static class EnumParser < E extends Enum < E > > {
private E parse ( String name , E [ ] values ) {
try {
2020-01-29 16:35:04 +01:00
return E . valueOf ( values [ 0 ] . getDeclaringClass ( ) , name . toUpperCase ( Locale . ROOT ) ) ;
2018-03-16 18:30:51 +01:00
} catch ( IllegalArgumentException exception ) {
E currentEnum = null ;
2020-07-08 22:40:06 +02:00
String [ ] typeParts = name . replaceAll ( " (?<!^)([A-Z1-9]) " , " _$1 " ) . toUpperCase ( Locale . ROOT ) . split ( " [ _] " ) ;
2018-03-16 18:30:51 +01:00
int length = Short . MAX_VALUE ;
for ( E e : values ) {
String enumName = e . name ( ) ;
if ( enumName . length ( ) < length & & enumName . startsWith ( name ) ) {
length = ( short ) enumName . length ( ) ;
currentEnum = e ;
2018-05-07 16:32:51 +02:00
} else if ( typeParts . length > 1 ) {
2018-03-16 18:30:51 +01:00
String [ ] nameParts = enumName . split ( " _ " ) ;
if ( typeParts . length = = nameParts . length ) {
boolean matched = true ;
for ( int i = 0 ; i < nameParts . length ; i + + ) {
if ( ! nameParts [ i ] . startsWith ( typeParts [ i ] ) ) {
matched = false ;
break ;
}
}
if ( matched ) {
currentEnum = e ;
break ;
}
}
}
}
return currentEnum ;
}
}
}
2012-06-08 15:28:36 +02:00
2013-01-24 22:35:28 +01:00
public static class Metadata {
/ * *
2018-03-12 21:01:28 +01:00
* Returns the ItemMeta represented by this code
2013-01-24 22:35:28 +01:00
*
2018-03-12 21:01:28 +01:00
* @param code Code representing the ItemMeta
* @return ItemMeta represented by code
2013-01-24 22:35:28 +01:00
* /
public static ItemMeta getFromCode ( String code ) {
ItemStack item = ChestShop . getItemDatabase ( ) . getFromCode ( code ) ;
if ( item = = null ) {
return null ;
} else {
return item . getItemMeta ( ) ;
}
}
/ * *
* Returns the code for this item
*
* @param item Item being represented
* @return Code representing the item
* /
public static String getItemCode ( ItemStack item ) {
return ChestShop . getItemDatabase ( ) . getItemCode ( item ) ;
}
}
2018-01-06 17:44:35 +01:00
public static class Show {
private static ShowItem showItem = null ;
2018-05-07 16:32:51 +02:00
2018-01-06 17:44:35 +01:00
/ * *
* Lets the class know that it ' s safe to use the ShowItem methods now
*
* @param plugin
* /
public static void initialize ( Plugin plugin ) {
showItem = ( ShowItem ) plugin ;
}
2018-05-07 16:32:51 +02:00
2018-01-06 17:44:35 +01:00
/ * *
* Send a message with hover info and icons
*
* @param player The player to send the message to
* @param message The raw message
* @param stock The items in stock
* /
2020-06-28 19:00:09 +02:00
public static boolean sendMessage ( Player player , Messages . Message message , ItemStack [ ] stock , Map < String , String > replacementMap , String . . . replacements ) {
2020-07-03 19:24:14 +02:00
return sendMessage ( player , player . getName ( ) , message , stock , replacementMap , replacements ) ;
}
/ * *
* Send a message with hover info and icons
*
* @param player The player to send the message to
* @param playerName The name of the player in case he is offline and bungee messages are enabled
* @param message The raw message
* @param stock The items in stock
* /
public static boolean sendMessage ( Player player , String playerName , Messages . Message message , ItemStack [ ] stock , Map < String , String > replacementMap , String . . . replacements ) {
2018-01-06 17:44:35 +01:00
if ( showItem = = null ) {
return false ;
}
2018-05-07 16:32:51 +02:00
2020-12-04 14:53:27 +01:00
TextComponent . Builder itemComponent = Component . text ( ) ;
2018-01-08 22:53:44 +01:00
for ( ItemStack item : InventoryUtil . mergeSimilarStacks ( stock ) ) {
2018-01-06 17:44:35 +01:00
try {
2020-12-04 14:53:27 +01:00
itemComponent . append ( GsonComponentSerializer . gson ( ) . deserialize ( showItem . getItemConverter ( ) . createComponent ( item , Level . FINE ) . toJsonString ( player ) ) ) ;
2018-01-06 17:44:35 +01:00
} catch ( Exception e ) {
ChestShop . getPlugin ( ) . getLogger ( ) . log ( Level . WARNING , " Error while trying to send message ' " + message + " ' to player " + player . getName ( ) + " : " + e . getMessage ( ) ) ;
return false ;
}
}
2018-05-07 16:32:51 +02:00
2020-06-28 19:00:09 +02:00
Map < String , String > newMap = new LinkedHashMap < > ( replacementMap ) ;
newMap . put ( " material " , " item " ) ;
2020-12-01 19:48:26 +01:00
Component component = new Replacer ( )
2020-06-28 19:00:09 +02:00
. placeholderSuffix ( " " )
2020-12-04 14:53:27 +01:00
. replace ( " item " , itemComponent . build ( ) )
2020-12-01 19:48:26 +01:00
. replaceIn ( message . getComponent ( player , true , newMap , replacements ) ) ;
2020-07-03 19:24:14 +02:00
if ( player ! = null ) {
2020-12-01 19:48:26 +01:00
ChestShop . getAudiences ( ) . player ( player ) . sendMessage ( component ) ;
2020-07-03 19:24:14 +02:00
return true ;
} else if ( playerName ! = null ) {
2020-12-01 19:48:26 +01:00
ChestShop . sendBungeeMessage ( playerName , component ) ;
2020-07-03 19:24:14 +02:00
return true ;
2018-10-23 20:02:09 +02:00
}
2020-07-03 19:24:14 +02:00
return true ;
2018-01-06 17:44:35 +01:00
}
}
2012-06-08 15:28:36 +02:00
}