Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ru.proempire.lisenochek.pj.anarchy.utils;
- import com.comphenix.protocol.utility.MinecraftReflection;
- import com.comphenix.protocol.wrappers.nbt.NbtFactory;
- import com.google.common.collect.Lists;
- import org.bukkit.Color;
- import org.bukkit.Material;
- import org.bukkit.enchantments.Enchantment;
- import org.bukkit.inventory.ItemFlag;
- import org.bukkit.inventory.ItemStack;
- import org.bukkit.inventory.meta.ItemMeta;
- import org.bukkit.inventory.meta.LeatherArmorMeta;
- import org.bukkit.util.io.BukkitObjectInputStream;
- import org.bukkit.util.io.BukkitObjectOutputStream;
- import org.yaml.snakeyaml.external.biz.base64Coder.Base64Coder;
- import java.io.ByteArrayInputStream;
- import java.io.ByteArrayOutputStream;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.List;
- public class ISBuilder {
- private ItemStack stack;
- private ItemMeta meta;
- private ISBuilder(ItemStack stack) {
- this.stack = MinecraftReflection.isCraftItemStack(stack) ? stack : MinecraftReflection.getBukkitItemStack(stack);
- this.meta = stack.getItemMeta();
- }
- public static ISBuilder getBuilder(ItemStack stack) {
- return new ISBuilder(stack);
- }
- public static ISBuilder getBuilder(Material material, int data, int count, String name, String... lore) {
- return getBuilder(material, data, count, name, Arrays.asList(lore));
- }
- public static ISBuilder getBuilder(Material material, int data, int count, String name, List<String> lore) {
- ItemStack stack = new ItemStack(material, count, (short) data);
- ItemMeta meta = stack.getItemMeta();
- meta.setDisplayName(Utils.stripColor(name));
- List<String> l = new ArrayList<>();
- for (String s : lore) if (s != null) l.add(Utils.stripColor(s));
- meta.setLore(l);
- stack.setItemMeta(meta);
- return new ISBuilder(stack);
- }
- public static String serialise(List<ItemStack> list) {
- try {
- ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
- BukkitObjectOutputStream dataOutput = new BukkitObjectOutputStream(outputStream);
- dataOutput.writeInt(list.size());
- for (ItemStack stack : list) dataOutput.writeObject(stack);
- dataOutput.close();
- return Base64Coder.encodeLines(outputStream.toByteArray());
- } catch (Exception e) {
- throw new IllegalStateException("Unable to save item stacks", e);
- }
- }
- public static String serialise(ItemStack stack) {
- try {
- ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
- BukkitObjectOutputStream dataOutput = new BukkitObjectOutputStream(outputStream);
- dataOutput.writeInt(1);
- dataOutput.writeObject(stack);
- dataOutput.close();
- return Base64Coder.encodeLines(outputStream.toByteArray());
- } catch (Exception e) {
- throw new IllegalStateException("Unable to save item stack", e);
- }
- }
- public static List<ItemStack> deserialiseStacks(String stacksData) {
- if (stacksData == null) return new ArrayList<>();
- ItemStack[] stacks = null;
- try {
- ByteArrayInputStream inputStream = new ByteArrayInputStream(Base64Coder.decodeLines(stacksData));
- BukkitObjectInputStream dataInput = new BukkitObjectInputStream(inputStream);
- stacks = new ItemStack[dataInput.readInt()];
- for (int i = 0; i < stacks.length; i++) stacks[i] = (ItemStack) dataInput.readObject();
- dataInput.close();
- } catch (Exception e) {
- e.printStackTrace();
- }
- return Lists.newArrayList(stacks);
- }
- public static ItemStack deserialiseStack(String stackData) {
- ItemStack stack = null;
- try {
- ByteArrayInputStream inputStream = new ByteArrayInputStream(Base64Coder.decodeLines(stackData));
- BukkitObjectInputStream dataInput = new BukkitObjectInputStream(inputStream);
- ItemStack[] s = new ItemStack[dataInput.readInt()];
- for (int i = 0; i < s.length; i++) s[i] = (ItemStack) dataInput.readObject();
- stack = s[0];
- dataInput.close();
- } catch (Exception e) {
- e.printStackTrace();
- }
- return stack;
- }
- public ItemStack getStack() {
- return stack;
- }
- public ISBuilder setUnbreakable(boolean unbreakable) {
- meta.setUnbreakable(unbreakable);
- stack.setItemMeta(meta);
- return this;
- }
- public ISBuilder setDisplayName(String displayName) {
- meta.setDisplayName(Utils.stripColor(displayName));
- stack.setItemMeta(meta);
- return this;
- }
- public ISBuilder setEnchantGlow() {
- meta.addEnchant(Enchantment.ARROW_DAMAGE, 1, true);
- stack.setItemMeta(meta);
- return this;
- }
- public ISBuilder setTag(String tag, Object value) {
- if (stack != null && stack.getType() != Material.AIR) NbtFactory.asCompound(NbtFactory.fromItemTag(stack)).putObject(tag, value);
- return this;
- }
- public boolean hasTag(String tag) {
- return NbtFactory.asCompound(NbtFactory.fromItemTag(stack)).getObject(tag) != null;
- }
- public int getIntTag(String tag) {
- return NbtFactory.asCompound(NbtFactory.fromItemTag(stack)).getInteger(tag);
- }
- public String getStringTag(String tag) {
- return NbtFactory.asCompound(NbtFactory.fromItemTag(stack)).getString(tag);
- }
- public double getDoubleTag(String tag) {
- return NbtFactory.asCompound(NbtFactory.fromItemTag(stack)).getDouble(tag);
- }
- public boolean getBooleanTag(String tag) {
- return Boolean.parseBoolean(NbtFactory.asCompound(NbtFactory.fromItemTag(stack)).getString(tag));
- }
- public ISBuilder removeTag(String tag) {
- if (stack != null && stack.getType() != Material.AIR) NbtFactory.asCompound(NbtFactory.fromItemTag(stack)).remove(tag);
- return this;
- }
- public ISBuilder addEnchantment(Enchantment enchantment, int level) {
- if (enchantment == null) return this;
- meta.addEnchant(enchantment, level, true);
- stack.setItemMeta(meta);
- return this;
- }
- public ISBuilder setAmount(int amount) {
- stack.setAmount(amount);
- return this;
- }
- public ISBuilder setColor(Color color) {
- ((LeatherArmorMeta) meta).setColor(color);
- stack.setItemMeta(meta);
- return this;
- }
- public ISBuilder formatLore(String oldReplace, Object newReplace) {
- List<String> list = new ArrayList<>();
- for (String s : meta.getLore()) if (oldReplace != null) list.add(Utils.stripColor(s.replace(oldReplace, String.valueOf(newReplace))));
- meta.setLore(list);
- stack.setItemMeta(meta);
- return this;
- }
- public ISBuilder setLore(String... lore) {
- List<String> list = new ArrayList<>();
- if (lore != null) for (String s : lore) list.add(Utils.stripColor(s));
- meta.setLore(list);
- stack.setItemMeta(meta);
- return this;
- }
- public ISBuilder addLore(String... lore) {
- List<String> list = meta.hasLore() ? meta.getLore() : new ArrayList<>();
- if (lore != null) for (String s : lore) list.add(Utils.stripColor(s));
- meta.setLore(list);
- stack.setItemMeta(meta);
- return this;
- }
- public ISBuilder addLore(List<String> lore) {
- List<String> list = meta.hasLore() ? meta.getLore() : new ArrayList<>();
- if (lore != null) for (String s : lore) list.add(Utils.stripColor(s));
- meta.setLore(list);
- stack.setItemMeta(meta);
- return this;
- }
- public ISBuilder hideFlags() {
- for (ItemFlag flag : new ItemFlag[]{ItemFlag.HIDE_ATTRIBUTES, ItemFlag.HIDE_ENCHANTS, ItemFlag.HIDE_UNBREAKABLE, ItemFlag.HIDE_POTION_EFFECTS}) meta.addItemFlags(flag);
- stack.setItemMeta(meta);
- return this;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement