Search in sources :

Example 1 with ArenaJoinEvent

use of com.gmail.stefvanschiedev.buildinggame.api.events.ArenaJoinEvent in project buildinggame by stefvanschie.

the class Arena method join.

/**
 * Joins the given player this arena, with all messages being send and all items given to the player. It'll teleport
 * the player to the {@link #lobby} and will join the first open plot. An ArenaJoinEvent may fire once this method
 * is called.
 *
 * @param player the player to join this arena
 * @since 2.1.0
 */
public void join(final Player player) {
    final YamlConfiguration config = SettingsManager.getInstance().getConfig();
    final YamlConfiguration messages = SettingsManager.getInstance().getMessages();
    // check if everything is set up
    if (!isSetup()) {
        MessageManager.getInstance().send(player, ChatColor.RED + "Your arena isn't setup right, you still need to do this:");
        if (lobby == null)
            MessageManager.getInstance().send(player, ChatColor.RED + " - The lobby of arena " + getName() + "(/bg setlobby " + getName() + ')');
        if (MainSpawnManager.getInstance().getMainSpawn() == null)
            MessageManager.getInstance().send(player, ChatColor.RED + " - The main spawn (/bg setmainspawn)");
        getPlots().stream().filter(plot -> plot.getBoundary() == null).forEach(plot -> MessageManager.getInstance().send(player, ChatColor.RED + " - The boundary of plot " + plot.getId() + " (/bg setbounds " + getName() + ' ' + plot.getId() + ')'));
        getPlots().stream().filter(plot -> plot.getFloor() == null).forEach(plot -> MessageManager.getInstance().send(player, ChatColor.RED + " - The floor of plot " + plot.getId() + " (/bg setfloor " + getName() + ' ' + plot.getId() + ')'));
        return;
    }
    if (ArenaManager.getInstance().getArena(player) != null) {
        MessageManager.getInstance().send(player, ChatColor.RED + "You're already in a game");
        return;
    }
    if (!canJoin()) {
        MessageManager.getInstance().send(player, messages.getStringList("join.in-game"));
        return;
    }
    if (isFull()) {
        MessageManager.getInstance().send(player, ChatColor.RED + "This arena is full");
        return;
    }
    // call event
    ArenaJoinEvent event = new ArenaJoinEvent(this, player);
    Bukkit.getPluginManager().callEvent(event);
    if (event.isCancelled())
        return;
    GamePlayer p = new GamePlayer(player, GamePlayerType.PLAYER);
    Plot plot = null;
    for (Plot pl : getPlots()) {
        if (!pl.isFull()) {
            plot = pl;
            pl.join(p);
            break;
        }
    }
    if (config.getBoolean("scoreboards.main.enable"))
        MainScoreboardManager.getInstance().remove(player);
    boolean enableLobbyScoreboard = config.getBoolean("scoreboards.lobby.enable");
    if (enableLobbyScoreboard && (getState() == GameState.WAITING || getState() == GameState.STARTING)) {
        getLobbyScoreboard(plot).show(player);
    } else {
        getBuildScoreboard(plot).show(player);
    }
    String name = player.getName();
    // add player to red scoreboard of others
    getPlots().stream().filter(pl -> !pl.getGamePlayers().contains(p)).forEach(pl -> {
        getLobbyScoreboard(pl).getRedTeam().addEntry(name);
        getBuildScoreboard(pl).getRedTeam().addEntry(name);
        getVoteScoreboard(pl).getRedTeam().addEntry(name);
        getWinScoreboard(pl).getRedTeam().addEntry(name);
    });
    messages.getStringList("join.message").forEach(message -> MessageManager.getInstance().send(player, message.replace("%players%", getPlayers() + "").replace("%max_players%", getMaxPlayers() + "")));
    getUsedPlots().stream().flatMap(pl -> pl.getGamePlayers().stream()).forEach(gamePlayer -> {
        Player pla = gamePlayer.getPlayer();
        messages.getStringList("join.otherPlayers").forEach(message -> MessageManager.getInstance().send(pla, message.replace("%player%", name).replace("%players%", getPlayers() + "").replace("%max_players%", getMaxPlayers() + "")));
    });
    if (lobby != null && (getState() == GameState.WAITING || getState() == GameState.STARTING)) {
        lobby.teleport(player);
    } else {
        plot.getLocation().teleport(player);
    }
    if (enableLobbyScoreboard && (getState() == GameState.WAITING || getState() == GameState.STARTING)) {
        getUsedPlots().forEach(pl -> pl.getGamePlayers().forEach(gamePlayer -> lobbyScoreboards.get(pl).show(gamePlayer.getPlayer())));
    } else {
        getUsedPlots().forEach(pl -> pl.getGamePlayers().forEach(gamePlayer -> buildScoreboards.get(pl).show(gamePlayer.getPlayer())));
    }
    player.getInventory().clear();
    player.getInventory().setArmorContents(null);
    // fill lives and hunger
    player.setHealth(player.getAttribute(Attribute.GENERIC_MAX_HEALTH).getValue());
    player.setFoodLevel(20);
    if (getState() == GameState.WAITING || getState() == GameState.STARTING) {
        player.setGameMode(GameMode.ADVENTURE);
    } else {
        player.setGameMode(GameMode.CREATIVE);
    }
    // time
    if (config.getBoolean("join.time-change.change"))
        player.setPlayerTime(config.getInt("join.time-change.time"), false);
    // bossbar
    getBossBar().addPlayer(player);
    // hide players from tab list
    if (config.getBoolean("tab-list.adjust"))
        Bukkit.getOnlinePlayers().stream().filter(pl -> !contains(pl)).forEach(pl -> player.hidePlayer(Main.getInstance(), pl));
    // show current joined player to others
    getUsedPlots().stream().flatMap(pl -> pl.getGamePlayers().stream()).forEach(gamePlayer -> gamePlayer.getPlayer().showPlayer(Main.getInstance(), player));
    if (getPlayers() >= minPlayers && !lobbyTimer.isActive() && (getState() == GameState.WAITING || getState() == GameState.STARTING)) {
        lobbyTimer.runTaskTimer(Main.getInstance(), 0L, 20L);
    }
    if (getPlayers() >= getMaxPlayers())
        lobbyTimer.setSeconds(0);
    var arena = this;
    // bukkit runnable because of instant leaving and instant subject opening
    var runnable = new BukkitRunnable() {

        @Override
        public void run() {
            // give team selection
            if (getState() == GameState.WAITING || getState() == GameState.STARTING) {
                if (getMode() == ArenaMode.TEAM) {
                    Material material = SettingsManager.getInstance().getMaterial("team-selection.item.id", Material.BARRIER);
                    player.getInventory().setItem(0, new ItemBuilder(player, material).setDisplayName(MessageManager.translate(messages.getString("team-gui.item.name"), player)).setLore(MessageManager.translate(messages.getStringList("team-gui.item.lores"), player)).setClickEvent(event -> {
                        getTeamSelection().show(player);
                        event.setCancelled(true);
                    }).build());
                }
                // give paper for subject
                if (player.hasPermission("bg.subjectmenu") && config.getBoolean("enable-subject-voting") && getSubjectMenu().getWhen() == SubjectMenu.When.LOBBY) {
                    giveSubjectMenuItem(player);
                    if (getSubjectMenu().opensInstantly()) {
                        getSubjectMenu().show(player);
                    }
                }
                Material material = SettingsManager.getInstance().getMaterial("leave-item.id", Material.BARRIER);
                player.getInventory().setItem(config.getInt("leave-item.slot"), new ItemBuilder(player, material).setDisplayName(MessageManager.translate(messages.getString("leave-item.name"), player)).setClickEvent(event -> {
                    leave(player);
                    event.setCancelled(true);
                }).build());
                player.updateInventory();
            } else if (getState() == GameState.BUILDING) {
                tryGiveOptionsMenu(player);
            }
        }

        /**
         * Returns the team selection and creates one if it doesn't exist yet
         *
         * @return the team selection
         * @see TeamSelection
         * @since 2.1.0
         */
        @NotNull
        private TeamSelection getTeamSelection() {
            if (teamSelection == null)
                teamSelection = new TeamSelection(arena);
            return teamSelection;
        }
    };
    runnable.runTaskLater(Main.getInstance(), 1L);
    SignManager.getInstance().updateJoinSigns(this);
}
Also used : java.util(java.util) ZonedDateTime(java.time.ZonedDateTime) LocalDateTime(java.time.LocalDateTime) Player(org.bukkit.entity.Player) LobbyTimer(com.gmail.stefvanschiedev.buildinggame.timers.LobbyTimer) org.bukkit(org.bukkit) BuildTimer(com.gmail.stefvanschiedev.buildinggame.timers.BuildTimer) ArenaLeaveEvent(com.gmail.stefvanschiedev.buildinggame.api.events.ArenaLeaveEvent) BarColor(org.bukkit.boss.BarColor) com.gmail.stefvanschiedev.buildinggame.utils(com.gmail.stefvanschiedev.buildinggame.utils) Main(com.gmail.stefvanschiedev.buildinggame.Main) Timer(com.gmail.stefvanschiedev.buildinggame.timers.utils.Timer) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) BarStyle(org.bukkit.boss.BarStyle) VoteTimer(com.gmail.stefvanschiedev.buildinggame.timers.VoteTimer) GamePlayerType(com.gmail.stefvanschiedev.buildinggame.utils.gameplayer.GamePlayerType) Attribute(org.bukkit.attribute.Attribute) ArenaStartEvent(com.gmail.stefvanschiedev.buildinggame.api.events.ArenaStartEvent) ArenaStopEvent(com.gmail.stefvanschiedev.buildinggame.api.events.ArenaStopEvent) Entity(org.bukkit.entity.Entity) BukkitRunnable(org.bukkit.scheduler.BukkitRunnable) PotentialLocation(com.gmail.stefvanschiedev.buildinggame.utils.potential.PotentialLocation) GamePlayer(com.gmail.stefvanschiedev.buildinggame.utils.gameplayer.GamePlayer) MessageManager(com.gmail.stefvanschiedev.buildinggame.managers.messages.MessageManager) com.gmail.stefvanschiedev.buildinggame.utils.scoreboards(com.gmail.stefvanschiedev.buildinggame.utils.scoreboards) SettingsManager(com.gmail.stefvanschiedev.buildinggame.managers.files.SettingsManager) SubjectMenu(com.gmail.stefvanschiedev.buildinggame.utils.guis.SubjectMenu) Plot(com.gmail.stefvanschiedev.buildinggame.utils.plot.Plot) Collectors(java.util.stream.Collectors) File(java.io.File) ItemStack(org.bukkit.inventory.ItemStack) TeamSelection(com.gmail.stefvanschiedev.buildinggame.utils.guis.TeamSelection) MainScoreboardManager(com.gmail.stefvanschiedev.buildinggame.managers.scoreboards.MainScoreboardManager) Nullable(org.jetbrains.annotations.Nullable) Contract(org.jetbrains.annotations.Contract) BossBar(org.bukkit.boss.BossBar) Region(com.gmail.stefvanschiedev.buildinggame.utils.region.Region) YamlConfiguration(org.bukkit.configuration.file.YamlConfiguration) DateTimeFormatter(java.time.format.DateTimeFormatter) SignManager(com.gmail.stefvanschiedev.buildinggame.managers.arenas.SignManager) When(com.gmail.stefvanschiedev.buildinggame.utils.guis.SubjectMenu.When) PotentialBlockPosition(com.gmail.stefvanschiedev.buildinggame.utils.potential.PotentialBlockPosition) NotNull(org.jetbrains.annotations.NotNull) ArenaManager(com.gmail.stefvanschiedev.buildinggame.managers.arenas.ArenaManager) MainSpawnManager(com.gmail.stefvanschiedev.buildinggame.managers.mainspawn.MainSpawnManager) ArenaJoinEvent(com.gmail.stefvanschiedev.buildinggame.api.events.ArenaJoinEvent) WinTimer(com.gmail.stefvanschiedev.buildinggame.timers.WinTimer) ArenaJoinEvent(com.gmail.stefvanschiedev.buildinggame.api.events.ArenaJoinEvent) Player(org.bukkit.entity.Player) GamePlayer(com.gmail.stefvanschiedev.buildinggame.utils.gameplayer.GamePlayer) TeamSelection(com.gmail.stefvanschiedev.buildinggame.utils.guis.TeamSelection) Plot(com.gmail.stefvanschiedev.buildinggame.utils.plot.Plot) BukkitRunnable(org.bukkit.scheduler.BukkitRunnable) YamlConfiguration(org.bukkit.configuration.file.YamlConfiguration) GamePlayer(com.gmail.stefvanschiedev.buildinggame.utils.gameplayer.GamePlayer)

