Good Day!
I desperately need some help with coding a general WorldTick handler in my custom mod (magical books). I plan on having multiple items that will use the tick handler after onPlayerStoppedUsing was called. An example of such usage is as follows: An item, when onPlayerStoppedUsing was called, will create a storm that summons lightning bolts that will strike the ground around the player per 20 ticks (which i heard was about one second). This is my main onPlayerStoppedUsing method:
@Override
public void onPlayerStoppedUsing(ItemStack stack, World worldIn, LivingEntity entityLiving, int timeLeft) {
PlayerEntity playerEntity = (PlayerEntity) entityLiving;
int a = this.getUseDuration(stack) - timeLeft;
if (a >= 0) { //0 because testing purposes ---- this btw determines how long the player held and used the item for
if (!worldIn.isRemote()) {
//TODO add some stuff here that will determine the area around the player, then pass it to castTome
castTome(stack, worldIn, playerEntity);
}
}
}
The code above works. This code below is my custom method belonging to a different class (which has the juicy parts):
@Override
public boolean castTome(ItemStack stack, World world, PlayerEntity playerEntity) {
//Sets world rain to true
if (!world.getWorldInfo().isRaining()) world.getWorldInfo().setRaining(true);
//Summons lightning bolt
((ServerWorld) world).addLightningBolt(new LightningBoltEntity(world, this.posX, this.posY, this.posZ, true));
return true;
}
The above also works. The last code below is the tick handler I was using before:
@SubscribeEvent
public void ticker (TickEvent.WorldTickEvent event) {
if (tickCounter==20) {
//After 1 second do smth
}
else if (tickCounter==80) {
tickCounter=0;
// resets the counter back to 0
}
tickCounter++;
}
Is there a way to tell the handler to count ticks and summon lightning every 20 ticks, without placing the actual 'summon lightning' (from the method castTome) code in the tick handler? As I said before, I aim to use this tick handler in almost all of my planned items, and I don't want to create multiple tick handlers which does different things.
Thanks in advance!