Search in sources :

Example 6 with Region

use of com.gmail.stefvanschiedev.buildinggame.utils.region.Region in project buildinggame by stefvanschie.

the class CommandManager method onSetFloor.

/**
 * Called whenever a player wants to set the floor of a plot
 *
 * @param player the player
 * @param arena the arena
 * @param id the plot id to set the floor of
 * @since 5.8.0
 */
@Subcommand("setfloor")
@Description("Set the floor of a plot (inclusive)")
@CommandPermission("bg.setfloor")
@CommandCompletion("@arenas @nothing")
public void onSetFloor(Player player, Arena arena, int id) {
    final var plot = arena.getPlot(id);
    if (plot == null) {
        MessageManager.getInstance().send(player, ChatColor.RED + "That's not a valid plot");
        return;
    }
    player.getInventory().setItemInMainHand(new ItemBuilder(player, Material.STICK).setDisplayName(ChatColor.LIGHT_PURPLE + "Wand").setClickEvent(new Consumer<>() {

        private Location previousLocation;

        @Override
        public void accept(PlayerInteractEvent event) {
            YamlConfiguration arenas = SettingsManager.getInstance().getArenas();
            var player = event.getPlayer();
            var action = event.getAction();
            if (action != Action.LEFT_CLICK_BLOCK && action != Action.RIGHT_CLICK_BLOCK)
                return;
            if (previousLocation == null) {
                previousLocation = event.getClickedBlock().getLocation();
                MessageManager.getInstance().send(player, ChatColor.GREEN + "Now click on the other corner");
            } else {
                // second time
                var location = event.getClickedBlock().getLocation();
                String name = arena.getName();
                int plotID = plot.getId();
                World world = location.getWorld();
                if (previousLocation.getWorld().equals(world)) {
                    arenas.set(name + '.' + plotID + ".floor.high.world", world.getName());
                    arenas.set(name + '.' + plotID + ".floor.low.world", previousLocation.getWorld().getName());
                } else {
                    MessageManager.getInstance().send(player, ChatColor.RED + "The world has to be the same");
                    event.setCancelled(true);
                    return;
                }
                int highestX = Math.max(previousLocation.getBlockX(), location.getBlockX());
                int lowestX = Math.min(previousLocation.getBlockX(), location.getBlockX());
                int highestY = Math.max(previousLocation.getBlockY(), location.getBlockY());
                int lowestY = Math.min(previousLocation.getBlockY(), location.getBlockY());
                int highestZ = Math.max(previousLocation.getBlockZ(), location.getBlockZ());
                int lowestZ = Math.min(previousLocation.getBlockZ(), location.getBlockZ());
                // x
                arenas.set(name + '.' + plotID + ".floor.high.x", highestX);
                arenas.set(name + '.' + plotID + ".floor.low.x", lowestX);
                // y
                arenas.set(name + '.' + plotID + ".floor.high.y", highestY);
                arenas.set(name + '.' + plotID + ".floor.low.y", lowestY);
                // z
                arenas.set(name + '.' + plotID + ".floor.high.z", highestZ);
                arenas.set(name + '.' + plotID + ".floor.low.z", lowestZ);
                SettingsManager.getInstance().save();
                String worldName = world.getName();
                Supplier<World> worldSupplier = () -> Bukkit.getWorld(worldName);
                Region region = RegionFactory.createRegion(worldSupplier, highestX, highestY, highestZ, lowestX, lowestY, lowestZ);
                plot.setFloor(region);
                MessageManager.getInstance().send(player, ChatColor.GREEN + "Floor set!");
                previousLocation = null;
                player.getInventory().setItemInMainHand(null);
            }
            event.setCancelled(true);
        }
    }).build());
    MessageManager.getInstance().send(player, ChatColor.GREEN + "Please click on one corner");
}
Also used : PlayerInteractEvent(org.bukkit.event.player.PlayerInteractEvent) Region(com.gmail.stefvanschiedev.buildinggame.utils.region.Region) Supplier(java.util.function.Supplier) YamlConfiguration(org.bukkit.configuration.file.YamlConfiguration) PotentialLocation(com.gmail.stefvanschiedev.buildinggame.utils.potential.PotentialLocation)

Example 7 with Region

use of com.gmail.stefvanschiedev.buildinggame.utils.region.Region in project buildinggame by stefvanschie.

the class Move method teleportBack.

/**
 * Teleport a entity back to the specified plot
 *
 * @param plot the plot to teleport the entity to
 * @param entity the entity to teleport
 *
 * @since 7.0.0
 */
private static void teleportBack(Plot plot, Entity entity, Location previousLocation, Location currentLocation) {
    YamlConfiguration messages = SettingsManager.getInstance().getMessages();
    if (plot == null)
        return;
    Region firstBoundary = plot.getBoundary();
    if (!firstBoundary.isInside(previousLocation)) {
        List<Block> allBlocks = firstBoundary.getAllBlocks();
        entity.teleport(allBlocks.get(ThreadLocalRandom.current().nextInt(allBlocks.size())).getLocation());
    } else if (!firstBoundary.isInside(currentLocation)) {
        entity.teleport(previousLocation);
        MessageManager.getInstance().send(entity, messages.getStringList("in-game.move-out-bounds"));
    }
}
Also used : Region(com.gmail.stefvanschiedev.buildinggame.utils.region.Region) Block(org.bukkit.block.Block) YamlConfiguration(org.bukkit.configuration.file.YamlConfiguration)

