Search in sources :

Example 6 with ListTag

use of com.wasteofplastic.org.jnbt.ListTag in project askyblock by tastybento.

the class NMSHandler method setBook.

@Override
public ItemStack setBook(Tag item) {
    ItemStack chestItem = new ItemStack(Material.WRITTEN_BOOK);
    // Bukkit.getLogger().info(item.toString());
    if (((CompoundTag) item).getValue().containsKey("tag")) {
        Map<String, Tag> contents = (Map<String, Tag>) ((CompoundTag) item).getValue().get("tag").getValue();
        // BookMeta bookMeta = (BookMeta) chestItem.getItemMeta();
        String author = "";
        if (contents.containsKey("author")) {
            author = ((StringTag) contents.get("author")).getValue();
        }
        // Bukkit.getLogger().info("Author: " + author);
        // bookMeta.setAuthor(author);
        String title = "";
        if (contents.containsKey("title")) {
            title = ((StringTag) contents.get("title")).getValue();
        }
        // Bukkit.getLogger().info("Title: " + title);
        // bookMeta.setTitle(title);
        List<String> lore = new ArrayList<String>();
        if (contents.containsKey("display")) {
            Map<String, Tag> display = (Map<String, Tag>) (contents.get("display")).getValue();
            List<Tag> loreTag = ((ListTag) display.get("Lore")).getValue();
            for (Tag s : loreTag) {
                lore.add(((StringTag) s).getValue());
            }
        }
        // Bukkit.getLogger().info("Lore: " + lore);
        net.minecraft.server.v1_8_R2.ItemStack stack = CraftItemStack.asNMSCopy(chestItem);
        // Pages
        // Create the NMS Stack's NBT (item data)
        NBTTagCompound tag = new NBTTagCompound();
        // Set the book's title
        tag.setString("title", title);
        tag.setString("author", author);
        if (contents.containsKey("pages")) {
            NBTTagList pages = new NBTTagList();
            List<Tag> pagesTag = ((ListTag) contents.get("pages")).getValue();
            for (Tag s : pagesTag) {
                pages.add(new NBTTagString(((StringTag) s).getValue()));
            }
            // Add the pages to the tag
            tag.set("pages", pages);
        }
        // Apply the tag to the item
        stack.setTag(tag);
        chestItem = CraftItemStack.asCraftMirror(stack);
        ItemMeta bookMeta = (ItemMeta) chestItem.getItemMeta();
        bookMeta.setLore(lore);
        chestItem.setItemMeta(bookMeta);
    }
    return chestItem;
}
Also used : StringTag(com.wasteofplastic.org.jnbt.StringTag) ArrayList(java.util.ArrayList) NBTTagCompound(net.minecraft.server.v1_8_R2.NBTTagCompound) NBTTagString(net.minecraft.server.v1_8_R2.NBTTagString) ListTag(com.wasteofplastic.org.jnbt.ListTag) NBTTagList(net.minecraft.server.v1_8_R2.NBTTagList) NBTTagString(net.minecraft.server.v1_8_R2.NBTTagString) StringTag(com.wasteofplastic.org.jnbt.StringTag) CompoundTag(com.wasteofplastic.org.jnbt.CompoundTag) Tag(com.wasteofplastic.org.jnbt.Tag) ListTag(com.wasteofplastic.org.jnbt.ListTag) CraftItemStack(org.bukkit.craftbukkit.v1_8_R2.inventory.CraftItemStack) ItemStack(org.bukkit.inventory.ItemStack) Map(java.util.Map) ItemMeta(org.bukkit.inventory.meta.ItemMeta)

Example 7 with ListTag

use of com.wasteofplastic.org.jnbt.ListTag in project askyblock by tastybento.

the class NMSHandler method setBook.

