gpt4 book ai didi

javascript - 为什么 String.replace 会缩短我的字符串?

转载 作者:行者123 更新时间:2023-11-29 21:46:02 25 4
gpt4 key购买 nike

我制作了一个脚本来动态更改字符串以满足我的需要。它应该可以替换字符串中的标记 ("[pid]","[dir]","[+X]")。但是,我发现 string.replace() 方法存在问题,该方法会 chop 我的字符串,留下重要信息。

这是最终字符串的样子(放入代码示例以提高可读性):

'summon FallingSand ~ ~4 ~ {Time:1,Data:0,TileID:152,Riding:{id:FallingSand,DropItem:0b,Time:1,Block:minecraft:command_block,TileEntityData:{Command:fill ~ ~1 ~-1 ~ ~-5 ~ air},Riding:{id:FallingSand,DropItem:0b,Time:1,Data:0,TileID:1,Riding:{id:FallingSand,DropItem:0b,Time:1,Data:0,TileID:152,Riding:{id:FallingSand,DropItem:0b,Time:1,Block:minecraft:command_block,TileEntityData:{Command:kill @e[type=MinecartCommandBlock,r=4]},Riding:{id:FallingSand,DropItem:0b,Time:1,Data:0,TileID:1,Riding:{id:MinecartCommandBlock,Command:setblock ~ ~ ~ air,Riding:{id:MinecartCommandBlock,Command:/kill @e[type=Item,r=4],Riding:{id:MinecartCommandBlock,Command:/tellraw @a {text:"Thanks to gentlegiantJGC and xafonyz for making the MCedit filter to make this possible",color:gold},Riding:{id:MinecartCommandBlock,Command:"setblock ~2 ~-2 ~-3 minecraft:redstone_block 0 destroy",Riding:{id:MinecartCommandBlock,Command:"setblock ~3 ~-2 ~-3 minecraft:redstone_block 0 destroy",Riding:{id:MinecartCommandBlock,Command:"setblock ~4 ~-2 ~-3 minecraft:redstone_block 0 destroy",Riding:{id:MinecartCommandBlock,Command:buffer,Riding:{id:MinecartCommandBlock,Command:buffer,Riding:{id:MinecartCommandBlock,Command:"setblock ~2 ~-2 ~-4 minecraft:command_block 1 replace {CustomName:\"@\",Command:\"/execute @a ~ ~ ~ /execute @e[type=ArmorStand,name=ParticleEmitterDisplay,score_pe_particle_min=3,score_pe_particle=3,score_pe_dir_min=8,score_pe_dir=8,score_pe_ctrl5_min=1,score_pe_ctr5=1,r=32] 0 -2 0 particle smoke ~ ~ ~ 0 -10 0 0.1 0 force\",x:41,y:1,z:-79,id:\"Control\"}",Riding:{id:MinecartCommandBlock,Command:"setblock ~2 ~-2 ~-3 minecraft:redstone_block 0 destroy",Riding:{id:MinecartCommandBlock,Command:"setblock ~2 ~-2 ~-2 minecraft:command_block 1 replace {CustomName:\"@\",Command:\"/execute @a ~ ~ ~ /execute @e[type=ArmorStand,name=ParticleEmitterDisplay,score_pe_particle_min=3,score_pe_particle=3,score_pe_dir_min=9,score_pe_dir=9,score_pe_ctrl5_min=1,score_pe_ctr5=1,r=32] 0 3 0 particle smoke ~ ~ ~ 0 10 0 0.1 0 force\",x:41,y:1,z:-77,id:\"Control\"}",Riding:{id:MinecartCommandBlock,Command:"setblock ~3 ~-2 ~-4 minecraft:command_block 1 replace {CustomName:\"@\",Command:\"/execute @a ~ ~ ~ /execute @e[type=ArmorStand,name=ParticleEmitterDisplay,score_pe_particle_min=3,score_pe_particle=3,score_pe_dir_min=10,score_pe_dir=10,score_pe_ctrl5_min=1,score_pe_ctr5=1,r=32] 0 0.5 -2 particle smoke ~ ~ ~ 0 0 -10 0.1 0 force\",x:42,y:1,z:-79,id:\"Control\"}",Riding:{id:MinecartCommandBlock,Command:"setblock ~3 ~-2 ~-3 minecraft:redstone_block 0 destroy",Riding:{id:MinecartCommandBlock,Command:"setblock ~3 ~-2 ~-2 minecraft:command_block 1 replace {CustomName:\"@\",Command:\"/execute @a ~ ~ ~ /execute @e[type=ArmorStand,name=ParticleEmitterDisplay,score_pe_particle_min=3,score_pe_particle=3,score_pe_dir_min=11,score_pe_dir=11,score_pe_ctrl5_min=1,score_pe_ctr5=1,r=32] 0 0.5 2 particle smoke ~ ~ ~ 0 0 10 0.1 0 force\",x:42,y:1,z:-77,id:\"Control\"}",Riding:{id:MinecartCommandBlock,Command:"setblock ~4 ~-2 ~-4 minecraft:command_block 1 replace {CustomName:\"@\",Command:\"/execute @a ~ ~ ~ /execute @e[type=ArmorStand,name=ParticleEmitterDisplay,score_pe_particle_min=3,score_pe_particle=3,score_pe_dir_min=13,score_pe_dir=13,score_pe_ctrl5_min=1,score_pe_ctr5=1,r=32] 2 0.5 0 particle smoke ~ ~ ~ 10 0 0 0.1 0 force\",x:43,y:1,z:-79,id:\"Control\"}",Riding:{id:MinecartCommandBlock,Command:"setblock ~4 ~-2 ~-3 minecraft:redstone_block 0 destroy",Riding:{id:MinecartCommandBlock,Command:"setblock ~4 ~-2 ~-2 minecraft:command_block 1 replace {CustomName:\"@\",Command:\"/execute @a ~ ~ ~ /execute @e[type=ArmorStand,name=ParticleEmitterDisplay,score_pe_particle_min=3,score_pe_particle=3,score_pe_dir_min=12,score_pe_dir=12,score_pe_ctrl5_min=1,score_pe_ctr5=1,r=32] -2 0.5 0 particle smoke ~ ~ ~ -10 0 0 0.1 0 force\",x:43,y:1,z:-77,id:\"Control\"}",Riding:{id:MinecartCommandBlock,Command:buffer,Riding:{id:FallingSand,DropItem:0b,Time:1,Data:0,TileID:157,Riding:{id:FallingSand,DropItem:0b,Time:1,Data:0,TileID:152,Riding:{id:Item,Age:5996,Motion:[0.0,0.0,-0.1]}}}}}}}}}}}}}}}}}}}}}}}}}}}'

