Advertisement
james1bow

Untitled

Apr 15th, 2025
266
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.00 KB | None | 0 0
  1. package rpcore.races;
  2.  
  3. import net.risingworld.api.assets.TextureAsset;
  4. import rpcore.RPCore;
  5. import java.io.FileInputStream;
  6. import java.io.IOException;
  7. import java.util.*;
  8. import static net.risingworld.api.Internals.println;
  9.  
  10. public class Races {
  11.  
  12.     private RPCore rpCore;
  13.     private Properties raceProperties;
  14.     private HashMap<String, Race> raceClasses;
  15.     private String[] raceNames;
  16.  
  17.     public Races(RPCore rpCore) {
  18.         this.rpCore = rpCore;
  19.         raceClasses = new HashMap<>();
  20.         loadProps();
  21.     }
  22.  
  23.     public Race getRaceByName(String raceName){
  24.         return raceClasses.get(raceName);
  25.     }
  26.  
  27.     public HashMap<String, Race> getAllRaces(){
  28.         return raceClasses;
  29.     }
  30.  
  31.     public HashMap<String, String[]> getRaceInfo(String raceName){
  32.         Race race = raceClasses.get(raceName);
  33.         return race.raceInfo;
  34.     }
  35.  
  36.     public HashMap<String, TextureAsset> getAvatars(String raceName){
  37.         Race race = raceClasses.get(raceName);
  38.         return race.avatars;
  39.     }
  40.  
  41.     public String[] getAllRaceNames(){
  42.         return raceNames;
  43.     }
  44.  
  45.     private void loadProps(){
  46.  
  47.         raceProperties = new Properties();
  48.         try (FileInputStream input = new FileInputStream(rpCore.getPath()+"/Races/Races.properties")) {
  49.             raceProperties.load(input);
  50.             String[] racesToLoad = getArrayFromIndexedKeys(0, raceProperties, "race");
  51.             raceNames = racesToLoad;
  52.             println("[RPCore] "+String.valueOf(racesToLoad.length+" races found: "+ Arrays.toString(racesToLoad)), 10);
  53.             buildRaces(racesToLoad);
  54.         } catch (IOException e) {
  55.             println("Error Finding Race Data", 12);
  56.             throw new RuntimeException(e);
  57.         }
  58.     }
  59.  
  60.     private void buildRaces(String[] racesToLoad){
  61.         for(String raceName : racesToLoad){
  62.             Race newRace = new Race(rpCore, raceName);
  63.             String raceFile = rpCore.getPath()+"/Races/"+raceName+"/race.properties";
  64.             println("[RPCore] Found "+raceName+" Race Data",10);
  65.             Properties properties = new Properties();
  66.             String[] types;
  67.             try (FileInputStream input = new FileInputStream(raceFile)) {
  68.                 properties.load(input);
  69.                 types = getArrayFromIndexedKeys(0, properties, "type");
  70.                 HashMap<String, String[]> raceInfo = new HashMap<>();
  71.                 for(String type: types){
  72.                     String[] typeData = getArrayFromIndexedKeys(0, properties, type);
  73.                     raceInfo.put(type, typeData);
  74.                 }
  75.                 String[] avatarNames = getArrayFromIndexedKeys(1, properties, "avatar");
  76.                 println("[RPCore] Found "+String.valueOf(avatarNames.length)+" "+raceName+" Avatars",10);
  77.                 buildRaceCLass(newRace,avatarNames,raceName,raceInfo);
  78.             } catch (IOException e) {
  79.                 println("Error Building Races", 12);
  80.                 throw new RuntimeException(e);
  81.             }
  82.         }
  83.     }
  84.  
  85.     private void buildRaceCLass(Race newRace,String[] avatarNames, String raceName, HashMap<String, String[]> raceInfo){
  86.         HashMap<String, TextureAsset> avatars = new HashMap<>();
  87.         for(String textureName : avatarNames){
  88.             TextureAsset textureAsset = TextureAsset.loadFromFile(rpCore.getPath()+"/Races/"+raceName+"/Avatars/"+textureName);
  89.             avatars.put(textureName, textureAsset);
  90.         }
  91.         newRace.buildRace(avatars,raceInfo);
  92.         raceClasses.put(raceName, newRace);
  93.     }
  94.  
  95.     private static String[] getArrayFromIndexedKeys(Integer index, Properties properties, String keyPrefix) {
  96.         List<String> values = new ArrayList<>();
  97.         String currentKey = keyPrefix + "[" + index + "]";
  98.         while (properties.containsKey(currentKey)) {
  99.             values.add(properties.getProperty(currentKey));
  100.             index++;
  101.             currentKey = keyPrefix + "[" + index + "]";
  102.         }
  103.         return values.toArray(new String[0]);
  104.     }
  105.  
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement