How to get subpackages of a package

Discussion in 'Plugin Development' started by audioXD, Jun 24, 2016.

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

    audioXD

    My plugin some reflect fetures and so I was wondering if someone can help. Pls.
     
  2. Offline

    Zombie_Striker

    @audioXD
    What is your issue? Are you asking how to access another class from another package?
     
  3. Offline

    audioXD

    No how to get the sub package name.
     
  4. Online

    timtower Administrator Administrator Moderator

    @audioXD It is easier if you could tell what you are trying to achieve and what you have already.
    There might be better ways.
     
  5. Offline

    audioXD

    Yes I could write all the strings manualy but I thought i could do it dynamicaly.
    Exacty what i want to do is to get the sub packade name with the version.
    Plugim GitHub
    Now I'm uasing reflection to use abstraction(create a NSM) and I want it to get the closes version.

    Example:
    Avaliable versions are v1_8, v1_8_8 and 1_9_10_R2
    If the version is v1_8, v1_8_1,... it will use v1_8
    If the version is v1_9 it won't use any version


    Note: Package.getPackages() won't list the packages that I need
     
  6. Online

    timtower Administrator Administrator Moderator

    @audioXD I have a similar thing.
    Just put in the entire package name besides the version number, or loop through all classes in the jar and check if they extend / implement ... class
     
  7. Offline

    audioXD

    Thanks

    The result for those thatare curius
    Code:
    Set<String> supportedVersions = new HashSet<>();
            try {
                ZipInputStream zip = new ZipInputStream(new FileInputStream(this.getFile().getPath()));
                String prefix = this.getClass().getPackage().getName() + ".abst.";
    
                for(ZipEntry entry = zip.getNextEntry(); entry != null; entry = zip.getNextEntry()) {
                    if(!entry.isDirectory()) continue;
    
                    String path = entry.getName().replace(File.separatorChar, '.').replace('/', '.');
                    if(!path.startsWith(prefix)) continue;
    
                    String version = path.replaceFirst(path.substring(0, prefix.length()), "");
                    version = version.split("\\\\.")[0].replace('.', ' ').trim();
                    if(version.length() == 0) continue;
                    if(version.startsWith("api")) continue;
    
                    supportedVersions.add(version);
                }
                zip.close();
            } catch(Exception e) {}
            return supportedVersions.toArray(new String[supportedVersions.size()]);
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
Thread Status:
Not open for further replies.

Share This Page