Jump to content

synchronize TileEntity


daafganggdg

Recommended Posts

I'm using an ISimpleBlockRenderingHandler to render a block, depending on a variable in the TileEntity of that block. I change that variable in the onBlockActivated() method of the block.

However, it only renders correctly when i remove the world.isRemote check and i think that because the client tileEntity is synchronized after the rendering. But I heard you should do all the tileEntity stuff on server only. I guess when running that mod on a server only the player that clicked the block is actually going to see the changes in the rendering.. So well What can i do to get this to work? :)

Link to comment
Share on other sites

	@Override
    public Packet getDescriptionPacket()
    {
    	NBTTagCompound nbt = new NBTTagCompound();
	nbt.setInteger("content", Item.getIdFromItem(this.content));
        return new S35PacketUpdateTileEntity(this.xCoord, this.yCoord, this.zCoord, 1, nbt);
    }
    
    @Override
    public void onDataPacket(NetworkManager net, S35PacketUpdateTileEntity pkt)
    {
    	super.onDataPacket(net, pkt);
    	this.content = Item.getItemById(pkt.func_148857_g().getInteger("content"));
    	net.processReceivedPackets();
    }

I already do

Link to comment
Share on other sites

When i use world.markBlockForUpdate(x, y, z); client and server side its working :P

Is that normal?

 

Edit: Actually no, now its now rendering the last state, like it should render stateB its rendering stateA, it should render stateC its rendering stateB, it should render stateD its rendering stateC...

Link to comment
Share on other sites

Trace the execution to check that whenever the state changes that a packet is sent as expected and that the value extracted from the packet is as expected. You can trace either with console statements (I usually use them because it is a bit easier to play the game at same time as tracing execution) or use breakpoints and watch values in debug mode of your IDE.

Check out my tutorials here: http://jabelarminecraft.blogspot.com/

Link to comment
Share on other sites

@Override
    public Packet getDescriptionPacket()
    {
    	NBTTagCompound nbt = new NBTTagCompound();
	nbt.setInteger("content", Item.getIdFromItem(this.content));
	System.out.println("PACKET SENT");
	return new S35PacketUpdateTileEntity(this.xCoord, this.yCoord, this.zCoord, 2, nbt);
       
    }
    
    @Override
    public void onDataPacket(NetworkManager net, S35PacketUpdateTileEntity pkt)
    {
    	super.onDataPacket(net, pkt);
    	this.content = Item.getItemById(pkt.func_148857_g().getInteger("content"));
    	net.processReceivedPackets();
	System.out.println(Item.getItemById(pkt.func_148857_g().getInteger("content")) != null ? Item.getItemById(pkt.func_148857_g().getInteger("content")).getUnlocalizedName() : "empty");
    }

 

console:

(right clicking with water bucket)

PACKET SENT

item.bucketWater

(right clicking with empty bucket)

PACKET SENT

empty

(right clicking with water bucket)

PACKET SENT

item.bucketWater

 

soo, just how its supposed to work..

Link to comment
Share on other sites

TE:

public class TileEntityCauldron extends TileEntity implements ISidedInventory{

private ItemStack[] slots = new ItemStack[9];
private Item content = null;





@Override
public int getSizeInventory() {

	return 0;
}

@Override
public ItemStack getStackInSlot(int var1) {
	if(!this.isInvalid()) return this.slots[var1];
	return null;
}

@Override
public void updateEntity(){

}

@Override
public ItemStack decrStackSize(int var1, int var2) {
	if(this.slots[var1] != null) {
		ItemStack itemstack;
		if(this.slots[var1].stackSize <= var2) {
			itemstack = this.slots[var1];
			this.slots[var1] = null;
			return itemstack;
		} else {
			itemstack = this.slots[var1].splitStack(var2);

			if(this.slots[var1].stackSize == 0) {
				this.slots[var1] = null;
			}
			return itemstack;
		}

	} else return null;
}

@Override
public ItemStack getStackInSlotOnClosing(int var1) {
	if(this.slots[var1] != null) {
		ItemStack itemstack = this.slots[var1];
		this.slots[var1] = null;
		return itemstack;
	}
	return null;
}

@Override
public void setInventorySlotContents(int var1, ItemStack var2) {
	this.slots[var1] = var2;
	if(var2 != null && var2.stackSize > this.getInventoryStackLimit()) {
		var2.stackSize = this.getInventoryStackLimit();
	}
}

@Override
public String getInventoryName() {
	return null;
}

@Override
public boolean hasCustomInventoryName() {
//        return this.localizedName != null && this.localizedName.length() > 0;
	return false;
}

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

@Override
public boolean isUseableByPlayer(EntityPlayer player) {
	return this.worldObj.getTileEntity(this.xCoord, this.yCoord, this.zCoord) != this ? false : player.getDistanceSq((double)this.xCoord + 0.5D, (double)this.yCoord + 0.5D, (double)this.zCoord + 0.5D) <= 64;
}

@Override
public void openInventory() {

}

@Override
public void closeInventory() {


}

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

@Override
public int[] getAccessibleSlotsFromSide(int var1) {

	return null;
}

@Override
public boolean canInsertItem(int var1, ItemStack var2, int var3) {
	return this.isItemValidForSlot(var1, var2);
}

@Override
public boolean canExtractItem(int var1, ItemStack var2, int var3) {
	return false;
}

public void serverSideClick() {
	Item item = CauldronRecipes.getResult(this.content, this.slots);
	if(item != null) {
		this.content = item;
		this.slots = new ItemStack[9];
	}
}

public void onButtonClick() {
	Civilized.network.sendToServer(new CallServerMessage(xCoord, yCoord, zCoord));
}

public Item getContent() {
	return this.content;
}

@Override
    public Packet getDescriptionPacket()
    {
	super.getDescriptionPacket();
    	NBTTagCompound nbt = new NBTTagCompound();
	nbt.setInteger("content", Item.getIdFromItem(this.content));
	System.out.println("PACKET SEND");
	return new S35PacketUpdateTileEntity(this.xCoord, this.yCoord, this.zCoord, 2, nbt);
       
    }
    
