Jump to content

[1.16.5] Help with Capability (Inventory)


Luis_ST

Recommended Posts

4 minutes ago, diesieben07 said:

I already told you, twice. I'll do it a third time:

so like this:

		public Provider(PlayerEntity player) {
			
			EnderChestInventory enderChestInventory = player.getInventoryEnderChest();
			IItemHandlerModifiable enderChestModifiable = (IItemHandlerModifiable) enderChestInventory;
			CombinedInvWrapper invWrapper = new CombinedInvWrapper(inventory, enderChestModifiable);
			
		}

 

 

Link to comment
Share on other sites

5 minutes ago, diesieben07 said:

"Hey, you need to do A".

"Ah so, like this: Z".

12 minutes ago, diesieben07 said:

And then use InvWrapper to wrap the IInventory into an IItemHandlerModifiable.

then like this:

			EnderChestInventory enderChestInventory = player.getInventoryEnderChest();
			IItemHandlerModifiable enderChestModifiable = new InvWrapper(enderChestInventory);
			CombinedInvWrapper invWrapper = new CombinedInvWrapper(enderChestModifiable, inventory);

 

and now i have to creat a LazyOptional from the CombinedInvWrapper and retrun the LazyOptional in getCapability?

Link to comment
Share on other sites

9 minutes ago, loordgek said:

yes

so that should work:

	public static class Provider implements ICapabilityProvider {
		
		private IModItemHandler inventory = new ModItemStackHandler();
		private CombinedInvWrapper invWrapper;
		private LazyOptional<CombinedInvWrapper> optional = LazyOptional.of(() -> invWrapper);
		
		public Provider(PlayerEntity player) {
			
			EnderChestInventory enderChestInventory = player.getInventoryEnderChest();
			IItemHandlerModifiable enderChestModifiable = new InvWrapper(enderChestInventory);
			invWrapper = new CombinedInvWrapper(enderChestModifiable, inventory);
			
		}
		
		@Override
		@SuppressWarnings({ "unchecked" })
		public <T> LazyOptional<T> getCapability(Capability<T> cap, Direction side) {
			
			return cap == CAPABILITY ? (LazyOptional<T>) optional : LazyOptional.empty();
			
		}
		
	}

 

Link to comment
Share on other sites

1 minute ago, loordgek said:

yes but you need to save your inventory,instead of ICapabilityProvider implement ICapabilitySerializable

so like this:

do I have to create a new nbt and save my inventory in it?

if yes how to do that?

 

	public static class Provider implements ICapabilitySerializable<INBT> {
		
		private IModItemHandler inventory = new ModItemStackHandler();
		private CombinedInvWrapper invWrapper;
		private LazyOptional<CombinedInvWrapper> optional = LazyOptional.of(() -> invWrapper);
		
		public Provider(PlayerEntity player) {
			
			EnderChestInventory enderChestInventory = player.getInventoryEnderChest();
			IItemHandlerModifiable enderChestModifiable = new InvWrapper(enderChestInventory);
			invWrapper = new CombinedInvWrapper(enderChestModifiable, inventory);
			
		}
		
		@Override
		@SuppressWarnings({ "unchecked" })
		public <T> LazyOptional<T> getCapability(Capability<T> cap, Direction side) {
			
			return cap == CAPABILITY ? (LazyOptional<T>) optional : LazyOptional.empty();
			
		}

		@Override
		public INBT serializeNBT() {
			
			CompoundNBT nbt = new CompoundNBT();
			return nbt;
			
		}

		@Override
		public void deserializeNBT(INBT nbt) {
			
			
			
		}
		
	}

 

Link to comment
Share on other sites

10 minutes ago, diesieben07 said:

ItemStackHandler has them.

this is now my Capability but is this correct?

public class ModCapability {
	
	@CapabilityInject(IModItemHandler.class)
	public static Capability<IModItemHandler> CAPABILITY = null;
	
	public ModCapability() {
		
		CapabilityManager.INSTANCE.register(IModItemHandler.class, new Storage(), new Factory());
		
	}
	
