001package com.ericlam.mc.eldgui.component.factory;
002
003import com.ericlam.mc.eld.services.ItemStackService;
004import com.ericlam.mc.eldgui.component.AttributeController;
005import com.ericlam.mc.eldgui.component.Component;
006import com.ericlam.mc.eldgui.component.ComponentFactory;
007import org.bukkit.Material;
008import org.bukkit.inventory.ItemStack;
009
010import java.util.function.Consumer;
011
012/**
013 * 用於繼承組件工廠,預設了很多事先的邏輯,方便創建
014 * @param <Factory> 工廠
015 */
016public abstract class AbstractComponentFactory<Factory extends ComponentFactory<Factory>> implements ComponentFactory<Factory> {
017
018    private final ItemStackService itemStackService;
019    protected final AttributeController attributeController;
020    private ItemStackService.ItemFactory itemFactory;
021
022    private final Factory factory;
023
024    @SuppressWarnings("unchecked")
025    public AbstractComponentFactory(ItemStackService itemStackService, AttributeController attributeController) {
026        this.itemStackService = itemStackService;
027        this.attributeController = attributeController;
028        this.itemFactory = itemStackService.build(Material.STONE);
029        this.factory = (Factory) this;
030        this.defaultProperties();
031    }
032
033    protected final Factory editItemByFactory(Consumer<ItemStackService.ItemFactory> factoryConsumer){
034        factoryConsumer.accept(this.itemFactory);
035        return factory;
036    }
037
038    @Override
039    public Factory icon(Material material) {
040        this.itemFactory.material(material);
041        return factory;
042    }
043
044    @Override
045    public Factory bind(String key, Object value) {
046        attributeController.setAttribute(this.itemFactory.getItem(), key, value);
047        return factory;
048    }
049
050    @Override
051    public Factory number(int amount) {
052        this.itemFactory.amount(amount);
053        return factory;
054    }
055
056    @Override
057    public Factory mirror(ItemStack item) {
058        this.itemFactory = itemStackService.edit(item);
059        return factory;
060    }
061
062    @Override
063    public final Component create() {
064        Component component = build(this.itemFactory);
065        this.itemFactory = itemStackService.build(Material.STONE);
066        this.defaultProperties();
067        return component;
068    }
069
070    protected abstract void defaultProperties();
071
072    public abstract Component build(ItemStackService.ItemFactory itemFactory);
073
074}