Search in sources :

Example 1 with PlayerJoinArenaEvent

use of com.andrei1058.bedwars.api.events.player.PlayerJoinArenaEvent in project BedWars1058 by andrei1058.

the class Arena method addPlayer.

/**
 * Add a player to the arena
 *
 * @param p              - Player to add.
 * @param skipOwnerCheck - True if you want to skip the party checking for this player. This
 * @return true if was added.
 */
public boolean addPlayer(Player p, boolean skipOwnerCheck) {
    if (p == null)
        return false;
    debug("Player added: " + p.getName() + " arena: " + getArenaName());
    /* used for base enter/leave event */
    isOnABase.remove(p);
    // 
    if (getArenaByPlayer(p) != null) {
        return false;
    }
    if (getParty().hasParty(p)) {
        if (!skipOwnerCheck) {
            if (!getParty().isOwner(p)) {
                p.sendMessage(getMsg(p, Messages.COMMAND_JOIN_DENIED_NOT_PARTY_LEADER));
                return false;
            }
            int partySize = (int) getParty().getMembers(p).stream().filter(member -> {
                IArena arena = Arena.getArenaByPlayer(member);
                if (arena == null) {
                    return true;
                }
                return arena.isSpectator(member);
            }).count();
            if (partySize > maxInTeam * getTeams().size() - getPlayers().size()) {
                p.sendMessage(getMsg(p, Messages.COMMAND_JOIN_DENIED_PARTY_TOO_BIG));
                return false;
            }
            for (Player mem : getParty().getMembers(p)) {
                if (mem == p)
                    continue;
                IArena a = Arena.getArenaByPlayer(mem);
                if (a != null) {
                    /*if (a.isPlayer(mem)) {
                            a.removePlayer(mem, false);
                        } else */
                    if (a.isSpectator(mem)) {
                        a.removeSpectator(mem, false);
                    }
                }
                addPlayer(mem, true);
            }
        }
    }
    leaving.remove(p);
    if (status == GameState.waiting || (status == GameState.starting && (startingTask != null && startingTask.getCountdown() > 1))) {
        if (players.size() >= maxPlayers && !isVip(p)) {
            TextComponent text = new TextComponent(getMsg(p, Messages.COMMAND_JOIN_DENIED_IS_FULL));
            text.setClickEvent(new ClickEvent(ClickEvent.Action.OPEN_URL, config.getYml().getString("storeLink")));
            p.spigot().sendMessage(text);
            return false;
        } else if (players.size() >= maxPlayers && isVip(p)) {
            boolean canJoin = false;
            for (Player on : new ArrayList<>(players)) {
                if (!isVip(on)) {
                    canJoin = true;
                    removePlayer(on, false);
                    TextComponent vipKick = new TextComponent(getMsg(p, Messages.ARENA_JOIN_VIP_KICK));
                    vipKick.setClickEvent(new ClickEvent(ClickEvent.Action.OPEN_URL, config.getYml().getString("storeLink")));
                    p.spigot().sendMessage(vipKick);
                    break;
                }
            }
            if (!canJoin) {
                p.sendMessage(getMsg(p, Messages.COMMAND_JOIN_DENIED_IS_FULL_OF_VIPS));
                return false;
            }
        }
        PlayerJoinArenaEvent ev = new PlayerJoinArenaEvent(this, p, false);
        Bukkit.getPluginManager().callEvent(ev);
        if (ev.isCancelled())
            return false;
        // Remove from ReJoin
        ReJoin rejoin = ReJoin.getPlayer(p);
        if (rejoin != null) {
            rejoin.destroy(true);
        }
        p.closeInventory();
        players.add(p);
        p.setFlying(false);
        p.setAllowFlight(false);
        p.setHealth(20);
        for (Player on : players) {
            on.sendMessage(getMsg(on, Messages.COMMAND_JOIN_PLAYER_JOIN_MSG).replace("{vPrefix}", getChatSupport().getPrefix(p)).replace("{vSuffix}", getChatSupport().getSuffix(p)).replace("{playername}", p.getName()).replace("{player}", p.getDisplayName()).replace("{on}", String.valueOf(getPlayers().size())).replace("{max}", String.valueOf(getMaxPlayers())));
        }
        setArenaByPlayer(p, this);
        /* check if you can start the arena */
        if (status == GameState.waiting) {
            int teams = 0, teammates = 0;
            for (Player on : getPlayers()) {
                if (getParty().isOwner(on)) {
                    teams++;
                }
                if (getParty().hasParty(on)) {
                    teammates++;
                }
            }
            if (minPlayers <= players.size() && teams > 0 && players.size() != teammates / teams) {
                changeStatus(GameState.starting);
            } else if (players.size() >= minPlayers && teams == 0) {
                changeStatus(GameState.starting);
            }
        }
        // half full arena time shorten
        if (players.size() >= getMaxPlayers() / 2 && players.size() > minPlayers) {
            if (startingTask != null) {
                if (Bukkit.getScheduler().isCurrentlyRunning(startingTask.getTask())) {
                    if (startingTask.getCountdown() > getConfig().getInt(ConfigPath.GENERAL_CONFIGURATION_START_COUNTDOWN_HALF)) {
                        startingTask.setCountdown(BedWars.config.getInt(ConfigPath.GENERAL_CONFIGURATION_START_COUNTDOWN_HALF));
                    }
                }
            }
        }
        /* save player inventory etc */
        if (getServerType() != ServerType.BUNGEE) {
            new PlayerGoods(p, true);
            playerLocation.put(p, p.getLocation());
        }
        p.teleport(getWaitingLocation(), PlayerTeleportEvent.TeleportCause.PLUGIN);
        BedWarsScoreboard.giveScoreboard(p, this, false);
        sendPreGameCommandItems(p);
        for (PotionEffect pf : p.getActivePotionEffects()) {
            p.removePotionEffect(pf.getType());
        }
    } else if (status == GameState.playing) {
        addSpectator(p, false, null);
        /* stop code if status playing*/
        return false;
    }
    p.getInventory().setArmorContents(null);
    Bukkit.getScheduler().runTaskLater(plugin, () -> {
        // bungee mode invisibility issues
        if (getServerType() == ServerType.BUNGEE) {
            // fix invisibility issue
            // if (BedWars.nms.getVersion() == 7) {
            BedWars.nms.sendPlayerSpawnPackets(p, this);
        // }
        }
        for (Player on : Bukkit.getOnlinePlayers()) {
            if (on == null)
                continue;
            if (on.equals(p))
                continue;
            if (isPlayer(on)) {
                BedWars.nms.spigotShowPlayer(p, on);
                BedWars.nms.spigotShowPlayer(on, p);
            } else {
                BedWars.nms.spigotHidePlayer(p, on);
                BedWars.nms.spigotHidePlayer(on, p);
            }
        }
        if (getServerType() == ServerType.BUNGEE) {
            // fix invisibility issue
            // if (BedWars.nms.getVersion() == 7) {
            BedWars.nms.sendPlayerSpawnPackets(p, this);
        // }
        }
    }, 17L);
    if (getServerType() == ServerType.BUNGEE) {
        p.getEnderChest().clear();
    }
    if (getPlayers().size() >= getMaxPlayers()) {
        if (startingTask != null) {
            if (Bukkit.getScheduler().isCurrentlyRunning(startingTask.getTask())) {
                if (startingTask.getCountdown() > BedWars.config.getInt(ConfigPath.GENERAL_CONFIGURATION_START_COUNTDOWN_SHORTENED)) {
                    startingTask.setCountdown(BedWars.config.getInt(ConfigPath.GENERAL_CONFIGURATION_START_COUNTDOWN_SHORTENED));
                }
            }
        }
    }
    refreshSigns();
    JoinNPC.updateNPCs(getGroup());
    return true;
}
Also used : TextComponent(net.md_5.bungee.api.chat.TextComponent) Player(org.bukkit.entity.Player) IArena(com.andrei1058.bedwars.api.arena.IArena) PotionEffect(org.bukkit.potion.PotionEffect) ClickEvent(net.md_5.bungee.api.chat.ClickEvent) PlayerJoinArenaEvent(com.andrei1058.bedwars.api.events.player.PlayerJoinArenaEvent)

