Properly serialize and deserialize the Dungeon rooms

This commit is contained in:
WayofTime 2016-08-21 13:17:04 -04:00
parent 9b64e2a2f6
commit 61b11a88bf
9 changed files with 176 additions and 38 deletions

View file

@ -0,0 +1,6 @@
package WayofTime.bloodmagic.gson;
public class Adapters
{
public static EnumFacingTypeAdapter adapter = new EnumFacingTypeAdapter();
}

View file

@ -0,0 +1,30 @@
package WayofTime.bloodmagic.gson;
import java.lang.reflect.Type;
import net.minecraft.util.EnumFacing;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
public class EnumFacingTypeAdapter implements JsonDeserializer<EnumFacing>, JsonSerializer<EnumFacing>
{
@Override
public EnumFacing deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException
{
String str = json.getAsString();
return EnumFacing.byName(str);
}
@Override
public JsonElement serialize(EnumFacing src, Type typeOfSrc, JsonSerializationContext context)
{
// Not necessary, since this is only used for deserialization.
return null;
}
}

View file

@ -25,6 +25,8 @@ public class Dungeon
{ {
public static boolean placeStructureAtPosition(Random rand, WorldServer world, BlockPos pos) public static boolean placeStructureAtPosition(Random rand, WorldServer world, BlockPos pos)
{ {
long startTime = System.nanoTime();
Map<EnumFacing, List<BlockPos>> availableDoorMap = new HashMap<EnumFacing, List<BlockPos>>(); //Map of doors. The EnumFacing indicates what way this door faces. Map<EnumFacing, List<BlockPos>> availableDoorMap = new HashMap<EnumFacing, List<BlockPos>>(); //Map of doors. The EnumFacing indicates what way this door faces.
List<AreaDescriptor> descriptorList = new ArrayList<AreaDescriptor>(); List<AreaDescriptor> descriptorList = new ArrayList<AreaDescriptor>();
Map<BlockPos, Pair<DungeonRoom, PlacementSettings>> roomMap = new HashMap<BlockPos, Pair<DungeonRoom, PlacementSettings>>(); // Placement positions in terms of actual positions Map<BlockPos, Pair<DungeonRoom, PlacementSettings>> roomMap = new HashMap<BlockPos, Pair<DungeonRoom, PlacementSettings>>(); // Placement positions in terms of actual positions
@ -158,6 +160,11 @@ public class Dungeon
} }
} }
long endTime = System.nanoTime();
long duration = (endTime - startTime); //divide by 1000000 to get milliseconds.
System.out.println("Duration: " + duration + "(ns), " + duration / 1000000 + "(ms)");
//Building what I've got //Building what I've got
for (Entry<BlockPos, Pair<DungeonRoom, PlacementSettings>> entry : roomMap.entrySet()) for (Entry<BlockPos, Pair<DungeonRoom, PlacementSettings>> entry : roomMap.entrySet())
{ {

View file

@ -8,6 +8,7 @@ import java.util.Map.Entry;
import java.util.Random; import java.util.Random;
import net.minecraft.util.EnumFacing; import net.minecraft.util.EnumFacing;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.BlockPos;
import net.minecraft.world.WorldServer; import net.minecraft.world.WorldServer;
import net.minecraft.world.gen.structure.template.PlacementSettings; import net.minecraft.world.gen.structure.template.PlacementSettings;
@ -16,12 +17,12 @@ import WayofTime.bloodmagic.api.ritual.AreaDescriptor;
public class DungeonRoom public class DungeonRoom
{ {
protected Map<DungeonStructure, BlockPos> structureMap = new HashMap<DungeonStructure, BlockPos>(); public Map<String, BlockPos> structureMap = new HashMap<String, BlockPos>();
Map<EnumFacing, List<BlockPos>> doorMap = new HashMap<EnumFacing, List<BlockPos>>(); //Map of doors. The EnumFacing indicates what way this door faces. public Map<EnumFacing, List<BlockPos>> doorMap = new HashMap<EnumFacing, List<BlockPos>>(); //Map of doors. The EnumFacing indicates what way this door faces.
List<AreaDescriptor> descriptorList = new ArrayList<AreaDescriptor>(); public List<AreaDescriptor.Rectangle> descriptorList = new ArrayList<AreaDescriptor.Rectangle>();
public DungeonRoom(Map<DungeonStructure, BlockPos> structureMap, Map<EnumFacing, List<BlockPos>> doorMap, List<AreaDescriptor> descriptorList) public DungeonRoom(Map<String, BlockPos> structureMap, Map<EnumFacing, List<BlockPos>> doorMap, List<AreaDescriptor.Rectangle> descriptorList)
{ {
this.structureMap = structureMap; this.structureMap = structureMap;
this.doorMap = doorMap; this.doorMap = doorMap;
@ -59,9 +60,10 @@ public class DungeonRoom
public boolean placeStructureAtPosition(Random rand, PlacementSettings settings, WorldServer world, BlockPos pos) public boolean placeStructureAtPosition(Random rand, PlacementSettings settings, WorldServer world, BlockPos pos)
{ {
for (Entry<DungeonStructure, BlockPos> entry : structureMap.entrySet()) for (Entry<String, BlockPos> entry : structureMap.entrySet())
{ {
DungeonStructure structure = entry.getKey(); ResourceLocation location = new ResourceLocation(entry.getKey());
DungeonStructure structure = new DungeonStructure(location);
BlockPos offsetPos = Template.transformedBlockPos(settings, entry.getValue()); BlockPos offsetPos = Template.transformedBlockPos(settings, entry.getValue());
structure.placeStructureAtPosition(rand, settings, world, pos.add(offsetPos)); structure.placeStructureAtPosition(rand, settings, world, pos.add(offsetPos));

View file

@ -0,0 +1,95 @@
package WayofTime.bloodmagic.structures;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.Writer;
import java.util.Random;
import net.minecraft.server.MinecraftServer;
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())
{
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()
{
String folder = "config/BloodMagic/schematics";
Gson gson = new GsonBuilder().setPrettyPrinting().registerTypeAdapter(EnumFacing.class, Adapters.adapter).create();
File file = new File(folder);
File[] files = file.listFiles();
BufferedReader br;
try
{
for (File f : files)
{
System.out.println("File: " + f);
br = new BufferedReader(new FileReader(f));
DungeonRoom room = gson.fromJson(br, DungeonRoom.class);
DungeonRoomRegistry.registerDungeonRoom(room, 1);
}
} catch (FileNotFoundException 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 = MinecraftServer.class.getResourceAsStream("/assets/" + s + "/schematics/" + s1 + ".nbt");
// this.readTemplateFromStream(s1, inputstream);
return;
} catch (Throwable var10)
{
} finally
{
IOUtils.closeQuietly(inputstream);
}
}
}

View file

@ -2,22 +2,30 @@ package WayofTime.bloodmagic.structures;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Map.Entry;
import java.util.Random; import java.util.Random;
public class DungeonRoomRegistry public class DungeonRoomRegistry
{ {
public static Map<DungeonRoom, Integer> dungeonWeightMap = new HashMap<DungeonRoom, Integer>(); public static Map<DungeonRoom, Integer> dungeonWeightMap = new HashMap<DungeonRoom, Integer>();
private static int totalWeight = 0;
public static void registerDungeonRoom(DungeonRoom room, int weight) public static void registerDungeonRoom(DungeonRoom room, int weight)
{ {
dungeonWeightMap.put(room, weight); dungeonWeightMap.put(room, weight);
totalWeight += weight;
} }
public static DungeonRoom getRandomDungeonRoom(Random rand) public static DungeonRoom getRandomDungeonRoom(Random rand)
{ {
for (DungeonRoom room : dungeonWeightMap.keySet()) int wantedWeight = rand.nextInt(totalWeight);
for (Entry<DungeonRoom, Integer> entry : dungeonWeightMap.entrySet())
{ {
return room; wantedWeight -= entry.getValue();
if (wantedWeight < 0)
{
return entry.getKey();
}
} }
return null; return null;

View file

@ -9,11 +9,10 @@ import net.minecraft.world.WorldServer;
import net.minecraft.world.gen.structure.template.PlacementSettings; import net.minecraft.world.gen.structure.template.PlacementSettings;
import net.minecraft.world.gen.structure.template.Template; import net.minecraft.world.gen.structure.template.Template;
import net.minecraft.world.gen.structure.template.TemplateManager; import net.minecraft.world.gen.structure.template.TemplateManager;
import WayofTime.bloodmagic.api.Constants;
public class DungeonStructure public class DungeonStructure
{ {
final ResourceLocation resource; public ResourceLocation resource;
public DungeonStructure(ResourceLocation resource) public DungeonStructure(ResourceLocation resource)
{ {
@ -28,7 +27,6 @@ public class DungeonStructure
MinecraftServer minecraftserver = world.getMinecraftServer(); MinecraftServer minecraftserver = world.getMinecraftServer();
TemplateManager templatemanager = world.getStructureTemplateManager(); TemplateManager templatemanager = world.getStructureTemplateManager();
ResourceLocation resource = new ResourceLocation(Constants.Mod.MODID, "Corridor1");
Template template = templatemanager.func_189942_b(minecraftserver, resource); Template template = templatemanager.func_189942_b(minecraftserver, resource);
if (template == null) if (template == null)

View file

@ -1,36 +1,28 @@
package WayofTime.bloodmagic.structures; package WayofTime.bloodmagic.structures;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.math.BlockPos;
import WayofTime.bloodmagic.api.Constants;
import WayofTime.bloodmagic.api.ritual.AreaDescriptor;
public class ModDungeons public class ModDungeons
{ {
public static void init() public static void init()
{ {
ResourceLocation resource = new ResourceLocation(Constants.Mod.MODID, "Corridor1"); // ResourceLocation resource = new ResourceLocation(Constants.Mod.MODID, "Corridor1");
//
// Map<String, BlockPos> structureMap = new HashMap<String, BlockPos>();
// structureMap.put(resource.toString(), new BlockPos(0, 0, 0));
//
// Map<EnumFacing, List<BlockPos>> doorMap = new HashMap<EnumFacing, List<BlockPos>>();
// List<AreaDescriptor.Rectangle> descriptorList = new ArrayList<AreaDescriptor.Rectangle>();
// descriptorList.add(new AreaDescriptor.Rectangle(new BlockPos(0, 0, 0), 5, 3, 7));
//
// DungeonUtil.addRoom(doorMap, EnumFacing.NORTH, new BlockPos(3, 0, 0));
// DungeonUtil.addRoom(doorMap, EnumFacing.SOUTH, new BlockPos(3, 0, 6));
// DungeonUtil.addRoom(doorMap, EnumFacing.WEST, new BlockPos(0, 0, 3));
//
// DungeonRoom room = new DungeonRoom(structureMap, doorMap, descriptorList);
//
// DungeonRoomRegistry.registerDungeonRoom(room, 1);
//
// DungeonRoomLoader.saveDungeons();
DungeonStructure structure = new DungeonStructure(resource); DungeonRoomLoader.loadDungeons();
Map<DungeonStructure, BlockPos> structureMap = new HashMap<DungeonStructure, BlockPos>();
structureMap.put(structure, new BlockPos(0, 0, 0));
Map<EnumFacing, List<BlockPos>> doorMap = new HashMap<EnumFacing, List<BlockPos>>();
List<AreaDescriptor> descriptorList = new ArrayList<AreaDescriptor>();
descriptorList.add(new AreaDescriptor.Rectangle(new BlockPos(0, 0, 0), 5, 3, 7));
DungeonUtil.addRoom(doorMap, EnumFacing.NORTH, new BlockPos(3, 0, 0));
DungeonUtil.addRoom(doorMap, EnumFacing.SOUTH, new BlockPos(3, 0, 6));
DungeonUtil.addRoom(doorMap, EnumFacing.WEST, new BlockPos(0, 0, 3));
DungeonRoom room = new DungeonRoom(structureMap, doorMap, descriptorList);
DungeonRoomRegistry.registerDungeonRoom(room, 1);
} }
} }