Search in sources :

Example 6 with Pattern

use of org.bukkit.block.banner.Pattern in project Prism-Bukkit by prism.

the class ItemStackAction method deserialize.

@Override
public void deserialize(String data) {
    if (data == null || !data.startsWith("{")) {
        return;
    }
    actionData = gson().fromJson(data, ItemStackActionData.class);
    item = new ItemStack(getMaterial(), actionData.amt);
    MaterialState.setItemDamage(item, actionData.durability);
    // Restore enchantment
    if (actionData.enchs != null && actionData.enchs.length > 0) {
        for (final String ench : actionData.enchs) {
            final String[] enchArgs = ench.split(":");
            Enchantment enchantment = Enchantment.getByKey(NamespacedKey.minecraft(enchArgs[0]));
            // Restore book enchantment
            if (enchantment != null) {
                if (item.getType() == Material.ENCHANTED_BOOK) {
                    final EnchantmentStorageMeta bookEnchantments = (EnchantmentStorageMeta) item.getItemMeta();
                    bookEnchantments.addStoredEnchant(enchantment, Integer.parseInt(enchArgs[1]), false);
                    item.setItemMeta(bookEnchantments);
                } else {
                    item.addUnsafeEnchantment(enchantment, Integer.parseInt(enchArgs[1]));
                }
            }
        }
    }
    ItemMeta meta = item.getItemMeta();
    // Leather color
    if (meta instanceof LeatherArmorMeta && actionData.color > 0) {
        final LeatherArmorMeta lam = (LeatherArmorMeta) meta;
        lam.setColor(Color.fromRGB(actionData.color));
        item.setItemMeta(lam);
    } else if (meta instanceof SkullMeta && actionData.owner != null) {
        final SkullMeta skull = (SkullMeta) meta;
        skull.setOwningPlayer(Bukkit.getOfflinePlayer(EntityUtils.uuidOf(actionData.owner)));
        item.setItemMeta(skull);
    } else if (meta instanceof BookMeta) {
        final BookMeta bookMeta = (BookMeta) meta;
        bookMeta.setAuthor(actionData.by);
        bookMeta.setTitle(actionData.title);
        bookMeta.setPages(actionData.content);
        item.setItemMeta(bookMeta);
    } else if (meta instanceof PotionMeta) {
        final PotionType potionType = PotionType.valueOf(actionData.potionType.toUpperCase());
        final PotionMeta potionMeta = (PotionMeta) meta;
        potionMeta.setBasePotionData(new PotionData(potionType, actionData.potionExtended, actionData.potionUpgraded));
    }
    if (meta instanceof FireworkEffectMeta && actionData.effectColors != null && actionData.effectColors.length > 0) {
        item = deserializeFireWorksMeta(item, meta, actionData);
    }
    if (meta instanceof BannerMeta && actionData.bannerMeta != null) {
        Map<String, String> stringStringMap = actionData.bannerMeta;
        List<Pattern> patterns = new ArrayList<>();
        stringStringMap.forEach((patternIdentifier, dyeName) -> {
            PatternType type = PatternType.getByIdentifier(patternIdentifier);
            DyeColor color = DyeColor.valueOf(dyeName);
            if (type != null && color != null) {
                Pattern p = new Pattern(color, type);
                patterns.add(p);
            }
        });
        ((BannerMeta) meta).setPatterns(patterns);
    }
    if (actionData.name != null) {
        if (meta == null) {
            meta = item.getItemMeta();
        }
        if (meta != null) {
            meta.setDisplayName(actionData.name);
        }
    }
    if (actionData.lore != null) {
        if (meta == null) {
            meta = item.getItemMeta();
        }
        if (meta != null) {
            meta.setLore(Arrays.asList(actionData.lore));
        }
    }
    if (meta != null) {
        item.setItemMeta(meta);
    }
}
Also used : PatternType(org.bukkit.block.banner.PatternType) BannerMeta(org.bukkit.inventory.meta.BannerMeta) Pattern(org.bukkit.block.banner.Pattern) ArrayList(java.util.ArrayList) SkullMeta(org.bukkit.inventory.meta.SkullMeta) PotionMeta(org.bukkit.inventory.meta.PotionMeta) DyeColor(org.bukkit.DyeColor) EnchantmentStorageMeta(org.bukkit.inventory.meta.EnchantmentStorageMeta) PotionData(org.bukkit.potion.PotionData) LeatherArmorMeta(org.bukkit.inventory.meta.LeatherArmorMeta) FireworkEffectMeta(org.bukkit.inventory.meta.FireworkEffectMeta) PotionType(org.bukkit.potion.PotionType) ItemStack(org.bukkit.inventory.ItemStack) Enchantment(org.bukkit.enchantments.Enchantment) BookMeta(org.bukkit.inventory.meta.BookMeta) ItemMeta(org.bukkit.inventory.meta.ItemMeta)

Example 7 with Pattern

use of org.bukkit.block.banner.Pattern in project Denizen-For-Bukkit by DenizenScript.

the class ItemPatterns method adjust.

