Skip to content

Commit

Permalink
Multiblock rendering for the book, taken from Immersive engineering a…
Browse files Browse the repository at this point in the history
…nd adapted to use world rendering

Comments are left in so @BluSunrize can adopt the world rendering for his book
  • Loading branch information
bonii-xx committed Oct 2, 2016
1 parent 9d7ae1f commit 6bd5b3a
Show file tree
Hide file tree
Showing 7 changed files with 775 additions and 273 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package slimeknights.mantle.client.book;

import net.minecraft.block.state.IBlockState;
import net.minecraft.init.Blocks;
import net.minecraft.item.ItemBlock;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.IBlockAccess;
import net.minecraft.world.WorldType;
import net.minecraft.world.biome.Biome;

import java.util.Arrays;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

import javax.annotation.Nullable;

public class StructureBlockAccess implements IBlockAccess {

private final StructureInfo data;
private final IBlockState[][][] structure;

public StructureBlockAccess(ItemStack[][][] structure, 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();
}
}

@Nullable
@Override
public TileEntity getTileEntity(BlockPos pos) {
return null;
}

@Override
public int getCombinedLight(BlockPos pos, int lightValue) {
// full brightness always
return 15 << 20 | 15 << 4;
}

@Override
public IBlockState getBlockState(BlockPos pos) {
int x = pos.getX();
int y = pos.getY();
int z = pos.getZ();

if(y >= 0 && y < structure.length) {
if(x >= 0 && x < structure[y].length) {
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 Blocks.AIR.getDefaultState();
}

@Override
public boolean isAirBlock(BlockPos pos) {
return getBlockState(pos).getBlock() == Blocks.AIR;
}

@Override
public Biome getBiome(BlockPos pos) {
return null;
}

@Override
public int getStrongPower(BlockPos pos, EnumFacing direction) {
return 0;
}

@Override
public WorldType getWorldType() {
return null;
}

@Override
public boolean isSideSolid(BlockPos pos, EnumFacing side, boolean _default) {
return false;
}
}
80 changes: 80 additions & 0 deletions src/main/java/slimeknights/mantle/client/book/StructureInfo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package slimeknights.mantle.client.book;

import net.minecraft.item.ItemStack;

public class StructureInfo {
public ItemStack[][][] data;
public int blockCount = 0;
public int[] countPerLevel;
public int structureHeight = 0;
public int structureLength = 0;
public int structureWidth = 0;
public int showLayer = -1;

private int blockIndex = -1;
private int maxBlockIndex;

public StructureInfo(ItemStack[][][] structure) {
init(structure);
maxBlockIndex = blockIndex = structureHeight * structureLength * structureWidth;
}

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++;
}
}
}
countPerLevel[h] = perLvl;
blockCount += perLvl;
}
}

public void setShowLayer(int layer) {
showLayer = layer;
blockIndex = (layer + 1) * (structureLength * structureWidth) - 1;
}

public void reset() {
blockIndex = maxBlockIndex;
}

public void step() {
int start = blockIndex;
do {
if(++blockIndex >= maxBlockIndex) {
blockIndex = 0;
}
} while(isEmpty(blockIndex) && blockIndex != start);
}

private boolean isEmpty(int index) {
int y = index / (structureLength * structureWidth);
int r = index % (structureLength * structureWidth);
int x = r / structureWidth;
int z = r % structureWidth;

return data[y][x][z] == null;
}

public int getLimiter() {
return blockIndex;
}
}
Loading

0 comments on commit 6bd5b3a

Please sign in to comment.