Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
* SlimeLoaders are in charge of loading worlds
Expand Down Expand Up @@ -50,6 +51,15 @@ public interface SlimeLoader {
*/
void saveWorld(String worldName, byte[] serializedWorld) throws IOException;

/**
* Saves multiple worlds at once. This method will also
* lock the worlds, in case they're not locked already.
*
* @param worlds a map containing the world names as keys and their data files as values
* @throws IOException if the worlds could not be saved.
*/
void saveWorlds(Map<String, byte[]> worlds) throws IOException;

/**
* Deletes a world from the data source.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.time.Duration;
import java.util.Base64;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
Expand Down Expand Up @@ -174,6 +175,11 @@ public void saveWorld(String worldName, byte[] serializedWorld) throws IOExcepti
this.logger.warn("Illegal call to saveWorld: API Worlds cannot be saved. They're always read-only.");
}

@Override
public void saveWorlds(Map<String, byte[]> worlds) throws IOException {
this.logger.warn("Illegal call to saveWorld: API Worlds cannot be saved. They're always read-only.");
}

@Override
public void deleteWorld(String worldName) {
this.logger.warn("Illegal call to deleteWorld: API Worlds cannot be deleted through the loader.");
Expand Down
2 changes: 1 addition & 1 deletion loaders/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ dependencies {
api(project(":loaders:api-loader"))
api(project(":loaders:file-loader"))
api(project(":loaders:mongo-loader"))
api(project(":loaders:mysql-loader"))
api(project(":loaders:sql-loader"))
api(project(":loaders:redis-loader"))

compileOnly(paperApi())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.nio.file.NotDirectoryException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class FileLoader implements SlimeLoader {
Expand Down Expand Up @@ -63,6 +64,16 @@ public void saveWorld(String worldName, byte[] serializedWorld) throws IOExcepti
}
}

@Override
public void saveWorlds(Map<String, byte[]> worlds) throws IOException {
for (Map.Entry<String, byte[]> entry : worlds.entrySet()) {
File file = new File(worldDir, entry.getKey() + ".slime");
try (FileOutputStream fos = new FileOutputStream(file)) {
fos.write(entry.getValue());
}
}
}

@Override
public void deleteWorld(String worldName) throws UnknownWorldException, IOException {
if (!worldExists(worldName)) {
Expand Down
2 changes: 1 addition & 1 deletion loaders/mongo-loader/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ dependencies {
compileOnly(project(":api"))
compileOnly(paperApi())

api(libs.mongo)
compileOnlyApi(libs.mongo)
}

publishConfiguration {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class MongoLoader extends UpdatableLoader {

Expand Down Expand Up @@ -179,6 +180,35 @@ public void saveWorld(String worldName, byte[] serializedWorld) throws IOExcepti
}
}

@Override
public void saveWorlds(Map<String, byte[]> worlds) throws IOException {
MongoDatabase mongoDatabase = client.getDatabase(database);
GridFSBucket bucket = GridFSBuckets.create(mongoDatabase, collection);
MongoCollection<Document> mongoCollection = mongoDatabase.getCollection(collection);

List<WriteModel<Document>> bulkOps = new ArrayList<>();

for (Map.Entry<String, byte[]> entry : worlds.entrySet()) {
String worldName = entry.getKey();
byte[] data = entry.getValue();

GridFSFile oldFile = bucket.find(Filters.eq("filename", worldName)).first();
if (oldFile != null) {
bucket.delete(oldFile.getObjectId());
}

bucket.uploadFromStream(worldName, new ByteArrayInputStream(data));

Bson query = Filters.eq("name", worldName);
Bson update = Updates.set("name", worldName);
bulkOps.add(new UpdateOneModel<>(query, update, new UpdateOptions().upsert(true)));
}

if (!bulkOps.isEmpty()) {
mongoCollection.bulkWrite(bulkOps);
}
}

@Override
public void deleteWorld(String worldName) throws IOException, UnknownWorldException {
try {
Expand All @@ -203,5 +233,4 @@ public void deleteWorld(String worldName) throws IOException, UnknownWorldExcept
throw new IOException(ex);
}
}

}

This file was deleted.

2 changes: 1 addition & 1 deletion loaders/redis-loader/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ plugins {
dependencies {
compileOnly(project(":api"))

api(libs.lettuce)
compileOnlyApi(libs.lettuce)

compileOnly(paperApi())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class RedisLoader implements SlimeLoader {
Expand Down Expand Up @@ -55,6 +56,23 @@ public void saveWorld(String worldName, byte[] bytes) throws IOException {
connection.sadd(WORLD_LIST_PREFIX, worldName.getBytes(StandardCharsets.UTF_8));
}

public void saveWorlds(Map<String, byte[]> worlds) throws IOException {
if (worlds.isEmpty()) {
return;
}

try {
connection.mset(worlds);
byte[][] names = worlds.keySet().stream()
.map(str -> str.getBytes(StandardCharsets.UTF_8))
.toArray(byte[][]::new);

connection.sadd(WORLD_LIST_PREFIX, names);
} catch (Exception e) {
throw new IOException(e);
}
}

@Override
public void deleteWorld(String worldName) throws UnknownWorldException, IOException {
long deletedCount = connection.del(WORLD_DATA_PREFIX + worldName);
Expand Down
Loading