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

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now

Announcements



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • It wasnt mappings issue, I was reading fabric and not forge. Fabric has PlayerEntity while forge has Player. Through a mixin I was able to change the player's velocity so that there is a sort of slow-motion type movement. Only issue is that jumping seems to be handled separately. Changing that velocity only makes the jump lower, not slower. Unsure how to increase the duration in which a jump happens.   @Mixin(LivingEntity.class) public abstract class ExampleMixin extends Entity { public ExampleMixin(EntityType<?> type, World world) { super(type, world); } @Inject(method = "tickMovement", at = @At("HEAD")) private void onTickMovement(CallbackInfo ci) { // Access the player entity LivingEntity entity = (LivingEntity)(Object)this; // Modify player's acceleration by applying a force in the opposite direction double slowdownFactor = 0.75; // Adjust as needed entity.setVelocity(entity.getVelocity().multiply(1, slowdownFactor, 1)); } }  
    • Hi so i made a modded survival, and when i go to the page 13 of my JEI,  my game instant crash and give this error "The game crashed whilst rendering item Error", I don't know what to do i give you the crash report : https://pastebin.com/EhzYdEZg
    • You need to run build at least once before running the game for the first time in a dev environment. Make sure stuff's working *before* you start making changes to the mdk template
    • Greetings, people of the forge forums: I come to you in a time of need. While creating my silly block entity that is (in theory) supposed to point to a position within world space I've started to question how to actually implement it.   How would I get a tile entity element (or bone) to change its pitch/yaw/roll? While I do have prior experience with programming, I have no actual experience with the forge API except the things listed in its docs. Please correct me if I am in any way wrong.
    • Making my own screen by extending from net.minecraft.client.gui.screens.Screen and override render method. I am trying to make green square only to be visible when intersecting with red square. This is what I got so far: @Override public void render(PoseStack poseStack, int mx, int my, float partialTick) {     RenderSystem.enableDepthTest(); // Red square     GuiComponent.fill(pPoseStack, 32, 32, 64, 64, 0xFFFF0000);     RenderSystem.depthFunc(GlConst.GL_LEQUAL); // Green square     GuiComponent.fill(pPoseStack, mx-16, my-16, mx+16, my+16, 0xFF00FF00); } Thank you in advance.
  • Topics

×
×
  • Create New...

Important Information

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