	private static class Storage implements IStorage<IModItemHandler> {
		@Override
		public INBT writeNBT(Capability<IModItemHandler> capability, IModItemHandler instance, Direction side) {
			return null;
		}
		@Override
		public void readNBT(Capability<IModItemHandler> capability, IModItemHandler instance, Direction side, INBT nbt) {
		}
	}
	
	private static class Factory implements Callable<IModItemHandler> {
		@Override
		public IModItemHandler call() throws Exception {
			return null;
		}
	}
	
	public static class Provider implements ICapabilitySerializable<CompoundNBT> {
		
		private ModItemStackHandler inventory = new ModItemStackHandler();
		private CombinedInvWrapper invWrapper;
		private LazyOptional<CombinedInvWrapper> optional = LazyOptional.of(() -> invWrapper);
		
		public Provider(PlayerEntity player) {
			
			EnderChestInventory enderChestInventory = player.getInventoryEnderChest();
			IItemHandlerModifiable enderChestModifiable = new InvWrapper(enderChestInventory);
			invWrapper = new CombinedInvWrapper(enderChestModifiable, inventory);
			
		}
		
		@Override
		@SuppressWarnings({ "unchecked" })
		public <T> LazyOptional<T> getCapability(Capability<T> cap, Direction side) {
			
			return cap == CAPABILITY ? (LazyOptional<T>) optional : LazyOptional.empty();
			
		}

		@Override
		public CompoundNBT serializeNBT() {
			
			return inventory.serializeNBT();
			
		}

		@Override
		public void deserializeNBT(CompoundNBT nbt) {
			
			inventory.deserializeNBT(nbt);
			
		}
		
	}
	
}

but when i now start the game i got an error:

2:21:05] [Server thread/ERROR] [ne.mi.ev.EventBus/EVENTBUS]: Exception caught during firing event: Cannot invoke "net.minecraft.inventory.IInventory.getSizeInventory()" because the return value of "net.minecraftforge.items.wrapper.InvWrapper.getInv()" is null
	Index: 1
	Listeners:
		0: NORMAL
		1: ASM: class net.luis.cave.events.capability.OnAttachCapabilitiesEvent AttachCapabilities(Lnet/minecraftforge/event/AttachCapabilitiesEvent;)V
java.lang.NullPointerException: Cannot invoke "net.minecraft.inventory.IInventory.getSizeInventory()" because the return value of "net.minecraftforge.items.wrapper.InvWrapper.getInv()" is null
	at net.minecraftforge.items.wrapper.InvWrapper.getSlots(InvWrapper.java:61)
	at net.minecraftforge.items.wrapper.CombinedInvWrapper.<init>(CombinedInvWrapper.java:42)
	at net.luis.cave.init.ModCapability$Provider.<init>(ModCapability.java:61)
	at net.luis.cave.events.capability.OnAttachCapabilitiesEvent.AttachCapabilities(OnAttachCapabilitiesEvent.java:23)
	at net.minecraftforge.eventbus.ASMEventHandler_16_OnAttachCapabilitiesEvent_AttachCapabilities_AttachCapabilitiesEvent.invoke(.dynamic)
	at net.minecraftforge.eventbus.ASMEventHandler.invoke(ASMEventHandler.java:85)
	at net.minecraftforge.eventbus.EventBus.post(EventBus.java:302)
	at net.minecraftforge.eventbus.EventBus.post(EventBus.java:283)
	at net.minecraftforge.event.ForgeEventFactory.gatherCapabilities(ForgeEventFactory.java:579)
	at net.minecraftforge.event.ForgeEventFactory.gatherCapabilities(ForgeEventFactory.java:573)
	at net.minecraftforge.common.capabilities.CapabilityProvider.gatherCapabilities(CapabilityProvider.java:48)
	at net.minecraftforge.common.capabilities.CapabilityProvider.gatherCapabilities(CapabilityProvider.java:44)
	at net.minecraft.entity.Entity.<init>(Entity.java:221)
	at net.minecraft.entity.LivingEntity.<init>(LivingEntity.java:207)
	at net.minecraft.entity.player.PlayerEntity.<init>(PlayerEntity.java:160)
	at net.minecraft.entity.player.ServerPlayerEntity.<init>(ServerPlayerEntity.java:182)
	at net.minecraft.server.management.PlayerList.createPlayerForUser(PlayerList.java:419)
	at net.minecraft.network.login.ServerLoginNetHandler.tryAcceptPlayer(ServerLoginNetHandler.java:122)
	at net.minecraft.network.login.ServerLoginNetHandler.tick(ServerLoginNetHandler.java:66)
	at net.minecraft.network.NetworkManager.tick(NetworkManager.java:244)
	at net.minecraft.network.NetworkSystem.tick(NetworkSystem.java:151)
	at net.minecraft.server.MinecraftServer.updateTimeLightAndEntities(MinecraftServer.java:898)
	at net.minecraft.server.MinecraftServer.tick(MinecraftServer.java:820)
	at net.minecraft.server.integrated.IntegratedServer.tick(IntegratedServer.java:84)
	at net.minecraft.server.MinecraftServer.func_240802_v_(MinecraftServer.java:663)
	at net.minecraft.server.MinecraftServer.lambda$startServer$0(MinecraftServer.java:233)
	at java.base/java.lang.Thread.run(Thread.java:832)

