Git Tile inventory rendering working

This commit is contained in:
kitsushadow
2016-12-04 23:51:05 -05:00
parent ad7b519c95
commit dc8b6e5474
4 changed files with 163 additions and 84 deletions

View File

@@ -10,6 +10,7 @@ import net.minecraft.block.properties.IProperty;
import net.minecraft.block.properties.PropertyBool;
import net.minecraft.block.state.BlockStateContainer;
import net.minecraft.block.state.IBlockState;
import net.minecraft.client.Minecraft;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.entity.player.EntityPlayer;
@@ -65,6 +66,7 @@ public class Firebox extends CustomContainerFacing implements ITileEntityProvide
TileFirebox tile = (TileFirebox) world.getTileEntity(pos);
if (tile != null)
{
System.out.println("Server Slot: " + tile.getStackInSlot(0));
ItemStack playerStack = player.getHeldItemMainhand();
Item playerItem;
@@ -77,8 +79,8 @@ public class Firebox extends CustomContainerFacing implements ITileEntityProvide
BlockPos tempPos = new BlockPos(pos.getX(), pos.getY()+1, pos.getZ());
if(world.getBlockState(tempPos).getBlock().equals(Blocks.AIR)){
world.setBlockState(tempPos, Blocks.FIRE.getDefaultState(), 2);
this.setLightLevel(1);
tile.markDirty();
world.notifyBlockUpdate(pos, state, state, 2);
}
}
}
@@ -89,11 +91,16 @@ public class Firebox extends CustomContainerFacing implements ITileEntityProvide
world.setBlockState(pos, state.withProperty(ACTIVE, false), 2);
ItemStack returnStack = new ItemStack(tileStack.getItem(), tileStack.stackSize - 1);
player.setHeldItem(EnumHand.MAIN_HAND, returnStack);
tile.markDirty();
world.notifyBlockUpdate(pos, state, state, 2);
} else {
player.setHeldItem(EnumHand.MAIN_HAND, tileStack);
tile.markDirty();
world.notifyBlockUpdate(pos, state, state, 2);
}
tile.setInventorySlotContents(0, null);
tile.markDirty();
world.notifyBlockUpdate(pos, state, state, 2);
}
if(tileStack == null && playerStack != null) {
@@ -103,11 +110,16 @@ public class Firebox extends CustomContainerFacing implements ITileEntityProvide
tile.setInventorySlotContents(0, playerStack);
player.setHeldItem(EnumHand.MAIN_HAND, null);
tile.markDirty();
world.notifyBlockUpdate(pos, state, state, 2);
}
}
}
}
}
if(world.isRemote){
TileFirebox tile = (TileFirebox) world.getTileEntity(pos);
System.out.println("Client Slot: " + tile.getStackInSlot(0));
}
return true;
}

View File

