2

I've made a script to dynamically change a string to fit my needs. It's supposed to work replacing markers ("[pid]","[dir]","[+X]") inside the string. However, I've found an issue with the string.replace() method that cuts my string, leaving important information behind.

Here is what the final string should look like (Put into code sample for readability):

'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]}}}}}}}}}}}}}}}}}}}}}}}}}}}'

However, this is how it looks like:

'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]}}}}}}}}}}}}}}}}}}}}}}}}}}}'

This is a big difference and it makes me unable to work with this. Any known workaround?

Here is the whole script:

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);

4
  • Please format your code properly. Ctrl-K or the button {}. Commented Jul 1, 2015 at 4:23
  • Why the while loop in your replace() function? Why not just use the "g" flag with a regex? Commented Jul 1, 2015 at 4:32
  • Phrase your headline as a question. Commented Jul 1, 2015 at 4:32
  • Followed 1999 and php_purest's suggestions. jfriend00: I guess I could use that, but I have no experience with RegExp. I tried to use it, but it gave some unexpected behavior that I wasn't willing to explore, although I accept any suggestions. Commented Jul 1, 2015 at 4:38

2 Answers 2

1

Your code works. It's prompt that spoils the result. Take a look in fiddle. I assign result to <textarea> where you can copy it

Sign up to request clarification or add additional context in comments.

Comments

1

by default String.replace when passed a string as the search param will only replace the first instance of a match. You could either redefine your search param as regex (and include the g param, i.e. String.replace(/regex/g, 'replace')) or you could try the method here:

JavaScript Non-regex Replace

which uses a mix of .split and .join rather than using .replace

2 Comments

I would just go with the regex - simpler and probably just as fast.
Although it is a good suggestion it is not the solution for original question

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.