Search in sources :

Example 1 with BukkitObjectInputStream

use of org.bukkit.util.io.BukkitObjectInputStream in project Bukkit_Bungee_PluginLib by GeorgH93.

the class BukkitItemStackSerializerTest method testDeserialize.

@Test
public void testDeserialize() throws Exception {
    BukkitItemStackSerializer deserializer = new BukkitItemStackSerializer();
    assertNull("Deserialized data should be null", deserializer.deserialize(null));
    assertNull("Deserialized data should be null when an error occurs", deserializer.deserialize(new byte[] { 2, 5, 6 }));
    BukkitObjectInputStream mockedInputStream = mock(BukkitObjectInputStream.class);
    doReturn(new ItemStack[] {}).when(mockedInputStream).readObject();
    whenNew(BukkitObjectInputStream.class).withAnyArguments().thenReturn(mockedInputStream);
    assertNotNull("Deserialized data should not be null", deserializer.deserialize(new byte[] { 22, 25, 65 }));
}
Also used : BukkitObjectInputStream(org.bukkit.util.io.BukkitObjectInputStream) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 2 with BukkitObjectInputStream

use of org.bukkit.util.io.BukkitObjectInputStream in project VehiclesPlus2.0 by legofreak107.

the class Main method fromBase64.

public static Inventory fromBase64(String data) {
    try {
        ByteArrayInputStream inputStream = new ByteArrayInputStream(Base64Coder.decodeLines(data));
        BukkitObjectInputStream dataInput = new BukkitObjectInputStream(inputStream);
        Inventory inventory = Bukkit.getServer().createInventory(null, dataInput.readInt());
        // Read the serialized inventory
        for (int i = 0; i < inventory.getSize(); i++) {
            inventory.setItem(i, (ItemStack) dataInput.readObject());
        }
        dataInput.close();
        return inventory;
    } catch (Exception e) {
    }
    return null;
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) BukkitObjectInputStream(org.bukkit.util.io.BukkitObjectInputStream) Inventory(org.bukkit.inventory.Inventory) IOException(java.io.IOException)

Example 3 with BukkitObjectInputStream

use of org.bukkit.util.io.BukkitObjectInputStream in project EliteMobs by MagmaGuy.

the class ObjectSerializer method itemStackArrayFromBase64.

/**
 * Gets an array of ItemStacks from Base64 string.
 * <p>
 * <p/>
 *
 * @param data Base64 string to convert to ItemStack array.
 * @return ItemStack array created from the Base64 string.
 * @throws IOException
 */
public static ItemStack itemStackArrayFromBase64(String data) throws IOException {
    try {
        ByteArrayInputStream inputStream = new ByteArrayInputStream(Base64Coder.decodeLines(data));
        BukkitObjectInputStream dataInput = new BukkitObjectInputStream(inputStream);
        ItemStack itemStack = (ItemStack) dataInput.readObject();
        dataInput.close();
        return itemStack;
    } catch (ClassNotFoundException e) {
        throw new IOException("Unable to decode class type.", e);
    }
}
Also used : BukkitObjectInputStream(org.bukkit.util.io.BukkitObjectInputStream) ItemStack(org.bukkit.inventory.ItemStack)

Example 4 with BukkitObjectInputStream

use of org.bukkit.util.io.BukkitObjectInputStream in project CitizensAPI by CitizensDev.

the class ItemStorage method deserialiseMeta.

