Raw material in a game

So I’m working in a game that I want to add mod support into, and have a question. How should I go about making raw material in the game? So far I have been thinking of having a abstract class called “material” which look something like this.

[quote]public abstract class Material {
private int amount = 1;
private final String name;
private BufferedImage icon;

public Material(String name, BufferedImage icon){
    this.name = name;
    this.icon = icon;
}

public Material(String name, BufferedImage icon, int amount){
    this.name = name;
    this.icon = icon;
    this.amount = amount;
}

public String getName(){
    return name;
}

public BufferedImage getIcon(){
    return icon;
}

public int getAmount(){
    return amount;
}

public void setAmount(int i){
    amount = i;
}

}
[/quote]
The point of this class is to be able to easily create new raw material, and add it into the game. But I’m sure this is not the best way to go about it, normally I would just a enum to make all the material, but since I want to have mod support inform of adding .jar file into a mod folder that’s not really an option since from what I know I can’t add things into a enum without changing the source code.

You probably shouldn’t have amount in there. Rather manage that in some other class like InventoryManager or something like that.
I would not worry about “mod support” right now, and just get the basis for the game finished. Then you can implement that kind of stuff.

Thanks, I’ll take the amount out, since you are right. It makes more sense to have it elsewhere.

I’m not going to go fully on mod support right now, but I want to build it so it’s easier to implement. If I hard code the material in a enums it will be difficult to add more materials, and would require recoding.

You have the right idea in that most everything is specified as data, although your material class should not be abstract. How you structure it depends on how materials are used in your game. For instance if you have a set of materials that can be used to build walls and a set of precious metal materials (gold, gems, etc) that are defined by their value. In that case you may have two classes, BuildableMaterial and ValuableMaterial.

So far I just focus on the material that’s is used to craft walls and items. My idea with abstract was to use something like new Wood() which extends the material class, but I should just use new Material(“Wood”,Texture.get(“wood.png”)); instead?

Also so far I use the name of the material to get it, like Material.get(“wood”); and if wood don’t exists it just return null.

I would lean towards new Material("wood",getTexture("wood.png")). It makes it easier to add new materials from a configuration file without having to change code.

Using some complex kind of inheritance for this is definitely not the way to go if you want to make it easily moddable.

Personally I would make a generic Material class and serialize it to/from a readable format like XML or JSON. The generic class contains all the properties that a material can have. The modder can then simply edit / create some JSON files to make new materials. E.g.

public class Material {
	public String name;
	public String iconFileName;
	public float value;
	public boolean forBuilding;
}

If you want to define that a Building needs Materials to be constructed you can for example make a MaterialRequirement class, e.g.:

public class MaterialRequirement {
	public String materialName;
	public float amount;
}

Then a Building class would (among many other things) contain a list of MaterialRequirements:

public class Building {
	public List<MaterialRequirement> materialReqs = new ArrayList<>();
	// other things
}

A JSON material file that a modder can edit would then look like this:

{
	name:			"wood"
,	iconFileName:	"images/wood-icon.png"
,	value:			2.0
,	forBuilding:	true
}

I’m not looking to make it very easy to mod, I have made plugins with bukkit and plan on going about it the same way, since it makes most sense to me. That is to make a mod/plugin the modder adds the game .jar file into the project and then can use event classes and methods to add, remove, change content in the game. Then after compile the mod it’s just to put it in the games plugin/mod folder.

Cool, that seems like a nice solution as well. Good luck with it.