I was kind of following this tutorial: https://www.youtube.com/watch?v=NNclUS5edcY&t=1230s
I have a custom item saved inside my TileEntity but it only shows up on world join not when changing the Item. Here is the Code:
The update get and set have the same Code as read and write so it should be working.
This is what is not working:
@Override
public void onDataPacket(NetworkManager net, SPacketUpdateTileEntity pkt) {
NBTTagCompound tag = pkt.getNbtCompound();
readUpdateTag(tag);
this.readFromNBT(tag);
}
@Override
public SPacketUpdateTileEntity getUpdatePacket() {
NBTTagCompound tag = new NBTTagCompound();
this.writeUpdateTag(tag);
return new SPacketUpdateTileEntity(pos, getBlockMetadata(), tag);
}
@Override
public NBTTagCompound getUpdateTag() {
NBTTagCompound tag = super.getUpdateTag();
writeUpdateTag(tag);
return tag;
}
public void writeUpdateTag(NBTTagCompound tag) {
tag.setBoolean("infusing", infusing);
tag.setInteger("infusiontimer", infusionTimer);
NBTTagCompound compoundItem = new NBTTagCompound();
compoundItem = item.serializeNBT();
tag.setTag("item", compoundItem);
}
public void readUpdateTag(NBTTagCompound tag) {
infusing = tag.getBoolean("infusing");
infusionTimer = tag.getInteger("infusiontimer");
NBTTagCompound compoundItem = tag.getCompoundTag("item");
item = new ItemStack(compoundItem);
}
.
First = TileEntity. Second = renderer
package net.mysticsouls.wandustrie.tileentity;
import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.network.NetworkManager;
import net.minecraft.network.play.server.SPacketUpdateTileEntity;
import net.minecraft.tileentity.TileEntity;
public class TileEntityManaInfuser extends TileEntity {
public ItemStack item;
boolean infusing;
int infusionTimer;
public void toggleItem(ItemStack item) {
if(world.isRemote) return;
ItemStack stack = removeItem();
if(stack != null) {
world.spawnEntity(new EntityItem(world, pos.getX(), pos.getY() + 1, pos.getZ(), stack));
}
ItemStack newItem = item.copy();
newItem.setCount(1);
setItem(newItem);
resetInfuse();
markDirty();
IBlockState state = world.getBlockState(pos);
world.notifyBlockUpdate(pos, state, state, 3);
}
ItemStack removeItem() {
ItemStack stack = this.item;
this.item = null;
return stack;
}
void setItem(ItemStack item) {
this.item = item;
}
void resetInfuse() {
infusing = false;
infusionTimer = 0;
}
@Override
public NBTTagCompound writeToNBT(NBTTagCompound compound) {
super.writeToNBT(compound);
this.writeUpdateTag(compound);
return compound;
}
@Override
public void readFromNBT(NBTTagCompound compound) {
super.readFromNBT(compound);
this.readUpdateTag(compound);
}
@Override
public void onDataPacket(NetworkManager net, SPacketUpdateTileEntity pkt) {
NBTTagCompound tag = pkt.getNbtCompound();
readUpdateTag(tag);
this.readFromNBT(tag);
}
@Override
public SPacketUpdateTileEntity getUpdatePacket() {
NBTTagCompound tag = new NBTTagCompound();
this.writeUpdateTag(tag);
return new SPacketUpdateTileEntity(pos, getBlockMetadata(), tag);
}
@Override
public NBTTagCompound getUpdateTag() {
NBTTagCompound tag = super.getUpdateTag();
writeUpdateTag(tag);
return tag;
}
public void writeUpdateTag(NBTTagCompound tag) {
tag.setBoolean("infusing", infusing);
tag.setInteger("infusiontimer", infusionTimer);
NBTTagCompound compoundItem = new NBTTagCompound();
compoundItem = item.serializeNBT();
tag.setTag("item", compoundItem);
}
public void readUpdateTag(NBTTagCompound tag) {
infusing = tag.getBoolean("infusing");
infusionTimer = tag.getInteger("infusiontimer");
NBTTagCompound compoundItem = tag.getCompoundTag("item");
item = new ItemStack(compoundItem);
}
}
package net.mysticsouls.wandustrie.tileentity.render;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.GlStateManager;
import net.minecraft.client.renderer.tileentity.TileEntitySpecialRenderer;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.item.ItemStack;
import net.mysticsouls.wandustrie.init.ModItems;
import net.mysticsouls.wandustrie.tileentity.TileEntityManaInfuser;
public class RendererManaInfuser extends TileEntitySpecialRenderer<TileEntityManaInfuser>{
private EntityItem ITEM = new EntityItem(Minecraft.getMinecraft().world, 0, 0, 0, new ItemStack(ModItems.magicOreIngot));
@Override
public void renderTileEntityAt(TileEntityManaInfuser te, double x, double y, double z, float partialTicks,
int destroyStage) {
super.renderTileEntityAt(te, x, y, z, partialTicks, destroyStage);
GlStateManager.pushMatrix();
{
if(te.item != null) {
ITEM = new EntityItem(Minecraft.getMinecraft().world, 0, 0, 0, te.item);
ITEM.hoverStart = 0F;
GlStateManager.translate(x, y, z);
GlStateManager.translate(0.5, 0, 0.5);
GlStateManager.rotate(45F, 0, 1, 0);
Minecraft.getMinecraft().getRenderManager().doRenderEntity(ITEM, 0, 0, 0, 0F, 0F, false);
}
}
GlStateManager.popMatrix();
}
}