Example 8 with Region

use of com.gmail.stefvanschiedev.buildinggame.utils.region.Region in project buildinggame by stefvanschie.

the class TreeGrow method onStructureGrow.

/**
 * Handles trees growing
 *
 * @param e an event representing a structure growing
 * @see StructureGrowEvent
 * @since 2.1.0
 */
@EventHandler
public static void onStructureGrow(StructureGrowEvent e) {
    Plot plot = null;
    for (var arena : ArenaManager.getInstance().getArenas()) {
        for (Plot p : arena.getPlots()) {
            Region boundary = p.getBoundary();
            if (boundary == null)
                continue;
            if (boundary.isInside(e.getLocation())) {
                plot = p;
                break;
            }
        }
    }
    if (plot == null)
        return;
    Region boundary = plot.getBoundary();
    if (boundary == null)
        return;
    e.getBlocks().removeIf(blockState -> !boundary.isInside(blockState.getLocation()));
}
Also used : Plot(com.gmail.stefvanschiedev.buildinggame.utils.plot.Plot) Region(com.gmail.stefvanschiedev.buildinggame.utils.region.Region) EventHandler(org.bukkit.event.EventHandler)

Example 9 with Region

use of com.gmail.stefvanschiedev.buildinggame.utils.region.Region in project buildinggame by stefvanschie.

the class Arena method setVotingPlot.

/**
 * Sets the new voting plot, send messages and titles, teleports all players, gives them new vote items and changes
 * their time and weather.
 *
 * @param votingPlot the new voting plot
 * @see Plot
 * @since 2.1.0
 */
