Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

[1.16.5] Help with Capability (Inventory)


Luis_ST
 Share

Recommended Posts

14 hours ago, diesieben07 said:

Do you listen to what I say? Do you know basic Java?

I forgot to take it from the constructor and didn't look at the code anymore🤦‍♂️

what do I have to change because the enderchest's normal inventory is currently being opened?

this is the event i use:

@Mod.EventBusSubscriber(modid=Cave.Mod_Id, bus = Mod.EventBusSubscriber.Bus.FORGE)
public class OnEnderchest {
	
	@SubscribeEvent
	public static void PlayerInteract(PlayerInteractEvent.RightClickBlock event) {
		
		PlayerEntity player = event.getPlayer();
		BlockPos pos = event.getPos();
		World world = event.getWorld();
		BlockState state = world.getBlockState(pos);
		
		if (state.getBlock() == Blocks.ENDER_CHEST) {
			
			if (player instanceof ServerPlayerEntity) {
				
				event.setCanceled(true);
				ServerPlayerEntity serverPlayer = (ServerPlayerEntity) player;
				NetworkHooks.openGui(serverPlayer, new SimpleNamedContainerProvider((id, inventory, playerIn) -> {
					return new ModEnderChestContainer(id, inventory);
				}, ModEnderChestContainer.getContainerName()), pos);
				
			}
			
		}

	}

}

and the gui has no slots and looks like this:

 

2021-02-17_17_27_32.png.09c593e6e4ec692c44a9d017504d30e5.png

Edited by Luis_ST
add event code
Link to comment
Share on other sites

14 hours ago, Luis_ST said:

what do I have to change because the enderchest's normal inventory is currently being opened?

I fixed it. the problem with the slots remains my gui has no slots.

and the screen is rendering weird but this is akulie the problem of the screenclass.

but what must i change that the gui has the slots

Link to comment
Share on other sites

46 minutes ago, loordgek said:

show your container class

the error must be in the container because I have already checked everything else.

i think i know where in the container i have to use the capability in the conatiner but i don't know how exactly.

I know that I have to use the PlayerEntity#getCapability method, but what do I have to do with it?

 

public class ModEnderChestContainer extends Container implements INamedContainerProvider {
	
	private static final ITextComponent CONTAINER_NAME = new TranslationTextComponent("container.enderchest");
	
	public ModEnderChestContainer(int id, PlayerInventory playerInventory, PacketBuffer buffer) {
		
		super(ModContainerType.ENDER_CHEST.get(), id);
		
	}
	
	public ModEnderChestContainer(int id, PlayerInventory playerInventory) {
		
		super(ModContainerType.ENDER_CHEST.get(), id);
		
	}

	@Override
	public Container createMenu(int id, PlayerInventory playerInventoryIn, PlayerEntity player) {
		
		return ChestContainer.createGeneric9X3(id, playerInventoryIn);
		
	}

	@Override
	public ITextComponent getDisplayName() {
		
		return CONTAINER_NAME;
		
	}

	@Override
	public boolean canInteractWith(PlayerEntity playerIn) {
		
		return true;
		
	}

	public static ITextComponent getContainerName() {
		
		return CONTAINER_NAME;
		
	}

}

 

Link to comment
Share on other sites

  • Implementing INamedContainerProvider on your container makes no sense.
  • You return a ChestContainer in createMenu, but your container class is never used as an INamedContainerProvider, so this method will never be called.
  • Your container does not add any slots. I wonder why it doesn't have any slots...
Link to comment
Share on other sites

27 minutes ago, diesieben07 said:
  • You return a ChestContainer in createMenu, but your container class is never used as an INamedContainerProvider, so this method will never be called.
  • Your container does not add any slots. I wonder why it doesn't have any slots...

can i create a container like a chest?

since the constructor of the chest contains an IInventory but I don't have one (like a chest has the tileentity) or do I need my capability there

Edited by Luis_ST
Link to comment
Share on other sites

10 minutes ago, diesieben07 said:

Sure, just add the slots in the same fashion.

this is now my container class:

Do I need the IInventory in the second constructor? If so, where do I get the inventory from when I open a container (from my Capability). if not what do I have to use instead

public class ModEnderChestContainer extends Container {
	
