-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
157 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
...PI/src/main/java/org/allaymc/api/entity/component/player/EntityPlayerHungerComponent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package org.allaymc.api.entity.component.player; | ||
|
||
import org.allaymc.api.entity.component.EntityComponent; | ||
|
||
/** | ||
* Allay Project 28/06/2024 | ||
* | ||
* @author IWareQ | ||
*/ | ||
public interface EntityPlayerHungerComponent extends EntityComponent { | ||
void tick(); | ||
|
||
int getFoodLevel(); | ||
|
||
void setFoodLevel(int foodLevel); | ||
|
||
int getFoodSaturationLevel(); | ||
|
||
void setFoodSaturationLevel(int saturationLevel); | ||
|
||
float getFoodTickTimer(); | ||
|
||
void setFoodTickTimer(float foodTickTimer); | ||
|
||
float getFoodExhaustionLevel(); | ||
|
||
void setFoodExhaustionLevel(float foodExhaustionLevel); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
...main/java/org/allaymc/server/entity/component/player/EntityPlayerHungerComponentImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package org.allaymc.server.entity.component.player; | ||
|
||
import lombok.Getter; | ||
import lombok.Setter; | ||
import org.allaymc.api.component.annotation.ComponentIdentifier; | ||
import org.allaymc.api.component.annotation.ComponentedObject; | ||
import org.allaymc.api.entity.attribute.AttributeType; | ||
import org.allaymc.api.entity.component.player.EntityPlayerHungerComponent; | ||
import org.allaymc.api.entity.interfaces.EntityPlayer; | ||
import org.allaymc.api.utils.Identifier; | ||
|
||
/** | ||
* Allay Project 28/06/2024 | ||
* | ||
* @author IWareQ | ||
* @see <a href="https://minecraft.wiki/w/Hunger">Hunger</a> | ||
*/ | ||
@Getter | ||
@Setter | ||
public class EntityPlayerHungerComponentImpl implements EntityPlayerHungerComponent { | ||
@ComponentIdentifier | ||
public static final Identifier IDENTIFIER = new Identifier("minecraft:player_hunger_component"); | ||
|
||
@ComponentedObject | ||
protected EntityPlayer player; | ||
|
||
private int foodLevel = 20; | ||
private int foodSaturationLevel = 20; | ||
|
||
private float foodTickTimer; | ||
private float foodExhaustionLevel = 5; | ||
|
||
@Override | ||
public void tick() { | ||
if (player.isSpawned()) { | ||
var needSend = false; | ||
|
||
var hunger = player.getAttribute(AttributeType.PLAYER_HUNGER); | ||
if (hunger.getCurrentValue() != foodLevel) { | ||
hunger.setCurrentValue(foodLevel); | ||
needSend = true; | ||
} | ||
|
||
var saturation = player.getAttribute(AttributeType.PLAYER_SATURATION); | ||
if (saturation.getCurrentValue() != foodSaturationLevel) { | ||
saturation.setCurrentValue(foodSaturationLevel); | ||
needSend = true; | ||
} | ||
|
||
var exhaustion = player.getAttribute(AttributeType.PLAYER_EXHAUSTION); | ||
if (exhaustion.getCurrentValue() != foodExhaustionLevel) { | ||
exhaustion.setCurrentValue(foodExhaustionLevel); | ||
needSend = true; | ||
} | ||
|
||
if (needSend) player.sendAttributesToClient(); | ||
} | ||
} | ||
|
||
@Override | ||
public void setFoodTickTimer(float foodTickTimer) { | ||
this.foodTickTimer = Math.max(foodTickTimer, 0); | ||
} | ||
|
||
@Override | ||
public void setFoodExhaustionLevel(float foodExhaustionLevel) { | ||
while (foodExhaustionLevel >= 4) { | ||
foodExhaustionLevel -= 4; | ||
Check warning on line 68 in Allay-Server/src/main/java/org/allaymc/server/entity/component/player/EntityPlayerHungerComponentImpl.java Codacy Production / Codacy Static Code AnalysisAllay-Server/src/main/java/org/allaymc/server/entity/component/player/EntityPlayerHungerComponentImpl.java#L68
|
||
|
||
if (this.foodSaturationLevel > 0) { | ||
this.foodSaturationLevel--; | ||
} else { | ||
this.foodLevel = Math.max(--this.foodLevel, 0); | ||
} | ||
} | ||
|
||
this.foodExhaustionLevel = foodExhaustionLevel; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters