Skip to content

Commit

Permalink
Convert existing lebmods
Browse files Browse the repository at this point in the history
  • Loading branch information
kyrptonaught committed Apr 8, 2024
1 parent e1be816 commit 28c7ba1
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 3 deletions.
10 changes: 10 additions & 0 deletions src/main/java/net/kyrptonaught/serverutils/FileHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,16 @@ public static boolean writeFile(Path filePath, String text) {
return false;
}

public static String readFile(Path filePath) {
try {
return Files.readString(filePath);
} catch (Exception e) {
System.out.println("Error reading file: " + filePath);
e.printStackTrace();
}
return null;
}

public static String fixPathSeparator(String name) {
return name.replaceAll("\\\\", "/");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public static void discoverAddons(MinecraftServer server) {
if (path.getParent().getFileName().toString().equals("base")) {
config.isBaseAddon = true;
config.addon_pack = "base_" + config.addon_pack;
} else if (!path.getParent().getFileName().toString().equals("lebmods")) {
} else if (!path.getParent().getFileName().toString().equals("lemaddons")) {
config.addon_pack = path.getParent().getFileName().toString();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ public class BaseAddon {

public Identifier addon_id;

private String name;
public String name;
public String name_key;

private String description;
public String description;
public String description_key;

public String authors;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package net.kyrptonaught.serverutils.customMapLoader.converter;

import com.google.common.collect.Sets;
import com.google.gson.JsonObject;
import net.kyrptonaught.serverutils.FileHelper;
import net.kyrptonaught.serverutils.ServerUtilsMod;
import net.kyrptonaught.serverutils.customMapLoader.MapSize;
Expand All @@ -26,6 +27,7 @@ public class Converter {
public static void ConvertAll() {
BattleConvert();
LobbyConvert();
LEBModsConvert();
}

public static void BattleConvert() {
Expand Down Expand Up @@ -164,6 +166,71 @@ public static void LobbyConvert() {
}
}

public static void LEBModsConvert() {
String input = "C:\\Users\\antho\\Desktop\\Minecraft Mod Dev\\LEMAddonConverter\\input\\lebmods";
String output = "C:\\Users\\antho\\Desktop\\Minecraft Mod Dev\\LEMAddonConverter\\output";

try (Stream<Path> files = Files.walk(Path.of(input), 2)) {
files.forEach(path -> {
try {
if (!Files.isDirectory(path) && path.getFileName().toString().endsWith("config.json")) {
String configJson = FileHelper.readFile(path);
JsonObject rawConfig = ServerUtilsMod.getGson().fromJson(configJson, JsonObject.class);

String mapname = rawConfig.get("name").getAsString();
Path tempOut = Path.of(output).resolve(mapname);

FileHelper.createDir(tempOut);

BattleMapAddon addon = new BattleMapAddon();
addon.addon_id = new Identifier("lemcommunity", rawConfig.get("id").getAsString());
addon.addon_type = BattleMapAddon.TYPE;
addon.addon_pack = "Other";
addon.name = mapname;
addon.description = rawConfig.get("description").getAsString();
addon.authors = rawConfig.get("authors").getAsString();
addon.version = rawConfig.get("version").getAsString();

addon.required_packs = new ResourcePackList();
addon.required_packs.packs.add(new DummyPack("lem.base:" + rawConfig.get("pack").getAsString()));

for (MapSize mapSize : MapSize.values()) {
Path dir = path.getParent().resolve("world").resolve(mapSize.fileName);

if (!Files.exists(dir)) continue;

FileHelper.copyDirectory(dir, tempOut.resolve("world").resolve(mapSize.fileName));

HashMap<String, List<String>> entities = getEntitesForMap(dir);

BattleMapAddon.MapSizeConfig config = new BattleMapAddon.MapSizeConfig();

config.center_coords = entities.get("MapCenter").get(0);
config.world_border_coords_1 = entities.get("BorderEntity").get(0);
config.world_border_coords_2 = entities.get("BorderEntity").get(1);
config.center_spawn_coords = entities.get("CenterTP").toArray(String[]::new);
config.random_spawn_coords = entities.get("RandomTP").toArray(String[]::new);
config.chest_tracker_coords = entities.get("Chest").toArray(String[]::new);

addon.setMapDataForSize(mapSize, config);
}
FileHelper.copyFile(path.getParent().resolve("world").resolve("dimension_type.json"), tempOut.resolve("dimension_type.json"));

String json = ServerUtilsMod.getGson().toJson(addon);
FileHelper.writeFile(tempOut.resolve("addon.json"), json);

FileHelper.zipDirectory(tempOut, Path.of(output).resolve(addon.name + ".lemaddon"));
FileHelper.deleteDir(tempOut);
}
} catch (Exception e) {
e.printStackTrace();
}
});
} catch (Exception e) {
e.printStackTrace();
}
}

private static HashMap<String, List<String>> getEntitesForMap(Path dir) {
HashMap<String, List<String>> entities = new HashMap<>();

Expand Down

0 comments on commit 28c7ba1

Please sign in to comment.