	public ModEnderChestContainer(int id, PlayerInventory playerInventoryIn, PacketBuffer extraData) {
		
		super(ModContainerType.ENDER_CHEST.get(), id);
		
	}

	public ModEnderChestContainer(int id, PlayerInventory playerInventoryIn, IInventory inventory) {
		
		super(ModContainerType.ENDER_CHEST.get(), id);
		assertInventorySize(inventory, 6 * 9);
		int i = (6 - 4) * 18;

		for (int j = 0; j < 6; ++j) {
			for (int k = 0; k < 9; ++k) {
				this.addSlot(new Slot(inventory, k + j * 9, 8 + k * 18, 18 + j * 18));
			}
		}

		for (int l = 0; l < 3; ++l) {
			for (int j1 = 0; j1 < 9; ++j1) {
				this.addSlot(new Slot(playerInventoryIn, j1 + l * 9 + 9, 8 + j1 * 18, 103 + l * 18 + i));
			}
		}

		for (int i1 = 0; i1 < 9; ++i1) {
			this.addSlot(new Slot(playerInventoryIn, i1, 8 + i1 * 18, 161 + i));
		}

	}

	public ItemStack transferStackInSlot(PlayerEntity playerIn, int index) {
		
		ItemStack itemstack = ItemStack.EMPTY;
		Slot slot = this.inventorySlots.get(index);
		
		if (slot != null && slot.getHasStack()) {
			
			ItemStack itemstack1 = slot.getStack();
			itemstack = itemstack1.copy();
			
			if (index < 6 * 9) {
				
				if (!this.mergeItemStack(itemstack1, 6 * 9, this.inventorySlots.size(), true)) {
					
					return ItemStack.EMPTY;
					
				}
				
			} else if (!this.mergeItemStack(itemstack1, 0, 6 * 9, false)) {
				
				return ItemStack.EMPTY;
				
			}

			if (itemstack1.isEmpty()) {
				
				slot.putStack(ItemStack.EMPTY);
				
			} else {
				
				slot.onSlotChanged();
				
			}
			
		}

		return itemstack;
		
	}

	@Override
	public boolean canInteractWith(PlayerEntity playerIn) {
		
		return true;
		
	}

}

 

Edited by Luis_ST
Link to comment
Share on other sites

On the server you create the container directly in the INamedContainerProvider, so just pass in your IItemHandlerModifiable.

On the client the container will be created by your IContainerFactory, which needs to do the same thing and pass in your IItemHandlerModifiable.

Link to comment
Share on other sites

I have to open the container twice, once on the client and on the server?

and if so I have to check with world.isRemote (client) and !world.isRemote (server)

 

1 hour ago, diesieben07 said:

On the server you create the container directly in the INamedContainerProvider, so just pass in your IItemHandlerModifiable.

and my IItemHandlerModifiable I get from the player with getCapability?

 

1 hour ago, diesieben07 said:

On the client the container will be created by your IContainerFactory, which needs to do the same thing and pass in your IItemHandlerModifiable.

Whats the best way to do this?

 

 

 

 

Edited by Luis_ST
Link to comment
Share on other sites

1 hour ago, diesieben07 said:

On the server you create the container directly in the INamedContainerProvider, so just pass in your IItemHandlerModifiable.

On the client the container will be created by your IContainerFactory, which needs to do the same thing and pass in your IItemHandlerModifiable

that is what I have already created, what do I have to change here so that it works:

is that server or client side? and how created i the other side?

				ServerPlayerEntity serverPlayer = (ServerPlayerEntity) player;
				IItemHandlerModifiable itemHandlerModifiable = (IModItemHandler) serverPlayer.getCapability(ModCapability.CAPABILITY);
				NetworkHooks.openGui(serverPlayer, new SimpleNamedContainerProvider((id, inventory, playerIn) -> {
					return new ModEnderChestContainer(id, inventory, itemHandlerModifiable);
				}, CONTAINER_NAME), pos);

 

Link to comment
Share on other sites

1 hour ago, Luis_ST said:

I have to open the container twice, once on the client and on the server?

No. You open it on the server using NetworkHooks.openGui. The opening on the client is taken care of by Forge, you just need to have a proper IContainerFactory, so that Forge can create the container on the client.

 

