Skip to content

Commit 5b84baf

Browse files
committed
Removed dungeons from RAM
- Made Schematics load into RAM as byte arrays at the start of game instead of as NBT (saves +- 1GB of RAM) (SH) - Added checks for non-default schematics to make sure they contain valid NBT at game start (SH) - Made sure that invalid templates/schematics aren't even added to the array (SH) - Made sure that PocketTemplate loads Schematic from byte array before it tries to place it (PT) - Made sure that PocketTemplate unloads Schematic after placinf it (PT) - Made sure that TileEntityRift doesn't check its Location while PocketTemplate is replacing placeholders in Schematics (otherwise it will try to use null worlds and shizzle) (TER and PT) - Added functionality when saving a Schematic, this should put the Schematic in memory as a byte array (SH) - Fixed little bug, where Schematics that would be too big, would only be loaded when configured not to (SH) - Added functionality to keep the Schematics that are placed most loaded in RAM as NBT (combination of usageList and usageMap) (SH and PT) - Added config option to set the maximum number of Schematics that will be cached as NBT (MC and SH) - Made sure that Dimdoors.log is consistently used (SH) - Added check for .json file extension for files in the json config folder (SH) - Schematic files in the jar have had the .schem extension for a while now. Generalised a bit. (SH)
1 parent d905340 commit 5b84baf

File tree

6 files changed

+230
-34
lines changed

6 files changed

+230
-34
lines changed

src/main/java/org/dimdev/dimdoors/shared/ModConfig.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,10 @@ public static class Pockets {
8080
@Name("loadAllSchematics")
8181
@LangKey("dimdoors.pockets.loadAllSchematics")
8282
public boolean loadAllSchematics = false;
83+
84+
@Name("cachedSchematics")
85+
@LangKey("dimdoors.pockets.cachedSchematics") //TODO add lang key to lang file
86+
public int cachedSchematics = 10;
8387
}
8488

8589
public static class World {

src/main/java/org/dimdev/dimdoors/shared/pockets/PocketGenerator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public static Pocket generatePocketFromTemplate(int dim, PocketTemplate pocketTe
1818

1919
PocketRegistry registry = PocketRegistry.instance(dim);
2020
Pocket pocket = registry.newPocket();
21-
pocketTemplate.place(pocket);
21+
pocketTemplate.place(pocket, setup);
2222
pocket.setVirtualLocation(virtualLocation);
2323
if (setup) pocketTemplate.setup(pocket, null, null);
2424
return pocket;

src/main/java/org/dimdev/dimdoors/shared/pockets/PocketTemplate.java

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,10 @@ public class PocketTemplate {
4545
@Getter private final String name;
4646
@Getter private final String author;
4747
@Getter @Setter private Schematic schematic;
48+
@Setter private byte[] schematicBytecode;
4849
@Getter private final int size; // number of chunks (16 blocks) on each side - 1
4950
@Getter private final int baseWeight;
51+
@Getter private static boolean isReplacingPlaceholders = false;
5052

5153
public float getWeight(int depth) {
5254
//noinspection IfStatementWithIdenticalBranches
@@ -59,6 +61,7 @@ public float getWeight(int depth) {
5961

6062
public static void replacePlaceholders(Schematic schematic) { // TODO: rift inheritance rather than placeholders
6163
// Replace placeholders (some schematics will contain them)
64+
isReplacingPlaceholders = true;
6265
List<NBTTagCompound> tileEntities = new ArrayList<>();
6366
for (NBTTagCompound tileEntityNBT : schematic.tileEntities) {
6467
if (tileEntityNBT.hasKey("placeholder")) {
@@ -149,20 +152,34 @@ public static void replacePlaceholders(Schematic schematic) { // TODO: rift inhe
149152
}
150153
}
151154
schematic.entities = entities;
155+
isReplacingPlaceholders = false;
152156
}
153157

154-
public void place(Pocket pocket) {
158+
public void place(Pocket pocket, boolean setup) {
155159
pocket.setSize(size);
156160
int gridSize = PocketRegistry.instance(pocket.getDim()).getGridSize();
157161
int dim = pocket.getDim();
158162
WorldServer world = WorldUtils.getWorld(dim);
159163
int xBase = pocket.getX() * gridSize * 16;
160164
int yBase = 0;
161165
int zBase = pocket.getZ() * gridSize * 16;
166+
167+
//Converting the schematic from bytearray if needed
168+
if (schematic == null) {
169+
DimDoors.log.debug("Schematic is null, trying to reload from byteArray.");
170+
schematic = SchematicHandler.INSTANCE.loadSchematicFromByteArray(schematicBytecode);
171+
replacePlaceholders(schematic);
172+
}
162173

163-
// Place the schematic
174+
//Place the schematic
164175
DimDoors.log.info("Placing new pocket using schematic " + id + " at x = " + xBase + ", z = " + zBase);
165176
schematic.place(world, xBase, yBase, zBase);
177+
178+
SchematicHandler.INSTANCE.incrementUsage(this);
179+
if (!setup && !SchematicHandler.INSTANCE.isUsedOftenEnough(this)) {
180+
//remove schematic from "cache"
181+
schematic = null;
182+
}
166183
}
167184

168185
public void setup(Pocket pocket, VirtualTarget linkTo, LinkProperties linkProperties) {
@@ -258,5 +275,10 @@ public void setup(Pocket pocket, VirtualTarget linkTo, LinkProperties linkProper
258275
rift.register();
259276
rift.markDirty();
260277
}
278+
279+
if (!SchematicHandler.INSTANCE.isUsedOftenEnough(this)) {
280+
//remove schematic from "cache"
281+
schematic = null;
282+
}
261283
}
262284
}

0 commit comments

Comments
 (0)