Search in sources :

Example 1 with GolfBall

use of gg.projecteden.nexus.features.minigolf.models.GolfBall 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)

Example 2 with GolfBall

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

the class MiniGolf method miniGolfTask.

private void miniGolfTask() {
    Tasks.repeat(0, TickTime.TICK, () -> {
        if (golfBalls.isEmpty())
            return;
        for (GolfBall golfBall : new ArrayList<>(golfBalls)) {
            Snowball ball = golfBall.getSnowball();
            if (ball == null)
                continue;
            if (!ball.isValid()) {
                golfBall.remove();
                continue;
            }
            // Location location = ball.getLocation();
            // if (golfBall.getLastLocation().equals(location))
            // continue;
            MiniGolfBallMoveEvent ballMoveEvent = new MiniGolfBallMoveEvent(golfBall, golfBall.getLastLocation(), ball.getLocation());
            if (!ballMoveEvent.callEvent()) {
                ball.teleportAsync(golfBall.getLastLocation());
                ball.setVelocity(ball.getVelocity());
            }
            Block below = golfBall.getBlockBelow();
            Material belowType = below.getType();
            for (ModifierBlockType modifierBlockType : ModifierBlockType.values()) {
                ModifierBlock modifierBlock = modifierBlockType.getModifierBlock();
                if (modifierBlockType.equals(ModifierBlockType.DEFAULT) || modifierBlock.getMaterials().contains(belowType)) {
                    MiniGolfBallModifierBlockEvent modifierBlockEvent = new MiniGolfBallModifierBlockEvent(golfBall, modifierBlockType);
                    if (modifierBlockEvent.callEvent()) {
                        modifierBlock.handleRoll(golfBall);
                        break;
                    }
                }
            }
        }
    });
}
Also used : Snowball(org.bukkit.entity.Snowball) ModifierBlock(gg.projecteden.nexus.features.minigolf.models.blocks.ModifierBlock) MiniGolfBallModifierBlockEvent(gg.projecteden.nexus.features.minigolf.models.events.MiniGolfBallModifierBlockEvent) ModifierBlockType(gg.projecteden.nexus.features.minigolf.models.blocks.ModifierBlockType) ArrayList(java.util.ArrayList) Block(org.bukkit.block.Block) ModifierBlock(gg.projecteden.nexus.features.minigolf.models.blocks.ModifierBlock) Material(org.bukkit.Material) MiniGolfBallMoveEvent(gg.projecteden.nexus.features.minigolf.models.events.MiniGolfBallMoveEvent) GolfBall(gg.projecteden.nexus.features.minigolf.models.GolfBall)

Aggregations

GolfBall (gg.projecteden.nexus.features.minigolf.models.GolfBall)2 Block (org.bukkit.block.Block)2 ProtectedRegion (com.sk89q.worldguard.protection.regions.ProtectedRegion)1 MiniGolfUser (gg.projecteden.nexus.features.minigolf.models.MiniGolfUser)1 ModifierBlock (gg.projecteden.nexus.features.minigolf.models.blocks.ModifierBlock)1 ModifierBlockType (gg.projecteden.nexus.features.minigolf.models.blocks.ModifierBlockType)1 MiniGolfBallModifierBlockEvent (gg.projecteden.nexus.features.minigolf.models.events.MiniGolfBallModifierBlockEvent)1 MiniGolfBallMoveEvent (gg.projecteden.nexus.features.minigolf.models.events.MiniGolfBallMoveEvent)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 ArrayList (java.util.ArrayList)1 Material (org.bukkit.Material)1 Snowball (org.bukkit.entity.Snowball)1 EventHandler (org.bukkit.event.EventHandler)1 Action (org.bukkit.event.block.Action)1 ItemStack (org.bukkit.inventory.ItemStack)1