Search in sources :

Example 1 with MissileWarsPlayer

use of com.leomelonseeds.missilewars.teams.MissileWarsPlayer in project MissileWars by Leomelonseeds.

the class Arena method endGame.

/**
 * End a MissileWars game with a winning team
 *
 * @param winningTeam the winning team
 */
public void endGame(MissileWarsTeam winningTeam) {
    // Ignore if game isn't running
    if (!running) {
        return;
    }
    MissileWarsPlugin plugin = MissileWarsPlugin.getPlugin();
    // Cancel all tasks
    for (BukkitTask task : tasks) {
        task.cancel();
    }
    running = false;
    resetting = true;
    waitingForTie = false;
    redTeam.stopDeckItems();
    blueTeam.stopDeckItems();
    // Produce winner/discord messages
    TextChannel discordChannel = DiscordSRV.getPlugin().getMainTextChannel();
    String discordMessage;
    String winner;
    // Notify players and discord users of win
    if (winningTeam == null) {
        // Send titles
        redTeam.sendTitle("tie");
        blueTeam.sendTitle("tie");
        // Set notify messages
        discordMessage = ":pencil: A game was tied in arena " + this.getName();
        winner = "&e&lNONE";
    } else if (winningTeam == blueTeam) {
        // Send titles
        blueTeam.sendTitle("victory");
        redTeam.sendTitle("defeat");
        // Set notify messages
        List<String> blueList = new ArrayList<>();
        for (MissileWarsPlayer player : players) {
            if (blueTeam.containsPlayer(player.getMCPlayerId())) {
                blueList.add(player.getMCPlayer().getName());
            }
        }
        String blueWinners = String.join(", ", blueList);
        discordMessage = ":tada: Team **blue** (" + blueWinners + ") has won a game in arena " + this.getName();
        winner = "&9&lBLUE";
    } else {
        // Send titles
        redTeam.sendTitle("victory");
        blueTeam.sendTitle("defeat");
        // Set notify messages
        List<String> redList = new ArrayList<>();
        for (MissileWarsPlayer player : players) {
            if (redTeam.containsPlayer(player.getMCPlayerId())) {
                redList.add(player.getMCPlayer().getName());
            }
        }
        String redWinners = String.join(", ", redList);
        discordMessage = ":tada: Team **red** (" + redWinners + ") has won a game in arena " + this.getName();
        winner = "&c&lRED";
    }
    discordChannel.sendMessage(discordMessage).queue();
    // Setup player variables
    List<String> winningMessages = ConfigUtils.getConfigTextList("messages.classic-end", null, null, null);
    String earnMessage = ConfigUtils.getConfigText("messages.earn-currency", null, null, null);
    FileConfiguration ranksConfig = ConfigUtils.getConfigFile(MissileWarsPlugin.getPlugin().getDataFolder().toString(), "ranks.yml");
    int spawn_missile = ranksConfig.getInt("experience.spawn_missile");
    int use_utility = ranksConfig.getInt("experience.use_utility");
    int kill = ranksConfig.getInt("experience.kill");
    int portal_broken = ranksConfig.getInt("experience.portal_broken");
    int shield_health = ranksConfig.getInt("experience.shield_health");
    int win = ranksConfig.getInt("experience.win");
    int red_portal_amount = (blueTeam.getFirstPortalStatus() ? portal_broken : 0) + (blueTeam.getSecondPortalStatus() ? portal_broken : 0);
    int blue_portal_amount = (redTeam.getFirstPortalStatus() ? portal_broken : 0) + (redTeam.getSecondPortalStatus() ? portal_broken : 0);
    int red_shield_health_amount = ((int) ((100 - blueTeam.getShieldHealth())) / 10) * shield_health;
    int blue_shield_health_amount = ((int) ((100 - redTeam.getShieldHealth())) / 10) * shield_health;
    // Find players with most deaths and kills
    List<MissileWarsPlayer> mostKills = new ArrayList<>();
    List<MissileWarsPlayer> mostDeaths = new ArrayList<>();
    for (MissileWarsPlayer player : players) {
        if (!getTeam(player.getMCPlayerId()).equals("no team")) {
            if (mostKills.isEmpty() || mostKills.get(0).getKills() < player.getKills()) {
                mostKills.clear();
                mostKills.add(player);
            } else if (mostKills.get(0).getKills() == player.getKills()) {
                mostKills.add(player);
            }
            if (mostDeaths.isEmpty() || mostDeaths.get(0).getDeaths() < player.getDeaths()) {
                mostDeaths.clear();
                mostDeaths.add(player);
            } else if (mostDeaths.get(0).getDeaths() == player.getDeaths()) {
                mostDeaths.add(player);
            }
        }
    }
    // Produce most kills/deaths list
    List<String> mostKillsList = new ArrayList<>();
    for (MissileWarsPlayer player : mostKills) {
        mostKillsList.add(ConfigUtils.getFocusName(player.getMCPlayer()));
    }
    String most_kills = String.join(", ", mostKillsList);
    List<String> mostDeathsList = new ArrayList<>();
    for (MissileWarsPlayer player : mostDeaths) {
        mostDeathsList.add(ConfigUtils.getFocusName(player.getMCPlayer()));
    }
    String most_deaths = String.join(", ", mostDeathsList);
    int most_kills_amount = mostKills.isEmpty() ? 0 : mostKills.get(0).getKills();
    int most_deaths_amount = mostDeaths.isEmpty() ? 0 : mostDeaths.get(0).getDeaths();
    Economy econ = MissileWarsPlugin.getPlugin().getEconomy();
    LocalDateTime endTime = LocalDateTime.now();
    long gameTime = Duration.between(startTime, endTime).toSeconds();
    // Update stats for each player
    for (MissileWarsPlayer player : players) {
        player.getMCPlayer().setGameMode(GameMode.SPECTATOR);
        // Send win message
        for (String s : winningMessages) {
            s = s.replaceAll("%umw_winning_team%", winner);
            s = s.replaceAll("%umw_most_kills_amount%", Integer.toString(most_kills_amount));
            s = s.replaceAll("%umw_most_deaths_amount%", Integer.toString(most_deaths_amount));
            s = s.replaceAll("%umw_most_kills%", most_kills);
            s = s.replaceAll("%umw_most_deaths%", most_deaths);
            player.getMCPlayer().sendMessage(ChatColor.translateAlternateColorCodes('&', s));
        }
        // -1 = TIE, 0 = LOST, 1 = WIN
        int won = winningTeam == null ? -1 : 0;
        // Calculate currency gain per-game
        int amountEarned = 0;
        int playerAmount = 0;
        int teamAmount = 0;
        UUID uuid = player.getMCPlayerId();
        if (!getTeam(uuid).equals("no team")) {
            playerAmount = spawn_missile * player.getMissiles() + use_utility * player.getUtility() + kill * player.getKills();
            if (blueTeam.containsPlayer(uuid)) {
                teamAmount = blue_portal_amount + blue_shield_health_amount;
                if (winningTeam == blueTeam) {
                    teamAmount += win;
                    won = 1;
                }
            } else {
                teamAmount = red_portal_amount + red_shield_health_amount;
                if (winningTeam == redTeam) {
                    teamAmount += win;
                    won = 1;
                }
            }
            long playTime = Duration.between(player.getJoinTime(), endTime).toSeconds();
            double percentPlayed = (double) playTime / gameTime;
            amountEarned = playerAmount + (int) (percentPlayed * teamAmount);
            // Update player stats
            SQLManager sql = MissileWarsPlugin.getPlugin().getSQL();
            sql.updateClassicStats(uuid, won, 1, player.getKills(), player.getMissiles(), player.getUtility(), player.getDeaths());
            sql.updateWinstreak(uuid, mapType, won);
            sql.updateExp(uuid, amountEarned);
            String earnMessagePlayer = earnMessage.replaceAll("%umw_amount_earned%", Integer.toString(amountEarned));
            player.getMCPlayer().sendMessage(ChatColor.translateAlternateColorCodes('&', earnMessagePlayer));
        }
        econ.depositPlayer(player.getMCPlayer(), amountEarned);
    }
    long waitTime = plugin.getConfig().getInt("victory-wait-time") * 20L;
    // Remove all players after a short time or immediately if none exist
    if (plugin.isEnabled() && players.size() > 0) {
        new BukkitRunnable() {

            @Override
            public void run() {
                removePlayers();
                startTime = null;
            }
        }.runTaskLater(plugin, waitTime);
        new BukkitRunnable() {

            @Override
            public void run() {
                resetWorld();
            }
        }.runTaskLater(plugin, waitTime + 40L);
    } else {
        removePlayers();
        resetWorld();
        startTime = null;
    }
}
Also used : LocalDateTime(java.time.LocalDateTime) BukkitTask(org.bukkit.scheduler.BukkitTask) ArrayList(java.util.ArrayList) BukkitRunnable(org.bukkit.scheduler.BukkitRunnable) MissileWarsPlugin(com.leomelonseeds.missilewars.MissileWarsPlugin) SQLManager(com.leomelonseeds.missilewars.utilities.SQLManager) FileConfiguration(org.bukkit.configuration.file.FileConfiguration) TextChannel(github.scarsz.discordsrv.dependencies.jda.api.entities.TextChannel) MissileWarsPlayer(com.leomelonseeds.missilewars.teams.MissileWarsPlayer) Economy(net.milkbowl.vault.economy.Economy) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) UUID(java.util.UUID)

