1. Welcome to skUnity!

    Welcome to skUnity! This is a forum where members of the Skript community can communicate and interact. Skript Resource Creators can post their Resources for all to see and use.

    If you haven't done so already, feel free to join our official Discord server to expand your level of interaction with the comminuty!

    Now, what are you waiting for? Join the community now!

  2. LOOKING FOR A VERSION OF SKRIPT?

    You can always check out our Wiki for downloads and any other information about Skript!

Dismiss Notice
This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Solved (Help) Unique NBT's between two items that share the same name and NBT

Discussion in 'Skript' started by Nutrition35, Jul 17, 2021.

Thread Status:
Not open for further replies.
  1. Nutrition35

    Nutrition35 New Member

    Joined:
    Feb 11, 2020
    Messages:
    9
    Likes Received:
    0
    Skript Version: 2.5.3
    Skript Author: YourSpeedDealer
    Minecraft Version: 1.16.5
    Full Code:

    Code (Text):
    1.  
    2. command /m4a1:
    3.     trigger:
    4.         set {_M4A1} to 1 leather horse armor named "&bM4A1"
    5.         add "{maxDurabilityM4:31}" to nbt of {_M4A1}
    6.         set {ammo} to tag "maxDurabilityM4" of nbt of {_M4A1}
    7.         broadcast "%{ammo}%"
    8.         give player {_M4A1}
    9.  
    10.  
    11. on rightclick:
    12.     if name of player's tool contains "&bM4A1":
    13.         broadcast "%{ammo}%"
    14.         if {kyle::%player%} is true:
    15.             cancel event
    16.         else:
    17.             if {ammo} is greater than 0:
    18.                 shoot an egg from player's head at speed 4.5
    19.                 set {bulletegg.%player%} to 1
    20.                 play sound "entity.generic.explode" from master category at volume 0.35 with pitch 2 at player's location
    21.                 wait 2 ticks
    22.                 set {ammo} to {ammo} - 1
    23.                 send action bar "%{_ammo}% / 31" to player
    24.             if {ammo} is equal to 0:
    25.                 play sound "BLOCK_DISPENSER_DISPENSE" at volume 0.35 with pitch 2 at player's location
    26.                 cancel event
    No errors whatsoever.

    If I were to have two of the same item with the command /m4a1, they would both have the same nbt and name. If I shoot one gun, the {ammo} changes for both of them because it isn't unique to each specific tool. My question being, how can I make each NBT unique to the held item, so that when I shoot from one gun, it doesn't drain {ammo} of the other?

     
  2. Best Answer:
    Post #6 by Minecoll_YT, Jul 17, 2021
  3. Minecoll_YT

    Supporter Forums Helper

    Joined:
    Dec 2, 2018
    Messages:
    597
    Likes Received:
    37
    You didn't use NBT for ammunition, you used a global, non-specific variable. What do you want to use for the ammunition now? A variable or nbt?
     
  4. Nutrition35

    Nutrition35 New Member

    Joined:
    Feb 11, 2020
    Messages:
    9
    Likes Received:
    0
    Either one if it works. I want ammo that works specifically to one Gun. Not if I switch between two of the same guns it uses the same variable.
     
  5. Minecoll_YT

    Supporter Forums Helper

    Joined:
    Dec 2, 2018
    Messages:
    597
    Likes Received:
    37
    the actuall weapon part has to work, not sure if I made that giving the weapon part right
    Code (Text):
    1. function setItemTag(item: item, tag: string, value: object) :: item:
    2.     set {_nbt} to nbt compound of {_item}
    3.     set tag "tag;custom;%{_tag}%" of {_nbt} to {_value}
    4.     return item from nbt {_nbt}
    5.  
    6. function getItemTag(item: item, tag: string) :: object:
    7.     set {_tag} to tag "custom;%{_tag}%" of nbt of {_item}
    8.     return {_tag}
    9.  
    10. function removeItemTag(item: item, tag: string) :: item:
    11.     set {_nbt} to nbt compound of {_item}
    12.     delete tag "tag;custom;%{_tag}%" of {_nbt}
    13.     return item from nbt {_nbt}
    14.  
    15. command /getgun:
    16.   trigger:
    17.     set {_M4A1} to 1 leather horse armor named "&bM4A1"
    18.     add "{ammunition:31}" to nbt of {_M4A1}
    19.     give player {_M4A1}
    20.  
    21. on right click:
    22.   if name of player's tool contains "&bM4A1":
    23.     if {kyle::%player%} is not true:
    24.       set {_ammunition} to getItemTag(player's tool, "ammunition")
    25.       if {_ammunition} is more than 0:
    26.         shoot an egg from player's head at speed 4.5
    27.         play sound "entity.generic.explode" from master category at volume 0.35 with pitch 2 at player's location
    28.         set player's tool to setItemTag(player's tool, "ammunition", {_ammunition}-1)
    29.       else:
    30.         play sound "BLOCK_DISPENSER_DISPENSE" at volume 0.35 with pitch 2 at player's location
    --- Double Post Merged, Jul 17, 2021, Original Post Date: Jul 17, 2021 ---
    if the giving gun part is not working try:
    Code (Text):
    1. command /getgun:
    2.   trigger:
    3.     set {_M4A1} to 1 leather horse armor named "&bM4A1"
    4.     set {_nbt} to nbt compound of {_M4A1}
    5.     set tag "tag;custom;ammunition" of {_nbt} to 32
    6.     give player {_M4A1}
     
  6. Nutrition35

    Nutrition35 New Member

    Joined:
    Feb 11, 2020
    Messages:
    9
    Likes Received:
    0
    I appreciate your macro amounts of effort toward my problem. However, giving myself the gun plays the dispenser noise with both /getgun commands. Also, I left out a bit of code since I didn't think it would be necessary, but now I do think it is.

    Here is entire code now as it is. My issue is no errors on reloading, but when I rightclick, it plays the dispenser noise as if _ammunition was <none>, and leftclick does nothing. Thank you very muc hfor your time and effort. Its very appreciated.

    Code (Text):
    1. on join:
    2.     set {kyle::%player%} to false
    3. on spawn:
    4.     if event-entity is chicken:
    5.         cancel event
    6.  
    7. function setItemTag(item: item, tag: string, value: object) :: item:
    8.     set {_nbt} to nbt compound of {_item}
    9.     set tag "tag;custom;%{_tag}%" of {_nbt} to {_value}
    10.     return item from nbt {_nbt}
    11.  
    12. function getItemTag(item: item, tag: string) :: object:
    13.     set {_tag} to tag "custom;%{_tag}%" of nbt of {_item}
    14.     return {_tag}
    15.  
    16. function removeItemTag(item: item, tag: string) :: item:
    17.     set {_nbt} to nbt compound of {_item}
    18.     delete tag "tag;custom;%{_tag}%" of {_nbt}
    19.     return item from nbt {_nbt}
    20.  
    21. command /getgun:
    22.   trigger:
    23.     set {_M4A1} to 1 leather horse armor named "&bM4A1"
    24.     set {_nbt} to nbt compound of {_M4A1}
    25.     set tag "tag;custom;ammunition" of {_nbt} to 32
    26.     give player {_M4A1}
    27.  
    28. on right click:
    29.   if name of player's tool contains "&bM4A1":
    30.     if {kyle::%player%} is not true:
    31.       set {_ammunition} to getItemTag(player's tool, "ammunition")
    32.       if {_ammunition} is more than 0:
    33.         shoot an egg from player's head at speed 4.5
    34.         play sound "entity.generic.explode" from master category at volume 0.35 with pitch 2 at player's location
    35.         set player's tool to setItemTag(player's tool, "ammunition", {_ammunition}-1)
    36.       else:
    37.         play sound "BLOCK_DISPENSER_DISPENSE" at volume 0.35 with pitch 2 at player's location
    38.  
    39. on leftclick:
    40.     if name of player's tool contains "&bM4A1":
    41.         cancel event
    42.         set {_ammunition} to getItemTag(player's tool, "ammunition")
    43.         if {_ammunition} is equal to 31:
    44.             cancel event
    45.         if {_ammunition} is less than 31:
    46.             if player has 1 spruce door named "&cAmmo Clip":
    47.                 {reloading.%player%} = false:
    48.                     set {kyle::%player%} to true
    49.                     remove spruce door named "&cAmmo Clip" from player's inventory
    50.                     play sound "block.wooden_door.open" from master category with pitch 2 at player's location
    51.                     set {reloading.%player%} to true
    52.                     wait 2 seconds
    53.                     play sound "block.wooden_door.close" from master category with pitch 2 at player's location
    54.                     wait 8 ticks
    55.                     play sound "block.wooden_door.close" from master category with pitch 2 at player's location
    56.                     set {_ammunition} to 31
    57.                     set {reloading.%player%} to false
    58.                     set {kyle::%player%} to false
    59.                     send action bar "%{_ammunition}% / 31" to player
    60.             else:
    61.                 cancel event
    62. on sneak toggle:
    63.   name of player's tool contains "&bM4A1"
    64.   if player is not sneaking:
    65.     apply slowness 1 to player
    66.   else:
    67.     remove slowness from player
    68.  
    69.  
    70. on damage:
    71.     event-projectile is egg:
    72.         {bulletegg.%player%} = 1
    73.         damage victim by 2 hearts
    74.  
    75. command /clip [<integer>]:
    76.     trigger:
    77.         give player arg-1 of spruce door named "&cAmmo Clip"
     
  7. Minecoll_YT

    Supporter Forums Helper

    Joined:
    Dec 2, 2018
    Messages:
    597
    Likes Received:
    37
    Code (Text):
    1. function setItemTag(item: item, tag: string, value: object) :: item:
    2.         set {_nbt} to nbt compound of {_item}
    3.         set tag "tag;custom;%{_tag}%" of {_nbt} to {_value}
    4.         return item from nbt {_nbt}
    5.  
    6. function getItemTag(item: item, tag: string) :: object:
    7.         set {_tag} to tag "custom;%{_tag}%" of nbt of {_item}
    8.         return {_tag}
    9.  
    10. function removeItemTag(item: item, tag: string) :: item:
    11.         set {_nbt} to nbt compound of {_item}
    12.         delete tag "tag;custom;%{_tag}%" of {_nbt}
    13.         return item from nbt {_nbt}
    14.  
    15. on quit:
    16.     delete {gun::reloading::%player%}
    17.  
    18. command /getgun:
    19.     trigger:
    20.         give player leather horse armor named "&bM4A1"
    21.  
    22. command /clip [<integer>]:
    23.     trigger:
    24.         give player arg-1 of spruce door named "&cAmmo Clip"
    25.  
    26. on right click:
    27.     if name of player's tool contains "&bM4A1":
    28.         if {kyle::%player%} is not true:
    29.             set {_ammunition} to getItemTag(player's tool, "ammunition")
    30.             if {_ammunition} is "ammunition":
    31.                 set player's tool to setItemTag(player's tool, "ammunition", 30)
    32.                 set {_ammunition} to getItemTag(player's tool, "ammunition")
    33.             if {_ammunition} is more than 0:
    34.                 send action bar "&c%{_ammunition}% &8/ &a31" to player
    35.                 shoot an egg from player's head at speed 4.5
    36.                 play sound "entity.generic.explode" from master category at volume 0.35 with pitch 2 at player's location
    37.                 set player's tool to setItemTag(player's tool, "ammunition", {_ammunition}-1)
    38.             else:
    39.                 send action bar "&cOUT OF AMMUNITION" to player
    40.                 play sound "BLOCK_DISPENSER_DISPENSE" at volume 0.35 with pitch 2 at player's location
    41.  
    42. on leftclick:
    43.     if name of player's tool contains "&bM4A1":
    44.         cancel event
    45.         set {_tool} to tool of player
    46.         set {_ammunition} to getItemTag(player's tool, "ammunition")
    47.         if {_ammunition} is less than 31:
    48.             if player has 1 spruce door named "&cAmmo Clip":
    49.                 if {gun::reloading::%player%} is not true:
    50.                     send action bar "&eRELOADING..." to player
    51.                     set {gun::reloading::%player%} to true
    52.                     remove spruce door named "&cAmmo Clip" from player's inventory
    53.                     play sound "block.wooden_door.open" from master category with pitch 2 at player's location
    54.                     if tool of player is {_tool}:
    55.                         wait 2 seconds
    56.                         play sound "block.wooden_door.close" from master category with pitch 2 at player's location
    57.                         if tool of player is {_tool}:
    58.                             wait 8 ticks
    59.                             play sound "block.wooden_door.close" from master category with pitch 2 at player's location
    60.                             set player's tool to setItemTag(player's tool, "ammunition", 31)
    61.                             send action bar "&aRELOAD COMPLETE" to player
    62.                             delete {gun::reloading::%player%}
    63.                         else:
    64.                             give player spruce door named "&cAmmo Clip"
    65.                             send action bar "&cRELOAD CANCELED" to player
    66.                             delete {gun::reloading::%player%}
    67.                     else:
    68.                         give player spruce door named "&cAmmo Clip"
    69.                         send action bar "&cRELOAD CANCELED" to player
    70.                         delete {gun::reloading::%player%}
    71.             else:
    72.                 send action bar "&eYOU DO NOT HAVE AN AMMUNITION CLIP" to player
    73.         else:
    74.             send action bar "&eYOUR WEAPON IS RELOADED ALREADY" to player
     
    #6 Minecoll_YT, Jul 17, 2021
    Last edited: Jul 17, 2021
  8. Nutrition35

    Nutrition35 New Member

    Joined:
    Feb 11, 2020
    Messages:
    9
    Likes Received:
    0
    Thank you very much. This functions as it's supposed to.
     
  9. Minecoll_YT

    Supporter Forums Helper

    Joined:
    Dec 2, 2018
    Messages:
    597
    Likes Received:
    37
    Hey, I marked this thread as solved and marked the best solution! To keep the forum tidy, we ask everyone to mark a solved thread as such (In the upper right corner → "Thread options"), also you can mark the best answer to give the user a small thank you and additionally this will be displayed at the top which helps others to find the best solution directly.
     
    • Like Like x 1
Thread Status:
Not open for further replies.

Share This Page

Loading...