Jump to content

[1.16.4] Displaying Contents of a double chest


Titmar

Recommended Posts

I am currently trying to display the contents of a double chest in the container of a different block. For single chests and barrels it works perfectly fine. The problem is, when adding Slots to the container it uses the IInventory vanilla implementation. So when looping over the chest contents with the ItemHandler capability i get thrown indexoutofbound exceptions since vanilla handles double chests quite weirdly. The capability shows me the full 54 size but vanilla thinks its only 27. Anyways, I tried getting around that by switching the IInventory of the newly generated Slot to the second part of the chest but now im getting an unlocated IndexOutOfBoundsException. I cant trace it back to where the issue is exactly. Maybe Im just overcomplicating things and there is a easier way.

Anyway here the code from the container class:

Spoiler

public void setActiveInventory(BlockPos pos) {
		TileEntity t = this.te.getWorld().getTileEntity(pos);
		if (t == null || !(t instanceof LockableLootTileEntity) || pos.equals(this.activeContainer)) {
			return;
		}
		BlockPos secondChest = VanillaChestHelper.isGetDoubleChest(this.te.getWorld(), pos);
		LockableLootTileEntity tile = (LockableLootTileEntity) this.te.getWorld().getTileEntity(pos);
		IItemHandler handler = tile.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY).orElse(null);
		if (handler != null) {
			this.inventorySlots.clear();
			int startX = 108;
			int startY = 34;
			int row = 0;
			int col = 0;
			if (secondChest == null) {
				for (int i = 0; i < 27; i++) {
					this.addSlot(new LockedSlot(tile, i, startX + (col * 18), startY + (row * 18)));
					col++;
					if (col == 9) {
						col = 1;
						row++;
					}
				}
			} else {
				for (int i = 0; i < 27; i++) {
					this.addSlot(new LockedSlot(tile, i, startX + (col * 18), startY + (row * 18)));
					col++;
					if (col == 9) {
						col = 1;
						row++;
					}
				}
				tile = (LockableLootTileEntity) this.te.getWorld().getTileEntity(secondChest);
				handler = tile.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY).orElse(null);
				if (handler != null) {
					for (int i = 28; i < 54; i++) {
						this.addSlot(new LockedSlot(tile, i - 27, startX + (col * 18), startY + (row * 18)));
						col++;
						if (col == 9) {
							col = 1;
							row++;
						}
					}
				}
			}
			this.activeContainer = pos;
		}
	}

 

LockedSlot is just a extension of Slot where you cant put and take items from.

 

And the error log:

Spoiler

