How do i create a cooldown for a entity (For the "if player collides with entity" logic)

Started by Stallmandev1111 on Sat, 11/19/2022 - 05:42

Topic category: Help with modding (Java Edition)

Last seen on 21:33, 28. Jan 2023
Joined May 2022
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • Tracker tickets:
  • MCreator plugins:
  • Comments:
How do i create a cooldown for a entity (For the "if player collides with entity" logic)
Sat, 11/19/2022 - 05:42

So, im trying to make it where my creature grabs you by mounting you on it but when you get off there is a cooldown that stops the creature from grabbing you when you get off for 10 seconds

https://imgur.com/HvdaNTZ

here is the procedures unlocked code please help!!

package net.mcreator.mineamore.procedures;


import net.minecraft.world.level.LevelAccessor;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.damagesource.DamageSource;

import net.mcreator.mineamore.MineamoreMod;

public class ChorusSquidPlayerCollidesWithThisEntityProcedure {
public static void execute(LevelAccessor world, Entity entity, Entity sourceentity) {
if (entity == null || sourceentity == null)
return;
entity.getPersistentData().putBoolean("cs", (false));
if (entity.getPersistentData().getBoolean("cs") == false) {
entity.getPersistentData().putBoolean("cs", (true));
sourceentity.startRiding(entity);
sourceentity.hurt(DamageSource.CRAMMING, 2);
MineamoreMod.queueServerWork(1000, () -> {
entity.getPersistentData().putBoolean("cs", (false));
});
}
}
}
Last seen on 16:27, 27. Jan 2023
Joined May 2022
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • Tracker tickets:
  • MCreator plugins:
  • Comments:
Create a player persistent…
Mon, 11/21/2022 - 00:15

Create a player persistent global variable, (name it 'cooldown' or something), that is set to zero by default. Then, create another procedure that decreases this variable by 1 every player tick if it is greater than zero.

When your entity attempts to grab the player, make it first check that the cooldown variable is equal to zero. Then, when it grabs the player, set the variable to however long you want the cooldown to be for that player. (In multiplayer, this means it can still grab other players, but won't grab the same player immediately after letting go.)

Keep in mind that the variable timer will be in ticks, so you need to multiply by 20 however many seconds you want the cooldown to be. If you wanted to randomize the cooldown, you could also add a chance of it not decreasing to add more randomness to it; it would then have a minimum duration, but a possibility to last longer.