[12:21:05] [Server thread/FATAL] [ne.mi.co.ForgeMod/]: Preparing crash report with UUID 5f9c0c16-6096-44b4-a0a3-48d79100f726
[12:21:05] [Server thread/ERROR] [minecraft/MinecraftServer]: Encountered an unexpected exception
net.minecraft.crash.ReportedException: Ticking memory connection
	at net.minecraft.network.NetworkSystem.tick(NetworkSystem.java:154) ~[forge:?] {re:classloading}
	at net.minecraft.server.MinecraftServer.updateTimeLightAndEntities(MinecraftServer.java:898) ~[forge:?] {re:classloading,pl:accesstransformer:B}
	at net.minecraft.server.MinecraftServer.tick(MinecraftServer.java:820) ~[forge:?] {re:classloading,pl:accesstransformer:B}
	at net.minecraft.server.integrated.IntegratedServer.tick(IntegratedServer.java:84) ~[forge:?] {re:classloading,pl:runtimedistcleaner:A}
	at net.minecraft.server.MinecraftServer.func_240802_v_(MinecraftServer.java:663) ~[forge:?] {re:classloading,pl:accesstransformer:B}
	at net.minecraft.server.MinecraftServer.lambda$startServer$0(MinecraftServer.java:233) ~[forge:?] {re:classloading,pl:accesstransformer:B}
	at java.lang.Thread.run(Thread.java:832) [?:?] {}
