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

  • 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!

  • LOOKING FOR A VERSION OF SKRIPT?

    You can always check out skUnity Downloads for downloads and any other information about Skript!

Status
Not open for further replies.

Nutrition35

Member
Feb 11, 2020
11
0
1
19
Skript Version: 2.5.3
Skript Author: YourSpeedDealer
Minecraft Version: 1.16.5
Full Code:

Code:
command /m4a1:
    trigger:
        set {_M4A1} to 1 leather horse armor named "&bM4A1"
        add "{maxDurabilityM4:31}" to nbt of {_M4A1}
        set {ammo} to tag "maxDurabilityM4" of nbt of {_M4A1}
        broadcast "%{ammo}%"
        give player {_M4A1}


on rightclick:
    if name of player's tool contains "&bM4A1":
        broadcast "%{ammo}%"
        if {kyle::%player%} is true:
            cancel event
        else:
            if {ammo} is greater than 0:
                shoot an egg from player's head at speed 4.5
                set {bulletegg.%player%} to 1
                play sound "entity.generic.explode" from master category at volume 0.35 with pitch 2 at player's location
                wait 2 ticks
                set {ammo} to {ammo} - 1
                send action bar "%{_ammo}% / 31" to player
            if {ammo} is equal to 0:
                play sound "BLOCK_DISPENSER_DISPENSE" at volume 0.35 with pitch 2 at player's location
                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?
 
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?
 
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?
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.
 
the actuall weapon part has to work, not sure if I made that giving the weapon part right
Code:
function setItemTag(item: item, tag: string, value: object) :: item:
    set {_nbt} to nbt compound of {_item}
    set tag "tag;custom;%{_tag}%" of {_nbt} to {_value}
    return item from nbt {_nbt}
 
function getItemTag(item: item, tag: string) :: object:
    set {_tag} to tag "custom;%{_tag}%" of nbt of {_item}
    return {_tag}
 
function removeItemTag(item: item, tag: string) :: item:
    set {_nbt} to nbt compound of {_item}
    delete tag "tag;custom;%{_tag}%" of {_nbt}
    return item from nbt {_nbt}

command /getgun:
  trigger:
    set {_M4A1} to 1 leather horse armor named "&bM4A1"
    add "{ammunition:31}" to nbt of {_M4A1}
    give player {_M4A1}

on right click:
  if name of player's tool contains "&bM4A1":
    if {kyle::%player%} is not true:
      set {_ammunition} to getItemTag(player's tool, "ammunition")
      if {_ammunition} is more than 0:
        shoot an egg from player's head at speed 4.5
        play sound "entity.generic.explode" from master category at volume 0.35 with pitch 2 at player's location
        set player's tool to setItemTag(player's tool, "ammunition", {_ammunition}-1)
      else:
        play sound "BLOCK_DISPENSER_DISPENSE" at volume 0.35 with pitch 2 at player's location
[doublepost=1626543122,1626542994][/doublepost]if the giving gun part is not working try:
Code:
command /getgun:
  trigger:
    set {_M4A1} to 1 leather horse armor named "&bM4A1"
    set {_nbt} to nbt compound of {_M4A1}
    set tag "tag;custom;ammunition" of {_nbt} to 32
    give player {_M4A1}
 
the actuall weapon part has to work, not sure if I made that giving the weapon part right
Code:
function setItemTag(item: item, tag: string, value: object) :: item:
    set {_nbt} to nbt compound of {_item}
    set tag "tag;custom;%{_tag}%" of {_nbt} to {_value}
    return item from nbt {_nbt}
 
function getItemTag(item: item, tag: string) :: object:
    set {_tag} to tag "custom;%{_tag}%" of nbt of {_item}
    return {_tag}
 
function removeItemTag(item: item, tag: string) :: item:
    set {_nbt} to nbt compound of {_item}
    delete tag "tag;custom;%{_tag}%" of {_nbt}
    return item from nbt {_nbt}

command /getgun:
  trigger:
    set {_M4A1} to 1 leather horse armor named "&bM4A1"
    add "{ammunition:31}" to nbt of {_M4A1}
    give player {_M4A1}

on right click:
  if name of player's tool contains "&bM4A1":
    if {kyle::%player%} is not true:
      set {_ammunition} to getItemTag(player's tool, "ammunition")
      if {_ammunition} is more than 0:
        shoot an egg from player's head at speed 4.5
        play sound "entity.generic.explode" from master category at volume 0.35 with pitch 2 at player's location
        set player's tool to setItemTag(player's tool, "ammunition", {_ammunition}-1)
      else:
        play sound "BLOCK_DISPENSER_DISPENSE" at volume 0.35 with pitch 2 at player's location
[doublepost=1626543122,1626542994][/doublepost]if the giving gun part is not working try:
Code:
command /getgun:
  trigger:
    set {_M4A1} to 1 leather horse armor named "&bM4A1"
    set {_nbt} to nbt compound of {_M4A1}
    set tag "tag;custom;ammunition" of {_nbt} to 32
    give player {_M4A1}

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:
on join:
    set {kyle::%player%} to false