    @Override
    public void onDataPacket(NetworkManager net, S35PacketUpdateTileEntity pkt)
    {
    	super.onDataPacket(net, pkt);
    	this.content = Item.getItemById(pkt.func_148857_g().getInteger("content"));
	System.out.println(Item.getItemById(pkt.func_148857_g().getInteger("content")) != null ? Item.getItemById(pkt.func_148857_g().getInteger("content")).getUnlocalizedName() : "empty");
    }

public void setContent(Item item) {
	this.content = item;
	this.worldObj.markBlockForUpdate(this.xCoord, this.yCoord, this.zCoord);
}

    public void readFromNBT(NBTTagCompound nbt)
    {
    	this.content = Item.getItemById(nbt.getInteger("content"));
    	super.readFromNBT(nbt);
        NBTTagList nbttaglist = nbt.getTagList("Items", 10);
        this.slots = new ItemStack[this.getSizeInventory()];

        for (int i = 0; i < nbttaglist.tagCount(); ++i)
        {
            NBTTagCompound nbttagcompound1 = nbttaglist.getCompoundTagAt(i);
            byte b0 = nbttagcompound1.getByte("Slot");

            if (b0 >= 0 && b0 < this.slots.length)
            {
                this.slots[b0] = ItemStack.loadItemStackFromNBT(nbttagcompound1);
            }
        }


    }

    public void writeToNBT(NBTTagCompound nbt)
    {
    	super.writeToNBT(nbt);
    	nbt.setInteger("content", Item.getIdFromItem(this.content));
        NBTTagList nbttaglist = new NBTTagList();

        for (int i = 0; i < this.slots.length; ++i)
        {
            if (this.slots[i] != null)
            {
                NBTTagCompound nbttagcompound1 = new NBTTagCompound();
                nbttagcompound1.setByte("Slot", (byte)i);
                this.slots[i].writeToNBT(nbttagcompound1);
                nbttaglist.appendTag(nbttagcompound1);
            }
        }
        
        nbt.setTag("Items", nbttaglist);

    }

}

 

ISBRH: (Yes its the vanilla cauldron code, completly unobviouscated :))

public class CauldronRenderer implements ISimpleBlockRenderingHandler {

@Override
public void renderInventoryBlock(Block block, int metadata, int modelId,
		RenderBlocks renderer) {
}

@Override
public boolean renderWorldBlock(IBlockAccess world, int x, int y, int z, Block block, int modelId, RenderBlocks renderer) {
		block = Blocks.cauldron;
		renderer.renderStandardBlock(block, x, y, z);
        Tessellator tessellator = Tessellator.instance;
        tessellator.setBrightness(block.getMixedBrightnessForBlock(renderer.blockAccess, x, y, z));
        int l = block.colorMultiplier(renderer.blockAccess, x, y, z);
        float f = (float)(l >> 16 & 255) / 255.0F;
        float f1 = (float)(l >> 8 & 255) / 255.0F;
        float f2 = (float)(l & 255) / 255.0F;
        float f4;

        if (EntityRenderer.anaglyphEnable)
        {
            float f3 = (f * 30.0F + f1 * 59.0F + f2 * 11.0F) / 100.0F;
            f4 = (f * 30.0F + f1 * 70.0F) / 100.0F;
            float f5 = (f * 30.0F + f2 * 70.0F) / 100.0F;
            f = f3;
            f1 = f4;
            f2 = f5;
        }

        tessellator.setColorOpaque_F(f, f1, f2);
        IIcon iicon1 = block.getBlockTextureFromSide(2);
        f4 = 0.125F;
        renderer.renderFaceXPos(block, (double)((float)x - 1.0F + f4), (double)y, (double)z, iicon1);
        renderer.renderFaceXNeg(block, (double)((float)x + 1.0F - f4), (double)y, (double)z, iicon1);
        renderer.renderFaceZPos(block, (double)x, (double)y, (double)((float)z - 1.0F + f4), iicon1);
        renderer.renderFaceZNeg(block, (double)x, (double)y, (double)((float)z + 1.0F - f4), iicon1);
        IIcon iicon2 = BlockCauldron.getCauldronIcon("inner");
        renderer.renderFaceYPos(block, (double)x, (double)((float)y - 1.0F + 0.25F), (double)z, iicon2);
        renderer.renderFaceYNeg(block, (double)x, (double)((float)y + 1.0F - 0.75F), (double)z, iicon2);
        System.out.println("RENDERING");
        TileEntityCauldron tileEntity = (TileEntityCauldron) world.getTileEntity(x, y, z);
        if (tileEntity != null)
        {	
        	IIcon iicon = null;
        	
        	if(tileEntity.getContent() == Items.water_bucket) iicon = BlockLiquid.getLiquidIcon("water_still");
//	        	else iicon = MyBlocks.CauldronBlock.getIcon(0, 0); //TODO
        	if(iicon != null) renderer.renderFaceYPos(block, x, y - 0.0625F * 2, z, iicon);	     
        }

        return true;
}

@Override
public boolean shouldRender3DInInventory(int modelId) {
	return false;
}

@Override
public int getRenderId() {
	return MyBlocks.CauldronBlock.getRenderType();
}

}

 

I'm not done yet, sorry if its a little mess :D

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.