130 lines
5.1 KiB
Java
130 lines
5.1 KiB
Java
package net.t2code.t2codelib.SPIGOT.api.items;
|
|
|
|
import com.mojang.authlib.GameProfile;
|
|
import com.mojang.authlib.properties.Property;
|
|
import net.t2code.t2codelib.SPIGOT.api.minecraftVersion.T2CmcVersion;
|
|
import net.t2code.t2codelib.SPIGOT.system.bstats.Metrics;
|
|
import org.bukkit.Bukkit;
|
|
import org.bukkit.Material;
|
|
import org.bukkit.inventory.Inventory;
|
|
import org.bukkit.inventory.ItemStack;
|
|
import org.bukkit.inventory.meta.ItemMeta;
|
|
import org.bukkit.inventory.meta.SkullMeta;
|
|
import org.bukkit.profile.PlayerProfile;
|
|
import org.bukkit.profile.PlayerTextures;
|
|
|
|
import java.lang.reflect.Field;
|
|
import java.net.MalformedURLException;
|
|
import java.net.URL;
|
|
import java.util.Base64;
|
|
|
|
import org.json.JSONObject;
|
|
|
|
import java.util.List;
|
|
import java.util.UUID;
|
|
|
|
public class T2CitemBuilder {
|
|
public static final boolean getLegacy = T2CmcVersion.isMc1_8() || T2CmcVersion.isMc1_9() || T2CmcVersion.isMc1_10() || T2CmcVersion.isMc1_11() || T2CmcVersion.isMc1_12();
|
|
|
|
public static void fillItem(boolean enable, String item, Integer lines, Inventory inventory) {
|
|
if (!enable) return;
|
|
fillItem(item, lines, inventory);
|
|
}
|
|
|
|
public static void fillItem(String item, Integer lines, Inventory inventory) {
|
|
try {
|
|
ItemStack glass;
|
|
if (getLegacy) {
|
|
glass = new ItemStack(Material.valueOf("STAINED_GLASS_PANE"), 1, Short.parseShort(item));
|
|
} else glass = new ItemStack(Material.valueOf(item.toUpperCase().replace(".", "_")));
|
|
ItemMeta itemMetaglass = glass.getItemMeta();
|
|
assert itemMetaglass != null;
|
|
itemMetaglass.setDisplayName(" ");
|
|
glass.setItemMeta(itemMetaglass);
|
|
glass.setAmount(1);
|
|
for (int i = 0; i < 9 * lines; i++) {
|
|
inventory.setItem(i, glass);
|
|
}
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
|
|
public static ItemStack itemStack(Material material, Integer amount, String displayName, List<String> lore) {
|
|
ItemStack itemStack = new ItemStack(material);
|
|
ItemMeta itemMeta = itemStack.getItemMeta();
|
|
|
|
itemMeta.setDisplayName(displayName);
|
|
itemMeta.setLore(lore);
|
|
itemStack.setItemMeta(itemMeta);
|
|
|
|
itemStack.setAmount(amount);
|
|
return itemStack;
|
|
}
|
|
|
|
public static void setItem(Integer slot, Integer amount, String material, String displayName, List<String> lore, Inventory inventory) {
|
|
ItemStack item;
|
|
if (getLegacy && material.contains(",")) {
|
|
String[] split = material.split(",");
|
|
item = new ItemStack(Material.valueOf(split[0]), 1, Short.parseShort(split[1]));
|
|
} else item = new ItemStack(Material.valueOf(material));
|
|
ItemMeta itemMeta = item.getItemMeta();
|
|
itemMeta.setDisplayName(displayName);
|
|
itemMeta.setLore(lore);
|
|
item.setItemMeta(itemMeta);
|
|
item.setAmount(amount);
|
|
inventory.setItem(slot, item);
|
|
}
|
|
|
|
public static ItemStack base64(String base64Value) {
|
|
if (!T2CmcVersion.minMc1_20() || T2CmcVersion.isNms1_20_R1()) {
|
|
return base64Old(base64Value);
|
|
} else return base64New(base64Value);
|
|
}
|
|
|
|
private static ItemStack base64Old(String base64Value) {
|
|
ItemStack itemStack = new ItemStack(T2CitemVersion.getHead());
|
|
SkullMeta itemMeta = (SkullMeta) itemStack.getItemMeta();
|
|
|
|
GameProfile profile = new GameProfile(UUID.randomUUID(), "");
|
|
profile.getProperties().put("textures", new Property("textures", base64Value));
|
|
Field profileField;
|
|
try {
|
|
profileField = itemMeta.getClass().getDeclaredField("profile");
|
|
profileField.setAccessible(true);
|
|
profileField.set(itemMeta, profile);
|
|
} catch (IllegalArgumentException | IllegalAccessException | NoSuchFieldException | SecurityException e) {
|
|
e.printStackTrace();
|
|
}
|
|
|
|
itemStack.setItemMeta(itemMeta);
|
|
return itemStack;
|
|
}
|
|
|
|
private static ItemStack base64New(String base64Value) {
|
|
ItemStack itemStack = new ItemStack(T2CitemVersion.getHead());
|
|
SkullMeta itemMeta = (SkullMeta) itemStack.getItemMeta();
|
|
|
|
PlayerProfile profile = Bukkit.createPlayerProfile(UUID.randomUUID());
|
|
PlayerTextures textures = profile.getTextures();
|
|
try {
|
|
textures.setSkin(new URL(base64Convert(base64Value)));
|
|
} catch (MalformedURLException e) {
|
|
throw new RuntimeException(e);
|
|
}
|
|
profile.setTextures(textures);
|
|
itemMeta.setOwnerProfile(profile);
|
|
itemStack.setItemMeta(itemMeta);
|
|
return itemStack;
|
|
}
|
|
|
|
private static String base64Convert(String base64Value) {
|
|
String jsonString = new String(Base64.getDecoder().decode(base64Value));
|
|
JSONObject obj = new JSONObject(jsonString);
|
|
String output = obj.getJSONObject("textures").getJSONObject("SKIN").getString("url");
|
|
if (output.startsWith("http://textures.minecraft.net/texture/")){
|
|
return output;
|
|
} else return "http://textures.minecraft.net/texture/d5d20330da59c207d78352838e91a48ea1e42b45a9893226144b251fe9b9d535";
|
|
}
|
|
}
|