Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Featured Replies

Posted

I have a tile entity with no GUI and it's completely functional but I want it to render differently if there's a piece of paper in it. I've looked for ages but I can't find a way to synchronise the server instance with the client instance without making my mod 300x bigger and 99.9% improvised. I tried initiating a Packet250CustomPayload but apparently they don't exist anymore. If anyone knows a way to send an itemstack from server to client, it would be greatly appreciated. Thanks in advance!

Hi

 

Some of the vanilla TileEntities do this (for example TileEntityBeacon)

 

Key places to look are:

TileEntityBeacon::

public Packet getDescriptionPacket()  (-->S35PacketUpdateTileEntity)

    public void readFromNBT(NBTTagCompound p_145839_1_)

    public void writeToNBT(NBTTagCompound p_145841_1_)

and since your TileEntity isn't vanilla you will also need the Forge method

TileEntity::

public void onDataPacket(NetworkManager net, S35PacketUpdateTileEntity pkt)

 

 

-TGG

  • Author

I tried that, but now the block doesn't render at all. It renders for a few ticks, then it seems after a bit of debugging System.out.println()s that renderTileEntityAt() isn't even getting called. I commented out the packet methods you told me about and it worked fine, without the syncing.

  • Author

public class TileEntityResearchDesk extends TileEntity implements IInventory {

private ItemStack[] contents = new ItemStack[1];

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

@Override
public ItemStack getStackInSlot(int p_70301_1_) {
	return contents[p_70301_1_];
}

@Override
public ItemStack decrStackSize(int p_70298_1_, int p_70298_2_) {
	if (this.contents[p_70298_1_] != null)
        {
            ItemStack itemstack;

            if (this.contents[p_70298_1_].stackSize <= p_70298_2_)
            {
                itemstack = this.contents[p_70298_1_];
                this.contents[p_70298_1_] = null;
                this.markDirty();
                return itemstack;
            }
            else
            {
                itemstack = this.contents[p_70298_1_].splitStack(p_70298_2_);

                if (this.contents[p_70298_1_].stackSize == 0)
                {
                    this.contents[p_70298_1_] = null;
                }

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

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

@Override
public void setInventorySlotContents(int p_70299_1_, ItemStack p_70299_2_) {

	contents[p_70299_1_] = p_70299_2_;

}

@Override
public String getInventoryName() {
	return "Research Desk";
}

@Override
public boolean hasCustomInventoryName() {
	return false;
}

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

@Override
public boolean isUseableByPlayer(EntityPlayer p_70300_1_) {
	return true;
}

@Override
public void openInventory() {}

@Override
public void closeInventory() {}

@Override
public boolean isItemValidForSlot(int p_94041_1_, ItemStack p_94041_2_) {
	if (p_94041_2_.getItem() == Item.itemRegistry.getObject("paper")) {
		return true;
	}
	else
		return false;
}

@Override
public void readFromNBT(NBTTagCompound nbtTag) {

	super.readFromNBT(nbtTag);
	contents[0] = ItemStack.loadItemStackFromNBT(nbtTag.getCompoundTag("Item"));

}

@Override
public void writeToNBT(NBTTagCompound nbtTag) {

	super.writeToNBT(nbtTag);
	NBTTagCompound item = new NBTTagCompound();
	if (contents[0] != null)
		contents[0].writeToNBT(item);
	nbtTag.setTag("Item", item);

}

public ResourceLocation getBlockTexture() {
	if (getStackInSlot(0) == null) {
		System.out.println("Loaded No Paper Texture");
		return new ResourceLocation("soulwizardry:textures/blocks/ModelResearchDesk.png");
	}
	else {
		System.out.println("Loaded Paper Texture");
		return new ResourceLocation("soulwizardry:textures/blocks/ModelResearchDeskPaper.png");
	}

}

@Override
public Packet getDescriptionPacket() {
	NBTTagCompound tag = new NBTTagCompound();
	writeToNBT(tag);
	return new S35PacketUpdateTileEntity(xCoord, yCoord, zCoord, 3, tag);
}

@Override
public void onDataPacket(NetworkManager net, S35PacketUpdateTileEntity pkt) {

	xCoord = pkt.func_148853_f();
	yCoord = pkt.func_148854_e();
	zCoord = pkt.func_148855_d();
	contents[0].loadItemStackFromNBT(pkt.func_148857_g());

}

}

public class RenderResearchDesk extends TileEntitySpecialRenderer {

private final ModelResearchDesk model;

@Override
public void renderTileEntityAt(TileEntity p_147500_1_, double x,
		double y, double z, float scale) {
	System.out.println("rendering");
	TileEntityResearchDesk tentity = (TileEntityResearchDesk) p_147500_1_;
	GL11.glPushMatrix();
	GL11.glTranslatef((float) x + 0.5F, (float) y + 1.5F, (float) z + 0.5F);
	ResourceLocation textures = tentity.getBlockTexture();
	Minecraft.getMinecraft().renderEngine.bindTexture(textures);
	System.out.println("Got the texture, bound it");
	GL11.glPushMatrix();
	GL11.glRotatef(180F, 0F, 0F, 1F);
	this.model.render((Entity)null, 0.0F, 0.0F, -0.1F, 0.0F, 0.0F, 0.0625F);
	GL11.glPopMatrix();
	GL11.glPopMatrix();
}

public RenderResearchDesk() {
	model = new ModelResearchDesk();
}

private void adjustRotatePivotViaMeta(World world, int x, int y, int z) {
        int meta = world.getBlockMetadata(x, y, z);
        GL11.glPushMatrix();
        GL11.glRotatef(meta * (-90), 0.0F, 0.0F, 1.0F);
        GL11.glPopMatrix();
}

private void adjustLightFixture(World world, int i, int j, int k, Block block) {
        Tessellator tess = Tessellator.instance;
        float brightness = block.getLightValue(world, i, j, k);
        int skyLight = world.getLightBrightnessForSkyBlocks(i, j, k, 0);
        int modulousModifier = skyLight % 65536;
        int divModifier = skyLight / 65536;
        tess.setColorOpaque_F(brightness, brightness, brightness);
        OpenGlHelper.setLightmapTextureCoords(OpenGlHelper.lightmapTexUnit,  (float) modulousModifier,  divModifier);
}

}

In the

onDataPacket

  method, you need call

readFromNBT

instead of directly calling

ItemStack#loadItemStackFromNBT

Don't PM me with questions. They will be ignored! Make a thread on the appropriate board for support.

 

1.12 -> 1.13 primer by williewillus.

 

1.7.10 and older versions of Minecraft are no longer supported due to it's age! Update to the latest version for support.

 

http://www.howoldisminecraft1710.today/

Also, for a custom packet make a class that implements IMessage

Check out my mod, Realms of Chaos, here.

 

If I helped you, be sure to press the "Thank You" button!

  • Author

Yeah, as expected, I crashed with an NPE at the line:

	contents[0].readFromNBT(pkt.func_148857_g().getCompoundTag("Item"));

  • Author

I can't think of a way to do this. For some reason using

	contents[0] = ItemStack.loadItemStackFromNBT(pkt.func_148857_g().getCompoundTag("Item"));

makes the render method only get called once, but I can't use

	contents[0].readFromNBT(pkt.funt_148857_g().getCompoundTag("Item"));

because contents[0] is null.  :-\

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...

Important Information

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

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.