From fdd081e8394724a2dd18b1d405e4cf63ca4dd0ad Mon Sep 17 00:00:00 2001
From: RichardDorian <richard.dorian@outlook.com>
Date: Tue, 5 Apr 2022 08:34:14 +0200
Subject: [PATCH] feat: :sparkles: add booleans

---
 README.md            |  2 ++
 package.json         |  2 +-
 src/index.ts         | 31 +++++++++++++++++++++++++++++++
 test/boolean.test.js | 26 ++++++++++++++++++++++++++
 4 files changed, 60 insertions(+), 1 deletion(-)
 create mode 100644 test/boolean.test.js

diff --git a/README.md b/README.md
index d72fb62..9433ef0 100644
--- a/README.md
+++ b/README.md
@@ -17,6 +17,8 @@ Encode and decode:
 - Array of strings
 - Array of ints
 - UUID
+- Byte
+- Boolean
   
 ⚠️ They are all signed and big endian (As Java does)
 
diff --git a/package.json b/package.json
index 29298f3..4969ebe 100644
--- a/package.json
+++ b/package.json
@@ -1,6 +1,6 @@
 {
   "name": "@minecraft-js/bufwrapper",
-  "version": "1.2.1",
+  "version": "1.2.2",
   "description": "Encode and decode data using buffers",
   "main": "dist/index.js",
   "scripts": {
diff --git a/src/index.ts b/src/index.ts
index 71652bc..6542f71 100644
--- a/src/index.ts
+++ b/src/index.ts
@@ -297,4 +297,35 @@ export default class BufWrapper {
     this.offset += length;
     return value;
   }
+
+  /**
+   * Write a boolean to the buffer
+   * @param value The value to write (boolean)
+   * @example
+   * ```javascript
+   * const buf = new BufWrapper();
+   * buf.writeBoolean(true);
+   * console.log(buf.buffer); // <Buffer 01>
+   * ```
+   */
+  public writeBoolean(value: boolean): void {
+    this.buffer = Buffer.concat([this.buffer, Buffer.from([value ? 1 : 0])]);
+  }
+
+  /**
+   * Read a boolean from the buffer
+   * @returns The boolean read from the buffer
+   * @example
+   * ```javascript
+   * const buffer = Buffer.from([ 0x01 ]);
+   * const buf = new BufWrapper(buffer);
+   * const decoded = buf.readBoolean();
+   * console.log(decoded); // true
+   * ```
+   */
+  public readBoolean(): boolean {
+    const value = this.buffer.readUInt8(this.offset) === 1;
+    this.offset += 1;
+    return value;
+  }
 }
diff --git a/test/boolean.test.js b/test/boolean.test.js
new file mode 100644
index 0000000..0cb8125
--- /dev/null
+++ b/test/boolean.test.js
@@ -0,0 +1,26 @@
+const { assert } = require('chai');
+const BufWrapper = require('../dist').default;
+
+describe('Boolean', () => {
+  it('Write 1', () => {
+    const buf = new BufWrapper();
+    buf.writeBoolean(true);
+    assert.equal(buf.buffer.toString('hex'), '01');
+  });
+
+  it('Write 2', () => {
+    const buf = new BufWrapper();
+    buf.writeBoolean(false);
+    assert.equal(buf.buffer.toString('hex'), '00');
+  });
+
+  it('Read 1', () => {
+    const buf = new BufWrapper(Buffer.from([0x01]));
+    assert.equal(buf.readBoolean(), true);
+  });
+
+  it('Read 2', () => {
+    const buf = new BufWrapper(Buffer.from([0x00]));
+    assert.equal(buf.readBoolean(), false);
+  });
+});