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

refactor(item): Rewrote the entire library #32

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions smithed_libraries/packs/item/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
# Item
This library allows for items to have custom durability. A maximum durability can be set when creating the item and the library will properly track the damage changes (both increase and decrease) and mirror the custom durability.

Additionally, this library adds API calls to damage custom items via a command.
## Usage
For usage information, see the [documentation](https://wiki.smithed.dev/libraries/item)
## Downloading
You can download it from [here](https://smithed.dev/thenuclearnexus/smithed.item)<br/>
or<br/>
You can build it from source using the [beet](https://github.com/mcbeet/beet)
You can download this library from [the website](https://api.smithed.dev/download?pack=smithed:item)
or
You can build it from source using [beet](https://github.com/mcbeet/beet)

### Building
```
$ pip install beet mecha
$ git clone https://github.com/Smithed-MC/Libraries
$ cd Libraries
$ beet -p smithed_libraries/packs/item
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"criteria": {
"requirement": {
"trigger": "minecraft:inventory_changed",
"conditions": {
"items": [
{
"count": {
"min": 1
},
"nbt": "{smithed:{durability:{}}}"
}
]
}
}
},
"rewards": {
"function": "smithed.item:impl/durability/track_changes/mark"
}
}

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# @public

# force damage to the item in the player's chest slot
# @s = player who needs to have durability changed in chest slot
# located at world spawn
# run from api call

# set chest durability
data modify storage smithed.item:main item set from entity @s Inventory[{Slot:102b}]
function smithed.item:impl/durability/damage/force/calc_unbreaking

# update chest
execute if score $out smithed.item matches 1 run item modify entity @s armor.chest smithed.item:impl/update_nbt
execute if score $out smithed.item matches 0 run item replace entity @s armor.chest with air
execute if score $out smithed.item matches -1 run function #smithed.item:event/item_changed/chest

scoreboard players reset $out smithed.item

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# @public

# force damage to the item in the player's feet slot
# @s = player who needs to have durability changed in feet slot
# located at world spawn
# run from api call

# set feet durability
data modify storage smithed.item:main item set from entity @s Inventory[{Slot:100b}]
function smithed.item:impl/durability/damage/force/calc_unbreaking

# update feet
execute if score $out smithed.item matches 1 run item modify entity @s armor.feet smithed.item:impl/update_nbt
execute if score $out smithed.item matches 0 run item replace entity @s armor.feet with air
execute if score $out smithed.item matches -1 run function #smithed.item:event/item_changed/feet

scoreboard players reset $out smithed.item

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# force damage to the item
# @s = player who needs to have durability changed
# located at world spawn
# run from durability/damage/<SLOT>

# get unbreaking level
execute store result score $unbreaking_lvl smithed.item run data get storage smithed.item:main item.tag.Enchantments[{id:"minecraft:unbreaking"}].lvl

# check if the item is unbreakable
scoreboard players set $unbreakable smithed.item 0
execute store result score $unbreakable smithed.item run data get storage smithed.item:main item.tag.Unbreakable 1
execute unless score $unbreakable smithed.item matches 0 run scoreboard players set $unbreaking_lvl smithed.item -1

# if delta is positive (i.e. adding durability), ignore unbreaking
execute if score $delta smithed.item matches 1.. run scoreboard players set $unbreaking_lvl smithed.item 0

# damage the item, based on its unbreaking level
execute if score $unbreaking_lvl smithed.item matches 0 run function smithed.item:impl/durability/damage/force/sub
execute if score $unbreaking_lvl smithed.item matches 1 if predicate smithed.item:impl/chance/50 run function smithed.item:impl/durability/damage/force/sub
execute if score $unbreaking_lvl smithed.item matches 2 if predicate smithed.item:impl/chance/33 run function smithed.item:impl/durability/damage/force/sub
execute if score $unbreaking_lvl smithed.item matches 3 if predicate smithed.item:impl/chance/25 run function smithed.item:impl/durability/damage/force/sub
execute if score $unbreaking_lvl smithed.item matches 4 if predicate smithed.item:impl/chance/20 run function smithed.item:impl/durability/damage/force/sub
execute if score $unbreaking_lvl smithed.item matches 5 if predicate smithed.item:impl/chance/16 run function smithed.item:impl/durability/damage/force/sub
execute if score $unbreaking_lvl smithed.item matches 6 if predicate smithed.item:impl/chance/14 run function smithed.item:impl/durability/damage/force/sub
execute if score $unbreaking_lvl smithed.item matches 7 if predicate smithed.item:impl/chance/12 run function smithed.item:impl/durability/damage/force/sub
execute if score $unbreaking_lvl smithed.item matches 8 if predicate smithed.item:impl/chance/11 run function smithed.item:impl/durability/damage/force/sub
execute if score $unbreaking_lvl smithed.item matches 9 if predicate smithed.item:impl/chance/10 run function smithed.item:impl/durability/damage/force/sub
execute if score $unbreaking_lvl smithed.item matches 10.. if predicate smithed.item:impl/chance/9 run function smithed.item:impl/durability/damage/force/sub
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
execute store result score $temp smithed.data run data get storage smithed.item:main item.tag.Damage
scoreboard players remove $temp smithed.data 1
execute store result storage smithed.item:main item.tag.smithed.durability.damage int 1 run scoreboard players get $temp smithed.data
# set damage change and run the damage handler
# @s = player who needs to have durability changed
# located at world spawn
# run from durability/damage/force/calc_unbreaking

function smithed.item:impl/durability/process/handle
# get durability change
execute store result score $old_damage smithed.item run data get storage smithed.item:main item.tag.Damage
scoreboard players operation $old_damage smithed.item += $delta smithed.item
scoreboard players set $delta smithed.item -1

# update durability
scoreboard players set $force smithed.item 1
execute if score $unbreakable smithed.item matches 0 run function smithed.item:impl/durability/track_changes/process_item
scoreboard players reset $force smithed.item
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# @public

# force damage to the item in the player's head slot
# @s = player who needs to have durability changed in head slot
# located at world spawn
# run from api call

# set head durability
data modify storage smithed.item:main item set from entity @s Inventory[{Slot:103b}]
function smithed.item:impl/durability/damage/force/calc_unbreaking

# update head
execute if score $out smithed.item matches 1 run item modify entity @s armor.head smithed.item:impl/update_nbt
execute if score $out smithed.item matches 0 run item replace entity @s armor.head with air
execute if score $out smithed.item matches -1 run function #smithed.item:event/item_changed/head

scoreboard players reset $out smithed.item

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# @public

# force damage to the item in the player's legs slot
# @s = player who needs to have durability changed in legs slot
# located at world spawn
# run from api call

# set legs durability
data modify storage smithed.item:main item set from entity @s Inventory[{Slot:101b}]
function smithed.item:impl/durability/damage/force/calc_unbreaking

# update legs
execute if score $out smithed.item matches 1 run item modify entity @s armor.legs smithed.item:impl/update_nbt
execute if score $out smithed.item matches 0 run item replace entity @s armor.legs with air
execute if score $out smithed.item matches -1 run function #smithed.item:event/item_changed/legs

scoreboard players reset $out smithed.item
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
# @public

# force damage to the item in the player's mainhand
# @s = player who needs to have durability changed in mainhand
# located at world spawn
# run from api call

# set mainhand durability
data modify storage smithed.item:main item set from entity @s SelectedItem
function smithed.item:impl/durability/damage/force/calc_unbreaking

function smithed.item:impl/durability/damage/force
# update mainhand
execute if score $out smithed.item matches 1 run item modify entity @s weapon.mainhand smithed.item:impl/update_nbt
execute if score $out smithed.item matches 0 run item replace entity @s weapon.mainhand with air
execute if score $out smithed.item matches -1 run function #smithed.item:event/item_changed/mainhand

execute unless data storage smithed.item:main {item:"null"} unless score $out smithed.data matches -1 run item modify entity @s weapon.mainhand smithed.item:update_nbt
execute if data storage smithed.item:main {item:"null"} run item replace entity @s weapon.mainhand with air
scoreboard players reset $out smithed.item
Loading