-
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Expand file tree
/
Copy pathPluginInitializerManager.java
More file actions
177 lines (150 loc) · 8.15 KB
/
PluginInitializerManager.java
File metadata and controls
177 lines (150 loc) · 8.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package io.papermc.paper.plugin;
import com.mojang.logging.LogUtils;
import io.papermc.paper.configuration.PaperConfigurations;
import io.papermc.paper.plugin.entrypoint.Entrypoint;
import io.papermc.paper.plugin.entrypoint.LaunchEntryPointHandler;
import io.papermc.paper.plugin.provider.PluginProvider;
import io.papermc.paper.plugin.provider.type.paper.PaperPluginParent;
import io.papermc.paper.plugin.provider.type.spigot.SpigotPluginProvider;
import java.util.Set;
import java.util.TreeSet;
import joptsimple.OptionSet;
import net.minecraft.server.dedicated.DedicatedServer;
import org.bukkit.configuration.file.YamlConfiguration;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.slf4j.Logger;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
public class PluginInitializerManager {
private static final Logger LOGGER = LogUtils.getClassLogger();
private static PluginInitializerManager impl;
private final Path pluginDirectory;
private final Path updateDirectory;
PluginInitializerManager(final Path pluginDirectory, final Path updateDirectory) {
this.pluginDirectory = pluginDirectory;
this.updateDirectory = updateDirectory;
}
private static PluginInitializerManager parse(@NotNull final OptionSet minecraftOptionSet) throws Exception {
// We have to load the bukkit configuration inorder to get the update folder location.
final File configFileLocationBukkit = (File) minecraftOptionSet.valueOf("bukkit-settings");
final Path pluginDirectory = ((File) minecraftOptionSet.valueOf("plugins")).toPath();
final YamlConfiguration configuration = PaperConfigurations.loadLegacyConfigFile(configFileLocationBukkit);
final String updateDirectoryName = configuration.getString("settings.update-folder", "update");
if (updateDirectoryName.isBlank()) {
return new PluginInitializerManager(pluginDirectory, null);
}
final Path resolvedUpdateDirectory = pluginDirectory.resolve(updateDirectoryName);
if (!Files.isDirectory(resolvedUpdateDirectory)) {
if (Files.exists(resolvedUpdateDirectory)) {
LOGGER.error("Misconfigured update directory!");
LOGGER.error("Your configured update directory ({}) in bukkit.yml is pointing to a non-directory path. " +
"Auto updating functionality will not work.", resolvedUpdateDirectory);
}
return new PluginInitializerManager(pluginDirectory, null);
}
boolean isSameFile;
try {
isSameFile = Files.isSameFile(resolvedUpdateDirectory, pluginDirectory);
} catch (final IOException e) {
LOGGER.error("Misconfigured update directory!");
LOGGER.error("Failed to compare update/plugin directory", e);
return new PluginInitializerManager(pluginDirectory, null);
}
if (isSameFile) {
LOGGER.error("Misconfigured update directory!");
LOGGER.error(("Your configured update directory (%s) in bukkit.yml is pointing to the same location as the plugin directory (%s). " +
"Disabling auto updating functionality.").formatted(resolvedUpdateDirectory, pluginDirectory));
return new PluginInitializerManager(pluginDirectory, null);
}
return new PluginInitializerManager(pluginDirectory, resolvedUpdateDirectory);
}
public static PluginInitializerManager init(final OptionSet optionSet) throws Exception {
impl = parse(optionSet);
return impl;
}
public static PluginInitializerManager instance() {
return impl;
}
@NotNull
public Path pluginDirectoryPath() {
return pluginDirectory;
}
@Nullable
public Path pluginUpdatePath() {
return updateDirectory;
}
public static void load(OptionSet optionSet) throws Exception {
LOGGER.info("Initializing plugins...");
// We have to load the bukkit configuration inorder to get the update folder location.
io.papermc.paper.plugin.PluginInitializerManager pluginSystem = io.papermc.paper.plugin.PluginInitializerManager.init(optionSet);
// Register the default plugin directory
io.papermc.paper.plugin.util.EntrypointUtil.registerProvidersFromSource(io.papermc.paper.plugin.provider.source.DirectoryProviderSource.INSTANCE, pluginSystem.pluginDirectoryPath());
// Register plugins from the flag
@SuppressWarnings("unchecked")
java.util.List<Path> files = ((java.util.List<File>) optionSet.valuesOf("add-plugin")).stream().map(File::toPath).toList();
io.papermc.paper.plugin.util.EntrypointUtil.registerProvidersFromSource(io.papermc.paper.plugin.provider.source.PluginFlagProviderSource.INSTANCE, files);
@SuppressWarnings("unchecked")
java.util.List<Path> pluginList = ((java.util.List<File>) optionSet.valuesOf("add-plugin-dir")).stream()
.filter(java.util.Objects::nonNull)
.map(f -> f.listFiles(file -> file.getName().endsWith(".jar")))
.filter(java.util.Objects::nonNull)
.flatMap(java.util.Arrays::stream)
.filter(File::isFile)
.map(File::toPath)
.toList();
io.papermc.paper.plugin.util.EntrypointUtil.registerProvidersFromSource(io.papermc.paper.plugin.provider.source.PluginFlagProviderSource.INSTANCE, pluginList);
final Set<String> paperPluginNames = new TreeSet<>();
final Set<String> legacyPluginNames = new TreeSet<>();
LaunchEntryPointHandler.INSTANCE.getStorage().forEach((entrypoint, providerStorage) -> {
providerStorage.getRegisteredProviders().forEach(provider -> {
if (provider instanceof final SpigotPluginProvider legacy) {
legacyPluginNames.add(String.format("%s (%s)", legacy.getMeta().getName(), legacy.getMeta().getVersion()));
} else if (provider instanceof final PaperPluginParent.PaperServerPluginProvider paper) {
paperPluginNames.add(String.format("%s (%s)", provider.getMeta().getName(), provider.getMeta().getVersion()));
}
});
});
final int total = paperPluginNames.size() + legacyPluginNames.size();
LOGGER.info("Initialized {} plugin{}", total, total == 1 ? "" : "s");
if (!paperPluginNames.isEmpty()) {
if (LOGGER.isDebugEnabled()) {
LOGGER.info("Paper plugins ({}):\n - {}", paperPluginNames.size(), String.join("\n - ", paperPluginNames));
} else {
LOGGER.info("Paper plugins ({}):\n - {}", paperPluginNames.size(), String.join(", ", paperPluginNames));
}
}
if (!legacyPluginNames.isEmpty()) {
if (LOGGER.isDebugEnabled()) {
LOGGER.info("Bukkit plugins ({}):\n - {}", legacyPluginNames.size(), String.join("\n - ", legacyPluginNames));
} else {
LOGGER.info("Bukkit plugins ({}):\n - {}", legacyPluginNames.size(), String.join(", ", legacyPluginNames));
}
}
}
// This will be the end of me...
public static void reload(DedicatedServer dedicatedServer) {
// Wipe the provider storage
LaunchEntryPointHandler.INSTANCE.populateProviderStorage();
try {
load(dedicatedServer.options);
} catch (Exception e) {
throw new RuntimeException("Failed to reload!", e);
}
boolean hasPaperPlugin = false;
for (PluginProvider<?> provider : LaunchEntryPointHandler.INSTANCE.getStorage().get(Entrypoint.PLUGIN).getRegisteredProviders()) {
if (provider instanceof PaperPluginParent.PaperServerPluginProvider) {
hasPaperPlugin = true;
break;
}
}
if (hasPaperPlugin) {
LOGGER.warn("======== WARNING ========");
LOGGER.warn("You are reloading while having Paper plugins installed on your server.");
LOGGER.warn("Paper plugins do NOT support being reloaded. This will cause some unexpected issues.");
LOGGER.warn("=========================");
}
}
}