private static void deserialiseMeta(DataKey root, ItemStack res) {
    if (root.keyExists("encoded-meta")) {
        byte[] raw = BaseEncoding.base64().decode(root.getString("encoded-meta"));
        try {
            BukkitObjectInputStream inp = new BukkitObjectInputStream(new ByteArrayInputStream(raw));
            ItemMeta meta = (ItemMeta) inp.readObject();
            res.setItemMeta(meta);
            Bukkit.getPluginManager().callEvent(new CitizensDeserialiseMetaEvent(root, res));
            return;
        } catch (IOException e1) {
            e1.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
    if (SUPPORTS_CUSTOM_MODEL_DATA) {
        try {
            if (root.keyExists("custommodel")) {
                ItemMeta meta = ensureMeta(res);
                meta.setCustomModelData(root.getInt("custommodel"));
                res.setItemMeta(meta);
            }
        } catch (Throwable t) {
            SUPPORTS_CUSTOM_MODEL_DATA = false;
        }
    }
    if (root.keyExists("flags")) {
        ItemMeta meta = ensureMeta(res);
        for (DataKey key : root.getRelative("flags").getIntegerSubKeys()) {
            meta.addItemFlags(ItemFlag.valueOf(key.getString("")));
        }
        res.setItemMeta(meta);
    }
    if (root.keyExists("lore")) {
        ItemMeta meta = ensureMeta(res);
        List<String> lore = Lists.newArrayList();
        for (DataKey key : root.getRelative("lore").getIntegerSubKeys()) lore.add(key.getString(""));
        meta.setLore(lore);
        res.setItemMeta(meta);
    }
    if (root.keyExists("displayname")) {
        ItemMeta meta = ensureMeta(res);
        meta.setDisplayName(root.getString("displayname"));
        res.setItemMeta(meta);
    }
    if (root.keyExists("firework")) {
        FireworkMeta meta = ensureMeta(res);
        for (DataKey sub : root.getRelative("firework.effects").getIntegerSubKeys()) {
            meta.addEffect(deserialiseFireworkEffect(sub));
        }
        meta.setPower(root.getInt("firework.power"));
        res.setItemMeta(meta);
    }
    if (root.keyExists("book")) {
        BookMeta meta = ensureMeta(res);
        for (DataKey sub : root.getRelative("book.pages").getIntegerSubKeys()) {
            meta.addPage(sub.getString(""));
        }
        meta.setTitle(root.getString("book.title"));
        meta.setAuthor(root.getString("book.author"));
        res.setItemMeta(meta);
    }
    if (root.keyExists("armor")) {
        LeatherArmorMeta meta = ensureMeta(res);
        meta.setColor(Color.fromRGB(root.getInt("armor.color")));
        res.setItemMeta(meta);
    }
    if (root.keyExists("map")) {
        MapMeta meta = ensureMeta(res);
        meta.setScaling(root.getBoolean("map.scaling"));
        res.setItemMeta(meta);
    }
    if (root.keyExists("blockstate")) {
        BlockStateMeta meta = ensureMeta(res);
        if (root.keyExists("blockstate.banner")) {
            Banner banner = (Banner) meta.getBlockState();
            deserialiseBanner(root.getRelative("blockstate"), banner);
            banner.update(true);
            meta.setBlockState(banner);
        }
        res.setItemMeta(meta);
    }
    if (root.keyExists("enchantmentstorage")) {
        EnchantmentStorageMeta meta = ensureMeta(res);
        for (DataKey key : root.getRelative("enchantmentstorage").getSubKeys()) {
            meta.addStoredEnchant(deserialiseEnchantment(key.name()), key.getInt(""), true);
        }
        res.setItemMeta(meta);
    }
    if (root.keyExists("skull")) {
        SkullMeta meta = ensureMeta(res);
        if (root.keyExists("skull.owner") && !root.getString("skull.owner").isEmpty()) {
            meta.setOwner(root.getString("skull.owner", ""));
        }
        if (root.keyExists("skull.texture") && !root.getString("skull.texture").isEmpty()) {
            CitizensAPI.getSkullMetaProvider().setTexture(root.getString("skull.texture", ""), meta);
        }
        res.setItemMeta(meta);
    }
    if (root.keyExists("banner")) {
        BannerMeta meta = ensureMeta(res);
        if (root.keyExists("banner.basecolor")) {
            meta.setBaseColor(DyeColor.valueOf(root.getString("banner.basecolor")));
        }
        if (root.keyExists("banner.patterns")) {
            for (DataKey sub : root.getRelative("banner.patterns").getIntegerSubKeys()) {
                Pattern pattern = new Pattern(DyeColor.valueOf(sub.getString("color")), PatternType.getByIdentifier(sub.getString("type")));
                meta.addPattern(pattern);
            }
        }
        res.setItemMeta(meta);
    }
    if (root.keyExists("potion")) {
        PotionMeta meta = ensureMeta(res);
        try {
            PotionData data = new PotionData(PotionType.valueOf(root.getString("potion.data.type")), root.getBoolean("potion.data.extended"), root.getBoolean("potion.data.upgraded"));
            meta.setBasePotionData(data);
        } catch (Throwable t) {
        }
        for (DataKey sub : root.getRelative("potion.effects").getIntegerSubKeys()) {
            int duration = sub.getInt("duration");
            int amplifier = sub.getInt("amplifier");
            PotionEffectType type = PotionEffectType.getByName(sub.getString("type"));
            boolean ambient = sub.getBoolean("ambient");
            meta.addCustomEffect(new PotionEffect(type, duration, amplifier, ambient), true);
        }
        res.setItemMeta(meta);
    }
    if (root.keyExists("crossbow") && SUPPORTS_1_14_API) {
        CrossbowMeta meta = null;
        try {
            meta = ensureMeta(res);
        } catch (Throwable t) {
            SUPPORTS_1_14_API = false;
        // old MC version
        }
        if (meta != null) {
            for (DataKey key : root.getRelative("crossbow.projectiles").getSubKeys()) {
                meta.addChargedProjectile(ItemStorage.loadItemStack(key));
            }
            res.setItemMeta(meta);
        }
    }
    if (root.keyExists("repaircost") && res.getItemMeta() instanceof Repairable) {
        ItemMeta meta = ensureMeta(res);
        ((Repairable) meta).setRepairCost(root.getInt("repaircost"));
        res.setItemMeta(meta);
    }
    if (root.keyExists("attributes") && SUPPORTS_ATTRIBUTES) {
        ItemMeta meta = ensureMeta(res);
        try {
            for (DataKey attr : root.getRelative("attributes").getSubKeys()) {
                Attribute attribute = Attribute.valueOf(attr.name());
                for (DataKey modifier : attr.getIntegerSubKeys()) {
                    UUID uuid = UUID.fromString(modifier.getString("uuid"));
                    String name = modifier.getString("name");
                    double amount = modifier.getDouble("amount");
                    Operation operation = Operation.valueOf(modifier.getString("operation"));
                    EquipmentSlot slot = modifier.keyExists("slot") ? EquipmentSlot.valueOf(modifier.getString("slot")) : null;
                    meta.addAttributeModifier(attribute, new AttributeModifier(uuid, name, amount, operation, slot));
                }
            }
        } catch (Throwable e) {
            SUPPORTS_ATTRIBUTES = false;
        }
        res.setItemMeta(meta);
    }
    ItemMeta meta = res.getItemMeta();
    if (meta != null) {
        try {
            meta.setUnbreakable(root.getBoolean("unbreakable", false));
        } catch (Throwable t) {
        // probably backwards-compat issue, don't log
        }
        res.setItemMeta(meta);
    }
    Bukkit.getPluginManager().callEvent(new CitizensDeserialiseMetaEvent(root, res));
}
Also used : CitizensDeserialiseMetaEvent(net.citizensnpcs.api.event.CitizensDeserialiseMetaEvent) PotionEffect(org.bukkit.potion.PotionEffect) Attribute(org.bukkit.attribute.Attribute) EquipmentSlot(org.bukkit.inventory.EquipmentSlot) SkullMeta(org.bukkit.inventory.meta.SkullMeta) Operation(org.bukkit.attribute.AttributeModifier.Operation) PotionData(org.bukkit.potion.PotionData) UUID(java.util.UUID) ItemMeta(org.bukkit.inventory.meta.ItemMeta) CrossbowMeta(org.bukkit.inventory.meta.CrossbowMeta) Repairable(org.bukkit.inventory.meta.Repairable) BannerMeta(org.bukkit.inventory.meta.BannerMeta) Pattern(org.bukkit.block.banner.Pattern) BukkitObjectInputStream(org.bukkit.util.io.BukkitObjectInputStream) Banner(org.bukkit.block.Banner) PotionEffectType(org.bukkit.potion.PotionEffectType) FireworkMeta(org.bukkit.inventory.meta.FireworkMeta) PotionMeta(org.bukkit.inventory.meta.PotionMeta) IOException(java.io.IOException) AttributeModifier(org.bukkit.attribute.AttributeModifier) BlockStateMeta(org.bukkit.inventory.meta.BlockStateMeta) EnchantmentStorageMeta(org.bukkit.inventory.meta.EnchantmentStorageMeta) ByteArrayInputStream(java.io.ByteArrayInputStream) MapMeta(org.bukkit.inventory.meta.MapMeta) LeatherArmorMeta(org.bukkit.inventory.meta.LeatherArmorMeta) BookMeta(org.bukkit.inventory.meta.BookMeta)

Aggregations

BukkitObjectInputStream (org.bukkit.util.io.BukkitObjectInputStream)4 ByteArrayInputStream (java.io.ByteArrayInputStream)2 IOException (java.io.IOException)2 UUID (java.util.UUID)1 CitizensDeserialiseMetaEvent (net.citizensnpcs.api.event.CitizensDeserialiseMetaEvent)1 Attribute (org.bukkit.attribute.Attribute)1 AttributeModifier (org.bukkit.attribute.AttributeModifier)1 Operation (org.bukkit.attribute.AttributeModifier.Operation)1 Banner (org.bukkit.block.Banner)1 Pattern (org.bukkit.block.banner.Pattern)1 EquipmentSlot (org.bukkit.inventory.EquipmentSlot)1 Inventory (org.bukkit.inventory.Inventory)1 ItemStack (org.bukkit.inventory.ItemStack)1 BannerMeta (org.bukkit.inventory.meta.BannerMeta)1 BlockStateMeta (org.bukkit.inventory.meta.BlockStateMeta)1 BookMeta (org.bukkit.inventory.meta.BookMeta)1 CrossbowMeta (org.bukkit.inventory.meta.CrossbowMeta)1 EnchantmentStorageMeta (org.bukkit.inventory.meta.EnchantmentStorageMeta)1 FireworkMeta (org.bukkit.inventory.meta.FireworkMeta)1 ItemMeta (org.bukkit.inventory.meta.ItemMeta)1