Jump to content

Recommended Posts

Posted (edited)

Hey folks,

I've been trying to implement a GUI , which should add and remove (or enables/disables) slots depending of an itemstack in a dedicated slot. Let me explain with using some images:
BasicGUI.png.ddbf38096330e1f1170631f28f5847af.png
the bottom slots are the player inventory. At the top, we have a slot for a tool, which implement IMateriaTool (for my own tool classes). Right next to it are the slots for Materia. And the last block is for displaying some information (the screen class will display some text here, so it is uninterresting for the container). On opening the GUI, the player inventory should be filled correctly, but all the slots at the top are empty. At this point, it should not be possible to add Materia in any of the Materia slots. Either because the slots don't exist yet or because they are disabled.
When I put an IMateriaTool into the right slot, I want the right amount of materia slots added/enabled and I also want to fill some of them with the right items. The number of slots and the corresponding itemstack all depend on the tool and said data can be retrieved from it

withToolGUI.png.bfeb5fe2c0cd69d2959ad707e1685bd9.png
Now I should be able to modify the data from the tool by adding/removing Materia.
When I remove the tool from his slot, I want to be back in the state from the first picture. This means, that the slots for the Materia need to be cleaned and removed/disabled again.
I hope this explanation was clear enough. In case of confusion, please ask.

Now to my question: How can implement this? For the last week I've been trying to figure something out by myself, but everytime I get a little bit confused and suddendly I have an IndexOutOfBounds error.
For starters I have slots specifically for MateriaTools and Materia:
https://github.com/Tavi007/Materia/blob/master/src/main/java/Tavi007/Materia/inventory/container/MateriaContainerSlot.java
https://github.com/Tavi007/Materia/blob/master/src/main/java/Tavi007/Materia/inventory/container/MateriaToolContainerSlot.java
Then I also have a container and an inventory, which is supposed to handle the items at the top (so the tool and the materia)
https://github.com/Tavi007/Materia/blob/master/src/main/java/Tavi007/Materia/inventory/container/EquippingStationContainer.java

https://github.com/Tavi007/Materia/blob/master/src/main/java/Tavi007/Materia/inventory/EquippingStationInventory.java

 

keep in mind, that the current state of my master branch was me trying to figure it out on my own. So stuff might be handled stupidly.
I also tried my best be reading some code of different tutorial mods, but none of them could help with my confusion so far.


So I really hope you guys can help me.

PS: For those, who think, that this kinda system seems familiar: I'm trying to implement the Materia system from Final Fantasy VII in Minecraft :)

Edited by Tavi007
Posted

I will try my best to implement your suggestion. Few question before I start diving in.
1: Slot#isEnabled ist client only. How can this prevent the server side container to not put stacks into certain slots? (for example via shift clicking)
2: Where can I find some classes/tutorials that implements IItemHandler in combination with a container?

Posted
17 minutes ago, TheGreyGhost said:

Hi

 

You might find this tutorial example useful when trying to understand how the vanilla inventories work

https://github.com/TheGreyGhost/MinecraftByExample/tree/master/src/main/java/minecraftbyexample/mbe30_inventory_basic

 

It uses IInventory instead of IItemHandlers but the rest is directly relevant.

 

-TGG

I know about your github. It is one of my references. What is the difference between the IITemHandler and an IIventory? And when am I supposed to use which?

Posted (edited)
34 minutes ago, Tavi007 said:

I know about your github. It is one of my references. What is the difference between the IITemHandler and an IIventory? And when am I supposed to use which?

IItemHandler exposes an interface for handling inventory slots. It can be applied to TileEntities (chests, machines, etc.), Entities (extra player slots, mob/creature inventories/bags), or ItemStacks (portable backpacks and such). It replaces the old IInventory and ISidedInventory with an automation-friendly system.

 

from: https://mcforge.readthedocs.io/en/latest/datastorage/capabilities/#forge-provided-capabilities

 

Edit: you should always use IItemHandler, unless you are calling RecipeManager#getRecipe(), in which case you would need to create a dummy inventory to be passed to the method

Edited by kiou.23
Posted
8 hours ago, Tavi007 said:

I know about your github. It is one of my references. What is the difference between the IITemHandler and an IIventory? And when am I supposed to use which?

Hi

IInventory is the vanilla implementation used for containers

IItemHandler is a Forge extension

 

You can use either one, they will both work fine if you implement them correctly:

The vanilla IInventory has some extra methods which are used by the slots to communicate with the container, but you can nearly always safely ignore those.

IItemHandler is more flexible; you can (for example) use it to implement an inventory on an item.  In some ways it is also stylistically preferable ("composition instead of inheritance").  As Kiou said it is more "automation friendly" in some ways.

 

In practice, just pick one and go with it, don't worry too much about which one is "optimum".  The users of your mod are highly unlikely to ever notice the difference, and if you do ever need to change it to IItemHandler in the future, it's easy to refactor.

 

Cheers

  TGG

 

 

 

 

