Search in sources :

Example 1 with MiniGolfBallSpawnEvent

use of gg.projecteden.nexus.features.minigolf.models.events.MiniGolfBallSpawnEvent in project Nexus by ProjectEdenGG.

the class InteractListener method onPutt.

@EventHandler
public void onPutt(PlayerInteractEvent event) {
    if (event.getHand() == null)
        return;
    if (!event.getHand().equals(EquipmentSlot.HAND))
        return;
    MiniGolfUser user = MiniGolfUtils.getUser(event.getPlayer().getUniqueId());
    if (user == null)
        return;
    ItemStack item = event.getItem();
    if (isNullOrAir(item)) {
        user.debug("item is null or air, returning");
        return;
    }
    Action action = event.getAction();
    Block block = event.getClickedBlock();
    if (item.getType().equals(Material.SNOWBALL)) {
        user.debug("placing golf ball...");
        if (user.getGolfBall() != null && user.getGolfBall().getSnowball() != null) {
            user.debug("you already have a ball placed");
            event.setCancelled(true);
            return;
        }
        if (isNullOrAir(block)) {
            user.debug("placed on block is air or null");
            event.setCancelled(true);
            return;
        }
        GolfBall golfBall = new GolfBall(user.getUuid());
        // Verify region
        WorldGuardUtils worldguard = new WorldGuardUtils(block);
        Set<ProtectedRegion> regions = worldguard.getRegionsLikeAt(MiniGolf.holeRegionRegex, block.getLocation());
        ProtectedRegion region = regions.stream().findFirst().orElse(null);
        if (region == null) {
            user.debug("hole region not found");
            event.setCancelled(true);
            return;
        }
        golfBall.setHoleRegion(region.getId());
        // Place Event
        MiniGolfUserPlaceBallEvent placeBallEvent = new MiniGolfUserPlaceBallEvent(user, golfBall, Set.of(Material.GREEN_WOOL));
        if (!placeBallEvent.callEvent()) {
            user.debug("place ball event cancelled");
            event.setCancelled(true);
            return;
        }
        if (!placeBallEvent.canPlaceBall(block.getType())) {
            user.debug("incorrect starting position");
            event.setCancelled(true);
            return;
        }
        // Spawn Event
        MiniGolfBallSpawnEvent ballSpawnEvent = new MiniGolfBallSpawnEvent(golfBall, block.getLocation());
        if (!ballSpawnEvent.callEvent()) {
            user.debug("place spawn event cancelled");
            event.setCancelled(true);
            return;
        }
        item.setAmount(item.getAmount() - 1);
        ballSpawnEvent.spawnBall();
        MiniGolf.getGolfBalls().add(golfBall);
    }
// TODO
}
Also used : MiniGolfUser(gg.projecteden.nexus.features.minigolf.models.MiniGolfUser) Action(org.bukkit.event.block.Action) MiniGolfUserPlaceBallEvent(gg.projecteden.nexus.features.minigolf.models.events.MiniGolfUserPlaceBallEvent) MiniGolfBallSpawnEvent(gg.projecteden.nexus.features.minigolf.models.events.MiniGolfBallSpawnEvent) ProtectedRegion(com.sk89q.worldguard.protection.regions.ProtectedRegion) Block(org.bukkit.block.Block) ItemStack(org.bukkit.inventory.ItemStack) WorldGuardUtils(gg.projecteden.nexus.utils.WorldGuardUtils) GolfBall(gg.projecteden.nexus.features.minigolf.models.GolfBall) EventHandler(org.bukkit.event.EventHandler)

Aggregations

ProtectedRegion (com.sk89q.worldguard.protection.regions.ProtectedRegion)1 GolfBall (gg.projecteden.nexus.features.minigolf.models.GolfBall)1 MiniGolfUser (gg.projecteden.nexus.features.minigolf.models.MiniGolfUser)1 MiniGolfBallSpawnEvent (gg.projecteden.nexus.features.minigolf.models.events.MiniGolfBallSpawnEvent)1 MiniGolfUserPlaceBallEvent (gg.projecteden.nexus.features.minigolf.models.events.MiniGolfUserPlaceBallEvent)1 WorldGuardUtils (gg.projecteden.nexus.utils.WorldGuardUtils)1 Block (org.bukkit.block.Block)1 EventHandler (org.bukkit.event.EventHandler)1 Action (org.bukkit.event.block.Action)1 ItemStack (org.bukkit.inventory.ItemStack)1