@Override
public ItemStack setBook(Tag item) {
    ItemStack chestItem = new ItemStack(Material.WRITTEN_BOOK);
    // Bukkit.getLogger().info(item.toString());
    if (((CompoundTag) item).getValue().containsKey("tag")) {
        Map<String, Tag> contents = (Map<String, Tag>) ((CompoundTag) item).getValue().get("tag").getValue();
        // BookMeta bookMeta = (BookMeta) chestItem.getItemMeta();
        String author = "";
        if (contents.containsKey("author")) {
            author = ((StringTag) contents.get("author")).getValue();
        }
        // Bukkit.getLogger().info("Author: " + author);
        // bookMeta.setAuthor(author);
        String title = "";
        if (contents.containsKey("title")) {
            title = ((StringTag) contents.get("title")).getValue();
        }
        // Bukkit.getLogger().info("Title: " + title);
        // bookMeta.setTitle(title);
        List<String> lore = new ArrayList<String>();
        if (contents.containsKey("display")) {
            Map<String, Tag> display = (Map<String, Tag>) (contents.get("display")).getValue();
            List<Tag> loreTag = ((ListTag) display.get("Lore")).getValue();
            for (Tag s : loreTag) {
                lore.add(((StringTag) s).getValue());
            }
        }
        // Bukkit.getLogger().info("Lore: " + lore);
        net.minecraft.server.v1_9_R2.ItemStack stack = CraftItemStack.asNMSCopy(chestItem);
        // Pages
        // Create the NMS Stack's NBT (item data)
        NBTTagCompound tag = new NBTTagCompound();
        // Set the book's title
        tag.setString("title", title);
        tag.setString("author", author);
        if (contents.containsKey("pages")) {
            NBTTagList pages = new NBTTagList();
            List<Tag> pagesTag = ((ListTag) contents.get("pages")).getValue();
            for (Tag s : pagesTag) {
                pages.add(new NBTTagString(((StringTag) s).getValue()));
            }
            // Add the pages to the tag
            tag.set("pages", pages);
        }
        // Apply the tag to the item
        stack.setTag(tag);
        chestItem = CraftItemStack.asCraftMirror(stack);
        ItemMeta bookMeta = (ItemMeta) chestItem.getItemMeta();
        bookMeta.setLore(lore);
        chestItem.setItemMeta(bookMeta);
    }
    return chestItem;
}
Also used : StringTag(com.wasteofplastic.org.jnbt.StringTag) ArrayList(java.util.ArrayList) NBTTagCompound(net.minecraft.server.v1_9_R2.NBTTagCompound) NBTTagString(net.minecraft.server.v1_9_R2.NBTTagString) ListTag(com.wasteofplastic.org.jnbt.ListTag) NBTTagList(net.minecraft.server.v1_9_R2.NBTTagList) NBTTagString(net.minecraft.server.v1_9_R2.NBTTagString) ListTag(com.wasteofplastic.org.jnbt.ListTag) StringTag(com.wasteofplastic.org.jnbt.StringTag) CompoundTag(com.wasteofplastic.org.jnbt.CompoundTag) Tag(com.wasteofplastic.org.jnbt.Tag) ItemStack(org.bukkit.inventory.ItemStack) CraftItemStack(org.bukkit.craftbukkit.v1_9_R2.inventory.CraftItemStack) Map(java.util.Map) ItemMeta(org.bukkit.inventory.meta.ItemMeta)

Example 8 with ListTag

use of com.wasteofplastic.org.jnbt.ListTag in project askyblock by tastybento.

the class IslandBlock method setChest.

@SuppressWarnings("deprecation")
public void setChest(NMSAbstraction nms, Map<String, Tag> tileData) {
    try {
        ListTag chestItems = (ListTag) tileData.get("Items");
        if (chestItems != null) {
            // int number = 0;
            for (Tag item : chestItems.getValue()) {
                if (item instanceof CompoundTag) {
                    try {
                        // Id is a number
                        short itemType = (Short) ((CompoundTag) item).getValue().get("id").getValue();
                        short itemDamage = (Short) ((CompoundTag) item).getValue().get("Damage").getValue();
                        byte itemAmount = (Byte) ((CompoundTag) item).getValue().get("Count").getValue();
                        byte itemSlot = (Byte) ((CompoundTag) item).getValue().get("Slot").getValue();
                        ItemStack chestItem = new ItemStack(itemType, itemAmount, itemDamage);
                        chestContents.put(itemSlot, chestItem);
                    } catch (ClassCastException ex) {
                        // Id is a material
                        String itemType = (String) ((CompoundTag) item).getValue().get("id").getValue();
                        try {
                            // Get the material
                            if (itemType.startsWith("minecraft:")) {
                                String material = itemType.substring(10).toUpperCase();
                                // Special case for non-standard material names
                                Material itemMaterial;
                                if (WEtoM.containsKey(material)) {
                                    // Bukkit.getLogger().info("DEBUG: Found in hashmap");
                                    itemMaterial = WEtoM.get(material);
                                } else {
                                    // Bukkit.getLogger().info("DEBUG: Not in hashmap");
                                    itemMaterial = Material.valueOf(material);
                                }
                                short itemDamage = (Short) ((CompoundTag) item).getValue().get("Damage").getValue();
                                byte itemAmount = (Byte) ((CompoundTag) item).getValue().get("Count").getValue();
                                byte itemSlot = (Byte) ((CompoundTag) item).getValue().get("Slot").getValue();
                                ItemStack chestItem = new ItemStack(itemMaterial, itemAmount, itemDamage);
                                if (itemMaterial.equals(Material.WRITTEN_BOOK)) {
                                    chestItem = nms.setBook(item);
                                }
                                // Check for potions
                                if (itemMaterial.toString().contains("POTION")) {
                                    chestItem = nms.setPotion(itemMaterial, item, chestItem);
                                }
                                chestContents.put(itemSlot, chestItem);
                            }
                        } catch (Exception exx) {
                            // Bukkit.getLogger().info(item.toString());
                            // Bukkit.getLogger().info(((CompoundTag)item).getValue().get("id").getName());
                            Bukkit.getLogger().severe("Could not parse item [" + itemType.substring(10).toUpperCase() + "] in schematic - skipping!");
                            // Bukkit.getLogger().severe(item.toString());
                            exx.printStackTrace();
                        }
                    }
                // Bukkit.getLogger().info("Set chest inventory slot "
                // + itemSlot + " to " +
                // chestItem.toString());
                }
            }
        // Bukkit.getLogger().info("Added " + number + " items to chest");
        }
    } catch (Exception e) {
        Bukkit.getLogger().severe("Could not parse schematic file item, skipping!");
    // e.printStackTrace();
    }
}
Also used : Material(org.bukkit.Material) StringTag(com.wasteofplastic.org.jnbt.StringTag) CompoundTag(com.wasteofplastic.org.jnbt.CompoundTag) Tag(com.wasteofplastic.org.jnbt.Tag) ListTag(com.wasteofplastic.org.jnbt.ListTag) ItemStack(org.bukkit.inventory.ItemStack) ListTag(com.wasteofplastic.org.jnbt.ListTag) CompoundTag(com.wasteofplastic.org.jnbt.CompoundTag) ParseException(org.json.simple.parser.ParseException)

