diff --git a/package-lock.json b/package-lock.json index a59c573..743aa2a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "pocketnode", - "version": "0.0.4", + "version": "0.0.5", "lockfileVersion": 1, "requires": true, "dependencies": { @@ -102,7 +102,7 @@ } }, "pocketnode-binarystream": { - "version": "git+https://github.com/PocketNode/PocketNode-BinaryStream.git#c12c26af5a25cdac758fa8e14eeb6bdbff7c9b25" + "version": "git+https://github.com/PocketNode/PocketNode-BinaryStream.git#1408d60c3cdea5cacb94a6d6a20b2c884678bf32" }, "pocketnode-language": { "version": "git+https://github.com/PocketNode/PocketNode-Language.git#d36c23f448f599636e4f8705bc15adcc6e0fb9cd" @@ -110,7 +110,7 @@ "raknet": { "version": "git+https://github.com/PocketNode/RakNet.git#883fc47dd246392d429e0d9027cf3f53ef6dd81b", "requires": { - "pocketnode-binarystream": "git+https://github.com/PocketNode/PocketNode-BinaryStream.git#c12c26af5a25cdac758fa8e14eeb6bdbff7c9b25" + "pocketnode-binarystream": "git+https://github.com/PocketNode/PocketNode-BinaryStream.git#1408d60c3cdea5cacb94a6d6a20b2c884678bf32" } }, "requizzle": { diff --git a/package.json b/package.json index 42f7d42..5da0460 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "pocketnode", - "version": "0.0.4", + "version": "0.0.5", "description": "Server software written in Javascript for Minecraft: PE", "main": "src/pocketnode/PocketNode.js", "scripts": { @@ -18,8 +18,9 @@ "license": "ISC", "dependencies": { "adm-zip": "^0.4.7", - "raknet": "git+https://github.com/PocketNode/RakNet.git", + "pocketnode-binarystream": "git+https://github.com/PocketNode/PocketNode-BinaryStream.git", "pocketnode-language": "git+https://github.com/PocketNode/PocketNode-Language.git", + "raknet": "git+https://github.com/PocketNode/RakNet.git", "time-stamp": "^2.0.0" }, "devDependencies": { diff --git a/src/pocketnode/PocketNode.js b/src/pocketnode/PocketNode.js index 2b8e221..df39ef8 100644 --- a/src/pocketnode/PocketNode.js +++ b/src/pocketnode/PocketNode.js @@ -9,7 +9,7 @@ function PocketNode(paths){ this.START_TIME = Date.now(); this.NAME = "PocketNode"; this.CODENAME = "[BEGINNINGS]"; - this.VERSION = "0.0.4"; + this.VERSION = "0.0.5"; this.API_VERSION = "1.0.0"; let logger = new Logger("Server"); @@ -19,16 +19,10 @@ function PocketNode(paths){ plugins: Path.normalize(__dirname + "/../../plugins/") }; - for(let i in paths){ - if(typeof path[i] !== "undefined"){ - path[i] = paths[i]; - } - } + for(let i in paths) if(typeof path[i] !== "undefined") path[i] = paths[i]; logger.info("Loading PocketNode..."); - global.TRAVIS_BUILD = process.argv.indexOf("--travis-build") !== -1; - let server = new Server(this, logger, path); if(TRAVIS_BUILD === true){ server.shutdown(); diff --git a/src/pocketnode/Server.js b/src/pocketnode/Server.js index d74707c..4edd069 100644 --- a/src/pocketnode/Server.js +++ b/src/pocketnode/Server.js @@ -23,18 +23,18 @@ const SFS = pocketnode("utils/SimpleFileSystem"); class Server { initVars(){ - this.PocketNode = {}; + this._pocketnode = {}; this._bannedIps = {}; this._bannedNames = {}; this._ops = {}; this._whitelist = {}; - + this._running = true; this._stopped = false; this._pluginManager = {}; - + this._scheduler = {}; //todo this._tickCounter = 0; @@ -51,18 +51,18 @@ class Server { this._commandMap = {}; this._resourcePackManager = {}; - + this._onlineMode = false; this._raknetAdapter = {}; - + this._serverId = Math.floor((Math.random() * 99999999)+1); this._paths = {}; this._config = {}; this._maxPlayers = -1; - + this._players = new PlayerList(); this._loggedInPlayers = new PlayerList(); this._playerList = new PlayerList(); @@ -72,10 +72,11 @@ class Server { this._entityCount = 0; } - constructor(PocketNode, logger, paths){ + constructor(pocketnode, logger, paths){ this.initVars(); - this.PocketNode = PocketNode; + this._pocketnode = pocketnode; + this._logger = logger; this._paths = paths; @@ -148,7 +149,7 @@ class Server { this._tickCounter = 0; - this.getLogger().info("Done ("+(Date.now() - this.PocketNode.START_TIME)+"ms)!"); + this.getLogger().info("Done ("+(Date.now() - this._pocketnode.START_TIME)+"ms)!"); this.tickProcessor(); //this.forceShutdown(); @@ -183,21 +184,21 @@ class Server { * @return {string} */ getName(){ - return this.PocketNode.NAME; + return this._pocketnode.NAME; } /** * @return {string} */ getCodeName(){ - return this.PocketNode.CODENAME; + return this._pocketnode.CODENAME; } /** * @return {string} */ getPocketNodeVersion(){ - return this.PocketNode.VERSION; + return this._pocketnode.VERSION; } /** @@ -218,7 +219,7 @@ class Server { * @return {string} */ getApiVersion(){ - return this.PocketNode.API_VERSION; + return this._pocketnode.API_VERSION; } /** @@ -306,7 +307,7 @@ class Server { * @return {string} */ getMotd(){ - return this._config.getNested("server.motd", this.PocketNode.NAME + " Server"); + return this._config.getNested("server.motd", this._pocketnode.NAME + " Server"); } /** diff --git a/src/pocketnode/level/chunk/Chunk.js b/src/pocketnode/level/chunk/Chunk.js index 274a604..13f02ea 100644 --- a/src/pocketnode/level/chunk/Chunk.js +++ b/src/pocketnode/level/chunk/Chunk.js @@ -1,4 +1,4 @@ -const BinaryStream = pocketnode("utils/BinaryStream"); +const BinaryStream = pocketnode("network/minecraft/NetworkBinaryStream"); const SubChunk = pocketnode("level/chunk/SubChunk"); const EmptySubChunk = pocketnode("level/chunk/EmptySubChunk"); @@ -7,7 +7,7 @@ class Chunk { this._x = 0; this._z = 0; - this._height = 256; + this._height = 16; /** * @type {Map} @@ -40,24 +40,8 @@ class Chunk { this._x = x; this._z = z; - if(subChunks.size !== 0) { - for (let [y, chunk] of subChunks) { - if (y < 0 || y >= this._height) { - throw new Error("Invalid subchunk index " + y); - } - - if (chunk.isEmpty()) { - this._subChunks.set(y, new EmptySubChunk()); - } else { - this._subChunks.set(y, chunk); - } - } - } - - for(let i = 0; i < this._height; ++i){ - if(!this._subChunks.has(i)){ - this._subChunks.set(i, new EmptySubChunk()); - } + for(let y = 0; y < this._height; y++){ + this._subChunks.set(y, subChunks.has(y) ? subChunks.get(y) : new EmptySubChunk()); } if(heightMap.length === 256){ @@ -76,7 +60,7 @@ class Chunk { if(biomes.length !== 0){ throw new Error("Wrong Biomes value count, expected 256, got "+biomes.length); }else{ - this._biomes = new Array(256).fill(0); + this._biomes = new Array(256).fill(0x00); } } } @@ -162,7 +146,7 @@ class Chunk { } setBlockData(x, y, z, data){ - return this.getSubChunk(y >> 4).setBlockData(x, y & 0x0f, z, data); + return this.getSubChunk(y >> 4, true).setBlockData(x, y & 0x0f, z, data); } getBlockLight(x, y, z){ @@ -181,17 +165,10 @@ class Chunk { return this.getSubChunk(y >> 4, true).setBlockSkyLight(x, y & 0x0f, z, level); } - getSubChunk(y, generateNew = false){ - if(y < 0 || y >= this._height){ - return new EmptySubChunk(); - }else if(generateNew && this._subChunks.has(y) instanceof EmptySubChunk){ - this._subChunks.set(y, new SubChunk()); - } - - if(this._subChunks.get(y) === null){ - throw new Error("something broke.."); + getSubChunk(y, genNew = false){ + if(genNew && this._subChunks.get(y) instanceof EmptySubChunk){ + return this._subChunks.set(y, new SubChunk()).get(y); } - return this._subChunks.get(y); } @@ -222,28 +199,24 @@ class Chunk { } recalculateHeightMap(){ - for(let z = 0; z < 16; ++z){ - for(let x = 0; x < 16; ++x){ - let id = this.getHighestBlockId(x, z); - + for(let x = 0; x < 16; x++){ + for(let z = 0; z < 16; z++){ this.setHeightMap(x, z, this.getHighestBlock(x, z) + 1); } } } getHighestSubChunk(){ - let highest = new EmptySubChunk(); - for(let y = 16; y > 0; --y){ - if(this._subChunks.has(y)){ + for(let y = 15; y >= 0; y--){ + if(!this._subChunks.has(y)){ continue; } if(this._subChunks.get(y).isEmpty()){ continue; } - highest = this._subChunks.get(y); - break; + return this._subChunks.get(y); } - return highest; + return new EmptySubChunk(); } getHighestBlockId(x, z){ @@ -255,23 +228,50 @@ class Chunk { } getHighestBlock(x, z){ - return this.getHighestSubChunk().getHighestBlock(x, z); + let index = this.getHighestSubChunkIndex(); + if(index === -1){ + return -1; + } + + for(let y = index; y >= 0; --y){ + let height = this.getSubChunk(y).getHighestBlock(x, z) | (y << 4); + if(height !== -1){ + return height; + } + } + + return -1; + } + + getHighestSubChunkIndex(){ + let y; + for(y = this._subChunks.size - 1; y >= 0; --y){ + if(this._subChunks.get(y) instanceof EmptySubChunk){ + continue; + } + break; + } + + return y; } getFilledSubChunks(){ - this.pruneEmptySubChunks(); - return this._subChunks.size; + //this.pruneEmptySubChunks(); + //return this._subChunks.size; + return this.getHighestSubChunkIndex() + 1; } pruneEmptySubChunks(){ - for(let [y, subChunk] of this._subChunks){ - if(y < 0 || y >= this._height){ - this._subChunks.delete(y); - }else if(subChunk instanceof EmptySubChunk){ + for(let y = 15; y >= 0; y--){ + if(!this._subChunks.has(y)){ continue; - }else if(subChunk.isEmpty()){ - this._subChunks.set(y, new EmptySubChunk()); } + + if(!this._subChunks.get(y).isEmpty()){ + return; + } + + this._subChunks.delete(y); } } @@ -305,12 +305,11 @@ class Chunk { let subChunkCount = this.getFilledSubChunks(); stream.writeByte(subChunkCount); - for(let i = 0; i < subChunkCount; i++){ - stream.append(this._subChunks.get(i).toBinary()); + for(let y = 0; y < subChunkCount; ++y){ + stream.append(this._subChunks.get(y).toBinary()); } - this._heightMap.forEach(v => stream.writeShort(v)); - + this._heightMap.forEach(v => stream.writeLShort(v)); this._biomes.forEach(v => stream.writeByte(v)); stream.writeByte(0); @@ -319,7 +318,7 @@ class Chunk { return stream.getBuffer(); } - static getIndex(x, y, z){ + static getIdIndex(x, y, z){ return (x << 12) | (z << 8) | y; } diff --git a/src/pocketnode/level/chunk/SubChunk.js b/src/pocketnode/level/chunk/SubChunk.js index 41509f5..944ac42 100644 --- a/src/pocketnode/level/chunk/SubChunk.js +++ b/src/pocketnode/level/chunk/SubChunk.js @@ -39,11 +39,11 @@ class SubChunk extends SubChunkInterface { } getBlockId(x, y, z){ - return this._blockIds[SubChunk.getIndex(x, y, z)]; + return this._blockIds[SubChunk.getIdIndex(x, y, z)]; } setBlockId(x, y, z, id){ - this._blockIds[SubChunk.getIndex(x, y, z)] = id; + this._blockIds[SubChunk.getIdIndex(x, y, z)] = id; return true; } @@ -95,10 +95,10 @@ class SubChunk extends SubChunkInterface { } } - setBlockSkyLight(x, y, z){ + setBlockSkyLight(x, y, z, level){ let i = SubChunk.getLightIndex(x, y, z); let byte = this._skyLight[i]; - if((y & 1) === 0){ + if((y & 0x01) === 0){ this._skyLight[i] = (byte & 0xf0) | (level & 0x0f); }else{ this._skyLight[i] = ((level & 0x0f) << 4) | (byte & 0x0f); @@ -107,15 +107,13 @@ class SubChunk extends SubChunkInterface { } getHighestBlockId(x, z){ - let low = (x << 8) | (z << 4); - let i = low | 0x0f; - for(; i >= low; --i){ - if(this._blockIds[i] !== 0x00){ - return i & 0x0f; + for(let y = 15; y >= 0; y--){ + let id = this.getBlockId(x, y, z); + if(id !== 0){ + return id; } } - - return -1; + return 0; } getHighestBlockData(x, z){ @@ -124,7 +122,7 @@ class SubChunk extends SubChunkInterface { getHighestBlock(x, z){ for(let y = 15; y >= 0; y--){ - if(this.getBlockData(x, y, z) !== 0){ + if(this.getBlockId(x, y, z) !== 0){ return y; } } @@ -133,12 +131,10 @@ class SubChunk extends SubChunkInterface { } toBinary(){ - let a = Buffer.from(this._blockIds); - let b = Buffer.from(this._blockData); - return Buffer.concat([a, b]); + return Buffer.from([0x00, ...this._blockIds, ...this._blockData]); } - static getIndex(x, y, z){ + static getIdIndex(x, y, z){ return (x << 8) | (z << 4) | y; } @@ -151,4 +147,6 @@ class SubChunk extends SubChunkInterface { } } +let subchunk = new SubChunk(); + module.exports = SubChunk; \ No newline at end of file diff --git a/src/pocketnode/network/PlayerSessionAdapter.js b/src/pocketnode/network/PlayerSessionAdapter.js index ad75431..54b6130 100644 --- a/src/pocketnode/network/PlayerSessionAdapter.js +++ b/src/pocketnode/network/PlayerSessionAdapter.js @@ -15,7 +15,7 @@ const TextPacket = pocketnode("network/minecraft/protocol/TextPacket"); const ResourcePack = pocketnode("resourcepacks/ResourcePack"); -const BinaryStream = pocketnode("utils/BinaryStream"); +const Async = pocketnode("utils/Async"); class PlayerSessionAdapter { constructor(player){ @@ -123,35 +123,37 @@ class PlayerSessionAdapter { handleRequestChunkRadius(packet){ this.player.setViewDistance(packet.radius); - let distance = this.player.getViewDistance(); - for(let chunkX = -distance; chunkX <= distance; chunkX++){ - for(let chunkZ = -distance; chunkZ <= distance; chunkZ++){ - let chunk = new Chunk(chunkX, chunkZ); - - for(let z = 0; z < 16; ++z){ - for(let x = 0; x < 16; ++x){ - let y = 0; - //chunk.setBlockId(x, y++, z, 7); - //chunk.setBlockId(x, y++, z, 3); - //chunk.setBlockId(x, y++, z, 3); - chunk.setBlockId(x, y++, z, 2); - - chunk.setHeight(y); - - for(let i = y - 1; i >= 0; i--){ - chunk.setBlockSkyLight(x, y, z, 0); + Async(function() { + let distance = this.player.getViewDistance(); + for (let chunkX = -distance; chunkX <= distance; chunkX++) { + for (let chunkZ = -distance; chunkZ <= distance; chunkZ++) { + let chunk = new Chunk(chunkX, chunkZ); + + for (let x = 0; x < 16; x++) { + for (let z = 0; z < 16; z++) { + let y = 0; + chunk.setBlockId(x, y++, z, 7); + chunk.setBlockId(x, y++, z, 3); + chunk.setBlockId(x, y++, z, 3); + chunk.setBlockId(x, y, z, 2); + + /*for (let i = y - 1; i >= 0; i--) { + chunk.setBlockSkyLight(x, y, z, 0); + }*/ } } - } - chunk.recalculateHeightMap(); + chunk.recalculateHeightMap(); + if (chunkX === -distance && chunkZ === -distance) console.log(`${chunk.toBinary().length} > ${chunk.toBinary().toString("hex")}`); - this.player.sendChunk(chunk); + this.player.sendChunk(chunk); + } } - } - - this.player.sendPlayStatus(PlayStatusPacket.PLAYER_SPAWN); - + }.bind(this)) + .then(function(){ + console.log("done sending chunks"); + this.player.sendPlayStatus(PlayStatusPacket.PLAYER_SPAWN); + }.bind(this)); return true; } diff --git a/src/pocketnode/network/RakNetAdapter.js b/src/pocketnode/network/RakNetAdapter.js index 51c4737..7c34d14 100644 --- a/src/pocketnode/network/RakNetAdapter.js +++ b/src/pocketnode/network/RakNetAdapter.js @@ -1,4 +1,4 @@ -const RakNetServer = ((process.argv.indexOf("--local") !== -1 || process.argv.indexOf("-l") !== -1) ? require("../../../../RakNet") : require("raknet")); +const RakNetServer = (global.RUNNING_LOCALLY ? require("../../../../RakNet") : require("raknet")); const Logger = pocketnode("logger/Logger"); @@ -8,8 +8,6 @@ const BatchPacket = pocketnode("network/minecraft/protocol/BatchPacket"); const Player = pocketnode("player/Player"); const PlayerList = pocketnode("player/PlayerList"); -const RakNet = raknet("RakNet"); - class RakNetAdapter { constructor(server){ this.server = server; @@ -39,7 +37,7 @@ class RakNetAdapter { if(packet instanceof BatchPacket){ let session; if((session = this.raknet.getSessionManager().getSessionByIdentifier(identifier))){ - session.queueConnectedPacket(packet, (needACK === true ? RakNet.FLAG_NEED_ACK : 0) | (immediate === true ? RakNet.PRIORITY_IMMEDIATE : RakNet.PRIORITY_NORMAL)); + session.queueConnectedPacketFromServer(packet, needACK, immediate); } return null; }else{ diff --git a/src/pocketnode/network/minecraft/NetworkBinaryStream.js b/src/pocketnode/network/minecraft/NetworkBinaryStream.js new file mode 100644 index 0000000..7f87a52 --- /dev/null +++ b/src/pocketnode/network/minecraft/NetworkBinaryStream.js @@ -0,0 +1,47 @@ +const UUID = pocketnode("utils/UUID"); + +class NetworkBinaryStream extends require("pocketnode-binarystream") { + /** + * @return {string} + */ + readString(){ + return this.read(this.readUnsignedVarInt()).toString(); + } + + /** + * @param v {string} + * @return {NetworkBinaryStream} + */ + writeString(v){ + this.writeUnsignedVarInt(Buffer.byteLength(v)); + if(v.length === 0) return this; + this.append(Buffer.from(v, "utf8")); + return this; + } + + /** + * @return {UUID} + */ + readUUID(){ + let [p1, p0, p3, p2] = [this.readLInt(), this.readLInt(), this.readLInt(), this.readLInt()]; + + return new UUID(p0, p1, p2, p3); + } + + /** + * @param uuid {UUID} + * @return {NetworkBinaryStream} + */ + writeUUID(uuid){ + this.writeLInt(uuid.getPart(1)) + .writeLInt(uuid.getPart(0)) + .writeLInt(uuid.getPart(3)) + .writeLInt(uuid.getPart(2)); + + return this; + } + + // todo everything else +} + +module.exports = NetworkBinaryStream; \ No newline at end of file diff --git a/src/pocketnode/network/minecraft/protocol/BatchPacket.js b/src/pocketnode/network/minecraft/protocol/BatchPacket.js index 90b524c..e8cda20 100644 --- a/src/pocketnode/network/minecraft/protocol/BatchPacket.js +++ b/src/pocketnode/network/minecraft/protocol/BatchPacket.js @@ -1,5 +1,5 @@ const DataPacket = pocketnode("network/minecraft/protocol/DataPacket"); -const BinaryStream = pocketnode("utils/BinaryStream"); +const BinaryStream = pocketnode("network/minecraft/NetworkBinaryStream"); const Zlib = require("zlib"); class BatchPacket extends DataPacket { @@ -62,7 +62,7 @@ class BatchPacket extends DataPacket { getPackets(){ let pks = []; while(!this.payload.feof()){ - pks.push(this.payload.readString(true)); + pks.push(this.payload.read(this.payload.readUnsignedVarInt())); } return pks; } diff --git a/src/pocketnode/network/minecraft/protocol/DataPacket.js b/src/pocketnode/network/minecraft/protocol/DataPacket.js index 85793c0..e2d4675 100644 --- a/src/pocketnode/network/minecraft/protocol/DataPacket.js +++ b/src/pocketnode/network/minecraft/protocol/DataPacket.js @@ -1,4 +1,4 @@ -const BinaryStream = pocketnode("utils/BinaryStream"); +const BinaryStream = pocketnode("network/minecraft/NetworkBinaryStream"); const Vector3 = pocketnode("math/Vector3"); class DataPacket extends BinaryStream { diff --git a/src/pocketnode/network/minecraft/protocol/FullChunkDataPacket.js b/src/pocketnode/network/minecraft/protocol/FullChunkDataPacket.js index 53467b0..569e5a1 100644 --- a/src/pocketnode/network/minecraft/protocol/FullChunkDataPacket.js +++ b/src/pocketnode/network/minecraft/protocol/FullChunkDataPacket.js @@ -20,7 +20,7 @@ class FullChunkDataPacket extends DataPacket { _decodePayload(){ this.chunkX = this.readVarInt(); this.chunkZ = this.readVarInt(); - this.data = this.readString(true); + this.data = this.read(this.readUnsignedVarInt()); } _encodePayload(){ diff --git a/src/pocketnode/network/minecraft/protocol/LoginPacket.js b/src/pocketnode/network/minecraft/protocol/LoginPacket.js index 53f02dc..88a4825 100644 --- a/src/pocketnode/network/minecraft/protocol/LoginPacket.js +++ b/src/pocketnode/network/minecraft/protocol/LoginPacket.js @@ -1,7 +1,7 @@ const DataPacket = pocketnode("network/minecraft/protocol/DataPacket"); const MinecraftInfo = pocketnode("network/minecraft/Info"); -const BinaryStream = pocketnode("utils/BinaryStream"); +const BinaryStream = pocketnode("network/minecraft/NetworkBinaryStream"); const Utils = pocketnode("utils/Utils"); const Isset = pocketnode("utils/methods/Isset"); @@ -49,7 +49,7 @@ class LoginPacket extends DataPacket { return; } - let stream = new BinaryStream(this.readString(true)); + let stream = new BinaryStream(this.read(this.readUnsignedVarInt())); this.chainData = JSON.parse(stream.read(stream.readLInt()).toString()); this.chainData.chain.forEach(chain => { diff --git a/src/pocketnode/network/minecraft/protocol/StartGamePacket.js b/src/pocketnode/network/minecraft/protocol/StartGamePacket.js index 70c7532..f5fc745 100644 --- a/src/pocketnode/network/minecraft/protocol/StartGamePacket.js +++ b/src/pocketnode/network/minecraft/protocol/StartGamePacket.js @@ -40,7 +40,7 @@ class StartGamePacket extends DataPacket { this.hasBonusChestEnabled = false; this.hasStartWithMapEnabled = false; this.hasTrustPlayersEnabled = false; - this.defaultPlayerPermission = 2;//PlayerPermissions::MEMBER; //TODO + this.defaultPlayerPermission = 1;//PlayerPermissions::MEMBER; //TODO this.xboxLiveBroadcastMode = 0; //TODO: find values this.serverChunkTickRadius = 4; diff --git a/src/pocketnode/network/minecraft/protocol/TextPacket.js b/src/pocketnode/network/minecraft/protocol/TextPacket.js index a30e193..f264a69 100644 --- a/src/pocketnode/network/minecraft/protocol/TextPacket.js +++ b/src/pocketnode/network/minecraft/protocol/TextPacket.js @@ -25,6 +25,11 @@ class TextPacket extends DataPacket { this.xuid = ""; } + constructor(){ + super(); + this.initVars(); + } + _decodePayload(){ this.type = this.readByte(); this.needsTranslation = this.readBool(); diff --git a/src/pocketnode/player/Player.js b/src/pocketnode/player/Player.js index 3dd1b24..23154b7 100644 --- a/src/pocketnode/player/Player.js +++ b/src/pocketnode/player/Player.js @@ -469,11 +469,11 @@ class Player extends CommandSender { let pk = new StartGamePacket(); pk.playerGamemode = this.server.getGamemode(); //todo? - pk.playerPosition = new Vector3(0, 10, 0); - pk.seed = 123456; + pk.playerPosition = new Vector3(0, 20, 0); + pk.seed = 0xdeadbeef; pk.generator = 2; pk.levelGamemode = 1; - [pk.spawnX, pk.spawnY, pk.spawnZ] = [0, 10, 0]; + [pk.spawnX, pk.spawnY, pk.spawnZ] = [0, 5, 0]; pk.isMultiplayerGame = true; pk.hasXboxLiveBroadcast = false; pk.hasLANBroadcast = true; @@ -482,7 +482,6 @@ class Player extends CommandSender { pk.hasBonusChestEnabled = false; pk.hasStartWithMapEnabled = false; pk.hasTrustPlayersEnabled = true; - pk.defaultPlayerPermission = 0; pk.xboxLiveBroadcastMode = 0; pk.levelName = this.server.getMotd(); pk.currentTick = this.server.getCurrentTick(); diff --git a/src/pocketnode/utils/Async.js b/src/pocketnode/utils/Async.js new file mode 100644 index 0000000..979e774 --- /dev/null +++ b/src/pocketnode/utils/Async.js @@ -0,0 +1,22 @@ +/** + * Async + * run stuff in async via setImmediate + * @param cb {Function} run this in async + * @return {Promise} + */ +function Async(cb){ + return new Promise((resolve, reject) => { + new Promise((done, fail) => { + setImmediate(() => { + try { + cb(); + done(); + } catch(e) { + fail(e); + } + }); + }).then(() => resolve()).catch(e => reject(e)); + }); +} + +module.exports = Async; \ No newline at end of file diff --git a/src/pocketnode/utils/BinaryStream.js b/src/pocketnode/utils/BinaryStream.js deleted file mode 100644 index cbe752b..0000000 --- a/src/pocketnode/utils/BinaryStream.js +++ /dev/null @@ -1,599 +0,0 @@ -class BinaryStream { - initVars(){ - /** @type {Buffer} */ - this.buffer = Buffer.alloc(0); - /** @type {number} */ - this.offset = 0; - } - - /** - * @param buffer - */ - constructor(buffer){ - this.initVars(); - - if(buffer instanceof Buffer){ - this.append(buffer); - this.offset = 0; - } - } - - read(len){ - return this.buffer.slice(this.offset, this.increaseOffset(len, true)); - } - - reset(){ - this.buffer = Buffer.alloc(0); - this.offset = 0; - } - - setBuffer(buffer = Buffer.alloc(0), offset = 0){ - this.buffer = buffer; - this.offset = offset; - } - - getOffset(){ - return this.offset; - } - - /** - * @return {Buffer} - */ - getBuffer(){ - return this.buffer; - } - - get length(){ - return this.buffer.length; - } - - /** - * @return {number} - */ - getRemainingBytes(){ - return this.buffer.length - this.offset; - } - - /** - * @return {Buffer} - */ - readRemaining(){ - let buf = this.buffer.slice(this.offset); - this.offset = this.buffer.length; - return buf; - } - - /** - * Increases offset - * @param v {number} Value to increase offset by - * @param ret {boolean} Return the new offset - * @return {number} - */ - increaseOffset(v, ret = false){ - return (ret === true ? (this.offset += v) : (this.offset += v) - v); - } - - /** - * Append data to buffer - * @param buf - */ - append(buf){ - if(buf instanceof Buffer){ - this.buffer = Buffer.concat([this.buffer, buf]); - this.offset += buf.length; - }else if(typeof buf === "string"){ - buf = Buffer.from(buf, "hex"); - this.buffer = Buffer.concat([this.buffer, buf]); - this.offset += buf.length; - } - return this; - } - - /** - * Reads a byte boolean - * @return {boolean} - */ - readBool(){ - return this.readByte() !== 0; - } - - /** - * Writes a byte boolean - * @param v {boolean} - * @return {BinaryStream} - */ - writeBool(v){ - this.writeByte(v === true ? 1 : 0); - return this; - } - - /** - * Reads a unsigned/signed byte - * @return {number} - */ - readByte(){ - return this.getBuffer()[this.increaseOffset(1)]; - } - - /** - * Writes a unsigned/signed byte - * @param v {number} - * @returns {BinaryStream} - */ - writeByte(v){ - let buf = Buffer.from([v & 0xff]); - this.append(buf); - - return this; - } - - /** - * Reads a 16-bit unsigned or signed big-endian number - * @return {number} - */ - readShort(){ - return this.buffer.readUInt16BE(this.increaseOffset(2)); - } - - /** - * Writes a 16-bit unsigned big-endian number - * @param v {number} - * @return {BinaryStream} - */ - writeShort(v){ - let buf = Buffer.alloc(2); - buf.writeUInt16BE(v); - this.append(buf); - - return this; - } - - /** - * Reads a 16-bit signed big-endian number - * @return {number} - */ - readSignedShort(){ - return this.buffer.readInt16BE(this.increaseOffset(2)); - } - - /** - * Writes a 16-bit signed big-endian number - * @param v {number} - * @return {BinaryStream} - */ - writeSignedShort(v){ - let buf = Buffer.alloc(2); - buf.writeInt16BE(v); - this.append(buf); - - return this; - } - - /** - * Reads a 16-bit unsigned little-endian number - * @return {number} - */ - readLShort(){ - return this.buffer.readUInt16LE(this.increaseOffset(2)); - } - - /** - * Writes a 16-bit unsigned little-endian number - * @param v {number} - * @return {BinaryStream} - */ - writeLShort(v){ - let buf = Buffer.alloc(2); - buf.writeUInt16BE(v); - this.append(buf); - - return this; - } - - /** - * Reads a 16-bit signed little-endian number - * @return {number} - */ - readSignedLShort(){ - return this.buffer.readInt16LE(this.increaseOffset(2)); - } - - /** - * Writes a 16-bit signed little-endian number - * @param v {number} - * @return {BinaryStream} - */ - writeSignedLShort(v){ - let buf = Buffer.alloc(2); - buf.writeInt16LE(v); - this.append(buf); - - return this; - } - - /** - * Reads a 3-byte big-endian number - * @return {number} - */ - readTriad(){ - return this.buffer.readUIntBE(this.increaseOffset(3), 3); - } - - /** - * Writes a 3-byte big-endian number - * @param v {number} - * @return {BinaryStream} - */ - writeTriad(v){ - let buf = Buffer.alloc(3); - buf.writeUIntBE(v, 0, 3); - this.append(buf); - - return this; - } - - /** - * Reads a 3-byte little-endian number - * @return {number} - */ - readLTriad(){ - return this.buffer.readUIntLE(this.increaseOffset(3), 3); - } - - /** - * Writes a 3-byte little-endian number - * @param v {number} - * @return {BinaryStream} - */ - writeLTriad(v){ - let buf = Buffer.alloc(3); - buf.writeUIntLE(v, 0, 3); - this.append(buf); - - return this; - } - - /** - * Reads a 32-bit signed big-endian number - * @return {number} - */ - readInt(){ - return this.buffer.readInt32BE(this.increaseOffset(4)); - } - - /** - * Writes a 32-bit signed big-endian number - * @param v {number} - * @return {BinaryStream} - */ - writeInt(v){ - let buf = Buffer.alloc(4); - buf.writeInt32BE(v); - this.append(buf); - - return this; - } - - /** - * Reads a 32-bit signed little-endian number - * @return {number} - */ - readLInt(){ - return this.buffer.readInt32LE(this.increaseOffset(4)); - } - - /** - * Writes a 32-bit signed little-endian number - * @param v {number} - * @return {BinaryStream} - */ - writeLInt(v){ - let buf = Buffer.alloc(4); - buf.writeInt32LE(v); - this.append(buf); - - return this; - } - - /** - * @return {number} - */ - readFloat(){ - return this.buffer.readFloatBE(this.increaseOffset(4)); - } - - /** - * @param accuracy {number} - * @return {number} - */ - readRoundedFloat(accuracy){ - return Math.round_php(this.readFloat(), accuracy); - } - - /** - * @param v {number} - * @return {BinaryStream} - */ - writeFloat(v) { - let buf = Buffer.alloc(8); // bc you never know *shrug* - let bytes = buf.writeFloatBE(v); - this.append(buf.slice(0, bytes)); - - return this; - } - - /** - * @return {number} - */ - readLFloat(){ - return this.buffer.readFloatLE(this.increaseOffset(4)); - } - - /** - * @param accuracy {number} - * @return {number} - */ - readRoundedLFloat(accuracy){ - return Math.round_php(this.readLFloat(), accuracy); - } - - /** - * @param v {number} - * @return {BinaryStream} - */ - writeLFloat(v){ - let buf = Buffer.alloc(8); // bc you never know *shrug* - let bytes = buf.writeFloatLE(v); - this.append(buf.slice(0, bytes)); - - return this; - } - - /** - * @return {number} - */ - readDouble(){ - return this.buffer.readDoubleBE(this.increaseOffset(8)); - } - - /** - * @param v {number} - * @return {BinaryStream} - */ - writeDouble(v) { - let buf = Buffer.alloc(8); - buf.writeDoubleBE(v); - this.append(buf); - - return this; - } - - /** - * @return {number} - */ - readLDouble(){ - return this.buffer.readDoubleLE(this.increaseOffset(8)); - } - - /** - * @param v {number} - * @return {BinaryStream} - */ - writeLDouble(v){ - let buf = Buffer.alloc(8); - buf.writeDoubleLE(v); - this.append(buf); - - return this; - } - - /** - * @return {number} - */ - readLong(){ - return (this.buffer.readUInt32BE(this.increaseOffset(4)) << 8) + this.buffer.readUInt32BE(this.increaseOffset(4)); - } - - /** - * @param v {number} - * @return {BinaryStream} - */ - writeLong(v){ - let MAX_UINT32 = 0xFFFFFFFF; - - let buf = Buffer.alloc(8); - buf.writeUInt32BE((~~(v / MAX_UINT32)), 0); - buf.writeUInt32BE((v & MAX_UINT32), 4); - this.append(buf); - - return this; - } - - readLLong(){ - return this.buffer.readUInt32LE(0) + (buffer.readUInt32LE(4) << 8); - } - - writeLLong(v){ - let MAX_UINT32 = 0xFFFFFFFF; - - let buf = Buffer.alloc(8); - buf.writeUInt32LE((v & MAX_UINT32), 0); - buf.writeUInt32LE((~~(v / MAX_UINT32)), 4); - this.append(buf); - - return this; - } - - readString(returnBuffer = false){ - let buffer = this.read(this.readUnsignedVarInt()); - return returnBuffer === true ? buffer : buffer.toString(); - } - - /** - * @param v {string} - * @return {BinaryStream} - */ - writeString(v){ - this.writeUnsignedVarInt(v.length); - - if(v.length === 0) return this; - - let buf = Buffer.alloc(v.length); - buf.write(v); - - this.append(buf); - return this; - } - - // todo: readUUID - // todo: writeUUID - - // todo: readSlot - // todo: writeSlot - - readUnsignedVarInt(){ - let value = 0; - - for(let i = 0; i <= 35; i += 7){ - let b = this.readByte(); - value |= ((b & 0x7f) << i); - - if((b & 0x80) === 0){ - return value; - } - } - - return 0; - } - - writeUnsignedVarInt(v){ - let stream = new BinaryStream(); - while (v !== 0){ - let tmp = v & 0x7f; - v >>>= 7; - if(v !== 0){ - tmp |= 0x80; - } - stream.writeByte(tmp); - } - this.append(stream.buffer); - - return this; - } - - readVarInt(){ - let raw = this.readUnsignedVarInt(); - let tmp = (((raw << 63) >> 63) ^ raw) >> 1; - return tmp ^ (raw & (1 << 63)); - } - - writeVarInt(v){ - v = (v << 32 >> 32); - return this.writeUnsignedVarInt((v << 1) ^ (v >> 31)); - } - - readUnsignedVarLong(){ - let value = 0; - for(let i = 0; i <= 63; i += 7){ - let b = this.readByte(); - value |= ((b & 0x7f) << i); - - if((b & 0x80) === 0){ - return value; - } - } - return 0; - } - - writeUnsignedVarLong(v){ - let stream = new BinaryStream(); - while(v !== 0){ - let tmp = v & 0x7f; - v >>>= 7; - if(v !== 0){ - tmp |= 0x80; - } - stream.writeByte(tmp); - } - this.append(stream.buffer); - - return this; - } - - readVarLong(){ - let raw = this.readUnsignedVarLong(); - let tmp = (((raw << 63) >> 63) ^ raw) >> 1; - return tmp ^ (raw & (1 << 63)); - } - - writeVarLong(v){ - return this.writeUnsignedVarLong((v << 1) ^ (v >> 63)); - } - - /** - * Found end of buffer - * @return {boolean} - */ - feof(){ - return typeof this.getBuffer()[this.offset] === "undefined"; - } - - /** - * Reads address from buffer - * @return {{ip: string, port: number, version: number}} - */ - readAddress(){ - let addr, port; - let version = this.readByte(); - switch(version){ - default: - case 4: - addr = []; - for(let i = 0; i < 4; i++){ - addr.push(this.readByte() & 0xff); - } - addr = addr.join("."); - port = this.readShort(); - break; - // add ipv6 support - } - return {ip: addr, port: port, version: version}; - } - - /** - * Writes address to buffer - * @param addr {string} - * @param port {number} - * @param version {number} - * @return {BinaryStream} - */ - writeAddress(addr, port, version = 4){ - this.writeByte(version); - switch(version){ - default: - case 4: - addr.split(".", 4).forEach(b => { - this.writeByte((Number(b)) & 0xff); - }); - this.writeShort(port); - break; - } - return this; - } - - flip(){ - this.offset = 0; - return this; - } - - /** - * Return hex from buffer - * @param spaces {boolean} - */ - toHex(spaces = false){ - let hex = this.buffer.toString("hex"); - return spaces ? hex.split(/(..)/).filter(v=>{return v !== ""}).join(" ") : hex; - } -} - -module.exports = BinaryStream; \ No newline at end of file diff --git a/src/pocketnode/utils/UUID.js b/src/pocketnode/utils/UUID.js index ad2ae5c..fa7c579 100644 --- a/src/pocketnode/utils/UUID.js +++ b/src/pocketnode/utils/UUID.js @@ -1,4 +1,4 @@ -const BinaryStream = pocketnode("utils/BinaryStream"); +const BinaryStream = pocketnode("network/minecraft/NetworkBinaryStream"); class UUID { initVars(){ @@ -36,6 +36,7 @@ class UUID { return new UUID(stream.readInt(), stream.readInt(), stream.readInt(), stream.readInt(), version); } - - + getPart(i){ + return this._parts[i] ? this._parts[i] : null; + } } \ No newline at end of file diff --git a/src/pocketnode/utils/methods/Globals.js b/src/pocketnode/utils/methods/Globals.js index e3fa684..1c9e13c 100644 --- a/src/pocketnode/utils/methods/Globals.js +++ b/src/pocketnode/utils/methods/Globals.js @@ -229,4 +229,7 @@ global.createInterval = function(fn, interval){ this.stop = () => clearTimeout(this.timer); }); -}; \ No newline at end of file +}; + +global.TRAVIS_BUILD = process.argv.indexOf("--travis-build") !== -1; +global.RUNNING_LOCALLY = (process.argv.indexOf("--local") !== -1 || process.argv.indexOf("-l") !== -1); \ No newline at end of file