Skip to content

Commit

Permalink
Fiasko with using arrays as Map keys (damn it they are wrong choice f…
Browse files Browse the repository at this point in the history
…or that)

Commented tests and other problem places.
  • Loading branch information
Pavel Fedin committed Nov 24, 2016
1 parent 005e91d commit e87a50e
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 8 deletions.
9 changes: 9 additions & 0 deletions src/main/java/ru/wpstuio/amorphine/mcaliases/Chunk.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public Chunk(CompoundTag tag) {
this.tag = tag;

CompoundTag c_tag_level = (CompoundTag) tag.get("Level");

IntTag x = (IntTag) c_tag_level.get("xPos");
IntTag z = (IntTag) c_tag_level.get("zPos");

Expand All @@ -43,6 +44,14 @@ public Chunk(CompoundTag tag) {
}
}

public int getX() {
return x;
}

public int getZ() {
return z;
}

public CompoundTag getTag() {
return tag;
}
Expand Down
18 changes: 18 additions & 0 deletions src/main/java/ru/wpstuio/amorphine/mcaliases/Region.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,21 @@
import org.apache.commons.collections4.map.LRUMap;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;

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

private RegionFile region_file;

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

public Region(RegionFile region_file) {
this.region_file = region_file;

for(int i = 31; i >= 0; i--) {
for(int j = 31; j >= 0; j--) {
CompoundTag tag;
Expand All @@ -40,4 +46,16 @@ public Region(RegionFile region_file) {
public Chunk[][] getChunks() {
return chunks;
}

public Chunk getChunk(int x, int z) {
return chunks[x][z];
}

public void saveChunk(Chunk chunk) throws IOException{
DataOutputStream stream = region_file.getChunkDataOutputStream(chunk.getX(), chunk.getZ());

NbtIo.write(chunks[chunk.getX()][chunk.getZ()].getTag(), stream);

stream.close();
}
}
17 changes: 10 additions & 7 deletions src/main/java/ru/wpstuio/amorphine/mcaliases/World.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class World {

private File world_dir;

private HashMap<int[], RegionFile> region_files = new HashMap<int[], RegionFile>();
private RegionFile[][] region_files = new RegionFile[32][32];

private ConcurrentMap<int[], Region> regions = new ConcurrentLinkedHashMap.Builder<int[], Region>()
.maximumWeightedCapacity(2)
Expand All @@ -41,25 +41,28 @@ public World(File world_dir) throws IOException {
String path_to_region = world_dir.getAbsolutePath() + "/" + file_name;
File region_file = new File(path_to_region);

int[] cords = {
Integer.parseInt(splitted_file_name[1]),
Integer.parseInt(splitted_file_name[2])
};
int x = Integer.parseInt(splitted_file_name[1]);
int z =Integer.parseInt(splitted_file_name[2]);

region_files.put(cords, new RegionFile(region_file));
region_files[x][z] = 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));
RegionFile region_file = region_files.get(coords);
Region region = new Region(region_file);
regions.put(coords, region);
return region;
}
}
*/
}
14 changes: 13 additions & 1 deletion src/test/java/TestWorld.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,27 @@
public class TestWorld {
@Test
public void testAdd() {

/*
World world;
try {
world = new World(new File ("/home/amorphine/mine-thermos/world/region"));
int[] coords = {-1, 0};
Region rg = world.getRegion(coords);
byte id_to_make = 16;
Chunk chunk = rg.getChunk(31, 0);
chunk.changeBlockId(-3, 64,6, id_to_make);
} catch (IOException e) {
e.printStackTrace();
}
*/

/*
String path_to_local_ini = "local_presets.ini";
String path_to_ini = "presets.ini";
Expand Down

0 comments on commit e87a50e

Please sign in to comment.