Jump to content

[1.8] Problems rendering Items in Inventory with TESR


ModernPatriot

Recommended Posts

Im trying to render the item in the inventory of my block one block above it, but it doesnt seem to work.

This may be pretty obvious because Im pretty bad when it comes to the TESR.

 

P.S.: If there are/is more errors/inefficient code please point it out. Im kinda new to 1.8 modding and the last mod I did was in 1.2.5 or so :)

 

TESR Class:

 

public class BasicGearboxTESR extends TileEntitySpecialRenderer{


public BasicGearboxTESR() {

}

@Override
public void renderTileEntityAt(TileEntity tileentity, double x, double z, double y, float f, int i) {
	Tessellator tessellator = Tessellator.getInstance();
	GL11.glPushMatrix();
	if(tileentity instanceof BasicGearboxData) {
			ItemStack itemstack = new ItemStack(((BasicGearboxData) tileentity).getStackInSlot(0).getItem());
			if(itemstack != null){
				BlockPos pos = tileentity.getPos();
				double posX = pos.getX();
				double posY = pos.getY();
				double posZ = pos.getZ();
				GL11.glTranslated(posX, posY+1, posZ);
				Minecraft.getMinecraft().getRenderItem().renderItemModel(itemstack);
				tessellator.draw();
				GL11.glPopMatrix();
			}
	}else{
		GL11.glPopMatrix();
	}
}

}

 

 

TileEntity class

 

public class BasicGearboxData extends TileEntity implements IInventory {

private final long INVALID_TIME = 0;
private long lastTime = INVALID_TIME;
private double lastAngularPosition;
private String customName;
private ItemStack[] inventoryContent;

public BasicGearboxData() {
	this.inventoryContent = new ItemStack[this.getSizeInventory()];
}

public String getCustomName() {
        return this.customName;
    }


public void setCustomName(String customName) {
        this.customName = customName;
    }
                  //  U S E D   F O R   R O T A T I O N
public double getNextAngularPosition(double revsPerSecond) {
	long timeNow = System.nanoTime();
	if (lastTime == INVALID_TIME) {
		lastTime = timeNow;
		lastAngularPosition = 0.0;
	}
	final double DEGREES_PER_REV = 360.0;
	final double NANOSECONDS_PER_SECOND = 1e9;
	double nextAngularPosition = lastAngularPosition + (timeNow - lastTime) * revsPerSecond * DEGREES_PER_REV / NANOSECONDS_PER_SECOND;
	nextAngularPosition = nextAngularPosition % DEGREES_PER_REV;
	lastAngularPosition = nextAngularPosition;
	lastTime = timeNow;
	return nextAngularPosition;
}

@Override
public String getName() {
	return this.hasCustomName() ? this.customName : "container.basicgearbox";
}

@Override
public boolean hasCustomName() {
	return this.customName != null && !this.customName.equals("Basic Gearbox");
}

@Override
public IChatComponent getDisplayName() {
	return this.hasCustomName() ? new ChatComponentText(this.getName()) : new ChatComponentTranslation(this.getName());
}

@Override
public int getSizeInventory() {
	return 1;
}

@Override
public ItemStack getStackInSlot(int index) {
	 if (index < 0 || index >= this.getSizeInventory())
	        return null;
	    return this.inventoryContent[index];
}

@Override
public ItemStack decrStackSize(int index, int count) {
	if (this.getStackInSlot(index) != null) {
        ItemStack itemstack;

        if (this.getStackInSlot(index).stackSize <= count) {
            itemstack = this.getStackInSlot(index);
            this.setInventorySlotContents(index, null);
            this.markDirty();
            return itemstack;
        } else {
            itemstack = this.getStackInSlot(index).splitStack(count);

            if (this.getStackInSlot(index).stackSize <= 0) {
                this.setInventorySlotContents(index, null);
        } else {
                this.setInventorySlotContents(index, this.getStackInSlot(index));
        }

            this.markDirty();
            return itemstack;
        }
    } else {
        return null;
    }
}

@Override
public ItemStack getStackInSlotOnClosing(int index) {
	ItemStack stack = this.getStackInSlot(index);
    this.setInventorySlotContents(index, null);
    return stack;
}

@Override
public void setInventorySlotContents(int index, ItemStack stack) {
	if (index < 0 || index >= this.getSizeInventory())
        return;

    if (stack != null && stack.stackSize > this.getInventoryStackLimit())
        stack.stackSize = this.getInventoryStackLimit();
        
    if (stack != null && stack.stackSize == 0)
        stack = null;

    this.inventoryContent[index] = stack;
    this.markDirty();
}

@Override
public int getInventoryStackLimit() {
	return 1;
}

@Override
public boolean isUseableByPlayer(EntityPlayer player) {
	return this.worldObj.getTileEntity(this.getPos()) == this && player.getDistanceSq(this.pos.add(0.5, 0.5, 0.5)) <= 64;
}

@Override
public void openInventory(EntityPlayer player) {
	// Not needed (yet)
}

@Override
public void closeInventory(EntityPlayer player) {
	// Not needed (yet)
}

@Override
public boolean isItemValidForSlot(int index, ItemStack stack) {
	return true;
}

@Override
public int getField(int id) {
	return 0;
}

@Override
public void setField(int id, int value) {
}

@Override
public int getFieldCount() {
	return 0;
}

@Override
public void clear() {
	 for (int i = 0; i < this.getSizeInventory(); i++)
	        this.setInventorySlotContents(i, null);
}

@Override
public void writeToNBT(NBTTagCompound nbt) {
    super.writeToNBT(nbt);

    NBTTagList listItems = 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);
            listItems.appendTag(stackTag);
        }
    }
    nbt.setTag("Items", listItems);

    if (this.hasCustomName()) {
        nbt.setString("CustomName", this.getCustomName());
    }
}


