Skip to content

Commit

Permalink
New alias - Section. Fixed problems connected with writing/reading bl…
Browse files Browse the repository at this point in the history
…ocks into 'not existing' sections (sections with air only)
  • Loading branch information
Pavel Fedin committed Nov 28, 2016
1 parent dc76631 commit a50683a
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package ru.wpstuio.amorphine.exception;

/**
* Created by amorphine on 28.11.16.
*/
public class EmptySection extends Exception {

}
35 changes: 25 additions & 10 deletions src/main/java/ru/wpstuio/amorphine/mcaliases/Chunk.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class Chunk {
private final int local_x;
private final int local_z;

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

public Chunk(Coordinates2d local_cords, CompoundTag tag) {
this.tag = tag;
Expand All @@ -40,9 +40,11 @@ public Chunk(Coordinates2d local_cords, CompoundTag tag) {

ListTag sections_tag = (ListTag) c_tag_level.get("Sections");
for(int i = 0; i <= 15; i++){
CompoundTag section;
Section section;
CompoundTag section_tag;
try{
section = (CompoundTag) sections_tag.get(i);
section_tag = (CompoundTag) sections_tag.get(i);
section = new Section((byte)i, section_tag);
} catch (IndexOutOfBoundsException e) {
continue;
}
Expand Down Expand Up @@ -70,6 +72,14 @@ public CompoundTag getTag() {
return tag;
}

public Section getSection(byte index) {
if(sections[index] == null) {
sections[index] = new Section(index);
}

return sections[index];
}

/**
* Changes specified block basic id to with the given one
* @param x represents EAST - SOUTH axe
Expand All @@ -85,9 +95,10 @@ public void changeBlockId(int x, int y, int z, byte id) {

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

int section_index = sectionFromBlock(y);
byte section_index = (byte) sectionFromBlock(y);
Section section = getSection(section_index);

ByteArrayTag block_bytes_tag = (ByteArrayTag) sections[section_index].get("Blocks");
ByteArrayTag block_bytes_tag = (ByteArrayTag) section.get("Blocks");

block_bytes_tag.data[offset] = id;
}
Expand All @@ -99,9 +110,10 @@ public void changeBlockId(Coordinates3d cords, byte id) {

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

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

ByteArrayTag block_bytes_tag = (ByteArrayTag) sections[section_index].get("Blocks");
ByteArrayTag block_bytes_tag = (ByteArrayTag) section.get("Blocks");

block_bytes_tag.data[offset] = id;
}
Expand All @@ -122,8 +134,11 @@ public byte getBlockId(int x, int y, int z) {

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

ByteArrayTag block_bytes_tag = (ByteArrayTag) sections[section_index].get("Blocks");

return block_bytes_tag.data[offset];
try {
ByteArrayTag block_bytes_tag = (ByteArrayTag) sections[section_index].get("Blocks");
return block_bytes_tag.data[offset];
} catch (NullPointerException e) {
return (byte) 0;
}
}
}
40 changes: 39 additions & 1 deletion src/main/java/ru/wpstuio/amorphine/mcaliases/Section.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,52 @@
package ru.wpstuio.amorphine.mcaliases;

import com.mojang.nbt.ByteArrayTag;
import com.mojang.nbt.ByteTag;
import com.mojang.nbt.CompoundTag;
import com.mojang.nbt.Tag;

import java.util.Arrays;


public class Section {
private byte index;
private final byte index;
private CompoundTag tag;

public byte getIndex() {
return index;
}

public CompoundTag getTag() {
return tag;
}

public Section(byte index, CompoundTag tag) {
this.index = index;
this.tag = tag;
}

public Section(byte index) {
this.index = index;

CompoundTag main_tag = new CompoundTag();

byte[] byte4096 = new byte[4096];
Arrays.fill(byte4096, (byte) 0);

byte[] byte2048 = new byte[2048];
Arrays.fill(byte2048, (byte) 0);

main_tag.put("Add", new ByteArrayTag("Add", byte2048.clone()));
main_tag.put("Blocks", new ByteArrayTag("Blocks", byte4096.clone()));
main_tag.put("SkyLight", new ByteArrayTag("SkyLight", byte2048.clone()));
main_tag.put("Y", new ByteTag("Y", index));
main_tag.put("BlockLight", new ByteArrayTag("BlockLight", byte2048.clone()));
main_tag.put("Data", new ByteArrayTag("Data", byte2048.clone()));

this.tag = main_tag;
}

public Tag get(String name) {
return this.tag.get(name);
}
}

0 comments on commit a50683a

Please sign in to comment.