forum Forumsplay_arrow Minecraft fiber_manual_record Redstone


Function Help

1 emerald5 replies83 views
created 09/19/2018 10:12 pm by HappyHippo77
last reply 09/21/2018 7:59 am
SO uhh... Hey again! I'm working on a function right now (adds Naruto stuff in) and I went to change some code which was once working aannnnddd I somehow managed to break it DX. I don't have time now to fix it again and so I'm asking for help here, if you guys can figure out what is wrong with these codes and tell me you will save me a lot of brain wracking later.

Spoiler - click to reveal
# Kusanagi-Unsheath
scoreboard objectives add kusanagitimer1 dummy
scoreboard players add @a[tag=kusanagiunsheath] kusanagitimer1 1
execute as @e[type=item,nbt={Item:{id:"minecraft:diamond_hoe",tag:{Unbreakable:1,Damage:1,display:{Name:"{\"text\":\"Kusanagi (Sheathed)\"}",Lore:"The Kusanagi blade!"]}}}}] at @s run tag @p add kusanagiunsheath
execute as @e[tag=kusanagiunsheath,scores={kusanagitimer1=1}] at @s run kill @e[distance=..2,type=item,nbt={Item:{id:"minecraft:diamond_hoe",tag:{Unbreakable:1,Damage:1,display:{Name:"{\"text\":\"Kusanagi (Sheathed)\"}",Lore:"The Kusanagi blade!"]}}}}]
execute as @e[tag=kusanagiunsheath,scores={kusanagitimer1=2}] at @s run replaceitem entity @p hotbar.0 minecraft:diamond_hoe{Damage:2,Unbreakable:1,AttributeModifiers:[{AttributeName:"generic.attackSpeed",Name:"Speed",Slot:"mainhand",Amount:1.8,Operation:0,UUIDLeast:111l,UUIDMost:111l},{AttributeName:"generic.attackDamage",Name:"Damage",Slot:"mainhand",Amount:7.0,Operation:0,UUIDLeast:222l,UUIDMost:222l}],display:{Name:"{\"text\":\"Kusanagi (Unsheathed)\"}",Lore:["The Kusanagi blade!"]}} 1
execute as @a[tag=kusanagiunsheath,scores={kusanagitimer1=3}] at @s run scoreboard players set @a[tag=kusanagiunsheath] kusanagitimer1 0
execute as @a[tag=kusanagiunsheath,scores={kusanagitimer1=0}] at @s run tag @p remove kusanagiunsheath
# Kusanagi-Sheath
scoreboard objectives add kusanagitimer2 dummy
execute as @e[type=item,nbt={Item:{id:"minecraft:diamond_hoe",tag:{Unbreakable:1,Damage:2,display:{Name:"{\"text\":\"Kusanagi (Unsheathed)\"}",Lore:"The Kusanagi blade!"]}}}}] at @s run tag @p add kusanagisheath
scoreboard players add @a[tag=kusanagisheath] kusanagitimer2 1
execute as @a[tag=kusanagisheath,scores={kusanagitimer2=1}] at @s run kill @e[distance=..2,type=item,nbt={Item:{id:"minecraft:diamond_hoe",tag:{display:{Name:"{\"text\":\"Kusanagi (Unsheathed)\"}",Lore:"The Kusanagi blade!"]}}}}]
execute as @e[tag=kusanagisheath,scores={kusanagitimer2=2}] at @s run replaceitem entity @p hotbar.0 minecraft:diamond_hoe{Damage:1,Unbreakable:1,AttributeModifiers:[{AttributeName:"generic.attackSpeed",Name:"Speed",Slot:"mainhand",Amount:0.0,Operation:0,UUIDLeast:111l,UUIDMost:111l},{AttributeName:"generic.attackDamage",Name:"Damage",Slot:"mainhand",Amount:0.0,Operation:0,UUIDLeast:222l,UUIDMost:222l}],display:{Name:"{\"text\":\"Kusanagi (Sheathed)\"}",Lore:["The Kusanagi blade!"]}} 1
execute as @a[tag=kusanagisheath,scores={kusanagitimer2=3}] at @s run scoreboard players set @a[tag=kusanagisheath] kusanagitimer2 0
execute as @a[tag=kusanagisheath,scores={kusanagitimer2=0}] at @s run tag @p remove kusanagisheath
For clarification, I'm trying to make the Kusanagi. When I drop the item I want it to be killed and I want it to be replaced with a different item. The same needs to happen in reverse when you drop the other item. If any of you can figure out how to do this without using /replace item (Using /give probably) that would also be really great as I hate having to have my first slot empty or else I'll loose that item. Thanks in advance, please help!
Posted by avatar
Level 25 : Expert Ninja