Example 9 with ListTag

use of com.wasteofplastic.org.jnbt.ListTag in project askyblock by tastybento.

the class BannerBlock method prep.

@SuppressWarnings("deprecation")
public boolean prep(Map<String, Tag> tileData) {
    // x = Int
    try {
        // Do the base color
        int baseColor = 15 - ((IntTag) tileData.get("Base")).getValue();
        // //ASkyBlock.getPlugin().getLogger().info("Base value = " +
        // baseColor);
        // baseColor green = 10
        bannerBaseColor = DyeColor.getByDyeData((byte) baseColor);
        // Do the patterns (no idea if this will work or not)
        bannerPattern = new ArrayList<Pattern>();
        ListTag patterns = (ListTag) tileData.get("Patterns");
        if (patterns != null) {
            for (Tag pattern : patterns.getValue()) {
                // Translate pattern to PatternType
                if (pattern instanceof CompoundTag) {
                    CompoundTag patternColor = (CompoundTag) pattern;
                    // The tag is made up of pattern (String) and color
                    // (int)
                    Map<String, Tag> patternValue = patternColor.getValue();
                    StringTag mark = (StringTag) patternValue.get("Pattern");
                    Integer markColor = 15 - ((IntTag) patternValue.get("Color")).getValue();
                    // ASkyBlock.getPlugin().getLogger().info("mark = " +
                    // mark.getValue());
                    // ASkyBlock.getPlugin().getLogger().info("color = " +
                    // markColor);
                    DyeColor dColor = DyeColor.getByDyeData(markColor.byteValue());
                    // + dColor.toString());
                    if (patternKey.containsKey(mark.getValue())) {
                        Pattern newPattern = new Pattern(dColor, patternKey.get(mark.getValue()));
                        bannerPattern.add(newPattern);
                    }
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return true;
}
Also used : StringTag(com.wasteofplastic.org.jnbt.StringTag) Pattern(org.bukkit.block.banner.Pattern) ListTag(com.wasteofplastic.org.jnbt.ListTag) StringTag(com.wasteofplastic.org.jnbt.StringTag) CompoundTag(com.wasteofplastic.org.jnbt.CompoundTag) IntTag(com.wasteofplastic.org.jnbt.IntTag) Tag(com.wasteofplastic.org.jnbt.Tag) DyeColor(org.bukkit.DyeColor) ListTag(com.wasteofplastic.org.jnbt.ListTag) CompoundTag(com.wasteofplastic.org.jnbt.CompoundTag)

Example 10 with ListTag

use of com.wasteofplastic.org.jnbt.ListTag in project acidisland by tastybento.

the class NMSHandler method setBook.

@Override
public ItemStack setBook(Tag item) {
    ItemStack chestItem = new ItemStack(Material.WRITTEN_BOOK);
    // Bukkit.getLogger().info(item.toString());
    if (((CompoundTag) item).getValue().containsKey("tag")) {
        Map<String, Tag> contents = (Map<String, Tag>) ((CompoundTag) item).getValue().get("tag").getValue();
        // BookMeta bookMeta = (BookMeta) chestItem.getItemMeta();
        String author = "";
        if (contents.containsKey("author")) {
            author = ((StringTag) contents.get("author")).getValue();
        }
        // Bukkit.getLogger().info("Author: " + author);
        // bookMeta.setAuthor(author);
        String title = "";
        if (contents.containsKey("title")) {
            title = ((StringTag) contents.get("title")).getValue();
        }
        // Bukkit.getLogger().info("Title: " + title);
        // bookMeta.setTitle(title);
        List<String> lore = new ArrayList<String>();
        if (contents.containsKey("display")) {
            Map<String, Tag> display = (Map<String, Tag>) (contents.get("display")).getValue();
            List<Tag> loreTag = ((ListTag) display.get("Lore")).getValue();
            for (Tag s : loreTag) {
                lore.add(((StringTag) s).getValue());
            }
        }
        // Bukkit.getLogger().info("Lore: " + lore);
        net.minecraft.server.v1_11_R1.ItemStack stack = CraftItemStack.asNMSCopy(chestItem);
        // Pages
        // Create the NMS Stack's NBT (item data)
        NBTTagCompound tag = new NBTTagCompound();
        // Set the book's title
        tag.setString("title", title);
        tag.setString("author", author);
        if (contents.containsKey("pages")) {
            NBTTagList pages = new NBTTagList();
            List<Tag> pagesTag = ((ListTag) contents.get("pages")).getValue();
            for (Tag s : pagesTag) {
                pages.add(new NBTTagString(((StringTag) s).getValue()));
            }
            // Add the pages to the tag
            tag.set("pages", pages);
        }
        // Apply the tag to the item
        stack.setTag(tag);
        chestItem = CraftItemStack.asCraftMirror(stack);
        ItemMeta bookMeta = (ItemMeta) chestItem.getItemMeta();
        bookMeta.setLore(lore);
        chestItem.setItemMeta(bookMeta);
    }
    return chestItem;
}
Also used : StringTag(com.wasteofplastic.org.jnbt.StringTag) ArrayList(java.util.ArrayList) NBTTagCompound(net.minecraft.server.v1_11_R1.NBTTagCompound) NBTTagString(net.minecraft.server.v1_11_R1.NBTTagString) ListTag(com.wasteofplastic.org.jnbt.ListTag) NBTTagList(net.minecraft.server.v1_11_R1.NBTTagList) NBTTagString(net.minecraft.server.v1_11_R1.NBTTagString) ListTag(com.wasteofplastic.org.jnbt.ListTag) StringTag(com.wasteofplastic.org.jnbt.StringTag) CompoundTag(com.wasteofplastic.org.jnbt.CompoundTag) Tag(com.wasteofplastic.org.jnbt.Tag) ItemStack(org.bukkit.inventory.ItemStack) CraftItemStack(org.bukkit.craftbukkit.v1_11_R1.inventory.CraftItemStack) HashMap(java.util.HashMap) Map(java.util.Map) ItemMeta(org.bukkit.inventory.meta.ItemMeta)

Aggregations

CompoundTag (com.wasteofplastic.org.jnbt.CompoundTag)24 ListTag (com.wasteofplastic.org.jnbt.ListTag)24 StringTag (com.wasteofplastic.org.jnbt.StringTag)24 Tag (com.wasteofplastic.org.jnbt.Tag)24 ItemStack (org.bukkit.inventory.ItemStack)22 ArrayList (java.util.ArrayList)20 Map (java.util.Map)20 ItemMeta (org.bukkit.inventory.meta.ItemMeta)20 HashMap (java.util.HashMap)4 IntTag (com.wasteofplastic.org.jnbt.IntTag)2 NBTTagCompound (net.minecraft.server.v1_10_R1.NBTTagCompound)2 NBTTagList (net.minecraft.server.v1_10_R1.NBTTagList)2 NBTTagString (net.minecraft.server.v1_10_R1.NBTTagString)2 NBTTagCompound (net.minecraft.server.v1_11_R1.NBTTagCompound)2 NBTTagList (net.minecraft.server.v1_11_R1.NBTTagList)2 NBTTagString (net.minecraft.server.v1_11_R1.NBTTagString)2 NBTTagCompound (net.minecraft.server.v1_12_R1.NBTTagCompound)2 NBTTagList (net.minecraft.server.v1_12_R1.NBTTagList)2 NBTTagString (net.minecraft.server.v1_12_R1.NBTTagString)2 NBTTagCompound (net.minecraft.server.v1_7_R3.NBTTagCompound)2