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

There are the following container slots, and items can be displayed in the slots by changing the backpack.

However, if you try to move an item in the backpack slot, you will not be able to pick it up, and if you move it from the inventory to the backpack slot, the item will disappear.

I looked through the code and couldn't figure out the cause of this bug. How can I get it to work properly?

 

Container class:

Spoiler

public class BelongingsUMUPlayerContainer extends AbstractUMUPlayerContainer {

	private static final UMUEquipmentSlotType[] VALID_EQUIPMENT_SLOTS = new UMUEquipmentSlotType[]{
			UMUEquipmentSlotType.HEAD, UMUEquipmentSlotType.CHEST, UMUEquipmentSlotType.LEGS, UMUEquipmentSlotType.FEET
	};
	private static final UMUEquipmentSlotType[] VALID_RING_SLOTS = new UMUEquipmentSlotType[]{
			UMUEquipmentSlotType.MAINRING1, UMUEquipmentSlotType.MAINRING2, UMUEquipmentSlotType.OFFRING1, UMUEquipmentSlotType.OFFRING2
	};
	private static final UMUEquipmentSlotType[] VALID_MISCELLANEOUS_SLOTS = new UMUEquipmentSlotType[]{
			UMUEquipmentSlotType.ENCYCLOPEDIA, UMUEquipmentSlotType.MAP
	};

	public BelongingsUMUPlayerContainer(int id, PlayerInventory playerInventory, PacketBuffer extraData) {
		this(UMUContainers.BELONGING_INVENTORY.get(), id, playerInventory, playerInventory.player);
	}

	public BelongingsUMUPlayerContainer(ContainerType<?> type, int id, PlayerInventory playerInventory, PlayerEntity player) {
		super(type, id, playerInventory, player);

		// index : 36 - 39
		for (int i = 0; i < 4; i++) {
			this.addSlot(new EquipmentSlot(playerInventory, 39 - i, 186, 18 + i * 18, VALID_EQUIPMENT_SLOTS[i]));
		}
		this.addSlot(new OffhandSlot(playerInventory, 40, 255, 72));
		this.addSlot(new EquipmentSlot(playerInventory, 41, 293, 27, UMUEquipmentSlotType.NECKLACE));
		// index : 42 - 45
		for (int iU = 0; iU < 2; iU++) {
			for (int iV = 0; iV < 2; iV++) {
				this.addSlot(new EquipmentSlot(playerInventory, 42 + iU * 2 + iV, 282 + iU * 18 + 4, 48 + iV * 18, VALID_RING_SLOTS[iU * 2 + iV]));
			}
		}
		for (int i = 0; i < 2; i++) {
			this.addSlot(new EquipmentSlot(playerInventory, 46 + i, 330, 72 + i * 18, VALID_MISCELLANEOUS_SLOTS[i]));
		}

		for (int iV = 0; iV < 4; iV++) {
			for (int iU = 0; iU < 9; iU++) {
				Slot slot = this.addSlot(new BackpackSlot(
						new ItemStackHandler(36), new ItemStackHandler(36), iU + iV * 9, 8 + iU * 18, 18 + iV * 18, player, iV + 1)
				);
				if (slot instanceof AbstractBelongingsSlot) {
					AbstractBelongingsSlot belongingsSlot = (AbstractBelongingsSlot) slot;
					belongingsSlot.updateInventory(38, this.player.getItemStackFromSlot(EquipmentSlotType.CHEST));
					this.addListener(belongingsSlot);
				}
			}
		}
	}
}

 


public abstract class AbstractUMUPlayerContainer extends Container {
	protected final PlayerEntity player;

	protected AbstractUMUPlayerContainer(ContainerType<?> type, int id, PlayerInventory playerInventory, PlayerEntity player) {
		super(type, id);

		this.player = player;

		//index : 9 - 35
		for(int iV = 0; iV < 3; iV++) {
			for(int iU = 0; iU < 9; iU++) {
				this.addSlot(new Slot(playerInventory, iU + (iV + 1) * 9, 186 + iU * 18, 104 + iV * 18));
			}
		}
		for(int i = 0; i < 9; i++) {
			this.addSlot(new Slot(playerInventory, i, 186 + i * 18, 162));
		}
	}

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

	@Override
	@SuppressWarnings({"ConstantConditions", "NullableProblems"})
	public final ItemStack transferStackInSlot(PlayerEntity playerIn, int index) {
		Slot slot = this.inventorySlots.get(index);
		ItemStack returnStack = ItemStack.EMPTY;

		if (slot == null || !slot.getHasStack()) return returnStack;

		ItemStack currentStack = slot.getStack();
		returnStack = currentStack.copy();

		if (index >= 0 && index < 27) {
			if (!this.mergeItemStack(currentStack, 27, 36, false)) {
				return ItemStack.EMPTY;
			}
		} else if (index >= 27 && index < 36) {
			if (!this.mergeItemStack(currentStack, 0, 27, false)) {
				return ItemStack.EMPTY;
			}
		} else {
			if (!this.mergeItemStack(currentStack, 0, 36, false)) {
				return ItemStack.EMPTY;
			}
		}

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

		if (currentStack.getCount() == returnStack.getCount()) {
			return ItemStack.EMPTY;
		}

		return returnStack;
	}
}