然而,这是它的样子:

'summon FallingSand ~ ~4 ~ {Time:1,Data:0,TileID:152,Riding:{id:FallingSand,DropItem:0b,Time:1,Block:minecraft:command_block,TileEntityData:{Command:fill ~ ~1 ~-1 ~ ~-5 ~ air},Riding:{id:FallingSand,DropItem:0b,Time:1,Data:0,TileID:1,Riding:{id:FallingSand,DropItem:0b,Time:1,Data:0,TileID:152,Riding:{id:FallingSand,DropItem:0b,Time:1,Block:minecraft:command_block,TileEntityData:{Command:kill @e[type=MinecartCommandBlock,r=4]},Riding:{id:FallingSand,DropItem:0b,Time:1,Data:0,TileID:1,Riding:{id:MinecartCommandBlock,Command:setblock ~ ~ ~ air,Riding:{id:MinecartCommandBlock,Command:/kill @e[type=Item,r=4],Riding:{id:MinecartCommandBlock,Command:/tellraw @a {text:"Thanks to gentlegiantJGC and xafonyz for making the MCedit filter to make this possible",color:gold},Riding:{id:MinecartCommandBlock,Command:"setblock ~2 ~-2 ~-3 minecraft:redstone_block 0 destroy",Riding:{id:MinecartCommandBlock,Command:"setblock ~3 ~-2 ~-3 minecraft:redstone_block 0 destroy",Riding:{id:MinecartCommandBlock,<b>Com...</b> ~ ~ /execute @e[type=ArmorStand,name=ParticleEmitterDisplay,score_pe_particle_min=3,score_pe_particle=3,score_pe_dir_min=12,score_pe_dir=12,score_pe_ctrl5_min=1,score_pe_ctr5=1,r=32] -2 0.5 0 particle smoke ~ ~ ~ -10 0 0 0.1 0 force",x:43,y:1,z:-79,id:"Control"}",Riding:{id:MinecartCommandBlock,Command:"setblock ~4 ~-2 ~-3 minecraft:redstone_block 0 destroy",Riding:{id:MinecartCommandBlock,Command:"setblock ~4 ~-2 ~-2 minecraft:command_block 1 replace {CustomName:"@",Command:"/execute @a ~ ~ ~ /execute @e[type=ArmorStand,name=ParticleEmitterDisplay,score_pe_particle_min=3,score_pe_particle=3,score_pe_dir_min=13,score_pe_dir=13,score_pe_ctrl5_min=1,score_pe_ctr5=1,r=32] 2 0.5 0 particle smoke ~ ~ ~ 10 0 0 0.1 0 force",x:43,y:1,z:-77,id:"Control"}",Riding:{id:MinecartCommandBlock,Command:buffer,Riding:{id:FallingSand,DropItem:0b,Time:1,Data:0,TileID:157,Riding:{id:FallingSand,DropItem:0b,Time:1,Data:0,TileID:152,Riding:{id:Item,Age:5996,Motion:[0.0,0.0,-0.1]}}}}}}}}}}}}}}}}}}}}}}}}}}}'

