Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package rpcore.races;
- import net.risingworld.api.assets.TextureAsset;
- import rpcore.RPCore;
- import java.io.FileInputStream;
- import java.io.IOException;
- import java.util.*;
- import static net.risingworld.api.Internals.println;
- public class Races {
- private RPCore rpCore;
- private Properties raceProperties;
- private HashMap<String, Race> raceClasses;
- private String[] raceNames;
- public Races(RPCore rpCore) {
- this.rpCore = rpCore;
- raceClasses = new HashMap<>();
- loadProps();
- }
- public Race getRaceByName(String raceName){
- return raceClasses.get(raceName);
- }
- public HashMap<String, Race> getAllRaces(){
- return raceClasses;
- }
- public HashMap<String, String[]> getRaceInfo(String raceName){
- Race race = raceClasses.get(raceName);
- return race.raceInfo;
- }
- public HashMap<String, TextureAsset> getAvatars(String raceName){
- Race race = raceClasses.get(raceName);
- return race.avatars;
- }
- public String[] getAllRaceNames(){
- return raceNames;
- }
- private void loadProps(){
- raceProperties = new Properties();
- try (FileInputStream input = new FileInputStream(rpCore.getPath()+"/Races/Races.properties")) {
- raceProperties.load(input);
- String[] racesToLoad = getArrayFromIndexedKeys(0, raceProperties, "race");
- raceNames = racesToLoad;
- println("[RPCore] "+String.valueOf(racesToLoad.length+" races found: "+ Arrays.toString(racesToLoad)), 10);
- buildRaces(racesToLoad);
- } catch (IOException e) {
- println("Error Finding Race Data", 12);
- throw new RuntimeException(e);
- }
- }
- private void buildRaces(String[] racesToLoad){
- for(String raceName : racesToLoad){
- Race newRace = new Race(rpCore, raceName);
- String raceFile = rpCore.getPath()+"/Races/"+raceName+"/race.properties";
- println("[RPCore] Found "+raceName+" Race Data",10);
- Properties properties = new Properties();
- String[] types;
- try (FileInputStream input = new FileInputStream(raceFile)) {
- properties.load(input);
- types = getArrayFromIndexedKeys(0, properties, "type");
- HashMap<String, String[]> raceInfo = new HashMap<>();
- for(String type: types){
- String[] typeData = getArrayFromIndexedKeys(0, properties, type);
- raceInfo.put(type, typeData);
- }
- String[] avatarNames = getArrayFromIndexedKeys(1, properties, "avatar");
- println("[RPCore] Found "+String.valueOf(avatarNames.length)+" "+raceName+" Avatars",10);
- buildRaceCLass(newRace,avatarNames,raceName,raceInfo);
- } catch (IOException e) {
- println("Error Building Races", 12);
- throw new RuntimeException(e);
- }
- }
- }
- private void buildRaceCLass(Race newRace,String[] avatarNames, String raceName, HashMap<String, String[]> raceInfo){
- HashMap<String, TextureAsset> avatars = new HashMap<>();
- for(String textureName : avatarNames){
- TextureAsset textureAsset = TextureAsset.loadFromFile(rpCore.getPath()+"/Races/"+raceName+"/Avatars/"+textureName);
- avatars.put(textureName, textureAsset);
- }
- newRace.buildRace(avatars,raceInfo);
- raceClasses.put(raceName, newRace);
- }
- private static String[] getArrayFromIndexedKeys(Integer index, Properties properties, String keyPrefix) {
- List<String> values = new ArrayList<>();
- String currentKey = keyPrefix + "[" + index + "]";
- while (properties.containsKey(currentKey)) {
- values.add(properties.getProperty(currentKey));
- index++;
- currentKey = keyPrefix + "[" + index + "]";
- }
- return values.toArray(new String[0]);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement