Skip to content

Instantly share code, notes, and snippets.

@Yuhtin
Last active January 11, 2022 20:41
Show Gist options
  • Save Yuhtin/87ff9bba1208531a5b2498debf7cf7a9 to your computer and use it in GitHub Desktop.
Save Yuhtin/87ff9bba1208531a5b2498debf7cf7a9 to your computer and use it in GitHub Desktop.
Parse ConfigurationSection as ItemStack
# Example of item in config
# head item example
example-1:
displayName: "&cCavaleiro Vulcanico"
head: "ebadbc4099e5b6b4a7d0865e88a18b61b263db7a756f2724e77b43e200c91018"
# Supports player's name
#head: "Yuhtin"
glow: false
hideFlags: true
description:
- '&7Boss legal'
# material based item example
example-2:
displayName: "&fKey &3Rara &ax15"
material: TRIPWIRE_HOOK
data: 0
description:
- '&7Keys legais'
- '&7Linha 2'
# unbreakable pickaxe
example-3:
displayName: "&aSUPER PICKAXE"
material: DIAMOND_PICKAXE
data: 0
unbreakable: true
description:
- '&aCustom pickaxe'
import com.mojang.authlib.GameProfile;
import com.mojang.authlib.properties.Property;
import org.bukkit.Color;
import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.inventory.meta.LeatherArmorMeta;
import org.bukkit.inventory.meta.SkullMeta;
import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.Base64;
import java.util.List;
import java.util.UUID;
import java.util.function.Consumer;
public class ItemBuilder {
private static final ItemStack SKULL_ITEM = TypeUtil.convertFromLegacy("SKULL_ITEM", 3);
private final ItemStack item;
private ItemBuilder(ItemStack item) {
this.item = item;
}
public static ItemBuilder of(ItemStack item) {
return new ItemBuilder(item);
}
public static ItemBuilder of(Material type) {
return new ItemBuilder(new ItemStack(type));
}
public static ItemBuilder of(Material type, int data) {
return new ItemBuilder(new ItemStack(type, 1, (short) data));
}
public static ItemBuilder of(String url) {
ItemStack item = SKULL_ITEM.clone();
SkullMeta meta = (SkullMeta) item.getItemMeta();
if (!url.contains("http://textures.minecraft.net/texture/")) meta.setOwner(url);
else {
Base64.Encoder encoder = Base64.getEncoder();
String textureData = String.format("{textures:{SKIN:{url:\"%s\"}}}", url);
byte[] encodedTextureData = encoder.encode(textureData.getBytes());
GameProfile profile = new GameProfile(UUID.randomUUID(), null);
profile.getProperties().put("textures", new Property("textures", new String(encodedTextureData)));
Field profileField;
try {
profileField = meta.getClass().getDeclaredField("profile");
profileField.setAccessible(true);
profileField.set(meta, profile);
} catch (Exception exception) {
exception.printStackTrace();
meta.setOwner("Yuhtin");
}
}
item.setItemMeta(meta);
return new ItemBuilder(item);
}
public static ItemBuilder of(Material type, Color color) {
ItemStack item = new ItemStack(type);
LeatherArmorMeta meta = (LeatherArmorMeta) item.getItemMeta();
meta.setColor(color);
item.setItemMeta(meta);
return new ItemBuilder(item);
}
public ItemBuilder customize(Consumer<ItemMeta> consumer) {
ItemMeta itemMeta = item.getItemMeta();
consumer.accept(itemMeta);
item.setItemMeta(itemMeta);
return this;
}
public ItemBuilder name(String name) {
return customize(it -> it.setDisplayName(ColorUtil.colored(name)));
}
public ItemBuilder lore(String... lore) {
return customize(it -> it.setLore(Arrays.asList(ColorUtil.colored(lore))));
}
public ItemBuilder lore(List<String> lore) {
return customize(it -> it.setLore(ColorUtil.colored(lore)));
}
public ItemStack fabric() {
return item;
}
}
import com.yuhtin.minecraft.steelbosses.api.item.ItemBuilder;
import com.yuhtin.minecraft.steelbosses.utils.ColorUtils;
import org.bukkit.Material;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.inventory.ItemFlag;
import org.bukkit.inventory.ItemStack;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;
/**
* @author Yuhtin
* Github: https://github.com/Yuhtin
*/
public class ItemParser {
public List<ItemStack> parseItemCollection(ConfigurationSection section) {
List<ItemStack> items = new ArrayList<>();
for (String key : section.getKeys(false)) {
items.add(
parseItemSection(section.getConfigurationSection(key))
);
}
return items;
}
public ItemStack parseItemSection(ConfigurationSection section) {
try {
ItemBuilder itemBuilder;
if (section.contains("head")) itemBuilder = new ItemBuilder(section.getString("head"));
else {
String material = section.getString("material");
itemBuilder = new ItemBuilder(
Material.valueOf(material),
1,
section.contains("data") ? (short) section.getInt("data") : 0
);
}
if (section.contains("quantity")) itemBuilder.setAmount(section.getInt("quantity"));
if (section.contains("glow") && section.getBoolean("glow")) itemBuilder.glow();
if (section.contains("unbreakable") && section.getBoolean("unbreakable")) itemBuilder.setUnbreakable(true);
if (section.contains("hideFlags") && section.getBoolean("hideFlags")) itemBuilder.addItemFlags(ItemFlag.values());
if (section.contains("displayName")) itemBuilder.name(ColorUtils.colored(section.getString("displayName")));
if (section.contains("description")) {
final List<String> lore = new ArrayList<>();
for (String description : section.getStringList("description")) {
lore.add(ColorUtils.colored(description));
}
itemBuilder.lore(lore);
}
if (section.contains("enchantments")) {
for (String enchantments : section.getStringList("enchantments")) {
String[] splited = enchantments.split(":");
int id = Integer.parseInt(splited[0]);
int level = Integer.parseInt(splited[1]);
itemBuilder.addEnchantment(Enchantment.getById(id), level, true);
}
}
return itemBuilder.result();
} catch (Throwable throwable) {
throwable.printStackTrace();
return null;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment