Search in sources :

Example 11 with CompoundTag

use of net.minecraft.nbt.CompoundTag in project MyPet by xXKeyleXx.

the class PlatformHelper method applyTagToEntity.

@Override
public void applyTagToEntity(TagCompound tag, Entity bukkitEntity) {
    net.minecraft.world.entity.Entity entity = ((CraftEntity) bukkitEntity).getHandle();
    CompoundTag vanillaNBT = (CompoundTag) ItemStackNBTConverter.compoundToVanillaCompound(tag);
    if (vanillaNBT != null) {
        if (bukkitEntity instanceof Villager) {
            net.minecraft.world.entity.npc.Villager villager = (net.minecraft.world.entity.npc.Villager) entity;
            villager.readAdditionalSaveData(vanillaNBT);
        } else if (bukkitEntity instanceof net.minecraft.world.entity.npc.WanderingTrader) {
            net.minecraft.world.entity.npc.WanderingTrader villager = (net.minecraft.world.entity.npc.WanderingTrader) entity;
            villager.addAdditionalSaveData(vanillaNBT);
        }
    }
}
Also used : CraftEntity(org.bukkit.craftbukkit.v1_17_R1.entity.CraftEntity) org.bukkit.entity(org.bukkit.entity) CompoundTag(net.minecraft.nbt.CompoundTag)

Example 12 with CompoundTag

use of net.minecraft.nbt.CompoundTag in project MyPet by xXKeyleXx.

the class ItemStackNBTConverter method compoundToVanillaCompound.

public static Tag compoundToVanillaCompound(TagBase tag) {
    switch(TagType.getTypeById(tag.getTagTypeId())) {
        case Int:
            return IntTag.valueOf(((TagInt) tag).getIntData());
        case Short:
            return ShortTag.valueOf(((TagShort) tag).getShortData());
        case String:
            return StringTag.valueOf(((TagString) tag).getStringData());
        case Byte:
            return ByteTag.valueOf(((TagByte) tag).getByteData());
        case Byte_Array:
            return new ByteArrayTag(((TagByteArray) tag).getByteArrayData());
        case Double:
            return DoubleTag.valueOf(((TagDouble) tag).getDoubleData());
        case Float:
            return FloatTag.valueOf(((TagFloat) tag).getFloatData());
        case Int_Array:
            return new IntArrayTag(((TagIntArray) tag).getIntArrayData());
        case Long:
            return LongTag.valueOf(((TagLong) tag).getLongData());
        case List:
            TagList TagList = (TagList) tag;
            ListTag tagList = new ListTag();
            for (TagBase tagInList : TagList.getReadOnlyList()) {
                tagList.add(compoundToVanillaCompound(tagInList));
            }
            return tagList;
        case Compound:
            TagCompound TagCompound = (TagCompound) tag;
            CompoundTag tagCompound = new CompoundTag();
            for (String name : TagCompound.getCompoundData().keySet()) {
                tagCompound.put(name, compoundToVanillaCompound(TagCompound.getCompoundData().get(name)));
            }
            return tagCompound;
        case End:
            return null;
    }
    throw new IllegalArgumentException("Not a valid tag type");
}
Also used : IntArrayTag(net.minecraft.nbt.IntArrayTag) TagBase(de.keyle.knbt.TagBase) TagList(de.keyle.knbt.TagList) TagString(de.keyle.knbt.TagString) ListTag(net.minecraft.nbt.ListTag) TagCompound(de.keyle.knbt.TagCompound) CompoundTag(net.minecraft.nbt.CompoundTag) ByteArrayTag(net.minecraft.nbt.ByteArrayTag)

Example 13 with CompoundTag

use of net.minecraft.nbt.CompoundTag in project MyPet by xXKeyleXx.

the class ItemStackNBTConverter method vanillaCompoundToCompound.

