From 091ab3af89bfe57fe1b1512dc431ad613048c7cf Mon Sep 17 00:00:00 2001
From: 7cc5c4f330d47060 <samsungipadistaken@outlook.com>
Date: Wed, 12 Feb 2025 23:30:49 -0500
Subject: [PATCH] Implement core refilling when broken

---
 package-lock.json       | 14 +-----
 package.json            |  2 +-
 plugins/chunk.js        | 98 +++++++++++++++++++++++++++++++++++------
 plugins/commandblock.js | 28 ++----------
 4 files changed, 91 insertions(+), 51 deletions(-)

diff --git a/package-lock.json b/package-lock.json
index 02a975e..f0a7c87 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -12,7 +12,7 @@
         "minecraft-protocol": "^1.54.0",
         "prismarine-chat": "^1.10.1",
         "prismarine-chunk": "^1.38.1",
-        "prismarine-world": "^3.6.3"
+        "vec3": "^0.1.10"
       }
     },
     "node_modules/@azure/msal-common": {
@@ -645,18 +645,6 @@
         "prismarine-nbt": "^2.0.0"
       }
     },
-    "node_modules/prismarine-world": {
-      "version": "3.6.3",
-      "resolved": "https://registry.npmjs.org/prismarine-world/-/prismarine-world-3.6.3.tgz",
-      "integrity": "sha512-zqdqPEYCDHzqi6hglJldEO63bOROXpbZeIdxBmoQq7o04Lf81t016LU6stFHo3E+bmp5+xU74eDFdOvzYNABkA==",
-      "license": "MIT",
-      "dependencies": {
-        "vec3": "^0.1.7"
-      },
-      "engines": {
-        "node": ">=8.0.0"
-      }
-    },
     "node_modules/process": {
       "version": "0.11.10",
       "resolved": "https://registry.npmjs.org/process/-/process-0.11.10.tgz",
diff --git a/package.json b/package.json
index d32229e..441509b 100644
--- a/package.json
+++ b/package.json
@@ -17,6 +17,6 @@
     "minecraft-protocol": "^1.54.0",
     "prismarine-chat": "^1.10.1",
     "prismarine-chunk": "^1.38.1",
-    "prismarine-world": "^3.6.3"
+    "vec3": "^0.1.10"
   }
 }
diff --git a/plugins/chunk.js b/plugins/chunk.js
index 2ab3621..89b7fcb 100644
--- a/plugins/chunk.js
+++ b/plugins/chunk.js
@@ -1,17 +1,89 @@
 import { default as c_loader } from 'prismarine-chunk';
-const c = c_loader("1.18")
-import { default as w_loader }  from 'prismarine-world';
-const w = w_loader()
-const rd = 4;
+import Vec3 from 'vec3'
+const rd = 8;
 
