Raxen/src/main/java/gq/unurled/raxen/utils/Items.java
unurled ce3c1d547c 0.4.9
remade attribute system, made everything work
2022-04-05 18:07:37 +02:00

309 lines
11 KiB
Java

package gq.unurled.raxen.utils;
import com.google.gson.Gson;
import de.tr7zw.nbtapi.NBTItem;
import gq.unurled.raxen.Raxen;
import gq.unurled.raxen.components.items.NBT;
import net.kyori.adventure.text.Component;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemFlag;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.PlayerInventory;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.util.io.BukkitObjectInputStream;
import org.bukkit.util.io.BukkitObjectOutputStream;
import org.jetbrains.annotations.NotNull;
import org.yaml.snakeyaml.external.biz.base64Coder.Base64Coder;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.*;
import static gq.unurled.raxen.utils.Utils.*;
public class Items {
public static @NotNull 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(colorComp(name));
}
if(lore != null) {
List<Component> list = new ArrayList<>();
for(String string : lore) {
list.add(colorComp(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 @NotNull ItemStack editItem(ItemStack item, String name, int amount, List<Component> lore) {
if(amount == 0) {
amount = 1;
}
item.setAmount(amount);
ItemMeta itemMeta = item.getItemMeta();
if (name != null) {
itemMeta.displayName(Component.text(name));
}
itemMeta.lore(lore);
item.setItemMeta(itemMeta);
return item;
}
public static @NotNull ItemStack closeItem () {
return createItem(Material.BARRIER, 1, false, false, colorString("<red>CLOSE"));
}
public static @NotNull ItemStack greyPane() {
return createItem(Material.GRAY_STAINED_GLASS_PANE, 1, false, true, "");
}
public static @NotNull String itemStackSerilize(ItemStack it, @NotNull Integer slot) {
String str = "";
str = itemTo64(it);
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 class itemStackListDeserilize {
HashMap<Integer, ItemStack> inv;
public itemStackListDeserilize(HashMap<Integer, ItemStack> inv) {
this.inv = inv;
}
}
public static @NotNull itemStackDeserilizeResult itemStackDeserilize(@NotNull String str) {
List<String> mapp = Arrays.asList(str.split("@#SLOT#"));
Gson gson = new Gson();
ItemStack it = new ItemStack(Material.AIR);
Integer slot = 0;
debug("full item " + str);
for (String s : mapp) {
if(s != null) {
try {
it = itemFrom64(s);
} catch (Exception e) {
if (!s.equals("")) {
debug("slot " + s);
slot = Integer.valueOf(s);
}
}
}
}
itemStackDeserilizeResult itt = new itemStackDeserilizeResult(slot, it);
return itt;
}
public static String listItemStackSerelize(@NotNull List<String> list) {
Gson gson = new Gson();
String str = "";
for(String a : list) {
str = str + "@#NEW_ITEM#" + a;
}
return str;
}
public static @NotNull HashMap<Integer, ItemStack> listItemStackDeserilize(@NotNull String str) {
Gson gson = new Gson();
List<String> map = Arrays.asList(str.split("@#NEW_ITEM#"));
HashMap<Integer, ItemStack> inv = new HashMap<>();
ArrayList<ItemStack> items = new ArrayList<ItemStack>();
for(String s : map) {
ItemStack itt;
Integer slot;
if(s != null) {
itemStackDeserilizeResult itm = itemStackDeserilize(s);
itt = itm.it;
if (itt != null && itt.getType() != Material.AIR) {
items.add(itt);
}
slot = itm.slot;
inv.put(slot, itt);
}
}
ItemStack[] it = items.toArray(new ItemStack[0]);
return inv;
}
public static Inventory setItemsToInventory(@NotNull HashMap<Integer, ItemStack> list, Inventory inv) {
for (Map.Entry<Integer, ItemStack> entry : list.entrySet()) {
inv.setItem(entry.getKey(), entry.getValue());
}
return inv;
}
public static PlayerInventory setItemsToInventory(@NotNull HashMap<Integer, ItemStack> list, PlayerInventory inv) {
for (Map.Entry<Integer, ItemStack> entry : list.entrySet()) {
if (entry.getKey() == 40) {
inv.setHelmet(entry.getValue());
} else if (entry.getKey() == 39) {
inv.setChestplate(entry.getValue());
} else if (entry.getKey() == 38) {
inv.setLeggings(entry.getValue());
} else if (entry.getKey() == 37) {
inv.setBoots(entry.getValue());
} else if (entry.getKey() == 41) {
inv.setItemInOffHand(entry.getValue());
}
else {
inv.setItem(entry.getKey(), entry.getValue());
}
}
return inv;
}
private static @NotNull String itemTo64(ItemStack stack) throws IllegalStateException {
try {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
BukkitObjectOutputStream dataOutput = new BukkitObjectOutputStream(outputStream);
dataOutput.writeObject(stack);
// Serialize that array
dataOutput.close();
return Base64Coder.encodeLines(outputStream.toByteArray());
}
catch (Exception e) {
throw new IllegalStateException("Unable to save item stack.", e);
}
}
private static ItemStack itemFrom64(String data) throws IOException {
try {
ByteArrayInputStream inputStream = new ByteArrayInputStream(Base64Coder.decodeLines(data));
BukkitObjectInputStream dataInput = new BukkitObjectInputStream(inputStream);
try {
return (ItemStack) dataInput.readObject();
} finally {
dataInput.close();
}
}
catch (ClassNotFoundException e) {
throw new IOException("Unable to decode class type.", e);
}
}
public static @NotNull String attributes(@NotNull String str) {
String stt = "";
if(str.contains("SPEED")) {
stt = "<white>Speed";
return stt;
}
if(str.contains("STRENGTH")) {
stt = "<dark_red>Strength";
return stt;
}
if(str.contains("HEALTH")) {
stt = "<red>Health";
return stt;
}
if(str.contains("DEFENSE")) {
stt = "<dark_blue>Defense";
return stt;
}
else {
stt = "None";
return stt;
}
}
public static NBTItem setAttributes(Raxen main, @NotNull String str, Integer nb, NBTItem nbti) {
switch (str) {
case "SPEED":
nbti.setInteger(NBT.SPEED, nb);
case "STRENGTH":
nbti.setInteger(NBT.STRENGTH, nb);
case "HEALTH":
nbti.setInteger(NBT.HEALTH, nb);
case "DEFENSE":
nbti.setInteger(NBT.DEFENSE, nb);
case "LUCK":
nbti.setInteger(NBT.LUCK, nb);
case "MANA":
nbti.setInteger(NBT.MANA, nb);
default:
error((Raxen) Bukkit.getPluginManager().getPlugin("Raxen"), "Cant set Attributes with an unknown attribute.");
}
return nbti;
}
public static @NotNull ItemStack setLoreFromNBT(@NotNull ItemStack it) {
ItemMeta itm = it.getItemMeta();
List<Component> lore = new ArrayList<>();
for (Component cp : itm.lore()) {
lore.add(cp);
}
NBTItem nbti = new NBTItem(it);
if(nbti.hasKey(NBT.SPEED)) {
if(lore.size() > 2) {
lore.add(2, colorComp(attributes("SPEED")).append(Component.text(nbti.getInteger(NBT.SPEED))));
} else {
lore.add(colorComp(attributes("SPEED")).append(Component.text(nbti.getInteger(NBT.SPEED))));
}
}
if(nbti.hasKey(NBT.HEALTH)) {
if(lore.size() > 1) {
lore.add(1, colorComp(attributes("HEALTH")).append(Component.text(nbti.getInteger(NBT.HEALTH))));
} else {
lore.add(colorComp(attributes("HEALTH")).append(Component.text(nbti.getInteger(NBT.HEALTH))));
}
}
if(nbti.hasKey(NBT.DEFENSE)) {
if(lore.size() > 3) {
lore.add(3, colorComp(attributes("DEFENSE")).append(Component.text(nbti.getInteger(NBT.DEFENSE))));
} else {
lore.add(colorComp(attributes("DEFENSE")).append(Component.text(nbti.getInteger(NBT.DEFENSE))));
}
}
if(nbti.hasKey(NBT.STRENGTH)) {
if(lore.size() > 3) {
lore.add(3, colorComp(attributes("STRENGTH")).append(Component.text(nbti.getInteger(NBT.STRENGTH))));
} else {
lore.add(colorComp(attributes("STRENGTH")).append(Component.text(nbti.getInteger(NBT.STRENGTH))));
}
}
if(nbti.hasKey(NBT.MANA)) {
if(lore.size() > 3) {
lore.add(3, colorComp(attributes("MANA")).append(Component.text(nbti.getInteger(NBT.MANA))));
} else {
lore.add(colorComp(attributes("MANA")).append(Component.text(nbti.getInteger(NBT.MANA))));
}
}
if(nbti.hasKey(NBT.LUCK)) {
if(lore.size() > 3) {
lore.add(3, colorComp(attributes("LUCK")).append(Component.text(nbti.getInteger(NBT.LUCK))));
} else {
lore.add(colorComp(attributes("LUCK")).append(Component.text(nbti.getInteger(NBT.LUCK))));
}
}
itm.lore(lore);
it.setItemMeta(itm);
return it;
}
}