Search in sources :

Example 1 with TagCompound

use of de.keyle.knbt.TagCompound in project MyPet by xXKeyleXx.

the class EntityConverterService method convertHorse.

public void convertHorse(Horse horse, TagCompound properties) {
    byte type = (byte) horse.getVariant().ordinal();
    int style = horse.getStyle().ordinal();
    int color = horse.getColor().ordinal();
    int variant = color & 255 | style << 8;
    if (horse.getInventory().getArmor() != null) {
        TagCompound armor = MyPetApi.getPlatformHelper().itemStackToCompund(horse.getInventory().getArmor());
        properties.getCompoundData().put("Armor", armor);
    }
    if (horse.getInventory().getSaddle() != null) {
        TagCompound saddle = MyPetApi.getPlatformHelper().itemStackToCompund(horse.getInventory().getSaddle());
        properties.getCompoundData().put("Saddle", saddle);
    }
    properties.getCompoundData().put("Type", new TagByte(type));
    properties.getCompoundData().put("Variant", new TagInt(variant));
    properties.getCompoundData().put("Chest", new TagByte(horse.isCarryingChest()));
    properties.getCompoundData().put("Age", new TagInt(horse.getAge()));
    if (horse.isCarryingChest()) {
        ItemStack[] contents = horse.getInventory().getContents();
        for (int i = 2; i < contents.length; i++) {
            ItemStack item = contents[i];
            if (item != null) {
                horse.getWorld().dropItem(horse.getLocation(), item);
            }
        }
    }
}
Also used : TagInt(de.keyle.knbt.TagInt) ItemStack(org.bukkit.inventory.ItemStack) TagCompound(de.keyle.knbt.TagCompound) TagByte(de.keyle.knbt.TagByte)

Example 2 with TagCompound

use of de.keyle.knbt.TagCompound in project MyPet by xXKeyleXx.

the class EntityConverterService method convertEntity.

@Override
public void convertEntity(MyPet myPet, LivingEntity normalEntity) {
    if (myPet instanceof MyCreeper) {
        if (((MyCreeper) myPet).isPowered()) {
            ((Creeper) normalEntity).setPowered(true);
        }
    } else if (myPet instanceof MyEnderman) {
        if (((MyEnderman) myPet).hasBlock()) {
            MaterialData materialData = new MaterialData(((MyEnderman) myPet).getBlock().getType(), ((MyEnderman) myPet).getBlock().getData().getData());
            ((Enderman) normalEntity).setCarriedMaterial(materialData);
        }
    } else if (myPet instanceof MyIronGolem) {
        ((IronGolem) normalEntity).setPlayerCreated(true);
    } else if (myPet instanceof MyMagmaCube) {
        ((MagmaCube) normalEntity).setSize(((MyMagmaCube) myPet).getSize());
    } else if (myPet instanceof MyOcelot) {
        ((Ocelot) normalEntity).setCatType(Ocelot.Type.WILD_OCELOT);
        ((Ocelot) normalEntity).setTamed(false);
    } else if (myPet instanceof MyPig) {
        ((Pig) normalEntity).setSaddle(((MyPig) myPet).hasSaddle());
    } else if (myPet instanceof MySheep) {
        ((Sheep) normalEntity).setSheared(((MySheep) myPet).isSheared());
        ((Sheep) normalEntity).setColor(((MySheep) myPet).getColor());
    } else if (myPet instanceof MyVillager) {
        MyVillager villagerPet = (MyVillager) myPet;
        Villager.Profession profession = Villager.Profession.values()[villagerPet.getProfession()];
        ((Villager) normalEntity).setProfession(profession);
        if (villagerPet.hasOriginalData()) {
            TagCompound villagerTag = MyPetApi.getPlatformHelper().entityToTag(normalEntity);
            for (String key : villagerPet.getOriginalData().getCompoundData().keySet()) {
                villagerTag.put(key, villagerPet.getOriginalData().get(key));
            }
            MyPetApi.getPlatformHelper().applyTagToEntity(villagerTag, normalEntity);
        }
    } else if (myPet instanceof MyWolf) {
        ((Wolf) normalEntity).setTamed(false);
    } else if (myPet instanceof MySlime) {
        ((Slime) normalEntity).setSize(((MySlime) myPet).getSize());
    } else if (myPet instanceof MyZombie) {
        ((Zombie) normalEntity).setVillager(((MyZombie) myPet).isVillager());
    } else if (myPet instanceof MySkeleton) {
        ((Skeleton) normalEntity).setSkeletonType(Skeleton.SkeletonType.values()[((MySkeleton) myPet).getType()]);
        if (((MySkeleton) myPet).isWither()) {
            normalEntity.getEquipment().setItemInHand(new ItemStack(Material.STONE_SWORD));
        } else {
            normalEntity.getEquipment().setItemInHand(new ItemStack(Material.BOW));
        }
    } else if (myPet instanceof MyPigZombie) {
        normalEntity.getEquipment().setItemInHand(new ItemStack(Material.GOLD_SWORD));
    } else if (myPet instanceof MyHorse) {
        Horse.Variant type = Horse.Variant.values()[((MyHorse) myPet).getHorseType()];
        Horse.Style style = Horse.Style.values()[(((MyHorse) myPet).getVariant() >>> 8)];
        Horse.Color color = Horse.Color.values()[(((MyHorse) myPet).getVariant() & 0xFF)];
        ((Horse) normalEntity).setVariant(type);
        ((Horse) normalEntity).setColor(color);
        ((Horse) normalEntity).setStyle(style);
        ((Horse) normalEntity).setCarryingChest(((MyHorse) myPet).hasChest());
        if (((MyHorse) myPet).hasSaddle()) {
            ((Horse) normalEntity).getInventory().setSaddle(((MyHorse) myPet).getSaddle().clone());
        }
        if (((MyHorse) myPet).hasArmor()) {
            ((Horse) normalEntity).getInventory().setArmor(((MyHorse) myPet).getArmor().clone());
        }
        ((Horse) normalEntity).setOwner(myPet.getOwner().getPlayer());
    }
    if (myPet instanceof MyPetBaby && normalEntity instanceof Ageable) {
        if (((MyPetBaby) myPet).isBaby()) {
            ((Ageable) normalEntity).setBaby();
        } else {
            ((Ageable) normalEntity).setAdult();
        }
    }
}
Also used : TagCompound(de.keyle.knbt.TagCompound) MyPetBaby(de.Keyle.MyPet.api.entity.MyPetBaby) MaterialData(org.bukkit.material.MaterialData) ItemStack(org.bukkit.inventory.ItemStack)