[Render thread/FATAL] [minecraft/ThreadTaskExecutor]: Error executing task on Client
java.lang.IndexOutOfBoundsException: Index: 52, Size: 27
	at java.util.ArrayList.rangeCheck(Unknown Source) ~[?:1.8.0_281] {}
	at java.util.ArrayList.get(Unknown Source) ~[?:1.8.0_281] {}
	at net.minecraft.inventory.container.Container.getSlot(Container.java:165) ~[forge-1.16.4-35.0.2_mapped_snapshot_20201028-1.16.3-recomp.jar:?] {re:classloading}
	at net.minecraft.inventory.container.Container.putStackInSlot(Container.java:480) ~[forge-1.16.4-35.0.2_mapped_snapshot_20201028-1.16.3-recomp.jar:?] {re:classloading}
	at net.minecraft.client.network.play.ClientPlayNetHandler.handleSetSlot(ClientPlayNetHandler.java:1189) ~[forge-1.16.4-35.0.2_mapped_snapshot_20201028-1.16.3-recomp.jar:?] {re:classloading,pl:runtimedistcleaner:A}
	at net.minecraft.network.play.server.SSetSlotPacket.processPacket(SSetSlotPacket.java:29) ~[forge-1.16.4-35.0.2_mapped_snapshot_20201028-1.16.3-recomp.jar:?] {re:classloading}
	at net.minecraft.network.play.server.SSetSlotPacket.processPacket(SSetSlotPacket.java:11) ~[forge-1.16.4-35.0.2_mapped_snapshot_20201028-1.16.3-recomp.jar:?] {re:classloading}
	at net.minecraft.network.PacketThreadUtil.lambda$checkThreadAndEnqueue$0(PacketThreadUtil.java:19) ~[forge-1.16.4-35.0.2_mapped_snapshot_20201028-1.16.3-recomp.jar:?] {re:classloading}
	at net.minecraft.util.concurrent.ThreadTaskExecutor.run(ThreadTaskExecutor.java:139) ~[forge-1.16.4-35.0.2_mapped_snapshot_20201028-1.16.3-recomp.jar:?] {re:classloading,pl:accesstransformer:B}
	at net.minecraft.util.concurrent.RecursiveEventLoop.run(RecursiveEventLoop.java:22) ~[forge-1.16.4-35.0.2_mapped_snapshot_20201028-1.16.3-recomp.jar:?] {re:classloading}
	at net.minecraft.util.concurrent.ThreadTaskExecutor.driveOne(ThreadTaskExecutor.java:109) ~[forge-1.16.4-35.0.2_mapped_snapshot_20201028-1.16.3-recomp.jar:?] {re:classloading,pl:accesstransformer:B}
	at net.minecraft.util.concurrent.ThreadTaskExecutor.drainTasks(ThreadTaskExecutor.java:97) ~[forge-1.16.4-35.0.2_mapped_snapshot_20201028-1.16.3-recomp.jar:?] {re:classloading,pl:accesstransformer:B}
	at net.minecraft.client.Minecraft.runGameLoop(Minecraft.java:973) ~[forge-1.16.4-35.0.2_mapped_snapshot_20201028-1.16.3-recomp.jar:?] {re:classloading,pl:accesstransformer:B,pl:runtimedistcleaner:A}
	at net.minecraft.client.Minecraft.run(Minecraft.java:612) ~[forge-1.16.4-35.0.2_mapped_snapshot_20201028-1.16.3-recomp.jar:?] {re:classloading,pl:accesstransformer:B,pl:runtimedistcleaner:A}
	at net.minecraft.client.main.Main.main(Main.java:184) ~[forge-1.16.4-35.0.2_mapped_snapshot_20201028-1.16.3-recomp.jar:?] {re:classloading,pl:runtimedistcleaner:A}
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_281] {}
	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_281] {}
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_281] {}
	at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_281] {}
	at net.minecraftforge.userdev.FMLUserdevClientLaunchProvider.lambda$launchService$0(FMLUserdevClientLaunchProvider.java:52) ~[forge-1.16.4-35.0.2_mapped_snapshot_20201028-1.16.3-recomp.jar:?] {}
	at cpw.mods.modlauncher.LaunchServiceHandlerDecorator.launch(LaunchServiceHandlerDecorator.java:37) [modlauncher-8.0.6.jar:?] {}
	at cpw.mods.modlauncher.LaunchServiceHandler.launch(LaunchServiceHandler.java:54) [modlauncher-8.0.6.jar:?] {}
	at cpw.mods.modlauncher.LaunchServiceHandler.launch(LaunchServiceHandler.java:72) [modlauncher-8.0.6.jar:?] {}
	at cpw.mods.modlauncher.Launcher.run(Launcher.java:82) [modlauncher-8.0.6.jar:?] {}
	at cpw.mods.modlauncher.Launcher.main(Launcher.java:66) [modlauncher-8.0.6.jar:?] {}
	at net.minecraftforge.userdev.LaunchTesting.main(LaunchTesting.java:105) [forge-1.16.4-35.0.2_mapped_snapshot_20201028-1.16.3-recomp.jar:?] {}

 

 

Link to comment
Share on other sites

Yeah but I need to give the slot an object of IInventory (in this case the chestTE) which for some reason only sees the single inventory. I could override the slot class to use IItemHandler instead of IInventory but that seems a bit overkill. Or is there a way to get the slot to use the IItemHandler that I dont see?

Link to comment
Share on other sites

Okay, I am at a loss - once again. Somehow resetting the container.inventorySlots field does not work as intended. When the MarketContainer is created it is starting out with the first inventory in the list and displays that. If that is a single chest and i switch to a double chest and back i get thrown the ioob exception. If the first inventory is a double chest and I switch to a single chest, it keeps showing the second part of the double chest but when i click one of those slots i still get the ioob exception.

I uploaded it to my github, because I cant even get close to where the issue is...

https://github.com/titmar/Caravans/blob/main/src/main/java/io/github/titmar/caravans/common/container/MarketContainer.java

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.