Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement item durability #714

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

thiago-buarqque
Copy link

@thiago-buarqque thiago-buarqque commented Apr 6, 2025

Description

  • What was changed?

    • It is possible now to increment the damage of a given item after use_item, use_item_on, on_attack and normal_use events or whatever other event that an item might handle.
    • We're now saving and reading item's damage for a given player.
  • Why were these changes necessary?
    It is part of the vanilla game.

  • What is the impact of this change?
    This is the first step to read and write item components (data) when saving/reading a player's data in a world. Also this change introduces a way of sending structured components for a given Slot.

  • Are there any known issues or limitations?
    This PR does not account for the unbreakable enchantment since it's not yet implemented.

Demo

Turn sound on 🔊 to hear the breaking sound :D

item-durability-demo-2025-04-06_16.39.08.1.mp4

@thiago-buarqque thiago-buarqque marked this pull request as ready for review April 7, 2025 13:50
@@ -31,6 +32,19 @@ impl ItemMetadata for HoeItem {

#[async_trait]
impl PumpkinItem for HoeItem {
async fn on_after_dig(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this vanilla ?

@@ -13,6 +14,17 @@ pub trait ItemMetadata {
#[async_trait]
pub trait PumpkinItem: Send + Sync {
async fn normal_use(&self, _block: &Item, _player: &Player) {}
async fn on_attack(&self, _item: &Item, _victim: &Entity) {}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this vanilla?

@Snowiiii
Copy link
Collaborator

Snowiiii commented Apr 9, 2025

I also wonder if the slot components are properly saved, so if you rejoin or the server restarts its saved in the player data

@Snowiiii
Copy link
Collaborator

Snowiiii commented Apr 9, 2025

i also think instead of using duplicated logic for all the tools you can just use a trait like Mojang does (abstract class)

@thiago-buarqque
Copy link
Author

@Snowiiii

I also wonder if the slot components are properly saved, so if you rejoin or the server restarts its saved in the player data
Yes, it is. It's mentioned in the description

We're now saving and reading item's damage for a given player.

Not very clearly thought, my bad.

@thiago-buarqque
Copy link
Author

thiago-buarqque commented Apr 9, 2025

i also think instead of using duplicated logic for all the tools you can just use a trait like Mojang does (abstract class)
I don't have access to Mojang code, how can I get access to it?

I did not create a trait for that because not all items is damaged after the same events. For instance.

All tools are damaged after breaking a block or using it in a block (if it's possible). Also if you hit an entity.

Swords break when hitting an entity.

Fishing rod does not break when you hit someone or break a block, but does when you fish.

Elytra breaks when in the chest slot and flying

others...

@thiago-buarqque
Copy link
Author

@Snowiiii I like the explicit way of saying "hey, after this event happen (normal use, use on block etc) damage the item

@Snowiiii Snowiiii added the enhancement New feature or request label Apr 11, 2025
@Snowiiii Snowiiii linked an issue Apr 11, 2025 that may be closed by this pull request
1 task
@Snowiiii Snowiiii changed the title #589 implement item durability implement item durability Apr 11, 2025
@Snowiiii
Copy link
Collaborator

i don't like the on_after_dig function i wonder why i can't be just done in the on_use_block thing function, i also don't like that check if the player in creative yourself all the time, you can just put it into the calc function like vanilla does

 private int calculateDamage(int baseDamage, ServerWorld world, @Nullable ServerPlayerEntity player) {
        if (!this.isDamageable()) {
            return 0;
        }
        if (player != null && player.isInCreativeMode()) {
            return 0;
        }
        if (baseDamage > 0) {
            return EnchantmentHelper.getItemDamage(world, this, baseDamage);
        }
        return baseDamage;
    }

Other tools like Shovels or Swords are missing

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Item durability
2 participants