Example 3 with TagCompound

use of de.keyle.knbt.TagCompound in project MyPet by xXKeyleXx.

the class CustomInventory method save.

public TagCompound save(TagCompound compound) {
    List<TagCompound> itemList = new ArrayList<>();
    for (int i = 0; i < this.items.size(); i++) {
        ItemStack itemStack = this.items.get(i);
        if (itemStack != null) {
            TagCompound item = ItemStackNBTConverter.itemStackToCompund(itemStack);
            item.getCompoundData().put("Slot", new TagByte((byte) i));
            itemList.add(item);
        }
    }
    compound.getCompoundData().put("Items", new TagList(itemList));
    return compound;
}
Also used : ArrayList(java.util.ArrayList) TagList(de.keyle.knbt.TagList) CraftItemStack(org.bukkit.craftbukkit.v1_7_R4.inventory.CraftItemStack) TagCompound(de.keyle.knbt.TagCompound) TagByte(de.keyle.knbt.TagByte)

Example 4 with TagCompound

use of de.keyle.knbt.TagCompound in project MyPet by xXKeyleXx.

the class EntityConverterService method convertEntity.

@Override
public void convertEntity(MyPet myPet, LivingEntity normalEntity) {
    if (myPet instanceof MyCreeper) {
        if (((MyCreeper) myPet).isPowered()) {
            ((Creeper) normalEntity).setPowered(true);
        }
    } else if (myPet instanceof MyEnderman) {
        if (((MyEnderman) myPet).hasBlock()) {
            MaterialData materialData = new MaterialData(((MyEnderman) myPet).getBlock().getType(), ((MyEnderman) myPet).getBlock().getData().getData());
            ((Enderman) normalEntity).setCarriedMaterial(materialData);
        }
    } else if (myPet instanceof MyIronGolem) {
        ((IronGolem) normalEntity).setPlayerCreated(true);
    } else if (myPet instanceof MyMagmaCube) {
        ((MagmaCube) normalEntity).setSize(((MyMagmaCube) myPet).getSize());
    } else if (myPet instanceof MyOcelot) {
        ((Ocelot) normalEntity).setCatType(Ocelot.Type.WILD_OCELOT);
        ((Ocelot) normalEntity).setTamed(false);
    } else if (myPet instanceof MyPig) {
        ((Pig) normalEntity).setSaddle(((MyPig) myPet).hasSaddle());
    } else if (myPet instanceof MySheep) {
        ((Sheep) normalEntity).setSheared(((MySheep) myPet).isSheared());
        ((Sheep) normalEntity).setColor(((MySheep) myPet).getColor());
    } else if (myPet instanceof MyVillager) {
        MyVillager villagerPet = (MyVillager) myPet;
        Villager.Profession profession = Villager.Profession.values()[villagerPet.getProfession()];
        ((Villager) normalEntity).setProfession(profession);
        if (villagerPet.hasOriginalData()) {
            TagCompound villagerTag = MyPetApi.getPlatformHelper().entityToTag(normalEntity);
            for (String key : villagerPet.getOriginalData().getCompoundData().keySet()) {
                villagerTag.put(key, villagerPet.getOriginalData().get(key));
            }
            MyPetApi.getPlatformHelper().applyTagToEntity(villagerTag, normalEntity);
        }
    } else if (myPet instanceof MyWolf) {
        ((Wolf) normalEntity).setTamed(false);
    } else if (myPet instanceof MySlime) {
        ((Slime) normalEntity).setSize(((MySlime) myPet).getSize());
    } else if (myPet instanceof MyZombie) {
        ((Zombie) normalEntity).setVillager(((MyZombie) myPet).isVillager());
    } else if (myPet instanceof MySkeleton) {
        ((Skeleton) normalEntity).setSkeletonType(Skeleton.SkeletonType.values()[((MySkeleton) myPet).getType()]);
        if (((MySkeleton) myPet).isWither()) {
            normalEntity.getEquipment().setItemInHand(new ItemStack(Material.STONE_SWORD));
        } else {
            normalEntity.getEquipment().setItemInHand(new ItemStack(Material.BOW));
        }
    } else if (myPet instanceof MyPigZombie) {
        normalEntity.getEquipment().setItemInHand(new ItemStack(Material.GOLD_SWORD));
    } else if (myPet instanceof MyHorse) {
        Horse.Variant type = Horse.Variant.values()[((MyHorse) myPet).getHorseType()];
        Horse.Style style = Horse.Style.values()[(((MyHorse) myPet).getVariant() >>> 8)];
        Horse.Color color = Horse.Color.values()[(((MyHorse) myPet).getVariant() & 0xFF)];
        ((Horse) normalEntity).setVariant(type);
        ((Horse) normalEntity).setColor(color);
        ((Horse) normalEntity).setStyle(style);
        ((Horse) normalEntity).setCarryingChest(((MyHorse) myPet).hasChest());
        if (((MyHorse) myPet).hasSaddle()) {
            ((Horse) normalEntity).getInventory().setSaddle(((MyHorse) myPet).getSaddle().clone());
        }
        if (((MyHorse) myPet).hasArmor()) {
            ((Horse) normalEntity).getInventory().setArmor(((MyHorse) myPet).getArmor().clone());
        }
        ((Horse) normalEntity).setOwner(myPet.getOwner().getPlayer());
    } else if (myPet instanceof MyRabbit) {
        ((Rabbit) normalEntity).setRabbitType(((MyRabbit) myPet).getVariant().getBukkitType());
    } else if (myPet instanceof MyGuardian) {
        ((Guardian) normalEntity).setElder(((MyGuardian) myPet).isElder());
    }
    if (myPet instanceof MyPetBaby && normalEntity instanceof Ageable) {
        if (((MyPetBaby) myPet).isBaby()) {
            ((Ageable) normalEntity).setBaby();
        } else {
            ((Ageable) normalEntity).setAdult();
        }
    }
}
Also used : TagCompound(de.keyle.knbt.TagCompound) MyPetBaby(de.Keyle.MyPet.api.entity.MyPetBaby) MaterialData(org.bukkit.material.MaterialData) ItemStack(org.bukkit.inventory.ItemStack)

