Simply put, I am making guns and have run into a serious issue. To start off I will have to explain how I made them.
So I made the guns by using a function. When you right click with a carrot on a stick with a specific model data it shoots, depending on the model data. Currently I have 2 working guns, a sniper and a shotgun. It is the latter with which the issue really started. This is because of how I made it so that bullets don't go through blocks. I made a seperate function which gives the "bullet" a score when it is in a block it is allowed to pass through. When it comes in a block it isn't allowed to pass through it is killed.
The issue comes when calling this function in the gun's shoot function. When I fire, the following happens:
- The bullet(s) is(are) spawned
- The bullet(s) teleport based on the nearest player's direction (the shooter)
- The following repeats a lot of times (depending on the gun's range):
Particle Give nearby entities "hit" score Call blockhitcheck function Teleport forward 0.25 blocks
For the sniper for example, this repeats 400 times (100 range) as more range started to create the issue. For the shotgun each bullet has seperate "hit" score so the damage could easily be counted together.So the exact look of the command differs quite a bit per gun, but the order is the same.
So, what issue does it actually create? A real weird kind of lag. When I give the sniper a lot of range it seems to create constant lag for everything in the world, yet the player seems unaffected. (so much lag I could see the bullet travel, which is supposed to happen all within a single game tick) With the shotgun 5 blocks range was enough to create this lag (only when fired) and upon removing the block checking it went back to normal. So I'm looking for a better, less laggy way to have bullets that don't just pass through every block and don't lag quickly.
I may have explained some things in a weird way, so if you don't understand what I mean feel free to ask.
Edit: I should mention that I have only basic function knowledge, I can only do things I could do with command blocks, just in larger quantities.
Edit: Here's some of the commands.
This is one example of the 222 "whitelisted" blocks, each is the same command though.
execute as @e[type=armor_stand,tag=bullet] at @s if block ~ ~ ~ air run scoreboard players set @s notinblock 5
This is then done after all the block checks to kill if it isn't in one of the "passable" blocks and then reset the score to check again.
kill @e[type=armor_stand,tag=bullet,scores={notinblock=0..2}]
scoreboard players set @s notinblock 0
And then for the actual shot.
This summons the bullet.
execute as @a[scores={shoot=1..,shootCD=..1},nbt={SelectedItem:{id:"minecraft:carrot_on_a_stick",tag:{CustomModelData:1}}}] at @s anchored eyes run summon armor_stand ^-0.25 ^-0.15 ^0.65 {Invisible:1b,Invulnerable:1b,PersistenceRequired:1b,NoGravity:1b,Small:1b,Tags:["bullet","sniperbullet"]}
This rotates the bullet in the shooter's direction.
execute as @e[type=armor_stand,tag=sniperbullet] at @s rotated as @a[limit=1,sort=nearest,nbt={SelectedItem:{id:"minecraft:carrot_on_a_stick",tag:{CustomModelData:1}}}] run tp ^ ^ ^
This is for hitting targets (slightly different from actual command to shorten it, works the same)
execute as @e[type=armor_stand,tag=sniperbullet] at @s run scoreboard players set @e[distance=..0.5] sniperhit 1
Then it runs the blockhitcheck function to see if it is in a block.
execute as @e[type=armor_stand,tag=sniperbullet] at @s run function grinn:blockhitcheck
Afterwards it teleports if not killed by blockhitcheck.
execute as @e[type=armor_stand,tag=sniperbullet] at @s run tp @s ^ ^ ^0.25
These last three repeat a number of times depending on how much range I wish the gun to have (ex. 400x for 100 blocks range)
I left out a lot of things like particles and sound effects, also the parts that kill/damage the targets that have been hit.
Clarity Edit: In case it wasn't obvious what caused the lag, it's the blockhitcheck function, removing it from my shoot function allows it to be used pretty much lagless.