2025-07-29 00:01:31 +03:00

100 lines
2.3 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package tokarotik.giftapi.NBT.item;
import net.minecraft.server.v1_6_R3.NBTTagList;
import net.minecraft.server.v1_6_R3.NBTTagString;
import org.bukkit.inventory.ItemStack;
import net.minecraft.server.v1_6_R3.NBTTagCompound;
import org.bukkit.inventory.meta.ItemMeta;
import java.util.Arrays;
import java.util.List;
public class ItemLoad
{
public static ItemStack getItem(NBTTagCompound tag)
{
ItemStack item = new ItemStack(
getId(tag), getCount(tag), getDurability(tag)
);
// тут чехорда. с начало сет айтем, после сет мета, а в самом конце гет мета
item.setItemMeta(
setMeta(
tag,
item.getItemMeta()
));
return item;
}
private static int getId(NBTTagCompound tag)
{
int id = 0;
if (tag.hasKey("id"))
{
id = tag.getInt("id");
}
return id;
}
private static short getCount(NBTTagCompound tag)
{
short count = 1;
if (tag.hasKey("count"))
{
count = tag.getShort("count");
}
return count;
}
private static short getDurability(NBTTagCompound tag)
{
short durability = 0;
if (tag.hasKey("damage"))
{
durability = tag.getShort("damage");
}
return durability;
}
private static ItemMeta setMeta(NBTTagCompound tag, ItemMeta meta)
{
if (tag.hasKey("meta"))
{
NBTTagCompound metaNBT = tag.getCompound("meta");
if (metaNBT.hasKey("name"))
{
meta.setDisplayName(metaNBT.getString("name"));
}
if (metaNBT.hasKey("lore"))
{
meta.setLore(
NBTListToStringList(
metaNBT.getList("lore")
)
);
}
}
return meta;
}
private static List<String> NBTListToStringList(NBTTagList nbt)
{
String[] list = new String[nbt.size()];
for (int i = 0; i < nbt.size(); i++)
{
NBTTagString base = (NBTTagString) nbt.get(i);
list[i] = base.data;
}
return Arrays.asList(list);
}
}