style: Escape quotes and backslashes in turtle place methods and connectToInventory for safe Lua string interpolation

This commit is contained in:
MayaTheShy
2026-02-20 02:48:00 -05:00
parent c862b2816c
commit fe44978f6e

View File

@@ -588,7 +588,7 @@ export class Turtle extends EventEmitter {
* Place block in front
*/
async place(text) {
const cmd = text ? `return turtle.place("${text}")` : 'return turtle.place()';
const cmd = text ? `return turtle.place("${text.replace(/\\/g, '\\\\').replace(/"/g, '\\"')}")` : 'return turtle.place()';
const result = await this.exec(cmd);
if (result === true || (Array.isArray(result) && result[0] === true)) {
await this.inspect(); // Auto-inspect to update world map
@@ -600,7 +600,7 @@ export class Turtle extends EventEmitter {
* Place block above
*/
async placeUp(text) {
const cmd = text ? `return turtle.placeUp("${text}")` : 'return turtle.placeUp()';
const cmd = text ? `return turtle.placeUp("${text.replace(/\\/g, '\\\\').replace(/"/g, '\\"')}")` : 'return turtle.placeUp()';
const result = await this.exec(cmd);
if (result === true || (Array.isArray(result) && result[0] === true)) {
await this.inspectUp();
@@ -612,7 +612,7 @@ export class Turtle extends EventEmitter {
* Place block below
*/
async placeDown(text) {
const cmd = text ? `return turtle.placeDown("${text}")` : 'return turtle.placeDown()';
const cmd = text ? `return turtle.placeDown("${text.replace(/\\/g, '\\\\').replace(/"/g, '\\"')}")` : 'return turtle.placeDown()';
const result = await this.exec(cmd);
if (result === true || (Array.isArray(result) && result[0] === true)) {
await this.inspectDown();
@@ -758,9 +758,10 @@ export class Turtle extends EventEmitter {
* Connect to an adjacent inventory peripheral and read its contents
*/
async connectToInventory(side) {
const safeSide = side.replace(/\\/g, '\\\\').replace(/"/g, '\\"');
const result = await this.exec(`
local size = peripheral.call("${side}", "size")
local content = peripheral.call("${side}", "list")
local size = peripheral.call("${safeSide}", "size")
local content = peripheral.call("${safeSide}", "list")
if next(content) == nil then content = nil end
return {size = size, content = content}
`);