package net.crankedpvp.cratekeys; import com.google.common.collect.Lists; import de.tr7zw.nbtapi.NBTCompound; import de.tr7zw.nbtapi.NBTItem; import net.crankedpvp.crankedutils.utils.PlayerUtils; import net.crankedpvp.crankedutils.utils.items.ItemUtils; import net.crankedpvp.cratekeys.data.CrateData; import net.crankedpvp.cratekeys.enums.CrateKeyType; import net.crankedpvp.cratekeys.items.CrateKeyItem; import net.crankedpvp.cratekeys.managers.CrateDataManager; import net.crankedpvp.lootfilter.LootFilterAPI; import net.crankedpvp.loottable.LootTableAPI; import net.crankedpvp.loottable.data.LootTable; import net.crankedpvp.loottable.struct.LootTableType; import org.bukkit.Bukkit; import org.bukkit.Location; import org.bukkit.Material; import org.bukkit.World; import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; import java.util.List; import java.util.Map; public class CrankedCrateKeysAPI { public static void addCrate(CrateKeyType type, Location location) { FileConfiguration file = CrankedCrateKeysPlugin.getInstance().getConfig(); List crates = file.contains("crates") ? file.getStringList("crates") : Lists.newArrayList(); crates.add(type.name() + ":" + toStringLocation(location)); file.set("crates", crates); CrankedCrateKeysPlugin.getInstance().saveConfig(); CrankedCrateKeysPlugin.getInstance().reloadConfig(); } public static void givePlayerKeys(Player player, CrateKeyType type, int amount) { while (amount != 0) { PlayerUtils.giveOrDropProtectedItem(player, CrateKeyItem.getItem(type), 60); amount--; } } public static void giveCrateKeyLoot(Player player, LootTableType type) { LootFilterAPI.giveLootTableItem(player, type, LootTableAPI.getRandomLootItem(type)); /* ItemStack item = LootTableAPI.getRandomLootItem(type); //getRandomItem(CrankedCrateKeysPlugin.getInstance().getCrateLootManager().getCachedCrateLoot().get(type)); PlayerUtils.giveOrDropProtectedItem(player, item, 60); */ } public static Double getTotalPercentageOfItems(Map items) { double totalPercentage = 0.0D; for (Map.Entry entry : items.entrySet()) totalPercentage += entry.getValue(); return totalPercentage; } /* public static ItemStack getRandomItem(Map items) { double totalPercentage = getTotalPercentageOfItems(items); double find = ThreadLocalRandom.current().nextDouble(0.0D, totalPercentage); double total = 0.0D; for (Map.Entry entry : items.entrySet()) { double value = entry.getValue(); if (value + total >= find) return entry.getKey(); total += value; } return null; } */ public static boolean isCrateKey(ItemStack item) { if (ItemUtils.isAirOrNull(item)) return false; NBTItem wrapper = new NBTItem(item); if (!wrapper.hasKey("cosmicData")) return false; NBTCompound tag = wrapper.getCompound("cosmicData"); if (tag == null) return false; return tag.hasKey("crate_type"); } public static void removeCrate(Location location) { FileConfiguration file = CrankedCrateKeysPlugin.getInstance().getConfig(); String loc = toStringLocation(location); List crates = file.contains("crates") ? file.getStringList("crates") : Lists.newArrayList(); for (int i = 0; i < crates.size(); i++) { if (crates.get(i).split(":")[1].equals(loc)) { crates.remove(i); file.set("crates", crates); CrankedCrateKeysPlugin.getInstance().saveConfig(); CrankedCrateKeysPlugin.getInstance().reloadConfig(); } } } public static void clearCrates() { CrateDataManager.clearCrates(); } public static void deleteCrates() { FileConfiguration file = CrankedCrateKeysPlugin.getInstance().getConfig(); file.set("crates", null); CrankedCrateKeysPlugin.getInstance().saveConfig(); CrankedCrateKeysPlugin.getInstance().reloadConfig(); } public static List getCrates() { List crates = Lists.newArrayList(); FileConfiguration file = CrankedCrateKeysPlugin.getInstance().getConfig(); List list = file.contains("crates") ? file.getStringList("crates") : Lists.newArrayList(); for (String data : list) { String[] split = data.split(":"); crates.add(new CrateData(CrateKeyType.valueOf(split[0]), fromStringLocation(split[1]))); } return crates; } public static CrateData findCrate(Location location) { List crates = CrateDataManager.getCrates(); for (CrateData data : crates) { if (data.getLocation().getX() == location.getX() && data.getLocation().getY() == location.getY() && data.getLocation().getZ() == location.getZ() && data.getLocation().getWorld().getName().equals(location.getWorld().getName())) return data; } return null; } public static String toStringLocation(Location location) { double x = location.getX(); double y = location.getY(); double z = location.getZ(); float yaw = location.getYaw(); float pitch = location.getPitch(); String world = location.getWorld().getName(); String result = world + " " + x + " " + y + " " + z + " " + yaw + " " + pitch; return result; } public static Location fromStringLocation(String location) { String[] list = location.split(" "); World world = Bukkit.getWorld(list[0]); double x = Double.parseDouble(list[1]); double y = Double.parseDouble(list[2]); double z = Double.parseDouble(list[3]); float yaw = Float.parseFloat(list[4]); float pitch = Float.parseFloat(list[5]); Location l = new Location(world, x, y, z); l.setPitch(pitch); l.setYaw(yaw); return l; } /* public static void setItems(CrateType type, Map items) { String key = type.name().toLowerCase() + ".items"; List list = Lists.newArrayList(); for (Map.Entry entry : items.entrySet()) { list.add(ItemStackSerializer.serialize(entry.getKey()) + ":" + entry.getValue()); } CrankedCrateKeysPlugin.getInstance().getConfig().set(key, list); CrankedCrateKeysPlugin.getInstance().saveConfig(); CrankedCrateKeysPlugin.getInstance().reloadConfig(); } */ /* public static Map getItems(CrateType type) { String key = type.name().toLowerCase() + ".items"; if (!CrankedCrateKeysPlugin.getInstance().getConfig().contains(key)) return Maps.newLinkedHashMap(); Map map = Maps.newLinkedHashMap(); List items = CrankedCrateKeysPlugin.getInstance().getConfig().getStringList(key); for (String item : items) { String[] split = item.split(":"); map.put(ItemStackSerializer.deserialize(split[0]), Double.valueOf(split[1])); } return map; } */ }