Mineplex2018-withcommit/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/items/RareItemFactory.java

141 lines
2.9 KiB
Java
Raw Normal View History

package mineplex.game.clans.items;
import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;
import mineplex.core.common.util.UtilInv;
import mineplex.game.clans.items.attributes.ItemAttribute;
import mineplex.game.clans.items.legendaries.LegendaryItem;
/**
* Quick little guide on how to use this thing:
* <p>
* The method fabricate() returns an ItemStack.
* <p>
*
* For creating a legendary item, do:
* <p>
* RareItemFactory.begin(ItemType.LEGENDARY).setLegendary(AlligatorsTooth.class)
* .fabricate();
* <p>
*
* For creating a Flaming Jagged Diamond Sword of Conquering, simply do:
* <p>
* RareItemFactory.begin(ItemType.WEAPON).setType(Material.DIAMOND_SWORD).
* setSuperPrefix(FlamingAttribute.class).setPrefix(JaggedAttribute.class).
* setSuffix(ConqueringAttribute.class).fabricate();
*/
public class RareItemFactory
{
private Class<? extends ItemAttribute> _superPrefix;
private Class<? extends ItemAttribute> _prefix;
private Class<? extends ItemAttribute> _suffix;
private ItemType _itemType;
private Material _material;
private CustomItem _item;
public RareItemFactory(ItemType itemType)
{
_itemType = itemType;
}
public static RareItemFactory begin(ItemType itemType)
{
return new RareItemFactory(itemType);
}
public RareItemFactory setType(Material type)
{
_item = new CustomItem(type);
_item.addDullEnchantment();
_material = type;
return this;
}
public RareItemFactory setLegendary(Class<? extends LegendaryItem> legendary)
{
if (_itemType.equals(ItemType.LEGENDARY))
{
try
{
_item = legendary.newInstance();
_item.addDullEnchantment();
_material = _item.getMaterial();
}
catch (InstantiationException | IllegalAccessException e)
{
e.printStackTrace();
}
}
else
{
throw new RuntimeException("Unexpected call to setLegendary(LegendaryType)");
}
return this;
}
public RareItemFactory setSuperPrefix(Class<? extends ItemAttribute> superPrefix)
{
_superPrefix = superPrefix;
return this;
}
public RareItemFactory setPrefix(Class<? extends ItemAttribute> prefix)
{
_prefix = prefix;
return this;
}
public RareItemFactory setSuffix(Class<? extends ItemAttribute> suffix)
{
_suffix = suffix;
return this;
}
public ItemStack fabricate()
{
try
{
if (_superPrefix != null)
{
_item.getAttributes().addAttribute(_superPrefix.newInstance());
}
if (_prefix != null)
{
_item.getAttributes().addAttribute(_prefix.newInstance());
}
if (_suffix != null)
{
_item.getAttributes().addAttribute(_suffix.newInstance());
}
}
catch (InstantiationException | IllegalAccessException e)
{
e.printStackTrace();
}
ItemStack item = _item.toItemStack();
return item;
}
public ItemType getItemType()
{
return _itemType;
}
public Material getMaterial()
{
return _material;
}
public CustomItem getWrapper()
{
return _item;
}
}