Files
src
build.gradle
settings.gradle
Code
5 278
src/main/java/com/kodari/robloxmirror/RobloxMirror.java
package com.kodari.robloxmirror;
import com.cryptomorin.xseries.XMaterial;
import com.cryptomorin.xseries.profiles.builder.XSkull;
import com.cryptomorin.xseries.profiles.objects.Profileable;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.entity.ArmorStand;
import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.scheduler.BukkitRunnable;
import org.bukkit.util.Vector;
public class RobloxMirror extends JavaPlugin {
private static final Pattern STRING_VALUE = Pattern.compile("\\\"([^\\\"]+)\\\"\\s*:\\s*\\\"([^\\\"]*)\\\"");
private static final Pattern NUMBER_VALUE = Pattern.compile("\\\"([^\\\"]+)\\\"\\s*:\\s*(-?\\d+(?:\\.\\d+)?(?:[eE][+-]?\\d+)?)");
private BukkitRunnable task;
private ArmorStand npc;
private String npcUsername;
@Override
public void onEnable() {
saveDefaultConfig();
final String apiUrl = getConfig().getString("api-url", "");
final int connectTimeout = Math.max(1, getConfig().getInt("connect-timeout-millis", 3000));
final int readTimeout = Math.max(1, getConfig().getInt("read-timeout-millis", 3000));
final long interval = Math.max(1L, getConfig().getLong("update-interval-ticks", 10L));
if (apiUrl.trim().isEmpty()) {
getLogger().severe("api-url is empty; RobloxMirror has been disabled.");
Bukkit.getPluginManager().disablePlugin(this);
return;
}
task = new BukkitRunnable() {
@Override
public void run() {
try {
RemotePlayerData data = parseData(getDataFromAPI(apiUrl, connectTimeout, readTimeout));
Bukkit.getScheduler().runTask(RobloxMirror.this, () -> updateNpc(data));
} catch (Exception exception) {
getLogger().warning("Unable to update the Roblox mirror: " + exception.getMessage());
}
}
};
task.runTaskTimerAsynchronously(this, 0L, interval);
}
@Override
public void onDisable() {
if (task != null) {
task.cancel();
task = null;
}
if (npc != null && !npc.isDead()) {
npc.remove();
}
npc = null;
npcUsername = null;
}
private void updateNpc(RemotePlayerData data) {
if (!isEnabled()) {
return;
}
World world = Bukkit.getWorld(getConfig().getString("world", "world"));
if (world == null) {
getLogger().warning("Configured world does not exist: " + getConfig().getString("world", "world"));
return;
}
Location location = new Location(world, data.getX(), data.getY(), data.getZ());
if (npc == null || npc.isDead() || !npc.getWorld().equals(world)) {
if (npc != null && !npc.isDead()) {
npc.remove();
}
npc = spawnSteveNpc(data.getUsername(), location);
} else {
npc.teleport(location);
if (!data.getUsername().equals(npcUsername)) {
npc.setCustomName(data.getUsername());
npc.getEquipment().setHelmet(createHead(data.getUsername()));
}
}
npcUsername = data.getUsername();
applyAnimation(data.getState());
}
private ArmorStand spawnSteveNpc(String name, Location location) {
ArmorStand stand = location.getWorld().spawn(location, ArmorStand.class);
stand.setCustomName(name);
stand.setCustomNameVisible(true);
stand.setGravity(false);
stand.setInvulnerable(true);
stand.setPersistent(false);
stand.setBasePlate(false);
stand.setArms(true);
stand.getEquipment().setHelmet(createHead(name));
return stand;
}
private ItemStack createHead(String username) {
try {
return XSkull.createItem().profile(Profileable.detect(username)).apply();
} catch (RuntimeException exception) {
return XMaterial.matchXMaterial("PLAYER_HEAD").map(XMaterial::parseItem).orElse(null);
}
}
private void applyAnimation(String state) {
String normalizedState = state.toLowerCase(Locale.ROOT);
if ("walking".equals(normalizedState)) {
npc.setVelocity(new Vector(0.2, 0.0, 0.0));
} else if ("jumping".equals(normalizedState)) {
npc.setVelocity(new Vector(0.0, 0.5, 0.0));
} else {
npc.setVelocity(new Vector(0.0, 0.0, 0.0));
}
}
private String getDataFromAPI(String endpoint, int connectTimeout, int readTimeout) throws IOException {
HttpURLConnection connection = (HttpURLConnection) new URL(endpoint).openConnection();
connection.setRequestMethod("GET");
connection.setConnectTimeout(connectTimeout);
connection.setReadTimeout(readTimeout);
connection.setRequestProperty("Accept", "application/json");
try {
int responseCode = connection.getResponseCode();
if (responseCode < 200 || responseCode >= 300) {
throw new IOException("API returned HTTP " + responseCode);
}
StringBuilder content = new StringBuilder();
try (BufferedReader reader = new BufferedReader(
new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8))) {
String line;
while ((line = reader.readLine()) != null) {
content.append(line);
}
}
return content.toString();
} finally {
connection.disconnect();
}
}
private RemotePlayerData parseData(String json) throws IOException {
String username = extractString(json, "username");
String state = extractString(json, "state");
double x = extractNumber(json, "x");
double y = extractNumber(json, "y");
double z = extractNumber(json, "z");
if (username.trim().isEmpty()) {
throw new IOException("API response contains an empty username");
}
if (!Double.isFinite(x) || !Double.isFinite(y) || !Double.isFinite(z)) {
throw new IOException("API response contains invalid coordinates");
}
return new RemotePlayerData(username, state, x, y, z);
}
private String extractString(String json, String key) throws IOException {
Matcher matcher = STRING_VALUE.matcher(json);
while (matcher.find()) {
if (key.equals(matcher.group(1))) {
return matcher.group(2);
}
}
throw new IOException("API response is missing string field: " + key);
}
private double extractNumber(String json, String key) throws IOException {
Matcher matcher = NUMBER_VALUE.matcher(json);
while (matcher.find()) {
if (key.equals(matcher.group(1))) {
try {
return Double.parseDouble(matcher.group(2));
} catch (NumberFormatException exception) {
throw new IOException("Invalid numeric field: " + key, exception);
}
}
}
throw new IOException("API response is missing numeric field: " + key);