Slot class:

Spoiler

public class BackpackSlot extends AbstractBelongingsSlot {
	private final PlayerEntity player;
	private final int size;

	public BackpackSlot(IItemHandler baseItemHandler, IItemHandler inventoryItemHandler, int index, int xPosition, int yPosition, PlayerEntity player, int size) {
		super(baseItemHandler, inventoryItemHandler, index, xPosition, yPosition);
		this.player = player;
		this.size = size;
	}

	@Override
	public boolean isEnabled() {
		this.updateInventory(38, this.player.getItemStackFromSlot(EquipmentSlotType.CHEST));
		if (UMUEntityUtil.hasBackpack(player)) {
			return UMUEntityUtil.getBackpackSize(Minecraft.getInstance().player) >= size;
		}

		return false;
	}

	@Override
	public void updateInventory(int slotIndex, ItemStack itemStack) {
		if (slotIndex != 38) return;
		if (itemStack.getItem() == UMUItems.BACKPACK.get()) {
			this.inventoryItemHandler = itemStack
					.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY).orElse(new ItemStackHandler(36));
			return;
		}
		this.inventoryItemHandler = new ItemStackHandler(36);
	}
}

 


public abstract class AbstractBelongingsSlot extends SlotItemHandler implements IContainerListener {
	protected IItemHandler inventoryItemHandler;

	public AbstractBelongingsSlot(IItemHandler baseItemHandler, IItemHandler inventoryItemHandler, int index, int xPosition, int yPosition) {
		super(baseItemHandler, index, xPosition, yPosition);
		this.inventoryItemHandler = inventoryItemHandler;
	}

	@Override
	public IItemHandler getItemHandler() {
		return this.inventoryItemHandler;
	}

	@Override
	public void sendAllContents(Container containerToSend, NonNullList<ItemStack> itemsList) {
	}

	@Override
	public void sendSlotContents(Container containerToSend, int slotInd, ItemStack stack) {
		this.updateInventory(slotInd, stack);
	}

	@Override
	public void sendWindowProperty(Container containerIn, int varToUpdate, int newValue) {
	}

	public abstract void updateInventory(int slotIndex, ItemStack itemStack);
}

 

 

  • Author

I've fixed the code so that item operations can now be done normally. However, the backpack inventory operations that you were equipped with when you opened the inventory are saved, but the inventory operations for the backpack that you replaced in that inventory after opening the inventory are not saved. I was wondering if the inventory I changed later wouldn't be saved because the inventory I saved when I opened the container was saved somewhere else, but I couldn't find a match by tracing the code in the IDE. did. Does anyone know why the modified inventory isn't saved?

 

Container class:

Spoiler


public class BelongingsUMUPlayerContainer extends AbstractUMUPlayerContainer {
	private NonNullList<ItemStack> inventoryItemStacks = NonNullList.create();

	private static final UMUEquipmentSlotType[] VALID_EQUIPMENT_SLOTS = new UMUEquipmentSlotType[]{
			UMUEquipmentSlotType.HEAD, UMUEquipmentSlotType.CHEST, UMUEquipmentSlotType.LEGS, UMUEquipmentSlotType.FEET
	};
	private static final UMUEquipmentSlotType[] VALID_RING_SLOTS = new UMUEquipmentSlotType[]{
			UMUEquipmentSlotType.MAINRING1, UMUEquipmentSlotType.MAINRING2, UMUEquipmentSlotType.OFFRING1, UMUEquipmentSlotType.OFFRING2
	};
	private static final UMUEquipmentSlotType[] VALID_MISCELLANEOUS_SLOTS = new UMUEquipmentSlotType[]{
			UMUEquipmentSlotType.ENCYCLOPEDIA, UMUEquipmentSlotType.MAP
	};

	public BelongingsUMUPlayerContainer(int id, PlayerInventory playerInventory, PacketBuffer extraData) {
		this(UMUContainers.BELONGING_INVENTORY.get(), id, playerInventory, playerInventory.player);
	}

