Jump to content
Divine-X

Java Text Based Game Inventory/Item help

Recommended Posts

Okay so, I currently have my items list saved in a .xml file and what I want to do is basically get the info from the xml for the specific item. Mm... Like

Lets say in my item list file I have 3 items: health_potion, coins, wooden_sword

I want to be able to get the item from the xml and use it in the game (like be able to get the specific items info like examine and shop price etc) and save it in Playersinvy.txt (I'm storing it in a .txt because I can't seem to get the inventory to save in the players XML file like RSPS do, if someone can help with that I would appreciate it!)

Here's my Item related stuff and XMLParser(don't know if you need that though)

[code]
public class XMLParser {

private List<Map<String, String>> elements = new ArrayList<Map<String, String>>();
private BufferedReader reader;

public XMLParser(final String file) throws IOException {
reader = new BufferedReader(new FileReader(file));
}

public void setByElement(String name) throws IOException {
String line;
String endName = "</" + name + ">";
name = "<" + name + ">";
while ((line = reader.readLine()) != null) {
if (line.contains(name)) {
Map<String, String> element = new HashMap<String, String>();
while (!(line = reader.readLine()).contains(endName)) {
element.put(line.substring(line.indexOf("<") + 1, line.indexOf(">")), line.substring(line.indexOf(">") + 1, line.lastIndexOf("<")));
}
elements.add(element);
}
}
reader.close();
}

public List<Map<String, String>> getElements() {
return elements;
}

public String getStringByTag(Map<String, String> element, String name) {
return element.get(name);
}

public long getLongByTag(Map<String, String> element, String name) {
return Long.parseLong(element.get(name));
}

public int getIntByTag(Map<String, String> element, String name) {
return Integer.parseInt(element.get(name));
}

public short getShortByTag(Map<String, String> element, String name) {
return Short.parseShort(element.get(name));
}

public byte getByteByTag(Map<String, String> element, String name) {
return Byte.parseByte(element.get(name));
}

}[/code]

[code]public class ItemList {

private int id;
private String name;
private String examine;
private boolean stackable;
private int minPrice;
private int normPrice;
private int maxPrice;
private int classType;

public ItemList(int id, String name, String examine,
int minPrice, int normPrice, int maxPrice, int classType,
boolean stackable) {
this.id = id;
this.name = name;
this.examine = examine;
this.stackable = stackable;
this.minPrice = minPrice;
this.normPrice = normPrice;
this.maxPrice = maxPrice;
this.classType = classType;
}

public int getId() {
return id;
}

public String getName() {
return name;
}

public String getExamine() {
return examine;
}

public boolean isStackable() {
return stackable;
}

public int getMinPrice() {
return minPrice;
}

public int getNormPrice() {
return normPrice;
}

public int getMaxPrice() {
return maxPrice;
}

public int getClassType() {
return classType;
}

}[/code]

[code]public class ItemManager {

private static Map<Integer, ItemList> itemLists = new HashMap<Integer, ItemList>();

public ItemManager() {
loadItemXML();
}

public ItemList getList(int id) {
return itemLists.get(id);
}

public int getItemClassType(int itemId) {
ItemList list = itemLists.get(itemId);
return list != null ? list.getClassType() : -1;
}

public String getItemName(int itemId) {
ItemList list = itemLists.get(itemId);
return list != null ? list.getName() : "Item";
}

public boolean getStackable(int itemId) {
ItemList list = itemLists.get(itemId);
return list != null && list.isStackable();
}

public String getItemExamine(int itemId) {
ItemList list = itemLists.get(itemId);
return list != null ? list.getExamine() : "an Item.";
}

public int getItemShopMinValue(int itemId) {
ItemList list = itemLists.get(itemId);
return list != null ? list.getMinPrice() : 1;
}

public int getItemShopNormPrice(int itemId) {
ItemList list = itemLists.get(itemId);
return list != null ? list.getNormPrice() : 1;
}

public int getItemShopMaxPrice(int itemId) {
ItemList list = itemLists.get(itemId);
return list != null ? list.getMaxPrice() : 1;
}

public static void loadItemXML() {
try {
XMLParser itemInfoXML = new XMLParser("data/items/items.xml");
itemInfoXML.setByElement("Item");
for(int i = 0; i < itemInfoXML.getElements().size(); i++) {
Map<String, String> element = itemInfoXML.getElements().get(i);
int id =
itemInfoXML.getIntByTag(element, "Id");
String name =
itemInfoXML.getStringByTag(element, "Name");
String examine =
itemInfoXML.getStringByTag(element, "Examine");
boolean stackable =
Boolean.parseBoolean(itemInfoXML.getStringByTag(element, "Stackable"));
int minPrice =
itemInfoXML.getIntByTag(element, "MinPrice");
int normPrice =
itemInfoXML.getIntByTag(element, "NormPrice");
int maxPrice =
itemInfoXML.getIntByTag(element, "MaxPrice");
int classType =
itemInfoXML.getIntByTag(element, "ClassType");
itemLists.put(id, new ItemList(id, name, examine, classType,
minPrice, normPrice, maxPrice, stackable));
}
Constants.println("Item info loaded: "+itemLists.size());
} catch (Exception e) {
System.out.println("Error loading items!");
}
}

}[/code]