@Override
public void readFromNBT(NBTTagCompound nbt) {
    super.readFromNBT(nbt);

    NBTTagList listItems = nbt.getTagList("Items", 10);
    for (int i = 0; i < listItems.tagCount(); ++i) {
        NBTTagCompound stackTag = listItems.getCompoundTagAt(i);
        int slot = stackTag.getByte("Slot") & 255;
        this.setInventorySlotContents(slot, ItemStack.loadItemStackFromNBT(stackTag));
    }

    if (nbt.hasKey("CustomName", ) {
        this.setCustomName(nbt.getString("CustomName"));
    }
}



}

 

 

 

ClientProxy class

 

public class ClientProxy extends CommonProxy {



@Override
public void registerRenders() {
	ReimaginedBlocks.registerRenders();
	ReimaginedItems.registerRenders();
	   
}
@Override
public void preInit(FMLPreInitializationEvent e) {
	super.preInit(e);
}

    @Override
    public void init(FMLInitializationEvent e) {
        super.init(e);
    }

    @Override
    public void postInit(FMLPostInitializationEvent e) {
        super.postInit(e);
    }
   
    public void registerTESR() {
    	ClientRegistry.bindTileEntitySpecialRenderer(BasicGearboxData.class, new BasicGearboxTESR());
    }
    @SideOnly(Side.CLIENT)
    public static void registerSided() {
    	
    }
}

Link to comment
Share on other sites

tesr's are for blocks not items.

Did you even read his question?

 

@OP Your problem is that the client side version of the TileEntity does not know anything about what is in its inventory, so if you want that information available, you need to send that information via packet. Luckily, TileEntities have a built-in method to do so called getDescriptionPacket:

@Override
public Packet getDescriptionPacket() {
	NBTTagCompound tag = new NBTTagCompound();
	this.writeToNBT(tag);
	return new S35PacketUpdateTileEntity(getPos(), 1, tag);
}

@Override
public void onDataPacket(NetworkManager net, S35PacketUpdateTileEntity packet) {
	readFromNBT(packet.getNbtCompound());
}

That's the simplest implementation, but if you have lots of data that doesn't need to be sent, you're better off only sending what you need (e.g. one single ItemStack instead of everything).

 

