(Java Minecraft 1.14) How to save a player's scoreboard team temporarily for later use?

I'm writing a datapack for a server I play on, and want to implement a feature like Xisumavoid's AFK datapack. What it does is it changes any AFK player's nametag to gray color in the tab menu, presumably by adding them to an AFK team. Unfortunately, my server already has teams on it, so I need to store the team that a player is on to put them back on it when they are no longer AFK. I have thought of a numeric method for accomplishing this, so am asking here simply to find if there is a simpler or easier way to do it other than my way, and to provide a method for doing this for future command users.

First, I give each team on the server a numeric, hard-coded integer value, from 1...n where n is the number of teams (0 can be unaffiliated/no team, and there is no number for the AFK team). Secondly, I set up a scoreboard which keeps track of a player's current team. Thirdly, in a tick function, I execute as all players on team one, setting their score in the scoreboard to 1. Same procedure for teams 2 through n. Finally, if a player has been still for long enough to be "AFK" I remove them from their team and add them to the afk team. When they move again I remove them from the AFK team and, depending on their score in the scoreboard, add them to the appropriate team (if their score is 1, they will be added to team one, same for teams 2 through n).

Is there a simpler way? If there is not I will add my function(s) here in an edit later.


Solution 1:

No, there is no simpler way, your solution is pretty close to what I would have done as well. You can't use team names, identifiers or colours in any dynamic way in Minecraft commands, so you would have to have commands per team anyway.

Solution 2:

Make an AFK objective with "walk 1 cm" criteria. command is like this:

/scoreboard objectives add AFK minecraft.custom:minecraft.walk_one_cm

Make a SLOW redstone clock (don't use repeating command block because it's fast) something like 2 fully-delayed repeaters next to 2 other fully-delayed repeaters in opposite direction linked with redstone dust on both sides obviously. And then on one side you place a redstone line linked to an impulse command block with the following command

/scoreboard players remove @a AFK 10

So that players who are moving will always surpass the score-removing clock keeping them unable to reach 0 AFK score, but for the players who are not moving they might even get a negative score if they stand still and there comes the repeating command block(Always Active) that detects players with AFK score of 0 or less, you can place it anywhere, it doesn't have to be linked to the clock, and in it you write the following command:

/execute if entity @a[score={AFK=..0}] run /scoreboard players reset @a[score={AFK=..0}] AFK

That way you remove the players(with AFK score of 0 or less) from the score tracker (so that the score doesn't keep going down because the score-removing clock) and then you place a comparator facing the opposite direction that the repeating command block is facing and then place an impulse command block also facing the same direction as the previous components and in this command block you do whatever you wanna do to the AFK players. And don't worry about them not being tracked by the score tracker, they will be tracked again once they start moving and the clock will automatically start affecting them again

i hope this was helpful for you or any other one with the same problem.