我已经创建了一个脚本来动态更改字符串以满足我的需求。它应该在字符串中替换标记(“[pid]”,“[dir]”,“[+ X]”)。但是,我发现string.replace()方法存在问题,它会切断我的字符串,留下重要信息。
这是最终字符串的样子(为了便于阅读,请放入代码示例中):
'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);
答案 0 :(得分:1)
默认情况下String.replace在传递字符串时,因为搜索参数只会替换匹配的第一个实例。您可以将搜索参数重新定义为正则表达式(并包含g
参数,即String.replace(/ regex / g,'replace')),或者您可以尝试此方法:
使用.split
和.join
的混合,而非使用.replace
答案 1 :(得分:1)
您的代码有效。它会prompt
破坏结果。看看fiddle。我将结果分配给<textarea>
,您可以将其复制到