Skip to content

Commit

Permalink
paper: Migrate data using world name to world keys for consistency wi…
Browse files Browse the repository at this point in the history
…th other platforms (#30)
  • Loading branch information
jpenilla authored Feb 12, 2022
1 parent 3450115 commit b544a3e
Show file tree
Hide file tree
Showing 13 changed files with 100 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,6 @@ public interface SquaremapPlatform {

String version();

default String configNameForWorld(final ServerLevel level) {
return level.dimension().location().toString();
}

default String webNameForWorld(final ServerLevel level) {
return level.dimension().location().toString().replace(":", "_");
}

default Path regionFileDirectory(final ServerLevel level) {
final Path worldPath = level.getServer().getWorldPath(LevelResource.ROOT);
return DimensionType.getStorageFolder(level.dimension(), worldPath).resolve("region");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.lang.reflect.Type;
import java.nio.file.Path;
import java.util.List;
import net.minecraft.server.level.ServerLevel;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.spongepowered.configurate.CommentedConfigurationNode;
Expand All @@ -21,6 +22,7 @@
import org.spongepowered.configurate.yaml.YamlConfigurationLoader;
import xyz.jpenilla.squaremap.common.Logging;
import xyz.jpenilla.squaremap.common.SquaremapCommon;
import xyz.jpenilla.squaremap.common.util.Util;

import static java.util.Objects.requireNonNull;
import static xyz.jpenilla.squaremap.common.util.Util.rethrow;
Expand Down Expand Up @@ -192,6 +194,22 @@ private <V> List<V> getList0(TypeToken<V> elementType, final String path, List<V
return ret == null ? storeDefault(node, type, def) : ret;
}

public final void migrateLevelSection(final ServerLevel level, final String oldName) {
final ConfigurationNode oldNode = this.config.node("world-settings", oldName);
if (oldNode.virtual()) {
return;
}
final String configName = Util.levelConfigName(level);
final ConfigurationNode newNode = this.config.node("world-settings", configName);
try {
newNode.set(oldNode);
oldNode.set(null);
} catch (final SerializationException e) {
rethrow(e);
}
this.save();
}

private static <V> V storeDefault(final ConfigurationNode node, final Type type, final V defValue) throws SerializationException {
requireNonNull(defValue, "defValue");
if (node.options().shouldCopyDefaults()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import xyz.jpenilla.squaremap.common.SquaremapCommon;
import xyz.jpenilla.squaremap.common.WorldManager;
import xyz.jpenilla.squaremap.common.data.MapWorldInternal;
import xyz.jpenilla.squaremap.common.util.Util;

@SuppressWarnings("unused")
public abstract class AbstractWorldConfig {
Expand All @@ -26,7 +27,7 @@ protected AbstractWorldConfig(
) {
this.configClass = worldConfigClass;
this.world = level;
this.worldName = SquaremapCommon.instance().platform().configNameForWorld(level)
this.worldName = Util.levelConfigName(level)
.replace(".", DOT); // replace '.' as we later split on it (see AbstractConfig.splitPath)
this.parent = parent;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,4 +114,8 @@ private static void progressLogging() {
PROGRESS_LOGGING_INTERVAL = config.getInt("settings.render-progress-logging.interval-seconds", 1);
}

public static Config config() {
return config;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,13 @@ protected MapWorldInternal(final ServerLevel level) {
this.levelBiomeColorData = LevelBiomeColorData.create(this);

this.dataPath = SquaremapCommon.instance().platform().dataDirectory().resolve("data").resolve(
SquaremapCommon.instance().platform().webNameForWorld(this.level)
Util.levelWebName(this.level)
);
try {
if (!Files.exists(this.dataPath)) {
Files.createDirectories(this.dataPath);
}
} catch (IOException e) {
} catch (final IOException e) {
throw this.failedToCreateDataDirectory(e);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public static void sendServerData(final ServerPlayer player) {

mapWorlds.forEach(($, mapWorld) -> {
out.writeUTF(mapWorld.identifier().asString());
out.writeUTF(SquaremapCommon.instance().platform().webNameForWorld(mapWorld.serverLevel()));
out.writeUTF(Util.levelWebName(mapWorld.serverLevel()));
out.writeInt(mapWorld.config().ZOOM_MAX);
out.writeInt(mapWorld.config().ZOOM_DEFAULT);
out.writeInt(mapWorld.config().ZOOM_EXTRA);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import xyz.jpenilla.squaremap.common.config.WorldConfig;
import xyz.jpenilla.squaremap.common.util.FileUtil;
import xyz.jpenilla.squaremap.common.util.HtmlComponentSerializer;
import xyz.jpenilla.squaremap.common.util.Util;

public class UpdatePlayers implements Runnable {
private static final Gson GSON = new Gson();
Expand Down Expand Up @@ -50,7 +51,7 @@ public void run() {
playerEntry.put("display_name", htmlComponentSerializer.serialize(this.platform.playerManager().displayName(player)));
}
playerEntry.put("uuid", player.getUUID().toString().replace("-", ""));
playerEntry.put("world", this.platform.webNameForWorld(world));
playerEntry.put("world", Util.levelWebName(world));
if (worldConfig.PLAYER_TRACKER_ENABLED) {
playerEntry.put("x", Mth.floor(playerLoc.x()));
playerEntry.put("z", Mth.floor(playerLoc.z()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import xyz.jpenilla.squaremap.common.config.WorldConfig;
import xyz.jpenilla.squaremap.common.data.MapWorldInternal;
import xyz.jpenilla.squaremap.common.util.FileUtil;
import xyz.jpenilla.squaremap.common.util.Util;

@DefaultQualifier(NonNull.class)
public final class UpdateWorldData implements Runnable {
Expand Down Expand Up @@ -79,9 +80,9 @@ public void run() {

Map<String, Object> worldsList = new HashMap<>();
//worldsList.put("name", world.getName());
worldsList.put("name", this.platform.webNameForWorld(world));
worldsList.put("name", Util.levelWebName(world));
worldsList.put("display_name", worldConfig.MAP_DISPLAY_NAME
.replace("{world}", this.platform.configNameForWorld(world)));
.replace("{world}", Util.levelConfigName(world)));
//.replace("{world}", world.getName()));
worldsList.put("icon", worldConfig.MAP_ICON);
//worldsList.put("type", world.getEnvironment().name().toLowerCase());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,8 @@ public static void reload() {
TILES_DIR = WEB_DIR.resolve("tiles");
}

private static Path getRegionFolder(final ServerLevel level) {
return SquaremapCommon.instance().platform().regionFileDirectory(level);
}

public static Path[] getRegionFiles(final ServerLevel level) {
final Path regionFolder = getRegionFolder(level);
final Path regionFolder = SquaremapCommon.instance().platform().regionFileDirectory(level);
Logging.debug(() -> "Listing region files for directory '" + regionFolder + "'...");
try (final Stream<Path> stream = Files.list(regionFolder)) {
return stream.filter(file -> file.getFileName().toString().endsWith(".mca")).toArray(Path[]::new);
Expand All @@ -60,11 +56,11 @@ public static Path[] getRegionFiles(final ServerLevel level) {
}

public static Path getAndCreateTilesDirectory(final ServerLevel level) {
final Path dir = TILES_DIR.resolve(SquaremapCommon.instance().platform().webNameForWorld(level));
final Path dir = TILES_DIR.resolve(Util.levelWebName(level));
if (!Files.exists(dir)) {
try {
Files.createDirectories(dir);
} catch (IOException e) {
} catch (final IOException e) {
Logging.error(Lang.LOG_COULD_NOT_CREATE_DIR, e, "path", dir.toAbsolutePath());
}
}
Expand All @@ -84,7 +80,7 @@ public static void deleteSubdirectories(Path dir) throws IOException {
}

public static void deleteDirectory(Path dir) throws IOException {
try (Stream<Path> walk = Files.walk(dir)) {
try (final Stream<Path> walk = Files.walk(dir)) {
//noinspection ResultOfMethodCallIgnored
walk.sorted(Comparator.reverseOrder())
.map(Path::toFile)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,14 @@ public static <T> T requireEntry(final Registry<T> registry, final ResourceLocat
return requireNonNull(registry.get(location));
}

public static String levelConfigName(final ServerLevel level) {
return level.dimension().location().toString();
}

public static String levelWebName(final ServerLevel level) {
return level.dimension().location().toString().replace(":", "_");
}

public static WorldIdentifier worldIdentifier(final ServerLevel level) {
final ResourceLocation location = level.dimension().location();
return worldIdentifier(location);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import xyz.jpenilla.squaremap.paper.util.BukkitRunnableAdapter;
import xyz.jpenilla.squaremap.paper.util.CraftBukkitReflection;
import xyz.jpenilla.squaremap.paper.util.PaperChunkSnapshotProvider;
import xyz.jpenilla.squaremap.paper.util.WorldNameToKeyMigration;

public final class SquaremapPlugin extends JavaPlugin implements SquaremapPlatform {
private static final Logger LOGGER = LogManager.getLogger("squaremap");
Expand Down Expand Up @@ -97,6 +98,7 @@ public void startCallback() {
this.updateWorldData = new BukkitRunnableAdapter(new UpdateWorldData(this));
this.updateWorldData.runTaskTimer(this, 0, 20 * 5);

WorldNameToKeyMigration.migrateLoaded();
this.worldManager = new PaperWorldManager();
this.worldManager.start(this);

Expand Down Expand Up @@ -190,16 +192,6 @@ public int maxPlayers() {
return PaperComponents.flattener();
}

@Override
public @NonNull String configNameForWorld(final @NonNull ServerLevel level) {
return CraftBukkitReflection.world(level).getName();
}

@Override
public @NonNull String webNameForWorld(final @NonNull ServerLevel level) {
return CraftBukkitReflection.world(level).getName();
}

@Override
public @NonNull Collection<ServerLevel> levels() {
final List<ServerLevel> levels = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,15 @@
import xyz.jpenilla.squaremap.common.config.WorldConfig;
import xyz.jpenilla.squaremap.paper.SquaremapPlugin;
import xyz.jpenilla.squaremap.paper.util.CraftBukkitReflection;
import xyz.jpenilla.squaremap.paper.util.WorldNameToKeyMigration;

@DefaultQualifier(NonNull.class)
public record WorldLoadListener(SquaremapPlugin plugin) implements Listener {
// Use low priority to load world before other plugins load listeners
@EventHandler(priority = EventPriority.LOW)
public void handleWorldLoad(final WorldLoadEvent event) {
WorldNameToKeyMigration.migrate(CraftBukkitReflection.serverLevel(event.getWorld()));

WorldConfig.get(CraftBukkitReflection.serverLevel(event.getWorld()));
this.plugin.worldManager().getWorldIfEnabled(event.getWorld());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package xyz.jpenilla.squaremap.paper.util;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import net.minecraft.server.level.ServerLevel;
import org.bukkit.Bukkit;
import org.bukkit.World;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.framework.qual.DefaultQualifier;
import xyz.jpenilla.squaremap.common.SquaremapCommon;
import xyz.jpenilla.squaremap.common.config.Advanced;
import xyz.jpenilla.squaremap.common.config.Config;
import xyz.jpenilla.squaremap.common.util.FileUtil;
import xyz.jpenilla.squaremap.common.util.Util;

@DefaultQualifier(NonNull.class)
public final class WorldNameToKeyMigration {
public static void migrateLoaded() {
for (final World world : Bukkit.getWorlds()) {
migrate(CraftBukkitReflection.serverLevel(world));
}
}

public static void migrate(final ServerLevel level) {
final String oldName = CraftBukkitReflection.world(level).getName();
Config.config().migrateLevelSection(level, oldName);
Advanced.config().migrateLevelSection(level, oldName);
try {
moveDirectories(level, oldName);
} catch (final IOException ex) {
throw new RuntimeException("Failed to migrate directories", ex);
}
}

private static void moveDirectories(final ServerLevel level, final String oldName) throws IOException {
final String webName = Util.levelWebName(level);
final Path tilesFrom = FileUtil.TILES_DIR.resolve(oldName);
if (Files.exists(tilesFrom)) {
final Path tilesDest = FileUtil.TILES_DIR.resolve(webName);
Files.move(tilesFrom, tilesDest);
}

final Path data = SquaremapCommon.instance().platform().dataDirectory().resolve("data");
final Path dataFrom = data.resolve(oldName);
if (Files.exists(dataFrom)) {
final Path dataDest = data.resolve(webName);
Files.move(dataFrom, dataDest);
}
}
}

0 comments on commit b544a3e

Please sign in to comment.