Transfer an item from world to inventory [SOLVED]

Hi guys,

I am struggling a bit with my libgdx game design regarding how items should be handled with an inventory system. Currently, my item class is based on a generic GameObject which has properties like position, velocity, and can interact with the world.


package net.bigfootsoftware.seacraft.objects;

import net.bigfootsoftware.seacraft.engine.World;

import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.Rectangle;


public abstract class GameObject
{
	// FIELDS
		
	public Physics physics;
	
	protected World world;
	protected float width;
	protected float height;
	
	private String id;
	private Rectangle bounds;
	private boolean alive;
	
	public GameObject(String id, World world, float x, float y, float width, float height)
	{
		this.world = world;
		this.physics = new Physics(this);
		this.physics.position.set(x,y);
		this.id = id;
		this.width = width;
		this.height = height;
		this.bounds = new Rectangle();
		this.alive = true;
	}
	
	// GETTERS AND SETTERS
	
	public String getID()
	{
		return id;
	}
	
	public boolean isAlive()
	{
		return alive;
	}
	
	public void setAlive(boolean b)
	{
		alive = b;
	}
	
	public Rectangle getBounds()
	{
		bounds.set(physics.position.x, physics.position.y, width, height);
		return bounds;
	}
	
	public float getWidth()
	{
		return width;
	}
	
	public float getHeight()
	{
		return height;
	}
	
	public World getWorld()
	{
		return world;
	}

	
	// ABSTRACT METHODS
	
	public abstract void update(float dt);
	public abstract TextureRegion getTextureRegion();
	public abstract void collision(float dx, float dy);
	
	
	
}


My Item class looks like this. Notice the abstract method onUse().


package net.bigfootsoftware.seacraft.items;

import net.bigfootsoftware.seacraft.Game;
import net.bigfootsoftware.seacraft.engine.World;
import net.bigfootsoftware.seacraft.objects.GameObject;

import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.MathUtils;

public abstract class Item extends GameObject
{
	private TextureRegion region;
	
	public Item(String id, World world, float x, float y, float width, float height)
	{
		super(id,world,x,y,width,height);
		region = Game.res.getTextureRegion(id);
		physics.setJumpSpeed(-0.2f);
		physics.velocity.x = MathUtils.random(-0.05f, 0.05f);
		physics.jump();
	}

	
	public void update(float dt)
	{
		physics.apply(dt);
	}
	
	public abstract void onUse(float x, float y);


	@Override
	public TextureRegion getTextureRegion()
	{
		return region;
	}


	@Override
	public void collision(float dx, float dy)
	{
		physics.stopXVelocity();
	}
	
	
}


This works great because it allows each item to decide what should happen when it used. The problem is how do I represent these items using a String id only as opposed to having hundreds of live Item objects in the players inventory? I want to be able to remove the item from the world when collided with it, add that item as some sort of array of item id’s, then select an item from inventory, and when “used” it will simply call the abstract method onUse().

I thought about creating one static instance of each Item with some sort of Item registry but all GameObject requires a position and world reference making that impossible. Do I just make an entirely new class to represent each item? I might be over thinking this and I really could use some good advice.