Ejemplo n.º 1
0
  @SuppressWarnings("unchecked")
  @Override
  public ClientEntityPrefab load(InputStream in) {
    Engine engine = Spout.getEngine();
    if (!(engine instanceof Client)) {
      throw new IllegalStateException("Prefabs can only be loaded on the client.");
    }

    final Yaml yaml = new Yaml();
    final Map<? extends String, ?> resourceProperties = checkerMapStringObject.check(yaml.load(in));

    if (!(resourceProperties.containsKey("Name"))
        || !(resourceProperties.containsKey("Components"))
        || !(resourceProperties.containsKey("Data"))) {
      throw new IllegalStateException("A property is missing (Name, Components or Data)");
    }

    final Object name = resourceProperties.get("Name");
    if (!(name instanceof String)) {
      throw new IllegalStateException("Tried to load an entity prefab but wasn't given a name");
    }

    final List<? extends String> componentsPath =
        checkerListString.check(resourceProperties.get("Components"));
    final List<Class<? extends Component>> components = new ArrayList<Class<? extends Component>>();
    for (String path : componentsPath) {
      Class<?> componentClass;
      try {
        try {
          componentClass = CommonClassLoader.findPluginClass(path);
        } catch (ClassNotFoundException e) {
          componentClass = Class.forName(path);
        }
      } catch (ClassNotFoundException e) {
        throw new IllegalStateException("A component is missing: " + path);
      }
      if (EntityComponent.class.isAssignableFrom(componentClass)) {
        components.add((Class<? extends EntityComponent>) componentClass);
      } else {
        throw new IllegalStateException("This is not an entity component.");
      }
    }

    final Map<? extends String, ?> datasOld =
        checkerMapStringObject.check(resourceProperties.get("Data"));
    final Map<String, Object> datas = new HashMap<String, Object>();
    datas.putAll(datasOld);

    return new ClientEntityPrefab((Client) engine, (String) name, components, datas);
  }