Raxen/src/main/java/me/unurled/raxen/manager/entity/ItemManager.java

172 lines
6 KiB
Java

package me.unurled.raxen.manager.entity;
import static me.unurled.raxen.utils.Utils.colorTextComp;
import static me.unurled.raxen.utils.Utils.debug;
import static me.unurled.raxen.utils.Utils.error;
import de.tr7zw.nbtapi.NBTItem;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import lombok.Getter;
import me.unurled.raxen.Raxen;
import me.unurled.raxen.components.items.ItemBuilder;
import me.unurled.raxen.components.items.NBTNames;
import me.unurled.raxen.components.items.custom.Item;
import me.unurled.raxen.config.ItemConfig;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.TextComponent;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.configuration.InvalidConfigurationException;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.jetbrains.annotations.NotNull;
public class ItemManager {
private final Raxen main;
@Getter
private HashMap<String, ItemStack> list = new HashMap<>();
private NBTNames nbt = new NBTNames();
private ItemConfig itemConfig;
private String[] items;
private Map<File, Map<String, ItemBuilder>> map;
public ItemManager(Raxen main) {
this.main = main;
// register();
registerItem();
}
/**
* register manually all item in Raxen
*/
private void registerItem() {
itemConfig = new ItemConfig(main);
map = itemConfig.parseItemConfigs();
final List<String> itemsList = new ArrayList<>();
for (final Map<String, ItemBuilder> subMap : map.values()) {
itemsList.addAll(subMap.keySet());
}
items = itemsList.toArray(new String[0]);
me.unurled.raxen.components.items.custom.List lisst =
new me.unurled.raxen.components.items.custom.List(main);
for (Item items : lisst.getItems()) {
registerItem(items.getItem());
}
}
/**
* Old system to register all items in the /items/ folder
*/
@Deprecated
private void register() {
File folder = new File(main.getDataFolder() + "/Items/");
File[] listFile = folder.listFiles();
for (int i = 0; i < listFile.length; i++) {
if (listFile[i].isFile()) {
FileConfiguration customItem = new YamlConfiguration();
try {
customItem.load(listFile[i]);
} catch (IOException | InvalidConfigurationException e) {
e.printStackTrace();
}
registerItem(customItem);
} else if (listFile[i].isDirectory()) {
for (int a = 0; a < listFile[i].listFiles().length; a++) {
if (listFile[i].listFiles()[a].isFile()) {
FileConfiguration customItem = new YamlConfiguration();
try {
customItem.load(listFile[i].listFiles()[a]);
} catch (
IOException | InvalidConfigurationException e
) {
e.printStackTrace();
}
registerItem(customItem);
} else if (listFile[i].listFiles()[a].isDirectory()) {
error(
(Raxen) Bukkit
.getPluginManager()
.getPlugin("Raxen"),
"Can't use more than 2 folder to get Items.yml"
);
}
}
}
}
}
/**
* Old system to register for a file config an item inside.
*
* @param file
*/
@Deprecated
private void registerItem(@NotNull FileConfiguration file) {
debug(main, file.getString("id"));
ItemStack it = new ItemStack(
Objects.requireNonNull(
Material.getMaterial(
(String) Objects.requireNonNull(file.get("id"))
)
)
);
ItemMeta itm = it.getItemMeta();
itm.displayName(colorTextComp((String) file.get("name")));
Object lore_obj = file.get("lore");
if (lore_obj instanceof List<?> lore) {
List<Component> lores = new ArrayList<>();
for (Object s : lore) {
if (s == null) {
continue;
}
if (s instanceof String) {
TextComponent textComponent = colorTextComp((String) s);
lores.add(textComponent);
}
}
itm.lore(lores);
}
itm.setCustomModelData(
Objects
.requireNonNull(file.getConfigurationSection("Pack"))
.getInt("custom_model_data")
);
it.setItemMeta(itm);
NBTItem nbti = new NBTItem(it);
nbti.setInteger(NBTNames.SPEED, file.getInt("speed"));
nbti.setInteger(NBTNames.HEALTH, file.getInt("health"));
nbti.setInteger(NBTNames.DEFENSE, file.getInt("defense"));
nbti.setInteger(NBTNames.STRENGTH, file.getInt("strength"));
nbti.setString(NBTNames.ID, file.getString("customId"));
nbti.setString(
NBTNames.CUSTOM_ABILITY,
file.getString("custom_ability")
);
nbti.setDouble(NBTNames.DROP_RATE, file.getDouble("drop_rate"));
it = nbti.getItem();
//it = setLoreFromNBT(it); deprecated
list.put(file.getString("customId"), it);
}
/**
* register Item into the custom item list Need that the Itm is already set with custom nbt!
*
* @param itm an ItemStack with already set custom nbt using NBTapi
*/
public void registerItem(ItemStack itm) {
NBTItem it = new NBTItem(itm);
list.put(it.getString("ID"), itm);
}
}