-
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.
Merge pull request #64 from Felipe-Devr/develop
feat: added ItemFoodComponent, exhausting player on attacking, sprinting and breaking blocks
- Loading branch information
Showing
11 changed files
with
183 additions
and
6 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
20 changes: 20 additions & 0 deletions
20
packages/world/src/components/entity/attribute/absorption.ts
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,20 @@ | ||
import { AttributeName } from "@serenityjs/protocol"; | ||
|
||
import { EntityAttributeComponent } from "./attribute"; | ||
|
||
import type { Player } from "../../../player"; | ||
|
||
class PlayerAbsorptionComponent extends EntityAttributeComponent { | ||
public static readonly identifier = AttributeName.Absorption; | ||
|
||
public readonly effectiveMin: number = 0; | ||
public readonly effectiveMax: number = 512; | ||
public readonly defaultValue: number = 0; | ||
|
||
public constructor(player: Player) { | ||
super(player, PlayerAbsorptionComponent.identifier); | ||
this.setCurrentValue(this.defaultValue, false); | ||
} | ||
} | ||
|
||
export { PlayerAbsorptionComponent }; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import { ItemIdentifier, type Items } from "@serenityjs/item"; | ||
|
||
import { ItemUseCause } from "../../enums"; | ||
import { ItemStack } from "../../item"; | ||
|
||
import { ItemComponent } from "./item-component"; | ||
|
||
import type { Player } from "../../player"; | ||
|
||
class ItemFoodComponent<T extends keyof Items> extends ItemComponent<T> { | ||
public static readonly identifier = "minecraft:food"; | ||
|
||
/** | ||
* The nutrition value that will be given to the player when eated | ||
*/ | ||
public nutrition: number = 0; | ||
|
||
/** | ||
* The saturation modifier to apply the saturation buff | ||
*/ | ||
public saturationModifier: number = 0; | ||
|
||
/** | ||
* Means if the food can be eaten whether the player is hungry or not | ||
*/ | ||
public canAlwaysEat: boolean = false; | ||
|
||
/** | ||
* It's the item that will be converted to when eaten | ||
*/ | ||
public convertsTo: ItemIdentifier = ItemIdentifier.Air; | ||
|
||
public constructor(item: ItemStack<T>) { | ||
super(item, ItemFoodComponent.identifier); | ||
} | ||
|
||
public onUse(player: Player, cause: ItemUseCause): boolean { | ||
if (cause != ItemUseCause.Use || !player.usingItem) return false; | ||
if (!this.canAlwaysEat && !player.isHungry()) return false; | ||
// ? Get the player hunger, saturation and inventory | ||
const hungerComponent = player.getComponent("minecraft:player.hunger"); | ||
const saturationComponent = player.getComponent( | ||
"minecraft:player.saturation" | ||
); | ||
const { container, selectedSlot } = player.getComponent( | ||
"minecraft:inventory" | ||
); | ||
// ? Increase the food based on nutrition | ||
hungerComponent.increaseValue(this.nutrition); | ||
// ? Add a saturation buff using the formula nutrition * saturationModifier * 2 | ||
saturationComponent.increaseValue( | ||
this.nutrition * this.saturationModifier * 2 | ||
); | ||
// ? Decrement the food item amount | ||
player.usingItem.decrement(1); | ||
|
||
// ? If the item will be converted to a item different than air, convert it | ||
if (this.convertsTo != ItemIdentifier.Air) { | ||
const convertedItemStack = new ItemStack(this.convertsTo, 1); | ||
|
||
if (player.usingItem.amount > 0) { | ||
container.addItem(convertedItemStack); | ||
return true; | ||
} | ||
container.setItem(selectedSlot, convertedItemStack); | ||
} | ||
return true; | ||
} | ||
|
||
/** | ||
* ? Necessary methods to make it work lol | ||
*/ | ||
public onStartUse(player: Player, cause: ItemUseCause): void {} | ||
|
||
public onStopUse(player: Player, cause: ItemUseCause): void {} | ||
} | ||
|
||
export { ItemFoodComponent }; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { Color, EffectType } from "@serenityjs/protocol"; | ||
|
||
import { Effect } from "./effect"; | ||
|
||
import type { Entity } from "../entity"; | ||
|
||
class AbsorptionEffect<T extends Entity> extends Effect { | ||
public effectType: EffectType = EffectType.Absorption; | ||
public color: Color = new Color(255, 37, 82, 165); | ||
|
||
public onTick?(entity: T): void; | ||
|
||
public onAdd?(entity: T): void { | ||
if (!entity.isPlayer()) return; | ||
const playerAbsorption = entity.getComponent("minecraft:absorption"); | ||
|
||
if (this.amplifier * 4 == playerAbsorption.getCurrentValue()) return; | ||
playerAbsorption.setCurrentValue(this.amplifier * 4, true); | ||
} | ||
|
||
public onRemove?(entity: T): void { | ||
if (!entity.isPlayer()) return; | ||
const playerAbsorption = entity.getComponent("minecraft:absorption"); | ||
|
||
playerAbsorption.resetToDefaultValue(); | ||
} | ||
} | ||
|
||
export { AbsorptionEffect }; |
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