Better bot creation, faceLocation() improved, Mojang API fetching cache added.
This commit is contained in:
30
src/main/java/net/nuggetmc/ai/utils/MathUtils.java
Normal file
30
src/main/java/net/nuggetmc/ai/utils/MathUtils.java
Normal file
@@ -0,0 +1,30 @@
|
||||
package net.nuggetmc.ai.utils;
|
||||
|
||||
import org.bukkit.util.NumberConversions;
|
||||
import org.bukkit.util.Vector;
|
||||
|
||||
public class MathUtils {
|
||||
|
||||
public static float[] fetchYawPitch(Vector dir) {
|
||||
double x = dir.getX();
|
||||
double z = dir.getZ();
|
||||
|
||||
float[] out = new float[2];
|
||||
|
||||
if (x == 0.0D && z == 0.0D) {
|
||||
out[1] = (float) (dir.getY() > 0.0D ? -90 : 90);
|
||||
}
|
||||
|
||||
else {
|
||||
double theta = Math.atan2(-x, z);
|
||||
out[0] = (float) Math.toDegrees((theta + 6.283185307179586D) % 6.283185307179586D);
|
||||
|
||||
double x2 = NumberConversions.square(x);
|
||||
double z2 = NumberConversions.square(z);
|
||||
double xz = Math.sqrt(x2 + z2);
|
||||
out[1] = (float) Math.toDegrees(Math.atan(-dir.getY() / xz));
|
||||
}
|
||||
|
||||
return out;
|
||||
}
|
||||
}
|
||||
@@ -6,12 +6,25 @@ import com.google.gson.JsonParser;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.net.URL;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class MojangAPI {
|
||||
|
||||
// Eventually create some sort of cache that stores the skin data so it doesn't have to keep pulling from the API
|
||||
// CATCHING NULLPOINTEREXCEPTION BAD!!!! eventually fix from the getAsJsonObject thingy
|
||||
private static final Map<String, String[]> CACHE = new HashMap<>();
|
||||
|
||||
public static String[] getSkin(String name) {
|
||||
if (CACHE.containsKey(name)) {
|
||||
return CACHE.get(name);
|
||||
}
|
||||
|
||||
String[] values = pullFromAPI(name);
|
||||
CACHE.put(name, values);
|
||||
return values;
|
||||
}
|
||||
|
||||
// CATCHING NULLPOINTEREXCEPTION BAD!!!! eventually fix from the getAsJsonObject thingy
|
||||
public static String[] pullFromAPI(String name) {
|
||||
try {
|
||||
String uuid = new JsonParser().parse(new InputStreamReader(new URL("https://api.mojang.com/users/profiles/minecraft/" + name)
|
||||
.openStream())).getAsJsonObject().get("id").getAsString();
|
||||
@@ -19,7 +32,7 @@ public class MojangAPI {
|
||||
.parse(new InputStreamReader(new URL("https://sessionserver.mojang.com/session/minecraft/profile/" + uuid + "?unsigned=false")
|
||||
.openStream())).getAsJsonObject().get("properties").getAsJsonArray().get(0).getAsJsonObject();
|
||||
return new String[] {property.get("value").getAsString(), property.get("signature").getAsString()};
|
||||
} catch (IOException | NullPointerException e) {
|
||||
} catch (IOException | IllegalStateException e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user