#61 chunkStore is getting filled/emptied on demand

This commit is contained in:
Danielv123 committed 2017-12-10 14:07:37 +01:00
1 parent b1f35c9b72
commit 8fce9a2cfe
6 files changed
+72 -62

No files matched your search

+8 -7
View File
@@ -13,7 +13,7 @@ module.exports = class chunkStore {
this.path = (path[path.length] == "/") ? path : path+"/";
mkdirpSync(path+name);
}
setEntity(x, y, newEntity){
setEntity(x/**{number}*/, y/**{number}*/, newEntity/**{object}*/){
return new Promise((resolve, reject) => {
var localX = Math.round(x % this.chunkSize);
x = Math.floor(Math.round(x)/this.chunkSize);
@@ -21,15 +21,14 @@ module.exports = class chunkStore {
y = Math.floor(Math.round(y)/this.chunkSize);
this.getChunk(x,y).then(chunk => {
log("Setentity")
log("Setentity");
if(!chunk.dataObject[localX]) chunk.dataObject[localX] = {};
if(!chunk.dataObject[localX][localY]) chunk.dataObject[localX][localY] = {};
let entity = chunk.dataObject[localX][localY]
if(newEntity && typeof newEntity == "object"){
log("Overwriting with new entity");
entity.name = newEntity.name;
log("Overwriting with new entity at "+localX+", "+localY);
chunk.dataObject[localX][localY] = newEntity;
} else {
log("Deleting entry at "+localX+", "+localY);
chunk.dataObject[localX][localY] = undefined;
@@ -44,8 +43,10 @@ module.exports = class chunkStore {
setChunk(x, y, chunk){
return new Promise((resolve, reject) => {
mkdirp(this.path+this.name+"/"+x).then(() => {
fs.writeFile(this.path+this.name+"/"+x+"/"+y, JSON.stringify(chunk), function(){
resolve();
fs.writeFile(this.path+this.name+"/"+x+"/"+y, JSON.stringify(chunk), function(err){
if(err) {
reject(err);
} else resolve();
});
});
});