Core/src/main/java/eu/univento/core/api/effects/ParticleWorldBoarder.java

119 lines
3.4 KiB
Java

/*
* Copyright (c) 2017 univento.eu - All rights reserved
* You are not allowed to use, distribute or modify this code
*/
package eu.univento.core.api.effects;
import eu.univento.core.api.events.BorderLeaveEvent;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerMoveEvent;
import org.bukkit.plugin.Plugin;
import java.util.ArrayList;
import java.util.List;
/**
* @author Janhektor
* @version 1.0
*/
public class ParticleWorldBoarder implements Listener {
private Plugin plugin;
private Location center;
private int radius;
private int particleDistance;
private double particlesPerBlock;
private List<Location> points;
private ParticleEffect particle;
public ParticleWorldBoarder() {
}
public ParticleWorldBoarder(Plugin plugin, Location center, int radius, ParticleEffect particle) {
this(plugin, center, radius, 15, 2.0D, particle);
}
public ParticleWorldBoarder(Plugin plugin, Location center, int radius, int particleDistance, double particlesPerBlock, ParticleEffect particle) {
this.plugin = plugin;
this.center = center;
this.radius = radius;
this.particleDistance = particleDistance;
this.particlesPerBlock = particlesPerBlock;
this.points = new ArrayList<>();
this.particle = particle;
Bukkit.getPluginManager().registerEvents(this, plugin);
calculatePoints();
}
private void calculatePoints() {
points.clear();
double u = 2 * Math.PI * radius;
double particleCount = u * this.particlesPerBlock;
double degree = 360 / particleCount;
for (double deg = 0; deg <= 360; deg += degree) {
double x = Math.cos(Math.toRadians(deg));
double z = Math.sin(Math.toRadians(deg));
points.add(center.clone().add(x * radius, 0, z * radius));
}
}
public Location getCenter() {
return center;
}
public int getRadius() {
return radius;
}
public int getParticleDistance() {
return particleDistance;
}
public double getParticlesPerBlock() {
return particlesPerBlock;
}
public void setRadius(int radius) {
this.radius = radius;
calculatePoints();
}
public void setCenter(Location center) {
this.center = center;
calculatePoints();
}
public void setParticleDistance(int particleDistance) {
this.particleDistance = particleDistance;
calculatePoints();
}
public void setParticlesPerBlock(double particlesPerBlock) {
this.particlesPerBlock = particlesPerBlock;
calculatePoints();
}
public boolean isInside(Location loc) {
double a = center.getX() - loc.getX();
double b = center.getZ() - loc.getZ();
return a * a + b * b < radius * radius;
}
@EventHandler
public void playerMoved(PlayerMoveEvent e) {
Player p = e.getPlayer();
points.stream().filter(loc -> loc.distanceSquared(p.getLocation()) < particleDistance * particleDistance).forEach(loc -> particle.display(0.1F, 0.0F, 0.1F, 0.0F, 1, loc, p));
if (isInside(e.getFrom()) && !isInside(e.getTo())) {
Bukkit.getPluginManager().callEvent(new BorderLeaveEvent(p));
}
}
}