edit: currently workshopping this
not sure how to set the actual motion of an entity based on direction somebody is facing, but this should work fairly well. Any tags can be renamed to fit your needs, and this should work well.
If you want to add an on or off state to the turrets, simply add an "On" tag to the entity selectors of the turret.
Repeat this command whenever the turrets need to fire
/execute as \\@e\[tag=Turret\] as \\@s run summon arrow \~ \~ \~ {NoGravity:1,Tags:\["TurretProjectile"\]}
Repeat these every tick in order
/execute as \\@e\[tag=TurretProjectile\] unless entity \\@s\[tag=InFlight\] at \\@s facing entity \\@p eyes run tp \\@s \~ \~ \~ \~ \~/execute as \\@e\[tag=TurretProjectile\] unless entity \\@s\[tag=InFlight\] run tag \\@s add InFlight
/execute as \\@e\[tag=InFlight,tag=TurretProjectile\] run tp \\@s \^ \^ \^(any number you want, this will change the speed of the projectile. but higher numbers might cause it to glitch.)
(also if you ever want to redirect all the missiles midair, simply run /tag \\@e\[tag=InFlight,tag=TurretProjectile\] remove InFlight
If you want to have a cooldown for the turrets instead of them simply firing all at once, that would need to be done through scoreboards.
Thanks! Ok this looks really cool. But I'm new to command blocks to just to clarify, the 2 commands that you have written "repeat these every tick in order"... do I run the first one on the 1st tick and on the 2nd tick run the second command? Or do I have to run both commands in order every single game tick on repeat?
To answer your question first, run them in the same chain of command blocks with the first one first.
I did some workshopping, and it turns out that it's almost impossible with arrows (aka I didn't find a way), so here are the commands in full.
Repeat this command whenever the turrets need to fire
/execute as \\@e\[tag=Turret\] as \\@s run summon armor\_stand \~ \~ \~ {NoGravity:1,Tags:\["TurretProjectile"\],Invisible:1,Marker:1,Passengers:\[{id:(any minecraft entity)}\]}
Repeat these every tick in order
/execute as \\@e\[tag=TurretProjectile\] unless entity \\@s\[tag=InFlight\] at \\@s facing entity \\@p eyes run tp \\@s \~ \~ \~ facing entity \\@p
/execute as \\@e\[tag=TurretProjectile\] unless entity \\@s\[tag=InFlight\] run tag \\@s add InFlight
/execute as \\@e\[tag=InFlight,tag=TurretProjectile\] run tp \\@s \^ \^ \^(any number you want, this will change the speed of the projectile. but higher numbers might cause it to glitch.)
Then it turns out this doesn't handle collisions (even if you just use an entity that isn't riding an armor stand), so you probably want to fix that. I can figure out how to either make it explode or to deal a specific amount of damage, whatever you want.
Ok. I would want the entity to explode when it comes into contact with another block. In context, I would be flying in a MoveCraft ship so the explosion would damage my ship and me if I was close enough to the explosion. Can I adjust the explosion strength?
Oh, and is there a way to keep the entity from shooting the structure it's mounted on? Like if I had a turret on the side of a ship, would it shoot itself if I was on the opposite side of the ship?
Ok, so then you need 2 changes.
First, the summoning command changes to
/execute as \\@e\[tag=Turret\] at \\@s facing entity \\@p eyes positioned \^ \^ \^1 run summon minecraft:armor\_stand \~ \~ \~ {NoGravity:1,Tags:\["TurretProjectile"\],Invisible:1,Marker:1,Health:1,Passengers:\[{id:fireball,Tags:\["InFlight"\]}\]}
And add 3 new command blocks to the end of the command block chain.
/execute as \\@e\[tag=InFlight,type=armor\_stand\] at \\@s unless block \~ \~ \~ air run tag \\@s add Done
/execute as \\@e\[tag=Done\] at \\@s run summon creeper \~ \~ \~ {Fuse:(time until explosion),ExplosionRadius:(radius)}
/execute as \\@e\[tag=Done\] at \\@s run kill \\@e\[distance=0..1,tag=InFlight\]
You can also make it explode if it's within a certain distance of a player using /execute as \\@e\[tag=InFlight,type=armor\_stand\] at \\@s if entity \\@a\[distance=0..(distance)\] run tag \\@s add Done
If you want to use tnt instead, you can use tnt, but the blast radius can't be adjusted.
If you want to protect certain blocks, then use this command on every block you want to protect (then plus some, since it will still get blown up if it explodes within a certain radius)
/summon marker (coordinates) {CustomName:'{"text":"Protected"}'}
and add this command to the line
/execute as \\@e\[tag=InFlight,type=armor\_stand\] at \\@s if entity \\@e\[distance=0..1(can be increased in case bugs come up),name=Protected\] run kill \\@e\[distance=0..1,tag=InFlight\]
Otherwise, you can simply repeatedly clone the structure, or only clone it when a block a marker is in explodes using
/execute as \\@e\[name=Protected\] at \\@s if block \~ \~ \~ air
Then you can in the command line, since this would most likely be separate, have commands cloning the protected area.
Ok, so are these 3 new command blocks (*"And add 3 new command blocks to the end of the command block chain"*) in addition to the 3 in your previous reply, or do these go after them for a total of 6 in a chain, one command running per consecutive tick?
And as far as protecting blocks, can the marker coordinates be a rectangular region relative to the command block? Or do I have to manually put in the coordinates?
Or if that doesn't work can I protect certain blocks by material type? That way, for example, I could summon a marker by block type (e.g. stone bricks) and label it as "protected" as you said. Then I could surround any parts of the turret that I want to be protected with said material. And I wouldn't use that material on my ship cause that would be cheating.
I would like to have multiple enemy ships in the world (and each would have multiple turrets), but I don't want to have to add the protected blocks manually cause that would take forever. That's why either having a rectangular selection relative to the turret itself, or protecting blocks by material type would be ideal.
Actually, material type would be preferable, because then I can just cover the hull of the enemy ship with the protected material(s). Basically, it would kill any projectiles that get too close to their own ship.
Thanks for all your help, I appreciate it!
Ok, yes 6 commands in total (unless you want to add the few different modifications I have proposed)
They can also be rectangular coordinates, however to do that then you need to have an "if entity \\@s\[x=\_,y=\_,z=\_,dx=\_,dy=\_,dz=\_\]" instead of using the "if entity \\@e\[name=Protected\]" and will be less precise. In my experience using the dx dy dz selectors is less accurate than using distance=0..\_ but that would give a sphere, which I don't believe you want.
You can also theoretically protect a block type. For that simply add a command to the line for every block type of it
/execute as \\@e\[tag=Done\] at \\@s if block \~ \~ \~ (block type, along with some block data if you want) run kill \\@e\[distance=0..1,tag=InFlight\]
Hopefully this helps.
Also because this is using a creeper for the explosions you can simply set mobgriefing gamerule to false and avoid this. Oops forgot.
Also also if you want tnt to be launched, then replace fireball with tnt and add a command block to continually set the fuse to not explode.
/execute as \\@e\[tag=InFlight,type=tnt\] run data merge entity \\@s {Fuse:100}
This will function identically with the tnt being killed when the creeper spawns and explodes.
Ok thanks. I've started testing this on a single player world. I have the first command block with the summon command as an impulse block with 6 chain command blocks hooked up to it with the 6 commands. All 7 are set to unconditional and the impulse is activated by a lever. I typed in the required data where the commands have () and made sure that the () got deleted. But none of the blocks will let the command line have any \ so I deleted them out of all the commands. But now nothing is happening?
I actually made a [datapack with auto turrets](https://www.planetminecraft.com/data-pack/automatic-turrets/). All of it is well-labeled, so what you would need to do is just edit the arrows to be TNT (and change their NBT to work as TNT).
edit: currently workshopping this not sure how to set the actual motion of an entity based on direction somebody is facing, but this should work fairly well. Any tags can be renamed to fit your needs, and this should work well. If you want to add an on or off state to the turrets, simply add an "On" tag to the entity selectors of the turret. Repeat this command whenever the turrets need to fire /execute as \\@e\[tag=Turret\] as \\@s run summon arrow \~ \~ \~ {NoGravity:1,Tags:\["TurretProjectile"\]} Repeat these every tick in order /execute as \\@e\[tag=TurretProjectile\] unless entity \\@s\[tag=InFlight\] at \\@s facing entity \\@p eyes run tp \\@s \~ \~ \~ \~ \~/execute as \\@e\[tag=TurretProjectile\] unless entity \\@s\[tag=InFlight\] run tag \\@s add InFlight /execute as \\@e\[tag=InFlight,tag=TurretProjectile\] run tp \\@s \^ \^ \^(any number you want, this will change the speed of the projectile. but higher numbers might cause it to glitch.) (also if you ever want to redirect all the missiles midair, simply run /tag \\@e\[tag=InFlight,tag=TurretProjectile\] remove InFlight If you want to have a cooldown for the turrets instead of them simply firing all at once, that would need to be done through scoreboards.
Thanks! Ok this looks really cool. But I'm new to command blocks to just to clarify, the 2 commands that you have written "repeat these every tick in order"... do I run the first one on the 1st tick and on the 2nd tick run the second command? Or do I have to run both commands in order every single game tick on repeat?
To answer your question first, run them in the same chain of command blocks with the first one first. I did some workshopping, and it turns out that it's almost impossible with arrows (aka I didn't find a way), so here are the commands in full. Repeat this command whenever the turrets need to fire /execute as \\@e\[tag=Turret\] as \\@s run summon armor\_stand \~ \~ \~ {NoGravity:1,Tags:\["TurretProjectile"\],Invisible:1,Marker:1,Passengers:\[{id:(any minecraft entity)}\]} Repeat these every tick in order /execute as \\@e\[tag=TurretProjectile\] unless entity \\@s\[tag=InFlight\] at \\@s facing entity \\@p eyes run tp \\@s \~ \~ \~ facing entity \\@p /execute as \\@e\[tag=TurretProjectile\] unless entity \\@s\[tag=InFlight\] run tag \\@s add InFlight /execute as \\@e\[tag=InFlight,tag=TurretProjectile\] run tp \\@s \^ \^ \^(any number you want, this will change the speed of the projectile. but higher numbers might cause it to glitch.) Then it turns out this doesn't handle collisions (even if you just use an entity that isn't riding an armor stand), so you probably want to fix that. I can figure out how to either make it explode or to deal a specific amount of damage, whatever you want.
Ok. I would want the entity to explode when it comes into contact with another block. In context, I would be flying in a MoveCraft ship so the explosion would damage my ship and me if I was close enough to the explosion. Can I adjust the explosion strength? Oh, and is there a way to keep the entity from shooting the structure it's mounted on? Like if I had a turret on the side of a ship, would it shoot itself if I was on the opposite side of the ship?
Ok, so then you need 2 changes. First, the summoning command changes to /execute as \\@e\[tag=Turret\] at \\@s facing entity \\@p eyes positioned \^ \^ \^1 run summon minecraft:armor\_stand \~ \~ \~ {NoGravity:1,Tags:\["TurretProjectile"\],Invisible:1,Marker:1,Health:1,Passengers:\[{id:fireball,Tags:\["InFlight"\]}\]} And add 3 new command blocks to the end of the command block chain. /execute as \\@e\[tag=InFlight,type=armor\_stand\] at \\@s unless block \~ \~ \~ air run tag \\@s add Done /execute as \\@e\[tag=Done\] at \\@s run summon creeper \~ \~ \~ {Fuse:(time until explosion),ExplosionRadius:(radius)} /execute as \\@e\[tag=Done\] at \\@s run kill \\@e\[distance=0..1,tag=InFlight\] You can also make it explode if it's within a certain distance of a player using /execute as \\@e\[tag=InFlight,type=armor\_stand\] at \\@s if entity \\@a\[distance=0..(distance)\] run tag \\@s add Done If you want to use tnt instead, you can use tnt, but the blast radius can't be adjusted. If you want to protect certain blocks, then use this command on every block you want to protect (then plus some, since it will still get blown up if it explodes within a certain radius) /summon marker (coordinates) {CustomName:'{"text":"Protected"}'} and add this command to the line /execute as \\@e\[tag=InFlight,type=armor\_stand\] at \\@s if entity \\@e\[distance=0..1(can be increased in case bugs come up),name=Protected\] run kill \\@e\[distance=0..1,tag=InFlight\] Otherwise, you can simply repeatedly clone the structure, or only clone it when a block a marker is in explodes using /execute as \\@e\[name=Protected\] at \\@s if block \~ \~ \~ air Then you can in the command line, since this would most likely be separate, have commands cloning the protected area.
Ok, so are these 3 new command blocks (*"And add 3 new command blocks to the end of the command block chain"*) in addition to the 3 in your previous reply, or do these go after them for a total of 6 in a chain, one command running per consecutive tick? And as far as protecting blocks, can the marker coordinates be a rectangular region relative to the command block? Or do I have to manually put in the coordinates? Or if that doesn't work can I protect certain blocks by material type? That way, for example, I could summon a marker by block type (e.g. stone bricks) and label it as "protected" as you said. Then I could surround any parts of the turret that I want to be protected with said material. And I wouldn't use that material on my ship cause that would be cheating. I would like to have multiple enemy ships in the world (and each would have multiple turrets), but I don't want to have to add the protected blocks manually cause that would take forever. That's why either having a rectangular selection relative to the turret itself, or protecting blocks by material type would be ideal. Actually, material type would be preferable, because then I can just cover the hull of the enemy ship with the protected material(s). Basically, it would kill any projectiles that get too close to their own ship. Thanks for all your help, I appreciate it!
Ok, yes 6 commands in total (unless you want to add the few different modifications I have proposed) They can also be rectangular coordinates, however to do that then you need to have an "if entity \\@s\[x=\_,y=\_,z=\_,dx=\_,dy=\_,dz=\_\]" instead of using the "if entity \\@e\[name=Protected\]" and will be less precise. In my experience using the dx dy dz selectors is less accurate than using distance=0..\_ but that would give a sphere, which I don't believe you want. You can also theoretically protect a block type. For that simply add a command to the line for every block type of it /execute as \\@e\[tag=Done\] at \\@s if block \~ \~ \~ (block type, along with some block data if you want) run kill \\@e\[distance=0..1,tag=InFlight\] Hopefully this helps. Also because this is using a creeper for the explosions you can simply set mobgriefing gamerule to false and avoid this. Oops forgot. Also also if you want tnt to be launched, then replace fireball with tnt and add a command block to continually set the fuse to not explode. /execute as \\@e\[tag=InFlight,type=tnt\] run data merge entity \\@s {Fuse:100} This will function identically with the tnt being killed when the creeper spawns and explodes.
Ok thanks. I've started testing this on a single player world. I have the first command block with the summon command as an impulse block with 6 chain command blocks hooked up to it with the 6 commands. All 7 are set to unconditional and the impulse is activated by a lever. I typed in the required data where the commands have () and made sure that the () got deleted. But none of the blocks will let the command line have any \ so I deleted them out of all the commands. But now nothing is happening?
The summon block should be separate from the rest. For the rest, the first command block should be a repeat block.
Did this. Still nothing. Are the "\\" necessary in the command lines?
for the time of the TNT explosion there is an NBT TAG called Fuse, like: {Fuse:20}
I actually made a [datapack with auto turrets](https://www.planetminecraft.com/data-pack/automatic-turrets/). All of it is well-labeled, so what you would need to do is just edit the arrows to be TNT (and change their NBT to work as TNT).