Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package games.coob.skywars.model;
- import games.coob.skywars.util.Constants;
- import lombok.AccessLevel;
- import lombok.Getter;
- import lombok.NonNull;
- import lombok.RequiredArgsConstructor;
- import org.bukkit.Material;
- import org.bukkit.entity.Player;
- import org.bukkit.inventory.ItemStack;
- import org.bukkit.inventory.PlayerInventory;
- import org.bukkit.potion.PotionEffect;
- import org.bukkit.potion.PotionEffectType;
- import org.mineacademy.fo.Common;
- import org.mineacademy.fo.FileUtil;
- import org.mineacademy.fo.PlayerUtil;
- import org.mineacademy.fo.Valid;
- import org.mineacademy.fo.collection.SerializedMap;
- import org.mineacademy.fo.model.ConfigSerializable;
- import org.mineacademy.fo.remain.CompMaterial;
- import org.mineacademy.fo.settings.YamlConfig;
- import javax.annotation.Nullable;
- import java.io.File;
- import java.util.ArrayList;
- import java.util.Collections;
- import java.util.List;
- import java.util.Map;
- import java.util.Map.Entry;
- public final class SkyWarsKit extends YamlConfig {
- /**
- * The list of loaded kits
- */
- private static final List<SkyWarsKit> loadedKits = new ArrayList<>();
- /**
- * The kit name
- */
- @Getter
- private final String name;
- /**
- * The icon to display in the menu for this kit
- */
- private ItemStack icon;
- /**
- * The permission to use this kit
- */
- @Getter
- private String permission;
- /**
- * The rarity of this kit
- */
- @Getter
- private String rarity;
- /**
- * The tiers that players can upgrade into
- */
- private List<SkyWarsKitTier> tiers;
- /**
- * Create a new arena kit by the name
- *
- * @param name
- */
- private SkyWarsKit(final String name) {
- this.name = name;
- loadConfiguration(NO_DEFAULT, "kits/" + name + ".yml");
- }
- /**
- * @see YamlConfig#onLoadFinish()
- */
- @Override
- protected void onLoadFinish() {
- this.icon = get("Icon", ItemStack.class);
- this.tiers = getList("Tiers", SkyWarsKitTier.class, this);
- this.permission = getString("Permission");
- this.rarity = getString("Rarity", "&fCOMMON");
- }
- /**
- * Assign this kit to the given player
- *
- * @param player
- */
- public void assignTo(final Player player) {
- final SkyWarsPlayer cache = SkyWarsPlayer.getCache(player);
- Valid.checkBoolean(cache.hasArena() && cache.getMode() == ArenaJoinMode.PLAYING, "Kits may only be selected when playing an arena");
- final Arena arena = cache.getArena();
- Valid.checkBoolean(canAssign(player), "Player " + player.getName() + " may not be assigned kit " + name);
- Valid.checkBoolean(arena.hasKits(), "SkyWars arena " + arena.getName() + " does not support kits!");
- int tier = cache.getTier(this);
- SkyWarsKitTier kitTier = getTier(tier);
- // Search for lower tiers if the one we want is not loaded/setup
- while (kitTier == null && tier > 1) {
- tier--;
- kitTier = getTier(tier);
- }
- Valid.checkNotNull(kitTier, "Could not find tier of kit " + name + " for player " + player.getName());
- kitTier.applyFor(player, true);
- cache.setSkyWarsKit(this);
- }
- public static void assignOnStart(final Player player, final SkyWarsKit skyWarsKit) {
- final SkyWarsPlayer cache = SkyWarsPlayer.getCache(player);
- if (skyWarsKit != null && cache.getSelectedKit().equals(skyWarsKit.getName()))
- skyWarsKit.assignTo(player);
- }
- /**
- * Return if the player may get this kit
- *
- * @param player
- * @return
- */
- public boolean canAssign(final Player player) {
- return permission == null || PlayerUtil.hasPerm(player, permission);
- //if (permission != null && !PlayerUtil.hasPerm(player, permission))
- //return false;
- }
- /**
- * Set the kit icon
- *
- * @param icon the icon to set
- */
- public void setIcon(final ItemStack icon) {
- this.icon = icon;
- save();
- }
- /**
- * Return the icon, or the default one if not set
- *
- * @return
- */
- public ItemStack getIcon() {
- return icon != null && icon.getType() != Material.AIR ? icon : new ItemStack(CompMaterial.IRON_SWORD.getMaterial());
- }
- /**
- * Set the permission to use this kit, set to null to allow everyone
- *
- * @param permission the permission to set
- */
- public void setPermission(final String permission) {
- this.permission = permission;
- save();
- }
- /**
- * Set the rarity of this kit
- */
- public void setRarity(final String rarity) {
- this.rarity = rarity;
- save();
- }
- /**
- * Return arena tier from the given tier number
- *
- * @param tier
- * @return
- */
- @Nullable
- public SkyWarsKitTier getTier(final int tier) {
- return hasTier(tier) ? tiers.get(tier - 1) : null;
- }
- /**
- * Return if the given tier is contained within this kit
- *
- * @param tier
- * @return
- */
- public boolean hasTier(final int tier) {
- return tiers.size() >= tier;
- }
- /**
- * Add a new kit tier
- *
- * @return
- */
- public SkyWarsKitTier addTier() {
- final SkyWarsKitTier tier = new SkyWarsKitTier(this);
- tiers.add(tier);
- save();
- return tiers.get(tiers.size() - 1);
- }
- /**
- * Removes a tier from this kit
- *
- * @param tier the tier level
- */
- public void removeTier(final int tier) {
- Valid.checkBoolean(getTiers() >= tier, "Cannot remove tier " + tier + " because the kit " + name + " only has " + tiers.size() + " tiers");
- tiers.remove(tier - 1);
- save();
- }
- /**
- * Return how many tiers this kit has
- *
- * @return
- */
- public int getTiers() {
- return tiers.size();
- }
- @Override
- public void save() {
- final SerializedMap map = SerializedMap.ofArray();
- // Enable null values
- map.asMap().put("Tiers", tiers);
- map.asMap().put("Icon", icon);
- map.asMap().put("Permission", permission);
- map.asMap().put("Rarity", rarity);
- for (final Map.Entry<String, Object> entry : map.entrySet())
- setNoSave(entry.getKey(), entry.getValue());
- super.save();
- }
- @Override
- public boolean equals(final Object object) {
- return object instanceof SkyWarsKit && ((SkyWarsKit) object).getName().equals(this.name);
- }
- // ------–------–------–------–------–------–------–------–------–------–------–------–
- // Static
- // ------–------–------–------–------–------–------–------–------–------–------–------–
- /**
- * Load all kits in the plugin
- */
- public static void loadKits() {
- loadedKits.clear();
- final File[] kitFiles = FileUtil.getFiles("kits", "yml");
- for (final File kitfile : kitFiles) {
- final String name = FileUtil.getFileName(kitfile);
- loadOrCreateKit(name);
- }
- }
- /**
- * Load or creates a new kit
- *
- * @param name
- * @return
- */
- public static SkyWarsKit loadOrCreateKit(final String name) {
- Valid.checkBoolean(!isKitLoaded(name), "Kit " + name + " is already loaded: " + getKitNames());
- try {
- final SkyWarsKit skyWarsKit = new SkyWarsKit(name);
- loadedKits.add(skyWarsKit);
- Common.log("[+] Loaded kit " + skyWarsKit.getName());
- return skyWarsKit;
- } catch (final Throwable t) {
- Common.throwError(t, "Failed to load kit " + name);
- }
- return null;
- }
- /**
- * Permanently delete a kit
- *
- * @param skyWarsKit
- */
- public static void removeKit(@NonNull final SkyWarsKit skyWarsKit) {
- Valid.checkBoolean(isKitLoaded(skyWarsKit.getName()), "Kit " + skyWarsKit.getName() + " not loaded. Available: " + getKitNames());
- skyWarsKit.delete();
- loadedKits.remove(skyWarsKit);
- }
- /**
- * Return true if the given kit exists
- *
- * @param name
- * @return
- */
- public static boolean isKitLoaded(final String name) {
- return findKit(name) != null;
- }
- /**
- * Find a kit by name
- *
- * @param name
- * @return
- */
- public static SkyWarsKit findKit(@NonNull final String name) {
- for (final SkyWarsKit skyWarsKit : loadedKits)
- if (skyWarsKit.getName().equalsIgnoreCase(name))
- return skyWarsKit;
- return null;
- }
- /**
- * Get all loaded kits
- *
- * @return
- */
- public static List<SkyWarsKit> getKits() {
- return Collections.unmodifiableList(loadedKits);
- }
- /**
- * Get all loaded kit names
- *
- * @return
- */
- public static List<String> getKitNames() {
- return Common.convert(loadedKits, SkyWarsKit::getName);
- }
- // ------–------–------–------–------–------–------–------–------–------–------–------–
- // Kits
- // ------–------–------–------–------–------–------–------–------–------–------–------–
- /**
- * Represents the tier data kit
- */
- @RequiredArgsConstructor(access = AccessLevel.PRIVATE)
- public final static class SkyWarsKitTier implements ConfigSerializable {
- /**
- * The kit in which this tier is set
- */
- private final SkyWarsKit skyWarsKit;
- /**
- * Price to obtain this tier
- */
- @Getter
- private double price;
- /**
- * The inventory content of this tier
- */
- @Getter
- private ItemStack[] content = new ItemStack[36];
- /**
- * The armor content of this tier
- */
- @Getter
- private ItemStack[] armorContent = new ItemStack[4];
- /**
- * The potion effects of this tier name:amplifier
- */
- private SerializedMap potionEffects = new SerializedMap();
- /**
- * Set the price for this tier
- *
- * @param price the price to set
- */
- public void setPrice(final double price) {
- this.price = price;
- skyWarsKit.save();
- }
- /**
- * Set both the content and armor content
- *
- * @param content
- * @param armor
- */
- public void setAllContent(final ItemStack[] content, final ItemStack[] armor) {
- this.content = content;
- this.armorContent = armor;
- skyWarsKit.save();
- }
- /**
- * Set inventory content
- *
- * @param content the content to set
- */
- public void setContent(final ItemStack[] content) {
- this.content = content;
- skyWarsKit.save();
- }
- /**
- * Set armor content
- *
- * @param armorContent the armorContent to set
- */
- public void setArmorContent(final ItemStack[] armorContent) {
- this.armorContent = armorContent;
- skyWarsKit.save();
- }
- /**
- * Get the potion effect or 0 if not set
- *
- * @param type
- * @return
- */
- public int getPotionEffect(final PotionEffectType type) {
- return potionEffects.getInteger(type.getName(), 0);
- }
- /**
- * Return true if the tier is completely empty
- *
- * @return
- */
- public boolean isEmpty() {
- return Valid.isNullOrEmpty(content) && Valid.isNullOrEmpty(armorContent) && potionEffects.isEmpty();
- }
- /**
- * Set potion effects
- */
- public void setPotionEffect(final PotionEffectType type, final int level) {
- final String name = type.getName();
- if (level == 0 && potionEffects.containsKey(name))
- potionEffects.asMap().remove(name);
- else
- potionEffects.override(type.getName(), level);
- skyWarsKit.save();
- }
- /**
- * Apply the tier for the given player
- *
- * @param player
- * @param giveInventory
- */
- public void applyFor(final Player player, final boolean giveInventory) {
- if (giveInventory) {
- final PlayerInventory inventory = player.getInventory();
- inventory.setContents(content);
- inventory.setArmorContents(armorContent);
- }
- for (final Entry<String, Object> effect : potionEffects.asMap().entrySet())
- player.addPotionEffect(new PotionEffect(PotionEffectType.getByName(effect.getKey()), Integer.MAX_VALUE, (int) effect.getValue() - 1), true);
- }
- /**
- * Create a new tier from saved config section
- *
- * @param map
- * @param skyWarsKit
- * @return
- */
- public static SkyWarsKitTier deserialize(final SerializedMap map, final SkyWarsKit skyWarsKit) {
- final SkyWarsKitTier tier = new SkyWarsKitTier(skyWarsKit);
- final List<ItemStack> content = map.getList("Content", ItemStack.class);
- final List<ItemStack> armor = map.getList("Armor_Content", ItemStack.class);
- tier.price = map.getDouble("Price", Constants.Defaults.TIER_UPGRADE_PRICE);
- tier.content = content.toArray(new ItemStack[content.size()]);
- tier.armorContent = armor.toArray(new ItemStack[armor.size()]);
- tier.potionEffects = map.getMap("Potion_Effects");
- return tier;
- }
- /**
- * @see ConfigSerializable#serialize()
- */
- @Override
- public SerializedMap serialize() {
- return SerializedMap.ofArray(
- "Price", price,
- "Content", content,
- "Armor_Content", armorContent,
- "Potion_Effects", potionEffects);
- }
- /**
- * @see Object#toString()
- */
- @Override
- public String toString() {
- return "Tier {content= " + Common.join(content, ", ", (item) -> item.getType().toString()) + "; armor=" + Common.join(armorContent, ", ", (item) -> item.getType().toString()) + "}";
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement