Search in sources :

Example 6 with GamePlayer

use of com.gmail.stefvanschiedev.buildinggame.utils.gameplayer.GamePlayer in project buildinggame by stefvanschie.

the class PlaceIgnoreSpectators method onPlayerInteract.

/**
 * Handles players trying to place a block where a spectator is in the way
 *
 * @param e the event used for this algorithm
 */
@EventHandler
public void onPlayerInteract(PlayerInteractEvent e) {
    var player = e.getPlayer();
    var arena = ArenaManager.getInstance().getArena(player);
    Block clickedBlock = e.getClickedBlock();
    // check if the player is actually playing and the clicked entity is a player
    if (arena == null || clickedBlock == null)
        return;
    boolean nearbySpectator = false;
    var relativeBlock = clickedBlock.getRelative(e.getBlockFace());
    var blockLocation = relativeBlock.getLocation();
    // check if there's a spectator nearby
    for (GamePlayer spectator : arena.getPlot(player).getSpectators()) {
        var location = spectator.getPlayer().getLocation();
        // check if the locations are in the same world before measuring the distance
        if (!location.getWorld().equals(blockLocation.getWorld()))
            continue;
        // this case, as the result will be the same (the player won't place the block, but we'll do it for them)
        if (location.distanceSquared(blockLocation) <= 1.8)
            nearbySpectator = true;
    }
    if (!nearbySpectator)
        return;
    PlayerInventory inventory = player.getInventory();
    // set block
    relativeBlock.setType((e.getHand() == EquipmentSlot.HAND ? inventory.getItemInMainHand() : inventory.getItemInOffHand()).getType());
}
Also used : GamePlayer(com.gmail.stefvanschiedev.buildinggame.utils.gameplayer.GamePlayer) Block(org.bukkit.block.Block) PlayerInventory(org.bukkit.inventory.PlayerInventory) EventHandler(org.bukkit.event.EventHandler)

Example 7 with GamePlayer

use of com.gmail.stefvanschiedev.buildinggame.utils.gameplayer.GamePlayer in project buildinggame by stefvanschie.

the class Plot method addVote.

/**
 * Adds a vote to this plot
 *
 * @param vote the vote to be added
 * @since 2.1.0
 */
