Search in sources :

Example 1 with SpawnEggs

use of me.realized.tokenmanager.util.compat.SpawnEggs in project TokenManager by RealizedMC.

the class ItemUtil method loadFromString.

public static ItemStack loadFromString(final String line) {
    if (line == null || line.isEmpty()) {
        throw new IllegalArgumentException("Line is empty or null!");
    }
    final String[] args = line.split(" +");
    final String[] materialData = args[0].split(":");
    final Material material = Material.matchMaterial(materialData[0]);
    if (material == null) {
        throw new IllegalArgumentException("'" + args[0] + "' is not a valid material.");
    }
    ItemStack result = new ItemStack(material, 1);
    if (materialData.length > 1) {
        // Handle potions and spawn eggs switching to NBT in 1.9+
        if (!ItemUtil.isPre1_9()) {
            if (material.name().contains("POTION")) {
                final String[] values = materialData[1].split("-");
                final PotionType type;
                if ((type = EnumUtil.getByName(values[0], PotionType.class)) == null) {
                    throw new IllegalArgumentException("'" + values[0] + "' is not a valid PotionType. Available: " + EnumUtil.getNames(PotionType.class));
                }
                result = new Potions(type, Arrays.asList(values)).toItemStack();
            } else if (material == Material.MONSTER_EGG) {
                final EntityType type;
                if ((type = EnumUtil.getByName(materialData[1], EntityType.class)) == null) {
                    throw new IllegalArgumentException("'" + materialData[0] + "' is not a valid EntityType. Available: " + EnumUtil.getNames(EntityType.class));
                }
                result = new SpawnEggs(type).toItemStack();
            }
        }
        final OptionalLong value;
        if ((value = NumberUtil.parseLong(materialData[1])).isPresent()) {
            result.setDurability((short) value.getAsLong());
        }
    }
    if (args.length < 2) {
        return result;
    }
    result.setAmount(Integer.parseInt(args[1]));
    if (args.length > 2) {
        for (int i = 2; i < args.length; i++) {
            final String argument = args[i];
            final String[] pair = argument.split(":", 2);
            if (pair.length < 2) {
                continue;
            }
            applyMeta(result, pair[0], pair[1]);
        }
    }
    return result;
}
Also used : Potions(me.realized.tokenmanager.util.compat.Potions) EntityType(org.bukkit.entity.EntityType) SpawnEggs(me.realized.tokenmanager.util.compat.SpawnEggs) OptionalLong(java.util.OptionalLong) Material(org.bukkit.Material) PotionType(org.bukkit.potion.PotionType) ItemStack(org.bukkit.inventory.ItemStack)

Aggregations

OptionalLong (java.util.OptionalLong)1 Potions (me.realized.tokenmanager.util.compat.Potions)1 SpawnEggs (me.realized.tokenmanager.util.compat.SpawnEggs)1 Material (org.bukkit.Material)1 EntityType (org.bukkit.entity.EntityType)1 ItemStack (org.bukkit.inventory.ItemStack)1 PotionType (org.bukkit.potion.PotionType)1