Fix guild shard formula
Created by: ABCRic
Summary
The guild shard calculation is wrong because the shifted value is cast to int
before the modulo operation.
Details
Here's a failing example:
ulong guildId = 860338524707094538UL;
int shardCount = 5;
return (int)(guildId >> 22) % shardCount; // returns -1
Operation precedence makes the result of (int)(guildId >> 22)
be -1037741601, as the intermediate value has too many bits for int
, which is incorrect.
Changes proposed
The PR makes the cast happen after the modulo:
(int)((guildId >> 22) % (ulong)shardCount)
The ulong
cast of shardCount
makes the compiler happy.
Notes
This also fixes GetShard() returning null in cases where it shouldn't.