Solved [Advanced] Can't get ressource from jar.

Discussion in 'Plugin Development' started by InflamedSebi, May 28, 2013.

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

    InflamedSebi

    hay,

    I try to set the default for my localization file ...

    Code:java
    1.  
    2. public boolean setDefaults(FileConfiguration fileConfiguration, String file) {
    3. // Look for defaults in the jar
    4. InputStream defConfigStream =
    5. if (defConfigStream != null) {
    6. YamlConfiguration defConfig = YamlConfiguration.loadConfiguration(defConfigStream);
    7. fileConfiguration.setDefaults(defConfig);
    8. return true;
    9. }
    10. return false;
    11. }
    12.  


    the defaults are saved in:
    "plugin.jar/localization/en.yml"

    but plugin.getResource(file); returns null ...

    ------ EDIT

    but if i place the en.yml in the root folder it works just fine ... so the path itself seems to be the problem ...

    ------ SOLUTION

    Code:java
    1. InputStream in = plugin.getResource(resourcePath);

    can't find the file, but:
    Code:java
    1. resourcePath = resourcePath.replace('\\', '/');
    2. InputStream in = plugin.getResource(resourcePath);

    can ...
    strange but works now :)
     
  2. Offline

    CaptainBern Retired Staff

    Try to use: urclass.class.getResourceAsStream();
     
  3. Offline

    InflamedSebi

    nope still the same result ... seems like the getRessource(path) cant get the file ... and i dont know why ..
     
  4. Offline

    CaptainBern Retired Staff

    In a plugin I made I use this to get an image:
    Main.class.getResourceAsStream("images/alchemy.png");
     
  5. Offline

    ferrybig

    can you verify with winrar is the output file contains the yml file you trying to access?
     
  6. Offline

    InflamedSebi

    ya this should work but i dont know why it does not ...

    ya, the jar contains it ... I can even copy it to my plugins datafolder with my saveDefaultConfig("localization/en.yml");

    but i cant get it with the getResource("localization/en.yml");


    I dont get it ... if i put the file in the root folder it works with setDefaults(fileconfig, "en.yml"); ...
    but if i put the en.yml in a subfolder it does not work anymore setDefaults(fileconfig, "localization/en.yml");

    i dont get it ... what is wrong with "localization/"?
     
  7. Offline

    ferrybig

    may I see your source from saveDefaultConfig(String);
     
  8. Offline

    stirante

    I use this:
    Code:
            if (!new File(plugin.getDataFolder(), "plugin.lang").exists()) plugin.saveResource("plugin.lang", false);
            lang = YamlConfiguration.loadConfiguration(new File(plugin.getDataFolder(), "plugin.lang"));
            FileConfiguration defaults = YamlConfiguration.loadConfiguration(plugin.getResource("plugin.lang"));
            lang.addDefaults(defaults);
    EDIT:Oops, Haven't read your edit in OP :p
     
  9. Offline

    InflamedSebi

    Code:java
    1. public void saveDefaultConfig(String fileName) {
    2. File file = new File(plugin.getDataFolder(), fileName);
    3. if (!file.exists()) {
    4. plugin.saveResource(fileName, false);
    5. }
    6. }
    7.  


    the .saveResource(fileName, false); is a bukkit method like the .getRessource() ...
     
  10. Offline

    Wingzzz

    Okay .saveResource(...) takes two parameters: String- the resource's path, and boolean- if true, the embedded resource will overwrite the contents of an existing file. To make sure this method will work, you can test by using getResource(String filename), and if it can't retrieve the resource, it obviously can't save it. So basically to use saveResource(...) method you can't have it in a folder (unless you specify its full path- not ideal when using say a Yaml class that holds the file's name). To put resources in other folders I'd suggest using Input/Output streams.

    Simple copy method:
    Code:java
    1. private void copy(InputStream in, File file) {
    2. try {
    3. OutputStream out = new FileOutputStream(file);
    4. byte[] buf = new byte[1024];
    5. int len;
    6. while((len = in.read(buf)) > 0) {
    7. out.write(buf, 0, len);
    8. }
    9. out.close();
    10. in.close();
    11. } catch(Exception exc) {
    12. exc.printStackTrace();
    13. }
    14. }
     
  11. Offline

    InflamedSebi

    Sure i read the source of bukkit but why does:
    plugin.saveResource("localisation/en.yml", false);
    work just fine, while
    plugin.getResource("localisation/en.yml");
    returns null ... if u look into the
    .saveResource(..)
    Method:

    Code:java
    1.  
    2. public void saveResource(String resourcePath, boolean replace) {
    3. if (resourcePath == null || resourcePath.equals("")) {
    4. throw new IllegalArgumentException("ResourcePath cannot be null or empty");
    5. }
    6.  
    7. resourcePath = resourcePath.replace('\\', '/');
    8. InputStream in = getResource(resourcePath);
    9. if (in == null) {
    10. throw new IllegalArgumentException("The embedded resource '" + resourcePath + "' cannot be found in " + file);
    11. }
    12.  
    13. File outFile = new File(dataFolder, resourcePath);
    14. int lastIndex = resourcePath.lastIndexOf('/');
    15. File outDir = new File(dataFolder, resourcePath.substring(0, lastIndex >= 0 ? lastIndex : 0));
    16.  
    17. if (!outDir.exists()) {
    18. outDir.mkdirs();
    19. }
    20.  
    21. try {
    22. if (!outFile.exists() || replace) {
    23. OutputStream out = new FileOutputStream(outFile);
    24. byte[] buf = new byte[1024];
    25. int len;
    26. while ((len = in.read(buf)) > 0) {
    27. out.write(buf, 0, len);
    28. }
    29. out.close();
    30. in.close();
    31. } else {
    32. logger.log(Level.WARNING, "Could not save " + outFile.getName() + " to " + outFile + " because " + outFile.getName()
    33. + " already exists.");
    34. }
    35. } catch (IOException ex) {
    36. logger.log(Level.SEVERE, "Could not save " + outFile.getName() + " to " + outFile, ex);
    37. }
    38. }


    it also uses the getRessource(...) and it seems to work there ...

    WTF!? I found the solution ...

    [sytax=java]InputStream in = plugin.getResource(resourcePath);[/syntax]
    can't find the file, but:
    [sytax=java]resourcePath = resourcePath.replace('\\', '/');
    InputStream in = plugin.getResource(resourcePath);[/syntax]
    can ...
    strange but works now :)

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

Share This Page