Add state support to structures

This commit is contained in:
Bernhard Bonigl committed 2016-10-09 17:09:48 +02:00
1 parent c6c923dcde
commit 72fb9f29fe
6 files changed
+90 -58

No files matched your search

@@ -22,24 +22,9 @@ public class StructureBlockAccess implements IBlockAccess {
private final StructureInfo data;
private final IBlockState[][][] structure;
public StructureBlockAccess(ItemStack[][][] structure, StructureInfo data) {
public StructureBlockAccess(StructureInfo data) {
this.data = data;
this.structure = Arrays.stream(structure).map(layer -> {
return Arrays.stream(layer).map(row -> {
return Arrays.stream(row).map(itemstack -> {
return convert(itemstack);
}).collect(Collectors.toList()).toArray(new IBlockState[0]);
}).collect(Collectors.toList()).toArray(new IBlockState[0][]);
}).collect(Collectors.toList()).toArray(new IBlockState[0][][]);
}
private IBlockState convert(ItemStack itemstack) {
if(itemstack != null && itemstack.getItem() instanceof ItemBlock) {
return ((ItemBlock) itemstack.getItem()).getBlock().getStateFromMeta(itemstack.getItemDamage());
}
else {
return Blocks.AIR.getDefaultState();
}
this.structure = data.data;
}
@Nullable
@@ -65,7 +50,7 @@ public class StructureBlockAccess implements IBlockAccess {
if(z >= 0 && z < structure[y][x].length) {
int index = y * (data.structureLength * data.structureWidth) + x * data.structureWidth + z;
if(index <= data.getLimiter()) {
return structure[y][x][z];
return structure[y][x][z] != null ? structure[y][x][z] : Blocks.AIR.getDefaultState();
}
}
}
@@ -1,9 +1,21 @@
package slimeknights.mantle.client.book;
import net.minecraft.block.Block;
import net.minecraft.block.properties.IProperty;
import net.minecraft.block.state.IBlockState;
import net.minecraft.init.Blocks;
import net.minecraft.item.ItemStack;
import net.minecraftforge.common.property.IExtendedBlockState;
import net.minecraftforge.common.property.IUnlistedProperty;
import java.util.Map;
import java.util.Optional;
import java.util.stream.IntStream;
import slimeknights.mantle.client.book.data.element.BlockData;
public class StructureInfo {
public ItemStack[][][] data;
public IBlockState[][][] data;
public int blockCount = 0;
public int[] countPerLevel;
public int structureHeight = 0;
@@ -11,40 +23,74 @@ public class StructureInfo {
public int structureWidth = 0;
public int showLayer = -1;
private int blockIndex = -1;
private int blockIndex = 0;
private int maxBlockIndex;
public StructureInfo(ItemStack[][][] structure) {
init(structure);
maxBlockIndex = blockIndex = structureHeight * structureLength * structureWidth;
}
public StructureInfo(int length, int height, int width, BlockData[] blockData) {
this.structureWidth = width;
this.structureHeight = height;
this.structureLength = length;
IBlockState[][][] states = new IBlockState[height][length][width];
public void init(ItemStack[][][] structure) {
data = structure;
structureHeight = structure.length;
structureWidth = 0;
structureLength = 0;
countPerLevel = new int[structureHeight];
blockCount = 0;
for(int h = 0; h < structure.length; h++) {
if(structure[h].length > structureLength) {
structureLength = structure[h].length;
}
int perLvl = 0;
for(int l = 0; l < structure[h].length; l++) {
if(structure[h][l].length > structureWidth) {
structureWidth = structure[h][l].length;
}
for(ItemStack ss : structure[h][l]) {
if(ss != null) {
perLvl++;
for(int y = 0; y < height; y++) {
for(int x = 0; x < length; x++) {
for(int z = 0; z < width; z++) {
for(BlockData data : blockData) {
if(inside(x, y, z, data.pos, data.endPos)) {
states[y][x][z] = convert(data);
break;
}
}
}
}
countPerLevel[h] = perLvl;
blockCount += perLvl;
}
data = states;
maxBlockIndex = blockIndex = structureHeight * structureLength * structureWidth;
}
private IBlockState convert(BlockData data) {
Block block = Block.getBlockFromName(data.block);
if(block == null) {
return Blocks.AIR.getDefaultState();
}
IBlockState state;
if(data.state == null || data.state.isEmpty()) {
state = block.getStateFromMeta(data.meta);
}
else {
state = block.getDefaultState();
for(Map.Entry<String, String> entry : data.state.entrySet()) {
Optional<IProperty<?>> property = state.getPropertyNames().stream().filter(iProperty -> entry.getKey().equals(iProperty.getName())).findFirst();
if(property.isPresent()) {
state = setProperty(state, property.get(), entry.getValue());
}
}
}
return state;
}
private <T extends Comparable<T>> IBlockState setProperty(IBlockState state, IProperty<T> prop, String valueString) {
com.google.common.base.Optional<T> value = prop.parseValue(valueString);
if(value.isPresent()) {
state = state.withProperty(prop, value.get());
}
return state;
}
private boolean inside(int x, int y, int z, int[] rangeStart, int[] rangeEnd) {
if(x >= rangeStart[0] && x <= rangeEnd[0]) {
if(y >= rangeStart[1] && y <= rangeEnd[1]) {
if(z >= rangeStart[2] && z <= rangeEnd[2]) {
return true;
}
}
}
return false;
}
public void setShowLayer(int layer) {
@@ -71,7 +117,7 @@ public class StructureInfo {
int x = r / structureWidth;
int z = r % structureWidth;
return data[y][x][z] == null;
return data[y][x][z] == null || data[y][x][z].getBlock() == Blocks.AIR;
}
public int getLimiter() {
@@ -2,6 +2,8 @@ package slimeknights.mantle.client.book.data.element;
import com.google.gson.JsonObject;
import java.util.Map;
public class BlockData {
public int[] pos;
@@ -9,4 +11,5 @@ public class BlockData {
public String block;
public byte meta;
public JsonObject nbt;
public Map<String, String> state;
}
@@ -78,7 +78,7 @@ public class ElementStructure extends SizedBookElement {
public void init(int[] size, BlockData[] data) {
int yOff = 0;
/*
ItemStack[][][] structure = new ItemStack[size[1]][size[0]][size[2]];
for(int sy = 0; sy < size[1]; sy++) {
@@ -91,10 +91,10 @@ public class ElementStructure extends SizedBookElement {
}
}
}
}
}*/
structureData = new StructureInfo(structure);
blockAccess = new StructureBlockAccess(structure, structureData);
structureData = new StructureInfo(size[0], size[1], size[2], data);
blockAccess = new StructureBlockAccess(structureData);
rotX = 25;
@@ -187,6 +187,7 @@ public class ElementStructure extends SizedBookElement {
}
}
canTick = false;
if(canTick) {
if(++tick % 20 == 0) {
structureData.step();