Jump to content

[1.16] Please help...... Creating a container that can switch inventory of different capability items


Zemelua

Recommended Posts

Help me. I've been trying it for about two days, but I don't know. Currently the code is below. If you open the inventory with the backpack equipped and close it after operating the backpack slot, it will be saved correctly. However, if you open it with it equipped, remove the backpack, re-equip it, close the slot after operating it, the operation is not saved. Here's what I want to implement: What should I do to do that? Where should I fix it and which method should I use? I need help.

1. The backpack slot is active when the player is equipped with a backpack.
2. The same applies when the player equips / removes the backpack in the inventory screen. At that time, the slot (the item operation performed in the backpack inventory) is saved correctly.
3. With the above, it is possible to put on and take off multiple backpack items (that is, each has a different inventory) in the inventory.

 

AbstractUMUPlayerContainer addsslot()s vanilla player inventory slots.

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++) {
//				//this.addSlot(new BackpackSlot(new ItemStackHandler(36), iU + iV * 9, 8 + iU * 18, 18 + iV * 18, Minecraft.getInstance().player, iV + 1));
//			}
//		}
		this.initBackpackSlots();
	}

	private void initBackpackSlots() {
		for (int iV = 0; iV < 4; iV++) {
			for (int iU = 0; iU < 9; iU++) {
				IItemHandler itemHandler = new ItemStackHandler(36);
				if (UMUEntityUtil.hasBackpack(this.player)) {
					itemHandler = this.player.getItemStackFromSlot(EquipmentSlotType.CHEST)
							.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY).orElse(new ItemStackHandler(36));
				}
				this.addSlot(new BSlot(itemHandler, iU + iV * 9, 8 + iU * 18, 18 + iV * 18, this.player, iV + 1));
			}
		}
	}

	@Override
	public ItemStack slotClick(int slotId, int dragType, ClickType clickTypeIn, PlayerEntity player) {
		if (slotId == 38) {
			//this.inventorySlots.subList(48, 83).forEach(s -> ((BackpackSlot) s).onEquipOrRemoveBackpack());
			player.openContainer = new BelongingsUMUPlayerContainer(this.windowId, player.inventory, null);
		}

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

	public class BSlot extends SlotItemHandler {
		private final PlayerEntity player;
		private final int size;

		public BSlot(IItemHandler itemHandler, int index, int xPosition, int yPosition, PlayerEntity player, int size) {
			super(itemHandler, 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;
		}
	}
}

 

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.