I have a group of methods that is going to be very large. I need to be able to call methods systematically from a large group, in two different ways. The methods create a new item object with variables specific to the type of item created, for instance the rubber(); method makes an item with
itemId = 12, weight = 1, name = "Rubber", ...
for 20+ more variables. I am currently able to call these by name
object.rubber();
I want to be able to call them by itemId
object.getById(12);
I need to be able to process them by itemId for situations like this:
/* itemId:
1: ruined copper
2: weak copper
3: refined copper
4: ruined iron
5: weak iron
6: refined iron
7: ruined brass
-(More metals in this pattern)-
36: sturdy aluminum
*/
//within furnace class
public void smelt(item input, inventory furnace){
//input is the item to smelt, furnace is the furnace's crucible.
if(input.getId() < 36 && input.getId()%3 = 1){
//if the imput's ID is less than 36, it's a metal, and if ID % 3 = 1, it's a ruined metal, meaning it is smeltable.
furnace.addItem(handler.list.addById(input.getId() + 1))
//add an item to the furnace with an ID one higher than that of the input, which will be a 'weak' metal of the same type
furnace.removeItem(input);
//delete the input item
}
}
The metals are all regularly ordered, so if I can call them by ID it will be possible to manipulate them with very little code, whereas otherwise the furnace will need an if statement for each individual metal to determine what it smelts into. Many other processes will need to be able to work similarly.
I've found two possible solutions so far:
one would be to use reflect to call the methods, but I've been warned that it's notoriously unstable and difficult to figure out.
the other would be to overhaul my current system and store the item properties on a text file, something like
Ruined_Copper false 4 256 ...
Weak_Copper false 4 128 ...
etc.
and then have a filereader open the file and count lines until the line number equaled the target itemId. It would then split the line by whitespace and assign the values to variables, ie:
newItem.setName(lineOutput(1));
newItem.setIsContainer(lineOutput(2));
...
This would output an item with
name=Ruined_Copper, isContainer = false, etc.
I'd prefer to figure this out early in development so I don't have to change thousands of items to a new format in the future. Which one of these will go faster? Which one will be more stable? Is there a better way alltogether?
EDIT: To clarify, I realize that it would be a bad idea to have metals 0-36, woods 37-56, etc. My goal is to allow systematic or player-created items to be added by player actions in game, so the finished product will have space between catagories, ie 0-60 are metals, 61-999 are blank, 1000-1085 are wood types, 1086-1999 are blank, etc.