public static TagBase vanillaCompoundToCompound(Tag vanillaTag) {
    switch(vanillaTag.getId()) {
        case 1:
            return new TagByte(((ByteTag) vanillaTag).getAsByte());
        case 2:
            return new TagShort(((ShortTag) vanillaTag).getAsShort());
        case 3:
            return new TagInt(((IntTag) vanillaTag).getAsInt());
        case 4:
            return new TagLong(((LongTag) vanillaTag).getAsLong());
        case 5:
            return new TagFloat(((FloatTag) vanillaTag).getAsFloat());
        case 6:
            return new TagDouble(((DoubleTag) vanillaTag).getAsDouble());
        case 7:
            return new TagByteArray(((ByteArrayTag) vanillaTag).getAsByteArray());
        case 8:
            return new TagString(vanillaTag.getAsString());
        case 9:
            ListTag tagList = (ListTag) vanillaTag;
            List compoundList = new ArrayList();
            try {
                ArrayList list = (ArrayList) TAG_LIST_LIST.get(tagList);
                for (Object aList : list) {
                    compoundList.add(vanillaCompoundToCompound((Tag) aList));
                }
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
            return new TagList(compoundList);
        case 10:
            TagCompound compound = new TagCompound();
            CompoundTag tagCompound = ((CompoundTag) vanillaTag);
            Set<String> keys = tagCompound.getAllKeys();
            for (String tagName : keys) {
                compound.getCompoundData().put(tagName, vanillaCompoundToCompound(tagCompound.get(tagName)));
            }
            return compound;
        case 11:
            return new TagIntArray(((IntArrayTag) vanillaTag).getAsIntArray());
    }
    return null;
}
Also used : TagIntArray(de.keyle.knbt.TagIntArray) TagShort(de.keyle.knbt.TagShort) TagInt(de.keyle.knbt.TagInt) ArrayList(java.util.ArrayList) TagString(de.keyle.knbt.TagString) TagString(de.keyle.knbt.TagString) TagFloat(de.keyle.knbt.TagFloat) ListTag(net.minecraft.nbt.ListTag) TagCompound(de.keyle.knbt.TagCompound) TagDouble(de.keyle.knbt.TagDouble) TagByteArray(de.keyle.knbt.TagByteArray) TagLong(de.keyle.knbt.TagLong) TagList(de.keyle.knbt.TagList) ArrayList(java.util.ArrayList) TagList(de.keyle.knbt.TagList) List(java.util.List) DoubleTag(net.minecraft.nbt.DoubleTag) Tag(net.minecraft.nbt.Tag) StringTag(net.minecraft.nbt.StringTag) ByteTag(net.minecraft.nbt.ByteTag) IntTag(net.minecraft.nbt.IntTag) IntArrayTag(net.minecraft.nbt.IntArrayTag) FloatTag(net.minecraft.nbt.FloatTag) ByteArrayTag(net.minecraft.nbt.ByteArrayTag) LongTag(net.minecraft.nbt.LongTag) ShortTag(net.minecraft.nbt.ShortTag) CompoundTag(net.minecraft.nbt.CompoundTag) ListTag(net.minecraft.nbt.ListTag) TagByte(de.keyle.knbt.TagByte) CompoundTag(net.minecraft.nbt.CompoundTag)

Example 14 with CompoundTag

use of net.minecraft.nbt.CompoundTag in project MyPet by xXKeyleXx.

the class ItemStackNBTConverter method itemStackToCompound.

public static TagCompound itemStackToCompound(ItemStack itemStack) {
    CompoundTag tagCompound = new CompoundTag();
    itemStack.save(tagCompound);
    return (TagCompound) vanillaCompoundToCompound(tagCompound);
}
Also used : TagCompound(de.keyle.knbt.TagCompound) CompoundTag(net.minecraft.nbt.CompoundTag)

Example 15 with CompoundTag

use of net.minecraft.nbt.CompoundTag in project MyPet by xXKeyleXx.

the class PlatformHelper method entityToTag.

@Override
public TagCompound entityToTag(Entity bukkitEntity) {
    net.minecraft.world.entity.Entity entity = ((CraftEntity) bukkitEntity).getHandle();
    CompoundTag vanillaNBT = new CompoundTag();
    if (entity instanceof net.minecraft.world.entity.LivingEntity) {
        ((net.minecraft.world.entity.LivingEntity) entity).addAdditionalSaveData(vanillaNBT);
    } else {
        Method b = ReflectionUtil.getMethod(entity.getClass(), "b", CompoundTag.class);
        try {
            b.invoke(entity, vanillaNBT);
        } catch (IllegalAccessException | InvocationTargetException e) {
            e.printStackTrace();
        }
    }
    return (TagCompound) ItemStackNBTConverter.vanillaCompoundToCompound(vanillaNBT);
}
Also used : CraftEntity(org.bukkit.craftbukkit.v1_18_R1.entity.CraftEntity) Method(java.lang.reflect.Method) TagCompound(de.keyle.knbt.TagCompound) InvocationTargetException(java.lang.reflect.InvocationTargetException) CraftLivingEntity(org.bukkit.craftbukkit.v1_18_R1.entity.CraftLivingEntity) org.bukkit.entity(org.bukkit.entity) CompoundTag(net.minecraft.nbt.CompoundTag)

Aggregations

CompoundTag (net.minecraft.nbt.CompoundTag)31 ListTag (net.minecraft.nbt.ListTag)14 TagCompound (de.keyle.knbt.TagCompound)12 TagList (de.keyle.knbt.TagList)8 TagString (de.keyle.knbt.TagString)6 TagShort (de.keyle.knbt.TagShort)4 InvocationTargetException (java.lang.reflect.InvocationTargetException)4 ByteArrayTag (net.minecraft.nbt.ByteArrayTag)4 ResourceLocation (net.minecraft.resources.ResourceLocation)4 ItemStack (org.bukkit.inventory.ItemStack)4 CraftItemStack (org.bukkit.craftbukkit.v1_18_R1.inventory.CraftItemStack)3 Dynamic (com.mojang.serialization.Dynamic)2 MyPetBaby (de.Keyle.MyPet.api.entity.MyPetBaby)2 MyAxolotl (de.Keyle.MyPet.api.entity.types.MyAxolotl)2 MyBee (de.Keyle.MyPet.api.entity.types.MyBee)2 MyCat (de.Keyle.MyPet.api.entity.types.MyCat)2 MyCreeper (de.Keyle.MyPet.api.entity.types.MyCreeper)2 MyEnderman (de.Keyle.MyPet.api.entity.types.MyEnderman)2 MyGoat (de.Keyle.MyPet.api.entity.types.MyGoat)2 MyHorse (de.Keyle.MyPet.api.entity.types.MyHorse)2