Aggregations

Main (com.gmail.stefvanschiedev.buildinggame.Main)1 ArenaJoinEvent (com.gmail.stefvanschiedev.buildinggame.api.events.ArenaJoinEvent)1 ArenaLeaveEvent (com.gmail.stefvanschiedev.buildinggame.api.events.ArenaLeaveEvent)1 ArenaStartEvent (com.gmail.stefvanschiedev.buildinggame.api.events.ArenaStartEvent)1 ArenaStopEvent (com.gmail.stefvanschiedev.buildinggame.api.events.ArenaStopEvent)1 ArenaManager (com.gmail.stefvanschiedev.buildinggame.managers.arenas.ArenaManager)1 SignManager (com.gmail.stefvanschiedev.buildinggame.managers.arenas.SignManager)1 SettingsManager (com.gmail.stefvanschiedev.buildinggame.managers.files.SettingsManager)1 MainSpawnManager (com.gmail.stefvanschiedev.buildinggame.managers.mainspawn.MainSpawnManager)1 MessageManager (com.gmail.stefvanschiedev.buildinggame.managers.messages.MessageManager)1 MainScoreboardManager (com.gmail.stefvanschiedev.buildinggame.managers.scoreboards.MainScoreboardManager)1 BuildTimer (com.gmail.stefvanschiedev.buildinggame.timers.BuildTimer)1 LobbyTimer (com.gmail.stefvanschiedev.buildinggame.timers.LobbyTimer)1 VoteTimer (com.gmail.stefvanschiedev.buildinggame.timers.VoteTimer)1 WinTimer (com.gmail.stefvanschiedev.buildinggame.timers.WinTimer)1 Timer (com.gmail.stefvanschiedev.buildinggame.timers.utils.Timer)1 com.gmail.stefvanschiedev.buildinggame.utils (com.gmail.stefvanschiedev.buildinggame.utils)1 GamePlayer (com.gmail.stefvanschiedev.buildinggame.utils.gameplayer.GamePlayer)1 GamePlayerType (com.gmail.stefvanschiedev.buildinggame.utils.gameplayer.GamePlayerType)1 SubjectMenu (com.gmail.stefvanschiedev.buildinggame.utils.guis.SubjectMenu)1