Item XML list
[code]<Item>
<Id>1</Id>
<Name>Health_Potion</Name>
<Examine>It's a health potion!</Examine>
<Stackable>true</Stackable>
<MinPrice>2</MinPrice>
<NormPrice>5</NormPrice>
<MaxPrice>10</MaxPrice>
<ClassType>0</ClassType>
</Item>
<Item>
<Id>2</Id>
<Name>Wooden_Sword</Name>
<Examine>Carved from a normal tree.</Examine>
<Stackable>false</Stackable>
<MinPrice>3</MinPrice>
<NormPrice>8</NormPrice>
<MaxPrice>13</MaxPrice>
<ClassType>0</ClassType>
</Item>[/code] Edited by Divine-X

Share this post


Link to post
Share on other sites
I don't exactly understand you. What is the issue? You can't save item data to the XML write, and want to write it to a text file instead?

Share this post


Link to post
Share on other sites
[quote name='Stacx']I don't exactly understand you. What is the issue? You can't save item data to the XML write, and want to write it to a text file instead?[/QUOTE]


Sorry I felt like I explained it wrong also. What I'm trying to do is I have items in my item.xml file, I want to be able to like do something like this [code]player.getInventory().addItem(IDHERE);[/code] and it'll add that item (which is the health potion) and it'll show in the players inventory as "Health_Potion". Then I want to be able to remove it when the player uses it like [code]player.getInventory().removeItem(IDHERE)[/code] the code doesn't have to be like that but it has to work the same way.

EDIT: And yeah, I don't know how to make it so that in the XML file will do this [code]<inventory><id>1</id></inventory>[/code] like RSPS. But I'm just going to stick with .txt file. Edited by Divine-X

Share this post


Link to post
Share on other sites
[quote name='Divine-X']Sorry I felt like I explained it wrong also. What I'm trying to do is I have items in my item.xml file, I want to be able to like do something like this [code]player.getInventory().addItem(IDHERE);[/code] and it'll add that item (which is the health potion) and it'll show in the players inventory as "Health_Potion". Then I want to be able to remove it when the player uses it like [code]player.getInventory().removeItem(IDHERE)[/code] the code doesn't have to be like that but it has to work the same way.

EDIT: And yeah, I don't know how to make it so that in the XML file it'll do this [code]<inventory><id>1</id></inventory>[/code] like RSPS. But I'm just going to stick with .txt file.[/QUOTE]

