Hub / src / main / java / com / lifeknight / relaymchub / cosmetics / DefinedLobbyCosmetic.java
DefinedLobbyCosmetic.java
Raw
package com.lifeknight.relaymchub.cosmetics;

import com.lifeknight.relaymchub.Main;
import com.lifeknight.relaymchub.cosmetics.types.PotionApplication;
import com.lifeknight.relaymchub.player.HubPlayer;
import com.lifeknight.relayutils.basic.Miscellaneous;
import com.lifeknight.relayutils.player.cosmetics.Cosmetic;
import com.lifeknight.relayutils.player.cosmetics.CosmeticType;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.event.Listener;
import org.bukkit.inventory.ItemStack;
import org.bukkit.potion.PotionEffectType;

import java.util.ArrayList;
import java.util.List;

public class DefinedLobbyCosmetic implements Listener {
    private static final List<DefinedLobbyCosmetic> COSMETICS = new ArrayList<>();

    private final Cosmetic original;
    protected final List<CosmeticApplication> applications = new ArrayList<>();

    public DefinedLobbyCosmetic(Cosmetic original) {
        this.original = original;

        COSMETICS.add(this);
        Bukkit.getPluginManager().registerEvents(this, Main.instance);
    }

    public boolean onClick(HubPlayer hubPlayer) {
        if (hubPlayer.hasLobbyCosmeticUnlocked(this)) {
            if (hubPlayer.getActiveLobbyCosmetics().contains(this)) {
                this.disable(hubPlayer, true);
            } else {
                this.enable(hubPlayer, true);
            }

            return true;
        } else {
            hubPlayer.sendErrorMessage("You do not have this cosmetic!");
        }

        return false;
    }

    public void enable(HubPlayer hubPlayer, boolean sendMessage) {
        this.doEnable(hubPlayer);
        if (sendMessage) {
            hubPlayer.sendSuccessMessage("You have equipped %s%s.", this.getPrettyName(), ChatColor.GREEN);
        }
    }

    public void doEnable(HubPlayer hubPlayer) {
        for (int i = 0; i < hubPlayer.getActiveLobbyCosmetics().size(); i++) {
            DefinedLobbyCosmetic activeCosmetic = hubPlayer.getActiveLobbyCosmetics().get(i);
            if (this.shouldDisable(activeCosmetic)) {
                activeCosmetic.disable(hubPlayer, true);
            }
        }

        hubPlayer.getActiveLobbyCosmetics().add(this);

        for (CosmeticApplication application : this.applications) {
            application.apply(hubPlayer);
        }
    }

    public boolean shouldDisable(DefinedLobbyCosmetic other) {
        for (CosmeticApplication application : this.applications) {
            for (CosmeticApplication cosmeticApplication : other.applications) {
                if (application.equals(cosmeticApplication)) {
                    return true;
                }
            }
        }

        return false;
    }

    public List<HubPlayer> getNearbyPlayers(HubPlayer hubPlayer, double radius, boolean includeOwner) {
        return Miscellaneous.filter(Miscellaneous.processList(getWorld().getNearbyPlayers(hubPlayer.getLocation(), radius), HubPlayer::getHubPlayer), hubPlayer1 -> {
            if (hubPlayer1.inArena() || hubPlayer1.isHidingOthers() || hubPlayer1.hasParkour()) {
                return false;
            }

            return hubPlayer1 != hubPlayer || includeOwner;
        });
    }

    public List<HubPlayer> getNearbyPlayers(Location location, double radius) {
        if (location == null) {
            return new ArrayList<>();
        }
        return Miscellaneous.filter(Miscellaneous.processList(getWorld().getNearbyPlayers(location, radius), HubPlayer::getHubPlayer), hubPlayer1 -> {
            return !hubPlayer1.inArena() && !hubPlayer1.isHidingOthers() && !hubPlayer1.hasParkour();
        });
    }