Example 2 with MissileWarsPlayer

use of com.leomelonseeds.missilewars.teams.MissileWarsPlayer in project MissileWars by Leomelonseeds.

the class Arena method leaveGame.

/**
 * Warp player back to the waiting lobby
 *
 * @param uuid the UUID of player
 */
public boolean leaveGame(UUID uuid) {
    MissileWarsPlayer toRemove = new MissileWarsPlayer(uuid);
    Player player = Bukkit.getPlayer(uuid);
    if (!isRunning() || redTeam == null || blueTeam == null) {
        return false;
    }
    if (redTeam.containsPlayer(uuid)) {
        redTeam.removePlayer(toRemove);
        for (MissileWarsPlayer mwPlayer : players) {
            ConfigUtils.sendConfigMessage("messages.leave-team-red", mwPlayer.getMCPlayer(), null, toRemove.getMCPlayer());
        }
    } else if (blueTeam.containsPlayer(uuid)) {
        blueTeam.removePlayer(toRemove);
        for (MissileWarsPlayer mwPlayer : players) {
            ConfigUtils.sendConfigMessage("messages.leave-team-blue", mwPlayer.getMCPlayer(), null, toRemove.getMCPlayer());
        }
    } else {
        return false;
    }
    checkEmpty();
    player.teleport(getPlayerSpawn(player));
    player.setGameMode(GameMode.ADVENTURE);
    return true;
}
Also used : Player(org.bukkit.entity.Player) MissileWarsPlayer(com.leomelonseeds.missilewars.teams.MissileWarsPlayer) MissileWarsPlayer(com.leomelonseeds.missilewars.teams.MissileWarsPlayer)

