Jump to content

Recommended Posts

Posted

Good day.

I've recently started working with events but I'm still quite new to them, so I would like to know where to find these following events:

- When lightning strikes a block.

- When animals have a baby (or when they spawn the baby)

- When the player is in a certain height

- When the it's a certain time

 

I'd be really grateful if you could tell me where to find these.

Thank you.

Posted

Unfortunately none of the scenarios you listed have events, but there are some workarounds and related events listed below.

 

- When lightning strikes a block.

 

As an example of a possible alternative: Immersive Engineering's Lightning Rod doesn't actually detect lightning strikes, it just checks if there's a thunderstorm and spawns a lightning bolt that hits the net of the Lightning Rod.

 

 

- When animals have a baby (or when they spawn the baby)

 

EntityJoinWorld

will fire when any entity joins the world (this includes newly spawned entities and entities in chunks that have just been loaded).

 

If it's an instance of

EntityAnimal

,

Entity#isChild

will return

true

for any child. If it's an instance of

EntitityAgeable

,

EntityAgeable#getGrowingAge

will return -24000 for brand new children (the growing age is incremented/decremented towards 0 each tick).

 

 

- When the player is in a certain height

 

Use

PlayerTickEvent

to check the height of the player (

Entity#posY

). This is fired once per player per tick on each side.

 

 

- When the it's a certain time

 

Use

WorldTickEvent

to check the world time (

World#getWorldTime

) each tick. This is fired once per world (dimension) per tick on each side.

 

If you only care about one dimension (e.g. The Overworld), you may want to use

ServerTickEvent

instead. This is fired once per tick on the server.

Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.

Posted

Also how would I get a random position around the player?

 

Just get the player's position (

Entity#getPosition

or

Entity#posX

/

posY

/

posZ

) and add a random offset (

Random#nextInt

) on each axis.

Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.

Posted

I have a problem. It doesn't want to work:

	@SubscribeEvent
public void onThunder(PlayerTickEvent event){
	int chanceOfStriking = random.nextInt(100);
	System.out.println(chanceOfStriking + String.valueOf(event.player.worldObj.isRaining()) + String.valueOf(!event.player.worldObj.isRemote));
	if (event.player.worldObj.isThundering() && !event.player.worldObj.isRemote && chanceOfStriking == 1){
		int posX = (int) (event.player.posX + (random.nextInt(1024)-512));
		int posZ = (int) (event.player.posZ + (random.nextInt(1024)-512));

		EntityLightningBolt entityLightningBolt = new EntityLightningBolt(event.player.worldObj, posX, 64, posZ);
		event.player.worldObj.addWeatherEffect(entityLightningBolt);
		event.player.worldObj.spawnEntityInWorld(entityLightningBolt);
		spawnCoinOnLightning(event, 5);
	}
}

@SubscribeEvent
public void onPlayerReachY(PlayerTickEvent event){
	if(event.player.posY > 250){
		int per = random.nextInt(3);
		if (per == 1) spawnCoinOnPlayerInY(event, 2);
	}
}


public void spawnCoinOnLightning(PlayerTickEvent event, int coinType){
	event.player.worldObj.spawnEntityInWorld(new EntityItem(event.player.worldObj, event.player.posX, event.player.posY, event.player.posZ, new ItemStack(TModItems.coinCollectable, 1, coinType)));
}

public void spawnCoinOnPlayerInY(PlayerTickEvent event, int coinType){
	event.player.worldObj.spawnEntityInWorld(new EntityItem(event.player.worldObj, event.player.posX, event.player.posY, event.player.posZ, new ItemStack(TModItems.coinCollectable, 1, coinType)));
}

 

What am I doing wrong?

Posted

The event doesn't fire. That's why I added the println in it, to test if it fires.

 

Have you registered your event handler with the appropriate event bus (the FML bus in this case)?

Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.

Posted

I used

MinecraftForge.EVENT_BUS.register(new TModEvents());

 

All events in the

net.minecraftforge.fml.common.gameevent

package are fired on the FML bus (

FMLCommonHandler.instance().bus()

) rather than the Forge bus (

MinecraftForge.EVENT_BUS

). Event handlers only work if they're registered with the right bus.

Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.

Posted

Good day, I have another problem:

The EntityJoinWorldEvent uses Entity, not EntityLiving. Entity doesn't have isChild, so I cant check if the Entity is a child. How would I get around that?

Thank you.

Posted

Use

Entity#getPosition

to get the player's current position,

World#getBiomeGenForCoords

to get the

BiomeGenBase

at that position and

BiomeGenBase#biomeName

to get the biome's name.

Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.