The packet is automatically sent any time the block is marked for an update, so any time your inventory changes you will want to call worldObj.markBlockForUpdate(getPos());

Link to comment
Share on other sites

Thank you very much for replying this quick :)

 

So Ive been going a lil wild with the code and I changed a lot, but it's still not rendering.

Where's my error?

 

BasicGearbox.class (IMPORTANT BITS)

@Override
    public boolean onBlockActivated(World worldIn, BlockPos pos, IBlockState state, EntityPlayer playerIn, EnumFacing side, float hitX, float hitY, float hitZ) {
BasicGearboxData bgd = (BasicGearboxData)worldIn.getTileEntity(pos);
if(bgd.getInventoryContent() == null) {
if(playerIn.getCurrentEquippedItem() != null) {
//SOMETHING IN HAND, NOTHING IN INVENTORY
int meta = playerIn.getCurrentEquippedItem().getMetadata();
bgd.setInventoryContent(new ItemStack(playerIn.getCurrentEquippedItem().getItem(), 1, meta));
if(playerIn.getCurrentEquippedItem().stackSize!=0 &! playerIn.capabilities.isCreativeMode == true) {
playerIn.getCurrentEquippedItem().stackSize--;
}
return true;
}else{
// NOTHING IN HAND AND INVENTORY
return false;
}
}else{
if(playerIn.getCurrentEquippedItem() == null) {
// SOMETHING IN INVENTORY, NOTHING IN HAND
ItemStack inInv = bgd.getInventoryContent();
if(!worldIn.isRemote) {
worldIn.spawnEntityInWorld(new EntityItem(worldIn, pos.getX() + 0.5, pos.getY() + 1, pos.getZ() + 0.5, inInv));
// Effects
worldIn.playSoundEffect(pos.getX() + 0.5, pos.getY() + 0.5, pos.getZ() + 0.5, "random.click", 0.3F, 1.3F-(worldIn.rand.nextFloat()*0.1F));
worldIn.playSoundEffect(pos.getX() + 0.5, pos.getY() + 0.5, pos.getZ() + 0.5, "random.orb", 0.6F, 0.6F-(worldIn.rand.nextFloat()*0.2F));
// Reset
bgd.setInventoryContent(null);
}
return true;
}else{
//SOMETHING IN INVENTORY AND HAND
return false;
}
}
}[/Code]

BasicGearboxData.class (TileEntity)

