From e05ebee02c9b4265a970382a9fb861517d8d5329 Mon Sep 17 00:00:00 2001 From: HittmanA Date: Wed, 28 Feb 2018 11:38:02 -0500 Subject: [PATCH 1/3] Add chat and plugin related stuff - Added event system - Added /say command - Added default join/quit messages --- package-lock.json | 26 ++-------- src/pocketnode/Server.js | 24 ++++++--- src/pocketnode/command/defaults/SayCommand.js | 18 +++++++ src/pocketnode/event/Event.js | 51 +++++++++++++++++++ src/pocketnode/event/EventHandler.js | 37 ++++++++++++++ src/pocketnode/event/TestEvent.js | 21 ++++++++ src/pocketnode/event/player/PlayerEvent.js | 26 ++++++++++ .../event/player/PlayerJoinEvent.js | 45 ++++++++++++++++ .../event/player/PlayerQuitEvent.js | 51 +++++++++++++++++++ src/pocketnode/level/Position.js | 2 +- src/pocketnode/logger/Logger.js | 2 +- src/pocketnode/network/RakNetAdapter.js | 2 +- .../network/minecraft/protocol/DataPacket.js | 2 +- src/pocketnode/player/Player.js | 26 +++++++--- src/pocketnode/player/PlayerList.js | 3 +- 15 files changed, 297 insertions(+), 39 deletions(-) create mode 100644 src/pocketnode/command/defaults/SayCommand.js create mode 100644 src/pocketnode/event/Event.js create mode 100644 src/pocketnode/event/EventHandler.js create mode 100644 src/pocketnode/event/TestEvent.js create mode 100644 src/pocketnode/event/player/PlayerEvent.js create mode 100644 src/pocketnode/event/player/PlayerJoinEvent.js create mode 100644 src/pocketnode/event/player/PlayerQuitEvent.js diff --git a/package-lock.json b/package-lock.json index ea16377..a59c573 100644 --- a/package-lock.json +++ b/package-lock.json @@ -21,14 +21,6 @@ "integrity": "sha512-MKiLiV+I1AA596t9w1sQJ8jkiSr5+ZKi0WKrYGUn6d1Fx+Ij4tIj+m2WMQSGczs5jZVxV339chE8iwk6F64wjA==", "dev": true }, - "bytebuffer": { - "version": "5.0.1", - "resolved": "https://registry.npmjs.org/bytebuffer/-/bytebuffer-5.0.1.tgz", - "integrity": "sha1-WC7qSxqHO20CCkjVjfhfC7ps/d0=", - "requires": { - "long": "3.2.0" - } - }, "catharsis": { "version": "0.8.9", "resolved": "https://registry.npmjs.org/catharsis/-/catharsis-0.8.9.tgz", @@ -38,11 +30,6 @@ "underscore-contrib": "0.3.0" } }, - "dgram": { - "version": "1.0.1", - "resolved": "https://registry.npmjs.org/dgram/-/dgram-1.0.1.tgz", - "integrity": "sha1-N/OyAPgDOl/3WTAwicgc42G2UcM=" - }, "escape-string-regexp": { "version": "1.0.5", "resolved": "https://registry.npmjs.org/escape-string-regexp/-/escape-string-regexp-1.0.5.tgz", @@ -93,11 +80,6 @@ "graceful-fs": "4.1.11" } }, - "long": { - "version": "3.2.0", - "resolved": "https://registry.npmjs.org/long/-/long-3.2.0.tgz", - "integrity": "sha1-2CG3E4yhy1gcFymQ7xTbIAtcR0s=" - }, "marked": { "version": "0.3.7", "resolved": "https://registry.npmjs.org/marked/-/marked-0.3.7.tgz", @@ -119,14 +101,16 @@ "minimist": "0.0.8" } }, + "pocketnode-binarystream": { + "version": "git+https://github.com/PocketNode/PocketNode-BinaryStream.git#c12c26af5a25cdac758fa8e14eeb6bdbff7c9b25" + }, "pocketnode-language": { "version": "git+https://github.com/PocketNode/PocketNode-Language.git#d36c23f448f599636e4f8705bc15adcc6e0fb9cd" }, "raknet": { - "version": "git+https://github.com/PocketNode/RakNet.git#ac7050c8ceb7cf3a7abbfe1448c9f379ff303c7d", + "version": "git+https://github.com/PocketNode/RakNet.git#883fc47dd246392d429e0d9027cf3f53ef6dd81b", "requires": { - "bytebuffer": "5.0.1", - "dgram": "1.0.1" + "pocketnode-binarystream": "git+https://github.com/PocketNode/PocketNode-BinaryStream.git#c12c26af5a25cdac758fa8e14eeb6bdbff7c9b25" } }, "requizzle": { diff --git a/src/pocketnode/Server.js b/src/pocketnode/Server.js index d74707c..227b9bb 100644 --- a/src/pocketnode/Server.js +++ b/src/pocketnode/Server.js @@ -13,6 +13,10 @@ const ConsoleCommandReader = pocketnode("command/ConsoleCommandReader"); const HelpCommand = pocketnode("command/defaults/HelpCommand"); const StopCommand = pocketnode("command/defaults/StopCommand"); const PluginsCommand = pocketnode("command/defaults/PluginsCommand"); +const SayCommand = pocketnode("command/defaults/SayCommand"); + +const EventHandler = pocketnode("event/EventHandler"); +const TestEvent = pocketnode("event/TestEvent"); const Player = pocketnode("player/Player"); const PlayerList = pocketnode("player/PlayerList"); @@ -29,12 +33,12 @@ class Server { this._bannedNames = {}; this._ops = {}; this._whitelist = {}; - + this._running = true; this._stopped = false; this._pluginManager = {}; - + this._scheduler = {}; //todo this._tickCounter = 0; @@ -51,21 +55,22 @@ 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(); + this._eventSystem = new EventHandler(this); this._levels = new Map(); @@ -143,7 +148,6 @@ class Server { } start(){ - //block banned ips this._tickCounter = 0; @@ -158,6 +162,7 @@ class Server { this.getCommandMap().registerCommand(new HelpCommand()); this.getCommandMap().registerCommand(new StopCommand()); this.getCommandMap().registerCommand(new PluginsCommand()); + this.getCommandMap().registerCommand(new SayCommand()); } /** @@ -352,6 +357,7 @@ class Server { } broadcastMessage(message, recipients = this.getOnlinePlayers()){ + this.getLogger().info(message); recipients.forEach(recipient => recipient.sendMessage(message)); return recipients.length; @@ -606,6 +612,10 @@ class Server { onPlayerCompleteLoginSequence(player){ } + + getEventSystem(){ + return this._eventSystem; + } } -module.exports = Server; \ No newline at end of file +module.exports = Server; diff --git a/src/pocketnode/command/defaults/SayCommand.js b/src/pocketnode/command/defaults/SayCommand.js new file mode 100644 index 0000000..030dbff --- /dev/null +++ b/src/pocketnode/command/defaults/SayCommand.js @@ -0,0 +1,18 @@ +const Command = pocketnode("command/Command"); +const TextFormat = pocketnode("utils/TextFormat"); + +class SayCommand extends Command { + constructor(){ + super("say", "Send a message to the server.", "pocketnode.command.say", []); + } + + execute(sender, args){ + let message = ""; + args.forEach(elem => { + message += elem + " "; + }); + sender.getServer().broadcastMessage("§b[SERVER] " + message); + } +} + +module.exports = SayCommand; diff --git a/src/pocketnode/event/Event.js b/src/pocketnode/event/Event.js new file mode 100644 index 0000000..c58c0e4 --- /dev/null +++ b/src/pocketnode/event/Event.js @@ -0,0 +1,51 @@ +/* + * _____ _ _ _ _ _ + * | __ \ | | | | | \ | | | | + * | |__) |__ ___| | _____| |_| \| | ___ __| | ___ + * | ___/ _ \ / __| |/ / _ \ __| . ` |/ _ \ / _` |/ _ \ + * | | | (_) | (__| < __/ |_| |\ | (_) | (_| | __/ + * |_| \___/ \___|_|\_\___|\__|_| \_|\___/ \__,_|\___| + * + * @author PocketNode Team + * @link https://pocketnode.me +*/ +class Event { + + constructor(){ + this.eventName = null; + this.isCancelled = false; + } + + /** + * @return string + */ + getEventName(){ + return this.eventName ? null : this.constructor.name; + } + + /** + * @return bool + * + * @throws Error + */ + isCancelled(){ + //if(!(this instanceof Cancellable)){ + // throw new Error("Event is not Cancellable"); + //} + return this.isCancelled === true; + } + + /** + * @param bool value + * + * @throws Error + */ + setCancelled(value = true){ + //if(!(this instanceof Cancellable)){ + // throw new Error("Event is not Cancellable"); + //} + this.isCancelled = value; + } + +} +module.exports = Event; diff --git a/src/pocketnode/event/EventHandler.js b/src/pocketnode/event/EventHandler.js new file mode 100644 index 0000000..53f91e5 --- /dev/null +++ b/src/pocketnode/event/EventHandler.js @@ -0,0 +1,37 @@ +/* + * _____ _ _ _ _ _ + * | __ \ | | | | | \ | | | | + * | |__) |__ ___| | _____| |_| \| | ___ __| | ___ + * | ___/ _ \ / __| |/ / _ \ __| . ` |/ _ \ / _` |/ _ \ + * | | | (_) | (__| < __/ |_| |\ | (_) | (_| | __/ + * |_| \___/ \___|_|\_\___|\__|_| \_|\___/ \__,_|\___| + * + * @author PocketNode Team + * @link https://pocketnode.me +*/ +const EventEmitter = require('events').EventEmitter; +const Event = pocketnode("event/Event"); + +class EventHandler extends EventEmitter { + + constructor(server){ + super(); + this.server = server; + this.emitter = new EventEmitter(); + } + + callEvent(ev){ + if(typeof ev !== "string" && typeof ev !== "number" && typeof ev !== "boolean" && typeof ev.isEvent !== null){ + this.emitter.emit(ev.getEventName(), ev); + } else { + this.server.getLogger().error("Calling event '" + ev.toString() + "' with non Event object"); + } + } + + getEmitter(){ + return this.emitter; + } + +} + +module.exports = EventHandler; diff --git a/src/pocketnode/event/TestEvent.js b/src/pocketnode/event/TestEvent.js new file mode 100644 index 0000000..e9b77cc --- /dev/null +++ b/src/pocketnode/event/TestEvent.js @@ -0,0 +1,21 @@ +/* + * _____ _ _ _ _ _ + * | __ \ | | | | | \ | | | | + * | |__) |__ ___| | _____| |_| \| | ___ __| | ___ + * | ___/ _ \ / __| |/ / _ \ __| . ` |/ _ \ / _` |/ _ \ + * | | | (_) | (__| < __/ |_| |\ | (_) | (_| | __/ + * |_| \___/ \___|_|\_\___|\__|_| \_|\___/ \__,_|\___| + * + * @author PocketNode Team + * @link https://pocketnode.me +*/ +const Event = pocketnode("event/Event"); + +class TestEvent extends Event{ + + constructor(){ + super(); + } + +} +module.exports = TestEvent; diff --git a/src/pocketnode/event/player/PlayerEvent.js b/src/pocketnode/event/player/PlayerEvent.js new file mode 100644 index 0000000..3c94cca --- /dev/null +++ b/src/pocketnode/event/player/PlayerEvent.js @@ -0,0 +1,26 @@ +/* + * _____ _ _ _ _ _ + * | __ \ | | | | | \ | | | | + * | |__) |__ ___| | _____| |_| \| | ___ __| | ___ + * | ___/ _ \ / __| |/ / _ \ __| . ` |/ _ \ / _` |/ _ \ + * | | | (_) | (__| < __/ |_| |\ | (_) | (_| | __/ + * |_| \___/ \___|_|\_\___|\__|_| \_|\___/ \__,_|\___| + * + * @author PocketNode Team + * @link https://pocketnode.me +*/ +const Event = pocketnode("event/Event"); + +class PlayerEvent extends Event { + + constructor(){ + super(); + this.player; + } + + getPlayer(){ + return this.player; + } + +} +module.exports = PlayerEvent; diff --git a/src/pocketnode/event/player/PlayerJoinEvent.js b/src/pocketnode/event/player/PlayerJoinEvent.js new file mode 100644 index 0000000..334e53b --- /dev/null +++ b/src/pocketnode/event/player/PlayerJoinEvent.js @@ -0,0 +1,45 @@ +/* + * _____ _ _ _ _ _ + * | __ \ | | | | | \ | | | | + * | |__) |__ ___| | _____| |_| \| | ___ __| | ___ + * | ___/ _ \ / __| |/ / _ \ __| . ` |/ _ \ / _` |/ _ \ + * | | | (_) | (__| < __/ |_| |\ | (_) | (_| | __/ + * |_| \___/ \___|_|\_\___|\__|_| \_|\___/ \__,_|\___| + * + * @author PocketNode Team + * @link https://pocketnode.me +*/ +const PlayerEvent = pocketnode("event/player/PlayerEvent"); + +class PlayerJoinEvent extends PlayerEvent { + + + /** + * PlayerJoinEvent constructor. + * + * @param Player player + * @param TextContainer|string joinMessage + */ + constructor(player, joinMessage){ + super(); + this.player = player; + /** @var string|TextContainer */ + this.joinMessage = joinMessage; + } + + /** + * @param string|TextContainer joinMessage + */ + setJoinMessage(joinMessage){ + this.joinMessage = joinMessage; + } + + /** + * @return string|TextContainer + */ + getJoinMessage(){ + return this.joinMessage; + } + +} +module.exports = PlayerJoinEvent; diff --git a/src/pocketnode/event/player/PlayerQuitEvent.js b/src/pocketnode/event/player/PlayerQuitEvent.js new file mode 100644 index 0000000..47ec407 --- /dev/null +++ b/src/pocketnode/event/player/PlayerQuitEvent.js @@ -0,0 +1,51 @@ +/* + * _____ _ _ _ _ _ + * | __ \ | | | | | \ | | | | + * | |__) |__ ___| | _____| |_| \| | ___ __| | ___ + * | ___/ _ \ / __| |/ / _ \ __| . ` |/ _ \ / _` |/ _ \ + * | | | (_) | (__| < __/ |_| |\ | (_) | (_| | __/ + * |_| \___/ \___|_|\_\___|\__|_| \_|\___/ \__,_|\___| + * + * @author PocketNode Team + * @link https://pocketnode.me +*/ +const PlayerEvent = pocketnode("event/player/PlayerEvent"); + +class PlayerQuitEvent extends PlayerEvent{ + + + /** + * @param Player player + * @param TranslationContainer|string quitMessage + * @param string quitReason + */ + constructor(player, quitMessage, quitReason){ + super(); + this.player = player; + this.quitMessage = quitMessage; + this.quitReason = quitReason; + } + + /** + * @param TranslationContainer|string quitMessage + */ + setQuitMessage(quitMessage){ + this.quitMessage = quitMessage; + } + + /** + * @return TranslationContainer|string + */ + getQuitMessage(){ + return this.quitMessage; + } + + /** + * @return string + */ + getQuitReason(){ + return this.quitReason; + } + +} +module.exports = PlayerQuitEvent; diff --git a/src/pocketnode/level/Position.js b/src/pocketnode/level/Position.js index ab29293..790dc80 100644 --- a/src/pocketnode/level/Position.js +++ b/src/pocketnode/level/Position.js @@ -117,4 +117,4 @@ class Position extends Vector3 { } -module.exports = Position; \ No newline at end of file +module.exports = Position; diff --git a/src/pocketnode/logger/Logger.js b/src/pocketnode/logger/Logger.js index 2a6eee9..797ca43 100644 --- a/src/pocketnode/logger/Logger.js +++ b/src/pocketnode/logger/Logger.js @@ -80,4 +80,4 @@ class Logger { } } -module.exports = Logger; \ No newline at end of file +module.exports = Logger; diff --git a/src/pocketnode/network/RakNetAdapter.js b/src/pocketnode/network/RakNetAdapter.js index 51c4737..60addb9 100644 --- a/src/pocketnode/network/RakNetAdapter.js +++ b/src/pocketnode/network/RakNetAdapter.js @@ -93,4 +93,4 @@ class RakNetAdapter { } } -module.exports = RakNetAdapter; \ No newline at end of file +module.exports = RakNetAdapter; diff --git a/src/pocketnode/network/minecraft/protocol/DataPacket.js b/src/pocketnode/network/minecraft/protocol/DataPacket.js index 85793c0..ec4c920 100644 --- a/src/pocketnode/network/minecraft/protocol/DataPacket.js +++ b/src/pocketnode/network/minecraft/protocol/DataPacket.js @@ -176,4 +176,4 @@ class DataPacket extends BinaryStream { } } -module.exports = DataPacket; \ No newline at end of file +module.exports = DataPacket; diff --git a/src/pocketnode/player/Player.js b/src/pocketnode/player/Player.js index 3dd1b24..33b8f08 100644 --- a/src/pocketnode/player/Player.js +++ b/src/pocketnode/player/Player.js @@ -20,6 +20,9 @@ const Skin = pocketnode("entity/Skin"); const TextFormat = pocketnode("utils/TextFormat"); const Base64 = pocketnode("utils/Base64"); +const PlayerJoinEvent = pocketnode("event/player/PlayerJoinEvent"); +const PlayerQuitEvent = pocketnode("event/player/PlayerQuitEvent"); + class Player extends CommandSender { static get SURVIVAL(){return 0} static get CREATIVE(){return 1} @@ -61,7 +64,7 @@ class Player extends CommandSender { this._needACK = {}; } - + constructor(server, clientId, ip, port){ super(server); this.initVars(); @@ -83,7 +86,7 @@ class Player extends CommandSender { isConnected(){ return this._sessionAdapter !== null; } - + static isValidUserName(name){ return name.toLowerCase() !== "rcon" && name.toLowerCase() !== "console" && name.length >= 1 && name.length <= 16 && /[^A-Za-z0-9_ ]/.test(name); } @@ -431,6 +434,14 @@ class Player extends CommandSender { this.spawned = false; + var ev = new PlayerQuitEvent(this, "A Player quit due to " + reason, reason) + this.server.getEventSystem().callEvent(ev); + if(ev.getQuitMessage().length > 0){ + var message = ev.getQuitMessage(); + this.server.broadcastMessage(message); + } else { + this.server.getLogger().warning("Player quit message is blank or null."); + } this.server.getLogger().info(TextFormat.AQUA + this.getName() + TextFormat.WHITE + " (" + this._ip + ":" + this._port + ") has disconnected due to " + reason); if(this.loggedIn){ @@ -493,7 +504,13 @@ class Player extends CommandSender { this.server.addOnlinePlayer(this); this.server.onPlayerCompleteLoginSequence(this); - + var ev = new PlayerJoinEvent(this, "A Player Joined!") + this.server.getEventSystem().callEvent(ev); + if(ev.getJoinMessage().length > 0){ + this.server.broadcastMessage(ev.getJoinMessage()); + } else { + this.server.getLogger().warning("Player join message is blank or null."); + } //this.sendPlayStatus(PlayStatusPacket.PLAYER_SPAWN); } @@ -503,9 +520,7 @@ class Player extends CommandSender { //} //this.resetCraftingGridType(); - message = TextFormat.clean(message, false);//this._removeFormat); - message = message.split("\n"); for(let i in message){ let messagePart = message[i]; @@ -518,7 +533,6 @@ class Player extends CommandSender { this.server.getCommandMap().dispatchCommand(this, messagePart.substr(1)); }else{ let msg = "<:player> :message".replace(":player", this.getName()).replace(":message", messagePart); - this.server.getLogger().info(msg); this.server.broadcastMessage(msg); } } diff --git a/src/pocketnode/player/PlayerList.js b/src/pocketnode/player/PlayerList.js index 6ed9e07..e5001bd 100644 --- a/src/pocketnode/player/PlayerList.js +++ b/src/pocketnode/player/PlayerList.js @@ -1,6 +1,7 @@ const Player = pocketnode("player/Player"); class PlayerList extends Map { + addPlayer(id, player){ CheckTypes([Player, player]); this.set(id, player); @@ -23,4 +24,4 @@ class PlayerList extends Map { } } -module.exports = PlayerList; \ No newline at end of file +module.exports = PlayerList; From 095aa96b7759bab97d2220e8bbdde65fac4dcaba Mon Sep 17 00:00:00 2001 From: HittmanA Date: Thu, 1 Mar 2018 08:04:07 -0500 Subject: [PATCH 2/3] Merge remote-tracking branch 'PocketNode/master' into plugin-events --- package.json | 5 +- src/pocketnode/PocketNode.js | 10 +- src/pocketnode/Server.js | 19 +- src/pocketnode/level/chunk/Chunk.js | 111 ++-- src/pocketnode/level/chunk/SubChunk.js | 30 +- .../network/PlayerSessionAdapter.js | 52 +- src/pocketnode/network/RakNetAdapter.js | 6 +- .../network/minecraft/NetworkBinaryStream.js | 47 ++ .../network/minecraft/protocol/BatchPacket.js | 4 +- .../network/minecraft/protocol/DataPacket.js | 2 +- .../minecraft/protocol/FullChunkDataPacket.js | 2 +- .../network/minecraft/protocol/LoginPacket.js | 4 +- .../minecraft/protocol/StartGamePacket.js | 2 +- .../network/minecraft/protocol/TextPacket.js | 5 + src/pocketnode/player/Player.js | 7 +- src/pocketnode/utils/Async.js | 22 + src/pocketnode/utils/BinaryStream.js | 599 ------------------ src/pocketnode/utils/UUID.js | 7 +- src/pocketnode/utils/methods/Globals.js | 5 +- 19 files changed, 205 insertions(+), 734 deletions(-) create mode 100644 src/pocketnode/network/minecraft/NetworkBinaryStream.js create mode 100644 src/pocketnode/utils/Async.js delete mode 100644 src/pocketnode/utils/BinaryStream.js 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 227b9bb..84e99d2 100644 --- a/src/pocketnode/Server.js +++ b/src/pocketnode/Server.js @@ -27,7 +27,7 @@ const SFS = pocketnode("utils/SimpleFileSystem"); class Server { initVars(){ - this.PocketNode = {}; + this._pocketnode = {}; this._bannedIps = {}; this._bannedNames = {}; @@ -77,10 +77,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; @@ -152,7 +153,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(); @@ -188,21 +189,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; } /** @@ -223,7 +224,7 @@ class Server { * @return {string} */ getApiVersion(){ - return this.PocketNode.API_VERSION; + return this._pocketnode.API_VERSION; } /** @@ -311,7 +312,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 60addb9..5748157 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 ec4c920..041be7b 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 33b8f08..9d26fbb 100644 --- a/src/pocketnode/player/Player.js +++ b/src/pocketnode/player/Player.js @@ -480,11 +480,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; @@ -493,7 +493,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 From 32bf489282ee174a31dde89e192fbb4236e5bd85 Mon Sep 17 00:00:00 2001 From: HittmanA Date: Thu, 1 Mar 2018 08:07:38 -0500 Subject: [PATCH 3/3] Git is stupid! --- package-lock.json | 8 -------- 1 file changed, 8 deletions(-) diff --git a/package-lock.json b/package-lock.json index 93d9f29..743aa2a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -102,11 +102,7 @@ } }, "pocketnode-binarystream": { -<<<<<<< HEAD - "version": "git+https://github.com/PocketNode/PocketNode-BinaryStream.git#c12c26af5a25cdac758fa8e14eeb6bdbff7c9b25" -======= "version": "git+https://github.com/PocketNode/PocketNode-BinaryStream.git#1408d60c3cdea5cacb94a6d6a20b2c884678bf32" ->>>>>>> PocketNode/master }, "pocketnode-language": { "version": "git+https://github.com/PocketNode/PocketNode-Language.git#d36c23f448f599636e4f8705bc15adcc6e0fb9cd" @@ -114,11 +110,7 @@ "raknet": { "version": "git+https://github.com/PocketNode/RakNet.git#883fc47dd246392d429e0d9027cf3f53ef6dd81b", "requires": { -<<<<<<< HEAD - "pocketnode-binarystream": "git+https://github.com/PocketNode/PocketNode-BinaryStream.git#c12c26af5a25cdac758fa8e14eeb6bdbff7c9b25" -======= "pocketnode-binarystream": "git+https://github.com/PocketNode/PocketNode-BinaryStream.git#1408d60c3cdea5cacb94a6d6a20b2c884678bf32" ->>>>>>> PocketNode/master } }, "requizzle": {