Jump to content

[1.16.5] TileEntityRenderer not accessing TileEntity inventory


bibouche_

Recommended Posts

Hi, so the thing is that I have a TileEntity for which I added a TileEntityRenderer, I already did this with two other TileEntities but this time it was not working and I found out that the Renderer class cannot reach TileEntity inventory, since when I have an ItemStack in a slot, it outputs Air. And also, my two other TileEntities uses ISidedInventory (which I know shouldn't be used but I will change this later), and this one uses IItemHandler. I don't know if it is what causes the issue or if I am networking bad or even if I am missing something, can someone help me please ?

 

TileEntityRenderer :

public class CelestialPowerPedestalTileEntityRenderer extends TileEntityRenderer<CelestialPowerPedestalTileEntity> {

	private Minecraft mc  = Minecraft.getInstance();

	public CelestialPowerPedestalTileEntityRenderer(
		TileEntityRendererDispatcher rendererDispatcherIn) {
		super(rendererDispatcherIn);
	}

	@Override
	public void render(
		CelestialPowerPedestalTileEntity te,
		float partialTicks,
		MatrixStack matrixStackIn,
		IRenderTypeBuffer bufferIn,
		int combinedLightIn,
		int combinedOverlayIn) {

		int lightLevel = getLightLevel(te.getWorld(), te.getPos());

		if (!te.isFillable()) {

			renderItem(te.inv.getStackInSlot(0), new double[] {0.48d, 0.83d, 0.42d},
			           90, 0, 0, matrixStackIn, bufferIn, partialTicks, combinedOverlayIn, lightLevel, 1.0f);

		}
	}

	private void renderItem(
		ItemStack stack, double[] translation, int rotX, int rotY, int rotZ, MatrixStack matrixStack, IRenderTypeBuffer buffer, float partialTicks, int combinedOverlay, int lightLevel, float scale) {

		matrixStack.push();
		matrixStack.translate(translation[0], translation[1], translation[2]);
		matrixStack.rotate(Vector3f.XP.rotationDegrees((float) rotX));
		matrixStack.rotate(Vector3f.YP.rotationDegrees((float) rotY));
		matrixStack.rotate(Vector3f.ZP.rotationDegrees((float) rotZ));
		matrixStack.scale(scale, scale, scale);

		IBakedModel model = mc.getItemRenderer().getItemModelWithOverrides(stack, null, null);
		mc.getItemRenderer().renderItem(stack,
		                                ItemCameraTransforms.TransformType.GROUND, true, matrixStack, buffer, lightLevel, combinedOverlay, model);
		matrixStack.pop();

	}

	private int getLightLevel(
		World world, BlockPos pos) {
		int blockLight = world.getLightFor(LightType.BLOCK, pos);
		int skyLight = world.getLightFor(LightType.SKY, pos);
		return LightTexture.packLight(blockLight, skyLight);
	}

}

TileEntity networking and writing :

public void read(
		BlockState state,
		CompoundNBT nbt) {
		super.read(
			state,
			nbt);

		NonNullList <ItemStack> inv = NonNullList.withSize(
			this.inv.getSlots(),
			ItemStack.EMPTY);
		ItemStackHelper.loadAllItems(
			nbt,
			inv);
		setItems(inv);
	}

	@Nonnull
	@Override
	public CompoundNBT write(
		CompoundNBT compound) {
		super.write(compound);

		ItemStackHelper.saveAllItems(
			compound,
			this.getItems());

		return compound;
	}

	@Nullable
	@Override
	public SUpdateTileEntityPacket getUpdatePacket() {
		CompoundNBT nbt = new CompoundNBT();
		this.write(nbt);
		return new SUpdateTileEntityPacket(
			this.pos,
			0,
			nbt);
	}

	@Override
	public void onDataPacket(
		NetworkManager net,
		SUpdateTileEntityPacket pkt) {
		this.read(
			world.getBlockState(pkt.getPos()),
			pkt.getNbtCompound());
	}

	@Override
	public CompoundNBT getUpdateTag() {
		CompoundNBT nbt = new CompoundNBT();
		this.write(nbt);
		return nbt;
	}

	@Override
	public void handleUpdateTag(
		BlockState state,
		CompoundNBT tag) {
		this.read(state, tag);
	}

 

Link to comment
Share on other sites

why do you use IItemHandler as inventory?
Edit: use ItemStackHandler instead, reason:

you use Capability#getDefaultInstance to get an IItemHandler,
why don't you use a new ItemStackHandler that contains the methods serializeNBT and deserializeNBT
which you can execute in read and write the TE, wich is very simple

Edited by Luis_ST
Link to comment
Share on other sites

55 minutes ago, diesieben07 said:

That is not at all what I said.

Yes, delete getItems, but use the proper alternative, don't just remove read and write.

Ok so I came up with this : https://pastebin.com/L01ZkFj2, however I think I did something wrong since it did not fixed it, for the Direction I put Direction#NORT but I don't know if it is ok to put a fixed direction like this.

 

49 minutes ago, Luis_ST said:

why do you use IItemHandler as inventory?
Edit: use ItemStackHandler instead, reason:

you use Capability#getDefaultInstance to get an IItemHandler,
why don't you use a new ItemStackHandler that contains the methods serializeNBT and deserializeNBT
which you can execute in read and write the TE, wich is very simple

I use the default instance since it creates a new IItemHandler with just one slot and it is to avoid creating more instances and classes

Link to comment
Share on other sites

22 minutes ago, bibouche_ said:

Ok so I came up with this : https://pastebin.com/L01ZkFj2, however I think I did something wrong since it did not fixed it, for the Direction I put Direction#NORT but I don't know if it is ok to put a fixed direction like this.

you can use null

22 minutes ago, bibouche_ said:

I use the default instance since it creates a new IItemHandler with just one slot and it is to avoid creating more instances and classes

Capability#getDefaultInstance do the same as the ItemStackHandler#new
and if you use an ItemStackHandler as I explained above, your error would simply be fixed
this is the simplest solution for your problem:
https://pastebin.com/ntZssi9b

Edit: another issue in your code, you call the write inside the getUpdatePacket method of your TE

Edited by Luis_ST
Link to comment
Share on other sites

19 minutes ago, Luis_ST said:

you can use null

 

Capability#getDefaultInstance do the same as the ItemStackHandler#new
and if you use an ItemStackHandler as I explained above, your error would simply be fixed
this is the simplest solution for your problem:
https://pastebin.com/ntZssi9b

Ok but I tried it and I keep having the same issue, I really don't see what is causing this

Link to comment
Share on other sites

1 hour ago, Luis_ST said:

post your updated code
and take a look at some vanilla TEs,
how they synchronize the TE data with the client

https://pastebin.com/DKKj7JNG

 

Yeah, I looked at some vanilla TEs, however they are using CompoundNBT, so I am a litte lost since IItemHandler uses INBT, and like the read and write methods are using CompoundNBT

Link to comment
Share on other sites

1 minute ago, bibouche_ said:

please post the full TE class

 

1 minute ago, bibouche_ said:

Yeah, I looked at some vanilla TEs, however they are using CompoundNBT, so I am a litte lost since IItemHandler uses INBT, and like the read and write methods are using CompoundNBT

that's basic java, INBT is an interfaces, and CompoundNBT is an implemention of INBT

Link to comment
Share on other sites

1 minute ago, bibouche_ said:

https://pastebin.com/SjjXivkK

Yeah I know that, but I did not found a way yet, so I will look at it, maybe add a new CompoundNBT and use it in the functions

You're still doing what you shouldn't

 

13 hours ago, Luis_ST said:

Capability#getDefaultInstance do the same as the ItemStackHandler#new
and if you use an ItemStackHandler as I explained above, your error would simply be fixed
this is the simplest solution for your problem:
https://pastebin.com/ntZssi9b

this is optional

 

13 hours ago, Luis_ST said:

another issue in your code, you call the write inside the getUpdatePacket method of your TE

that is required

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.