Raxen/src/main/java/gq/unurled/raxen/utils/Utils.java

252 lines
8.2 KiB
Java
Raw Normal View History

2021-11-27 10:49:11 +00:00
package gq.unurled.raxen.utils;
2021-11-20 10:33:03 +00:00
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import de.tr7zw.changeme.nbtapi.NBTItem;
2021-11-27 10:49:11 +00:00
import gq.unurled.raxen.Raxen;
2021-11-20 10:33:03 +00:00
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.Inventory;
2021-11-20 10:33:03 +00:00
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 final Logger logger = Raxen.getPluginLogger();
2021-11-20 10:33:03 +00:00
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<Component> 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, String name,int amount, List<Component> lore) {
2021-11-20 10:33:03 +00:00
if(amount == 0) {
amount = 1;
}
item.setAmount(amount);
ItemMeta itemMeta = item.getItemMeta();
if (name != null) {
itemMeta.displayName(Component.text(name));
}
2021-11-20 10:33:03 +00:00
itemMeta.lore(lore);
item.setItemMeta(itemMeta);
return item;
}
public static ItemStack closeItem () {
return createItem(Material.BARRIER, 1, false, false, color("&cCLOSE"));
}
public static ItemStack greyPane() {
return createItem(Material.GRAY_STAINED_GLASS_PANE, 1, false, true, "");
}
public static Inventory fillGreyPane(Inventory inv) {
Integer in = -1;
for (ItemStack it : inv) {
in++;
if (it == null || it.getType() == Material.AIR) {
inv.setItem(in, greyPane());
}
}
return inv;
}
2021-11-20 10:33:03 +00:00
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<Map>(){}.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<String> 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<String, Object> map = gson.fromJson(s, new TypeToken<Map<String, Object>>(){}.getType());
it = ItemStack.deserialize(map);
} catch (Exception e) {
List<String> 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<String> 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<String> map = Arrays.asList(str.split("@#NEW_ITEM#"));
ArrayList<ItemStack> items = new ArrayList<ItemStack>();
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;
}
}