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
}
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;
}
}
}
}
});
}
Aggregations