Jump to content

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


Recommended Posts

Posted

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;
		}
	}
}

 

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



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • I myself am quite new to the modding scene, I started modding in February but have been in development hell a lot and have struggled to focus on one mod though I have been developing the current mod I've been working on since September and have made a lot of progress; during this time I have found a lot of helpful resources and have learnt a lot of things.    1. You can, I have had no Java experience previously and since modding have managed to be quite knowledgeable in Java.   2. Modding does not require massive teams, I and many others work alone, though having a team is certainly helpful in the modding process to get things done in a quicker and more organised way.   3. I absolutely have to recommend ModdingByKaupenjoe, he makes loads of tutorials for Minecraft from 1.16.5 and above with support for Forge, Fabric, and as of his 1.21 tutorials, Neoforge. These tutorials are really well made, covering almost every modding topic, (such as items, blocks, mobs, worldgen, etc.) and are pretty easy to follow, and Kaupenjoe always leaves the link to his GitHub repositories where you can view the code of that tutorial at your own pace as well as linking textures in the description of his videos for you to use. These forums are also quite good if you need help, though I have found that it sometimes takes a little while for a response but it is always worth the wait; from my experience the people on these forums have always been kind and helpful. There are also user-submitted tutorials that may be helpful as well.  Using GitHub to search up a certain class that you are wanting to use in your mod is also quite helpful, the video I have linked goes into more detail. I would also recommend planning out your mod before you make it to have a clearer idea of how you want your mod to be, including sketches and annotations are also a good idea for this. It has helped me make progress a lot quicker when programming as I already know how I want things to look/act.   I hope all this information helps!
    • I don't know what I did differently, but the error code changed to "Invalid signature for profile public key" so I set "enforce-secure-profile" to false since only my friend will be allowed to join anyway and then everything worked properly. Here's the logs anyway, but I hope the problem doesn't come back. https://paste.ee/p/Ri44L
    • I tried to look for similar issues here on the forum already and couldn't find a fix. Just people who didn't read the rules properly.
  • Topics

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.