这是一个很大的不同,它让我无法使用它。任何已知的解决方法?

这是整个脚本:

var pid = 3;
var particle = "smoke";
var freq = 5;
var baseCommand = 'summon FallingSand ~ ~4 ~ {Time:1,Data:0,TileID:152,Riding:{id:FallingSand,DropItem:0b,Time:1,Block:minecraft:command_block,TileEntityData:{Command:fill ~ ~1 ~-1 ~ ~-5 ~ air},Riding:{id:FallingSand,DropItem:0b,Time:1,Data:0,TileID:1,Riding:{id:FallingSand,DropItem:0b,Time:1,Data:0,TileID:152,Riding:{id:FallingSand,DropItem:0b,Time:1,Block:minecraft:command_block,TileEntityData:{Command:kill @e[type=MinecartCommandBlock,r=4]},Riding:{id:FallingSand,DropItem:0b,Time:1,Data:0,TileID:1,Riding:{id:MinecartCommandBlock,Command:setblock ~ ~ ~ air,Riding:{id:MinecartCommandBlock,Command:/kill @e[type=Item,r=4],Riding:{id:MinecartCommandBlock,Command:/tellraw @a {text:"Thanks to gentlegiantJGC and xafonyz for making the MCedit filter to make this possible",color:gold},Riding:{id:MinecartCommandBlock,Command:"setblock ~2 ~-2 ~-3 minecraft:redstone_block 0 destroy",Riding:{id:MinecartCommandBlock,Command:"setblock ~3 ~-2 ~-3 minecraft:redstone_block 0 destroy",Riding:{id:MinecartCommandBlock,Command:"setblock ~4 ~-2 ~-3 minecraft:redstone_block 0 destroy",Riding:{id:MinecartCommandBlock,Command:buffer,Riding:{id:MinecartCommandBlock,Command:buffer,Riding:{id:MinecartCommandBlock,Command:"setblock ~2 ~-2 ~-4 minecraft:command_block 1 replace {CustomName:\"@\",Command:\"[-Y]\",x:41,y:1,z:-79,id:\"Control\"}",Riding:{id:MinecartCommandBlock,Command:"setblock ~2 ~-2 ~-3 minecraft:redstone_block 0 destroy",Riding:{id:MinecartCommandBlock,Command:"setblock ~2 ~-2 ~-2 minecraft:command_block 1 replace {CustomName:\"@\",Command:\"[+Y]\",x:41,y:1,z:-77,id:\"Control\"}",Riding:{id:MinecartCommandBlock,Command:"setblock ~3 ~-2 ~-4 minecraft:command_block 1 replace {CustomName:\"@\",Command:\"[-Z]\",x:42,y:1,z:-79,id:\"Control\"}",Riding:{id:MinecartCommandBlock,Command:"setblock ~3 ~-2 ~-3 minecraft:redstone_block 0 destroy",Riding:{id:MinecartCommandBlock,Command:"setblock ~3 ~-2 ~-2 minecraft:command_block 1 replace {CustomName:\"@\",Command:\"[+Z]\",x:42,y:1,z:-77,id:\"Control\"}",Riding:{id:MinecartCommandBlock,Command:"setblock ~4 ~-2 ~-4 minecraft:command_block 1 replace {CustomName:\"@\",Command:\"[-X]\",x:43,y:1,z:-79,id:\"Control\"}",Riding:{id:MinecartCommandBlock,Command:"setblock ~4 ~-2 ~-3 minecraft:redstone_block 0 destroy",Riding:{id:MinecartCommandBlock,Command:"setblock ~4 ~-2 ~-2 minecraft:command_block 1 replace {CustomName:\"@\",Command:\"[+X]\",x:43,y:1,z:-77,id:\"Control\"}",Riding:{id:MinecartCommandBlock,Command:buffer,Riding:{id:FallingSand,DropItem:0b,Time:1,Data:0,TileID:157,Riding:{id:FallingSand,DropItem:0b,Time:1,Data:0,TileID:152,Riding:{id:Item,Age:5996,Motion:[0.0,0.0,-0.1]}}}}}}}}}}}}}}}}}}}}}}}}}}}';
var baseParticleCmd = '/execute @a ~ ~ ~ /execute @e[type=ArmorStand,name=ParticleEmitterDisplay,score_pe_particle_min=[pid],score_pe_particle=[pid],score_pe_dir_min=[dir],score_pe_dir=[dir],score_pe_ctrl[freq]_min=1,score_pe_ctr[freq]=1,r=32] [rx] [ry] [rz] particle [particle] ~ ~ ~ [dx] [dy] [dz] 0.1 0 force';