Example 3 with MissileWarsPlayer

use of com.leomelonseeds.missilewars.teams.MissileWarsPlayer in project MissileWars by Leomelonseeds.

the class ArenaGameruleListener method onDeath.

/**
 * Handle player deaths.
 */
@EventHandler
public void onDeath(PlayerDeathEvent event) {
    // Check if player was killed in an Arena
    Player player = event.getEntity();
    ArenaManager manager = MissileWarsPlugin.getPlugin().getArenaManager();
    Arena playerArena = manager.getArena(player.getUniqueId());
    if (playerArena == null) {
        player.setBedSpawnLocation(ConfigUtils.getSpawnLocation(), true);
        return;
    }
    // Find killer and increment kills
    if (player.getKiller() != null) {
        MissileWarsPlayer killer = playerArena.getPlayerInArena(player.getKiller().getUniqueId());
        killer.incrementKills();
        ConfigUtils.sendConfigSound("player-kill", killer.getMCPlayer());
    }
    Component deathMessage = event.deathMessage();
    event.setDeathMessage("");
    // Count death if player is on a team
    if (!playerArena.getTeam(player.getUniqueId()).equals("no team")) {
        MissileWarsPlayer missileWarsPlayer = playerArena.getPlayerInArena(player.getUniqueId());
        missileWarsPlayer.incrementDeaths();
        for (Player p : player.getWorld().getPlayers()) {
            p.sendMessage(deathMessage);
        }
    }
    player.setBedSpawnLocation(playerArena.getPlayerSpawn(player), true);
}
Also used : ArenaManager(com.leomelonseeds.missilewars.arenas.ArenaManager) Player(org.bukkit.entity.Player) MissileWarsPlayer(com.leomelonseeds.missilewars.teams.MissileWarsPlayer) MissileWarsPlayer(com.leomelonseeds.missilewars.teams.MissileWarsPlayer) Component(net.kyori.adventure.text.Component) Arena(com.leomelonseeds.missilewars.arenas.Arena) EventHandler(org.bukkit.event.EventHandler)

