Skip to content

Commit

Permalink
Realised Block class (doubt it is really usefull). Implemented some m…
Browse files Browse the repository at this point in the history
…ethods about getting block ids
  • Loading branch information
Pavel Fedin committed Nov 29, 2016
1 parent 7d9d833 commit c22837b
Show file tree
Hide file tree
Showing 6 changed files with 151 additions and 11 deletions.
76 changes: 72 additions & 4 deletions src/main/java/mregioneer/mcaliases/Block.java
Original file line number Diff line number Diff line change
@@ -1,23 +1,91 @@
package mregioneer.mcaliases;


import com.mojang.nbt.ByteArrayTag;
import mregioneer.utils.Coordinates3d;
import mregioneer.utils.Formulae;
import mregioneer.utils.Helpers;

public class Block {
private final int x;
private final int z;
private final int y;

private short id_a;
private short id_b;
private final int section_index;

private final int offset;

private byte id_a;
private byte id_b;

private byte[] blocks;
private byte[] add;

private TileEntity tileEntity = null;

public Block(Coordinates3d cords, short id_a) {
protected Block(Coordinates3d cords, Section section) {
this.x = cords.getX();
this.z = cords.getZ();
this.y = cords.getY();

this.id_a = id_a;
this.blocks = ((ByteArrayTag)section.get("Blocks")).data;

int local_x = Formulae.localBlockFromBlock(x);
int local_y = Formulae.localBlockFromBlock(y);
int local_z = Formulae.localBlockFromBlock(z);

int section_index = Formulae.sectionFromBlock(y);
this.section_index = section_index;

this.offset = local_y * 16 * 16 + local_z * 16 + local_x;

this.id_a = blocks[offset];
try {
this.add = ((ByteArrayTag)section.get("Add")).data;
this.id_b = Helpers.Nibble4(add, offset);
} catch (NullPointerException e) {
add = null;
this.id_b = 0;
}
}

public int getX() {
return x;
}

public int getZ() {
return z;
}

public int getY() {
return y;
}

public byte getId_a() {
return id_a;
}

public void setId_a(byte id_a) {
this.blocks[offset] = id_a;
}

public byte getId_b() {
return id_b;
}

/* method should work with 4-bits
public void setId_b(byte id_b) {
this.id_b = id_b;
}
*/

/* not implemented yet
public TileEntity getTileEntity() {
return tileEntity;
}
*/

public void setTileEntity(TileEntity tileEntity) {
this.tileEntity = tileEntity;
}
}
32 changes: 28 additions & 4 deletions src/main/java/mregioneer/mcaliases/Chunk.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import mregioneer.utils.Coordinates3d;
import mregioneer.utils.Formulae;

import static mregioneer.utils.Formulae.sectionFromBlock;


public class Chunk {

Expand Down Expand Up @@ -93,7 +95,7 @@ public void changeBlockId(int x, int y, int z, byte id) {

int offset = local_y * 16 * 16 + local_z * 16 + local_x;

byte section_index = (byte) Formulae.sectionFromBlock(y);
byte section_index = (byte) sectionFromBlock(y);
Section section = getSection(section_index);

ByteArrayTag block_bytes_tag = (ByteArrayTag) section.get("Blocks");
Expand All @@ -113,7 +115,7 @@ public void changeBlockId(Coordinates3d cords, byte id) {

int offset = local_y * 16 * 16 + local_z * 16 + local_x;

byte section_index = (byte) Formulae.sectionFromBlock(cords.getY());
byte section_index = (byte) sectionFromBlock(cords.getY());
Section section = getSection(section_index);

ByteArrayTag block_bytes_tag = (ByteArrayTag) section.get("Blocks");
Expand All @@ -123,7 +125,7 @@ public void changeBlockId(Coordinates3d cords, byte id) {

/**
* Returns id of the block with specified XYZ coordinates
* @param x represents EAST - SOUTH axe
* @param x represents EAST - WEST axe
* @param y represents height axe
* @param z represents NORTH - SOUTH axe
* @return byte of the basic id of the block
Expand All @@ -133,7 +135,7 @@ public byte getBlockId(int x, int y, int z) {
int local_y = Formulae.localBlockFromBlock(y);
int local_z = Formulae.localBlockFromBlock(z);

int section_index = Formulae.sectionFromBlock(y);
int section_index = sectionFromBlock(y);

int offset = local_y * 16 * 16 + local_z * 16 + local_x;

Expand All @@ -144,4 +146,26 @@ public byte getBlockId(int x, int y, int z) {
return (byte) 0;
}
}

public byte getBlockAddId(int x, int y, int z) {
int local_x = Formulae.localBlockFromBlock(x);
int local_y = Formulae.localBlockFromBlock(y);
int local_z = Formulae.localBlockFromBlock(z);

int section_index = sectionFromBlock(y);

int offset = local_y * 16 * 16 + local_z * 16 + local_x;

try {
ByteArrayTag add_bytes_tag = (ByteArrayTag) sections[section_index].get("Add");
return add_bytes_tag.data[offset];
} catch (NullPointerException e) {
return (byte) 0;
}
}

public Block getBlock(Coordinates3d cords) {
byte section_index = (byte) sectionFromBlock(cords.getY());
return new Block(cords, getSection(section_index));
}
}
26 changes: 26 additions & 0 deletions src/main/java/mregioneer/mcaliases/Region.java
Original file line number Diff line number Diff line change
Expand Up @@ -156,4 +156,30 @@ public byte getBlockId(Coordinates3d cords) {

return id;
}

public byte getBlockAddId(Coordinates3d cords) {
int block_global_x = cords.getX();
int block_global_z = cords.getZ();
int block_global_y = cords.getY();

int chunk_local_x = localChunkFromBlock(block_global_x);
int chunk_local_z = localChunkFromBlock(block_global_z);

Chunk chunk = chunks[chunk_local_x][chunk_local_z];
byte id = chunk.getBlockAddId(block_global_x, block_global_y, block_global_z);

return id;
}

public Block getBlock(Coordinates3d cords) {
int block_global_x = cords.getX();
int block_global_z = cords.getZ();

int chunk_local_x = localChunkFromBlock(block_global_x);
int chunk_local_z = localChunkFromBlock(block_global_z);

Chunk chunk = chunks[chunk_local_x][chunk_local_z];

return chunk.getBlock(cords);
}
}
8 changes: 8 additions & 0 deletions src/main/java/mregioneer/mcaliases/Vector.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package mregioneer.mcaliases;

/**
* Represents a set of blocks included in a cuboid specified with two coordinates;
*/
public class Vector {

}
16 changes: 16 additions & 0 deletions src/main/java/mregioneer/mcaliases/World.java
Original file line number Diff line number Diff line change
Expand Up @@ -113,4 +113,20 @@ public byte getBlockId(Coordinates3d cords) {
Region region = this.getRegion(region_x, region_z);
return region.getBlockId(cords);
}

public byte getBlockAddId(Coordinates3d cords) {
int region_x = regionFromBlock(cords.getX());
int region_z = regionFromBlock(cords.getZ());

Region region = this.getRegion(region_x, region_z);
return region.getBlockAddId(cords);
}

public Block getBlock(Coordinates3d cords) {
int region_x = regionFromBlock(cords.getX());
int region_z = regionFromBlock(cords.getZ());

Region region = this.getRegion(region_x, region_z);
return region.getBlock(cords);
}
}
4 changes: 1 addition & 3 deletions src/main/java/mregioneer/utils/Helpers.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package mregioneer.utils;

/**
* Created by amorphine on 23.11.16.
*/

public class Helpers {
/**
* Helps to calculate offset for Add and Data sections which are known to keep 4 bits for a block
Expand Down

0 comments on commit c22837b

Please sign in to comment.