Make ItemBuilder properly clone all options and implement a builder to create enchanted books

This commit is contained in:
AlexTheCoder 2017-05-04 18:28:49 -04:00
parent 53eb499236
commit f9f2e362c4
2 changed files with 76 additions and 2 deletions

View File

@ -0,0 +1,63 @@
package mineplex.core.itemstack;
import java.util.HashMap;
import java.util.Map;
import org.bukkit.Material;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.EnchantmentStorageMeta;;
public class EnchantedBookBuilder
{
private Map<Enchantment, Integer> _enchantments;
private int _amount;
public EnchantedBookBuilder(int amount)
{
_enchantments = new HashMap<>();
_amount = amount;
}
public Map<Enchantment, Integer> getEnchantments()
{
return _enchantments;
}
public Integer getLevel(Enchantment enchantment)
{
return _enchantments.getOrDefault(enchantment, 0);
}
public EnchantedBookBuilder setLevel(Enchantment enchantment, Integer level)
{
if (level <= 0)
{
_enchantments.remove(enchantment);
}
else
{
_enchantments.put(enchantment, level);
}
return this;
}
public EnchantedBookBuilder removeEnchantment(Enchantment enchantment)
{
_enchantments.remove(enchantment);
return this;
}
public ItemStack build()
{
ItemStack item = new ItemStack(Material.ENCHANTED_BOOK, _amount);
EnchantmentStorageMeta meta = (EnchantmentStorageMeta) item.getItemMeta();
_enchantments.entrySet().forEach(entry -> meta.addStoredEnchant(entry.getKey(), entry.getValue(), true));
item.setItemMeta(meta);
return item;
}
}

View File

@ -8,8 +8,6 @@ import java.util.HashSet;
import java.util.List;
import java.util.Map;
import mineplex.core.common.util.UtilInput;
import mineplex.core.common.util.UtilInv;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Color;
@ -26,6 +24,7 @@ import org.bukkit.inventory.meta.LeatherArmorMeta;
import org.bukkit.inventory.meta.SkullMeta;
import mineplex.core.common.util.C;
import mineplex.core.common.util.UtilInv;
public class ItemBuilder
{
@ -311,6 +310,18 @@ public class ItemBuilder
// newBuilder.potion = potion;
newBuilder.setDurability(_durability);
newBuilder.setData(_data);
newBuilder.setAmount(_amount);
newBuilder.setUnbreakable(_unbreakable);
newBuilder.setGlow(_glow);
newBuilder.setItemFlags(_itemFlags);
newBuilder.setPlayerHead(_playerHeadName);
return newBuilder;
}