Skocz do zawartości
  • 👋 Witaj na MPCForum!

    Przeglądasz forum jako gość, co oznacza, że wiele świetnych funkcji jest jeszcze przed Tobą! 😎

    • Pełny dostęp do działów i ukrytych treści
    • Możliwość pisania i odpowiadania w tematach
    • System prywatnych wiadomości
    • Zbieranie reputacji i rozwijanie swojego profilu
    • Członkostwo w jednej z największych społeczności graczy

    👉 Dołączenie zajmie Ci mniej niż minutę – a zyskasz znacznie więcej!

    Zarejestruj się teraz

Odczyt itemów z pliku .yml


Lumenowaty

Rekomendowane odpowiedzi

Opublikowano

Cześć,

ostatnio bawiłem się sporo w tworzenie GUI, wczytywanie itemów z pliku, więc stworzyłem, myślę uniwersalną klasę, która odczyta takie rzeczy jak własna nazwę, ilość, ulepszenia, opis i "świecenie się" przedmiotu.

 

Spoiler

/*
  <- SUPPORTED FORMAT ->
  items:
   UniqueSortName:
    material:
    name:
    amount:
    lore:
    enchants:
 */
// import spigot not spigot-api 
import net.minecraft.server.v1_16_R3.NBTTagCompound;
import net.minecraft.server.v1_16_R3.NBTTagList;
//
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.craftbukkit.v1_16_R3.inventory.CraftItemStack;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;

import java.util.*;
import java.util.stream.Collectors;

class ItemStackYamlParser {
    private final FileConfiguration file;
    private final String pathToItems;
    private List<ItemStack> itemStackList;

    public ItemStackYamlParser(final FileConfiguration fileConfiguration, final String pathToItems) {
        this.file = fileConfiguration;
        this.pathToItems = pathToItems;
        this.itemStackList = new ArrayList<>();
    }

    public void parseItems() {
        loadItems();
    }

    public List<ItemStack> getItemStackList() {
        return new ArrayList<>(itemStackList);
    }

    public List<ItemStack> getItemStacksByMaterial(Material material) {
        return itemStackList.stream().filter(itemStack ->
                itemStack.getType().equals(material))
                .collect(Collectors.toList());
    }

    public List<ItemStack> getItemStacksByEnchants(Enchantment enchantment) {
        return itemStackList.stream().filter(itemStack ->
                itemStack.getEnchantments().containsKey(enchantment))
                .collect(Collectors.toList());
    }


    private Set<String> getSection(String path) {
        ConfigurationSection section = file.getConfigurationSection(path);

        Set<String> sectionSet = new HashSet<>();

        if (section != null) {
               sectionSet.addAll(section.getKeys(false));
        } else {
            System.out.println("Section " + path + " is empty.");
        }

        return sectionSet;
    }

    private void loadItems() {
        getItemSection().forEach(key ->
                itemStackList.add(createItem(pathToItems + "." + key)));
    }

    private Set<String> getItemSection() {
        return getSection(pathToItems);
    }

    private ItemStack createItem(String path) {
        ItemStack itemStack = new ItemStack(getMaterial(path), getAmount(path));
        String itemName = getName(path);
        List<String> lore = getLore(path);
        Map<Enchantment, Integer> enchantments = getEnchantments(path);
        ItemMeta itemMeta = itemStack.getItemMeta();


        if (itemName != null) {
            itemMeta.setDisplayName(formatText(itemName));
        }

        itemMeta.setLore(formatText(lore));

        itemStack.setItemMeta(itemMeta);

        itemStack.addUnsafeEnchantments(enchantments);

        setGlow(path, itemStack);

        return itemStack;
    }

    private Material getMaterial(String path) {
        String materialName = file.getString(path + ".material");

        if (materialName == null) throw new NullPointerException("Material cannot be null.");

        Optional<Material> opMaterial = Optional.ofNullable(Material.getMaterial(materialName.toUpperCase()));

        if (! opMaterial.isPresent()) throw new IllegalArgumentException("This material does not exist.  (" + materialName + ").");

        return opMaterial.get();
    }

    private int getAmount(String path) {
        return file.getInt(path + ".amount");
    }

    private String getName(String path) {
        return file.getString(path + ".name");
    }

    private List<String> getLore(String path) {
        return new ArrayList<>(file.getStringList(path + ".lore"));
    }

    private Map<Enchantment, Integer> getEnchantments(String path) {
        Set<String> enchantmentsSection = getEnchantSection(path);

        Map<Enchantment, Integer> enchantments = new HashMap<>();

        enchantmentsSection.forEach(en -> {
            enchantments.put(Enchantment.getByName(en.toUpperCase()),
                    file.getInt(path + ".enchants." + en));
        });

        return enchantments;
    }

    private Set<String> getEnchantSection(String path) {
        return getSection(path + ".enchants");
    }

    private String formatText(String toFormat) {
        return ChatColor.translateAlternateColorCodes('&', toFormat);
    }

    private List<String> formatText(List<String> toFormat) {
        return toFormat.stream()
                .map(this::formatText)
                .collect(Collectors.toList());
    }

    private void setGlow(String path, ItemStack itemStack) {
        if (file.getBoolean(path + ".glow")) {
            addGlow(itemStack);
        }
    }

    private ItemStack addGlow(ItemStack item){
        net.minecraft.server.v1_16_R3.ItemStack nmsStack = CraftItemStack.asNMSCopy(item);
        NBTTagCompound tag = null;
        if (!nmsStack.hasTag()) {
            tag = new NBTTagCompound();
            nmsStack.setTag(tag);
        }
        if (tag == null) tag = nmsStack.getTag();
        NBTTagList ench = new NBTTagList();
        tag.set("ench", ench);
        nmsStack.setTag(tag);
        return CraftItemStack.asCraftMirror(nmsStack);
    }
}

 

 

Link do Gita, jeżeli niezgodny z regulaminem, wybaczcie.

 

https://github.com/LumenowatyDev/utils/blob/main/ItemStackYamlParser.java

 

Jeżeli macie jakieś propozycje, pytania, bądź ta klasa jest zupełnie niepotrzebna nikomu, to piszcie 🙂

Zarchiwizowany

Ten temat przebywa obecnie w archiwum. Dodawanie nowych odpowiedzi zostało zablokowane.

×
×
  • Dodaj nową pozycję...