资源到url java

j5fpnvbx  于 2021-07-03  发布在  Java
关注(0)|答案(1)|浏览(317)

所以你肯定知道我的游戏。你可以通过forge添加mod。有一个mod是a非常想要的,但它在启动时给了我一个错误:
属性异常组件:'simplengrammodel'属性:'location'-错误的url c:\users\samuel\desktop\multimc\instances\gfwg\minecraft\config\spells.lmunknown协议:c
虽然我绝对想要这个mod,而且我认为开发人员已经放弃了我想要的项目,在发现witch函数使我的minecraft崩溃后反编译mod,但我真的不知道出了什么问题(我不是一个大的java开发人员):
edu/cmu/sphinx/util/props/configurationmanagerutils.class

public static URL getResource(String name, PropertySheet ps)
    throws PropertyException
  {
    String location = ps.getString(name);
    if (location == null) {
      throw new InternalConfigurationException(ps.getInstanceName(), name, "Required resource property '" + name + "' not set");
    }
    try
    {
      URL url = resourceToURL(location);
      if (url == null) {
        throw new InternalConfigurationException(ps.getInstanceName(), name, "Can't locate " + location);
      }
      return url;
    }
    catch (MalformedURLException e)
    {
      throw new InternalConfigurationException(e, ps.getInstanceName(), name, "Bad URL " + location + e.getMessage());
    }
  }

  static final Pattern jarPattern = Pattern.compile("resource:(.*)", 2);

  public static URL resourceToURL(String location)
    throws MalformedURLException
  {
    Matcher jarMatcher = jarPattern.matcher(location);
    if (jarMatcher.matches())
    {
      String resourceName = jarMatcher.group(1);
      return ConfigurationManagerUtils.class.getResource(resourceName);
    }
    if (location.indexOf(':') == -1) {
      location = "file:" + location;
    }
    return new URL(location);
  }

为了寻求帮助,如果我把我的雷击机移到另一个磁盘(i://),我得到这个错误
属性异常组件:'simplengrammodel'属性:'location'-错误的url i:\multimc\instances\gfwg\minecraft\config\spells.lmunknown协议:i
原始mod线程:http://www.minecraftforum.net/forums/mapping-and-modding/minecraft-mods/2692177-forge-hp-spells-cast-spells-with-your-voice?page=7
附言:对不起,我错了

t8e9dugd

t8e9dugd1#

if (location.indexOf(':') == -1) {
  location = "file:" + location;
}

把它改成

if (location.indexOf(':') != -1) {
  location = "file:///" + location.replace('\\', '/');
}

如果这也失败了,另一种解决方案可能是

if (location.indexOf(':') != -1) {
  File f = new File(location);
  return f.toURI().toURL();
}

有一个 toURL 中的方法 java.io.File 同样,但它是不推荐的,我避免使用不推荐的方法。

相关问题