44import com .google .common .hash .HashingInputStream ;
55import com .google .common .hash .HashingOutputStream ;
66import net .fabricmc .loader .api .FabricLoader ;
7- import net .minecraft .util .Pair ;
7+ import net .minecraft .util .Tuple ;
88import net .modfest .rolesync .ModFestRoleSync ;
99import net .modfest .rolesync .SyncedRole ;
1010import org .jspecify .annotations .NonNull ;
1111
12- import java .io .*;
12+ import java .io .BufferedInputStream ;
13+ import java .io .BufferedOutputStream ;
14+ import java .io .DataInputStream ;
15+ import java .io .DataOutputStream ;
16+ import java .io .FileInputStream ;
17+ import java .io .FileNotFoundException ;
18+ import java .io .FileOutputStream ;
19+ import java .io .IOException ;
20+ import java .io .OutputStream ;
1321import java .nio .file .Files ;
1422import java .nio .file .Path ;
15- import java .util .*;
23+ import java .util .ArrayList ;
24+ import java .util .Arrays ;
25+ import java .util .Comparator ;
26+ import java .util .List ;
27+ import java .util .UUID ;
1628import java .util .concurrent .locks .ReentrantLock ;
1729import java .util .function .Consumer ;
1830import java .util .function .Supplier ;
@@ -23,7 +35,7 @@ public class CacheManager {
2335 private static final long MAGIC = 0xBEBE220da001BEBEL ;
2436 private final @ NonNull Path location ;
2537 private byte [] hash ;
26- private ReentrantLock writeLock = new ReentrantLock ();
38+ private final ReentrantLock writeLock = new ReentrantLock ();
2739
2840 public CacheManager () {
2941 this (FabricLoader .getInstance ().getGameDir ().resolve ("platform_role_cache.bin" ));
@@ -33,7 +45,7 @@ public CacheManager(@NonNull Path location) {
3345 this .location = location ;
3446 }
3547
36- public void read (Consumer <Stream <Pair <UUID ,SyncedRole >>> onReadFinished ) {
48+ public void read (Consumer <Stream <Tuple <UUID ,SyncedRole >>> onReadFinished ) {
3749 // No protection is needed against two threads reading at once. The callback already
3850 // has locking and checks the time of each read
3951 var readThread = new Thread (() -> {
@@ -47,7 +59,7 @@ public void read(Consumer<Stream<Pair<UUID,SyncedRole>>> onReadFinished) {
4759 return ;
4860 }
4961 var len = s .readInt ();
50- var contents = new ArrayList <Pair <UUID ,SyncedRole >>(len );
62+ var contents = new ArrayList <Tuple <UUID ,SyncedRole >>(len );
5163
5264 for (int i = 0 ; i < len ; i ++) {
5365 var uuid = new UUID (s .readLong (), s .readLong ());
@@ -57,7 +69,7 @@ public void read(Consumer<Stream<Pair<UUID,SyncedRole>>> onReadFinished) {
5769 case 8 -> SyncedRole .PARTICIPANT ;
5870 default -> throw new IOException ("Invalid role in cache" );
5971 };
60- contents .add (new Pair <>(uuid , role ));
72+ contents .add (new Tuple <>(uuid , role ));
6173 }
6274 // We only set the hash and call the callback if we successfully read the whole file.
6375 // It's possible for the file to be corrupt (especially since our writes aren't atomic),
@@ -77,7 +89,7 @@ public void read(Consumer<Stream<Pair<UUID,SyncedRole>>> onReadFinished) {
7789 readThread .start ();
7890 }
7991
80- public void write (Supplier <Stream <Pair <UUID ,SyncedRole >>> provider ) {
92+ public void write (Supplier <Stream <Tuple <UUID ,SyncedRole >>> provider ) {
8193 // Run on a separate thread to not bog down the server with IO
8294 // This function should only be running whenever the server shuts down (or
8395 // something in the configuration changes), so there's little performance concern
@@ -87,7 +99,7 @@ public void write(Supplier<Stream<Pair<UUID,SyncedRole>>> provider) {
8799 try {
88100 // Ensure the list is consistent by sorting it by uuid
89101 var list = provider .get ().collect (Collectors .toCollection (ArrayList ::new ));
90- list .sort (Comparator .comparing (Pair :: getLeft ));
102+ list .sort (Comparator .comparing (Tuple :: getA ));
91103 // We write the collection to a hashing output first, and we only save the file if
92104 // the hash changes
93105 var hash = Hashing .sha512 ();
@@ -117,14 +129,14 @@ public void write(Supplier<Stream<Pair<UUID,SyncedRole>>> provider) {
117129 writeThread .start ();
118130 }
119131
120- private void write (List <Pair <UUID ,SyncedRole >> data , OutputStream stream ) throws IOException {
132+ private void write (List <Tuple <UUID ,SyncedRole >> data , OutputStream stream ) throws IOException {
121133 var s = new DataOutputStream (stream );
122134 s .writeLong (MAGIC );
123135 s .writeInt (data .size ());
124136 for (var p : data ) {
125- s .writeLong (p .getLeft ().getMostSignificantBits ());
126- s .writeLong (p .getLeft ().getLeastSignificantBits ());
127- s .writeByte (switch (p .getRight ()) {
137+ s .writeLong (p .getA ().getMostSignificantBits ());
138+ s .writeLong (p .getA ().getLeastSignificantBits ());
139+ s .writeByte (switch (p .getB ()) {
128140 case TEAM -> 1 ;
129141 case PARTICIPANT -> 8 ;
130142 });
0 commit comments