making an inventory work.

I just was looking for some one to shoot these ideas with, someone with more knowledge then me :P.

In my swing text based game I want to have a inventory system the player can use to well use items.

I’m wondering if these ideas are good, bad, or just ugly.
(MVC pattern in use…sorta)

Controller gets Inventory from player
Controller Gets each item from an ArrayList in the inventory
for each item create a button though a “dynamic” button maker in the view class
view returns the button to have controller add an actionlistener
actionListener calls the appropriate method in the player class to use the item
controller returns to state prior to using the items though Enum gameState

I hope that isnt too bad looking.
My issue with this is that this means each and every item would need an actionListener inside the controller class. Is that normal or is there a better way.

…I thought I might have more in my head or on paper but I don’t. I feel there has to be a better way… Any help is appreciate and welcome. So thank-you.

If I were to approach this, I would have to agree with you up until you start throwing actionlisteners in. I’m not sure if this is best practice, but I would have all the “Items” extend one abstract class. In the abstract class, I would have an abstract method called something like “doAction(Player player, Environment env)”. Then, you can have each item class have its own instruction, without throwing all the instructions into one class. Of course, the issue with this is that you have to create a new class for every item.

hmmm the many classes isnt an issue, as thats already the route I’m going. I also have an parent to those classes that i just called item, however I would still need actionListeners would i not? I’m thinking to have the button call this doAction method of the item the player wants. I guess in this. I’m a newbie so you’ll have to bare with me. is it possible when creating an actionListner to pass in another parameter then the default ones? because I’m thinking then i could have one listener which stores the current index of the item its associated to. Maybe this is still wrong could elaborate further please.

Thank-you for your help

I would make an extension of whatever button class you are using that has an Item variable in it. When you generate all of your buttons, add the item object to the constructor of the button. Then, when you process the actionevent, you can cast the source of the event to your new button class. From your source, you can access the item variable you had stored in it.

Sorry for the delay in response. I’m very much learning still so please forgive me if I mess this up, but let me see if I understand what your saying.
(I’m using JButton btw)
Extend the Jbutton as a new class lets say myButton
give myButton a constuctor which gives it a connection to the item stored in the inventory
have an action event which calls to that stored item and effects the player though a collection of methods.

okay if thats right would the code in the actionevent call something like this
this.getItem.doAction(player);
(saying my button has a method called getItem returning the stored item)

I would still assume that the controller would hold the action event like in my previous statement.

Here’s some quick code I wrote up. No time to try it so tell me if it doesn’t work.
This is just a mockup of what I would suggest for the “MyButton” class. (Although, I don’t recommend naming it that.

public class MyButton extends JButton{

	public Item i;

	public MyButton(....., Item i){ // The "......." is whatever arguments you're already passing to create the JButton, if any.
		super(....);
		this.i = i;
	}
	public Item getItem(){
		return i;
	}
}

Here’s where your button press would be handled.

public void actionPerformed(ActionEvent e){
	Object o = e.getSource();
	if(o instanceof MyButton){
		MyButton b = (MyButton)o;
		b.getItem().doAction(player);
	}
	
}

That worked although I’m having an issue where the action isnt happening except for right after its acquired even though all the other stuff is obtained for generating the button itself so I guess its something with the actionlistener ceasing to work for some reason.

Well anyways thank-you for your help cubemaster21