on spawn:
    if event-entity is chicken:
        cancel event

function setItemTag(item: item, tag: string, value: object) :: item:
    set {_nbt} to nbt compound of {_item}
    set tag "tag;custom;%{_tag}%" of {_nbt} to {_value}
    return item from nbt {_nbt}
 
function getItemTag(item: item, tag: string) :: object:
    set {_tag} to tag "custom;%{_tag}%" of nbt of {_item}
    return {_tag}
 
function removeItemTag(item: item, tag: string) :: item:
    set {_nbt} to nbt compound of {_item}
    delete tag "tag;custom;%{_tag}%" of {_nbt}
    return item from nbt {_nbt}
 
command /getgun:
  trigger:
    set {_M4A1} to 1 leather horse armor named "&bM4A1"
    set {_nbt} to nbt compound of {_M4A1}
    set tag "tag;custom;ammunition" of {_nbt} to 32
    give player {_M4A1}
 
on right click:
  if name of player's tool contains "&bM4A1":
    if {kyle::%player%} is not true:
      set {_ammunition} to getItemTag(player's tool, "ammunition")
      if {_ammunition} is more than 0:
        shoot an egg from player's head at speed 4.5
        play sound "entity.generic.explode" from master category at volume 0.35 with pitch 2 at player's location
        set player's tool to setItemTag(player's tool, "ammunition", {_ammunition}-1)
      else:
        play sound "BLOCK_DISPENSER_DISPENSE" at volume 0.35 with pitch 2 at player's location

