#Make Custom Component recognize enchants

1 messages · Page 1 of 1 (latest)

limpid prism
#

Moving this from #1067869136606220288 since I was told it would be better suited here

I'm trying to patch durability for my custom items, but I need unbreaking to actually work. I've tried several different formats and setups but I cannot figure out how to make it recognize it.

Currently on 1.13.0 scripting version, as I am unsure how high I can push the version before one of my other scripts breaks.

import { system, world } from "@minecraft/server";
import * as mc from "@minecraft/server";
import { EquipmentSlot, GameMode, Player } from "@minecraft/server";
/** @type {import("@minecraft/server").ItemCustomComponent} */

/*
const ItemUnbreakableComponent = {
    onBeforeDurabilityDamage(event) {
        event.durabilityDamage = 0;
    },
};
system.beforeEvents.startup.subscribe(({ itemComponentRegistry }) => {
    itemComponentRegistry.registerCustomComponent("wiki:unbreakable", ItemUnbreakableComponent);
});
*/

const percentageCheck = Math.random();

const DurabilityPatchComponent = {
    onBeforeDurabilityDamage(event) {
        //const player = e.source;
        //const item = e.itemStack;
        let item = entity?.getComponent("equippable")?.getEquipment("Mainhand")
        const unbreakingLevel = item.getComponent(mc.ItemComponentTypes?.Enchantable)?.getEnchantment("unbreaking")?.level || 0
        if ((1/(unbreakingLevel+1))>=percentageCheck) {
            event.durabilityDamage = 1;
        } else { return; }
    },
};
world.beforeEvents.worldInitialize.subscribe(({ itemComponentRegistry }) => {
    itemComponentRegistry.registerCustomComponent("htc:durability_patch", DurabilityPatchComponent);
});
torn flint
#

However some methods may have changed in the way that they are formatted such as the apply knockback method

delicate finch
torn flint
# delicate finch There are some differences between Script V1 (like 1.13.0) and Script V2 (like 2...

That is true there are some changes however I believe from what I have been keeping up with the changes are minimal at best for example server ui now use a options object rather than what it was previously when you were sitting the default values and so on as for the server as I mentioned the knockback has changed format apply impulses now available on the player class oh and there's custom component V2 as well and a few other things but nothing comes to mind when it comes to any major script rewriting that's why I was said what I said

delicate finch
#

runCommandAsync no longer works in V2

torn flint
#

But anyway that's not really that important What's important is helping OP get their issue fixed

torn flint
delicate finch
#

I did help them a little in items

torn flint
#

And that is the majority of what async was used for which is detecting whether a command was successful in execution or not

delicate finch
#

The main issue is the source, onBeforeDurabilityDamage doesn't use source, so I suggested this.

Instead of doing const player = event.source; use const player = event.attackingEntity;
There was more to the conversation but that was the end of it

torn flint
#

It's not permission it's privileges but you get what I mean

delicate finch
#

I know, but will a Custom Component Item Event work like that. Aren't they afterEvents? It is called onBeforDurabilityDamage, so it would make sense. But then that would be the only beforeEvent then in the item events that I have seen

limpid prism
#

I'm losing my mind trying to get this to work

torn flint
torn flint
#

That you can diagnose

limpid prism
#
const DurabilityPatchComponent = {
    onBeforeDurabilityDamage(event) {
        const player = event.attackingEntity
        if (!(player instanceof Player)) return;

        const equippable = player.getComponent("minecraft:equippable");
        const mainhand = equippable.getEquipmentSlot(EquipmentSlot.Mainhand);
        const enchantable = itemStack.getComponent("minecraft:enchantable");
        const unbreakingLevel = enchantable?.getEnchantment("unbreaking")?.level;

        const damageChance = durability.getDamageChance(unbreakingLevel) / 100;

        if (Math.random() > damageChance) return;
        const shouldBreak = durability.damage === durability.maxDurability;

        if (shouldBreak) {
            mainhand.setItem(undefined); // Remove the item
            source.playSound("random.break"); // Play break sound
        } else {
            durability.damage++; // Increase durability damage
            mainhand.setItem(itemStack); // Update item in main hand
        }
    },
};
#

this is what I'm trying currently, based off of what is shown on bedrock.dev

torn flint
delicate finch
#

Itemstack is not defined

torn flint
#

Oh wait never mind You're using the get equipment slot You're not using get equipment my bad that wouldn't work

delicate finch
limpid prism
#

that did not help

#

wait

#

I may have ordered it wrong

#

let me try that first

torn flint
limpid prism
torn flint
delicate finch
#

That was never defined either

limpid prism
#

this feels like it is overcomplicated for what I need

torn flint
limpid prism
#

and now it uses 3 durability instead of 2