-
-//Chunk data parsing placeholder, for core
 export default function load (b) {
-    
-    b.worlds={}
-    b._client.on("map_chunk", payload => {
-        //console.log(payload);
-        //b._chunk = new c();
-        //b._chunk.load(payload.chunkData)
-    })
+  const c = c_loader(b._client.version)
+  b.chunks = {};
+  b._client.on("map_chunk", payload => {
+    //payload.x payload.z
+    if(!b.chunks[payload.x]){
+      b.chunks[payload.x]=[]
+    }
+    let chunk = new c();
+    chunk.load(payload.chunkData)
+    b.chunks[payload.x][payload.z]=chunk 
+  })
+  b._client.on("block_change", payload => {
+    const chunkX = payload.location.x >> 4
+    const chunkZ = payload.location.z >> 4
+    const blockX = payload.location.x & 15
+    const blockZ = payload.location.z & 15
+    if(b.chunks[chunkX] && b.chunks[chunkX][chunkZ]){
+      b.chunks[chunkX][chunkZ].setBlockStateId(new Vec3(blockX,payload.location.y,blockZ),payload.type)
+    }
+  })
+  b._client.on("multi_block_change", payload => {
+    for(const record of payload.records){
+      const blockState = record >> 12
+      const blockX = record >> 8 & 15
+      const blockZ = record >> 4 & 15
+      const blockY = record & 15
+      if(b.chunks[payload.chunkCoordinates.x] && b.chunks[payload.chunkCoordinates.x][payload.chunkCoordinates.z]){
+        b.chunks[payload.chunkCoordinates.x][payload.chunkCoordinates.z].setBlockStateId(new Vec3(blockX,blockY+16*payload.chunkCoordinates.y,blockZ),blockState)
+      }
+    }
+  })
+  b._client.on('position', function (data) {
+    if (!b.ccStarted) {
+      b.original_pos = { x: data.x >> 4, y: data.y, z: data.z >> 4 }
+      b.currentChunk = { x: data.x >> 4, z: data.z >> 4}
+      b.pos = { x: data.x, y: data.y, z: data.z }
+    } else {
+      b.pos = { x: data.x, y: data.y, z: data.z }
+      if (data.x >> 4 !== b.original_pos.x || data.z >> 4 !== b.original_pos.z) {
+        b.original_pos = { x: data.x >> 4, y: data.y, z: data.z >> 4 }
+        b.sc_tasks.cc.failed = 1
+      }
+    }
+    b.commandPos = {
+      x: data.x >> 4,
+      z: data.z >> 4,
+      y: 55
+    }
+    b._client.write('teleport_confirm', { teleportId: data.teleportId })
+  })
+  b.interval.unloadChunks=setInterval(()=>{
+    for(const i in b.chunks){
+      //X-values
+      if(i > b.currentChunk.x + rd || i < b.currentChunk.x - rd){
+        //console.log(`Unloading X value ${i}`)
+        delete b.chunks[i]
+      }
+      for(const z in b.chunks[i]){
+        //Z-values
+        if(z > b.currentChunk.z + rd || z < b.currentChunk.z - rd){
+          //console.log(`Unloading Z value ${z} in X row ${i}`)
+          delete b.chunks[i][z]
+        }
+      }
+    }
+  },1500)
+  b.interval.coreCheck=setInterval(()=>{
+    let cf = false
+    if(!b.currentChunk) return
+    const chunk = b.chunks[b.currentChunk.x][b.currentChunk.z];
+    for(let x=0; x<=15; x++){
+      for(let z=0; z<=15; z++){
+        const blockName = chunk.getBlock(new Vec3(x,55,z)).name
+        if(blockName !== "command_block" && blockName !== "repeating_command_block" && blockName !== "chain_command_block"){
+          //console.log(`Core fault at ${x} ${z}`)
+          cf = true
+          if(b.sc_tasks.cc) b.sc_tasks.cc.failed = true
+          break
+        }
+      }
+      if(cf) break
+    }
+  },500)
 }
diff --git a/plugins/commandblock.js b/plugins/commandblock.js
index 8c0401f..15a8c1f 100755
--- a/plugins/commandblock.js
+++ b/plugins/commandblock.js
@@ -1,15 +1,12 @@
 import uuidToInt from '../util/uuidtoint.js'
 import plainParser from '../util/chatparse_plain.js'
 import mcParser from '../util/chatparse_mc.js'
-const cs = {
+const cs = { // This value will be removed soon, as changing it can break things.
   x: 16,
   y: 1,
   z: 16
 }
 
-const r16 = number => {
-  return Math.floor(number/16)*16
-}
 
 export default function load (b) {
   b.ccq = []
@@ -70,8 +67,8 @@ export default function load (b) {
     })
     if (!b.host.options.useChat) {
       b.add_sc_task('cc', () => {
-        const xstart = r16(b.pos.x);
-        const zstart = r16(b.pos.z);
+        const xstart = Math.floor(b.pos.x/16)*16;
+        const zstart = Math.floor(b.pos.z/16)*16;
         b.chat(`/fill ${xstart} 55 ${zstart} ${xstart + cs.x - 1} 55 ${zstart + cs.z - 1} ${refillPayload}`)
       })
       b.add_sc_task('cc_size', () => {
@@ -95,24 +92,7 @@ export default function load (b) {
       b.sc_tasks.cc_size.failed = 1
     }
   })
-  b._client.on('position', function (data) {
-    if (!b.ccStarted) {
-      b.original_pos = { x: r16(data.x), y: data.y, z: r16(data.z) }
-      b.pos = { x: data.x, y: data.y, z: data.z }
-    } else {
-      b.pos = { x: data.x, y: data.y, z: data.z }
-      if (r16(data.x) !== b.original_pos.x || r16(data.z) !== b.original_pos.z) {
-        b.original_pos = { x: r16(data.x), y: data.y, z: r16(data.z) }
-        b.sc_tasks.cc.failed = 1
-      }
-    }
-    b.commandPos = {
-      x: r16(data.x),
-      z: r16(data.z),
-      y: 55
-    }
-    b._client.write('teleport_confirm', { teleportId: data.teleportId })
-  })
+
 
   b.tellraw = (uuid, message) => {
     let finalname = ''