无谓狗蛋
服务端后台在启动的时候loading插件的顺序
有什么规律或者按照什么排序来加载的呢?
求解答



strings
按照字母排序,具体和 windows 里按照名字排序差不多。

要更准确的话,得翻下源码。不过意义不大,插件的 plugin.yml 中,能控制加载顺序,能比前置加载完。

无谓狗蛋
もぺもぺ 发表于 2021-7-18 21:20
按照字母排序,具体和 windows 里按照名字排序差不多。

要更准确的话,得翻下源码。不过意义不大,插件的  ...

可是后台加载并不是按照ABCD的顺序加载的...p开头的也会在t开头的插件前面加载

strings
本帖最后由 もぺもぺ 于 2021-7-18 21:34 编辑
无谓狗蛋 发表于 2021-7-18 21:24
可是后台加载并不是按照ABCD的顺序加载的...p开头的也会在t开头的插件前面加载 ...

我怀疑这是一个 xy 问题,你的目的是什么。


能看到实际调用的是 directory.listFiles(),而这个的顺序可以理解为乱序,顺序由操作系统提供
https://stackoverflow.com/questi ... t-by-java-listfiles
  1.   @NotNull
  2.   public Plugin[] loadPlugins(@NotNull File directory) {
  3.     Validate.notNull(directory, "Directory cannot be null");
  4.     Validate.isTrue(directory.isDirectory(), "Directory must be a directory");
  5.     List<Plugin> result = new ArrayList<>();
  6.     Set<Pattern> filters = this.fileAssociations.keySet();
  7.     if (!this.server.getUpdateFolder().equals(""))
  8.       this.updateDirectory = new File(directory, this.server.getUpdateFolder());
  9.     Map<String, File> plugins = new HashMap<>();
  10.     Set<String> loadedPlugins = new HashSet<>();
  11.     Map<String, String> pluginsProvided = new HashMap<>();
  12.     Map<String, Collection<String>> dependencies = new HashMap<>();
  13.     Map<String, Collection<String>> softDependencies = new HashMap<>();
  14.     for (File file : directory.listFiles()) {
  15.       PluginLoader loader = null;
  16.       for (Pattern filter : filters) {
  17.         Matcher match = filter.matcher(file.getName());
  18.         if (match.find())
  19.           loader = this.fileAssociations.get(filter);
  20.       }
  21.       if (loader != null) {
  22.         PluginDescriptionFile description = null;
  23.         try {
  24.           description = loader.getPluginDescription(file);
  25.           String name = description.getName();
  26.           if (name.equalsIgnoreCase("bukkit") || name.equalsIgnoreCase("minecraft") || name.equalsIgnoreCase("mojang")) {
  27.             this.server.getLogger().log(Level.SEVERE, "Could not load '" + file.getPath() + "' in folder '" + directory.getPath() + "': Restricted Name");
  28.           } else if (description.rawName.indexOf(' ') != -1) {
  29.             this.server.getLogger().log(Level.SEVERE, "Could not load '" + file.getPath() + "' in folder '" + directory.getPath() + "': uses the space-character (0x20) in its name");
  30.           } else {
  31.             File replacedFile = plugins.put(description.getName(), file);
  32.             if (replacedFile != null)
  33.               this.server.getLogger().severe(String.format("Ambiguous plugin name `%s' for files `%s' and `%s' in `%s'", new Object[] { description
  34.                      
  35.                       .getName(), file
  36.                       .getPath(), replacedFile
  37.                       .getPath(), directory
  38.                       .getPath() }));
  39.             String removedProvided = pluginsProvided.remove(description.getName());
  40.             if (removedProvided != null)
  41.               this.server.getLogger().warning(String.format("Ambiguous plugin name `%s'. It is also provided by `%s'", new Object[] { description
  42.                      
  43.                       .getName(), removedProvided }));
  44.             for (String provided : description.getProvides()) {
  45.               File pluginFile = plugins.get(provided);
  46.               if (pluginFile != null) {
  47.                 this.server.getLogger().warning(String.format("`%s provides `%s' while this is also the name of `%s' in `%s'", new Object[] { file
  48.                         
  49.                         .getPath(), provided, pluginFile
  50.                         
  51.                         .getPath(), directory
  52.                         .getPath() }));
  53.                 continue;
  54.               }
  55.               String replacedPlugin = pluginsProvided.put(provided, description.getName());
  56.               if (replacedPlugin != null)
  57.                 this.server.getLogger().warning(String.format("`%s' is provided by both `%s' and `%s'", new Object[] { provided, description
  58.                         
  59.                         .getName(), replacedPlugin }));
  60.             }
  61.             Collection<String> softDependencySet = description.getSoftDepend();
  62.             if (softDependencySet != null && !softDependencySet.isEmpty()) {
  63.               if (softDependencies.containsKey(description.getName())) {
  64.                 ((Collection<String>)softDependencies.get(description.getName())).addAll(softDependencySet);
  65.               } else {
  66.                 softDependencies.put(description.getName(), new LinkedList<>(softDependencySet));
  67.               }
  68.               for (String depend : softDependencySet)
  69.                 this.dependencyGraph.putEdge(description.getName(), depend);
  70.             }
  71.             Collection<String> dependencySet = description.getDepend();
  72.             if (dependencySet != null && !dependencySet.isEmpty()) {
  73.               dependencies.put(description.getName(), new LinkedList<>(dependencySet));
  74.               for (String depend : dependencySet)
  75.                 this.dependencyGraph.putEdge(description.getName(), depend);
  76.             }
  77.             Collection<String> loadBeforeSet = description.getLoadBefore();
  78.             if (loadBeforeSet != null && !loadBeforeSet.isEmpty())
  79.               for (String loadBeforeTarget : loadBeforeSet) {
  80.                 if (softDependencies.containsKey(loadBeforeTarget)) {
  81.                   ((Collection<String>)softDependencies.get(loadBeforeTarget)).add(description.getName());
  82.                 } else {
  83.                   Collection<String> shortSoftDependency = new LinkedList<>();
  84.                   shortSoftDependency.add(description.getName());
  85.                   softDependencies.put(loadBeforeTarget, shortSoftDependency);
  86.                 }
  87.                 this.dependencyGraph.putEdge(loadBeforeTarget, description.getName());
  88.               }  
  89.           }
  90.         } catch (InvalidDescriptionException ex) {
  91.           this.server.getLogger().log(Level.SEVERE, "Could not load '" + file.getPath() + "' in folder '" + directory.getPath() + "'", ex);
  92.         }
  93.       }
  94.     }
  95.     while (!plugins.isEmpty()) {
  96.       boolean missingDependency = true;
  97.       Iterator<Map.Entry<String, File>> pluginIterator = plugins.entrySet().iterator();
  98.       while (pluginIterator.hasNext()) {
  99.         Map.Entry<String, File> entry = pluginIterator.next();
  100.         String plugin = entry.getKey();
  101.         if (dependencies.containsKey(plugin)) {
  102.           Iterator<String> dependencyIterator = ((Collection<String>)dependencies.get(plugin)).iterator();
  103.           while (dependencyIterator.hasNext()) {
  104.             String dependency = dependencyIterator.next();
  105.             if (loadedPlugins.contains(dependency)) {
  106.               dependencyIterator.remove();
  107.               continue;
  108.             }
  109.             if (!plugins.containsKey(dependency) && !pluginsProvided.containsKey(dependency)) {
  110.               missingDependency = false;
  111.               pluginIterator.remove();
  112.               softDependencies.remove(plugin);
  113.               dependencies.remove(plugin);
  114.               this.server.getLogger().log(Level.SEVERE, "Could not load '" + ((File)entry
  115.                   
  116.                   .getValue()).getPath() + "' in folder '" + directory.getPath() + "'", new UnknownDependencyException("Unknown dependency " + dependency + ". Please download and install " + dependency + " to run this plugin."));
  117.               break;
  118.             }
  119.           }
  120.           if (dependencies.containsKey(plugin) && ((Collection)dependencies.get(plugin)).isEmpty())
  121.             dependencies.remove(plugin);
  122.         }
  123.         if (softDependencies.containsKey(plugin)) {
  124.           Iterator<String> softDependencyIterator = ((Collection<String>)softDependencies.get(plugin)).iterator();
  125.           while (softDependencyIterator.hasNext()) {
  126.             String softDependency = softDependencyIterator.next();
  127.             if (!plugins.containsKey(softDependency) && !pluginsProvided.containsKey(softDependency))
  128.               softDependencyIterator.remove();
  129.           }
  130.           if (((Collection)softDependencies.get(plugin)).isEmpty())
  131.             softDependencies.remove(plugin);
  132.         }
  133.         if (!dependencies.containsKey(plugin) && !softDependencies.containsKey(plugin) && plugins.containsKey(plugin)) {
  134.           File file = plugins.get(plugin);
  135.           pluginIterator.remove();
  136.           missingDependency = false;
  137.           try {
  138.             Plugin loadedPlugin = loadPlugin(file);
  139.             if (loadedPlugin != null) {
  140.               result.add(loadedPlugin);
  141.               loadedPlugins.add(loadedPlugin.getName());
  142.               loadedPlugins.addAll(loadedPlugin.getDescription().getProvides());
  143.               continue;
  144.             }
  145.             this.server.getLogger().log(Level.SEVERE, "Could not load '" + file.getPath() + "' in folder '" + directory.getPath() + "'");
  146.           } catch (InvalidPluginException ex) {
  147.             this.server.getLogger().log(Level.SEVERE, "Could not load '" + file.getPath() + "' in folder '" + directory.getPath() + "'", ex);
  148.           }
  149.         }
  150.       }
  151.       if (missingDependency) {
  152.         pluginIterator = plugins.entrySet().iterator();
  153.         while (pluginIterator.hasNext()) {
  154.           Map.Entry<String, File> entry = pluginIterator.next();
  155.           String plugin = entry.getKey();
  156.           if (!dependencies.containsKey(plugin)) {
  157.             softDependencies.remove(plugin);
  158.             missingDependency = false;
  159.             File file = entry.getValue();
  160.             pluginIterator.remove();
  161.             try {
  162.               Plugin loadedPlugin = loadPlugin(file);
  163.               if (loadedPlugin != null) {
  164.                 result.add(loadedPlugin);
  165.                 loadedPlugins.add(loadedPlugin.getName());
  166.                 loadedPlugins.addAll(loadedPlugin.getDescription().getProvides());
  167.                 break;
  168.               }
  169.               this.server.getLogger().log(Level.SEVERE, "Could not load '" + file.getPath() + "' in folder '" + directory.getPath() + "'");
  170.               break;
  171.             } catch (InvalidPluginException ex) {
  172.               this.server.getLogger().log(Level.SEVERE, "Could not load '" + file.getPath() + "' in folder '" + directory.getPath() + "'", ex);
  173.             }
  174.           }
  175.         }
  176.         if (missingDependency) {
  177.           softDependencies.clear();
  178.           dependencies.clear();
  179.           Iterator<File> failedPluginIterator = plugins.values().iterator();
  180.           while (failedPluginIterator.hasNext()) {
  181.             File file = failedPluginIterator.next();
  182.             failedPluginIterator.remove();
  183.             this.server.getLogger().log(Level.SEVERE, "Could not load '" + file.getPath() + "' in folder '" + directory.getPath() + "': circular dependency detected");
  184.           }
  185.         }
  186.       }
  187.     }
  188.     return result.<Plugin>toArray(new Plugin[result.size()]);
  189.   }
  190.   
复制代码

PercyDan
首先是按文件名,然后soft-depend会先加载

无谓狗蛋
もぺもぺ 发表于 2021-7-18 21:30
我怀疑这是一个 xy 问题,你的目的是什么。

纯纯好奇,长知识
好的,谢谢您

第一页 上一页 下一页 最后一页