Posted (edited)
25 minutes ago, diesieben07 said:

This is not true.

IInventory will prevent modded machines from interacting with your inventory. IItemHandler is the standard. Do not use IInventory.

Since the inventory is called the same way as a the GUI of a workbench, it would actually not matter in my case. I will stick to the ItemHandler however, because I might change my mind on the former and that would make switching easier.

Edited by Tavi007
  • 3 weeks later...
Posted

Thanks, I finally figured most of it out. The slots now get enabled as they should and also shift-left clicking works as intended.

Extracting itemstacks from my inventory using a simple mousclick is slightly broken however. After clicking i can move the itemstack, but i have some weird ghost stack left in the slot, that disappears after I place the stack back into an inventory. So something is definitely wrong in my extractItem function, but I don't know what. (link)

Posted

Because I'm too dumb to read your post, lol. Please take a look at my new code:
 

package Tavi007.Materia.inventory;

import java.util.ArrayList;

import Tavi007.Materia.inventory.container.EquippingStationContainer;
import Tavi007.Materia.items.IMateriaTool;
import Tavi007.Materia.util.MateriaToolUtil;
import net.minecraft.item.ItemStack;
import net.minecraftforge.items.ItemStackHandler;

public class EquippingStationItemHandler extends ItemStackHandler {
	private final EquippingStationContainer container;

	public EquippingStationItemHandler(EquippingStationContainer container) {
		super(9);
		this.container = container;
	}
	
	@Override
	public void onContentsChanged(int slot) {
		if(slot>=0 && slot < 8) {
			// BaseMateria
			ItemStack stack = getStackInSlot(slot);
			if (stack.isEmpty()) {
				// Materia got removed
				IMateriaTool tool = (IMateriaTool) getMateriaToolStack().getItem(); //the tool
				MateriaToolUtil.setMateriaStack(tool, ItemStack.EMPTY, slot);
			}
			else {
				// Materia got inserted
				IMateriaTool tool = (IMateriaTool) getMateriaToolStack().getItem(); //the tool
				MateriaToolUtil.setMateriaStack(tool, stack, slot);
			}
			
		}
		else if(slot == 8) {
			// IMateriaTool
			ItemStack stack = getStackInSlot(slot);
			if (stack.isEmpty()) {
				// IMateriaTool got removed
				for(int i=0; i<8; i++) {
					stacks.set(i, ItemStack.EMPTY);
				}
			}
			else {
				// IMateriaTool got inserted
				IMateriaTool tool = (IMateriaTool) stack.getItem();
				ArrayList<ItemStack> topStacks = MateriaToolUtil.getMateriaStacks(tool.getTopSlots());
				for(int i=0; i<topStacks.size(); i++) {
					stacks.set(i, topStacks.get(i));
				}
				ArrayList<ItemStack> botStacks = MateriaToolUtil.getMateriaStacks(tool.getBotSlots());
				for(int i=0; i<botStacks.size(); i++) {
					stacks.set(i+4, botStacks.get(i));
				}
			}
		}
		else {
			// Should not have happened
		}
	}


	@Override
	public int getSlotLimit(int slot) {
		return 1;
	}


	@Override
	public boolean isItemValid(int slot, ItemStack stack) {
		return container.inventorySlots.get(slot).isItemValid(stack);
	}

	public boolean isMateriaSlotEnabled(int slotIndex) {
		ItemStack itemstack = container.getMateriaToolStack();
		if(itemstack.isEmpty()) {
			return false;
		}
		IMateriaTool tool = (IMateriaTool) itemstack.getItem();
		int noSlots;
		if(slotIndex<4) {
			noSlots = MateriaToolUtil.getNumberOfSlots(tool.getTopSlots());
			if(slotIndex<noSlots) {
				return true;
			}
		}
		else {
			noSlots = MateriaToolUtil.getNumberOfSlots(tool.getBotSlots());
			if(slotIndex-4<noSlots) {
				return true;
			}
		}
		return false;
	}
	
	public ItemStack getMateriaToolStack() {
		return stacks.get(8);
	}
}


I removed my own implementation of insertItem and extractItem and instead added my own code to onContentsChanged.
Sadly a few new problems arise. First of, if I click with an item on a slot, that already has an itemstack, then the itemstack, that I'm holding (the one near the cursor) changes correctly. But the stack in the slot stays the same. Even more so, the stack in the slot is a weird ghost stack, that disappears after I insert the holded stack into the player inventory.

I also noticed that shift-left click slots in my inventory to extract an item, never triggers the onContentsChanged function. That is kinda bad for me, because I would like to apply the same logic as the normal item extraction.

Posted

So, I should implement a capability, that extends ItemStackHandler (so it can store other ItemStacks), which then gets attached to the any of my IMateriaTool ItemStacks? Then I can fill the capability as I want in my EquippingStation. You might notice, that I'm a little bit confused ^^'

 

Well I can implement that part, but would that really solve my problem with my EquippingStation inventory behaving so weirdly?

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.