Skip to content

Commit

Permalink
refactor: Removed explicit occurences of hat items
Browse files Browse the repository at this point in the history
  • Loading branch information
OrangeUtan committed Jul 7, 2021
1 parent 9e01394 commit af6c71f
Show file tree
Hide file tree
Showing 23 changed files with 78 additions and 74 deletions.
9 changes: 5 additions & 4 deletions beet.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,13 @@ meta:
cmd_id: 777
default_item_head: minecraft:stick
default_item_inventory: minecraft:leather_helmet
render:
data_pack:
advancements: ["oran9eutan:*"]
functions: ["oran9eutan:*"]
installation_advancement:
author_skull_owner: Oran9eUtan
icon:
item: "minecraft:stick"
nbt: "{CustomModelData: {{ hats.hat.fez.cmd }}}"
render:
data_pack:
advancements: ["oran9eutan:*"]
functions: ["oran9eutan:*"]
item_tags: ["oran9eutan:*"]
2 changes: 1 addition & 1 deletion datapack/data/oran9eutan/advancements/hats/root.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"display": {
"icon": {
"item": "minecraft:stick",
"item": "{{ hats.default_item_head }}",
"nbt": "{CustomModelData: {{ hats.hat.fez.cmd }}}"
},
"title": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
# Descr: Adds offer of the favorite cat the Villager Trader is wearing

# Add template offer
data modify entity @s Offers.Recipes append value {sell:{id:"minecraft:leather_helmet",Count:1b}}
data modify entity @s Offers.Recipes append value {sell:{id:"{{ hats.default_item_inventory }}",Count:1b}}

# Set the sell item of the template offer to the cat hat on the villagers head
data modify entity @s Offers.Recipes[-1].sell.tag set from entity @s ArmorItems[3].tag

# Set offer price
loot replace entity @s weapon.offhand loot oran9eutan:hats/trades/villager/fav_cat_price
data modify entity @s Offers.Recipes[-1].buy set from entity @s HandItems[1]
data modify entity @s Offers.Recipes[-1].buy set from entity @s HandItems[1]
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
# descr: Adds profession dependent special offer to the Villager

# Add template offer
data modify entity @s Offers.Recipes append value {buy:{id:"minecraft:emerald", Count:1b}, sell:{id:"minecraft:leather_helmet",Count:1b}}
data modify entity @s Offers.Recipes append value {buy:{id:"minecraft:emerald", Count:1b}, sell:{id:"{{ hats.default_item_inventory }}",Count:1b}}

# Give Villager item depending on profession
loot replace entity @s weapon.offhand loot oran9eutan:hats/trades/villager/profession
data modify entity @s Offers.Recipes[-1].sell.tag set from entity @s HandItems[1].tag

# Set offer price
loot replace entity @s weapon.offhand loot oran9eutan:hats/trades/villager/profession_price
data modify entity @s Offers.Recipes[-1].buy set from entity @s HandItems[1]
data modify entity @s Offers.Recipes[-1].buy set from entity @s HandItems[1]
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
# Descr: Adds offer of the favorite cat the Wandering Trader is wearing

# Add template offer
data modify entity @s Offers.Recipes append value {buy:{id:"minecraft:emerald", Count:1b}, sell:{id:"minecraft:leather_helmet",Count:1b}}
data modify entity @s Offers.Recipes append value {buy:{id:"minecraft:emerald", Count:1b}, sell:{id:"{{ hats.default_item_inventory }}",Count:1b}}

# Set the sell item of the template offer to the cat hat on the Wandering Traders head
data modify entity @s Offers.Recipes[-1].sell.tag set from entity @s ArmorItems[3].tag

# Set offer price
loot replace entity @s weapon.offhand loot oran9eutan:hats/trades/wandering_trader/fav_cat_price
data modify entity @s Offers.Recipes[-1].buy set from entity @s HandItems[1]
data modify entity @s Offers.Recipes[-1].buy set from entity @s HandItems[1]
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# descr: Adds a hat trade related to Villagers

# Add template offer that is modified later
data modify entity @s Offers.Recipes append value {buy:{id:"minecraft:emerald", Count:1b}, sell:{id:"minecraft:leather_helmet",Count:1b}}
data modify entity @s Offers.Recipes append value {buy:{id:"minecraft:emerald", Count:1b}, sell:{id:"{{ hats.default_item_inventory }}",Count:1b}}

# Give trade sell item
loot replace entity @s weapon.offhand loot oran9eutan:hats/hat_on_head/villager/random
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Player dropped a stick or a leather helmet. Not certain that item is a hat
# executor: Player getting advancement
# Player dropped a #hat_head or a #hat_inventory. Not certain that item is a hat
# @s: Player that made advancement

# Convert all nearby dog hat items into dogs
execute if {{ hats.setting["dog_conversion"].is_true }} as @e[type=item,distance=..2,nbt={Item:{tag:{Tags:["hats.hat.type.dog"]}}}] at @s if data entity @s Item.tag.dog_data run function oran9eutan:hats/dog_mechanism/copy_dog_data_from_item
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Replace leather helmet hats on armor stands nearby with stick hats
# Replace #hat_inventory hats on armor stands nearby with #hat_head hats
execute as @e[type=minecraft:armor_stand,distance=0..8,tag=!global.ignore] if predicate oran9eutan:hats/entity/wears_inventory_hat run function oran9eutan:hats/hat_mechanism/equip_armorstand_with_correct_hat

advancement revoke @s only oran9eutan:hats/event/player_interacted_with_armor_stand
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
# Tag Player that they received a #hat_head
tag @s add hats.tmp.received_head_hat
# Fix the #hat_head in the next tick. Changing the inventory on an advancement trigger is buggy
schedule function oran9eutan:hats/proxies/fix_players_stick_hats_if_necessary 1t
schedule function oran9eutan:hats/proxies/fix_players_head_hats_if_necessary 1t

# Reset trigger
advancement revoke @s only oran9eutan:hats/event/player_received_head_hat
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
########################################################################
# as: ArmorStand #
# Descr: Replace the #hat on the ArmorStands head with an #hat_on_head #
# Descr: Replace the #hat on the ArmorStands head with an #hat_on_head #
########################################################################

# Since #hat and #hat_on_head items have the same nbt, only change the item id
data modify entity @s ArmorItems[3] merge value {id:"minecraft:stick"}
# Since #hat_inventory and #hat_head items have the same nbt, only change the item id
data modify entity @s ArmorItems[3] merge value {id:"{{ hats.default_item_head }}"}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ data modify storage minecraft:hats buffer.armor set value []
data modify storage minecraft:hats buffer.armor append from storage minecraft:hats buffer.all[{Slot:103b}]

# Modify hat
data modify storage minecraft:hats buffer.armor[0].id set value "minecraft:stick"
data modify storage minecraft:hats buffer.armor[0].id set value "{{ hats.default_item_head }}"
data modify storage minecraft:hats buffer.armor[0].Slot set value 0b

# Replace Player helmet with modified hat
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# executor: Player who have a stick hat in their inventory that needs to be fixed
# descr: Replaces any stick hats in the Players inventory with leather helmet hats
# Replaces any #hat_head hats in the players inventory with #hat_inventory hats
# @s: Player who has a #hat_head hat in their inventory

# Extract hotbar from buffer.inv
data modify storage minecraft:hats buffer.hotbar append from storage minecraft:hats buffer.inv[{Slot:0b}]
Expand All @@ -12,7 +12,7 @@ data modify storage minecraft:hats buffer.hotbar append from storage minecraft:h
data modify storage minecraft:hats buffer.hotbar append from storage minecraft:hats buffer.inv[{Slot:7b}]
data modify storage minecraft:hats buffer.hotbar append from storage minecraft:hats buffer.inv[{Slot:8b}]

execute if data storage minecraft:hats buffer.hotbar[{id:"minecraft:stick", tag:{Tags:["hats.hat"]}}] run function oran9eutan:hats/hat_mechanism/fix_players_stick_hats_in_hotbar
execute if data storage minecraft:hats buffer.hotbar[{id:"{{ hats.default_item_head }}", tag:{Tags:["hats.hat"]}}] run function oran9eutan:hats/hat_mechanism/fix_players_head_hats_in_hotbar

# Remove item in offhand from buffer.inv
data remove storage minecraft:hats buffer.inv[{Slot:-106b}]
Expand All @@ -34,4 +34,4 @@ data remove storage minecraft:hats buffer.inv[{Slot:1b}]
data remove storage minecraft:hats buffer.inv[{Slot:0b}]

# Now only items in the Players inventory should be left in buffer.inv
execute if data storage minecraft:hats buffer.inv[{id:"minecraft:stick", tag:{Tags:["hats.hat"]}}] run function oran9eutan:hats/hat_mechanism/fix_players_stick_hats_in_inventory
execute if data storage minecraft:hats buffer.inv[{id:"{{ hats.default_item_head }}", tag:{Tags:["hats.hat"]}}] run function oran9eutan:hats/hat_mechanism/fix_players_head_hats_in_inventory
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Determines if the Player has any #hat_head items in their inventory and then fixes them
# @s: Player whose #hat_head hats are going to be fixed

# Store the Players inventory
data modify storage hats buffer.inv set from entity @s Inventory
# #hat_head hats on the Players head shold not get fixed
data remove storage minecraft:hats buffer.inv[{Slot:103b}]

execute if data storage minecraft:hats buffer.inv[{id:"{{ hats.default_item_head }}", tag:{Tags:["hats.hat"]}}] run function oran9eutan:hats/hat_mechanism/fix_players_head_hats

# Clean up
data remove storage minecraft:hats buffer
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Fixes all #hat_head hats in a players hotbar slots
# @s: Player whos inventory is getting fixed
# requirements:
# - storage buffer.hotbar has to be populated
# - at least one #hat_head hat has to exist in buffer.hotbar

# Replace all #hat_head hats with helmet hats
data modify storage minecraft:hats buffer.hotbar[{id:"{{ hats.default_item_head }}", tag:{Tags:["hats.hat"]}}].id set value "{{ hats.default_item_inventory }}"

# Replaced Players hotbar with modified hotbar
setblock ~ 0 ~ minecraft:yellow_shulker_box
data modify block ~ 0 ~ Items set from storage minecraft:hats buffer.hotbar
loot replace entity @s hotbar.0 9 mine ~ 0 ~ minecraft:air{drop_contents:1b}

# Clean up
setblock ~ 0 ~ minecraft:air
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# executor: Player whos inventory is getting fixed
# descr: Fixes all stick hats in the Players inventory slots
# Fixes all #hat_head hats in a players inventory slots
# @s: Player whos inventory is getting fixed
# requirements:
# - storage buffer.inv has to be populated
# - at least one stick hat has to exist in buffer.inv
# - at least one #hat_head hat has to exist in buffer.inv

# Correct slot data
data modify storage minecraft:hats buffer.inv[{Slot:9b}].Slot set value 0b
Expand Down Expand Up @@ -33,10 +33,10 @@ data modify storage minecraft:hats buffer.inv[{Slot:33b}].Slot set value 24b
data modify storage minecraft:hats buffer.inv[{Slot:34b}].Slot set value 25b
data modify storage minecraft:hats buffer.inv[{Slot:35b}].Slot set value 26b

# Replace all stick Hats with helmet hats
data modify storage minecraft:hats buffer.inv[{id:"minecraft:stick", tag:{Tags:["hats.hat"]}}].id set value "minecraft:leather_helmet"
# Replace all #hat_head hats with #hat_inventory hats
data modify storage minecraft:hats buffer.inv[{id:"{{ hats.default_item_head }}", tag:{Tags:["hats.hat"]}}].id set value "{{ hats.default_item_inventory }}"

# Replaced Players inventory with modified inventory
# Replaced players inventory with modified inventory
setblock ~ 0 ~ minecraft:yellow_shulker_box
data modify block ~ 0 ~ Items set from storage minecraft:hats buffer.inv
loot replace entity @s inventory.0 27 mine ~ 0 ~ minecraft:air{drop_contents:1b}
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# This function is scheduled as a proxy, so that another function call can be scheduled with a desired context
# @s: World

# Execute proxied function as Players that received a #hat_head hat
execute as @a[tag=!global.ignore,tag=hats.tmp.received_head_hat] run function oran9eutan:hats/hat_mechanism/fix_players_head_hats_if_necessary
tag @a remove hats.tmp.received_head_hat

This file was deleted.

2 changes: 1 addition & 1 deletion datapack/data/oran9eutan/tags/items/hat_head.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"values":["minecraft:stick"]}
{"values":["{{ hats.default_item_head }}"]}
2 changes: 1 addition & 1 deletion datapack/data/oran9eutan/tags/items/hat_inventory.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"values":["minecraft:leather_helmet"]}
{"values":["{{ hats.default_item_inventory }}"]}
2 changes: 1 addition & 1 deletion hats/macros/advancements.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

