I know there is a function in essentials to return the player´s public Trainer ID ($Trainer.publicID). Though there isn't a function which returns the player's secret ID; the remainder of the full trainer ID. I've looked at the function (publicID) to try and write the function I want myself, yet I don't understand how publicID works.
Could anyone explain me how PokeBattle_Trainer.publicID works so I can write a function which returns the secret ID?
$Trainer.id is the player's full 32-bit trainer ID. The upper 16 bits of this number are the secret ID, and the lower 16 bits are the public ID. Both numbers on their own go between 0 and 65535. The two numbers are stuck together in binary form, like so:
To get either the secret ID or public ID back from this, we're going to have to fiddle with the number's bits. There are several different ways you can do this.
return id ? id&0xFFFF : (@id&0xFFFF)
This is how you get the public ID. What it does is perform a bitwise AND operation on the ID number with the number 0xFFFF (where & is the symbol used for this operator, like how = is the symbol meaning "set to equal" or == means "check if equals"). The number 0xFFFF is a hexadecimal number (you know it's hex because it has 0x at the beginning - 0xFFFF is the number 65535), which in binary is the number 1111 1111 1111 1111. A bitwise AND operation looks at each bit in both numbers in turn, and in the result, makes that bit 1 if the first number's bit is 1 AND the second number's bit is 1 (or 0 otherwise).
There's a bunch of zeroes at the beginning of the result, but just like the number 00042, they can be ignored. The result is the public ID (in blue).
The same approach would work if you want to get the secret ID (the red number). Remember that you can stick zeroes in front of a number, so 0xFFFF is the same as 0x0000FFFF. If that got just the second half of the $Trainer.id, and we now want just the first half, how about using 0xFFFF0000 instead?
Success! Of sorts. We've gotten rid of the blue number, but we're still left with a bunch of zeroes at the end. 300 is a different number to 3, so we can't just ignore them. How do we get 3 from 300, then? Divide by 100! In this case, we want to divide by the binary number 1 0000 0000 0000 0000 to get rid of those 16 zeroes. In other words, divide by 65536.
Putting all this together, we get this code:
return (@id&0xFFFF0000) / 65536
This isn't the most elegant solution, but it'll do what you want. The most elegant solution involves bitwise shifting, but you've already got something that works so there's no point trying to explain something new just for the sake of having some slightly prettier code.
My explanation of binary/hexadecimal numbers and the bitwise AND operator is quite poor, I know. There's a nice page on Bulbapedia which explains these things too, and even though it's about calculating a Pokémon's gender/nature/ability/shininess from its personal ID, the maths and explanation is still good.
Thanks for the detailed reply. I think I get how it works now.
I'm still fairly inexperienced at coding and try to learn to do what I want by looking at other code. I already knew what binary and hexadecimal numbers where, yet wasn't familiar with the AND operator. I also haven't worked with binary and hexadecimal values before.