Splitting Config String

Discussion in 'Plugin Development' started by Fl1pzta, Aug 25, 2013.

Thread Status:
Not open for further replies.
  1. Offline

    Fl1pzta

    So, I'm using this to create an entity and give it multiple metadatas(through config) once spawned. Everything worked find til I started using a seperator and began splitting everything, so Creeper spawned fine, but Creeper|metadatakey;value returns a nullpointerexception.

    MobData Class
    Code:java
    1. import java.util.Map;
    2.  
    3. import org.bukkit.entity.EntityType;
    4.  
    5. public class MobData {
    6. protected final EntityType type;
    7. protected Map<String,String> metadata = null;
    8. public MobData(String full){
    9. String[] splits = full.split("|");
    10. if(splits!=null){
    11. if(splits.length>1){
    12. String[] metas = splits[1].split(",");
    13. if(metas!=null){
    14. for(String thisMeta:metas){
    15. String[] metawithvalue = thisMeta.split(";");
    16. if(metawithvalue.length>1){
    17. metadata.put(metawithvalue[0], metawithvalue[1]);
    18. }
    19. }
    20. }
    21. }
    22.  
    23.  
    24. }
    25. this.type = EntityType.fromName(splits[0]);
    26. }
    27. public EntityType getEntityType(){
    28. return this.type;
    29. }
    30. public String getMetaValue(String string){
    31. return this.metadata.get(string);
    32. }
    33. public Map<String,String> getMetaData(){
    34. return this.metadata;
    35. }
    36. }


    SpawnLocation Class
    Code:java
    1. public class SpawnLocation extends BlockLocation {
    2.  
    3. private MobData creatureType;
    4.  
    5. public SpawnLocation(World world, int x, int y, int z) {
    6.  
    7. super(world, x, y, z);
    8.  
    9. }
    10.  
    11. public void setCreatureType(MobData mobData) {
    12.  
    13. this.creatureType = mobData;
    14.  
    15. }
    16.  
    17. public LivingEntity spawnCreature() {
    18. LivingEntity thisEntity = (LivingEntity) world.spawnEntity(new Location(world,x,y,z), creatureType.getEntityType());
    19. Map<String,String> metas = creatureType.getMetaData();
    20. if(metas!=null){
    21. for (Entry<String, String> entry : metas.entrySet()) {
    22. String key = entry.getKey();
    23. String value = entry.getValue();
    24. thisEntity.setMetadata(key, new FixedMetadataValue(Bukkit.getServer().getPluginManager().getPlugin("Quarantine"), value));
    25. }
    26. }
    27. return thisEntity;
    28. }
    29. }
    30.  


    Line 35: LivingEntity thisEntity = (LivingEntity) world.spawnEntity(new Location(world,x,y,z), creatureType.getEntityType());(NullPointerException)

    Some example strings that are instantiated as MobData(from config).
    mob_types:
    - 'Zombie|super_speed;true'(doesn't work)
    - 'Spider|displayname;Timone,super_speed;true'(doesn't work)
    - 'Skeleton'(works)
     
  2. Fl1pzta In java, use "\\|" if you want to split on "|"
     
  3. Offline

    Fl1pzta

    same with ":"?
     
  4. Fl1pzta Yes, for ":" just use "\\:"
     
  5. Offline

    Stoux

    To be honest, I do not get why people do this. Same issue as this one. Just make proper use of the YAML file instead of splitting strings & stuff.
     
  6. Offline

    Fl1pzta

    What are these considered? Reservered regex characters or something

    It feels quicker to me lol

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 4, 2016
Thread Status:
Not open for further replies.

Share This Page