Skip to content

Fix guild shard formula

Mateusz Brawański requested to merge github/fork/ABCRic/patch-1 into master

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.

Merge request reports

Loading