Skip to content

Commit

Permalink
add minecraft-data 1.8 and 1.9 as submodules, start doing #4
Browse files Browse the repository at this point in the history
  • Loading branch information
rom1504 committed Aug 17, 2015
1 parent 063501e commit 354a1be
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 45 deletions.
8 changes: 8 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[submodule "minecraft-data-1.8"]
path = minecraft-data/1.8
url = https://github.com/PrismarineJS/minecraft-data.git
branch = 1.8
[submodule "minecraft-data-1.9"]
path = minecraft-data/1.9
url = https://github.com/PrismarineJS/minecraft-data.git
branch = 1.9
2 changes: 1 addition & 1 deletion example.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
var nmcData=require("./");
var nmcData=require("./")("1.8.8");

console.log(nmcData.blocksByName["stone"]);
104 changes: 73 additions & 31 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,42 +1,84 @@
var mcData=require("minecraft-data");
var indexes=require("./indexes.js");
var fs = require("fs");

module.exports = {
blocks: indexes.blocksById,
blocksByName: indexes.blocksByName,
blocksArray:mcData.blocks,

biomes: indexes.biomesById,
biomesArray: mcData.biomes,
var cache={}; // prevent reindexing when requiring multiple time the same version

items: indexes.itemsById,
itemsByName: indexes.itemsByName,
itemsArray: mcData.items,
module.exports = function(mcVersion)
{
var majorVersion=toMajor(mcVersion);
if(majorVersion==null)
return null;
if(cache[majorVersion])
return cache[majorVersion];
var mcData=mcVersionToMcData(majorVersion);
if(mcData==null)
return null;
var nmcData=mcDataToNode(mcData);
cache[majorVersion]=nmcData;
return nmcData;
};

recipes: mcData.recipes,
function toMajor(mcVersion)
{
var parts=mcVersion.split(".");
if(parts.size<=1)
return null;
mcVersion=parts.slice(0,2).join(".");
return mcVersion;
}

instruments: indexes.instrumentsById,
instrumentsArray: mcData.instruments,
function mcVersionToMcData(mcVersion)
{
var dir="./minecraft-data/"+mcVersion;
if(!fs.existsSync(dir))
return null;
return {
blocks: require(dir+'/enums/blocks'),
biomes: require(dir+'/enums/biomes'),
items: require(dir+'/enums/items'),
recipes: require(dir+'/enums/recipes'),
instruments: require(dir+'/enums/instruments'),
materials: require(dir+'/enums/materials'),
entities: require(dir+'/enums/entities'),
protocol: require(dir+'/enums/protocol')
};
}

materials: mcData.materials,
function mcDataToNode(mcData) {
var indexes=require("./indexes.js")(mcData);
return {
blocks: indexes.blocksById,
blocksByName: indexes.blocksByName,
blocksArray: mcData.blocks,

entities: indexes.entitiesById,
entitiesByName:indexes.entitiesByName,
entitiesArray: mcData.entities,
biomes: indexes.biomesById,
biomesArray: mcData.biomes,

items: indexes.itemsById,
itemsByName: indexes.itemsByName,
itemsArray: mcData.items,

recipes: mcData.recipes,

instruments: indexes.instrumentsById,
instrumentsArray: mcData.instruments,

materials: mcData.materials,

entities: indexes.entitiesById,
entitiesByName: indexes.entitiesByName,
entitiesArray: mcData.entities,

findItemOrBlockById:function(id)
{
var item=indexes.itemsById[id];
if(item !== undefined) return item;
return indexes.blocksById[id];
},
findItemOrBlockByName:function(name)
{
var item=indexes.itemsByName[name];
if(item !== undefined) return item;
return indexes.blocksByName[name];
}
};

findItemOrBlockById: function (id) {
var item = indexes.itemsById[id];
if (item !== undefined) return item;
return indexes.blocksById[id];
},
findItemOrBlockByName: function (name) {
var item = indexes.itemsByName[name];
if (item !== undefined) return item;
return indexes.blocksByName[name];
}
};
}
21 changes: 11 additions & 10 deletions indexes.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
var mcData=require("minecraft-data");
var indexer=require("./indexer.js");

module.exports= {
biomesById:indexer.buildIndexFromArray(mcData.biomes,"id"),
module.exports= function(mcData){
return {
biomesById:indexer.buildIndexFromArray(mcData.biomes,"id"),

blocksById:indexer.buildIndexFromArray(mcData.blocks,"id"),
blocksByName:indexer.buildIndexFromArray(mcData.blocks,"name"),
blocksById:indexer.buildIndexFromArray(mcData.blocks,"id"),
blocksByName:indexer.buildIndexFromArray(mcData.blocks,"name"),

entitiesById:indexer.buildIndexFromArray(mcData.entities,"id"),
entitiesByName:indexer.buildIndexFromArray(mcData.entities,"name"),
entitiesById:indexer.buildIndexFromArray(mcData.entities,"id"),
entitiesByName:indexer.buildIndexFromArray(mcData.entities,"name"),

instrumentsById:indexer.buildIndexFromArray(mcData.instruments,"id"),
instrumentsById:indexer.buildIndexFromArray(mcData.instruments,"id"),

itemsById:indexer.buildIndexFromArray(mcData.items,"id"),
itemsByName:indexer.buildIndexFromArray(mcData.items,"name")
itemsById:indexer.buildIndexFromArray(mcData.items,"id"),
itemsByName:indexer.buildIndexFromArray(mcData.items,"name")
};
};
1 change: 1 addition & 0 deletions minecraft-data/1.8
Submodule 1.8 added at 47226e
1 change: 1 addition & 0 deletions minecraft-data/1.9
Submodule 1.9 added at daf959
3 changes: 0 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,5 @@
"license": "MIT",
"bugs": {
"url": "https://github.com/PrismarineJS/node-minecraft-data/issues"
},
"dependencies": {
"minecraft-data":"~0.3.0"
}
}

0 comments on commit 354a1be

Please sign in to comment.