Jump to content

How to cancel the oxygen consumption event


iYM8jXCs3pcvblTE

Recommended Posts

Hello, Mr./Ms.

My current requirement is to perform some behavior if the player's oxygen value decreases and some other behavior if the player's oxygen increases.

I didn't find an event that `listened to the player's oxygen value` change, so I used `TickEvent.ClientTickEvent` to try to listen to each player's oxygen change per tick.

I tried to get the instance of the player using `getEntity` and looked for methods related to oxygen in it, I found `getAirSupply` and `setAirSupply` methods.

These two methods do allow me to modify the player's oxygen, but if the player is in air, minecraft automatically restores 1 oxygen per tick, and when flooded, minecraft makes the player consume 1 oxygen per tick.

If I want my Mod to rigorously reduce the player's oxygen based on my conditions, can I just calculate and write multiple `if` judgments to rigorously reduce the player's oxygen?

Or does `forge` provide a way to cancel the `oxygen consumed/increased` event? Like canceling the injury event.

Addendum: What I said about strictly consuming the player's oxygen is the behavior of my Mod.

For example, I need to consume 3 oxygen per tick, but if the player is in air, the player will only actually consume 2 oxygen.

And if I need to regain 4 oxygen per tick, but if the player is in water, the player will actually only regain 3 oxygen.

Link to comment
Share on other sites

You definitely need to be doing this on the server, not the client.

AFAIK there is no oxygen used event, so you will have to use a tick event. I would suggest using a capability to store the previous air level, and comparing it each tick.

Looking at LivingEntity#increaseAirSupply and LivingEntity#decreaseAirSupply you can see exactly how the air supply is replenished, the actual replenishing or losing is done in LivingEntity#baseTick.

Entity#setAirSupply is public, so you can set that to whatever you wish in order to update the vanilla bubbles rendering. Remember to check for the maxAirSupply, and if applicable the enchantments/effects on the player.

Link to comment
Share on other sites

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.