@@ -3,10 +3,16 @@ package nmd.primal.forgecraft.renders;
import net.minecraft.block.Block;
import net.minecraft.block.state.IBlockState;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.GlStateManager;
import net.minecraft.client.renderer.OpenGlHelper;
import net.minecraft.client.renderer.RenderHelper;
import net.minecraft.client.renderer.RenderItem;
import net.minecraft.client.renderer.block.model.ItemCameraTransforms;
import net.minecraft.client.renderer.entity.RenderManager;
import net.minecraft.client.renderer.texture.TextureMap;
import net.minecraft.client.renderer.tileentity.TileEntitySpecialRenderer;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.init.Items;
import net.minecraft.item.ItemBlock;
import net.minecraft.item.ItemStack;
import net.minecraft.util.EnumFacing;
@@ -22,9 +28,10 @@ public class TileFireboxRender extends TileEntitySpecialRenderer<TileFirebox>
{
private final RenderItem renderItem = Minecraft.getMinecraft().getRenderItem();
@Override
public void renderTileEntityAt(TileFirebox tile, double x, double y, double z, float partialTicks, int destroyStage)
{
public void renderTileEntityAt(TileFirebox tile, double x, double y, double z, float partialTicks, int destroyStage) {
GL11.glPushMatrix();
GL11.glTranslated(x + 0.5D, y + 0.875D, z + 0.5D);
GL11.glScalef(0.25F, 0.25F, 0.25F);
@@ -39,45 +46,48 @@ public void renderTileEntityAt(TileFirebox tile, double x, double y, double z, f
//int bright = (this.getLight(tile.getWorld(), pos) / 2);
OpenGlHelper.setLightmapTextureCoords(OpenGlHelper.lightmapTexUnit, bright % 65536, bright / 65536);
IBlockState state = this.getWorld().getBlockState(pos);
Block block = state.getBlock();
float rotation = 0;
if(state.getValue(Firebox.FACING) == EnumFacing.EAST){
rotation = 90;
}
///
// offset height of rendering for worktable slabs
// Think about ways to avoid instanceof
///
//IBlockState state = this.getWorld().getBlockState(pos);
//Block block = state.getBlock();
ItemStack stack = tile.getStackInSlot(0);
if (stack != null) {
float height = 0.2f;
float scale = 1.5F;
///
// rotate 90 degrees for blocks but not items?
///
boolean is_block = stack.getItem() instanceof ItemBlock;
float rotation = is_block ? 180.0F : 90.0F;
float height = is_block ? 0.52F : 0.42F;
//float scale = is_block ? 0.8F : 1.2F;
float scale = is_block ? 0.8F : 1.5F;
//RenderHelper.glItem(renderItem, stack, shelf_column, 0.0F, row_height, rotation);
GL11.glPushMatrix();
//GL11.glTranslated(0.5F, 0.0D, 0.5D);
//GL11.glTranslated(rack_column * 0.75F, 0.0D, craft_row * 0.75F);
//GL11.glTranslated(rack_column * 1.5F, 0.0D, rack_row);
GL11.glTranslated(1.4F, 0.0D, 1.5D);
GL11.glTranslatef(0.0F, 0.0F, 0.0F);
//GL11.glTranslatef(0.75F, 0.52F, 0.25F);
GL11.glTranslatef(0.75F, height, 0.25F);
GL11.glScalef(scale, scale, scale);
//
// item/block rotation
GL11.glRotatef(rotation, 0.0F, 1.0F, 0.0F);
//GL11.glRotatef(0F , 0.0F, 0.0F, 0.0F);
//GL11.glRotatef(90.0F, 1.0F, 0.0F, 0.0F);
GL11.glRotatef(rotation, 1.0F, 0.0F, 0.0F);
GL11.glRotatef(180.0F, 0.0F, 0.0F, 1.0F);
renderItem.renderItem(stack, renderItem.getItemModelMesher().getItemModel(stack));
GL11.glPopMatrix();
}
OpenGlHelper.setLightmapTextureCoords(OpenGlHelper.lightmapTexUnit, prevLGTX, prevLGTY);
GL11.glPopMatrix();
}
}

View File

@@ -1,6 +1,7 @@
package nmd.primal.forgecraft.tiles;
import net.minecraft.block.state.IBlockState;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
@@ -15,4 +16,32 @@ public abstract class BaseTile extends TileEntity{
{
return oldState.getBlock() != newSate.getBlock();
}
@Override
public void readFromNBT(NBTTagCompound nbt)
{
super.readFromNBT(nbt);
this.readNBT(nbt);
}
@Override
public NBTTagCompound writeToNBT(NBTTagCompound nbt)
{
nbt = super.writeToNBT(nbt);
return this.writeNBT(nbt);
}
public NBTTagCompound readNBT(NBTTagCompound nbt)
{
// Override in lower tile classes
return nbt;
}
public NBTTagCompound writeNBT(NBTTagCompound nbt)
{
// Override in lower tile classes
return nbt;
}
}

View File

@@ -4,6 +4,7 @@ import com.sun.org.apache.xpath.internal.operations.Bool;
import net.minecraft.block.properties.PropertyBool;
import net.minecraft.block.state.IBlockState;
import net.minecraft.client.gui.GuiChat;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Blocks;
import net.minecraft.inventory.IInventory;
@@ -16,6 +17,8 @@ import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.ITickable;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraftforge.common.util.Constants;
import nmd.primal.forgecraft.CommonUtils;
import nmd.primal.forgecraft.blocks.Firebox;
import nmd.primal.forgecraft.init.ModBlocks;
@@ -28,7 +31,7 @@ import static nmd.primal.forgecraft.CommonUtils.getVanillaItemBurnTime;
*/
public class TileFirebox extends BaseTile implements IInventory, ITickable {
private ItemStack[] inventory;
private ItemStack[] inventory = new ItemStack [0];
private String customName;
private int iteration = 0;
@@ -39,25 +42,36 @@ public class TileFirebox extends BaseTile implements IInventory, ITickable {
@Override
public void update () {
if(!worldObj.isRemote){
World world = this.getWorld();
this.iteration ++;
if(this.iteration == 200 ) {
//System.out.println(iteration);
this.iteration = 0;
IBlockState state = worldObj.getBlockState(this.getPos());
IBlockState state = world.getBlockState(this.pos);
if (worldObj.getBlockState(this.getPos()).getValue(Firebox.ACTIVE)) {
if (this.getStackInSlot(0) == null) {
worldObj.setBlockState(this.getPos(), state.withProperty(Firebox.ACTIVE, false), 2);
this.markDirty();
world.notifyBlockUpdate(pos, state, state, 2);
} else {
if(this.getStackInSlot(0) != null) {
if (worldObj.rand.nextInt((int) Math.floor(getVanillaItemBurnTime(this.getStackInSlot(0)) / 20)) == 0) {
this.decrStackSize(0, 1);
//System.out.println(this.getStackInSlot(0));
this.markDirty();
world.notifyBlockUpdate(pos, state, state, 2);
}
}
}
}
}
}
/*if (worldObj.isRemote){
World world = this.getWorld();
if(this.getStackInSlot(0)!=null){
renderItem = new EntityItem(world, this.getPos().getX(), this.getPos().getY(), this.getPos().getZ(), this.getStackInSlot(0));
}
}*/
}
public String getCustomName() {
@@ -121,17 +135,24 @@ public class TileFirebox extends BaseTile implements IInventory, ITickable {
@Override
public void setInventorySlotContents(int index, ItemStack stack) {
if (index < 0 || index >= this.getSizeInventory())
if (index < 0 || index >= this.getSizeInventory()) {
return;
}
if (stack != null && stack.stackSize > this.getInventoryStackLimit())
if (stack != null && stack.stackSize > this.getInventoryStackLimit()) {
stack.stackSize = this.getInventoryStackLimit();
}
if (stack != null && stack.stackSize == 0)
if (stack != null && stack.stackSize == 0) {
stack = null;
}
this.inventory[index] = stack;
this.markDirty();
World world = this.getWorld();
IBlockState state = world.getBlockState(this.pos);
world.notifyBlockUpdate(this.pos, state, state, 3);
}
@Override
@@ -186,42 +207,49 @@ public class TileFirebox extends BaseTile implements IInventory, ITickable {
}
}
// ***************************************************************************** //
// NBT
// ***************************************************************************** //
@Override
public NBTTagCompound writeToNBT(NBTTagCompound nbt) {
super.writeToNBT(nbt);
public NBTTagCompound readNBT(NBTTagCompound nbt)
{
NBTTagList list = nbt.getTagList("Items", Constants.NBT.TAG_COMPOUND);
inventory = new ItemStack[this.getSizeInventory()];
NBTTagList list = new NBTTagList();
for (int i = 0; i < this.getSizeInventory(); ++i) {
if (this.getStackInSlot(i) != null) {
NBTTagCompound stackTag = new NBTTagCompound();
stackTag.setByte("Slot", (byte) i);
this.getStackInSlot(i).writeToNBT(stackTag);
list.appendTag(stackTag);
}
}
nbt.setTag("Items", list);
if (this.hasCustomName()) {
nbt.setString("CustomName", this.getCustomName());
}
return nbt;
}
@Override
public void readFromNBT(NBTTagCompound nbt) {
super.readFromNBT(nbt);
NBTTagList list = nbt.getTagList("Items", 10);
for (int i = 0; i < list.tagCount(); ++i) {
NBTTagCompound stackTag = list.getCompoundTagAt(i);
int slot = stackTag.getByte("Slot") & 255;
this.setInventorySlotContents(slot, ItemStack.loadItemStackFromNBT(stackTag));
for (int i = 0; i < list.tagCount(); ++i)
{
NBTTagCompound tag = list.getCompoundTagAt(i);
this.inventory[tag.getByte("slot")] = ItemStack.loadItemStackFromNBT(tag);
}
if (nbt.hasKey("CustomName", 8)) {
this.setCustomName(nbt.getString("CustomName"));
}
return nbt;
}
@Override
public NBTTagCompound writeNBT(NBTTagCompound nbt)
{
NBTTagList list = new NBTTagList();
///
// RackMatrix
///
for (int i = 0; i < this.getSizeInventory(); ++i)
{
if (inventory[i] != null) {
NBTTagCompound tag = new NBTTagCompound();
tag.setByte("slot", (byte) i);
inventory[i].writeToNBT(tag);
list.appendTag(tag);
}
}
nbt.setTag("Items", list);
return nbt;
}
@Override