Using two variables to make a combined product (Minecraft Command Blocks)

Using two variables to make a combined product (Minecraft Command Blocks) - Content female customer with long curly hair wearing casual outfit sitting at wooden table with netbook in classic interior restaurant while making online order

My goal is to make a pig have its name and its lvl displayed on the same text

but to do this I need to combine "name" and "its lvl" is there any way that I can take both variables and put them in one string

Additional Info I am right now taking the pigs name from a sign and its lvl from a different one I can put one name on the pig but not both

on a multiplayer server, 1.16 is my version, i am in java

my end goal is I'm also trying to make a hypixel skyblock health system if you are wondering



Best Answer

You can simply do this by using this command:

summon pig [x] [y] [z] {CustomName:"\"<name and level>\""}

This command will spawn a pig with the name and level you choose, although this will not change the pig itself, only its name, the pig will remain the same as it would be if you spawned it with a spawn egg. If you want to change the pigs health and other attributes you can do this with this command:

summon pig [x] [y] [z] {CustomName:"\"<name and level>\"",ActiveEffects:[{Id:21,Amplifier:<health amount>,Duration:999999}]}

This command simply adds an effect to the pig that boosts its health for an infinite amount of time where, {health amount} is the number put you can input to change the health, the number has to be between 1 and 255, you can also change the id to another effect or add one using this template, you just have to know the effect id, which you can easily find on the internet, or more simply use a command generator.

If you want to change the color of the pig's name and level just put this in the name slot:

[{"text":"<name/level>","color":"<color, red, blue, etc>"}]

If you want to change the name for both the name and level use this:

[{"text":"<level/name>","color":"<color>"},{"text":"<level/name>","color":"<color>"}]

Ex:

summon pig [x] [y] [z] {CustomName:"\"[{"text":"{Lvl:3}","color":"grey"},{"text":"Pig","color":"Red"}]\"",ActiveEffects [{Id:21,Amplifier:{health amount},Duration:999999}]}

Finally since you are on a server, you can add plugins that can do this for you, the plugins will have much more configurations for the mob, (such as showing the mobs current health).

Notice: I haven't yet tested if the color works, but if it doesn't you can just use the first and/or second command.

Hope this helps! If you have any questions feel free to ask me!

(Also this is my first time doing this)




Pictures about "Using two variables to make a combined product (Minecraft Command Blocks)"

Using two variables to make a combined product (Minecraft Command Blocks) - Unrecognizable crop African American female pouring essential oil in glass bottle while making liquid incense at table
Using two variables to make a combined product (Minecraft Command Blocks) - Concentrated redhead female wearing light apron kneading clay in baking pan while creating in modern apartment
Using two variables to make a combined product (Minecraft Command Blocks) - Crop hacker silhouette typing on computer keyboard while hacking system



Can you use variables in Minecraft commands?

Minecraft commands do have a form of variables, but, unfortunately, they can only store numeric values. This is called the scoreboard, and you can add and access variables by using the /scoreboard command.

How do you give multiple items in Minecraft using commands?

This lets you give yourself \u2014 or another player in your world \u2014 any amount of almost any item. You'll type it as /give PlayerName ItemName Quantity. So for example, if your username was JohnDoe and you wanted to give yourself 30 diamonds, you'd type /give JohnDoe diamond 30.

How do you chain a command block together?

Command Block Chains
  • Place another command block next to the existing block. ...
  • Open the command block interface.
  • Set the Block Type to Chain.
  • Set the Condition to Conditional.
  • Set the Redstone to Always Active.
  • Set the Command Input to /testfor @p[tag=!placed_block]


  • How do you activate two command blocks at once?

    Place your first command block on the ground. It will face towards you, similar to how pistons face towards you. Enter the first command of your chain into the command block. No matter which command chain you are using, all subsequent commands in your chain use chain command blocks.



    Coding PAINTBALL in Minecraft With Only 8 Command Blocks in 10 Minutes!




    More answers regarding using two variables to make a combined product (Minecraft Command Blocks)

    Answer 2

    If you want to have a changeable level, you'll want to use a scoreboard to store the level of the pig.

    You can use the following command like this:

    scoreboard players add pigLvl dummy
    

    to create the required objective.

    Then if you want to change it, you can do this:

    scoreboard players set @e[type=pig,limit=1] pigLvl 2
    

    replacing 2 with whatever level.

    The trouble is to actually update the level on the pig's custom name whenever it is updated. The problem is that the name has to be manually updated every tick. You can't set it to a score and have it dynamically update.

    You would use the JSON text component score for this but unfortunately this cannot resolve the score to an actual text value. Therefore you must copy the new JSON text to a sign to have it resolved before putting it on the pig. The standard location for this sign is (?30000000, 0, 1603).

    The following commands, to be run by the pig to update name, will work for one pig at a time. If you want to have each pig update its name at once, you must use a function.

    tag @s add updatingName
    data modify block -30000000 0 1603 Text1 set value '{"score":{"name":"@e[tag=updatingName,limit=1]","objective":"pigLvl"}}'
    data modify entity @s CustomName set from block -30000000 0 1603 Text1
    tag @s remove updatingName
    

    Sources: Stack Exchange - This article follows the attribution requirements of Stack Exchange and is licensed under CC BY-SA 3.0.

    Images: Yan Krukov, Anna Shvets, Monstera, Anete Lusina