Skip to content

Instantly share code, notes, and snippets.

@irisfofs
Last active October 21, 2024 00:14
Show Gist options
  • Save irisfofs/5fa66d898d47b15e04dcab760330a661 to your computer and use it in GitHub Desktop.
Save irisfofs/5fa66d898d47b15e04dcab760330a661 to your computer and use it in GitHub Desktop.
Converts FTB Chunks waypoints to Xaero's Minimap waypoints.
/**
* To use:
* ```
* ftbJsonToXaeros('paste your waypoints.json contents here');
* ```
* then append to your Xaero's waypoints file(s), located in
* `xaero/minimap/<server name>/<dimension>/mw$default_1.txt`
*/
const MINECRAFT_COLORS = [
"000000",
"0000AA",
"00AA00",
"00AAAA",
"AA0000",
"AA00AA",
"FFAA00",
"AAAAAA",
"555555",
"5555FF",
"55FF55",
"55FFFF",
"FF5555",
"FF55FF",
"FFFF55",
"FFFFFF",
];
function ftbJsonToXaeros(ftbWaypointsJson) {
const waypoints = JSON.parse(ftbWaypointsJson)["waypoints"];
const xaerosWaypoints = waypoints.map((wp) => convertWaypoint(wp));
console.log(xaerosWaypoints.join("\n"));
return xaerosWaypoints;
}
// #waypoint:name:initials:x:y:z:color:disabled:type:set:rotate_on_tp:tp_yaw:visibility_type:destination
function convertWaypoint(wp) {
const name = wp["name"];
const initials = name[0]; // Doesn't work outside basic multilingual plane.
const x = wp["x"];
const y = wp["y"];
const z = wp["z"];
const color = colorToClosestMinecraftColor(wp["color"]);
const disabled = wp["hidden"];
const type = 0;
const set = "gui.xaero_default";
const rotateOnTp = false;
const tpYaw = 0;
const visibilityType = 0;
const destination = false;
return [
"waypoint",
name,
initials,
x,
y,
z,
color,
disabled,
type,
set,
rotateOnTp,
tpYaw,
visibilityType,
destination,
].join(":");
}
const HEX_COMPONENT_REGEX =
/#?(?<r>[0-9A-F]{2})(?<g>[0-9A-F]{2})(?<b>[0-9A-F]{2})/;
function colorToClosestMinecraftColor(hex) {
if (!hex.match(/#?[0-9A-F]{6}/)) return 0;
let minDiff = Number.POSITIVE_INFINITY;
let bestColor = 0;
for (let i = 0; i < MINECRAFT_COLORS.length; i++) {
const diff = colorDifference(hex, MINECRAFT_COLORS[i]);
if (diff < minDiff) {
minDiff = diff;
bestColor = i;
}
}
return bestColor;
}
/**
* Returns the squared Euclidean difference between two hex colors in sRGB
* space.
*/
function colorDifference(hex1, hex2) {
const c1 = hex1.match(HEX_COMPONENT_REGEX).groups;
const c2 = hex2.match(HEX_COMPONENT_REGEX).groups;
const r1 = parseInt(c1.r, 16);
const g1 = parseInt(c1.g, 16);
const b1 = parseInt(c1.b, 16);
const r2 = parseInt(c2.r, 16);
const g2 = parseInt(c2.g, 16);
const b2 = parseInt(c2.b, 16);
return Math.pow(r1 - r2, 2) + Math.pow(g1 - g2, 2) + Math.pow(b1 - b2, 2);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment