forked from ModFest/scattered-shards
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
179 additions
and
166 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
src/main/java/cn/zbx1425/scatteredshards/sync/FileSerializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package cn.zbx1425.scatteredshards.sync; | ||
|
||
import net.minecraft.util.Identifier; | ||
import net.modfest.scatteredshards.ScatteredShards; | ||
import net.modfest.scatteredshards.api.ShardCollection; | ||
import net.modfest.scatteredshards.api.impl.ShardCollectionImpl; | ||
|
||
import java.io.FileOutputStream; | ||
import java.io.IOException; | ||
import java.nio.file.FileAlreadyExistsException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.Map; | ||
import java.util.UUID; | ||
import java.util.stream.Stream; | ||
|
||
public class FileSerializer { | ||
|
||
private final Path basePath; | ||
|
||
public FileSerializer(Path basePath) { | ||
this.basePath = basePath; | ||
} | ||
|
||
public void loadInto(Map<UUID, ShardCollection> playerShardCollections) throws IOException { | ||
playerShardCollections.clear(); | ||
try { | ||
Files.createDirectories(basePath.resolve("collections")); | ||
} catch (Exception ignored) { } | ||
try (Stream<Path> userFiles = Files.list(basePath.resolve("collections"))) { | ||
for (Path userFile : userFiles.toList()) { | ||
try { | ||
String[] fileNameParts = userFile.getFileName().toString().split("\\."); | ||
if (fileNameParts.length != 2 || !fileNameParts[1].equals("txt")) continue; | ||
ShardCollection collection = new ShardCollectionImpl(); | ||
for (String line : Files.readAllLines(userFile)) { | ||
if (line.isEmpty()) continue; | ||
Identifier shardId = Identifier.tryParse(line); | ||
if (shardId != null) collection.add(shardId); | ||
} | ||
playerShardCollections.put(UUID.fromString(fileNameParts[0]), collection); | ||
} catch (IOException ex) { | ||
ScatteredShards.LOGGER.error("Failed to load shard collections from disk for file " + userFile.getFileName(), ex); | ||
} | ||
} | ||
} | ||
} | ||
|
||
private Path getUserPath(UUID bearer) { | ||
return basePath.resolve("collections") | ||
.resolve(bearer.toString() + ".txt"); | ||
} | ||
|
||
public void write(UUID bearer, ShardCollection existingEntry) throws IOException { | ||
Path targetFile = getUserPath(bearer); | ||
try (FileOutputStream fos = new FileOutputStream(targetFile.toFile())) { | ||
for (Identifier shardId : existingEntry) { | ||
fos.write((shardId.toString() + "\n").getBytes()); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 0 additions & 24 deletions
24
src/main/java/cn/zbx1425/scatteredshards/sync/SyncDispatcher.java
This file was deleted.
Oops, something went wrong.
81 changes: 81 additions & 0 deletions
81
src/main/java/cn/zbx1425/scatteredshards/sync/SyncPersistDispatcher.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package cn.zbx1425.scatteredshards.sync; | ||
|
||
import net.minecraft.server.MinecraftServer; | ||
import net.minecraft.util.Identifier; | ||
import net.minecraft.util.WorldSavePath; | ||
import net.modfest.scatteredshards.ScatteredShards; | ||
import net.modfest.scatteredshards.api.ShardCollection; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.Path; | ||
import java.util.Map; | ||
import java.util.UUID; | ||
|
||
public class SyncPersistDispatcher implements Synchronizer { | ||
|
||
public final MinecraftServer server; | ||
public final Path basePath; | ||
|
||
public boolean isHost; | ||
public final Synchronizer peerChannel; | ||
public final FileSerializer persistAccess; | ||
|
||
public static SyncPersistDispatcher CURRENT; | ||
|
||
public SyncPersistDispatcher(MinecraftServer server, boolean isHost, Synchronizer peerChannel) { | ||
this.server = server; | ||
this.basePath = Path.of(server.getSavePath(WorldSavePath.ROOT).toString(), "scattered_shards"); | ||
this.isHost = isHost; | ||
this.peerChannel = peerChannel; | ||
this.persistAccess = new FileSerializer(basePath); | ||
} | ||
|
||
public void loadFromToShareOrDiskAndInto(Map<UUID, ShardCollection> playerShardCollections) { | ||
if (isHost) { | ||
try { | ||
persistAccess.loadInto(playerShardCollections); | ||
} catch (IOException e) { | ||
ScatteredShards.LOGGER.error("Failed to load shard collections from disk", e); | ||
} | ||
peerChannel.writeAllToShare(playerShardCollections); | ||
} else { | ||
peerChannel.readAllFromShareInto(playerShardCollections); | ||
} | ||
} | ||
|
||
@Override | ||
public void notifyCollectionChange(UUID bearer, ShardCollection newEntry) { | ||
if (!isHost) return; | ||
peerChannel.notifyCollectionChange(bearer, newEntry); | ||
try { | ||
persistAccess.write(bearer, newEntry); | ||
} catch (IOException e) { | ||
ScatteredShards.LOGGER.error("Failed to persist shard collection for player " + bearer, e); | ||
} | ||
} | ||
|
||
@Override | ||
public void notifyCollect(UUID bearer, Identifier shardId) { | ||
peerChannel.notifyCollect(bearer, shardId); | ||
} | ||
|
||
@Override | ||
public void notifyUncollect(UUID bearer, Identifier shardId) { | ||
peerChannel.notifyUncollect(bearer, shardId); | ||
} | ||
|
||
@Override | ||
public void writeAllToShare(Map<UUID, ShardCollection> collections) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public void readAllFromShareInto(Map<UUID, ShardCollection> collections) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public void close() throws Exception { | ||
peerChannel.close(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.