Caused by: java.lang.NullPointerException: Cannot invoke "net.minecraft.inventory.IInventory.getSizeInventory()" because the return value of "net.minecraftforge.items.wrapper.InvWrapper.getInv()" is null
	at net.minecraftforge.items.wrapper.InvWrapper.getSlots(InvWrapper.java:61) ~[forge-1.16.5-36.0.1_mapped_snapshot_20201028-1.16.3-recomp.jar:?] {re:classloading}
	at net.minecraftforge.items.wrapper.CombinedInvWrapper.<init>(CombinedInvWrapper.java:42) ~[forge-1.16.5-36.0.1_mapped_snapshot_20201028-1.16.3-recomp.jar:?] {re:classloading}
	at net.luis.cave.init.ModCapability$Provider.<init>(ModCapability.java:61) ~[main/:?] {re:classloading}
	at net.luis.cave.events.capability.OnAttachCapabilitiesEvent.AttachCapabilities(OnAttachCapabilitiesEvent.java:23) ~[main/:?] {re:classloading}
	at net.minecraftforge.eventbus.ASMEventHandler_16_OnAttachCapabilitiesEvent_AttachCapabilities_AttachCapabilitiesEvent.invoke(.dynamic) ~[?:?] {}
	at net.minecraftforge.eventbus.ASMEventHandler.invoke(ASMEventHandler.java:85) ~[eventbus-4.0.0.jar:?] {}
	at net.minecraftforge.eventbus.EventBus.post(EventBus.java:302) ~[eventbus-4.0.0.jar:?] {}
	at net.minecraftforge.eventbus.EventBus.post(EventBus.java:283) ~[eventbus-4.0.0.jar:?] {}
	at net.minecraftforge.event.ForgeEventFactory.gatherCapabilities(ForgeEventFactory.java:579) ~[forge:?] {re:classloading}
	at net.minecraftforge.event.ForgeEventFactory.gatherCapabilities(ForgeEventFactory.java:573) ~[forge:?] {re:classloading}
	at net.minecraftforge.common.capabilities.CapabilityProvider.gatherCapabilities(CapabilityProvider.java:48) ~[forge:?] {re:classloading}
	at net.minecraftforge.common.capabilities.CapabilityProvider.gatherCapabilities(CapabilityProvider.java:44) ~[forge:?] {re:classloading}
	at net.minecraft.entity.Entity.<init>(Entity.java:221) ~[forge:?] {re:classloading,pl:accesstransformer:B}
	at net.minecraft.entity.LivingEntity.<init>(LivingEntity.java:207) ~[forge:?] {re:classloading}
	at net.minecraft.entity.player.PlayerEntity.<init>(PlayerEntity.java:160) ~[forge:?] {re:classloading,pl:accesstransformer:B}
	at net.minecraft.entity.player.ServerPlayerEntity.<init>(ServerPlayerEntity.java:182) ~[forge:?] {re:classloading,pl:accesstransformer:B}
	at net.minecraft.server.management.PlayerList.createPlayerForUser(PlayerList.java:419) ~[forge:?] {re:classloading}
	at net.minecraft.network.login.ServerLoginNetHandler.tryAcceptPlayer(ServerLoginNetHandler.java:122) ~[forge:?] {re:classloading}
	at net.minecraft.network.login.ServerLoginNetHandler.tick(ServerLoginNetHandler.java:66) ~[forge:?] {re:classloading}
	at net.minecraft.network.NetworkManager.tick(NetworkManager.java:244) ~[forge:?] {re:classloading}
	at net.minecraft.network.NetworkSystem.tick(NetworkSystem.java:151) ~[forge:?] {re:classloading}
	... 6 more


 

Link to comment
Share on other sites

1 hour ago, diesieben07 said:

So you need to lazily initialize your inventory stuff, because getEnderChestInventory will just return null when called that early.

so i have to create a temporary inventory or is there a way to initialize the inventory directly in the event?

Or is there another way to add the capability to the player?

 

Link to comment
Share on other sites

1 minute ago, diesieben07 said:

If not, initialize them first, then proceed as normal.

and how to do that?

In addition, the error is not in getCapability but in the constructor because I call the constructor with the AttachCapabilitiesEvent and not getCapability

		public Provider(PlayerEntity player) {
			
			EnderChestInventory enderChestInventory = player.getInventoryEnderChest();
			IItemHandlerModifiable enderChestModifiable = new InvWrapper(enderChestInventory);
			
	// ->	invWrapper = new CombinedInvWrapper(enderChestModifiable, inventory);
			
		}

 

Link to comment
Share on other sites

18 minutes ago, diesieben07 said:

Like I told you you need to lazily initialize the fields in getCapability, because the constructor is too early to call getInventoryEnderChest.

I've understood that now, but which class / method do you mean exactly because I can't find a class with the name lazily initialize

Link to comment
Share on other sites

6 minutes ago, diesieben07 said:

Lazy initialization is a concept. In this case it means checking if the fields are initialized in getCapability (lazily doing it only when it is actually necessary) instead of eagerly doing it immediately in the constructor

So I have to check getCapability whether the fields == are null, if they are the fields to initiate (call the same code that is in the constuctor)?