{%- macro icon_hat(cmd) -%}
{
"item": "minecraft:stick",
"item": "{{ hats.default_item_head }}",
"nbt": "{CustomModelData: {{ cmd }}}"
}
{%- endmacro %}
Expand Down
17 changes: 10 additions & 7 deletions hats/plugins/item_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,29 @@ class ModelOverride:

def beet_default(ctx: Context):
opts = HatsOptions.from_json(ctx.meta["hats"])
ctx.assets.merge(item_models(ctx, opts.cmd_id))
ctx.assets.merge(item_models(ctx, opts))


def item_models(ctx: Context, cmd_id: int):
def item_models(ctx: Context, opts: HatsOptions):
"""Create item models used for custom model data"""

assets = ResourcePack()

registry = HatRegistry.get(cmd_id)
registry = HatRegistry.get(opts.cmd_id)

overrides = []
for category, hats in registry.categories.items():
overrides += [ModelOverride(hat.cmd, hat.model_path(category)) for hat in hats]
overrides = sorted(overrides, key=lambda o: o.cmd)

assets.models["minecraft:item/stick"] = Model(
ctx.template.render("models/stick.json", overrides=overrides)
item_head_name = opts.default_item_head.split(":")[1]
item_inventory_name = opts.default_item_inventory.split(":")[1]

assets.models[f"minecraft:item/{item_head_name}"] = Model(
ctx.template.render(f"models/{item_head_name}.json", overrides=overrides)
)
assets.models["minecraft:item/leather_helmet"] = Model(
ctx.template.render("models/leather_helmet.json", overrides=overrides)
assets.models[f"minecraft:item/{item_inventory_name}"] = Model(
ctx.template.render(f"models/{item_inventory_name}.json", overrides=overrides)
)

return assets

0 comments on commit af6c71f

Please sign in to comment.