use of de.keyle.knbt.TagCompound 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");
}
use of de.keyle.knbt.TagCompound 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;
}
use of de.keyle.knbt.TagCompound 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);
}
use of de.keyle.knbt.TagCompound 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);
}
use of de.keyle.knbt.TagCompound in project MyPet by xXKeyleXx.
the class IconMenuInventory method createItemStack.
protected ItemStack createItemStack(IconMenuItem icon) {
// TODO Check if this works properly
ItemStack is = CraftItemStack.asNMSCopy(new org.bukkit.inventory.ItemStack(icon.getMaterial(), icon.getAmount()));
if (is == null) {
is = CraftItemStack.asNMSCopy(new org.bukkit.inventory.ItemStack(Material.STONE));
}
if (is.getTag() == null) {
is.setTag(new CompoundTag());
}
if (icon.getBukkitMeta() != null) {
try {
applyToItemMethod.invoke(icon.getBukkitMeta(), is.getTag());
} catch (InvocationTargetException | IllegalAccessException e) {
e.printStackTrace();
}
}
// add enchantment glowing
if (icon.isGlowing()) {
TagCompound enchTag = new TagCompound();
enchTag.put("id", new TagString("minecraft:feather_falling"));
enchTag.put("lvl", new TagShort(1));
TagList enchList = new TagList();
enchList.addTag(enchTag);
is.getTag().put("Enchantments", ItemStackNBTConverter.compoundToVanillaCompound(enchList));
} else {
is.getTag().remove("Enchantments");
}
// hide item attributes like attack damage
is.getTag().putInt("HideFlags", 63);
// Prepare display tag
CompoundTag display;
if (is.getTag().contains("display")) {
display = is.getTag().getCompound("display");
} else {
display = new CompoundTag();
is.getTag().put("display", display);
}
// set Title
if (!icon.getTitle().equals("")) {
display.putString("Name", "{\"text\":\"" + icon.getTitle() + "\"}");
}
if (icon.getLore().size() > 0) {
// set Lore
ListTag loreTag = new ListTag();
display.put("Lore", loreTag);
for (String loreLine : icon.getLore()) {
Component cm = CraftChatMessage.fromStringOrNull(loreLine);
loreTag.add(StringTag.valueOf(Component.Serializer.toJson(cm)));
}
}
if (icon.hasMeta()) {
TagCompound tag = new TagCompound();
icon.getMeta().applyTo(tag);
CompoundTag vanillaTag = (CompoundTag) ItemStackNBTConverter.compoundToVanillaCompound(tag);
for (String key : vanillaTag.getAllKeys()) {
is.getTag().put(key, vanillaTag.get(key));
}
}
if (icon.getTags() != null) {
CompoundTag vanillaTag = (CompoundTag) ItemStackNBTConverter.compoundToVanillaCompound(icon.getTags());
for (String key : vanillaTag.getAllKeys()) {
is.getTag().put(key, vanillaTag.get(key));
}
}
return is;
}
Aggregations