@Contract("null -> fail")
public void addVote(Vote vote) {
    YamlConfiguration config = SettingsManager.getInstance().getConfig();
    YamlConfiguration messages = SettingsManager.getInstance().getMessages();
    if (arena.getState() != GameState.VOTING)
        return;
    Player sender = vote.getSender();
    // check if player votes on his/her own plot
    for (GamePlayer gamePlayer : getGamePlayers()) {
        if (gamePlayer.getPlayer().equals(sender)) {
            MessageManager.getInstance().send(sender, messages.getStringList("vote.own-plot"));
            return;
        }
    }
    // check how many times voted
    if (getTimesVoted(sender) == config.getInt("max-vote-change")) {
        messages.getStringList("vote.maximum-votes").forEach(message -> MessageManager.getInstance().send(sender, message.replace("%max_votes%", config.getInt("max-votes-change") + "")));
        return;
    }
    getTimesVoted().put(sender, getTimesVoted(sender) + 1);
    messages.getStringList("vote.message").forEach(message -> MessageManager.getInstance().send(sender, message.replace("%playerplot%", arena.getVotingPlot().getPlayerFormat()).replace("%points%", vote.getPoints() + "")));
    messages.getStringList("vote.receiver").forEach(message -> arena.getVotingPlot().getGamePlayers().forEach(player -> MessageManager.getInstance().send(player.getPlayer(), message.replace("%points%", vote.getPoints() + "").replace("%sender%", sender.getName()))));
    var senderArena = ArenaManager.getInstance().getArena(sender);
    if (senderArena != null)
        senderArena.getPlot(sender).getGamePlayers().forEach(player -> {
            player.addTitleAndSubtitle(messages.getString("vote.title").replace("%points%", vote.getPoints() + ""), messages.getString("vote.subtitle").replace("%points%", vote.getPoints() + ""));
            player.sendActionbar(messages.getString("vote.actionbar").replace("%points%", String.valueOf(vote.getPoints())));
        });
    int previousPoints = getPoints();
    if (hasVoted(sender))
        getVotes().remove(getVote(sender));
    votes.add(vote);
    if (!config.getBoolean("scoreboards.vote.text"))
        arena.getVoteScoreboard(this).setScore(getPlayerFormat(), getPoints());
    if (!config.getBoolean("names-after-voting") && config.getBoolean("scoreboards.vote.enable"))
        arena.getPlots().stream().filter(p -> !p.getGamePlayers().isEmpty()).flatMap(p -> getGamePlayers().stream()).forEach(player -> arena.getVoteScoreboard(this).show(player.getPlayer()));
    // point actions
    var configurationSection = config.getConfigurationSection("voting.point-actions");
    configurationSection.getKeys(false).forEach(key -> {
        int points;
        try {
            points = Integer.parseInt(key);
        } catch (NumberFormatException e) {
            if (config.getBoolean("debug"))
                Main.getInstance().getLogger().warning("Unsupported value found in config.yml in voting > point-actions > " + key);
            return;
        }
        // of points made the amount go over the minimum, it shouldn't already have been higher than the minimum)
        if (getPoints() < points || previousPoints >= points)
            return;
        configurationSection.getStringList(key).forEach(command -> {
            if (!command.isEmpty() && command.charAt(0) == '@') {
                String targetText = command.split(" ")[0];
                Target.parse(targetText).execute(command.substring(targetText.length() + 1));
            } else
                getGamePlayers().forEach(gamePlayer -> Bukkit.dispatchCommand(gamePlayer.getPlayer(), command));
        });
    });
    // track stats
    var statManager = StatManager.getInstance();
    getGamePlayers().forEach(gamePlayer -> {
        var player = gamePlayer.getPlayer();
        var stat = statManager.getStat(player, StatType.POINTS_RECEIVED);
        statManager.registerStat(player, StatType.POINTS_RECEIVED, (stat == null ? 0 : stat.getValue()) + vote.getPoints());
    });
    var stat = statManager.getStat(sender, StatType.POINTS_GIVEN);
    statManager.registerStat(sender, StatType.POINTS_GIVEN, (stat == null ? 0 : stat.getValue()) + vote.getPoints());
}
Also used : java.util(java.util) Player(org.bukkit.entity.Player) Biome(org.bukkit.block.Biome) org.bukkit(org.bukkit) Block(org.bukkit.block.Block) com.gmail.stefvanschiedev.buildinggame.utils(com.gmail.stefvanschiedev.buildinggame.utils) Main(com.gmail.stefvanschiedev.buildinggame.Main) GamePlayerType(com.gmail.stefvanschiedev.buildinggame.utils.gameplayer.GamePlayerType) CommandSender(org.bukkit.command.CommandSender) Particle(com.gmail.stefvanschiedev.buildinggame.utils.particle.Particle) Entity(org.bukkit.entity.Entity) PotentialLocation(com.gmail.stefvanschiedev.buildinggame.utils.potential.PotentialLocation) ArenaMode(com.gmail.stefvanschiedev.buildinggame.utils.arena.ArenaMode) GamePlayer(com.gmail.stefvanschiedev.buildinggame.utils.gameplayer.GamePlayer) MessageManager(com.gmail.stefvanschiedev.buildinggame.managers.messages.MessageManager) BlockState(org.bukkit.block.BlockState) SettingsManager(com.gmail.stefvanschiedev.buildinggame.managers.files.SettingsManager) LivingEntity(org.bukkit.entity.LivingEntity) Collectors(java.util.stream.Collectors) Nullable(org.jetbrains.annotations.Nullable) Contract(org.jetbrains.annotations.Contract) Region(com.gmail.stefvanschiedev.buildinggame.utils.region.Region) YamlConfiguration(org.bukkit.configuration.file.YamlConfiguration) StatManager(com.gmail.stefvanschiedev.buildinggame.managers.stats.StatManager) Arena(com.gmail.stefvanschiedev.buildinggame.utils.arena.Arena) BuildMenu(com.gmail.stefvanschiedev.buildinggame.utils.guis.buildmenu.BuildMenu) NotNull(org.jetbrains.annotations.NotNull) StatType(com.gmail.stefvanschiedev.buildinggame.utils.stats.StatType) ArenaManager(com.gmail.stefvanschiedev.buildinggame.managers.arenas.ArenaManager) MainSpawnManager(com.gmail.stefvanschiedev.buildinggame.managers.mainspawn.MainSpawnManager) SpectatorMenu(com.gmail.stefvanschiedev.buildinggame.utils.guis.spectatormenu.SpectatorMenu) Player(org.bukkit.entity.Player) GamePlayer(com.gmail.stefvanschiedev.buildinggame.utils.gameplayer.GamePlayer) GamePlayer(com.gmail.stefvanschiedev.buildinggame.utils.gameplayer.GamePlayer) YamlConfiguration(org.bukkit.configuration.file.YamlConfiguration) Contract(org.jetbrains.annotations.Contract)

Example 8 with GamePlayer

use of com.gmail.stefvanschiedev.buildinggame.utils.gameplayer.GamePlayer in project buildinggame by stefvanschie.

the class Plot method addSpectator.

/**
 * Adds a spectator to the plot
 *
 * @param spectator the player that wants to spectate
 * @param spectates the player the spectator wants to spectate
 * @since 2.1.0
 */
