Core/src/main/java/eu/univento/core/api/camera/Traveller.java

141 lines
4.6 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.camera;
import eu.univento.core.Core;
import eu.univento.core.api.player.CustomPlayer;
import io.vertx.core.json.JsonArray;
import io.vertx.core.json.JsonObject;
import lombok.Getter;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
/**
* @author Redstoneore, joethei
* @version 1.0
*/
public class Traveller {
private CustomPlayer player;
@Getter private List<Location> points = new ArrayList<>();
@Getter private int time;
private boolean traveling = false;
private String finished;
private String failed;
public Traveller(CustomPlayer player, String route) {
this.player = player;
Core.getCommons().getDatabaseManager().getMongoDB().getClient().findOne("routes", new JsonObject().put("route", route), null, event -> {
finished = event.result().getString("finished");
failed = event.result().getString("failed");
time = event.result().getInteger("time");
JsonArray locations = event.result().getJsonArray("locations");
for(int i = 0; i > locations.size(); i++) {
JsonObject json = locations.getJsonObject(i);
points.add(new Location(Bukkit.getWorld(json.getString("world")), json.getDouble("X"), json.getDouble("Y"), json.getDouble("Z"), json.getFloat("Yaw"), json.getFloat("Pitch")));
}
});
}
public void travel() {
if(points.size() == 0) {
player.sendMessage(failed);
return;
}
List<Double> diffs = new ArrayList<>();
List<Integer> travelTimes = new ArrayList<>();
Double totalDiff = 0.0D;
for (int i = 0; i < points.size() - 1; i++) {
Location start = points.get(i);
Location next = points.get(i + 1);
Double diff = start.distance(next);
totalDiff += diff;
diffs.add(diff);
}
for (Double d : diffs) {
travelTimes.add((int) (d / totalDiff * time));
}
final List<Location> tps = new ArrayList<>();
UUID world = player.getLocation().getWorld().getUID();
for (int i = 0; i < points.size() - 1; i++) {
Location thisPoint = points.get(i);
Location nextPoint = points.get(i + 1);
int travelPoint = travelTimes.get(i);
double moveX = nextPoint.getX() - thisPoint.getX();
double moveY = nextPoint.getY() - thisPoint.getY();
double moveZ = nextPoint.getZ() - thisPoint.getZ();
double movePitch = nextPoint.getPitch() - thisPoint.getPitch();
double yawDiff = Math.abs(nextPoint.getYaw() - thisPoint.getYaw());
double c;
if (yawDiff <= 180.0) {
if (thisPoint.getYaw() < nextPoint.getYaw()) {
c = yawDiff;
} else {
c = -yawDiff;
}
} else if (thisPoint.getYaw() < nextPoint.getYaw()) {
c = -(360.0 - yawDiff);
} else {
c = 360.0 - yawDiff;
}
double d = c / travelPoint;
for (int x = 0; x < travelPoint; x++) {
Location l = new Location(Bukkit.getWorld(world), thisPoint.getX() + moveX / travelPoint * x, thisPoint.getY() + moveY / travelPoint * x, thisPoint.getZ() + moveZ / travelPoint * x, (float) (thisPoint.getYaw() + d * x), (float) (thisPoint.getPitch() + movePitch / travelPoint * x));
tps.add(l);
}
}
try {
player.teleport(tps.get(0));
Bukkit.getScheduler().runTaskLater(Core.getInstance(), new Runnable() {
private int ticks = 0;
public void run() {
if (this.ticks < tps.size()) {
player.teleport(tps.get(this.ticks));
// check if they cancelled
if (traveling) {
Bukkit.getScheduler().runTaskLater(Core.getInstance(), this, 1L);
}
this.ticks += 1;
} else {
traveling = false;
player.sendMessage(finished);
}
}
}, 1L);
} catch (Exception e) {
player.sendMessage(failed);
e.printStackTrace();
}
}
//TODO: GitHub repo to code after: https://github.com/redstone/RCameraStudio
}