...

View Full Version : Get Class by Field Value



djh101
12-14-2011, 09:33 AM
What I want to be able to do is give each class an id (for easy storage in a database) and be able to use that id to call the class whenever I may need to.

So, let's say I'm building an MMORPG. When a player obtains a sword (let's call it Sword A), the id for that sword will be stored in a database (so when the player logs out they can log back in and still have the sword). When the player gets into a fight, I would need to be able to take the id or Sword A and use it to access the class of Sword A so that I can retrieve its stats for damage calculation.



int[] inventory = {1476} //Player has 1 item in their inventory with id 1476
...
class SwordA {
public static int id = 1476;
public static byte attack = 14;
}


Is there any way to access class SwordA using its id, apart from a method featuring an if statement for every item?

djh101
12-14-2011, 09:38 AM
This is the only way I know of that could accomplish this, but I'd prefer a method that avoids going through up to several thousand if statements:


Item getItem(int id){
if(id == 1) return BootsA();
else if(id == 2) return ShieldC();
else if(id == 3) return Cabbage76();
...
else if(id == 1476) return SwordA();
}
Item foo = getItem(1476);

Fou-Lu
12-14-2011, 04:11 PM
Not easily. If its hard coded, you need to look it up manually.
Here's an example of what would work. Although, if you have a class called ShieldC, is it fair to assume you also have one called ShieldA and ShieldB? If so, this is overly complex, unless these are explicitly different datatypes. id is typically related to the object itself, and not that of the class.



public class ItemGetIDTest
{
public Object constructByID(ArrayList<Class<?>> c, int i) throws IllegalArgumentException, SecurityException, IllegalAccessException, NoSuchFieldException, InstantiationException, InvocationTargetException, NoSuchMethodException
{
Class<?> create = null;
for (Class<?> itor : c)
{
if (itor.getDeclaredField("id").getInt(itor) == i)
{
create = itor;
break;
}
}

return create.getConstructor().newInstance();
}

public static void main(String... argv)
{
ItemGetIDTest t = new ItemGetIDTest();
ArrayList<Class<?>> al = new ArrayList<Class<?>>();
al.add(SwordA.class);
al.add(Item.class);
try
{
SwordA s = (SwordA)(t.constructByID(al, 1476));
System.out.println(s);
}
catch (Exception ex)
{
System.out.println("Error: " + ex.getMessage());
}
}
}

class Item
{
public static int id = 0;
public int getID()
{
return id;
}
}

class SwordA extends Item
{
public static int id = 1476;
public SwordA()
{

}
}

djh101
12-14-2011, 06:47 PM
Thank you very much. And no, that was just an example. All items would have unique names (e.g. ultimate chaotic shield of doom) and would be extensions of different classes (i.e. class SwordA extends sword which extends item).

Fou-Lu
12-15-2011, 05:43 AM
Thank you very much. And no, that was just an example. All items would have unique names (e.g. ultimate chaotic shield of doom) and would be extensions of different classes (i.e. class SwordA extends sword which extends item).

This still sounds like a data issue, and not a structural one.


class Item
{
protected int id;
protected double value;
protected String name;
// whatever else + methods
}

interface IWeapon
{
public double getDamage();
public void setDamage(double d);
}

class Sword extends Item implements IWeapon
{
private double damage;
public Sword(int id, String name, double value, double damage)
{
super(id, name, value);
setDamage(damage);
}
public void setDamage(double d)
{
this.damage = d;
}
public double getDamage()
{
return this.damage;
}

public static void main(String... argv)
{
Sword s1 = new Sword(1, "Ultimate Weapon", 0, 255);
Sword s2 = new Sword(2, "Wooden Sword", 10, 10);
// etc.
}
}


Same logic applies to any type.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum