how do i make a lightweight projectile lifespan procedure for rapid fire(solved)

Started by potatomancer on

Topic category: Help with Minecraft modding (Java Edition)

Joined Oct 2025
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
how do i make a lightweight projectile lifespan procedure for rapid fire(solved)
Mon, 10/13/2025 - 00:11 (edited)

im trying to make a ranged item that has short range projectiles and shoot constantly while active enabled. i have tried a wait __ ticks then on server side on server side do despawn entity, but this causes the game to pretty much die (all mobs despawn and new chunks struggle to load). i have tried adding a cooldown to the ranged item, but i cant have the game run and the ranged item fire fast enough at the same time. is there any way to set a timer thats less asking of the game performance? (turns out it wasn't a lag issue, i just had the wrong entity type so the game was trying to despawn the player, which it can't do)

Edited by potatomancer on Mon, 10/13/2025 - 00:11
Joined Apr 2023
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
You could probably do an nbt…
Fri, 10/03/2025 - 22:11

You could probably do an nbt number tag on the immediate source entity (the projectile itself) which goes up by 1 on each projectile flying tick, and despawns the immediate source entity once it reaches a certain number (greater than or equal to 20 for example, to make it last for a second).

This works a lot better than the wait _ ticks procedure block, because that procedure block really doesn't work at all for what you are trying to do. (It has a few very specific use cases, but generally, you want to use an alternative timer method, like the one I described)

Joined Oct 2025
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
i havent used nbt blocks…
Fri, 10/03/2025 - 22:41

i havent used nbt blocks before, so im not sure if im doing something wrong, but i got the same results as the previous code. heres the generated code

package net.mcreator.bluedwarf.procedures;

/* imports omitted */

public class ChainsawSparksWhileProjectileFlyingTickProcedure {
public static void execute(Entity entity) {
if (entity == null)
return;
while (entity.getPersistentData().getDouble("lifespan") <= 2) {
entity.getPersistentData().putDouble("lifespan", (entity.getPersistentData().getDouble("lifespan") + 1));
}
if (!entity.level().isClientSide())
entity.discard();
}
}
Joined Apr 2023
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
I would use an if/else block…
Fri, 10/03/2025 - 23:53

I would use an if/else block instead of a while block, using the same condition of lifespan <= 2, with the nbt increase inside the if do part, and the despawn in the else do part. The way the projectile flying tick trigger works is it calls it 20 times per second, and the nbt will stay between those times, so the first time the procedure triggers, it will set lifespawn to 1, the second time will add 1 and bring it to 2, and the third time will no longer be <= 2, so it will call the else condition and despawn the immediate source entity.

 

It would probably look something like (but not exactly like) this:

 

If nbt number lifespan of immediate source entity <= 2

Do set lifespan of immediate source entity to lifespawn of immediate source entity + 1

Else Do despawn immediate source entity

Joined Oct 2025
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
i tried your new suggestion…
Sat, 10/04/2025 - 00:34

i tried your new suggestion but got similar results. i understand you specified the code is not exact, but i didn't know that else to add or remove from it. i think all the logic checks out and it would work for a single shot ranged item, but i need it to be lighter on processing power.

heres the code i have right now

package net.mcreator.bluedwarf.procedures;

/* imports omitted */

public class ChainsawSparksWhileProjectileFlyingTickProcedure {
public static void execute(Entity entity) {
if (entity == null)
return;
if (entity.getPersistentData().getDouble("lifespan") <= 2) {
entity.getPersistentData().putDouble("lifespan", (entity.getPersistentData().getDouble("lifespan") + 1));
} else {
if (!entity.level().isClientSide())
entity.discard();
}
}
}
Joined Apr 2023
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
I just double checked the…
Sat, 10/04/2025 - 12:09

I just double checked the code and realized that you are using event/target entity instead of immediate source entity (you can find it under Minecraft Components and drag it over to replace event/target entity in all of the procedure blocks) and swapping it to immediate source entity should help things. Event/target entity is the player, instead of the projectile, which means that it wouldn't be limiting the lifespan of the projectile, only trying to despawn the player, which doesn't work. So that could be part of the problem.

I know you are saying that it might be too complex a procedure to run, but the current version really is extremely lightweight, and I don't really see it causing noticeable lag. Unless you are running it on literally hundreds of projectiles, in which case the sheer number of projectiles could also be a contributing factor for lag regardless of whether or not it is running a procedure. And even then it wouldn't be the game-breaking level of lag that you said was occurring with the initial procedure you made.

My guess is that there is something else causing the lag. Are you running a custom procedure to fire the projectiles? If so, there is a possibility that it is causing some of the lag. If you could share the generated code and how you are triggering it, I could try to figure out if it would be causing the game-breaking lag, because I really don't think that the procedure to despawn the projectile, running every tick on multiple projectiles, should be causing that lag... Unless it is related to attempting to despawn the player (event/target entity), I really haven't experimented with that enough to know if it would cause game-breaking lag, or disconnect you from the internal server (which could maybe cause what you were experiencing), or anything like that.

Joined Oct 2025
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
just switched the entity…
Sat, 10/04/2025 - 20:58

just switched the entity blocks and it works! thanks for your help!

Joined Apr 2023
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
I'm glad I was able to help.
Sat, 10/04/2025 - 22:58

I'm glad I was able to help.