5 replies

09/20/2018 12:00 am
Level 35 Artisan Engineer
First: your function is a bit of a mess. Don't leave initialization code in the same function you plan on constantly running because it'll be a waste of resources. Although constantly trying to create objectives which already exist may not immediately cause lag it will gobble up precious resources which over time could cause more problems. Especially if you use this design in other functions or datapacks.

This is also why it's preferable not to blindly run commands to try and see if they work but instead set up a condition and test for that before running the actual command. It doesn't always work this way, but when it does it can save (some) resources.

So... to make sure I got this right: you have 2 custom items and want one item to change into the other once the player drops it on the ground. Therefor the key is not to end up in an endless cycle of changing items (one into the other). Because of that I wouldn't go for an endless running check but instead make this event driven.

You're using two different variants of a diamond hoe, that makes it easy because the only event we would need to check for is the player dropping a diamond hoe.
  • /scoreboard objectives add dropped_hoe minecraft.dropped:minecraft.diamond_hoe
So far the preparation, this could be your function:

execute if entity @a[scores={dropped_hoe=1}] as @p[scores={dropped_hoe=1}] run tag @s add reset_hoe
execute at @p[tag=reset_hoe] as @e[type=minecraft:item,tag=!orig,distance=..4,nbt={Item:{id:"minecraft:diamond_hoe"}}] run tag @s add orig
execute at @e[type=minecraft:item,tag=orig,nbt={Item:{tag:{display:{Name:"\"Test\""}}}}] run summon minecraft:item ~ ~ ~ {Item:{id:"minecraft:diamond_hoe",Count:1b,tag:{display:{Name:"\"Done\""}}}}
execute at @e[type=minecraft:item,tag=orig,nbt={Item:{tag:{display:{Name:"\"Done\""}}}}] run summon minecraft:item ~ ~ ~ {Item:{id:"minecraft:diamond_hoe",Count:1b,tag:{display:{Name:"\"Test\""}}}}
execute at @p[tag=reset_hoe] run kill @e[tag=orig,distance=..5]
scoreboard players reset @a[tag=reset_hoe] dropped_hoe
tag @a[tag=reset_hoe] remove reset_hoe

Note: this will insta-add the new item to the players inventory, I only focused on solving the "endless loop" problem and not so much any cosmetic details.

So, the moment you give yourself a diamond hoe named "Test":
  • /give @p minecraft:diamond_hoe{display:{Name:"\"Test\""}}
... and then drop this on the ground you'll immediately get a hoe back which is named 'Done'. Drop that one on the ground and you'll get 'Test' back. All that's left to do is fill in the right nbt tags you want to test for and you should be home free.
09/20/2018 2:12 pm
Level 25 Expert Ninja
Well I'm not very sure how else to make the objectives be created automatically (I want it so that anyone can simply load the resource pack, insert the data pack into their world, and play away without worrying about running any startup commands) but I never even though about looking for an objective testing for an item being dropped. That is perfect! Thank you very much!

EDIT: OMG! Thanks to you not only is it working again it's way better than it was! Thanks a ton!!!!
09/20/2018 11:29 pm
Level 35 Artisan Engineer
Happy to hear that helped.

Crash course in initialization

Are you familiar with tags? There are two tags which are very special: minecraft:tick and minecraft:load. I'm sure you're familiar with the first one: it tells Minecraft to run a function over and over again, comparable to a repeating command block. You're going to need this.

The second though it also important: it tells Minecraft to run a function once; when your game is starting or when your datapack got (re)loaded. That's what makes it the ideal option to separate your "initialization" routines with other stuff.

Example: I would dump that "/scoreboard objectives add" command into a function of its own, and then define a tag which points Minecraft to that function. Now.. I made a (rather simple!) datapack a few weeks ago: Herobrine miniboss. It's not too big: consists of 5 functions, 2 advancements and 2 tags. It might help you get a grasp on these tags.
09/21/2018 7:59 am
Level 25 Expert Ninja
Ooh! I'm still very new to data packs so I don't know how to set up tags but I'm certain the internet has something on it. Thanks a bunch! I didn't know about the load tag!
09/19/2018 11:08 pm
Level 3 Apprentice Skinner
Im not sure but maybe you hit a wrong key or you misspelled something




© 2010 - 2018