package gq.unurled.raxen.utils; import com.google.gson.Gson; import com.google.gson.reflect.TypeToken; import de.tr7zw.changeme.nbtapi.NBTItem; import gq.unurled.raxen.Raxen; import net.kyori.adventure.text.Component; import org.bukkit.ChatColor; import org.bukkit.Material; import org.bukkit.enchantments.Enchantment; import org.bukkit.entity.Player; import org.bukkit.inventory.ItemFlag; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.ItemMeta; import java.util.*; import java.util.logging.Logger; public class Utils { private static Logger logger = Raxen.getPluginLogger(); public static String color(String string) { return ChatColor.translateAlternateColorCodes('&', string); } public static String decolor(String string) { return ChatColor.stripColor(color(string)); } public static void log(String... strings) { for(String string : strings) { logger.info(string); } } public static void warn(String... strings) { for(String string : strings) { logger.warning(string); } } public static void error(String... strings) { for(String string : strings) { logger.severe(string); } } public static void msgPlayer(Player player, String... strings) { for(String string : strings) { player.sendMessage(Component.text(color(string))); } } public static ItemStack createItem(Material material, int amount, boolean glow, boolean unb, String name, String... lore) { ItemStack item = new ItemStack(material, amount); ItemMeta itemMeta = item.getItemMeta(); if(name != null) { itemMeta.displayName(Component.text(color(name))); } if(lore != null) { List list = new ArrayList<>(); for(String string : lore) { list.add(Component.text(color(string))); } itemMeta.lore(list); } if(glow) { itemMeta.addItemFlags(ItemFlag.HIDE_ENCHANTS); itemMeta.addEnchant(Enchantment.DURABILITY, 1, true); } if(unb) { itemMeta.setUnbreakable(true); } item.setItemMeta(itemMeta); return item; } public static ItemStack editItem(ItemStack item, int amount, List lore) { if(amount == 0) { amount = 1; } item.setAmount(amount); ItemMeta itemMeta = item.getItemMeta(); itemMeta.lore(lore); item.setItemMeta(itemMeta); return item; } public static Component noPerms() { return Component.text(color("&cYou don't have the permission to use this feature.")); } public static Component error() { return Component.text(color("&cAn Error has occurred. Please retry or contact an Admin.")); } public static String itemStackSerilize(ItemStack it, Integer slot) { Gson gson = new Gson(); //String str = gson.toJson(it.serialize()); String str = gson.toJson(it.serialize(), new TypeToken(){}.getType()); NBTItem nbti = new NBTItem(it); str = str + "@#NBT#" + nbti.toString(); str = str + "@#SLOT#" + slot.toString(); return str; } public static class itemStackDeserilizeResult { int slot; ItemStack it; public itemStackDeserilizeResult(Integer slot, ItemStack it) { this.slot = slot; this.it = it; } } public static itemStackDeserilizeResult itemStackDeserilize(String str) { List mapp = Arrays.asList(str.split("@#NBT#")); Gson gson = new Gson(); ItemStack it = new ItemStack(Material.AIR); Integer slot = 0; for (String s : mapp) { if(s != null && s.length() > 2) { try { Map map = gson.fromJson(s, new TypeToken>(){}.getType()); it = ItemStack.deserialize(map); } catch (Exception e) { List mappp = Arrays.asList(s.split("@#SLOT#")); for (String ss : mappp) { try { if (ss != null && ss.length() > 2) { NBTItem nbti = new NBTItem(it); String strrr = ss; if (strrr.length() > 2) { for (String strr : strrr.split(",")) { if (!strr.equals("{") && !strr.equals("}")) { strr = strr.replace("{", ""); strr = strr.replace("}", ""); String[] nb = strr.split(":"); nbti.setInteger(nb[0], Integer.valueOf(nb[1])); it = nbti.getItem(); } } } } } catch (Exception ee) { slot = Integer.valueOf(ss); } } } } } itemStackDeserilizeResult itt = new itemStackDeserilizeResult(slot, it); return itt; } public static String listItemStackSerelize(List list) { Gson gson = new Gson(); String str = ""; for(String a : list) { str = str + "@#NEW_ITEM#" + a; } return str; } public static ItemStack[] listItemStackDeserilize(String str) { Gson gson = new Gson(); List map = Arrays.asList(str.split("@#NEW_ITEM#")); ArrayList items = new ArrayList(); for(String s : map) { ItemStack itt; if(s != null) { itt = itemStackDeserilize(s).it; if (itt != null && itt.getType() != Material.AIR) { items.add(itt); } } } ItemStack[] it = items.toArray(new ItemStack[0]); return it; } public static String attributes(String str) { String stt = ""; if(str.contains("SPEED")) { stt = "&fSpeed"; return stt; } if(str.contains("STRENGTH")) { stt = "&4Strength"; return stt; } if(str.contains("HEALTH")) { stt = "&cHealth"; return stt; } if(str.contains("DEFENSE")) { stt = "&1Defense"; return stt; } else { stt = "None"; return stt; } } public static NBTItem setAttributes(String str, Integer nb, NBTItem nbti) { log(str); switch (str) { case "SPEED": nbti.setInteger("SPEED", nb); case "STRENGTH": nbti.setInteger("STRENGTH", nb); case "HEALTH": nbti.setInteger("HEALTH", nb); case "DEFENSE": nbti.setInteger("DEFENSE", nb); default: error("Cant set Attributes with an unknown attribute."); } return nbti; } }