style: Escape quotes and backslashes in turtle place methods and connectToInventory for safe Lua string interpolation
This commit is contained in:
@@ -588,7 +588,7 @@ export class Turtle extends EventEmitter {
|
|||||||
* Place block in front
|
* Place block in front
|
||||||
*/
|
*/
|
||||||
async place(text) {
|
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);
|
const result = await this.exec(cmd);
|
||||||
if (result === true || (Array.isArray(result) && result[0] === true)) {
|
if (result === true || (Array.isArray(result) && result[0] === true)) {
|
||||||
await this.inspect(); // Auto-inspect to update world map
|
await this.inspect(); // Auto-inspect to update world map
|
||||||
@@ -600,7 +600,7 @@ export class Turtle extends EventEmitter {
|
|||||||
* Place block above
|
* Place block above
|
||||||
*/
|
*/
|
||||||
async placeUp(text) {
|
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);
|
const result = await this.exec(cmd);
|
||||||
if (result === true || (Array.isArray(result) && result[0] === true)) {
|
if (result === true || (Array.isArray(result) && result[0] === true)) {
|
||||||
await this.inspectUp();
|
await this.inspectUp();
|
||||||
@@ -612,7 +612,7 @@ export class Turtle extends EventEmitter {
|
|||||||
* Place block below
|
* Place block below
|
||||||
*/
|
*/
|
||||||
async placeDown(text) {
|
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);
|
const result = await this.exec(cmd);
|
||||||
if (result === true || (Array.isArray(result) && result[0] === true)) {
|
if (result === true || (Array.isArray(result) && result[0] === true)) {
|
||||||
await this.inspectDown();
|
await this.inspectDown();
|
||||||
@@ -758,9 +758,10 @@ export class Turtle extends EventEmitter {
|
|||||||
* Connect to an adjacent inventory peripheral and read its contents
|
* Connect to an adjacent inventory peripheral and read its contents
|
||||||
*/
|
*/
|
||||||
async connectToInventory(side) {
|
async connectToInventory(side) {
|
||||||
|
const safeSide = side.replace(/\\/g, '\\\\').replace(/"/g, '\\"');
|
||||||
const result = await this.exec(`
|
const result = await this.exec(`
|
||||||
local size = peripheral.call("${side}", "size")
|
local size = peripheral.call("${safeSide}", "size")
|
||||||
local content = peripheral.call("${side}", "list")
|
local content = peripheral.call("${safeSide}", "list")
|
||||||
if next(content) == nil then content = nil end
|
if next(content) == nil then content = nil end
|
||||||
return {size = size, content = content}
|
return {size = size, content = content}
|
||||||
`);
|
`);
|
||||||
|
|||||||
Reference in New Issue
Block a user