package net.glowstone.block.blocktype;

import net.glowstone.block.GlowBlock;
import net.glowstone.entity.GlowPlayer;
import net.glowstone.inventory.GlowItemFactory;
import net.glowstone.inventory.MaterialMatcher;
import net.glowstone.inventory.ToolType;
import org.bukkit.BannerPattern;
import org.bukkit.GameMode;
import org.bukkit.Material;
import org.bukkit.block.BlockFace;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.BannerMeta;
import org.bukkit.inventory.meta.LeatherArmorMeta;
import org.bukkit.util.Vector;

import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Map;

public class BlockCauldron extends BlockNeedsTool {
    private static final Collection<ItemStack> DROP = Arrays.asList(new ItemStack(Material.CAULDRON_ITEM));

    @Override
    public boolean blockInteract(GlowPlayer player, GlowBlock block, BlockFace face, Vector clickedLoc) {
        if (player.getItemInHand() == null) {
            return super.blockInteract(player, block, face, clickedLoc);
        }

        switch (player.getItemInHand().getType()) {
            case WATER_BUCKET:
                fillCauldron(player, block);
                return true;

            case GLASS_BOTTLE:
                fillBottle(player, block);
                return true;

            case LEATHER_BOOTS:
            case LEATHER_LEGGINGS:
            case LEATHER_CHESTPLATE:
            case LEATHER_HELMET:
                return bleachLeatherArmor(player, block);

            case BANNER:
                return bleachBanner(player, block);

            default:
                return super.blockInteract(player, block, face, clickedLoc);
        }
    }

    private void fillCauldron(GlowPlayer player, GlowBlock block) {
        if (block.getData() < 3) {
            if (player.getGameMode() != GameMode.CREATIVE) {
                player.getItemInHand().setType(Material.BUCKET);
            }

            block.setData((byte) 3);
        }
    }

    private void fillBottle(GlowPlayer player, GlowBlock block) {
        if (block.getData() > 0) {
            block.setData((byte) (block.getData() - 1));

            if (player.getGameMode() != GameMode.CREATIVE) {
                Map<Integer, ItemStack> drops = player.getInventory().addItem(new ItemStack(Material.POTION));
                if (!drops.isEmpty()) {
                    player.getWorld().dropItemNaturally(player.getLocation(), new ItemStack(Material.POTION));
                }

                player.getItemInHand().setAmount(player.getItemInHand().getAmount() - 1);
            }
        }
    }

    private boolean bleachBanner(GlowPlayer player, GlowBlock block) {
        if (player.getGameMode() == GameMode.CREATIVE)
            return false;

        if (block.getData() > 0) {
            ItemStack inHand = player.getItemInHand();
            BannerMeta meta = (BannerMeta) inHand.getItemMeta();
            BannerPattern pattern = meta.getPattern();
            List<BannerPattern.BannerLayer> layers = pattern.getLayers();
            if (layers == null || layers.isEmpty()) {
                return false;
            }

            BannerPattern.Builder builder = BannerPattern.builder();
            for (int i = 0; i < layers.size() - 1; i++) {
                BannerPattern.BannerLayer layer = layers.get(i);
                builder.layer(layer.getTexture(), layer.getColor());
            }

            meta.setPattern(builder.build());
            inHand.setItemMeta(meta);

            block.setData((byte) (block.getData() - 1));
            return true;
        } else {
            return false;
        }
    }

    private boolean bleachLeatherArmor(GlowPlayer player, GlowBlock block) {
        if (block.getData() > 0) {
            ItemStack inHand = player.getItemInHand();
            LeatherArmorMeta im = (LeatherArmorMeta) inHand.getItemMeta();
            im.setColor(GlowItemFactory.instance().getDefaultLeatherColor());
            inHand.setItemMeta(im);
            block.setData((byte) (block.getData() - 1));
            return true;
        } else {
            return false;
        }
    }

    @Override
    public Collection<ItemStack> getMinedDrops(GlowBlock block) {
        return DROP;
    }

    @Override
    protected MaterialMatcher getNeededMiningTool(GlowBlock block) {
        return ToolType.PICKAXE;
    }
}

@@ -252,41 +252,8 @@
 pe;%0A
-import org.bukkit.BannerPattern;%0A
 impo
@@ -339,16 +339,56 @@
 ckFace;%0A
+import org.bukkit.block.banner.Pattern;%0A
 import o
@@ -2952,120 +2952,46 @@
     
-BannerPattern pattern = meta.getPattern();%0A            List%3CBannerPattern.BannerLayer%3E layers = pattern.getLay
+List%3CPattern%3E layers = meta.getPatt
 er
+n
 s();
@@ -3106,315 +3106,31 @@
     
-BannerPattern.Builder builder = BannerPattern.builder();%0A            for (int i = 0; i %3C layers.size() - 1; i++) %7B%0A                BannerPattern.BannerLayer layer = layers.get(i);%0A                builder.layer(layer.getTexture(), layer.getColor());%0A            %7D%0A%0A            meta.setPattern(builder.build()
+meta.setPatterns(layers
 );%0A 

Update to use Banner.Pattern instead of BannerPattern API