	public BelongingsUMUPlayerContainer(ContainerType<?> type, int id, PlayerInventory playerInventory, PlayerEntity player) {
		super(type, id, playerInventory, player);

		// index : 36 - 39
		for (int i = 0; i < 4; i++) {
			this.addSlot(new EquipmentSlot(playerInventory, 39 - i, 186, 18 + i * 18, VALID_EQUIPMENT_SLOTS[i]));
		}
		this.addSlot(new OffhandSlot(playerInventory, 40, 255, 72));
		this.addSlot(new EquipmentSlot(playerInventory, 41, 293, 27, UMUEquipmentSlotType.NECKLACE));
		// index : 42 - 45
		for (int iU = 0; iU < 2; iU++) {
			for (int iV = 0; iV < 2; iV++) {
				this.addSlot(new EquipmentSlot(playerInventory, 42 + iU * 2 + iV, 282 + iU * 18 + 4, 48 + iV * 18, VALID_RING_SLOTS[iU * 2 + iV]));
			}
		}
		for (int i = 0; i < 2; i++) {
			this.addSlot(new EquipmentSlot(playerInventory, 46 + i, 330, 72 + i * 18, VALID_MISCELLANEOUS_SLOTS[i]));
		}

		for (int iV = 0; iV < 4; iV++) {
			for (int iU = 0; iU < 9; iU++) {
				Slot slot = this.addSlot(new BackpackSlot(
						new ItemStackHandler(36), new ItemStackHandler(36), iU + iV * 9, 8 + iU * 18, 18 + iV * 18, player, iV + 1)
				);
				if (slot instanceof AbstractBelongingsSlot) {
					((AbstractBelongingsSlot) slot).updateInventory(37, this.player.getItemStackFromSlot(EquipmentSlotType.CHEST));
				}
			}
		}
	}

	@Override
	public ItemStack slotClick(int slotId, int dragType, ClickType clickTypeIn, PlayerEntity player) {
		this.inventorySlots.subList(48, 83).stream()
				.filter(s -> s instanceof AbstractBelongingsSlot)
				.forEach(s -> ((AbstractBelongingsSlot) s)
						.updateInventory(slotId, player.inventory.getItemStack()));

		return super.slotClick(slotId, dragType, clickTypeIn, player);
	}
}


public abstract class AbstractUMUPlayerContainer extends Container {
	protected final PlayerEntity player;

	protected AbstractUMUPlayerContainer(ContainerType<?> type, int id, PlayerInventory playerInventory, PlayerEntity player) {
		super(type, id);

		this.player = player;

		//index : 9 - 35
		for(int iV = 0; iV < 3; iV++) {
			for(int iU = 0; iU < 9; iU++) {
				this.addSlot(new Slot(playerInventory, iU + (iV + 1) * 9, 186 + iU * 18, 104 + iV * 18));
			}
		}
		for(int i = 0; i < 9; i++) {
			this.addSlot(new Slot(playerInventory, i, 186 + i * 18, 162));
		}
	}

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

	@Override
	@SuppressWarnings({"ConstantConditions", "NullableProblems"})
	public final ItemStack transferStackInSlot(PlayerEntity playerIn, int index) {
		Slot slot = this.inventorySlots.get(index);
		ItemStack returnStack = ItemStack.EMPTY;

		if (slot == null || !slot.getHasStack()) return returnStack;

		ItemStack currentStack = slot.getStack();
		returnStack = currentStack.copy();

		if (index >= 0 && index < 27) {
			if (!this.mergeItemStack(currentStack, 27, 36, false)) {
				return ItemStack.EMPTY;
			}
		} else if (index >= 27 && index < 36) {
			if (!this.mergeItemStack(currentStack, 0, 27, false)) {
				return ItemStack.EMPTY;
			}
		} else {
			if (!this.mergeItemStack(currentStack, 0, 36, false)) {
				return ItemStack.EMPTY;
			}
		}

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

		if (currentStack.getCount() == returnStack.getCount()) {
			return ItemStack.EMPTY;
		}

		return returnStack;
	}
}

 

Slot class:

Spoiler


public class BackpackSlot extends AbstractBelongingsSlot {
	private final PlayerEntity player;
	private final int size;

	public BackpackSlot(IItemHandler baseItemHandler, IItemHandler inventoryItemHandler, int index, int xPosition, int yPosition, PlayerEntity player, int size) {
		super(baseItemHandler, inventoryItemHandler, index, xPosition, yPosition);
		this.player = player;
		this.size = size;
	}

	@Override
	public boolean isEnabled() {
		if (UMUEntityUtil.hasBackpack(player)) {
			return UMUEntityUtil.getBackpackSize(Minecraft.getInstance().player) >= size;
		}

		return false;
	}

	@Override
	public void updateInventory(int slotIndex, ItemStack itemStack) {
		if (slotIndex != 37) return;
		if (itemStack.getItem() == UMUItems.BACKPACK.get()) {
			this.inventoryItemHandler = itemStack.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY)
					.orElse(new ItemStackHandler(36));
		} else {
			this.inventoryItemHandler = new ItemStackHandler(36);
		}
	}
}


public abstract class AbstractBelongingsSlot extends SlotItemHandler {
	protected IItemHandler inventoryItemHandler;

	public AbstractBelongingsSlot(IItemHandler baseItemHandler, IItemHandler inventoryItemHandler, int index, int xPosition, int yPosition) {
		super(baseItemHandler, index, xPosition, yPosition);
		this.inventoryItemHandler = inventoryItemHandler;
	}

	@Override
	public IItemHandler getItemHandler() {
		return this.inventoryItemHandler;
	}

	public abstract void updateInventory(int slotIndex, ItemStack itemStack);
}

 

 

Edited by Zemelua

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.