Example 2 with PlayerJoinArenaEvent

use of com.andrei1058.bedwars.api.events.player.PlayerJoinArenaEvent in project BedWars1058 by andrei1058.

the class Arena method addSpectator.

/**
 * Add a player as Spectator
 *
 * @param p            Player to be added
 * @param playerBefore True if the player has played in this arena before and he died so now should be a spectator.
 */
public boolean addSpectator(@NotNull Player p, boolean playerBefore, Location staffTeleport) {
    if (allowSpectate || playerBefore || staffTeleport != null) {
        debug("Spectator added: " + p.getName() + " arena: " + getArenaName());
        if (!playerBefore) {
            PlayerJoinArenaEvent ev = new PlayerJoinArenaEvent(this, p, true);
            Bukkit.getPluginManager().callEvent(ev);
            if (ev.isCancelled())
                return false;
        }
        // Remove from ReJoin
        ReJoin reJoin = ReJoin.getPlayer(p);
        if (reJoin != null) {
            reJoin.destroy(true);
        }
        leaving.remove(p);
        p.closeInventory();
        spectators.add(p);
        players.remove(p);
        updateSpectatorCollideRule(p, false);
        if (!playerBefore) {
            /* save player inv etc if isn't saved yet*/
            if (getServerType() != ServerType.BUNGEE) {
                new PlayerGoods(p, true);
                playerLocation.put(p, p.getLocation());
            }
            setArenaByPlayer(p, this);
        }
        BedWarsScoreboard.giveScoreboard(p, this, false);
        nms.setCollide(p, this, false);
        if (!playerBefore) {
            if (staffTeleport == null) {
                p.teleport(getSpectatorLocation(), PlayerTeleportEvent.TeleportCause.PLUGIN);
            } else {
                p.teleport(staffTeleport, PlayerTeleportEvent.TeleportCause.PLUGIN);
            }
        }
        p.setGameMode(GameMode.ADVENTURE);
        Bukkit.getScheduler().runTaskLater(plugin, () -> {
            if (leaving.contains(p))
                return;
            p.setAllowFlight(true);
            p.setFlying(true);
        }, 5L);
        if (p.getPassenger() != null && p.getPassenger().getType() == EntityType.ARMOR_STAND)
            p.getPassenger().remove();
        Bukkit.getScheduler().runTask(plugin, () -> {
            if (leaving.contains(p))
                return;
            for (Player on : Bukkit.getOnlinePlayers()) {
                if (on == p)
                    continue;
                if (getSpectators().contains(on)) {
                    BedWars.nms.spigotShowPlayer(p, on);
                    BedWars.nms.spigotShowPlayer(on, p);
                } else if (getPlayers().contains(on)) {
                    BedWars.nms.spigotHidePlayer(p, on);
                    BedWars.nms.spigotShowPlayer(on, p);
                } else {
                    BedWars.nms.spigotHidePlayer(p, on);
                    BedWars.nms.spigotHidePlayer(on, p);
                }
            }
            if (!playerBefore) {
                if (staffTeleport == null) {
                    p.teleport(getSpectatorLocation(), PlayerTeleportEvent.TeleportCause.PLUGIN);
                } else {
                    p.teleport(staffTeleport);
                }
            } else {
                p.teleport(getSpectatorLocation());
            }
            p.setAllowFlight(true);
            p.setFlying(true);
            /* Spectator items */
            sendSpectatorCommandItems(p);
            // make invisible because it is annoying whene there are many spectators around the map
            p.addPotionEffect(new PotionEffect(PotionEffectType.INVISIBILITY, Integer.MAX_VALUE, 1, false));
            p.getInventory().setArmorContents(null);
        });
        leaving.remove(p);
        p.sendMessage(getMsg(p, Messages.COMMAND_JOIN_SPECTATOR_MSG).replace("{arena}", this.getDisplayName()));
        /* update generator holograms for spectators */
        String iso = Language.getPlayerLanguage(p).getIso();
        for (IGenerator o : getOreGenerators()) {
            o.updateHolograms(p, iso);
        }
        for (ITeam t : getTeams()) {
            for (IGenerator o : t.getGenerators()) {
                o.updateHolograms(p, iso);
            }
        }
        for (ShopHolo sh : ShopHolo.getShopHolo()) {
            if (sh.getA() == this) {
                sh.updateForPlayer(p, iso);
            }
        }
    } else {
        p.sendMessage(getMsg(p, Messages.COMMAND_JOIN_SPECTATOR_DENIED_MSG));
        return false;
    }
    showTime.remove(p);
    refreshSigns();
    JoinNPC.updateNPCs(getGroup());
    return true;
}
Also used : ShopHolo(com.andrei1058.bedwars.api.arena.shop.ShopHolo) Player(org.bukkit.entity.Player) PotionEffect(org.bukkit.potion.PotionEffect) IGenerator(com.andrei1058.bedwars.api.arena.generator.IGenerator) ITeam(com.andrei1058.bedwars.api.arena.team.ITeam) PlayerJoinArenaEvent(com.andrei1058.bedwars.api.events.player.PlayerJoinArenaEvent)