[Code]public class BasicGearboxData extends TileEntityBase implements IInventory {


private final long INVALID_TIME = 0;
private long lastTime = INVALID_TIME;
private double lastAngularPosition;
private String customName;
private ItemStack[] inventoryContent;

public BasicGearboxData() {
this.inventoryContent = new ItemStack[this.getSizeInventory()];
}

public String getCustomName() {
        return this.customName;
    }


public void setCustomName(String customName) {
        this.customName = customName;
    }

//  U S E D  F O R  R O T A T I N G
public double getNextAngularPosition(double revsPerSecond) {
long timeNow = System.nanoTime();
if (lastTime == INVALID_TIME) {
lastTime = timeNow;
lastAngularPosition = 0.0;
}
final double DEGREES_PER_REV = 360.0;
final double NANOSECONDS_PER_SECOND = 1e9;
double nextAngularPosition = lastAngularPosition + (timeNow - lastTime) * revsPerSecond * DEGREES_PER_REV / NANOSECONDS_PER_SECOND;
nextAngularPosition = nextAngularPosition % DEGREES_PER_REV;
lastAngularPosition = nextAngularPosition;
lastTime = timeNow;
return nextAngularPosition;
}

@Override
public String getName() {
return this.hasCustomName() ? this.customName : "container.basicgearbox";
}

@Override
public boolean hasCustomName() {
return this.customName != null && !this.customName.equals("Basic Gearbox");
}

@Override
public IChatComponent getDisplayName() {
return this.hasCustomName() ? new ChatComponentText(this.getName()) : new ChatComponentTranslation(this.getName());
}

@Override
public int getSizeInventory() {
return 1;
}

@Override
public ItemStack getStackInSlot(int index) {
if (index < 0 || index >= this.getSizeInventory())
        return null;
    return this.inventoryContent[index];
}

@Override
public ItemStack decrStackSize(int index, int count) {
if (this.getStackInSlot(index) != null) {
        ItemStack itemstack;

        if (this.getStackInSlot(index).stackSize <= count) {
            itemstack = this.getStackInSlot(index);
            this.setInventorySlotContents(index, null);
            this.markDirty();
            return itemstack;
        } else {
            itemstack = this.getStackInSlot(index).splitStack(count);

            if (this.getStackInSlot(index).stackSize <= 0) {
                this.setInventorySlotContents(index, null);
        } else {
                this.setInventorySlotContents(index, this.getStackInSlot(index));
        }

            this.markDirty();
            return itemstack;
        }
    } else {
        return null;
    }
}

@Override
public ItemStack getStackInSlotOnClosing(int index) {
ItemStack stack = this.getStackInSlot(index);
    this.setInventorySlotContents(index, null);
    return stack;
}

@Override
public void setInventorySlotContents(int index, ItemStack stack) {
if (index < 0 || index >= this.getSizeInventory())
        return;

    if (stack != null && stack.stackSize > this.getInventoryStackLimit())
        stack.stackSize = this.getInventoryStackLimit();
       
    if (stack != null && stack.stackSize == 0)
        stack = null;

    this.inventoryContent[index] = stack;
    this.markDirty();
}

@Override
public int getInventoryStackLimit() {
return 1;
}

@Override
public boolean isUseableByPlayer(EntityPlayer player) {
return this.worldObj.getTileEntity(this.getPos()) == this && player.getDistanceSq(this.pos.add(0.5, 0.5, 0.5)) <= 64;
}

@Override
public boolean isItemValidForSlot(int index, ItemStack stack) {
return true;
}

@Override
public int getField(int id) {
return 0;
}

@Override
public void setField(int id, int value) {
}

@Override
public int getFieldCount() {
return 0;
}

@Override
public void clear() {
for (int i = 0; i < this.getSizeInventory(); i++)
        this.setInventorySlotContents(i, null);
}

@Override
public void writeToNBT(NBTTagCompound nbt) {
    super.writeToNBT(nbt);

    NBTTagList listItems = new NBTTagList();
    for (int i = 0; i < this.getSizeInventory(); ++i) {
        if (this.getStackInSlot(i) != null) {
            NBTTagCompound insideInv = new NBTTagCompound();
            insideInv.setByte("Slots", (byte) i);
            this.getStackInSlot(i).writeToNBT(insideInv);
            listItems.appendTag(insideInv);
           
        }
    }
    nbt.setTag("Items", listItems);
    worldObj.markBlockForUpdate(getPos());
    if (this.hasCustomName()) {
        nbt.setString("CustomName", this.getCustomName());
     
    }
}


@Override
public void readFromNBT(NBTTagCompound nbt) {
    super.readFromNBT(nbt);

    NBTTagList listItems = nbt.getTagList("Items", 10);
    for (int i = 0; i < listItems.tagCount(); ++i) {
        NBTTagCompound stackTag = listItems.getCompoundTagAt(i);
        int slot = stackTag.getByte("Slots") & 255;
        this.setInventorySlotContents(slot, ItemStack.loadItemStackFromNBT(stackTag));
    }

    if (nbt.hasKey("CustomName", 8)) {
        this.setCustomName(nbt.getString("CustomName"));
    }
}

public void writeToPacket(ByteBuf buf){
ByteBufUtils.writeItemStack(buf, inventoryContent[0]);
}

public void readFromPacket(ByteBuf buf) {
inventoryContent[0] = ByteBufUtils.readItemStack(buf);
}

@Override
public void openInventory(EntityPlayer player) {

}

@Override
public void closeInventory(EntityPlayer player) {

}
public void setInventoryContent(ItemStack newStack){
inventoryContent[0] = newStack;
worldObj.markBlockForUpdate(pos);
}
public ItemStack getInventoryContent() {
return inventoryContent[0];
}


}
[/Code]

