Remove unnecessary methods

This commit is contained in:
Roman Zhuravlev 2025-09-03 19:30:11 +05:00
parent bcdf6d510d
commit c95619439d

View file

@ -2,6 +2,9 @@ package net.frankheijden.serverutils.bukkit.managers;
import java.io.Closeable; import java.io.Closeable;
import java.io.File; import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
@ -12,8 +15,8 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Optional; import java.util.Optional;
import java.util.Set; import java.util.Set;
import java.util.regex.Matcher; import java.util.jar.JarEntry;
import java.util.regex.Pattern; import java.util.jar.JarFile;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import net.frankheijden.serverutils.bukkit.entities.BukkitPluginDescription; import net.frankheijden.serverutils.bukkit.entities.BukkitPluginDescription;
import net.frankheijden.serverutils.bukkit.events.BukkitPluginDisableEvent; import net.frankheijden.serverutils.bukkit.events.BukkitPluginDisableEvent;
@ -41,9 +44,11 @@ import org.bukkit.command.PluginIdentifiableCommand;
import org.bukkit.plugin.InvalidDescriptionException; import org.bukkit.plugin.InvalidDescriptionException;
import org.bukkit.plugin.InvalidPluginException; import org.bukkit.plugin.InvalidPluginException;
import org.bukkit.plugin.Plugin; import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.PluginDescriptionFile;
import org.bukkit.plugin.PluginLoader; import org.bukkit.plugin.PluginLoader;
import org.bukkit.plugin.PluginManager; import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.UnknownDependencyException; import org.bukkit.plugin.UnknownDependencyException;
import org.yaml.snakeyaml.error.YAMLException;
public class BukkitPluginManager extends AbstractPluginManager<Plugin, BukkitPluginDescription> { public class BukkitPluginManager extends AbstractPluginManager<Plugin, BukkitPluginDescription> {
@ -348,45 +353,43 @@ public class BukkitPluginManager extends AbstractPluginManager<Plugin, BukkitPlu
return knownCommands.get(command); return knownCommands.get(command);
} }
/**
* Retrieves all file associations, i.e. all plugin loaders.
* @return A map with all pluginloaders.
*/
public static Map<Pattern, PluginLoader> getFileAssociations() {
try {
return RSimplePluginManager.getFileAssociations(Bukkit.getPluginManager());
} catch (IllegalAccessException ex) {
ex.printStackTrace();
return null;
}
}
/**
* Retrieves the PluginLoader for the input file.
* @param file The file.
* @return The appropiate PluginLoader.
*/
public static Optional<PluginLoader> getPluginLoader(File file) {
Map<Pattern, PluginLoader> fileAssociations = getFileAssociations();
if (fileAssociations != null) {
for (Map.Entry<Pattern, PluginLoader> entry : fileAssociations.entrySet()) {
Matcher match = entry.getKey().matcher(file.getName());
if (match.find()) {
return Optional.ofNullable(entry.getValue());
}
}
}
return Optional.empty();
}
@Override @Override
public Optional<BukkitPluginDescription> getPluginDescription(File file) throws InvalidPluginDescriptionException { public Optional<BukkitPluginDescription> getPluginDescription(File file) throws InvalidPluginDescriptionException {
if (!file.exists()) return Optional.empty(); if (!file.exists()) return Optional.empty();
Optional<PluginLoader> loader = getPluginLoader(file);
if (!loader.isPresent()) throw new InvalidPluginDescriptionException("Plugin loader is not present!");
try { try {
return Optional.of(new BukkitPluginDescription(loader.get().getPluginDescription(file), file)); JarFile jar = null;
InputStream stream = null;
try {
jar = new JarFile(file);
JarEntry entry = jar.getJarEntry("plugin.yml");
if (entry == null) {
throw new InvalidDescriptionException(new FileNotFoundException("Jar does not contain plugin.yml"));
}
stream = jar.getInputStream(entry);
return Optional.of(new BukkitPluginDescription(new PluginDescriptionFile(stream), file));
} catch (IOException | YAMLException ex) {
throw new InvalidDescriptionException(ex);
} finally {
if (jar != null) {
try {
jar.close();
} catch (IOException ignored) {
// we do nothing
}
}
if (stream != null) {
try {
stream.close();
} catch (IOException ignored) {
// we do nothing
}
}
}
} catch (InvalidDescriptionException ex) { } catch (InvalidDescriptionException ex) {
throw new InvalidPluginDescriptionException(ex); throw new InvalidPluginDescriptionException(ex);
} }