Skip to content

Commit

Permalink
More delegating methods. New Test showing writing ids by blocks is ba…
Browse files Browse the repository at this point in the history
…d strategy
  • Loading branch information
Pavel Fedin committed Nov 27, 2016
1 parent 1c0edcb commit 4bab776
Show file tree
Hide file tree
Showing 6 changed files with 156 additions and 7 deletions.
16 changes: 13 additions & 3 deletions src/main/java/ru/wpstuio/amorphine/mcaliases/Block.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
package ru.wpstuio.amorphine.mcaliases;


import ru.wpstuio.amorphine.utils.Coordinates3d;

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

private short id_a;
private short id_b;

private TileEntity tileEntity = null;

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

this.id_a = id_a;
}
}
15 changes: 15 additions & 0 deletions src/main/java/ru/wpstuio/amorphine/mcaliases/Chunk.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.mojang.nbt.IntTag;
import com.mojang.nbt.ListTag;
import ru.wpstuio.amorphine.utils.Coordinates2d;
import ru.wpstuio.amorphine.utils.Coordinates3d;

import static ru.wpstuio.amorphine.utils.Formulae.localBlockFromBlock;
import static ru.wpstuio.amorphine.utils.Formulae.sectionFromBlock;
Expand Down Expand Up @@ -91,6 +92,20 @@ public void changeBlockId(int x, int y, int z, byte id) {
block_bytes_tag.data[offset] = id;
}

public void changeBlockId(Coordinates3d cords, byte id) {
int local_x = localBlockFromBlock(cords.getX());
int local_y = localBlockFromBlock(cords.getY());
int local_z = localBlockFromBlock(cords.getZ());

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

int section_index = sectionFromBlock(cords.getY());

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

block_bytes_tag.data[offset] = id;
}

/**
* Returns id of the block with specified XYZ coordinates
* @param x represents EAST - SOUTH axe
Expand Down
54 changes: 53 additions & 1 deletion src/main/java/ru/wpstuio/amorphine/mcaliases/Region.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,18 @@
import com.mojang.nbt.NbtIo;
import net.minecraft.world.level.chunk.storage.RegionFile;
import ru.wpstuio.amorphine.utils.Coordinates2d;
import ru.wpstuio.amorphine.utils.Coordinates3d;
import ru.wpstuio.amorphine.utils.Formulae;

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

import static ru.wpstuio.amorphine.utils.Formulae.localChunkFromBlock;

public class Region {

private RegionFile region_file;
private final RegionFile region_file;

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

Expand Down Expand Up @@ -94,4 +96,54 @@ public void saveTagAsChunk(Coordinates2d cords, CompoundTag tag) throws IOExcept

stream.close();
}

/**
* Saves all property chunks to region file
*/
public void save() throws IOException {
for(int x = 31; x >= 0; x--) {
for (int z = 31; z >= 0; z--) {

Chunk chunk = chunks[x][z];

if (chunk == null)
continue;

CompoundTag tag = chunk.getTag();

DataOutputStream stream = region_file.getChunkDataOutputStream(x, z);

NbtIo.write(tag, stream);

stream.close();
}
}
}

public void changeBlockId(Coordinates3d cords, byte id) throws IOException {
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];
chunk.changeBlockId(cords, id);

this.save();
}

public byte getBlockId(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.getBlockId(block_global_x, block_global_y, block_global_z);

return id;
}
}
29 changes: 27 additions & 2 deletions src/main/java/ru/wpstuio/amorphine/mcaliases/World.java
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
package ru.wpstuio.amorphine.mcaliases;

import com.googlecode.concurrentlinkedhashmap.ConcurrentLinkedHashMap;
import com.sun.xml.internal.ws.policy.privateutil.PolicyUtils;
import net.minecraft.world.level.chunk.storage.RegionFile;
import ru.wpstuio.amorphine.utils.Coordinates2d;
import ru.wpstuio.amorphine.utils.Coordinates3d;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentMap;

import static ru.wpstuio.amorphine.utils.Formulae.regionFromBlock;


public class World {

private final File world_dir;

Map<Coordinates2d, RegionFile> region_files = new HashMap<Coordinates2d, RegionFile>();
private Map<Coordinates2d, RegionFile> region_files = new HashMap<Coordinates2d, RegionFile>();

private ConcurrentMap<Coordinates2d, Region> regions = new ConcurrentLinkedHashMap.Builder<Coordinates2d, Region>()
.maximumWeightedCapacity(10)
Expand Down Expand Up @@ -52,6 +54,13 @@ public World(File world_dir) throws IOException {
}
}

public void save() throws IOException {
for(Map.Entry entry: regions.entrySet()) {
Region region = (Region) entry.getValue();
region.save();
}
}

/**
* Returns a region with specified coordinates from cache. If it is not in the cache, it is put there.
* @param x
Expand All @@ -73,4 +82,20 @@ public Region getRegion(int x, int z) {
return region;
}
}

public void changeBlockID(Coordinates3d cords, byte id) throws IOException {
int region_x = regionFromBlock(cords.getX());
int region_z = regionFromBlock(cords.getZ());

Region region = this.getRegion(region_x, region_z);
region.changeBlockId(cords, id);
}

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

Region region = this.getRegion(region_x, region_z);
return region.getBlockId(cords);
}
}
2 changes: 1 addition & 1 deletion src/test/java/TestReadWrite.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class TestReadWrite {
public void testAdd() {

World world;
byte id_to_make = 26;
byte id_to_make = 3;

try {

Expand Down
47 changes: 47 additions & 0 deletions src/test/java/TestReadWrite2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import org.ini4j.Ini;
import org.junit.Test;
import ru.wpstuio.amorphine.mcaliases.World;
import ru.wpstuio.amorphine.utils.Coordinates3d;

import java.io.File;
import java.io.IOException;

import static org.junit.Assert.assertTrue;


public class TestReadWrite2 {
@Test
public void testAdd() {

World world;
byte id_to_make = 13;

try {
//initializing path to region folder
String presets_url = TestReadWrite.class.getClassLoader().getResource("local_presets.ini").getPath();
Ini ini = new Ini(new File(presets_url));
String path_to_mca_folder = ini.get("paths", "world_path", String.class);

world = new World(new File (path_to_mca_folder));

Coordinates3d cords;
for(int inty = 66; inty < 100; inty++){
for(int intz = -10; intz < 10; intz++){
for(int intx = -10; intx < 10; intx++){
world.changeBlockID(new Coordinates3d(intx, inty, intz), id_to_make);
}
}
}
world.save();

world = new World(new File (path_to_mca_folder));
cords = new Coordinates3d(1, 66 ,1);
byte id = world.getBlockId(cords);

assertTrue(id == id_to_make);

} catch (IOException e) {
e.printStackTrace();
}
}
}

0 comments on commit 4bab776

Please sign in to comment.