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

Add getItemEnchantmentLevel() function, to easily see if an item has an enchantment and at what level. #110

Closed
wants to merge 2 commits into from

Conversation

zisis912
Copy link

@zisis912 zisis912 commented Aug 1, 2023

title

@extremeheat
Copy link
Member

This seems unnecessary when you can do this in one line already with .find

item.enchants.find(e => e.name === 'efficiency')?.level

@extremeheat
Copy link
Member

In this case item.enchants likely would have been better as a dictionary (since enchant.name is a unique key), but not a big deal.

You can get a dict yourself with Object.fromEntries(item.enchants.map(e => [e.name, e]))

@extremeheat
Copy link
Member

Ah, one thing I did realize with the current implementation is that it has a enchants setter (these are bad), so to edit the enchants, you have to

const enchants = item.enchants
enchants.find(ench => enc.name === 'efficiency').level = 3
item.enchants = enchants

IMO all the get/set inside prismarine-item should be removed for non-obvious usage patterns like this. item.enchants returns a mutation, so you need to store to a variable else simply editing the object and trying to trigger the setter again will simply edit a GC'ed item.

@zisis912
Copy link
Author

zisis912 commented Aug 1, 2023

I made this commit because I am translating large parts of the java source code to mineflayer to implement more clicking tyoes, so having identically named functions is a big help.

Also in my opinion both setters and getters should not be a thing, they are both stupid and serve no purpose. I would choose getEnchants() and setEnchants any day

@@ -158,6 +158,14 @@ function loader (registryOrVersion) {
throw new Error("Don't know how to deserialize for this mc version ")
}

getItemEnchantmentLevel (enchantName) {
if (!this.enchants) return 0
for (const enchant of this.enchants) {
Copy link
Member

Choose a reason for hiding this comment

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

enchants should be a dict if you want that

@@ -24,6 +24,7 @@ export class Item {
get blocksCanDestroy(): [string][];
set blocksCanDestroy(blockNames: string[]);
repairCost: number;
getItemEnchantmentLevel(enchantName: string): number
Copy link
Member

Choose a reason for hiding this comment

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

can you update the doc

@rom1504
Copy link
Member

rom1504 commented Dec 31, 2023

old, please re open to finish

@rom1504 rom1504 closed this Dec 31, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Closed
Development

Successfully merging this pull request may close these issues.

3 participants