@Contract("null, _ -> fail; _, null -> fail")
public void addSpectator(final Player spectator, GamePlayer spectates) {
    YamlConfiguration config = SettingsManager.getInstance().getConfig();
    final var gamePlayer = new GamePlayer(spectator, GamePlayerType.SPECTATOR);
    gamePlayer.setSpectates(spectates);
    getAllGamePlayers().add(gamePlayer);
    getAllGamePlayers().forEach(player -> player.getPlayer().hidePlayer(Main.getInstance(), spectator));
    Material material = SettingsManager.getInstance().getMaterial("leave-item.id", Material.BARRIER);
    spectator.getInventory().setItem(config.getInt("leave-item.slot"), new ItemBuilder(spectator, material).setDisplayName(MessageManager.translate(SettingsManager.getInstance().getMessages().getString("leave-item.name"), spectator)).setClickEvent(event -> {
        gamePlayer.connect(MainSpawnManager.getInstance().getServer(), MainSpawnManager.getInstance().getMainSpawn());
        removeSpectator(gamePlayer);
        MessageManager.getInstance().send(spectator, ChatColor.GREEN + "Stopped spectating");
        event.setCancelled(true);
    }).build());
    spectator.getInventory().setItem(8, new ItemBuilder(spectator, Material.EMERALD).setDisplayName(ChatColor.GREEN + "Spectator menu").setClickEvent(event -> {
        new SpectatorMenu().show(spectator);
        event.setCancelled(true);
    }).build());
    spectator.teleport(spectates.getPlayer().getLocation());
    spectator.setGameMode(GameMode.CREATIVE);
    spectator.setCanPickupItems(false);
}
Also used : GamePlayer(com.gmail.stefvanschiedev.buildinggame.utils.gameplayer.GamePlayer) YamlConfiguration(org.bukkit.configuration.file.YamlConfiguration) SpectatorMenu(com.gmail.stefvanschiedev.buildinggame.utils.guis.spectatormenu.SpectatorMenu) Contract(org.jetbrains.annotations.Contract)

Example 9 with GamePlayer

use of com.gmail.stefvanschiedev.buildinggame.utils.gameplayer.GamePlayer in project buildinggame by stefvanschie.

the class Plot method removeSpectator.

/**
 * Removes the specified spectator from this plot and thus makes him stop spectating
 *
 * @param spectator the spectator to remove
 * @since 2.1.0
 */
@Contract("null -> fail")
public void removeSpectator(GamePlayer spectator) {
    getAllGamePlayers().remove(spectator);
    getAllGamePlayers().forEach(player -> player.getPlayer().showPlayer(Main.getInstance(), spectator.getPlayer()));
    Player spPlayer = spectator.getPlayer();
    spectator.restore();
    spPlayer.setCanPickupItems(true);
}
Also used : Player(org.bukkit.entity.Player) GamePlayer(com.gmail.stefvanschiedev.buildinggame.utils.gameplayer.GamePlayer) Contract(org.jetbrains.annotations.Contract)

Example 10 with GamePlayer

use of com.gmail.stefvanschiedev.buildinggame.utils.gameplayer.GamePlayer in project buildinggame by stefvanschie.

the class Arena method preStart.

/**
 * This is called before starting the game as to allow the players to vote on a subject if the subject choosing
 * should be done at {@link When#BEFORE_BUILD}. This will automatically progress the game when needed. When starting
 * the game this should be called rather than {@link #postStart()}.
 *
 * @since 6.4.0
 */
public void preStart() {
    if (getSubjectMenu().getWhen() == SubjectMenu.When.BEFORE_BUILD) {
        new BukkitRunnable() {

            @Override
            public void run() {
                postStart();
            }
        }.runTaskLater(Main.getInstance(), 200L);
        getUsedPlots().forEach(plot -> plot.getGamePlayers().forEach(gamePlayer -> {
            var player = gamePlayer.getPlayer();
            plot.getLocation().teleport(player);
            giveSubjectMenuItem(player);
            if (getSubjectMenu().opensInstantly()) {
                getSubjectMenu().show(player);
            }
        }));
        return;
    }
    postStart();
}
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) BukkitRunnable(org.bukkit.scheduler.BukkitRunnable)

Aggregations

GamePlayer (com.gmail.stefvanschiedev.buildinggame.utils.gameplayer.GamePlayer)13 YamlConfiguration (org.bukkit.configuration.file.YamlConfiguration)11 Contract (org.jetbrains.annotations.Contract)11 Player (org.bukkit.entity.Player)10 Main (com.gmail.stefvanschiedev.buildinggame.Main)9 ArenaManager (com.gmail.stefvanschiedev.buildinggame.managers.arenas.ArenaManager)9 SettingsManager (com.gmail.stefvanschiedev.buildinggame.managers.files.SettingsManager)9 MainSpawnManager (com.gmail.stefvanschiedev.buildinggame.managers.mainspawn.MainSpawnManager)9 MessageManager (com.gmail.stefvanschiedev.buildinggame.managers.messages.MessageManager)9 com.gmail.stefvanschiedev.buildinggame.utils (com.gmail.stefvanschiedev.buildinggame.utils)9 GamePlayerType (com.gmail.stefvanschiedev.buildinggame.utils.gameplayer.GamePlayerType)9 PotentialLocation (com.gmail.stefvanschiedev.buildinggame.utils.potential.PotentialLocation)9 Region (com.gmail.stefvanschiedev.buildinggame.utils.region.Region)9 org.bukkit (org.bukkit)9 NotNull (org.jetbrains.annotations.NotNull)9 java.util (java.util)8 Collectors (java.util.stream.Collectors)8 Entity (org.bukkit.entity.Entity)8 Nullable (org.jetbrains.annotations.Nullable)8 Plot (com.gmail.stefvanschiedev.buildinggame.utils.plot.Plot)7