2023-01-09 20:15:56 +01:00
package nl.sbdeveloper.showcontrol.utils ;
2021-01-25 13:30:02 +01:00
import com.cryptomorin.xseries.SkullUtils ;
2023-09-10 16:05:01 +02:00
import com.cryptomorin.xseries.XMaterial ;
import lombok.Getter ;
2021-01-25 13:30:02 +01:00
import org.bukkit.Color ;
import org.bukkit.Material ;
import org.bukkit.enchantments.Enchantment ;
import org.bukkit.inventory.ItemFlag ;
import org.bukkit.inventory.ItemStack ;
import org.bukkit.inventory.meta.ItemMeta ;
2023-09-10 16:05:01 +02:00
import org.bukkit.inventory.meta.LeatherArmorMeta ;
import org.bukkit.potion.PotionData ;
2021-01-25 13:30:02 +01:00
import org.bukkit.potion.PotionType ;
2023-09-10 16:05:01 +02:00
import java.util.ArrayList ;
import java.util.Collections ;
import java.util.List ;
import java.util.Map ;
import java.util.function.UnaryOperator ;
2021-01-25 13:30:02 +01:00
2023-09-10 16:05:01 +02:00
@Getter
2021-01-25 13:30:02 +01:00
public class ItemBuilder {
2023-09-10 16:05:01 +02:00
private final ItemStack itemStack ;
2021-01-25 13:30:02 +01:00
2023-09-10 16:05:01 +02:00
public ItemBuilder ( Material material ) {
this . itemStack = new ItemStack ( material ) ;
2021-01-25 13:30:02 +01:00
}
2023-09-10 16:05:01 +02:00
public ItemBuilder ( ItemStack stack ) {
this . itemStack = stack . clone ( ) ;
2021-01-25 13:30:02 +01:00
}
2023-09-10 16:05:01 +02:00
public ItemBuilder ( XMaterial material ) {
ItemStack item = material . parseItem ( ) ;
if ( item = = null ) {
throw new IllegalArgumentException ( " Received invalid / unsupported XMaterial: " + material . name ( ) ) ;
2021-01-25 13:30:02 +01:00
}
2023-09-10 16:05:01 +02:00
this . itemStack = item ;
2021-01-25 13:30:02 +01:00
}
2023-09-10 16:05:01 +02:00
private void applyToMeta ( UnaryOperator < ItemMeta > callback ) {
this . itemStack . setItemMeta ( callback . apply ( this . itemStack . getItemMeta ( ) ) ) ;
2021-01-25 13:30:02 +01:00
}
2023-09-10 16:05:01 +02:00
public ItemBuilder amount ( int amount ) {
this . itemStack . setAmount ( amount ) ;
2021-01-25 13:30:02 +01:00
return this ;
}
2023-09-10 16:05:01 +02:00
public ItemBuilder displayname ( String name ) {
applyToMeta ( meta - > {
meta . setDisplayName ( name ) ;
return meta ;
} ) ;
2021-01-25 13:30:02 +01:00
return this ;
}
2023-09-10 16:05:01 +02:00
public ItemBuilder lore ( String . . . lore ) {
applyToMeta ( meta - > {
List < String > lores = meta . getLore ( ) ;
if ( lores = = null ) lores = new ArrayList < > ( ) ;
for ( String loreString : lore ) {
String [ ] loreParts = loreString . split ( " [ \\ r \\ n]+ " ) ;
Collections . addAll ( lores , loreParts ) ;
}
meta . setLore ( lores ) ;
return meta ;
} ) ;
2021-01-25 13:30:02 +01:00
return this ;
}
2023-09-10 16:05:01 +02:00
public ItemBuilder lore ( List < String > lore ) {
lore ( lore . toArray ( String [ ] : : new ) ) ;
2021-01-25 13:30:02 +01:00
return this ;
}
2023-09-10 16:05:01 +02:00
public ItemBuilder flag ( ItemFlag . . . flags ) {
applyToMeta ( meta - > {
meta . addItemFlags ( flags ) ;
return meta ;
} ) ;
2021-01-25 13:30:02 +01:00
return this ;
}
2023-09-10 16:05:01 +02:00
public ItemBuilder hideAllFlags ( ) {
return flag ( ItemFlag . values ( ) ) ;
2021-01-25 13:30:02 +01:00
}
2023-09-10 16:05:01 +02:00
public ItemBuilder customModelData ( int customModelData ) {
if ( XMaterial . supports ( 13 ) ) {
applyToMeta ( meta - > {
meta . setCustomModelData ( customModelData ) ;
return meta ;
} ) ;
2021-01-25 13:30:02 +01:00
}
return this ;
}
2023-09-10 16:05:01 +02:00
public ItemBuilder enchant ( Map < Enchantment , Integer > enchantments ) {
itemStack . addEnchantments ( enchantments ) ;
2021-01-25 13:30:02 +01:00
return this ;
}
2023-09-10 16:05:01 +02:00
public ItemBuilder enchant ( Enchantment enchantment , int level ) {
itemStack . addEnchantment ( enchantment , level ) ;
2021-01-25 13:30:02 +01:00
return this ;
}
2023-09-10 16:05:01 +02:00
public ItemBuilder durability ( int damage ) {
if ( XMaterial . supports ( 13 ) ) {
applyToMeta ( meta - > {
if ( ! ( meta instanceof org . bukkit . inventory . meta . Damageable ) ) return meta ;
( ( org . bukkit . inventory . meta . Damageable ) meta ) . setDamage ( damage ) ;
return meta ;
} ) ;
} else {
itemStack . setDurability ( ( short ) damage ) ;
}
2021-01-25 13:30:02 +01:00
return this ;
}
2023-09-10 16:05:01 +02:00
public ItemBuilder unbreakable ( ) {
return unbreakable ( true ) ;
2021-01-25 13:30:02 +01:00
}
2023-09-10 16:05:01 +02:00
public ItemBuilder unbreakable ( boolean unbreakable ) {
applyToMeta ( meta - > {
meta . setUnbreakable ( unbreakable ) ;
return meta ;
} ) ;
2021-01-25 13:30:02 +01:00
return this ;
}
2023-09-10 16:05:01 +02:00
public ItemBuilder glow ( ) {
this . itemStack . addUnsafeEnchantment ( Enchantment . LURE , 0 ) ;
flag ( ItemFlag . HIDE_ENCHANTS ) ;
2021-01-25 13:30:02 +01:00
return this ;
}
2023-09-10 16:05:01 +02:00
public ItemBuilder skullTexture ( String identifier ) {
applyToMeta ( meta - > {
SkullUtils . applySkin ( meta , identifier ) ;
return meta ;
} ) ;
2021-01-25 13:30:02 +01:00
return this ;
}
2023-09-10 16:05:01 +02:00
public ItemBuilder armorColor ( Color color ) {
applyToMeta ( meta - > {
if ( ! ( meta instanceof LeatherArmorMeta ) ) return meta ;
( ( LeatherArmorMeta ) meta ) . setColor ( color ) ;
return meta ;
} ) ;
2021-01-25 13:30:02 +01:00
return this ;
}
2023-09-10 16:05:01 +02:00
public ItemBuilder potionEffect ( PotionType type ) {
if ( ! itemStack . getType ( ) . name ( ) . contains ( " POTION " ) ) throw new UnsupportedOperationException ( " ItemStack is not a potion! (Type: " + itemStack . getType ( ) . name ( ) + " ) " ) ;
applyToMeta ( meta - > {
if ( ! ( meta instanceof org . bukkit . inventory . meta . PotionMeta ) ) return meta ;
( ( org . bukkit . inventory . meta . PotionMeta ) meta ) . setBasePotionData ( new PotionData ( type ) ) ;
return meta ;
} ) ;
2021-01-25 13:30:02 +01:00
return this ;
}
2023-09-10 16:05:01 +02:00
}