Skip to content
Open
Show file tree
Hide file tree
Changes from 3 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 @@ -84,4 +84,14 @@ public interface SlimeLoader {
*/
void deleteWorld(String worldName) throws UnknownWorldException, IOException;

/**
* Renames a world in the data source.
*
* @param oldWorldName current (old) name of the world
* @param newWorldName new name of the world
*
* @throws UnknownWorldException if the world could not be found.
* @throws IOException if the world could not be renamed.
*/
boolean renameWorld(String oldWorldName, String newWorldName) throws UnknownWorldException, IOException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,13 @@
import com.grinderwolf.swm.api.loaders.SlimeLoader;
import com.grinderwolf.swm.plugin.log.Logging;
import org.apache.commons.io.FileUtils;
import org.bukkit.Bukkit;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;
import java.nio.channels.OverlappingFileLockException;
import java.nio.file.NotDirectoryException;
import java.util.*;
Expand Down Expand Up @@ -174,4 +172,17 @@ public void deleteWorld(String worldName) throws UnknownWorldException {
}
}
}

@Override
public boolean renameWorld(String oldWorldName, String newWorldName) throws IOException {
if(this.isWorldLocked(oldWorldName)) {
return false;
}

return this.getWorldDir(oldWorldName).renameTo(this.getWorldDir(newWorldName));
}

private File getWorldDir(String worldName) {
return new File(this.worldDir, worldName + ".slime");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.mongodb.client.model.Updates;
import com.mongodb.client.result.UpdateResult;
import org.bson.Document;
import org.bson.conversions.Bson;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
Expand Down Expand Up @@ -285,4 +286,30 @@ public void deleteWorld(String worldName) throws IOException, UnknownWorldExcept
throw new IOException(ex);
}
}

@Override
public boolean renameWorld(String oldWorldName, String newWorldName) throws UnknownWorldException {
if(this.lockedWorlds.containsKey(oldWorldName)) {
return false;
}

MongoDatabase mongoDatabase = this.client.getDatabase(this.database);
MongoCollection<Document> mongoCollection = mongoDatabase.getCollection(this.collection);

Bson oldWorldNameFilter = Filters.eq("name", oldWorldName);
Document worldDoc = mongoCollection.find(oldWorldNameFilter).first();

GridFSBucket bucket = GridFSBuckets.create(mongoDatabase, this.collection);
GridFSFile file = bucket.find(Filters.eq("filename", oldWorldName)).first();

if(file == null || worldDoc == null) {
throw new UnknownWorldException(oldWorldName);
}

bucket.rename(file.getObjectId(), newWorldName);
worldDoc.put("name", newWorldName);

UpdateResult updateResult = mongoCollection.replaceOne(oldWorldNameFilter, worldDoc);
return updateResult.getModifiedCount() != 0;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public class MysqlLoader extends UpdatableLoader {
private static final String UPDATE_LOCK_QUERY = "UPDATE `worlds` SET `locked` = ? WHERE `name` = ?;";
private static final String DELETE_WORLD_QUERY = "DELETE FROM `worlds` WHERE `name` = ?;";
private static final String LIST_WORLDS_QUERY = "SELECT `name` FROM `worlds`;";
private static final String RENAME_WORLD_QUERY = "UPDATE `worlds` SET `name` = ? WHERE `name` = ?;";

private final Map<String, ScheduledFuture> lockedWorlds = new HashMap<>();
private final HikariDataSource source;
Expand Down Expand Up @@ -289,4 +290,25 @@ public void deleteWorld(String worldName) throws IOException, UnknownWorldExcept
throw new IOException(ex);
}
}

@Override
public boolean renameWorld(String oldWorldName, String newWorldName) throws IOException, UnknownWorldException {
if(this.lockedWorlds.containsKey(oldWorldName)) {
return false;
}

try(Connection con = this.source.getConnection(); PreparedStatement statement = con.prepareStatement(RENAME_WORLD_QUERY)) {

statement.setString(1, newWorldName);
statement.setString(2, oldWorldName);

if(statement.executeUpdate() == 0) {
throw new UnknownWorldException(oldWorldName);
}

return true;
}catch(SQLException ex) {
throw new IOException(ex);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,9 @@ public void deleteWorld(String name) throws UnknownWorldException, IOException {
}
connection.del(WORLD_DATA_PREFIX + name, WORLD_LOCK_PREFIX + name);
}

@Override
public boolean renameWorld(String oldWorldName, String newWorldName) {
return true;
}
}