Search in sources :

Example 1 with AttributeType

use of me.deecaad.core.utils.AttributeType in project MechanicsMain by WeaponMechanics.

the class ItemSerializer method serializeWithoutRecipe.

public ItemStack serializeWithoutRecipe(SerializeData data) throws SerializerException {
    // TODO Add byte data support using 'Data:' or 'Extra_Data:' key
    Material type = data.of("Type").assertExists().getEnum(Material.class);
    ItemStack itemStack = new ItemStack(type);
    ItemMeta itemMeta = itemStack.getItemMeta();
    if (itemMeta == null) {
        throw data.exception("Type", "Did you use air as a material? This is not allowed!", SerializerException.forValue(type));
    }
    String name = data.of("Name").assertType(String.class).get(null);
    if (name != null)
        itemMeta.setDisplayName(StringUtil.color(name));
    List<?> lore = data.of("Lore").assertType(List.class).get(null);
    if (lore != null && !lore.isEmpty()) {
        itemMeta.setLore(convertListObject(lore));
    }
    short durability = (short) data.of("Durability").assertPositive().getInt(-99);
    if (durability != -99) {
        if (CompatibilityAPI.getVersion() >= 1.132) {
            ((org.bukkit.inventory.meta.Damageable) itemMeta).setDamage(durability);
        } else {
            itemStack.setDurability(durability);
        }
    }
    boolean unbreakable = data.of("Unbreakable").getBool(false);
    if (CompatibilityAPI.getVersion() >= 1.11) {
        itemMeta.setUnbreakable(unbreakable);
    } else {
        setupUnbreakable();
        ReflectionUtil.invokeMethod(setUnbreakable, ReflectionUtil.invokeMethod(spigotMethod, itemMeta), true);
    }
    int customModelData = data.of("Custom_Model_Data").assertPositive().getInt(-99);
    if (customModelData != -99 && CompatibilityAPI.getVersion() >= 1.14) {
        itemMeta.setCustomModelData(customModelData);
    }
    boolean hideFlags = data.of("Hide_Flags").getBool(false);
    if (hideFlags) {
        itemMeta.addItemFlags(ItemFlag.values());
    }
    List<String[]> enchantments = data.ofList("Enchantments").addArgument(Enchantment.class, true, true).addArgument(int.class, true).get();
    if (enchantments != null) {
        for (String[] split : enchantments) {
            Enchantment enchant;
            if (CompatibilityAPI.getVersion() < 1.13) {
                enchant = Enchantment.getByName(split[0]);
            } else {
                enchant = Enchantment.getByKey(org.bukkit.NamespacedKey.minecraft(split[0]));
            }
            if (enchant == null) {
                throw new SerializerOptionsException("Item", "Enchantment", Arrays.stream(Enchantment.values()).map(Enchantment::getName).collect(Collectors.toList()), split[0], data.of("Enchantments").getLocation());
            }
            int enchantmentLevel = Integer.parseInt(split[1]);
            itemMeta.addEnchant(enchant, enchantmentLevel - 1, true);
        }
    }
    itemStack.setItemMeta(itemMeta);
    List<String[]> attributes = data.ofList("Attributes").addArgument(AttributeType.class, true).addArgument(double.class, true).addArgument(NBTCompatibility.AttributeSlot.class, false).get();
    if (attributes != null) {
        for (String[] split : attributes) {
            List<AttributeType> attributeTypes = EnumUtil.parseEnums(AttributeType.class, split[0]);
            List<NBTCompatibility.AttributeSlot> attributeSlots = split.length > 2 ? EnumUtil.parseEnums(NBTCompatibility.AttributeSlot.class, split[2]) : null;
            double amount = Double.parseDouble(split[1]);
            for (AttributeType attribute : attributeTypes) {
                if (attributeSlots == null) {
                    CompatibilityAPI.getNBTCompatibility().setAttribute(itemStack, attribute, null, amount);
                    continue;
                }
                for (NBTCompatibility.AttributeSlot slot : attributeSlots) {
                    CompatibilityAPI.getNBTCompatibility().setAttribute(itemStack, attribute, slot, amount);
                }
            }
        }
    }
    String owningPlayer = data.of("Skull_Owning_Player").assertType(String.class).get(null);
    if (owningPlayer != null) {
        try {
            SkullMeta skullMeta = (SkullMeta) itemStack.getItemMeta();
            UUID uuid;
            try {
                uuid = UUID.fromString(owningPlayer);
            } catch (IllegalArgumentException e) {
                uuid = null;
            }
            if (uuid != null) {
                if (CompatibilityAPI.getVersion() >= 1.12) {
                    skullMeta.setOwningPlayer(Bukkit.getServer().getOfflinePlayer(uuid));
                } else {
                    skullMeta.setOwner(Bukkit.getServer().getOfflinePlayer(uuid).getName());
                }
            } else {
                skullMeta.setOwner(owningPlayer);
            }
            itemStack.setItemMeta(skullMeta);
        } catch (ClassCastException e) {
            throw data.exception("Skull_Owning_Player", "Tried to use Skulls when the item wasn't a player head!", SerializerException.forValue(type));
        }
    }
    if (CompatibilityAPI.getVersion() >= 1.11 && data.config.contains(data.key + ".Potion_Color")) {
        try {
            Color color = data.of("Potion_Color").serializeNonStandardSerializer(new ColorSerializer());
            PotionMeta potionMeta = (PotionMeta) itemStack.getItemMeta();
            potionMeta.setColor(color);
            itemStack.setItemMeta(potionMeta);
        } catch (ClassCastException e) {
            throw data.exception("Potion_Color", "Tried to use Potion Color when the item wasn't a potion!", SerializerException.forValue(type));
        }
    }
    if (data.config.contains(data.key + ".Leather_Color")) {
        try {
            Color color = data.of("Leather_Color").serializeNonStandardSerializer(new ColorSerializer());
            LeatherArmorMeta meta = (LeatherArmorMeta) itemStack.getItemMeta();
            meta.setColor(color);
            itemStack.setItemMeta(meta);
        } catch (ClassCastException e) {
            throw data.exception("Leather_Color", "Tried to use Leather Color when the item wasn't leather armor!", SerializerException.forValue(type));
        }
    }
    if (data.config.contains(data.key + ".Firework")) {
        // <FireworkEffect.Type>-<Color>-<Boolean=Trail>-<Boolean=Flicker>-<Color=Fade>
        List<String[]> list = data.ofList("Firework.Effects").addArgument(FireworkEffect.Type.class, true).addArgument(ColorSerializer.class, true, true).addArgument(boolean.class, false).addArgument(boolean.class, false).addArgument(ColorSerializer.class, false).assertExists().assertList().get();
        FireworkMeta meta = (FireworkMeta) itemMeta;
        meta.setPower(data.of("Firework.Power").assertPositive().getInt(1));
        for (String[] split : list) {
            FireworkEffect.Builder builder = FireworkEffect.builder();
            builder.with(FireworkEffect.Type.valueOf(split[0]));
            // Handle initial colors
            String[] colors = split[1].split(", ?");
            for (String color : colors) builder.withColor(new ColorSerializer().fromString(data.move("Firework.Effects"), color));
            builder.trail(split.length > 2 && split[2].equalsIgnoreCase("true"));
            builder.flicker(split.length > 3 && split[3].equalsIgnoreCase("true"));
            // Handle the fade colors
            String[] fadeColors = split.length > 4 ? split[4].split(", ?") : new String[0];
            for (String color : fadeColors) builder.withFade(new ColorSerializer().fromString(data.move("Firework.Effects"), color));
            // Add the newly constructed firework effect to the list.
            meta.addEffect(builder.build());
        }
        itemStack.setItemMeta(meta);
    }
    if (data.of("Deny_Use_In_Crafting").getBool(false)) {
        CompatibilityAPI.getNBTCompatibility().setInt(itemStack, "MechanicsCore", "deny-crafting", 1);
    }
    return itemStack;
}
Also used : NBTCompatibility(me.deecaad.core.compatibility.nbt.NBTCompatibility) SkullMeta(org.bukkit.inventory.meta.SkullMeta) FireworkEffect(org.bukkit.FireworkEffect) AttributeType(me.deecaad.core.utils.AttributeType) ArrayList(java.util.ArrayList) List(java.util.List) UUID(java.util.UUID) ItemMeta(org.bukkit.inventory.meta.ItemMeta) Color(org.bukkit.Color) FireworkMeta(org.bukkit.inventory.meta.FireworkMeta) Material(org.bukkit.Material) PotionMeta(org.bukkit.inventory.meta.PotionMeta) SerializerOptionsException(me.deecaad.core.file.SerializerOptionsException) AttributeType(me.deecaad.core.utils.AttributeType) LeatherArmorMeta(org.bukkit.inventory.meta.LeatherArmorMeta) ItemStack(org.bukkit.inventory.ItemStack) Enchantment(org.bukkit.enchantments.Enchantment)

Aggregations

ArrayList (java.util.ArrayList)1 List (java.util.List)1 UUID (java.util.UUID)1 NBTCompatibility (me.deecaad.core.compatibility.nbt.NBTCompatibility)1 SerializerOptionsException (me.deecaad.core.file.SerializerOptionsException)1 AttributeType (me.deecaad.core.utils.AttributeType)1 Color (org.bukkit.Color)1 FireworkEffect (org.bukkit.FireworkEffect)1 Material (org.bukkit.Material)1 Enchantment (org.bukkit.enchantments.Enchantment)1 ItemStack (org.bukkit.inventory.ItemStack)1 FireworkMeta (org.bukkit.inventory.meta.FireworkMeta)1 ItemMeta (org.bukkit.inventory.meta.ItemMeta)1 LeatherArmorMeta (org.bukkit.inventory.meta.LeatherArmorMeta)1 PotionMeta (org.bukkit.inventory.meta.PotionMeta)1 SkullMeta (org.bukkit.inventory.meta.SkullMeta)1