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

按照字母排序,具体和 windows 里按照名字排序差不多。
要更准确的话,得翻下源码。不过意义不大,插件的 plugin.yml 中,能控制加载顺序,能比前置加载完。
要更准确的话,得翻下源码。不过意义不大,插件的 plugin.yml 中,能控制加载顺序,能比前置加载完。
もぺもぺ 发表于 2021-7-18 21:20
按照字母排序,具体和 windows 里按照名字排序差不多。
要更准确的话,得翻下源码。不过意义不大,插件的 ...
可是后台加载并不是按照ABCD的顺序加载的...p开头的也会在t开头的插件前面加载

本帖最后由 もぺもぺ 于 2021-7-18 21:34 编辑
我怀疑这是一个 xy 问题,你的目的是什么。
能看到实际调用的是 directory.listFiles(),而这个的顺序可以理解为乱序,顺序由操作系统提供
https://stackoverflow.com/questi ... t-by-java-listfiles
复制代码
无谓狗蛋 发表于 2021-7-18 21:24
可是后台加载并不是按照ABCD的顺序加载的...p开头的也会在t开头的插件前面加载 ...
我怀疑这是一个 xy 问题,你的目的是什么。
能看到实际调用的是 directory.listFiles(),而这个的顺序可以理解为乱序,顺序由操作系统提供
https://stackoverflow.com/questi ... t-by-java-listfiles
- @NotNull
- public Plugin[] loadPlugins(@NotNull File directory) {
- Validate.notNull(directory, "Directory cannot be null");
- Validate.isTrue(directory.isDirectory(), "Directory must be a directory");
- List<Plugin> result = new ArrayList<>();
- Set<Pattern> filters = this.fileAssociations.keySet();
- if (!this.server.getUpdateFolder().equals(""))
- this.updateDirectory = new File(directory, this.server.getUpdateFolder());
- Map<String, File> plugins = new HashMap<>();
- Set<String> loadedPlugins = new HashSet<>();
- Map<String, String> pluginsProvided = new HashMap<>();
- Map<String, Collection<String>> dependencies = new HashMap<>();
- Map<String, Collection<String>> softDependencies = new HashMap<>();
- for (File file : directory.listFiles()) {
- PluginLoader loader = null;
- for (Pattern filter : filters) {
- Matcher match = filter.matcher(file.getName());
- if (match.find())
- loader = this.fileAssociations.get(filter);
- }
- if (loader != null) {
- PluginDescriptionFile description = null;
- try {
- description = loader.getPluginDescription(file);
- String name = description.getName();
- if (name.equalsIgnoreCase("bukkit") || name.equalsIgnoreCase("minecraft") || name.equalsIgnoreCase("mojang")) {
- this.server.getLogger().log(Level.SEVERE, "Could not load '" + file.getPath() + "' in folder '" + directory.getPath() + "': Restricted Name");
- } else if (description.rawName.indexOf(' ') != -1) {
- this.server.getLogger().log(Level.SEVERE, "Could not load '" + file.getPath() + "' in folder '" + directory.getPath() + "': uses the space-character (0x20) in its name");
- } else {
- File replacedFile = plugins.put(description.getName(), file);
- if (replacedFile != null)
- this.server.getLogger().severe(String.format("Ambiguous plugin name `%s' for files `%s' and `%s' in `%s'", new Object[] { description
-
- .getName(), file
- .getPath(), replacedFile
- .getPath(), directory
- .getPath() }));
- String removedProvided = pluginsProvided.remove(description.getName());
- if (removedProvided != null)
- this.server.getLogger().warning(String.format("Ambiguous plugin name `%s'. It is also provided by `%s'", new Object[] { description
-
- .getName(), removedProvided }));
- for (String provided : description.getProvides()) {
- File pluginFile = plugins.get(provided);
- if (pluginFile != null) {
- this.server.getLogger().warning(String.format("`%s provides `%s' while this is also the name of `%s' in `%s'", new Object[] { file
-
- .getPath(), provided, pluginFile
-
- .getPath(), directory
- .getPath() }));
- continue;
- }
- String replacedPlugin = pluginsProvided.put(provided, description.getName());
- if (replacedPlugin != null)
- this.server.getLogger().warning(String.format("`%s' is provided by both `%s' and `%s'", new Object[] { provided, description
-
- .getName(), replacedPlugin }));
- }
- Collection<String> softDependencySet = description.getSoftDepend();
- if (softDependencySet != null && !softDependencySet.isEmpty()) {
- if (softDependencies.containsKey(description.getName())) {
- ((Collection<String>)softDependencies.get(description.getName())).addAll(softDependencySet);
- } else {
- softDependencies.put(description.getName(), new LinkedList<>(softDependencySet));
- }
- for (String depend : softDependencySet)
- this.dependencyGraph.putEdge(description.getName(), depend);
- }
- Collection<String> dependencySet = description.getDepend();
- if (dependencySet != null && !dependencySet.isEmpty()) {
- dependencies.put(description.getName(), new LinkedList<>(dependencySet));
- for (String depend : dependencySet)
- this.dependencyGraph.putEdge(description.getName(), depend);
- }
- Collection<String> loadBeforeSet = description.getLoadBefore();
- if (loadBeforeSet != null && !loadBeforeSet.isEmpty())
- for (String loadBeforeTarget : loadBeforeSet) {
- if (softDependencies.containsKey(loadBeforeTarget)) {
- ((Collection<String>)softDependencies.get(loadBeforeTarget)).add(description.getName());
- } else {
- Collection<String> shortSoftDependency = new LinkedList<>();
- shortSoftDependency.add(description.getName());
- softDependencies.put(loadBeforeTarget, shortSoftDependency);
- }
- this.dependencyGraph.putEdge(loadBeforeTarget, description.getName());
- }
- }
- } catch (InvalidDescriptionException ex) {
- this.server.getLogger().log(Level.SEVERE, "Could not load '" + file.getPath() + "' in folder '" + directory.getPath() + "'", ex);
- }
- }
- }
- while (!plugins.isEmpty()) {
- boolean missingDependency = true;
- Iterator<Map.Entry<String, File>> pluginIterator = plugins.entrySet().iterator();
- while (pluginIterator.hasNext()) {
- Map.Entry<String, File> entry = pluginIterator.next();
- String plugin = entry.getKey();
- if (dependencies.containsKey(plugin)) {
- Iterator<String> dependencyIterator = ((Collection<String>)dependencies.get(plugin)).iterator();
- while (dependencyIterator.hasNext()) {
- String dependency = dependencyIterator.next();
- if (loadedPlugins.contains(dependency)) {
- dependencyIterator.remove();
- continue;
- }
- if (!plugins.containsKey(dependency) && !pluginsProvided.containsKey(dependency)) {
- missingDependency = false;
- pluginIterator.remove();
- softDependencies.remove(plugin);
- dependencies.remove(plugin);
- this.server.getLogger().log(Level.SEVERE, "Could not load '" + ((File)entry
-
- .getValue()).getPath() + "' in folder '" + directory.getPath() + "'", new UnknownDependencyException("Unknown dependency " + dependency + ". Please download and install " + dependency + " to run this plugin."));
- break;
- }
- }
- if (dependencies.containsKey(plugin) && ((Collection)dependencies.get(plugin)).isEmpty())
- dependencies.remove(plugin);
- }
- if (softDependencies.containsKey(plugin)) {
- Iterator<String> softDependencyIterator = ((Collection<String>)softDependencies.get(plugin)).iterator();
- while (softDependencyIterator.hasNext()) {
- String softDependency = softDependencyIterator.next();
- if (!plugins.containsKey(softDependency) && !pluginsProvided.containsKey(softDependency))
- softDependencyIterator.remove();
- }
- if (((Collection)softDependencies.get(plugin)).isEmpty())
- softDependencies.remove(plugin);
- }
- if (!dependencies.containsKey(plugin) && !softDependencies.containsKey(plugin) && plugins.containsKey(plugin)) {
- File file = plugins.get(plugin);
- pluginIterator.remove();
- missingDependency = false;
- try {
- Plugin loadedPlugin = loadPlugin(file);
- if (loadedPlugin != null) {
- result.add(loadedPlugin);
- loadedPlugins.add(loadedPlugin.getName());
- loadedPlugins.addAll(loadedPlugin.getDescription().getProvides());
- continue;
- }
- this.server.getLogger().log(Level.SEVERE, "Could not load '" + file.getPath() + "' in folder '" + directory.getPath() + "'");
- } catch (InvalidPluginException ex) {
- this.server.getLogger().log(Level.SEVERE, "Could not load '" + file.getPath() + "' in folder '" + directory.getPath() + "'", ex);
- }
- }
- }
- if (missingDependency) {
- pluginIterator = plugins.entrySet().iterator();
- while (pluginIterator.hasNext()) {
- Map.Entry<String, File> entry = pluginIterator.next();
- String plugin = entry.getKey();
- if (!dependencies.containsKey(plugin)) {
- softDependencies.remove(plugin);
- missingDependency = false;
- File file = entry.getValue();
- pluginIterator.remove();
- try {
- Plugin loadedPlugin = loadPlugin(file);
- if (loadedPlugin != null) {
- result.add(loadedPlugin);
- loadedPlugins.add(loadedPlugin.getName());
- loadedPlugins.addAll(loadedPlugin.getDescription().getProvides());
- break;
- }
- this.server.getLogger().log(Level.SEVERE, "Could not load '" + file.getPath() + "' in folder '" + directory.getPath() + "'");
- break;
- } catch (InvalidPluginException ex) {
- this.server.getLogger().log(Level.SEVERE, "Could not load '" + file.getPath() + "' in folder '" + directory.getPath() + "'", ex);
- }
- }
- }
- if (missingDependency) {
- softDependencies.clear();
- dependencies.clear();
- Iterator<File> failedPluginIterator = plugins.values().iterator();
- while (failedPluginIterator.hasNext()) {
- File file = failedPluginIterator.next();
- failedPluginIterator.remove();
- this.server.getLogger().log(Level.SEVERE, "Could not load '" + file.getPath() + "' in folder '" + directory.getPath() + "': circular dependency detected");
- }
- }
- }
- }
- return result.<Plugin>toArray(new Plugin[result.size()]);
- }
首先是按文件名,然后soft-depend会先加载