torn flint
limpid prism
#
const DurabilityPatchComponent = {
    onBeforeDurabilityDamage(event) {
        const player = event.attackingEntity
        if (!(player instanceof Player)) return;
        
        const itemStack = event.itemStack;
        const equippable = player.getComponent("minecraft:equippable");
        const mainhand = equippable.getEquipmentSlot(EquipmentSlot.Mainhand);
        const enchantable = itemStack.getComponent("minecraft:enchantable");
        const unbreakingLevel = enchantable?.getEnchantment("unbreaking")?.level;

        const durability = itemStack.getComponent("minecraft:durability");
        const damageChance = durability.getDamageChance(unbreakingLevel) / 100;

        if (Math.random() > damageChance) return;
        const shouldBreak = durability.damage === durability.maxDurability;

        if (shouldBreak) {
            mainhand.setItem(undefined); // Remove the item
            source.playSound("random.break"); // Play break sound
        } else {
            durability.damage++; // Increase durability damage
            mainhand.setItem(itemStack); // Update item in main hand
        }
    },
};
limpid prism
#

with the script.. ""working"" it now uses 3

torn flint
torn flint
delicate finch
#

I think it is because it is adding on the durabilityDamage

limpid prism
#

because it even applies to marketplace items

delicate finch
#

This might help, don't know.
event.durabilityDamage // The damage applied to the item's durability when the event occurs.

torn flint
torn flint
limpid prism
#

used the free stuff as example

limpid prism
torn flint
# limpid prism would you mind sharing your addon?

The problem with that is that the ones I made custom weapons for have been basically re worked at this point have their own damage systems and repair systems in there so it wouldn't do you any good to see them since it wouldn't apply but I think the problem here is simply that you have the damage chance from the durability component interacting with your script and as a result of that I think it's tripling the loss durability like you said the only thing I can think of is either triple checking your durability component or using console.warn to identify the particular section of your code that might be interacting with the component itself on the item

limpid prism
#

it uses 2 durability with just the vanilla durability component and no script

torn flint
limpid prism
#

no

#

I just started working on adding custom component support BECAUSE of the double durability usage

#

as stated, this applies to other addons as well

delicate finch
#

I have never noticed the double durability

torn flint
# limpid prism no

Do you mind going into VC as maybe actually seeing your screen may help fix this better than typing

torn flint
limpid prism
torn flint
#

The only other thing I can think of is that he may have another add-on on the world that is interacting with his

limpid prism
#

nope

#

I'll just drop this here to show the bug itself

torn flint
delicate finch
#

I applied a texture pack that has a durability thing. It does 2 damage for me too

torn flint
limpid prism
#

and if you try vanilla items, you'll see it's not an issue with the durability viewer

torn flint
torn flint
delicate finch
torn flint
#

Ok

delicate finch
#

Wait, forgot that an axe is more of a tool not a weapon. So it would do 2, like if you mined with a sword it would do 2

torn flint
#

I'm about to test it with my own utility function just to see

#

I just realised I don't have a custom item to test I'm gonna have to make one

delicate finch
#

💀

torn flint
#

Just made a quick test sword

#

Ok yeah I just tested it and it does go down two Which is strange but I'm going to see if I can write a custom component that cancels it out

#

Yeah I know it seems to be a bug in the new version of Minecraft then which needs to be reported because I've even tried to use the item component to negate the extra damage but no matter what I do it still takes two off rather than the one that it's supposed to even with a damaged chance of maximum one and minimum one @delicate finch @limpid prism

limpid prism
#

already reported

#

and it isn't new either

#

been around for several updates

torn flint
torn flint
#

Good hopefully they'll fix it soon because it shouldn't be doing too it should be doing one I have no idea why it's doing two the only thing I could wonder is could it be maybe something about durability values being too high because on the sword I just made and tested had a thousand durability on it

limpid prism
limpid prism
#

I have an idea on how to make a custom component to fix this now, but I can't try it until I get home

#

if it works, I'll post it here... and if not, we'll have a new point to work from

delicate finch
#

Ok

limpid prism
#

I HAVE HAD A BREAKTHROUGH

#
const DurabilityPatchComponent = {
    onBeforeDurabilityDamage(event) {
        const player = event.attackingEntity
        if (!(player instanceof Player)) return;
        
        const itemStack = event.itemStack;
        const unbreakingLevel = itemStack.getComponent(mc.ItemComponentTypes?.Enchantable)?.getEnchantment("unbreaking")?.level || 0
        const percentageCheck = Math.random();

        if ((1/(unbreakingLevel+1))>=percentageCheck) {
            event.durabilityDamage = 1;
        } else { event.durabilityDamage = 0; }
    },
};
#

not only does this work, but it is more performant and takes less code than the other examples

#

:D

#

if anyone else wants to use this, feel free... but could you please credit me? Custom components are hard and this took me a lot of experimentation to get working

#

@delicate finch check this out when you get the chance

delicate finch
limpid prism
#

is that a good or bad "hmmm"

#

honestly, I'm not even sure if the player check at the start is needed, considering I don't think mobs use weapon durability on bedrock?

#

maybe I'm just crazy though

delicate finch
limpid prism
#

alright

delicate finch
#

It is more condensed then mine, everything looks good

limpid prism
#

oop, didn't realise you were trying to make one as well

delicate finch
#

I work on a lot of different things, I always try to make things more compact but work the same or better

limpid prism