    public List<HubPlayer> getNearbyPlayers(HubPlayer hubPlayer, double radius) {
        return this.getNearbyPlayers(hubPlayer, radius, false);
    }

    public void doDisable(HubPlayer hubPlayer) {
        hubPlayer.getActiveLobbyCosmetics().remove(this);

        for (CosmeticApplication application : this.applications) {
            application.remove(hubPlayer);
        }
    }

    public void disable(HubPlayer hubPlayer, boolean sendMessage) {
        this.doDisable(hubPlayer);
        if (sendMessage) {
            hubPlayer.sendSuccessMessage("You have dequipped %s%s.", this.getPrettyName(), ChatColor.GREEN);
        }
    }

    public ItemStack getRepresentative() {
        return this.original.getRepresentative();
    }

    public static List<DefinedLobbyCosmetic> getCosmeticItems(CosmeticType cosmeticType) {
        return Miscellaneous.filter(getCosmetics(), definedLobbyCosmetic -> definedLobbyCosmetic.original.getType() == cosmeticType);
    }

    public ItemStack getItemStack(HubPlayer hubPlayer) {
        return this.original.getItemStack(hubPlayer.hasLobbyCosmeticUnlocked(this), hubPlayer.hasLobbyCosmeticActive(this));
    }

    public void tick(HubPlayer hubPlayer) {

    }

    public Cosmetic getOriginal() {
        return this.original;
    }

    public String getCodeName() {
        return this.original.getCodeName();
    }

    public String getPrettyName() {
        return this.original.getPrettyName();
    }

    public int getCost() {
        return this.original.getCost();
    }

    public List<String> getDescription(boolean withExtraLine) {
        return this.original.getDescription(withExtraLine);
    }

    public String[] getDescriptionArray() {
        return this.getDescription(false).toArray(new String[0]);
    }

    public boolean isPurchasable() {
        return this.original.isPurchasable();
    }

    public World getWorld() {
        return Main.getWorld();
    }

    public DefinedLobbyCosmetic addApplication(CosmeticApplication application) {
        this.applications.add(application);

        return this;
    }

    public DefinedLobbyCosmetic addPotionApplication(PotionApplication potionApplication) {
        return this.addApplication(potionApplication);
    }

    public DefinedLobbyCosmetic addPotionApplication(PotionEffectType type, double seconds, int amplifier) {
        return this.addApplication(new PotionApplication(type, seconds, amplifier));
    }

    public DefinedLobbyCosmetic addPotionApplication(PotionEffectType type, int amplifier) {
        return this.addApplication(new PotionApplication(type, amplifier));
    }

    public DefinedLobbyCosmetic addPotionApplication(PotionEffectType type) {
        return this.addApplication(new PotionApplication(type));
    }

    public static DefinedLobbyCosmetic getCosmetic(String codeName) {
        for (DefinedLobbyCosmetic cosmeticItem : COSMETICS) {
            if (cosmeticItem.getCodeName().equalsIgnoreCase(codeName)) {
                return cosmeticItem;
            }
        }

        return null;
    }

    public static DefinedLobbyCosmetic getCosmetic(Cosmetic cosmetic) {
        for (DefinedLobbyCosmetic cosmeticItem : COSMETICS) {
            if (cosmeticItem.original == cosmetic) {
                return cosmeticItem;
            }
        }

        return null;
    }

    private static List<DefinedLobbyCosmetic> orderedCosmetics;
    public static List<DefinedLobbyCosmetic> getCosmetics() {
        if (orderedCosmetics == null) {
            orderedCosmetics = Miscellaneous.getList(COSMETICS);
            orderedCosmetics.sort((o1, o2) -> {
                if (o1.original.getCost() > o2.original.getCost()) {
                    return 1;
                } else {
                    return o1.original.getCost() < o2.original.getCost() ? -1 : Long.compare(o1.original.getRewardLevel(), o2.original.getRewardLevel());
                }
            });
        }
        return orderedCosmetics;
    }
}