Skip to content

Commit

Permalink
ConcurrentLinkedHashMap as a cache for stored regions.
Browse files Browse the repository at this point in the history
  • Loading branch information
Pavel Fedin committed Nov 24, 2016
1 parent f072508 commit 005e91d
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 10 deletions.
7 changes: 6 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,18 @@
<scope>test</scope>
</dependency>

<!-- https://mvnrepository.com/artifact/org.ini4j/ini4j -->
<dependency>
<groupId>org.ini4j</groupId>
<artifactId>ini4j</artifactId>
<version>0.5.1</version>
</dependency>

<dependency>
<groupId>com.googlecode.concurrentlinkedhashmap</groupId>
<artifactId>concurrentlinkedhashmap-lru</artifactId>
<version>1.4.2</version>
</dependency>


</dependencies>

Expand Down
2 changes: 2 additions & 0 deletions src/main/java/ru/wpstuio/amorphine/mcaliases/Region.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@
import com.mojang.nbt.CompoundTag;
import com.mojang.nbt.NbtIo;
import net.minecraft.world.level.chunk.storage.RegionFile;
import org.apache.commons.collections4.map.LRUMap;

import java.io.DataInputStream;

/**
* Created by amorphine on 21.11.16.
*/
public class Region {

private Chunk chunks[][] = new Chunk[32][32];

public Region(RegionFile region_file) {
Expand Down
32 changes: 23 additions & 9 deletions src/main/java/ru/wpstuio/amorphine/mcaliases/World.java
Original file line number Diff line number Diff line change
@@ -1,24 +1,28 @@
package ru.wpstuio.amorphine.mcaliases;

import com.googlecode.concurrentlinkedhashmap.ConcurrentLinkedHashMap;
import net.minecraft.world.level.chunk.storage.RegionFile;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.concurrent.ConcurrentMap;

/**
* Created by amorphine on 21.11.16.
*/
public class World {

private File world_dir;

private HashMap region_files = new HashMap<int[], File>();
private HashMap regions = new HashMap<int[], Region>();
private HashMap<int[], RegionFile> region_files = new HashMap<int[], RegionFile>();

private ConcurrentMap<int[], Region> regions = new ConcurrentLinkedHashMap.Builder<int[], Region>()
.maximumWeightedCapacity(2)
.build();

public World(File world_dir) throws IOException {

this.world_dir = world_dir;
if (!world_dir.exists()) {
throw new IOException("Cannot access " + world_dir.getAbsolutePath() + ": No such file or directory");
Expand All @@ -27,7 +31,9 @@ public World(File world_dir) throws IOException {
String[] file_list = world_dir.list();
for(String file_name: file_list) {
if (file_name.endsWith(".mca")) {

String[] splitted_file_name = file_name.split("\\.");

if (splitted_file_name.length < 4) {
continue;
}
Expand All @@ -39,13 +45,21 @@ public World(File world_dir) throws IOException {
Integer.parseInt(splitted_file_name[1]),
Integer.parseInt(splitted_file_name[2])
};
region_files.put(cords, region_file);

RegionFile rg_file = new RegionFile(region_file);
Region region = new Region(rg_file);
regions.put(cords, region);
region_files.put(cords, new RegionFile(region_file));
}
}
}
public Region getRegion(int x, int z) {
int[] coords = {x, z};

if(regions.containsKey(coords)) {
return regions.get(coords);
} else {
Region region = new Region(region_files.get(coords));
regions.put(coords, region);

return region;
}
}
}

0 comments on commit 005e91d

Please sign in to comment.