Example 4 with MissileWarsPlayer

use of com.leomelonseeds.missilewars.teams.MissileWarsPlayer in project MissileWars by Leomelonseeds.

the class Arena method announceMessage.

/**
 * Send a message to all players in the arena.
 *
 * @param path the path to the configurable message
 */
public void announceMessage(String path, MissileWarsPlayer focus) {
    for (MissileWarsPlayer player : players) {
        Player mcPlayer = player.getMCPlayer();
        ConfigUtils.sendConfigMessage(path, mcPlayer, this, focus != null ? focus.getMCPlayer() : null);
    }
}
Also used : Player(org.bukkit.entity.Player) MissileWarsPlayer(com.leomelonseeds.missilewars.teams.MissileWarsPlayer) MissileWarsPlayer(com.leomelonseeds.missilewars.teams.MissileWarsPlayer)

Example 5 with MissileWarsPlayer

use of com.leomelonseeds.missilewars.teams.MissileWarsPlayer in project MissileWars by Leomelonseeds.

the class CustomItemListener method getItemUsed.

/**
 * Checks what item a player is using to account for offhand
 *
 * @param player
 * @return
 */
private ItemStack getItemUsed(Player player) {
    ItemStack hand = player.getInventory().getItemInMainHand();
    ItemStack offhand = player.getInventory().getItemInOffHand();
    MissileWarsPlayer mwPlayer = getPlayerArena(player).getPlayerInArena(player.getUniqueId());
    if (mwPlayer.getDeck() == null) {
        return new ItemStack(Material.AIR);
    }
    if (mwPlayer.getDeck().getGear().contains(hand) || hand.getType() == Material.AIR) {
        return offhand;
    }
    return hand;
}
Also used : MissileWarsPlayer(com.leomelonseeds.missilewars.teams.MissileWarsPlayer) ItemStack(org.bukkit.inventory.ItemStack)

Aggregations

MissileWarsPlayer (com.leomelonseeds.missilewars.teams.MissileWarsPlayer)15 Player (org.bukkit.entity.Player)9 Arena (com.leomelonseeds.missilewars.arenas.Arena)4 ArenaManager (com.leomelonseeds.missilewars.arenas.ArenaManager)4 TextChannel (github.scarsz.discordsrv.dependencies.jda.api.entities.TextChannel)3 EventHandler (org.bukkit.event.EventHandler)3 BukkitRunnable (org.bukkit.scheduler.BukkitRunnable)3 MissileWarsPlugin (com.leomelonseeds.missilewars.MissileWarsPlugin)2 LinkedList (java.util.LinkedList)2 FileConfiguration (org.bukkit.configuration.file.FileConfiguration)2 BukkitTask (org.bukkit.scheduler.BukkitTask)2 MissileWarsTeam (com.leomelonseeds.missilewars.teams.MissileWarsTeam)1 SQLManager (com.leomelonseeds.missilewars.utilities.SQLManager)1 LocalDateTime (java.time.LocalDateTime)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Random (java.util.Random)1 UUID (java.util.UUID)1