Example 3 with PlayerJoinArenaEvent

use of com.andrei1058.bedwars.api.events.player.PlayerJoinArenaEvent in project BedWars1058 by andrei1058.

the class QuickBuyListener method onArenaJoin.

@EventHandler(priority = EventPriority.LOWEST)
public void onArenaJoin(PlayerJoinArenaEvent e) {
    if (e == null)
        return;
    if (e.isSpectator())
        return;
    PlayerQuickBuyCache cache = PlayerQuickBuyCache.getQuickBuyCache(e.getPlayer().getUniqueId());
    if (cache != null) {
        cache.destroy();
    }
    new PlayerQuickBuyCache(e.getPlayer());
}
Also used : PlayerQuickBuyCache(com.andrei1058.bedwars.shop.quickbuy.PlayerQuickBuyCache) EventHandler(org.bukkit.event.EventHandler)

Example 4 with PlayerJoinArenaEvent

use of com.andrei1058.bedwars.api.events.player.PlayerJoinArenaEvent in project BedWars1058 by andrei1058.

the class ShopCacheListener method onArenaJoin.

@EventHandler(priority = EventPriority.LOWEST)
public void onArenaJoin(PlayerJoinArenaEvent e) {
    if (e.isSpectator())
        return;
    ShopCache sc = ShopCache.getShopCache(e.getPlayer().getUniqueId());
    if (sc != null) {
        sc.destroy();
    }
    new ShopCache(e.getPlayer().getUniqueId());
}
Also used : ShopCache(com.andrei1058.bedwars.shop.ShopCache) EventHandler(org.bukkit.event.EventHandler)