var dirIndex = {
"8": "-Y",
"9": "+Y",
"10": "-Z",
"11": "+Z",
"12": "-X",
"13": "+X"
};

function replace(str,v1,v2) {
while(str.indexOf(v1) > -1){
str = str.replace(v1,v2);
}
return str;
}

for(var dir = 8; dir <= 13; dir++){
var cmd = baseParticleCmd;
var rx = 0;
var ry = 0.5;
var rz = 0;

var dx = 0;
var dy = 0;
var dz = 0;

var axis = dirIndex["" + dir + ""];
if(axis.charAt(1) === "X"){
rx = (axis.charAt(0) + "2") - 0;
dx = (axis.charAt(0) + "10") - 0;
} else if(axis.charAt(1) === "Y"){
ry = (axis.charAt(0) + "2") - 0;
if(axis.charAt(0) === "+"){
ry = 3;
}
dy = (axis.charAt(0) + "10") - 0;
} else if(axis.charAt(1) === "Z"){
rz = (axis.charAt(0) + "2") - 0;
dz = (axis.charAt(0) + "10") - 0;
}

cmd = replace(cmd,"[pid]",pid);
cmd = replace(cmd,"[freq]",freq);
cmd = replace(cmd,"[dir]",dir);
cmd = replace(cmd,"[rx]",rx);
cmd = replace(cmd,"[ry]",ry);
cmd = replace(cmd,"[rz]",rz);
cmd = replace(cmd,"[dx]",dx);
cmd = replace(cmd,"[dy]",dy);
cmd = replace(cmd,"[dz]",dz);
cmd = replace(cmd,"[particle]",particle);
baseCommand = baseCommand.replace(("[" + axis + "]"),cmd);
}

prompt("Command:",baseCommand);

最佳答案

您的代码有效。这是prompt这破坏了结果。看看fiddle .我将结果分配给 <textarea>你可以在哪里复制它

关于javascript - 为什么 String.replace 会缩短我的字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31152490/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com