Example 5 with TagCompound

use of de.keyle.knbt.TagCompound in project MyPet by xXKeyleXx.

the class IconMenuInventory method createItemStack.

protected ItemStack createItemStack(IconMenuItem icon) {
    ItemStack is = CraftItemStack.asNMSCopy(new org.bukkit.inventory.ItemStack(icon.getMaterial(), icon.getAmount(), (short) icon.getData()));
    // TODO allow items like FIRE (51)
    if (is == null) {
        is = CraftItemStack.asNMSCopy(new org.bukkit.inventory.ItemStack(Material.SAPLING));
    }
    NBTTagList emptyList = new NBTTagList();
    if (is.getTag() == null) {
        is.setTag(new NBTTagCompound());
    }
    if (icon.getBukkitMeta() != null) {
        try {
            applyToItemMethhod.invoke(icon.getBukkitMeta(), is.getTag());
        } catch (InvocationTargetException | IllegalAccessException e) {
            e.printStackTrace();
        }
    }
    // remove item attributes like attack damage
    is.getTag().set("AttributeModifiers", emptyList);
    // add enchantment glowing
    if (icon.isGlowing()) {
        is.getTag().set("ench", emptyList);
    } else {
        is.getTag().remove("ench");
    }
    // Prepare display tag
    NBTTagCompound display;
    if (is.getTag().hasKey("display")) {
        display = is.getTag().getCompound("display");
    } else {
        display = new NBTTagCompound();
        is.getTag().set("display", display);
    }
    // set Title
    if (!icon.getTitle().equals("")) {
        display.setString("Name", icon.getTitle());
    }
    if (icon.getLore().size() > 0) {
        // set Lore
        NBTTagList loreTag = new NBTTagList();
        display.set("Lore", loreTag);
        for (String loreLine : icon.getLore()) {
            loreTag.add(new NBTTagString(loreLine));
        }
    }
    if (icon.hasMeta()) {
        TagCompound tag = new TagCompound();
        icon.getMeta().applyTo(tag);
        NBTTagCompound vanillaTag = (NBTTagCompound) ItemStackNBTConverter.compoundToVanillaCompound(tag);
        for (Object o : vanillaTag.c()) {
            String key = o.toString();
            is.getTag().set(key, vanillaTag.get(key));
        }
    }
    return is;
}
Also used : NBTTagCompound(net.minecraft.server.v1_8_R1.NBTTagCompound) NBTTagString(net.minecraft.server.v1_8_R1.NBTTagString) NBTTagCompound(net.minecraft.server.v1_8_R1.NBTTagCompound) TagCompound(de.keyle.knbt.TagCompound) InvocationTargetException(java.lang.reflect.InvocationTargetException) NBTTagList(net.minecraft.server.v1_8_R1.NBTTagList) NBTTagString(net.minecraft.server.v1_8_R1.NBTTagString) ItemStack(net.minecraft.server.v1_8_R1.ItemStack) CraftItemStack(org.bukkit.craftbukkit.v1_8_R1.inventory.CraftItemStack)

Aggregations

TagCompound (de.keyle.knbt.TagCompound)199 TagInt (de.keyle.knbt.TagInt)67 TagByte (de.keyle.knbt.TagByte)65 TagList (de.keyle.knbt.TagList)57 ItemStack (org.bukkit.inventory.ItemStack)53 ArrayList (java.util.ArrayList)27 TagString (de.keyle.knbt.TagString)24 InvocationTargetException (java.lang.reflect.InvocationTargetException)18 CompoundTag (net.minecraft.nbt.CompoundTag)12 MyPetBaby (de.Keyle.MyPet.api.entity.MyPetBaby)11 TagShort (de.keyle.knbt.TagShort)10 HashSet (java.util.HashSet)9 MaterialData (org.bukkit.material.MaterialData)9 ListTag (net.minecraft.nbt.ListTag)8 CraftItemStack (org.bukkit.craftbukkit.v1_17_R1.inventory.CraftItemStack)6 CraftItemStack (org.bukkit.craftbukkit.v1_18_R1.inventory.CraftItemStack)6 TagBase (de.keyle.knbt.TagBase)5 List (java.util.List)5 ItemStack (net.minecraft.world.item.ItemStack)4 WorldGroup (de.Keyle.MyPet.api.WorldGroup)3