max-mapper / minecraft-region

parses chunks out of a minecraft region file
18 stars 6 forks source link

Zlib problem in getChunk method #3

Open Didericis opened 8 years ago

Didericis commented 8 years ago

I'm trying to view and manipulate mca data in node.js. I'd like to use the getChunk(x, y) method, but it's been giving me trouble. Here some test code I used to see if I could read chunks. It's a modified version of the test.js file in the repo.

mcRegion = require('./')
var fs = require('fs')
var mcaData = fs.readFileSync('r.0.1.mca')
var region = mcRegion(mcaData)
console.log('region loaded')
console.log(region.getChunk(0, 0)) 

Here's the output I'm getting:

region loaded
[ommited path info]/zlibjs-node.js:2
(function() {'use strict';function m(a){throw a;}var p=void 0,u=!0;var A="unde
                                              ^
Error: unsupported compression method
    at Error (native)
    at new Db (/Users/eric/Documents/Programming/Projects/local/minecraft-journal/node_modules/minecraft-region/zlibjs-node.js:53:430)
    at Object.Hb [as inflateSync] (/Users/eric/Documents/Programming/Projects/local/minecraft-journal/node_modules/minecraft-region/zlibjs-node.js:55:46)
    at Region.getChunk (/Users/eric/Documents/Programming/Projects/local/minecraft-journal/node_modules/minecraft-region/index.js:68:29)
    at Region.getChunk (/Users/eric/Documents/Programming/Projects/local/minecraft-journal/node_modules/minecraft-region/index.js:8:59)
    at Object.<anonymous> (/Users/eric/Documents/Programming/Projects/local/minecraft-journal/node_modules/minecraft-region/test.js:6:20)
    at Module._compile (module.js:460:26)
    at Object.Module._extensions..js (module.js:478:10)
    at Module.load (module.js:355:32)
    at Function.Module._load (module.js:310:12)

I'm using node v0.12.2. Also tried with node v0.10.40 and got the same error. The only other thing I tried was changing line 5 in index.js from

 else var Zlib = require('./zlibjs-node')

to

 else var Zlib = require('zlib')

That gave me the following output:

region loaded
zlib.js:236
    throw new TypeError('Not a string or buffer');
          ^
TypeError: Not a string or buffer
    at zlibBufferSync (zlib.js:236:11)
    at Object.exports.inflateSync (zlib.js:172:10)
    at Region.getChunk (/Users/eric/Documents/Programming/Projects/local/minecraft-journal/node_modules/minecraft-region/index.js:68:29)
    at Region.getChunk (/Users/eric/Documents/Programming/Projects/local/minecraft-journal/node_modules/minecraft-region/index.js:8:59)
    at Object.<anonymous> (/Users/eric/Documents/Programming/Projects/local/minecraft-journal/node_modules/minecraft-region/test.js:6:20)
    at Module._compile (module.js:460:26)
    at Object.Module._extensions..js (module.js:478:10)
    at Module.load (module.js:355:32)
    at Function.Module._load (module.js:310:12)
    at Function.Module.runMain (module.js:501:10)

Any help would be much appreciated.

fidian commented 8 years ago

Try this:

var mcaData = fs.readFileSync('r.0.1.mca', 'binary')

I had the same problem until I realized Node was reading my file as the wrong encoding.

Didericis commented 8 years ago

D'Oh, right! Thanks!

Still having trouble with this library, though. I'm not able to get anything other than null from the getChunk(x, y) function. I've tried it with the r.0.1.mca file included in this repo and two of my own, all of which I know have data in them, but it doesn't matter. Also, it should have been returning true for a lot of those region.outOfBounds(x, y) calls I was making, but it's not.

Here's the code I'm using:

var mcRegion = require('minecraft-region');
var fs = require('fs');
var mcaData = fs.readFileSync('r.0.1.mca', 'binary');
var region = mcRegion(mcaData);
for (var x=-100; x<100; x++) {
    for (var y=-100; y<100; y++) {
        if (region.outOfBounds(x, y)){
            console.log(x + ', ' + y);
        } else if (region.getChunk(x, y)){
            console.log(region.getChunk(x, y));
        }
    }
}
fidian commented 8 years ago

I've made minor changes to the code that makes it work for me with Node. They're in the pull request #5. I hope they help with your experiments.

thelonious commented 8 years ago

@fidian I ran into this issue today as well. I can confirm that your changes work in node 0.10.41, 0.12.9, and 5.4.1. However, in the sample code, when I load the region, I have to include the region coordinate. For example, I have to change var region = mcRegion(mcData) to var region = mcRegion(mcData, 0, 0) where 0,0 should reflect the region coordinate from the mca file name.