Jump to content

GUI with dynamic item slots


Tavi007

Recommended Posts

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
Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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
Link to comment
Share on other sites

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

 

 

 

 

Link to comment
Share on other sites

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
Link to comment
Share on other sites

  • 3 weeks later...

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)

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

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



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • DAFTAR & LOGIN SIRITOGEL Siritogel adalah kumpulan kata yang mungkin baru saja dikenal oleh masyarakat, namun dengan perkembangan teknologi dan banyaknya informasi yang tersedia di internet, kalau kita siritogel (mencari informasi dengan cara yang cermat dan rinci) tentang situs slot gacor online, maka kita akan menemukan banyak hal yang menarik dan membahayakan sama sekali. Dalam artikel ini, kita akan mencoba menjelaskan apa itu situs slot gacor online dan bagaimana cara mengatasi dampaknya yang negatif.
    • This honestly might just work for you @SubscribeEvent public static void onScreenRender(ScreenEvent.Render.Post event) { final var player = Minecraft.getInstance().player; if(!hasMyEffect(player)) return; // TODO: You provide hasMyEffect float f = Mth.lerp(p_109094_, this.minecraft.player.oSpinningEffectIntensity, this.minecraft.player.spinningEffectIntensity); float f1 = ((Double)this.minecraft.options.screenEffectScale().get()).floatValue(); if(f <= 0F || f1 >= 1F) return; float p_282656_ = ?; final var p_282460_ = event.getGuiGraphics(); int i = p_282460_.guiWidth(); int j = p_282460_.guiHeight(); p_282460_.pose().pushPose(); float f = Mth.lerp(p_282656_, 2.0F, 1.0F); p_282460_.pose().translate((float)i / 2.0F, (float)j / 2.0F, 0.0F); p_282460_.pose().scale(f, f, f); p_282460_.pose().translate((float)(-i) / 2.0F, (float)(-j) / 2.0F, 0.0F); float f1 = 0.2F * p_282656_; float f2 = 0.4F * p_282656_; float f3 = 0.2F * p_282656_; RenderSystem.disableDepthTest(); RenderSystem.depthMask(false); RenderSystem.enableBlend(); RenderSystem.blendFuncSeparate(SourceFactor.ONE, DestFactor.ONE, SourceFactor.ONE, DestFactor.ONE); p_282460_.setColor(f1, f2, f3, 1.0F); p_282460_.blit(NAUSEA_LOCATION, 0, 0, -90, 0.0F, 0.0F, i, j, i, j); p_282460_.setColor(1.0F, 1.0F, 1.0F, 1.0F); RenderSystem.defaultBlendFunc(); RenderSystem.disableBlend(); RenderSystem.depthMask(true); RenderSystem.enableDepthTest(); p_282460_.pose().popPose(); }   Note: Most of this is directly copied from GameRenderer as you pointed out you found. The only thing you'll have to likely do is update the `oSpinningEffectIntensity` + `spinningEffectIntensity` variables on the player when your effect is applied. Which values should be there? Not 100% sure, might be a game of guess and check, but `handleNetherPortalClient` in LocalPlayer has some hard coded you might be able to start with.
    • Dalam dunia perjudian online yang berkembang pesat, mencari platform yang dapat memberikan kemenangan maksimal dan hasil terbaik adalah impian setiap penjudi. OLXTOTO, dengan bangga, mempersembahkan dirinya sebagai jawaban atas pencarian itu. Sebagai platform terbesar untuk kemenangan maksimal dan hasil optimal, OLXTOTO telah menciptakan gelombang besar di komunitas perjudian online. Satu dari banyak keunggulan yang dimiliki OLXTOTO adalah koleksi permainan yang luas dan beragam. Dari togel hingga slot online, dari live casino hingga permainan kartu klasik, OLXTOTO memiliki sesuatu untuk setiap pemain. Dibangun dengan teknologi terkini dan dikembangkan oleh para ahli industri, setiap permainan di platform ini dirancang untuk memberikan pengalaman yang tak tertandingi bagi para penjudi. Namun, keunggulan OLXTOTO tidak hanya terletak pada variasi permainan yang mereka tawarkan. Mereka juga menonjol karena komitmen mereka terhadap keamanan dan keadilan. Dengan sistem keamanan tingkat tinggi dan proses audit yang ketat, OLXTOTO memastikan bahwa setiap putaran permainan berjalan dengan adil dan transparan. Para pemain dapat merasa aman dan yakin bahwa pengalaman berjudi mereka di OLXTOTO tidak akan terganggu oleh masalah keamanan atau keadilan. Tak hanya itu, OLXTOTO juga terkenal karena layanan pelanggan yang luar biasa. Tim dukungan mereka selalu siap sedia untuk membantu para pemain dengan segala pertanyaan atau masalah yang mereka hadapi. Dengan respon cepat dan solusi yang efisien, OLXTOTO memastikan bahwa pengalaman berjudi para pemain tetap mulus dan menyenangkan. Dengan semua fitur dan keunggulan yang ditawarkannya, tidak mengherankan bahwa OLXTOTO telah menjadi pilihan utama bagi jutaan penjudi online di seluruh dunia. Jika Anda mencari platform yang dapat memberikan kemenangan maksimal dan hasil optimal, tidak perlu mencari lebih jauh dari OLXTOTO. Bergabunglah dengan OLXTOTO hari ini dan mulailah petualangan Anda menuju kemenangan besar dan hasil terbaik!
    • Selamat datang di OLXTOTO, situs slot gacor terpanas yang sedang booming di industri perjudian online. Jika Anda mencari pengalaman bermain yang luar biasa, maka OLXTOTO adalah tempat yang tepat untuk Anda. Dapatkan sensasi tidak biasa dengan variasi slot online terlengkap dan peluang memenangkan jackpot slot maxwin yang sering. Di sini, Anda akan merasakan keseruan yang luar biasa dalam bermain judi slot. DAFTAR OLXTOTO DISINI LOGIN OLXTOTO DISINI AKUN PRO OLXTOTO DISINI   Jackpot Slot Maxwin Sering Untuk Peluang Besar Di OLXTOTO, kami tidak hanya memberikan hadiah slot biasa, tapi juga memberikan kesempatan kepada pemain untuk memenangkan jackpot slot maxwin yang sering. Dengan demikian, Anda dapat meraih keberuntungan besar dan memenangkan ribuan rupiah sebagai hadiah jackpot slot maxwin kami. Jackpot slot maxwin merupakan peluang besar bagi para pemain judi slot untuk meraih keuntungan yang lebih besar. Dalam permainan kami, Anda tidak harus terpaku pada kemenangan biasa saja. Kami hadir dengan jackpot slot maxwin yang sering, sehingga Anda memiliki peluang yang lebih besar untuk meraih kemenangan besar dengan hadiah yang menggiurkan. Dalam permainan judi slot, pengalaman bermain bukan hanya tentang keseruan dan hiburan semata. Kami memahami bahwa para pemain juga menginginkan kesempatan untuk meraih keberuntungan besar. Oleh karena itu, OLXTOTO hadir dengan jackpot slot maxwin yang sering untuk memberikan peluang besar kepada para pemain kami. Peluang Besar Menang Jackpot Slot Maxwin Peluang menang jackpot slot maxwin di OLXTOTO sangatlah besar. Anda tidak perlu khawatir tentang batasan atau pembatasan dalam meraih jackpot tersebut. Kami ingin memberikan kesempatan kepada semua pemain kami untuk merasakan sensasi menang dalam jumlah yang luar biasa. Jackpot slot maxwin kami dibuka untuk semua pemain judi slot di OLXTOTO. Anda memiliki peluang yang sama dengan pemain lainnya untuk memenangkan hadiah jackpot yang besar. Kami percaya bahwa semua orang memiliki kesempatan untuk meraih keberuntungan besar, dan itulah mengapa kami menyediakan jackpot slot maxwin yang sering untuk memenuhi harapan dan keinginan Anda.   Kesimpulan OLXTOTO adalah situs slot gacor terbaik yang memberikan pengalaman bermain judi slot online yang tak terlupakan. Dengan variasi slot online terlengkap dan peluang memenangkan jackpot slot maxwin yang sering, OLXTOTO menjadi pilihan terbaik bagi para pemain yang mencari kesenangan dan kemenangan besar dalam perjudian online. Di samping itu, OLXTOTO juga menawarkan layanan pelanggan yang ramah dan responsif, siap membantu setiap pemain dalam mengatasi masalah teknis atau pertanyaan seputar perjudian online. Kami menjaga integritas game dan memberikan lingkungan bermain yang adil serta menjalankan kebijakan perlindungan pelanggan yang cermat. Bergabunglah dengan OLXTOTO sekarang dan nikmati pengalaman bermain slot online yang luar biasa. Jadilah bagian dari komunitas perjudian yang mengagumkan ini dan raih kesempatan untuk meraih kemenangan besar. Dapatkan akses mudah dan praktis ke situs OLXTOTO dan rasakan sensasi bermain judi slot yang tak terlupakan.  
    • OLXTOTO: Platform Maxwin dan Gacor Terbesar Sepanjang Masa Di dunia perjudian online yang begitu kompetitif, mencari platform yang dapat memberikan kemenangan maksimal (Maxwin) dan hasil terbaik (Gacor) adalah prioritas bagi para penjudi yang cerdas. Dalam upaya ini, OLXTOTO telah muncul sebagai pemain kunci yang mengubah lanskap perjudian online dengan menawarkan pengalaman tanpa tandingan.     Sejak diluncurkan, OLXTOTO telah menjadi sorotan industri perjudian online. Dikenal sebagai "Platform Maxwin dan Gacor Terbesar Sepanjang Masa", OLXTOTO telah menarik perhatian pemain dari seluruh dunia dengan reputasinya yang solid dan kinerja yang luar biasa. Salah satu fitur utama yang membedakan OLXTOTO dari pesaingnya adalah komitmen mereka untuk memberikan pengalaman berjudi yang unik dan memuaskan. Dengan koleksi game yang luas dan beragam, termasuk togel, slot online, live casino, dan banyak lagi, OLXTOTO menawarkan sesuatu untuk semua orang. Dibangun dengan teknologi terkini dan didukung oleh tim ahli yang berdedikasi, platform ini memastikan bahwa setiap pengalaman berjudi di OLXTOTO tidak hanya menghibur, tetapi juga menguntungkan. Namun, keunggulan OLXTOTO tidak hanya terletak pada permainan yang mereka tawarkan. Mereka juga terkenal karena keamanan dan keadilan yang mereka berikan kepada para pemain mereka. Dengan sistem keamanan tingkat tinggi dan audit rutin yang dilakukan oleh otoritas regulasi independen, para pemain dapat yakin bahwa setiap putaran permainan di OLXTOTO adalah adil dan transparan. Tidak hanya itu, OLXTOTO juga dikenal karena layanan pelanggan yang luar biasa. Dengan tim dukungan yang ramah dan responsif, para pemain dapat yakin bahwa setiap pertanyaan atau masalah mereka akan ditangani dengan cepat dan efisien. Dengan semua fitur dan keunggulan yang ditawarkannya, tidak mengherankan bahwa OLXTOTO telah menjadi platform pilihan bagi para penjudi online yang mencari kemenangan maksimal dan hasil terbaik. Jadi, jika Anda ingin bergabung dengan jutaan pemain yang telah merasakan keajaiban OLXTOTO, jangan ragu untuk mendaftar dan mulai bermain hari ini!  
  • Topics

×
×
  • Create New...

Important Information

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