Loads weapon attack and defence animations from an XML file using xStream
Code:
package org.hyperion.util.xstream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.hyperion.rs2.Constants;
import org.hyperion.util.xstream.impl.CombatData;
/**
* Loads Combat Data
* @author crystalcasltes
*/
public class CombatDataLoader {
private static Map<Integer, CombatData> combatData = new HashMap<Integer, CombatData>();
/**
* Looks up the Weapon data
* @param id The weapon ID
* @return The ID
*/
public static CombatData lookup(int id) {
return combatData.get(id);
}
/**
* Loads the combat data XML
* @throws FileNotFoundException File not found
*/
@SuppressWarnings("unchecked")
public static void init() throws FileNotFoundException {
List<CombatData> data = (List<CombatData>) xStreamManager.load(new FileInputStream("./server_data/CombatData.xml"));
Iterator<CombatData> it = data.iterator();
while (it.hasNext()) {
CombatData cdata = it.next();
for (int id : cdata.getWeaponID()) {
combatData.put(id, cdata);
}
}
}
}
Code:
package org.hyperion.util.xstream.impl;
/**
* Handles all the Combat Data
* @author vhince
*/
public class CombatData {
/**
* The Weapon ID
*/
private int[] WEAPON_ID;
/**
* The Attack Animation
*/
private int ATTACK_ANIMATION;
/**
* The Defence Animation
*/
private int DEFENCE_ANIMATION;
/**
* Combat Data
* @param WEAPON_ID, the Weapon ID
* @param ANIMATION_ID, the Attacking Animation
* @param DEFENCE_ANIMATION, the Defence Animation
*/
public CombatData(int[] WEAPON_ID, int ATTACK_ANIMATION, int DEFENCE_ANIMATION) {
this.WEAPON_ID = WEAPON_ID;
this.ATTACK_ANIMATION = ATTACK_ANIMATION;
this.DEFENCE_ANIMATION = DEFENCE_ANIMATION;
}
/**
* Gets the Weapon ID
* @return The Weapon ID.
*/
public int[] getWeaponID() {
return WEAPON_ID;
}
/**
* Gets the Attack Animation ID
* @return The Attack Animation ID.
*/
public int getAttackAnimation() {
return ATTACK_ANIMATION;
}
/**
* Gets the Defence Animation ID
* @return The Defence Animation ID.
*/
public int getDefenceAnimation() {
return DEFENCE_ANIMATION;
}
}
Code:
<?xml version="1.0" encoding="UTF-8"?>
<!--
Document : CombatData.xml
Created on : April 26, 2011, 12:15 AM
Author : vhince
Description:
Loads all Weapon Attack Animations and Defence Animations
-->
<list>
<CombatData>
<WEAPON_ID>
<int>### here</int>
<int>### Another Item here</int>
</WEAPON_ID>
<ATTACK_ANIMATION>#### Attack animation</ATTACK_ANIMATION>
<DEFENCE_ANIMATION>#### Defence animation</DEFENCE_ANIMATION>
</CombatData>
<CombatData>
<!-- To make a different weapon
</CombatData>
</list>
Don't forget to load it
Code:
CombatDataLoader.init();
And add this
Code:
xstream.alias("CombatData", CombatData.class);
Usage:
Code:
public static int getAttackAnim(Entity entity) {
Player player = (Player) entity;
int id = player.getEquipment().get(Equipment.SLOT_WEAPON).getId();
return CombatDataLoader.lookup(id).getAttackAnimation();
}