public void setVotingPlot(Plot votingPlot) {
    SettingsManager instance = SettingsManager.getInstance();
    YamlConfiguration config = instance.getConfig();
    YamlConfiguration messages = instance.getMessages();
    this.votingPlot = votingPlot;
    Region boundary = votingPlot.getBoundary();
    Location safeLocation = boundary.getSafeLocation();
    Location center = boundary.getCenter();
    // https://bukkit.org/threads/lookat-and-move-functions.26768/
    if (safeLocation != null) {
        // Clone the loc to prevent applied changes to the input loc
        safeLocation = safeLocation.clone();
        // Values of change in distance (make it relative)
        double dx = center.getX() - safeLocation.getX();
        double dz = center.getZ() - safeLocation.getZ();
        // Set yaw
        if (dx != 0)
            safeLocation.setYaw((dx < 0 ? ((float) (1.5 * Math.PI)) : ((float) (0.5 * Math.PI))) - (float) Math.atan(dz / dx));
        else if (dz < 0)
            safeLocation.setYaw((float) Math.PI);
        // Set pitch
        safeLocation.setPitch((float) -Math.atan((center.getY() - safeLocation.getY()) / (Math.sqrt(Math.pow(dx, 2) + Math.pow(dz, 2)))));
        // Set values, convert to degrees (invert the yaw since Bukkit uses a different yaw dimension format)
        safeLocation.setYaw(-safeLocation.getYaw() * 180f / (float) Math.PI);
        safeLocation.setPitch(safeLocation.getPitch() * 180f / (float) Math.PI);
    }
    for (Plot plot : getUsedPlots()) {
        for (GamePlayer gamePlayer : plot.getAllGamePlayers()) {
            Player player = gamePlayer.getPlayer();
            if (!config.getBoolean("names-after-voting")) {
                messages.getStringList("voting.message").forEach(message -> MessageManager.getInstance().send(player, message.replace("%playerplot%", votingPlot.getPlayerFormat())));
                gamePlayer.addTitleAndSubtitle(messages.getString("voting.title").replace("%playerplot%", votingPlot.getPlayerFormat()), messages.getString("voting.subtitle").replace("%playerplot%", votingPlot.getPlayerFormat()));
                gamePlayer.sendActionbar(messages.getString("voting.actionbar").replace("%playerplot%", votingPlot.getPlayerFormat()));
            }
            int blockIndex = ThreadLocalRandom.current().nextInt(votingPlot.getBoundary().getAllBlocks().size());
            player.teleport(Objects.requireNonNullElseGet(safeLocation, () -> boundary.getAllBlocks().get(blockIndex).getLocation()));
            // give blocks
            player.getInventory().clear();
            if (gamePlayer.getGamePlayerType() == GamePlayerType.PLAYER) {
                config.getConfigurationSection("voting.items").getKeys(false).forEach(identifier -> {
                    boolean save = false;
                    if (!messages.contains("voting.items." + identifier + ".name")) {
                        messages.set("voting.items." + identifier + ".name", "Type: Null");
                        save = true;
                    }
                    if (!messages.contains("voting.items." + identifier + ".lore")) {
                        messages.set("voting.items." + identifier + ".lore", new ArrayList<String>(0));
                        save = true;
                    }
                    if (save)
                        instance.save();
                    Material material = SettingsManager.getInstance().getMaterial("voting.items." + identifier + ".id", Material.BARRIER);
                    player.getInventory().setItem(config.getInt("voting.items." + identifier + ".slot") - 1, new ItemBuilder(player, material).setDisplayName(MessageManager.translate(messages.getString("voting.items." + identifier + ".name"))).setLore(MessageManager.translate(messages.getStringList("voting.items." + identifier + ".lore"))).movable(false).setClickEvent(event -> {
                        int points = config.getInt("voting.items." + identifier + ".points");
                        votingPlot.addVote(new Vote(points, player));
                        event.setCancelled(true);
                    }).build());
                });
                boolean worldEditEnabled = Bukkit.getPluginManager().isPluginEnabled("WorldEdit");
                if (worldEditEnabled && config.getBoolean("reports.enable")) {
                    player.getInventory().setItem(8, new ItemBuilder(player, Material.BOOK).setDisplayName(ChatColor.RED + "Report build").movable(false).setClickEvent(event -> {
                        var dateTimeFormatter = DateTimeFormatter.ofPattern("yyy-MM-dd_HH-mm-ss");
                        String players = getVotingPlot().getGamePlayers().stream().map(gp -> '-' + gp.getPlayer().getName()).reduce("", String::concat);
                        var fileName = LocalDateTime.now().format(dateTimeFormatter) + players + ".schem";
                        var file = new File(instance.getReportsSchematicsFolder(), fileName);
                        getVotingPlot().getBoundary().saveSchematic(file, () -> MessageManager.getInstance().send(event.getPlayer(), ChatColor.GREEN + "Your report has been saved."));
                        getVotingPlot().getGamePlayers().forEach(gp -> Report.add(new Report(gp.getPlayer(), player, ZonedDateTime.now(), file)));
                    }).build());
                }
            }
            // update scoreboard and update time and weather
            if (config.getBoolean("scoreboards.vote.enable"))
                getVoteScoreboard(plot).show(player);
            player.setPlayerTime(plot.getTime(), false);
            player.setPlayerWeather(plot.isRaining() ? WeatherType.DOWNFALL : WeatherType.CLEAR);
        }
    }
    getVotedPlots().add(votingPlot);
}
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) Player(org.bukkit.entity.Player) GamePlayer(com.gmail.stefvanschiedev.buildinggame.utils.gameplayer.GamePlayer) SettingsManager(com.gmail.stefvanschiedev.buildinggame.managers.files.SettingsManager) Plot(com.gmail.stefvanschiedev.buildinggame.utils.plot.Plot) YamlConfiguration(org.bukkit.configuration.file.YamlConfiguration) GamePlayer(com.gmail.stefvanschiedev.buildinggame.utils.gameplayer.GamePlayer) Region(com.gmail.stefvanschiedev.buildinggame.utils.region.Region) File(java.io.File) PotentialLocation(com.gmail.stefvanschiedev.buildinggame.utils.potential.PotentialLocation)

Aggregations

Region (com.gmail.stefvanschiedev.buildinggame.utils.region.Region)9 YamlConfiguration (org.bukkit.configuration.file.YamlConfiguration)6 Plot (com.gmail.stefvanschiedev.buildinggame.utils.plot.Plot)4 Main (com.gmail.stefvanschiedev.buildinggame.Main)3 ArenaManager (com.gmail.stefvanschiedev.buildinggame.managers.arenas.ArenaManager)3 SettingsManager (com.gmail.stefvanschiedev.buildinggame.managers.files.SettingsManager)3 MessageManager (com.gmail.stefvanschiedev.buildinggame.managers.messages.MessageManager)3 com.gmail.stefvanschiedev.buildinggame.utils (com.gmail.stefvanschiedev.buildinggame.utils)3 GamePlayerType (com.gmail.stefvanschiedev.buildinggame.utils.gameplayer.GamePlayerType)3 PotentialLocation (com.gmail.stefvanschiedev.buildinggame.utils.potential.PotentialLocation)3 Player (org.bukkit.entity.Player)3 MainSpawnManager (com.gmail.stefvanschiedev.buildinggame.managers.mainspawn.MainSpawnManager)2 Timer (com.gmail.stefvanschiedev.buildinggame.timers.utils.Timer)2 Arena (com.gmail.stefvanschiedev.buildinggame.utils.arena.Arena)2 GamePlayer (com.gmail.stefvanschiedev.buildinggame.utils.gameplayer.GamePlayer)2 Supplier (java.util.function.Supplier)2 org.bukkit (org.bukkit)2 Location (org.bukkit.Location)2 EventHandler (org.bukkit.event.EventHandler)2 PlayerInteractEvent (org.bukkit.event.player.PlayerInteractEvent)2