
Now requires new rooms to be registered in `resources/assets/bloodmagic/schematics/Schematics.json`. This *can* be overwritten by a resource pack, so adding new rooms is still possible. Also allows sub-folders, so sorting rooms is now possible (sort by themes maybe?)
102 lines
3.2 KiB
Java
102 lines
3.2 KiB
Java
package WayofTime.bloodmagic.structures;
|
|
|
|
import java.io.File;
|
|
import java.io.FileWriter;
|
|
import java.io.IOException;
|
|
import java.io.InputStream;
|
|
import java.io.Writer;
|
|
import java.net.URL;
|
|
import java.util.List;
|
|
import java.util.Random;
|
|
|
|
import com.google.common.base.Charsets;
|
|
import com.google.common.io.Resources;
|
|
import com.google.common.reflect.TypeToken;
|
|
import net.minecraft.util.EnumFacing;
|
|
import net.minecraft.util.ResourceLocation;
|
|
|
|
import org.apache.commons.io.IOUtils;
|
|
|
|
import WayofTime.bloodmagic.api.Constants;
|
|
import WayofTime.bloodmagic.gson.Adapters;
|
|
|
|
import com.google.gson.Gson;
|
|
import com.google.gson.GsonBuilder;
|
|
|
|
public class DungeonRoomLoader
|
|
{
|
|
public static void saveDungeons()
|
|
{
|
|
for (DungeonRoom room : DungeonRoomRegistry.dungeonWeightMap.keySet())
|
|
{
|
|
saveSingleDungeon(room);
|
|
}
|
|
}
|
|
|
|
public static void saveSingleDungeon(DungeonRoom room)
|
|
{
|
|
Gson gson = new GsonBuilder().setPrettyPrinting().create();
|
|
String json = gson.toJson(room);
|
|
|
|
Writer writer;
|
|
try
|
|
{
|
|
File file = new File("config/BloodMagic/schematics");
|
|
file.mkdirs();
|
|
|
|
writer = new FileWriter("config/BloodMagic/schematics/" + new Random().nextInt() + ".json");
|
|
writer.write(json);
|
|
writer.close();
|
|
} catch (IOException e)
|
|
{
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
|
|
public static void loadDungeons()
|
|
{
|
|
Gson gson = new GsonBuilder().setPrettyPrinting().registerTypeAdapter(EnumFacing.class, Adapters.adapter).create();
|
|
|
|
try
|
|
{
|
|
URL schematicURL = DungeonRoomLoader.class.getResource(resLocToResourcePath(new ResourceLocation("bloodmagic:Schematics")));
|
|
List<String> schematics = gson.fromJson(Resources.toString(schematicURL, Charsets.UTF_8), new TypeToken<List<String>>(){}.getType());
|
|
for (String schematicKey : schematics)
|
|
{
|
|
ResourceLocation schematic = new ResourceLocation(schematicKey);
|
|
URL dungeonURL = DungeonRoomLoader.class.getResource(resLocToResourcePath(schematic));
|
|
DungeonRoom dungeonRoom = gson.fromJson(Resources.toString(dungeonURL, Charsets.UTF_8), DungeonRoom.class);
|
|
DungeonRoomRegistry.registerDungeonRoom(dungeonRoom, Math.max(1, dungeonRoom.dungeonWeight));
|
|
}
|
|
} catch (Exception e)
|
|
{
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
|
|
public static void test()
|
|
{
|
|
ResourceLocation id = new ResourceLocation(Constants.Mod.MODID, "testGson");
|
|
String s = id.getResourceDomain();
|
|
String s1 = id.getResourcePath();
|
|
InputStream inputstream = null;
|
|
|
|
try
|
|
{
|
|
inputstream = DungeonRoomLoader.class.getResourceAsStream("/assets/" + s + "/schematics/" + s1 + ".nbt");
|
|
// this.readTemplateFromStream(s1, inputstream);
|
|
return;
|
|
} catch (Throwable var10)
|
|
{
|
|
|
|
} finally
|
|
{
|
|
IOUtils.closeQuietly(inputstream);
|
|
}
|
|
}
|
|
|
|
public static String resLocToResourcePath(ResourceLocation resourceLocation)
|
|
{
|
|
return "/assets/" + resourceLocation.getResourceDomain() + "/schematics/" + resourceLocation.getResourcePath() + ".json";
|
|
}
|
|
}
|