Skip to content

Commit

Permalink
Impelemting coordinates, some comments for code;
Browse files Browse the repository at this point in the history
  • Loading branch information
Pavel Fedin committed Nov 25, 2016
1 parent f932036 commit c01c9c5
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 17 deletions.
47 changes: 38 additions & 9 deletions src/main/java/ru/wpstuio/amorphine/mcaliases/Chunk.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.mojang.nbt.CompoundTag;
import com.mojang.nbt.IntTag;
import com.mojang.nbt.ListTag;
import ru.wpstuio.amorphine.utils.Coordinates2d;

import static ru.wpstuio.amorphine.utils.Formulae.localBlockFromBlock;
import static ru.wpstuio.amorphine.utils.Formulae.sectionFromBlock;
Expand All @@ -16,21 +17,27 @@ public class Chunk {

private CompoundTag tag;

private int x;
private int z;
private final int global_x;
private final int global_z;

private final int local_x;
private final int local_z;

private CompoundTag[] sections = new CompoundTag[15];

public Chunk(CompoundTag tag) {
public Chunk(Coordinates2d local_cords, 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");

this.x = x.data;
this.z = z.data;
this.global_x = x.data;
this.global_z = z.data;

this.local_x = local_cords.getX();
this.local_z = local_cords.getZ();

ListTag sections_tag = (ListTag) c_tag_level.get("Sections");
for(int i = 0; i <= 15; i++){
Expand All @@ -44,18 +51,33 @@ public Chunk(CompoundTag tag) {
}
}

public int getX() {
return x;
public int getGlobalX() {
return global_x;
}

public int getGlobalZ() {
return global_z;
}

public int getLocalX() {
return local_x;
}

public int getZ() {
return z;
public int getLocalZ() {
return local_z;
}

public CompoundTag getTag() {
return tag;
}

/**
* Changes specified block basic id to with the given one
* @param x represents EAST - SOUTH axe
* @param y represents height axe
* @param z represents NORTH - SOUTH axe
* @param id which the block should be
*/
public void changeBlockId(int x, int y, int z, byte id) {

int local_x = localBlockFromBlock(x);
Expand All @@ -71,6 +93,13 @@ public void changeBlockId(int x, int y, int z, byte id) {
block_bytes_tag.data[offset] = id;
}

/**
* Returns id of the block with specified XYZ coordinates
* @param x represents EAST - SOUTH axe
* @param y represents height axe
* @param z represents NORTH - SOUTH axe
* @return byte of the basic id of the block
*/
public byte getBlockId(int x, int y, int z) {
int local_x = localBlockFromBlock(x);
int local_y = localBlockFromBlock(y);
Expand Down
39 changes: 34 additions & 5 deletions src/main/java/ru/wpstuio/amorphine/mcaliases/Region.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import com.mojang.nbt.NbtIo;
import net.minecraft.world.level.chunk.storage.RegionFile;
import org.apache.commons.collections4.map.LRUMap;
import ru.wpstuio.amorphine.utils.Coordinates2d;
import ru.wpstuio.amorphine.utils.Formulae;

import java.io.DataInputStream;
import java.io.DataOutputStream;
Expand Down Expand Up @@ -34,9 +36,11 @@ public Region(RegionFile region_file) {
tag = null;
}

Coordinates2d local_cords = new Coordinates2d(i, j);

Chunk chunk = null;
if (tag != null)
chunk = new Chunk(tag);
chunk = new Chunk(local_cords, tag);

this.chunks[i][j] = chunk;
}
Expand All @@ -47,14 +51,39 @@ public Chunk[][] getChunks() {
return chunks;
}

public Chunk getChunk(int x, int z) {
return chunks[x][z];
public Chunk getChunk(Coordinates2d cords) {
return chunks[cords.getX()][cords.getZ()];
}

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);
int local_x = chunk.getLocalX();
int local_z = chunk.getLocalZ();

DataOutputStream stream = region_file.getChunkDataOutputStream(local_x, local_z);

NbtIo.write(chunks[local_x][local_z].getTag(), stream);

stream.close();
}

/**
*
* @param cords
* @param tag
* @throws IOException
*/
public void saveTagAsChunk(Coordinates2d cords, CompoundTag tag) throws IOException{

int global_x = cords.getX();
int global_z = cords.getZ();

int local_x = Formulae.localChunkFromChunk(global_x);
int local_z = Formulae.localChunkFromChunk(global_z);

DataOutputStream stream = region_file.getChunkDataOutputStream(local_x, local_z);

NbtIo.write(chunks[local_x][local_z].getTag(), stream);

stream.close();
}
Expand Down
14 changes: 11 additions & 3 deletions src/main/java/ru/wpstuio/amorphine/mcaliases/World.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.googlecode.concurrentlinkedhashmap.ConcurrentLinkedHashMap;
import net.minecraft.world.level.chunk.storage.RegionFile;
import ru.wpstuio.amorphine.utils.Coordinates2d;

import java.io.File;
import java.io.IOException;
Expand All @@ -17,7 +18,7 @@ public class World {

private RegionFile[][] region_files = new RegionFile[32][32];

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

Expand All @@ -39,12 +40,19 @@ public World(File world_dir) throws IOException {
}

String path_to_region = world_dir.getAbsolutePath() + "/" + file_name;
File region_file = new File(path_to_region);
File mca_file = new File(path_to_region);
RegionFile region_file = new RegionFile(mca_file);


int x = Integer.parseInt(splitted_file_name[1]);
int z =Integer.parseInt(splitted_file_name[2]);
Coordinates2d cords = new Coordinates2d(x, z);
region_files[x][z] = region_file;

Region region = new Region(region_file);

regions.putIfAbsent(cords, region);

region_files[x][z] = new RegionFile(region_file);
}
}
}
Expand Down

0 comments on commit c01c9c5

Please sign in to comment.