BasicGearboxTESR.class

[Code]public BasicGearboxTESR() {

}

@Override
public void renderTileEntityAt(TileEntity tileentity, double x, double y, double z, float partialTicks, int partialBlockDamage) {
Tessellator tessellator = Tessellator.getInstance();
BlockPos posB = tileentity.getPos();
World worldIn = tileentity.getWorld();
BasicGearboxData bgd = (BasicGearboxData)worldIn.getTileEntity(posB);
int meta = bgd.getInventoryContent().getMetadata();
GL11.glPushMatrix();
GL11.glPushAttrib(GL11.GL_ENABLE_BIT);
ItemStack itemstack = new ItemStack(bgd.getInventoryContent().getItem(), 1, meta);
if(itemstack != null){
BlockPos pos = tileentity.getPos();
double posX = pos.getX();
double posY = pos.getY();
double posZ = pos.getZ();
GlStateManager.scale(0.9D, 0.9D, 0.9D);
GL11.glTranslated(posX, posY+1, posZ);
Minecraft.getMinecraft().getRenderItem().renderItemModel(itemstack);
tessellator.draw();
}
GL11.glPopMatrix();
}

}[/Code]

DescHandler.class (For redirecting Packages)

[Code]@ChannelHandler.Sharable
public class DescHandler extends SimpleChannelInboundHandler<FMLProxyPacket>{

public static final String CHANNEL = Reference.MODID + "Description";

static {
NetworkRegistry.INSTANCE.newChannel(CHANNEL, new DescHandler());
}

public static void init() {

}


@Override
protected void channelRead0(ChannelHandlerContext ctx, FMLProxyPacket msg) throws Exception {
ByteBuf bufB = msg.payload();
int x = bufB.readInt();
int y = bufB.readInt();
int z = bufB.readInt();
BlockPos pos = new BlockPos(x, y, z);
TileEntity te = ReimaginedIndustries.proxy.getClientPlayer().worldObj.getTileEntity(pos);
if(te instanceof BasicGearboxData) {
((BasicGearboxData)te).readFromPacket(bufB);
}
}

}[/Code]

TileEntityBase.class (Writing Packets)

[Code]public class TileEntityBase extends TileEntity {

public Packet getDescriptionPacket() {

ByteBuf bufB = Unpooled.buffer();
bufB.writeInt(pos.getX());
bufB.writeInt(pos.getY());
bufB.writeInt(pos.getZ());
writeToPacket(bufB);
PacketBuffer buf = new PacketBuffer(bufB);
return new FMLProxyPacket(buf, DescHandler.CHANNEL);
}

public void writeToPacket(ByteBuf buf) {

}

public void readFromPacket(ByteBuf buf) {

}
}[/Code]

 

Theres a bit more code in the Proxies and Main modfile but nothing important :)

Link to comment
Share on other sites

Why would you go about implementing an entire packet & handling system when #getDescriptionPacket and #onDataPacket already exist? Just use the vanilla S35PacketUpdateTileEntity and only send the ItemStack. Less code = less things to go wrong / debug.

 

Similarly, why implement custom get/setInventoryContent methods when there are already getStackInSlot and setInventorySlotContents? Why are you not using the methods already available?

 

And this:

ItemStack itemstack = new ItemStack(bgd.getInventoryContent().getItem(), 1, meta);

Why are you making a NEW ItemStack here? Just get the contents of the first slot:

ItemStack itemstack = bgd.getStackInSlot(0);

That goes for when placing items into the inventory as well. You are going to lose a LOT of information the first time you place an ItemStack with NBT (e.g. enchantments) in there:

bgd.setInventoryContent(new ItemStack(playerIn.getCurrentEquippedItem().getItem(), 1, meta));

Yikes.

 

Finally, did you register your TESR? Have you tried putting debug statements in there to see what's happening, e.g. is the ItemStack ever not null?

 

Aside: Don't mark the block for update when writing to NBT - there is no reason to do this.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.