on leftclick:
    if name of player's tool contains "&bM4A1":
        cancel event
        set {_ammunition} to getItemTag(player's tool, "ammunition")
        if {_ammunition} is equal to 31:
            cancel event
        if {_ammunition} is less than 31:
            if player has 1 spruce door named "&cAmmo Clip":
                {reloading.%player%} = false:
                    set {kyle::%player%} to true
                    remove spruce door named "&cAmmo Clip" from player's inventory
                    play sound "block.wooden_door.open" from master category with pitch 2 at player's location
                    set {reloading.%player%} to true
                    wait 2 seconds
                    play sound "block.wooden_door.close" from master category with pitch 2 at player's location
                    wait 8 ticks
                    play sound "block.wooden_door.close" from master category with pitch 2 at player's location
                    set {_ammunition} to 31
                    set {reloading.%player%} to false
                    set {kyle::%player%} to false
                    send action bar "%{_ammunition}% / 31" to player
            else:
                cancel event
on sneak toggle:
  name of player's tool contains "&bM4A1"
  if player is not sneaking:
    apply slowness 1 to player
  else:
    remove slowness from player


on damage:
    event-projectile is egg:
        {bulletegg.%player%} = 1
        damage victim by 2 hearts

command /clip [<integer>]:
    trigger:
        give player arg-1 of spruce door named "&cAmmo Clip"
 
Code:
function setItemTag(item: item, tag: string, value: object) :: item:
        set {_nbt} to nbt compound of {_item}
        set tag "tag;custom;%{_tag}%" of {_nbt} to {_value}
        return item from nbt {_nbt}
 
function getItemTag(item: item, tag: string) :: object:
        set {_tag} to tag "custom;%{_tag}%" of nbt of {_item}
        return {_tag}
 
function removeItemTag(item: item, tag: string) :: item:
        set {_nbt} to nbt compound of {_item}
        delete tag "tag;custom;%{_tag}%" of {_nbt}
        return item from nbt {_nbt}

on quit:
    delete {gun::reloading::%player%}

command /getgun:
    trigger:
        give player leather horse armor named "&bM4A1"

command /clip [<integer>]:
    trigger:
        give player arg-1 of spruce door named "&cAmmo Clip"

on right click:
    if name of player's tool contains "&bM4A1":
        if {kyle::%player%} is not true:
            set {_ammunition} to getItemTag(player's tool, "ammunition")
            if {_ammunition} is "ammunition":
                set player's tool to setItemTag(player's tool, "ammunition", 30)
                set {_ammunition} to getItemTag(player's tool, "ammunition")
            if {_ammunition} is more than 0:
                send action bar "&c%{_ammunition}% &8/ &a31" to player
                shoot an egg from player's head at speed 4.5
                play sound "entity.generic.explode" from master category at volume 0.35 with pitch 2 at player's location
                set player's tool to setItemTag(player's tool, "ammunition", {_ammunition}-1)
            else:
                send action bar "&cOUT OF AMMUNITION" to player
                play sound "BLOCK_DISPENSER_DISPENSE" at volume 0.35 with pitch 2 at player's location

on leftclick:
    if name of player's tool contains "&bM4A1":
        cancel event
        set {_tool} to tool of player
        set {_ammunition} to getItemTag(player's tool, "ammunition")
        if {_ammunition} is less than 31:
            if player has 1 spruce door named "&cAmmo Clip":
                if {gun::reloading::%player%} is not true:
                    send action bar "&eRELOADING..." to player
                    set {gun::reloading::%player%} to true
                    remove spruce door named "&cAmmo Clip" from player's inventory
                    play sound "block.wooden_door.open" from master category with pitch 2 at player's location
                    if tool of player is {_tool}:
                        wait 2 seconds
                        play sound "block.wooden_door.close" from master category with pitch 2 at player's location
                        if tool of player is {_tool}:
                            wait 8 ticks
                            play sound "block.wooden_door.close" from master category with pitch 2 at player's location
                            set player's tool to setItemTag(player's tool, "ammunition", 31)
                            send action bar "&aRELOAD COMPLETE" to player
                            delete {gun::reloading::%player%}
                        else:
                            give player spruce door named "&cAmmo Clip"
                            send action bar "&cRELOAD CANCELED" to player
                            delete {gun::reloading::%player%}
                    else:
                        give player spruce door named "&cAmmo Clip"
                        send action bar "&cRELOAD CANCELED" to player
                        delete {gun::reloading::%player%}
            else:
                send action bar "&eYOU DO NOT HAVE AN AMMUNITION CLIP" to player
        else:
            send action bar "&eYOUR WEAPON IS RELOADED ALREADY" to player
 
Last edited:
Code:
function setItemTag(item: item, tag: string, value: object) :: item:
        set {_nbt} to nbt compound of {_item}
        set tag "tag;custom;%{_tag}%" of {_nbt} to {_value}
        return item from nbt {_nbt}
 
function getItemTag(item: item, tag: string) :: object:
        set {_tag} to tag "custom;%{_tag}%" of nbt of {_item}
        return {_tag}
 
function removeItemTag(item: item, tag: string) :: item:
        set {_nbt} to nbt compound of {_item}
        delete tag "tag;custom;%{_tag}%" of {_nbt}
        return item from nbt {_nbt}

on quit:
    delete {gun::reloading::%player%}

command /getgun:
    trigger:
        give player leather horse armor named "&bM4A1"

command /clip [<integer>]:
    trigger:
        give player arg-1 of spruce door named "&cAmmo Clip"

on right click:
    if name of player's tool contains "&bM4A1":
        if {kyle::%player%} is not true:
            set {_ammunition} to getItemTag(player's tool, "ammunition")
            if {_ammunition} is "ammunition":
                set player's tool to setItemTag(player's tool, "ammunition", 30)
                set {_ammunition} to getItemTag(player's tool, "ammunition")
            if {_ammunition} is more than 0:
                send action bar "&c%{_ammunition}% &8/ &a31" to player
                shoot an egg from player's head at speed 4.5
                play sound "entity.generic.explode" from master category at volume 0.35 with pitch 2 at player's location
                set player's tool to setItemTag(player's tool, "ammunition", {_ammunition}-1)
            else:
                send action bar "&cOUT OF AMMUNITION" to player
                play sound "BLOCK_DISPENSER_DISPENSE" at volume 0.35 with pitch 2 at player's location

on leftclick:
    if name of player's tool contains "&bM4A1":
        cancel event
        set {_tool} to tool of player
        set {_ammunition} to getItemTag(player's tool, "ammunition")
        if {_ammunition} is less than 31:
            if player has 1 spruce door named "&cAmmo Clip":
                if {gun::reloading::%player%} is not true:
                    send action bar "&eRELOADING..." to player
                    set {gun::reloading::%player%} to true
                    remove spruce door named "&cAmmo Clip" from player's inventory
                    play sound "block.wooden_door.open" from master category with pitch 2 at player's location
                    if tool of player is {_tool}:
                        wait 2 seconds
                        play sound "block.wooden_door.close" from master category with pitch 2 at player's location
                        if tool of player is {_tool}:
                            wait 8 ticks
                            play sound "block.wooden_door.close" from master category with pitch 2 at player's location
                            set player's tool to setItemTag(player's tool, "ammunition", 31)
                            send action bar "&aRELOAD COMPLETE" to player
                            delete {gun::reloading::%player%}
                        else:
                            give player spruce door named "&cAmmo Clip"
                            send action bar "&cRELOAD CANCELED" to player
                            delete {gun::reloading::%player%}
                    else:
                        give player spruce door named "&cAmmo Clip"
                        send action bar "&cRELOAD CANCELED" to player
                        delete {gun::reloading::%player%}
            else:
                send action bar "&eYOU DO NOT HAVE AN AMMUNITION CLIP" to player
        else:
            send action bar "&eYOUR WEAPON IS RELOADED ALREADY" to player

Thank you very much. This functions as it's supposed to.
 
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
Reactions: Nutrition35
Status
Not open for further replies.