1 hour ago, Luis_ST said:

and my IItemHandlerModifiable I get from the player with getCapability?

Yes.

 

4 minutes ago, Luis_ST said:

that is what I have already created, what do I have to change here so that it works:

is that server or client side? and how created i the other side?


				ServerPlayerEntity serverPlayer = (ServerPlayerEntity) player;
				IItemHandlerModifiable itemHandlerModifiable = (IModItemHandler) serverPlayer.getCapability(ModCapability.CAPABILITY);
				NetworkHooks.openGui(serverPlayer, new SimpleNamedContainerProvider((id, inventory, playerIn) -> {
					return new ModEnderChestContainer(id, inventory, itemHandlerModifiable);
				}, CONTAINER_NAME), pos);

 

This is server side (at least the code will crash on the client). The client will call the IContainerFactory, which needs to get the capability from the player and return the container.

Link to comment
Share on other sites

30 minutes ago, diesieben07 said:

This is server side (at least the code will crash on the client). The client will call the IContainerFactory, which needs to get the capability from the player and return the container.

if i understand correctly i have to call the OContainerFactory in my code

37 minutes ago, Luis_ST said:

what do I have to change here so that it works:

? beacuse i got an error: "The constructor ModEnderChestContainer(int, PlayerInventory, IItemHandlerModifiable) is undefined"

do i have to set a cast?

Link to comment
Share on other sites

21 minutes ago, Luis_ST said:

if i understand correctly i have to call the OContainerFactory in my code

No, you just have to register it.

 

21 minutes ago, Luis_ST said:

? beacuse i got an error: "The constructor ModEnderChestContainer(int, PlayerInventory, IItemHandlerModifiable) is undefined"

do i have to set a cast?

Jesus lord have mercy.

IT IS YOUR DAMN CLASS. Make the constructor however you need it. IT IS YOUR CODE.

Link to comment
Share on other sites

1 hour ago, Luis_ST said:

IItemHandlerModifiable itemHandlerModifiable = (IModItemHandler) serverPlayer.getCapability(ModCapability.CAPABILITY);

1 does getCapability return a IModItemHandler ?

2 does IModItemHandler extend IItemHandlerModifiable ?

Link to comment
Share on other sites

1 hour ago, diesieben07 said:

IT IS YOUR DAMN CLASS. Make the constructor however you need it. IT IS YOUR CODE.

i already have a constructor.

but what do I have to do with IItemHandlerModifiable,

how do I get an inventory from it beacuse when i want to add the solts i need an IInventory?

 

1 hour ago, loordgek said:

1 does getCapability return a IModItemHandler ?

nop but it return a LazyOptional which holds the IModItemHandler

Link to comment
Share on other sites

1 hour ago, Luis_ST said:

nop but it return a LazyOptional which holds the IModItemHandler

why do you do

 

4 hours ago, Luis_ST said:

IItemHandlerModifiable itemHandlerModifiable = (IModItemHandler) serverPlayer.getCapability(ModCapability.CAPABILITY);

then?

Link to comment
Share on other sites

19 minutes ago, diesieben07 said:

That the quoted code makes no sense.

I found the error myself

 

19 minutes ago, diesieben07 said:

It's a parameter to your ContainerType.

Sorry, I don't understand what you mean by parameters exactly, do I need to add something to my containertype?

this is my containerType:

	public static final DeferredRegister<ContainerType<?>> CONTAINERS = DeferredRegister.create(ForgeRegistries.CONTAINERS, Cave.Mod_Id);
	
	public static final RegistryObject<ContainerType<ModEnderChestContainer>> ENDER_CHEST = CONTAINERS.register("mod_ender_chest", 
			() -> IForgeContainerType.create(ModEnderChestContainer::new));

 

Edited by Luis_ST
Link to comment
Share on other sites

10 hours ago, diesieben07 said:

Great. Now look at what you are passing in there as parameters...

i just looked at the register class of ContainerType (IForgeContainerType) and this class only needs a single parameter, the IContainerFactory

therefore I don't understand what i should add as a parameter

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

 Share




×
×
  • Create New...

Important Information

By using this site, you agree to our Privacy Policy.