@Override
public void adjust(Mechanism mechanism) {
    // -->
    if (mechanism.matches("patterns")) {
        List<Pattern> patterns = new ArrayList<>();
        ListTag list = mechanism.valueAsType(ListTag.class);
        List<String> split;
        for (String string : list) {
            try {
                split = CoreUtilities.split(string, '/', 2);
                patterns.add(new Pattern(DyeColor.valueOf(split.get(0).toUpperCase()), PatternType.valueOf(split.get(1).toUpperCase())));
            } catch (Exception e) {
                Debug.echoError("Could not apply pattern to banner: " + string);
            }
        }
        setPatterns(patterns);
    }
}
Also used : Pattern(org.bukkit.block.banner.Pattern) ArrayList(java.util.ArrayList) ListTag(com.denizenscript.denizencore.objects.core.ListTag)

Example 8 with Pattern

use of org.bukkit.block.banner.Pattern in project Glowstone by GlowstoneMC.

the class BlockBanner method fromNbt.

/**
 * Converts NBT tags to banner patterns.
 *
 * @param tag a list of banner patterns as NBT tags
 * @return the patterns as Pattern instances
 */
public static List<Pattern> fromNbt(List<CompoundTag> tag) {
    List<Pattern> banner = new ArrayList<>();
    for (CompoundTag layer : tag) {
        PatternType patternType = PatternType.getByIdentifier(layer.getString("Pattern"));
        DyeColor color = DyeColor.getByDyeData((byte) layer.getInt("Color"));
        banner.add(new Pattern(color, patternType));
    }
    return banner;
}
Also used : PatternType(org.bukkit.block.banner.PatternType) Pattern(org.bukkit.block.banner.Pattern) ArrayList(java.util.ArrayList) DyeColor(org.bukkit.DyeColor) CompoundTag(net.glowstone.util.nbt.CompoundTag)

Example 9 with Pattern

use of org.bukkit.block.banner.Pattern in project Glowstone by GlowstoneMC.

the class BlockCauldron method bleachBanner.

private boolean bleachBanner(GlowPlayer player, GlowBlock block) {
    // fired when a player bleaches a banner using the cauldron
    if (player.getGameMode() == GameMode.CREATIVE) {
        return false;
    }
    if (block.getData() > LEVEL_EMPTY) {
        ItemStack inHand = player.getItemInHand();
        BannerMeta meta = (BannerMeta) inHand.getItemMeta();
        List<Pattern> layers = meta.getPatterns();
        if (layers.isEmpty()) {
            return false;
        }
        if (!setCauldronLevel(block, block.getData() - 1, player, CauldronLevelChangeEvent.ChangeReason.BANNER_WASH)) {
            return false;
        }
        meta.setPatterns(layers);
        inHand.setItemMeta(meta);
        return true;
    } else {
        return false;
    }
}
Also used : BannerMeta(org.bukkit.inventory.meta.BannerMeta) Pattern(org.bukkit.block.banner.Pattern) ItemStack(org.bukkit.inventory.ItemStack)

Example 10 with Pattern

use of org.bukkit.block.banner.Pattern in project Glowstone by GlowstoneMC.

the class GlowMetaShield method serialize.

@Override
@NotNull
public Map<String, Object> serialize() {
    Map<String, Object> result = super.serialize();
    result.put("meta-type", "SHIELD");
    List<Map<String, String>> patternsList = new ArrayList<>();
    for (Pattern pattern : patterns) {
        patternsList.add(ImmutableMap.of(pattern.getPattern().toString(), pattern.getColor().toString()));
    }
    result.put("pattern", patternsList);
    if (baseColor != null) {
        result.put("baseColor", baseColor);
    }
    return result;
}
Also used : Pattern(org.bukkit.block.banner.Pattern) ArrayList(java.util.ArrayList) ImmutableMap(com.google.common.collect.ImmutableMap) Map(java.util.Map) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

Pattern (org.bukkit.block.banner.Pattern)31 ArrayList (java.util.ArrayList)17 DyeColor (org.bukkit.DyeColor)9 HashMap (java.util.HashMap)6 BannerMeta (org.bukkit.inventory.meta.BannerMeta)6 PatternType (org.bukkit.block.banner.PatternType)5 CompoundTag (net.aufdemrand.denizen.nms.util.jnbt.CompoundTag)4 ListTag (net.aufdemrand.denizen.nms.util.jnbt.ListTag)4 CompoundTag (net.glowstone.util.nbt.CompoundTag)4 BookMeta (org.bukkit.inventory.meta.BookMeta)4 EnchantmentStorageMeta (org.bukkit.inventory.meta.EnchantmentStorageMeta)4 LeatherArmorMeta (org.bukkit.inventory.meta.LeatherArmorMeta)4 PotionMeta (org.bukkit.inventory.meta.PotionMeta)4 SkullMeta (org.bukkit.inventory.meta.SkullMeta)4 CompoundTag (com.denizenscript.denizen.nms.util.jnbt.CompoundTag)3 JNBTListTag (com.denizenscript.denizen.nms.util.jnbt.JNBTListTag)3 MCPattern (com.laytonsmith.abstraction.MCPattern)3 Map (java.util.Map)3 Banner (org.bukkit.block.Banner)3 Enchantment (org.bukkit.enchantments.Enchantment)3