Skip to content

Instantly share code, notes, and snippets.

@comp500
Last active April 7, 2017 12:20
Show Gist options
  • Save comp500/7fb653acf15930ca77f89bbf5b5f6707 to your computer and use it in GitHub Desktop.
Save comp500/7fb653acf15930ca77f89bbf5b5f6707 to your computer and use it in GitHub Desktop.
I'll just leave this here. It probably won't work on any system unless you clone my PC.
var fs = require('fs');
var windows1252 = require('windows-1252');
function parseRecipes(callback, itemList) {
fs.readFile('recipes.txt', 'utf8', function (err, data) {
if (err) throw err;
var dataLines = data.split('\n');
console.log(dataLines.length + " recipes");
var recipes = [];
var oreDictionary = [];
for (var i = 1; i < dataLines.length; i++) {
if (dataLines[i].length < 24) continue;
if (dataLines[i].charAt(24) == '/' && dataLines[i].charAt(25) == '/') continue;
var matches = dataLines[i].match(/\[SERVER_STARTED\]\[SERVER\]recipes\.add(Shaped|Shapeless)\((.*)\);/);
if (matches == null || matches.length < 3) {console.error("Line " + i + " failed: No recipesAdd match"); continue;}
var materials = matches[2].replace(/.withTag\(.*?\)/g, '').split(','); // change this later, to show NBT
if (materials == null || materials.length < 2) {console.error("Line " + i + " failed: No Materials match"); continue;}
var materialsFixed = [];
for (var j = 0; j < materials.length; j++) {
var trimmed = materials[j].replace(/[\s,<>[\]]+/g, '').split(":");
if (trimmed[0] == "null") {
materialsFixed[j] = null;
continue;
}
if (trimmed[0] == "ore") {
//console.log(trimmed);
var output = oreDictionary.find(function (element) {
return element.split(":")[1] == trimmed[1];
});
if (output == null) {
output = Object.keys(itemList).find(function (element) {
return element.split(":")[1] == trimmed[1];
});
if (output != null) {
oreDictionary.push(output);
console.log(oreDictionary);
}
} else {
console.log("oreDict hit!");
}
if (output != null) {
trimmed = output.replace(/[\s,<>[\]]+/g, '').split(":");
}
}
if (trimmed[2] == "*") {
trimmed[2] = 0;
}
if (trimmed.length < 3) {
trimmed.push("0");
}
var converted = itemList[trimmed.join(':')];
if (converted == null) {
materialsFixed[j] = trimmed.join(':');
} else {
materialsFixed[j] = converted[1];
}
}
materialsFixed.push(matches[1]);
recipes.push(materialsFixed);
}
callback(recipes);
});
}
function parseItemPanel(callback) {
fs.readFile('itempanel.csv', function (err, raw) {
if (err) throw err;
var data = windows1252.decode(raw.toString('binary')); // why annoying encoding!
var dataLines = data.split('\r\n');
console.log((dataLines.length - 1) + " itempanel items");
// indexedDB?!
var itemsRecipeIndexed = {}; // indexed by name:meta
var itemsIDIndexed = []; // indexed by order of itempanel.csv, for order of search
var itemsNameIndexed = {}; // for search
for (var i = 1; i < dataLines.length; i++) {
if (dataLines[i].length < 4) continue;
var item = dataLines[i].split(',');
itemsRecipeIndexed[item[0] + ":" + item[2]] = [item[1], item[4]];
itemsIDIndexed.push(item);
itemsNameIndexed[item[4]] = item;
}
callback(itemsRecipeIndexed, itemsIDIndexed, itemsNameIndexed);
});
}
parseItemPanel(function (items, foo, bar) {
parseRecipes(function (recipes) {
fs.writeFile("recipelist.json", JSON.stringify(recipes), function (err) {if (err) throw err;});
var html = '<meta charset="utf-8">';
/*for (var i = 0; i < 18000; i++) {
var pngname = encodeURIComponent(foo[i][4].replace(/[:\/"]/g, "_"));
html += '<img src="itempanel_icons/'+ pngname +'.png" title="'+ foo[i][4] +'" width="32" height="32">';
}*/
for (var i = 0; i < recipes.length; i++) {
var pngname = encodeURIComponent(recipes[i][0].replace(/[:\/"]/g, "_"));
html += '<img src="itempanel_icons/'+ pngname +'.png" title="'+ recipes[i][0] +'" width="32" height="32">';
html += ' from ';
for (var j = 1; j < recipes[i].length; j++) {
if (recipes[i][j] == "Shapeless" || recipes[i][j] == "Shaped") {
html += " (" + recipes[i][j] + ")";
break;
}
if (recipes[i][j] == null) {
continue;
}
var pngname = encodeURIComponent(recipes[i][j].replace(/[:\/"]/g, "_"));
html += '<img src="itempanel_icons/'+ pngname +'.png" title="'+ recipes[i][j] +'" width="32" height="32">';
}
html += '<br>';
}
fs.writeFile("test.html", html, function (err) {if (err) throw err;});
}, items);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment