Created
November 27, 2014 13:06
-
-
Save Muwbi/795de1044a0e0b889d20 to your computer and use it in GitHub Desktop.
ItemBuilder mit Lombok <3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package <your-package>; | |
import lombok.RequiredArgsConstructor; | |
import lombok.Setter; | |
import lombok.experimental.Accessors; | |
import org.bukkit.Material; | |
import org.bukkit.enchantments.Enchantment; | |
import org.bukkit.entity.Player; | |
import org.bukkit.inventory.ItemStack; | |
import org.bukkit.inventory.meta.ItemMeta; | |
import org.bukkit.material.MaterialData; | |
import java.util.*; | |
/** | |
* @author Muwbi | |
*/ | |
@Accessors(chain = true) | |
@Setter | |
@RequiredArgsConstructor | |
public class ItemBuilder { | |
private final Material material; | |
private int amount = 1; | |
private List<String> lore; | |
private String displayName; | |
private MaterialData materialData; | |
private short damage = 0; | |
private ItemMeta itemMeta; | |
private Map<Enchantment, Integer> enchantments; | |
public ItemBuilder setLore( String... lore ) { | |
this.lore = Arrays.asList( lore ); | |
return this; | |
} | |
public ItemBuilder addEnchantment( Enchantment enchantment, int level ) { | |
if ( enchantments == null ) { | |
enchantments = new HashMap<>(); | |
} | |
enchantments.put( enchantment, level ); | |
return this; | |
} | |
public ItemStack build() { | |
ItemStack itemStack = new ItemStack( material, amount, damage ); | |
itemMeta = itemMeta != null ? itemMeta : itemStack.getItemMeta(); | |
if ( materialData != null ) { | |
itemStack.setData( materialData ); | |
} | |
if ( displayName != null ) { | |
itemMeta.setDisplayName( displayName ); | |
} | |
if ( lore != null ) { | |
itemMeta.setLore( lore ); | |
} | |
if ( enchantments != null ) { | |
itemStack.addUnsafeEnchantments( enchantments ); | |
} | |
itemStack.setItemMeta( itemMeta ); | |
return itemStack; | |
} | |
public void give( Player player ) { | |
player.getInventory().addItem( build() ); | |
} | |
public void give( Player... players ) { | |
give( new HashSet<>( Arrays.asList( players ) ) ); | |
} | |
public void give( Set<Player> players ) { | |
final ItemStack itemStack = build(); | |
players.forEach( player -> player.getInventory().addItem( itemStack ) ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Irgendwie kommt mir das bekannt vor...