Example 5 with PlayerJoinArenaEvent

use of com.andrei1058.bedwars.api.events.player.PlayerJoinArenaEvent in project BedWars1058 by andrei1058.

the class ArenaListeners method onPlayerJoinArena.

@EventHandler
public void onPlayerJoinArena(PlayerJoinArenaEvent e) {
    if (e == null)
        return;
    final IArena a = e.getArena();
    Bukkit.getScheduler().runTaskAsynchronously(BedWars.plugin, () -> ArenaSocket.sendMessage(ArenaSocket.formatUpdateMessage(a)));
}
Also used : IArena(com.andrei1058.bedwars.api.arena.IArena) EventHandler(org.bukkit.event.EventHandler)

Aggregations

EventHandler (org.bukkit.event.EventHandler)3 IArena (com.andrei1058.bedwars.api.arena.IArena)2 PlayerJoinArenaEvent (com.andrei1058.bedwars.api.events.player.PlayerJoinArenaEvent)2 Player (org.bukkit.entity.Player)2 PotionEffect (org.bukkit.potion.PotionEffect)2 IGenerator (com.andrei1058.bedwars.api.arena.generator.IGenerator)1 ShopHolo (com.andrei1058.bedwars.api.arena.shop.ShopHolo)1 ITeam (com.andrei1058.bedwars.api.arena.team.ITeam)1 ShopCache (com.andrei1058.bedwars.shop.ShopCache)1 PlayerQuickBuyCache (com.andrei1058.bedwars.shop.quickbuy.PlayerQuickBuyCache)1 ClickEvent (net.md_5.bungee.api.chat.ClickEvent)1 TextComponent (net.md_5.bungee.api.chat.TextComponent)1