so like this:

		@Override
		@SuppressWarnings({ "unchecked" })
		public <T> LazyOptional<T> getCapability(Capability<T> cap, Direction side) {
			
			if (optional == null || inventory == null || invWrapper == null) {
				
				EnderChestInventory enderChestInventory = player.getInventoryEnderChest();
				IItemHandlerModifiable enderChestModifiable = new InvWrapper(enderChestInventory);
				invWrapper = new CombinedInvWrapper(enderChestModifiable, inventory);
				optional = LazyOptional.of(() -> invWrapper);
			}
			
			return cap == CAPABILITY && cap != null ? (LazyOptional<T>) optional : LazyOptional.empty();
			
		}

 

Edited by Luis_ST
Link to comment
Share on other sites

4 minutes ago, diesieben07 said:

Yes, the constructor would only initialize your ItemStackHandler.

Then the other stuff goes in getCapability, but only do it once using the null checks.

okay now this is my code:

package net.luis.cave.init;

import java.util.concurrent.Callable;

import net.luis.cave.api.capability.IModItemHandler;
import net.luis.cave.api.capability.ModItemStackHandler;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.inventory.EnderChestInventory;
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.nbt.INBT;
import net.minecraft.util.Direction;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.capabilities.Capability.IStorage;
import net.minecraftforge.common.capabilities.CapabilityInject;
import net.minecraftforge.common.capabilities.CapabilityManager;
import net.minecraftforge.common.capabilities.ICapabilitySerializable;
import net.minecraftforge.common.util.LazyOptional;
import net.minecraftforge.items.IItemHandlerModifiable;
import net.minecraftforge.items.wrapper.CombinedInvWrapper;
import net.minecraftforge.items.wrapper.InvWrapper;

public class ModCapability {
	
	@CapabilityInject(IModItemHandler.class)
	public static Capability<IModItemHandler> CAPABILITY = null;
	
	public ModCapability() {
		
		CapabilityManager.INSTANCE.register(IModItemHandler.class, new Storage(), new Factory());
		
	}
	
	private static class Storage implements IStorage<IModItemHandler> {
		@Override
		public INBT writeNBT(Capability<IModItemHandler> capability, IModItemHandler instance, Direction side) {
			return null;
		}
		@Override
		public void readNBT(Capability<IModItemHandler> capability, IModItemHandler instance, Direction side, INBT nbt) {
		}
	}
	
	private static class Factory implements Callable<IModItemHandler> {
		@Override
		public IModItemHandler call() throws Exception {
			return null;
		}
	}
	
	public static class Provider implements ICapabilitySerializable<CompoundNBT> {
		
		private ModItemStackHandler inventory = new ModItemStackHandler();
		private CombinedInvWrapper invWrapper;
		private LazyOptional<CombinedInvWrapper> optional = LazyOptional.of(() -> invWrapper);
		private PlayerEntity player;
		
		public Provider(PlayerEntity playerIn) {
			
			this.player = playerIn;
			EnderChestInventory enderChestInventory = player.getInventoryEnderChest();
			IItemHandlerModifiable enderChestModifiable = new InvWrapper(enderChestInventory);
			invWrapper = new CombinedInvWrapper(enderChestModifiable, inventory);
			
		}
		
		@Override
		@SuppressWarnings({ "unchecked" })
		public <T> LazyOptional<T> getCapability(Capability<T> cap, Direction side) {
			
			if (optional == null) {
				
				EnderChestInventory enderChestInventory = player.getInventoryEnderChest();
				IItemHandlerModifiable enderChestModifiable = new InvWrapper(enderChestInventory);
				invWrapper = new CombinedInvWrapper(enderChestModifiable, inventory);
				optional = LazyOptional.of(() -> invWrapper);
			}
			
			return cap == CAPABILITY && cap != null ? (LazyOptional<T>) optional : LazyOptional.empty();
			
		}

		@Override
		public CompoundNBT serializeNBT() {
			
			return inventory.serializeNBT();
			
		}

		@Override
		public void deserializeNBT(CompoundNBT nbt) {
			
			inventory.deserializeNBT(nbt);
			
		}
		
	}
	
}

 but i got the same error again

Link to comment
Share on other sites

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

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

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

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

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.