mirror of
https://github.com/DieReicheErethons/Brewery.git
synced 2024-11-15 10:25:15 +01:00
fix addon resources
This commit is contained in:
parent
457a4ec1f3
commit
82c1b3e3cf
@ -2,16 +2,14 @@ package com.dre.brewery.api.addons;
|
||||
|
||||
import com.dre.brewery.BreweryPlugin;
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.net.URL;
|
||||
import java.net.URLConnection;
|
||||
import java.nio.file.Files;
|
||||
import java.util.jar.JarEntry;
|
||||
import java.util.jar.JarInputStream;
|
||||
|
||||
public class AddonFileManager {
|
||||
private final static BreweryPlugin plugin = BreweryPlugin.getInstance();
|
||||
@ -21,39 +19,54 @@ public class AddonFileManager {
|
||||
private final File addonFolder;
|
||||
private final AddonLogger logger;
|
||||
private final File configFile;
|
||||
private final YamlConfiguration addonConfig;
|
||||
private YamlConfiguration addonConfig;
|
||||
private final File jarFile;
|
||||
|
||||
public AddonFileManager(BreweryAddon addon) {
|
||||
public AddonFileManager(BreweryAddon addon, File jarFile) {
|
||||
this.addon = addon;
|
||||
this.jarFile = jarFile;
|
||||
this.addonName = addon.getClass().getSimpleName();
|
||||
this.addonFolder = new File(plugin.getDataFolder().getAbsolutePath() + File.separator + "addons" + File.separator + addonName);
|
||||
this.logger = addon.getLogger();
|
||||
this.configFile = new File(addonFolder, addonName + ".yml");
|
||||
this.addonConfig = YamlConfiguration.loadConfiguration(configFile);
|
||||
this.addonConfig = configFile.exists() ? YamlConfiguration.loadConfiguration(configFile) : null;
|
||||
}
|
||||
|
||||
|
||||
public void generateFile(String fileName) {
|
||||
generateFile(new File(addonFolder, fileName));
|
||||
}
|
||||
public void generateFileAbsPath(String absolutePath) {
|
||||
generateFile(new File(absolutePath));
|
||||
}
|
||||
public void generateFile(File parent, String fileName) {
|
||||
generateFile(new File(parent, fileName));
|
||||
}
|
||||
public void generateFile(File file) {
|
||||
createAddonFolder();
|
||||
File file = new File(addonFolder, fileName);
|
||||
try {
|
||||
if (!file.exists()) {
|
||||
file.createNewFile();
|
||||
InputStream inputStream = getResource(fileName);
|
||||
if (inputStream != null) {
|
||||
OutputStream outputStream = Files.newOutputStream(file.toPath());
|
||||
byte[] buffer = new byte[1024];
|
||||
int bytesRead;
|
||||
while ((bytesRead = inputStream.read(buffer)) != -1) {
|
||||
outputStream.write(buffer, 0, bytesRead);
|
||||
try (JarInputStream jarInputStream = new JarInputStream(new FileInputStream(jarFile))) {
|
||||
JarEntry jarEntry;
|
||||
while ((jarEntry = jarInputStream.getNextJarEntry()) != null) {
|
||||
if (jarEntry.isDirectory() || !jarEntry.getName().equals(file.getName())) {
|
||||
continue;
|
||||
}
|
||||
OutputStream outputStream = Files.newOutputStream(file.toPath());
|
||||
byte[] buffer = new byte[1024];
|
||||
int bytesRead;
|
||||
while ((bytesRead = jarInputStream.read(buffer)) != -1) {
|
||||
outputStream.write(buffer, 0, bytesRead);
|
||||
}
|
||||
outputStream.flush();
|
||||
outputStream.close();
|
||||
break;
|
||||
}
|
||||
inputStream.close();
|
||||
outputStream.flush();
|
||||
outputStream.close();
|
||||
}
|
||||
}
|
||||
} catch (IOException ex) {
|
||||
logger.severe("Failed to generate file " + fileName, ex);
|
||||
logger.severe("Failed to generate file " + file.getName(), ex);
|
||||
}
|
||||
}
|
||||
|
||||
@ -71,29 +84,21 @@ public class AddonFileManager {
|
||||
|
||||
|
||||
public YamlConfiguration getAddonConfig() {
|
||||
generateAddonConfig();
|
||||
return addonConfig;
|
||||
}
|
||||
public void saveAddonConfig() {
|
||||
generateAddonConfig();
|
||||
try {
|
||||
addonConfig.save(configFile);
|
||||
} catch (IOException ex) {
|
||||
logger.severe("Failed to save addon config", ex);
|
||||
}
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public InputStream getResource(@NotNull String filename) {
|
||||
try {
|
||||
URL url = addon.getClass().getClassLoader().getResource(filename);
|
||||
if (url == null) {
|
||||
return null;
|
||||
} else {
|
||||
URLConnection connection = url.openConnection();
|
||||
connection.setUseCaches(false);
|
||||
return connection.getInputStream();
|
||||
}
|
||||
} catch (IOException var4) {
|
||||
return null;
|
||||
private void generateAddonConfig() {
|
||||
if (addonConfig == null) {
|
||||
generateFile(configFile);
|
||||
addonConfig = YamlConfiguration.loadConfiguration(configFile);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -20,7 +20,7 @@ import java.util.jar.JarEntry;
|
||||
import java.util.jar.JarInputStream;
|
||||
import java.util.logging.Level;
|
||||
|
||||
public class AddonManager {
|
||||
public class AddonManager extends ClassLoader {
|
||||
|
||||
private final BreweryPlugin plugin;
|
||||
private final File addonsFolder;
|
||||
@ -28,7 +28,7 @@ public class AddonManager {
|
||||
private final static List<AddonCommand> addonCommands = new ArrayList<>();
|
||||
|
||||
public AddonManager(BreweryPlugin plugin) {
|
||||
this.plugin = plugin;
|
||||
this.plugin = plugin;
|
||||
this.addonsFolder = new File(plugin.getDataFolder(), "addons");
|
||||
if (!addonsFolder.exists()) {
|
||||
addonsFolder.mkdirs();
|
||||
@ -66,7 +66,7 @@ public class AddonManager {
|
||||
Class<? extends BreweryAddon> addonClass = clazz.asSubclass(BreweryAddon.class);
|
||||
try {
|
||||
BreweryAddon addon = addonClass.getConstructor(BreweryPlugin.class, AddonLogger.class).newInstance(plugin, new AddonLogger(addonClass));
|
||||
addon.onAddonEnable(new AddonFileManager(addon));
|
||||
addon.onAddonEnable(new AddonFileManager(addon, file));
|
||||
addons.add(addon);
|
||||
} catch (Exception e) {
|
||||
plugin.getLogger().log(Level.SEVERE,"Failed to load addon class " + clazz.getSimpleName(), e);
|
||||
@ -107,9 +107,9 @@ public class AddonManager {
|
||||
}
|
||||
|
||||
public List<Class<?>> loadAllClassesFromJar(File jarFile) {
|
||||
|
||||
List<Class<?>> classes = new ArrayList<>();
|
||||
try (URLClassLoader classLoader = new URLClassLoader(new URL[]{jarFile.toURI().toURL()}, getClass().getClassLoader())) {
|
||||
|
||||
try (JarInputStream jarInputStream = new JarInputStream(new FileInputStream(jarFile))) {
|
||||
JarEntry jarEntry;
|
||||
while ((jarEntry = jarInputStream.getNextJarEntry()) != null) {
|
||||
@ -117,9 +117,11 @@ public class AddonManager {
|
||||
String className = jarEntry.getName().replaceAll("/", ".").replace(".class", "");
|
||||
try {
|
||||
Class<?> clazz = Class.forName(className, true, classLoader);
|
||||
if (BreweryAddon.class.isAssignableFrom(clazz)) {
|
||||
classes.add(clazz);
|
||||
if (!BreweryAddon.class.isAssignableFrom(clazz)) {
|
||||
continue;
|
||||
}
|
||||
classes.add(clazz);
|
||||
|
||||
} catch (ClassNotFoundException e) {
|
||||
plugin.getLogger().log(Level.SEVERE, "Failed to load class " + className, e);
|
||||
}
|
||||
|
@ -2,7 +2,7 @@ package com.dre.brewery.api.addons;
|
||||
|
||||
import com.dre.brewery.BreweryPlugin;
|
||||
|
||||
public abstract class BreweryAddon {
|
||||
public abstract class BreweryAddon extends ClassLoader {
|
||||
|
||||
protected final BreweryPlugin plugin;
|
||||
protected final AddonLogger logger;
|
||||
|
Loading…
Reference in New Issue
Block a user