Well, let me assume that ItemList is an instance of an item. (change the name to Item though, this doesn't make sense)

You have a class that represents an inventory that encapsulates a [code]List<Item> inventory[/code].

Adding is just
[code]
void addItem(int itemId) {
inventory.add(ItemManager.getList(itemId);
}
[/code]
I don't exactly see the issue here, or I'm not properly understanding you here haha.

Share this post


Link to post
Share on other sites
[quote name='Stacx']Well, let me assume that ItemList is an instance of an item. (change the name to Item though, this doesn't make sense)

You have a class that represents an inventory that encapsulates a [code]List<Item> inventory[/code].

Adding is just
[code]
void addItem(int itemId) {
inventory.add(ItemManager.getList(itemId);
}
[/code]
I don't exactly see the issue here, or I'm not properly understanding you here haha.[/QUOTE]

Mmmmmmm, okay lol, I think I got how to explain it properly

I'm making a TBAG(omg I just wowowowow, Text Based Adventure Game), and I have some items and the items info a .xml file, I want to be able to have a method or something that'll do something like this

[code]player.getInventory().addItem(1);[/code] this will add the item Health_Potion to the players inventory BUT I don't want it show as the Number 1 in the players inventory, I want it to show as the items name.

If I can't figure out how to use the XML file as like my item database I'll just change how the game is really, make it a force story or something and I'll just create items by using the Item.java Constructor when they're needed

Share this post


Link to post
Share on other sites
[quote name='Divine-X']Mmmmmmm, okay lol, I think I got how to explain it properly

I'm making a TBAG(omg I just wowowowow, Text Based Adventure Game), and I have some items and the items info a .xml file, I want to be able to have a method or something that'll do something like this

[code]player.getInventory().addItem(1);[/code] this will add the item Health_Potion to the players inventory BUT I don't want it show as the Number 1 in the players inventory, I want it to show as the items name.

If I can't figure out how to use the XML file as like my item database I'll just change how the game is really, make it a force story or something and I'll just create items by using the Item.java Constructor when they're needed[/QUOTE]

Does it load the items? [code]Constants.println("Item info loaded: "+itemLists.size());[/code] what does this tell you?

Share this post


Link to post
Share on other sites
[quote name='Stacx']Does it load the items? [code]Constants.println("Item info loaded: "+itemLists.size());[/code] what does this tell you?[/QUOTE]

Current Date is: [2013/07/26 14:20:45]
Using Java Version: 1.6.0_43
OS: Windows 7
Item info loaded: 2
NPC info loaded: 1

Share this post


Link to post
Share on other sites
Well I'm pretty sure RSPS's use [URL="http://xstream.codehaus.org/"]XStream[/URL] so I would [COLOR=#252C2F][FONT=Helvetica]just use that. I'm kinda confused by what your problem is tho :\[/FONT][/COLOR]

Share this post


Link to post
Share on other sites
[quote name='Steve']Well I'm pretty sure RSPS's use [URL="http://xstream.codehaus.org/"]XStream[/URL] so I would [COLOR=#252C2F][FONT=Helvetica]just use that. I'm kinda confused by what your problem is tho :\[/FONT][/COLOR][/QUOTE]

I'm using XStream, not sure if I'm using it correctly though
[code]public class FileManager {

private FileManager() {
//Leave blank
}

private static XStream xstream = null;

public static XStream getXStream() {
if(xstream == null) {
xstream = new XStream();
xstream.alias("player", framework.game.player.Player.class);
}
return xstream;
}

public static void save(Player p) {
try {
XStream xst = FileManager.getXStream();
xst.toXML(p, new FileOutputStream(
"data/saves/"+p.getDetails().getUsername()+"/"+p.getDetails().getUsername()+".xml"));
} catch (Exception e) {
Constants.println("Error with XML Saving");
}
}[/code]

Like mm, I'm trying to make a text based adventure game inventory, and how I want the inventory to work is lets say the player comes across a random sword and I want to have a method that goes into my items.xml file and puts the text "Wooden_Sword" in their inventory instead of a number.

Ehh.. x-x like when I use this (it's just an example) [code]player.getInventory().addItem(2);[/code] the 2 is taking the spot of the Wooden_Sword's itemId which is written inside of the items.xml file. BUTT I don't want it to show in the players inventory likes this

[code]
Player Inventory:
2[/code]

I want it to show like this
[code]Player Inventory:
Wooden_Sword[/code]

My reason for why I put my items in an xml file is because I want to be able to just get the information I need from the XML and not do something like this everytime I want them to come across an item or w/e
[code]Item Wooden_Sword = new Item(0, null, null, 0, 0, 0, false);[/code]

Share this post


Link to post
Share on other sites
But... you are loading the data correctly. I don't see the problem o.o

[code][MENTION=15855]Over[/MENTION]ride
public String toString() {
return ItemManager.getItemName(this.id);
}
[/code]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×