Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package me.filipenock.cypherapi.Utils;
- import java.io.ByteArrayInputStream;
- import java.io.ByteArrayOutputStream;
- import java.io.IOException;
- import java.lang.reflect.Field;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.List;
- import java.util.UUID;
- import org.apache.commons.codec.binary.Base64;
- import org.bukkit.Color;
- import org.bukkit.DyeColor;
- import org.bukkit.Material;
- import org.bukkit.block.banner.Pattern;
- import org.bukkit.block.banner.PatternType;
- import org.bukkit.enchantments.Enchantment;
- import org.bukkit.inventory.ItemFlag;
- import org.bukkit.inventory.ItemStack;
- import org.bukkit.inventory.meta.BannerMeta;
- import org.bukkit.inventory.meta.ItemMeta;
- import org.bukkit.inventory.meta.LeatherArmorMeta;
- import org.bukkit.inventory.meta.PotionMeta;
- import org.bukkit.inventory.meta.SkullMeta;
- import org.bukkit.potion.Potion;
- import org.bukkit.potion.PotionEffect;
- import org.bukkit.potion.PotionEffectType;
- import org.bukkit.potion.PotionType;
- import org.bukkit.util.io.BukkitObjectInputStream;
- import org.bukkit.util.io.BukkitObjectOutputStream;
- import org.yaml.snakeyaml.external.biz.base64Coder.Base64Coder;
- import com.mojang.authlib.GameProfile;
- import com.mojang.authlib.properties.Property;
- @SuppressWarnings("all")
- public class ItemBuilder {
- private ItemStack is;
- public ItemBuilder(Material s, int data) {
- is = new ItemStack(s, 1, (short) 1, (byte) data);
- }
- public ItemBuilder(Material s) {
- is = new ItemStack(s);
- }
- public ItemBuilder setName(String name) {
- ItemMeta ism = is.getItemMeta();
- ism.setDisplayName(name.replace("&", "§"));
- is.setItemMeta(ism);
- return this;
- }
- public ItemBuilder addPattern(PatternType type, DyeColor color) {
- BannerMeta meta = (BannerMeta) is.getItemMeta();
- meta.addPattern(new Pattern(color, type));
- is.setItemMeta(meta);
- return this;
- }
- public ItemBuilder setBasecolor(DyeColor color) {
- BannerMeta meta = (BannerMeta) is.getItemMeta();
- meta.setBaseColor(color);
- is.setItemMeta(meta);
- return this;
- }
- public ItemBuilder setSkullOwner(String owner) {
- try {
- SkullMeta im = (SkullMeta) is.getItemMeta();
- im.setOwner(owner);
- is.setItemMeta(im);
- } catch (ClassCastException expected) {
- }
- return this;
- }
- public ItemBuilder setLatherColor(Color color) {
- LeatherArmorMeta im = (LeatherArmorMeta) is.getItemMeta();
- im.setColor(color);
- is.setItemMeta(im);
- return this;
- }
- public ItemBuilder removeFlags() {
- ItemMeta im = is.getItemMeta();
- im.addItemFlags(ItemFlag.HIDE_ATTRIBUTES);
- im.addItemFlags(ItemFlag.HIDE_ENCHANTS);
- im.addItemFlags(ItemFlag.HIDE_UNBREAKABLE);
- im.addItemFlags(ItemFlag.HIDE_POTION_EFFECTS);
- is.setItemMeta(im);
- return this;
- }
- public ItemBuilder addEnchant(Enchantment ench, int level) {
- ItemMeta im = is.getItemMeta();
- im.addEnchant(ench, level, true);
- is.setItemMeta(im);
- return this;
- }
- public ItemBuilder addLoreLine(String line) {
- ItemMeta im = is.getItemMeta();
- List<String> lore = new ArrayList<>();
- if (im.hasLore())
- lore = new ArrayList<>(im.getLore());
- lore.add(line);
- im.setLore(lore);
- is.setItemMeta(im);
- return this;
- }
- public ItemBuilder addLoreLine(String line, int pos) {
- ItemMeta im = is.getItemMeta();
- List<String> lore = new ArrayList<>(im.getLore());
- lore.set(pos, line);
- im.setLore(lore);
- is.setItemMeta(im);
- return this;
- }
- public ItemBuilder addPotionEffect(PotionEffectType type, int duration, int amplifier) {
- PotionMeta meta = (PotionMeta) is.getItemMeta();
- meta.addCustomEffect(new PotionEffect(type, duration, amplifier), true);
- is.setItemMeta(meta);
- return this;
- }
- public ItemStack build() {
- return is;
- }
- public static boolean isEqual(ItemStack item1, ItemStack item2) {
- if (item1 == null)
- return false;
- if (item2 == null)
- return false;
- if (item1.getType() == null)
- return false;
- if (item2.getType() == null)
- return false;
- if (item1.getType() == item2.getType()) {
- if (item1.getItemMeta().hasDisplayName()) {
- if (item2.getItemMeta().hasDisplayName()) {
- if (item1.getItemMeta().getDisplayName().equals(item2.getItemMeta().getDisplayName())) {
- if (item1.getData().getData() == item2.getData().getData()) {
- return true;
- }
- }
- }
- }
- }
- return false;
- }
- public static String DeSerialize(ItemStack item) {
- String serialized = "";
- StringBuilder builder = new StringBuilder(serialized);
- builder.append("id=" + item.getType().name() + " : ");
- builder.append("data=" + item.getData().getData() + " : ");
- if (item.getItemMeta().hasDisplayName()) {
- builder.append("name=" + item.getItemMeta().getDisplayName().replace("§", "&") + " : ");
- }
- if (item.getItemMeta().hasLore()) {
- List<String> lore = item.getItemMeta().getLore();
- List<String> formated = new ArrayList<>();
- for (String l : lore) {
- formated.add(l.replace("§", "&"));
- }
- builder.append("lore=" + String.join("/-/", formated) + " : ");
- }
- if (item.getItemMeta().hasEnchants()) {
- for (Enchantment en : item.getItemMeta().getEnchants().keySet()) {
- builder.append("enchant=" + en.getName() + ";" + item.getItemMeta().getEnchants().get(en) + " : ");
- }
- }
- if (item.getItemMeta().hasItemFlag(ItemFlag.HIDE_ATTRIBUTES)
- || item.getItemMeta().hasItemFlag(ItemFlag.HIDE_ENCHANTS)
- || item.getItemMeta().hasItemFlag(ItemFlag.HIDE_POTION_EFFECTS)
- || item.getItemMeta().hasItemFlag(ItemFlag.HIDE_UNBREAKABLE)) {
- builder.append("hideflags=true" + " : ");
- }
- return builder.toString();
- }
- public static ItemStack Serialize(String fromtext) {
- // id=TEXT/NUMBER : data=NUMBER : amount=16 : name=TEXT : lore=TEXT/-/TEXT :
- // enchant=DAMAGE_ALL;5 : hideflags=BOOLEAN : potioneffect=SPEED;10;2;true : texture=TEXT
- ItemStack item = null;
- String[] textparts = fromtext.split(" : ");
- boolean hideflags = false;
- int material = 0;
- Material materialreal = null;
- byte data = 0;
- String name = null;
- List<String> lore = null;
- List<String> enchants = new ArrayList<>();
- int amount = 0;
- List<String> potions = new ArrayList<>();
- String headtexture = null;
- for (String part : Arrays.asList(textparts)) {
- if (part.contains("id=")) {
- try {
- String cc = part.replace("id=", "");
- material = Integer.valueOf(cc);
- } catch (Exception e) {
- String cc = part.replace("id=", "");
- materialreal = Material.valueOf(cc.toUpperCase());
- }
- }
- if (part.contains("data=")) {
- try {
- String cc = part.replace("data=", "");
- data = Byte.valueOf(cc);
- } catch (Exception e) {
- data = 0;
- }
- }
- if (part.contains("name=")) {
- String cc = part.replace("name=", "").replace("&", "§");
- name = cc;
- }
- if (part.contains("lore=")) {
- String[] lorelines = part.replace("&", "§").replace("lore=", "").split("/-/");
- lore = Arrays.asList(lorelines);
- }
- if (part.contains("enchant=")) {
- String replaced = part.replace("enchant=", "");
- enchants.add(replaced);
- }
- if (part.contains("hideflags=true")) {
- hideflags = true;
- }
- if (part.contains("amount=")) {
- amount = Integer.valueOf(part.replace("amount=", ""));
- }
- if (part.contains("potioneffect=")) {
- potions.add(part.replace("potioneffect=", ""));
- }
- if (part.contains("texture=")) {
- headtexture = part.replace("texture=", "");
- }
- }
- if (material != 0) {
- if (materialreal == null) {
- if (data == 0) {
- item = new ItemStack(material);
- } else {
- item = new ItemStack(material, 1, (short) 0, data);
- }
- }
- } else {
- if (materialreal != null) {
- if (data == 0) {
- item = new ItemStack(materialreal);
- } else {
- item = new ItemStack(materialreal, 1, (short) 0, data);
- }
- }
- }
- if (name != null) {
- ItemMeta meta = item.getItemMeta();
- meta.setDisplayName(name);
- item.setItemMeta(meta);
- }
- if (lore != null) {
- ItemMeta meta = item.getItemMeta();
- meta.setLore(lore);
- item.setItemMeta(meta);
- }
- if (enchants.size() > 0) {
- ItemMeta meta = item.getItemMeta();
- for (String ec : enchants) {
- String[] splitter = ec.split(";");
- Enchantment enchantter = null;
- try {
- enchantter = Enchantment.getById(Integer.valueOf(splitter[0]));
- } catch (Exception e) {
- String encname = splitter[0];
- enchantter = Enchantment.getByName(encname);
- }
- try {
- meta.addEnchant(enchantter, Integer.parseInt(splitter[1]), true);
- } catch (Exception e) {
- }
- }
- item.setItemMeta(meta);
- }
- if (hideflags) {
- ItemMeta meta = item.getItemMeta();
- meta.addItemFlags(ItemFlag.HIDE_ENCHANTS);
- meta.addItemFlags(ItemFlag.HIDE_ATTRIBUTES);
- meta.addItemFlags(ItemFlag.HIDE_POTION_EFFECTS);
- meta.addItemFlags(ItemFlag.HIDE_UNBREAKABLE);
- item.setItemMeta(meta);
- }
- if (amount != 0) {
- item.setAmount(amount);
- }
- if (potions.size() > 0) {
- PotionMeta meta = (PotionMeta) item.getItemMeta();
- boolean splash = false;
- for (String st : potions) {
- String[] split = st.split(";");
- meta.addCustomEffect(new PotionEffect(PotionEffectType.getByName(split[0].toUpperCase()),
- 20 * Integer.valueOf(split[1]), Integer.valueOf(split[2])), true);
- if (split[3] != null) {
- splash = Boolean.valueOf(split[3]);
- }
- }
- item.setItemMeta(meta);
- Potion potion = Potion.fromItemStack(item);
- potion.setSplash(splash);
- potion.apply(item);
- }
- if (headtexture != null) {
- String skinURL = "http://textures.minecraft.net/texture/"+headtexture;
- ItemMeta headMeta = item.getItemMeta();
- GameProfile profile = new GameProfile(UUID.randomUUID(), null);
- byte[] encodedData = Base64.encodeBase64(String.format("{textures:{SKIN:{url:\"%s\"}}}", skinURL).getBytes());
- profile.getProperties().put("textures", new Property("textures", new String(encodedData)));
- Field profileField = null;
- try {
- profileField = headMeta.getClass().getDeclaredField("profile");
- } catch (NoSuchFieldException | SecurityException e) {
- }
- profileField.setAccessible(true);
- try {
- profileField.set(headMeta, profile);
- } catch (IllegalArgumentException | IllegalAccessException e) {
- }
- item.setItemMeta(headMeta);
- }
- return item;
- }
- public static ItemStack getSkull(String skin) {
- String skinURL = "http://textures.minecraft.net/texture/"+skin;
- ItemStack stack = new ItemBuilder(Material.SKULL_ITEM, 3).build();
- ItemMeta headMeta = stack.getItemMeta();
- GameProfile profile = new GameProfile(UUID.randomUUID(), null);
- byte[] encodedData = Base64.encodeBase64(String.format("{textures:{SKIN:{url:\"%s\"}}}", skinURL).getBytes());
- profile.getProperties().put("textures", new Property("textures", new String(encodedData)));
- Field profileField = null;
- try {
- profileField = headMeta.getClass().getDeclaredField("profile");
- } catch (NoSuchFieldException | SecurityException e) {
- }
- profileField.setAccessible(true);
- try {
- profileField.set(headMeta, profile);
- } catch (IllegalArgumentException | IllegalAccessException e) {
- }
- stack.setItemMeta(headMeta);
- return stack;
- }
- public static String itemto64(ItemStack stack) {
- try {
- ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
- BukkitObjectOutputStream dataOutput = new BukkitObjectOutputStream(outputStream);
- dataOutput.writeObject(stack);
- dataOutput.close();
- return Base64Coder.encodeLines(outputStream.toByteArray());
- } catch (Exception e) {
- throw new IllegalStateException("Nao foi possivel.", e);
- }
- }
- public static ItemStack itemFrom64(String data) {
- try {
- ByteArrayInputStream inputStream = new ByteArrayInputStream(Base64Coder.decodeLines(data));
- BukkitObjectInputStream dataInput = new BukkitObjectInputStream(inputStream);
- try {
- return (ItemStack) dataInput.readObject();
- } finally {
- dataInput.close();
- }
- } catch (Exception e) {
- throw new IllegalStateException("Nao foi possivel.", e);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement