Completed the addons additions.

Everything seems to work okay.
This commit is contained in:
Tastybento 2017-12-27 19:16:13 -08:00
parent f80a933c34
commit 687a71876d
29 changed files with 689 additions and 277 deletions

View File

@ -1,6 +1,7 @@
package us.tastybento.bskyblock;
import java.io.File;
import java.io.IOException;
import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;
@ -8,6 +9,7 @@ import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin;
import us.tastybento.bskyblock.api.BSBModule;
import us.tastybento.bskyblock.api.addons.event.AddOnDisableEvent;
import us.tastybento.bskyblock.commands.AdminCommand;
import us.tastybento.bskyblock.commands.IslandCommand;
import us.tastybento.bskyblock.config.PluginConfig;
@ -18,6 +20,7 @@ import us.tastybento.bskyblock.database.managers.island.IslandsManager;
import us.tastybento.bskyblock.generators.IslandWorld;
import us.tastybento.bskyblock.listeners.JoinLeaveListener;
import us.tastybento.bskyblock.listeners.PanelListener;
import us.tastybento.bskyblock.managers.AddOnManager;
import us.tastybento.bskyblock.managers.CommandsManager;
import us.tastybento.bskyblock.managers.LocalesManager;
import us.tastybento.bskyblock.util.Util;
@ -42,6 +45,7 @@ public class BSkyBlock extends JavaPlugin implements BSBModule {
// Managers
private CommandsManager commandsManager;
private LocalesManager localesManager;
private AddOnManager addonsManager;
@Override
public void onEnable(){
@ -100,6 +104,12 @@ public class BSkyBlock extends JavaPlugin implements BSBModule {
// Register Listeners
registerListeners();
// Load addons
addonsManager = new AddOnManager();
addonsManager.loadAddons();
/*
*DEBUG CODE
Island loadedIsland = islandsManager.getIsland(owner);
@ -137,6 +147,20 @@ public class BSkyBlock extends JavaPlugin implements BSBModule {
@Override
public void onDisable(){
// Unload addons
addonsManager.getAddons().forEach(addon -> {
addon.onDisable();
getServer().getPluginManager().callEvent(new AddOnDisableEvent(addon));
System.out.println("Disabling " + addon.getDescription().getName() + "...");
});
addonsManager.getLoader().forEach(loader -> {
try {
loader.close();
} catch (IOException e) {
e.printStackTrace();
}
});
// Save data
playersManager.shutdown();
islandsManager.shutdown();
@ -194,7 +218,7 @@ public class BSkyBlock extends JavaPlugin implements BSBModule {
return islandsManager;
}
public static BSkyBlock getPlugin() {
public static BSkyBlock getInstance() {
return plugin;
}

View File

@ -0,0 +1,167 @@
package us.tastybento.bskyblock.api.addons;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.logging.Logger;
import org.bukkit.Server;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.event.Listener;
import org.bukkit.plugin.java.JavaPlugin;
import us.tastybento.bskyblock.BSkyBlock;
/**
* Add-on class for BSkyBlock. Extend this to create an add-on.
* The operation and methods are very similar to Bukkit's JavaPlugin.
* @author ben, comminq
*
*/
public abstract class AddOn implements AddOnInterface {
private static final String ADDON_CONFIG_FILENAME = "config.yml";
private boolean enabled;
private AddOnDescription description;
private FileConfiguration config;
private File dataFolder;
private File file;
public AddOn() {
this.enabled = false;
}
public JavaPlugin getBSkyBlock(){
return BSkyBlock.getInstance();
}
public FileConfiguration getConfig() {
config = loadYamlFile(ADDON_CONFIG_FILENAME);
return config;
}
public File getDataFolder() {
return dataFolder;
}
public AddOnDescription getDescription() {
return description;
}
/**
* @return the file
*/
public File getFile() {
return file;
}
public Logger getLogger() {
return getBSkyBlock().getLogger();
}
public Server getServer() {
return getBSkyBlock().getServer();
}
public boolean isEnabled() {
return enabled;
}
private FileConfiguration loadYamlFile(String file) {
File yamlFile = new File(dataFolder, file);
YamlConfiguration config = null;
if (yamlFile.exists()) {
try {
config = new YamlConfiguration();
config.load(yamlFile);
} catch (Exception e) {
e.printStackTrace();
}
}
return config;
}
public void registerListener(Listener listener){
BSkyBlock.getInstance().getServer().getPluginManager().registerEvents(listener, BSkyBlock.getInstance());
}
public void saveDefaultConfig() {
saveResource(ADDON_CONFIG_FILENAME, true);
config = loadYamlFile(ADDON_CONFIG_FILENAME);
}
/**
* Saves a resource contained in this add-on's jar file.
* @param resourcePath
* @param replace
*/
public void saveResource(String resourcePath, boolean replace) {
if (resourcePath == null || resourcePath.equals("")) {
throw new IllegalArgumentException("ResourcePath cannot be null or empty");
}
resourcePath = resourcePath.replace('\\', '/');
InputStream in = null;
try {
JarFile jar = new JarFile(file);
JarEntry config = jar.getJarEntry(resourcePath);
if (config != null) {
in = jar.getInputStream(config);
}
if (in == null) {
jar.close();
throw new IllegalArgumentException("The embedded resource '" + resourcePath + "' cannot be found in " + jar.getName());
}
File outFile = new File(dataFolder, resourcePath);
int lastIndex = resourcePath.lastIndexOf('/');
File outDir = new File(dataFolder, resourcePath.substring(0, lastIndex >= 0 ? lastIndex : 0));
if (!outDir.exists()) {
outDir.mkdirs();
}
if (!outFile.exists() || replace) {
OutputStream out = new FileOutputStream(outFile);
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
out.close();
in.close();
} else {
getLogger().warning("Could not save " + outFile.getName() + " to " + outFile + " because " + outFile.getName() + " already exists.");
}
jar.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
public void setDataFolder(File file) {
this.dataFolder = file;
}
public void setDescription(AddOnDescription desc){
this.description = desc;
}
public void setEnabled(boolean enabled) {
this.enabled = enabled;
}
/**
* @param f the file to set
*/
public void setFile(File f) {
this.file = f;
}
}

View File

@ -0,0 +1,65 @@
package us.tastybento.bskyblock.api.addons;
import java.io.BufferedReader;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.Map;
import us.tastybento.bskyblock.api.addons.AddOnDescription.AddonDescriptionBuilder;
import us.tastybento.bskyblock.api.addons.exception.InvalidAddOnFormatException;
import us.tastybento.bskyblock.api.addons.exception.InvalidAddOnInheritException;
public class AddOnClassLoader extends URLClassLoader{
public AddOn addon;
public AddOnClassLoader(Map<String, String>data, File path, BufferedReader reader, ClassLoader loaders) throws InvalidAddOnInheritException, MalformedURLException, InvalidAddOnFormatException {
super(new URL[]{path.toURI().toURL()}, loaders);
AddOn addon = null;
Class<?> javaClass = null;
try {
//Bukkit.getLogger().info("data " + data.get("main"));
/*
for (Entry<String, String> en : data.entrySet()) {
Bukkit.getLogger().info(en.getKey() + " => " + en.getValue());
}*/
javaClass = Class.forName(data.get("main"), true, this);
if(data.get("main").contains("us.tastybento")){
throw new InvalidAddOnFormatException("Packages declaration cannot start with 'us.tastybento'");
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
Class<? extends AddOn> addonClass;
try{
addonClass = javaClass.asSubclass(AddOn.class);
}catch(ClassCastException e){
throw new InvalidAddOnInheritException("Main class doesn't not extends super class 'AddOn'");
}
try {
addon = addonClass.newInstance();
} catch (InstantiationException | IllegalAccessException e) {
e.printStackTrace();
}
addon.setDescription(this.asDescription(data));
this.addon = addon;
}
private AddOnDescription asDescription(Map<String, String> data){
String[] authors = data.get("authors").split("\\,");
return new AddonDescriptionBuilder(data.get("name"))
.withVersion(data.get("version"))
.withAuthor(authors).build();
}
}

View File

@ -0,0 +1,108 @@
package us.tastybento.bskyblock.api.addons;
import java.util.Arrays;
import java.util.List;
public final class AddOnDescription {
private String main;
private String name;
private String version;
private String description;
private List<String> authors;
public AddOnDescription() {}
public AddOnDescription(String main, String name, String version, String description, List<String> authors) {
this.main = main;
this.name = name;
this.version = version;
this.description = description;
this.authors = authors;
}
/**
* @param main the main to set
*/
public void setMain(String main) {
this.main = main;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @param version the version to set
*/
public void setVersion(String version) {
this.version = version;
}
/**
* @param description the description to set
*/
public void setDescription(String description) {
this.description = description;
}
/**
* @param authors the authors to set
*/
public void setAuthors(List<String> authors) {
this.authors = authors;
}
public String getName() {
return name;
}
public String getMain() {
return main;
}
public String getVersion() {
return version;
}
public String getDescription() {
return description;
}
public List<String> getAuthors() {
return authors;
}
public static class AddonDescriptionBuilder{
public AddOnDescription description;
public AddonDescriptionBuilder(String name){
description = new AddOnDescription();
description.setName(name);
}
public AddonDescriptionBuilder withAuthor(String... authors){
this.description.setAuthors(Arrays.asList(authors));
return this;
}
public AddonDescriptionBuilder withDescription(String desc){
this.description.setDescription(desc);
return this;
}
public AddonDescriptionBuilder withVersion(String version){
this.description.setVersion(version);
return this;
}
public AddOnDescription build(){
return this.description;
}
}
}

View File

@ -0,0 +1,7 @@
package us.tastybento.bskyblock.api.addons;
public interface AddOnInterface {
public abstract void onEnable();
public abstract void onDisable();
public default void onLoad() {};
}

View File

@ -1,40 +0,0 @@
package us.tastybento.bskyblock.api.addons;
import java.util.List;
public final class AddonDescription {
private String main;
private String name;
private String version;
private String description;
private List<String> authors;
public AddonDescription(String main, String name, String version, String description, List<String> authors) {
this.main = main;
this.name = name;
this.version = version;
this.description = description;
this.authors = authors;
}
public String getName() {
return name;
}
public String getMain() {
return main;
}
public String getVersion() {
return version;
}
public String getDescription() {
return description;
}
public List<String> getAuthors() {
return authors;
}
}

View File

@ -1,55 +0,0 @@
package us.tastybento.bskyblock.api.addons;
import java.io.File;
import us.tastybento.bskyblock.BSkyBlock;
import us.tastybento.bskyblock.api.BSBModule;
import us.tastybento.bskyblock.managers.CommandsManager;
import us.tastybento.bskyblock.managers.LocalesManager;
public abstract class BSBAddon implements BSBModule {
private File folder;
private AddonDescription description;
private AddonState state;
public abstract void enable();
public abstract void disable();
public abstract void load();
public abstract void reload();
public AddonDescription getDescription() {
return description;
}
public AddonState getState() { return state; }
public boolean isEnabled() { return state == AddonState.ENABLED; }
public boolean isDisabled() { return state == AddonState.DISABLED; }
public boolean isIncompatible() { return state == AddonState.INCOMPATIBLE; }
public CommandsManager getCommandsManager() {
return BSkyBlock.getPlugin().getCommandsManager();
}
public LocalesManager getLocalesManager() {
return BSkyBlock.getPlugin().getLocalesManager();
}
@Override
public final String getIdentifier() {
return getDescription().getName();
}
@Override
public final boolean isAddon() {
return true;
}
@Override
public final File getFolder() {
if (folder == null) {
folder = new File(BSkyBlock.getPlugin().getFolder() + "/addons/" + getIdentifier());
}
return folder;
}
}

View File

@ -1,62 +0,0 @@
package us.tastybento.bskyblock.api.addons;
import java.io.BufferedReader;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.Map;
import us.tastybento.bskyblock.api.module.Module;
import us.tastybento.bskyblock.api.module.ModuleDescription;
import us.tastybento.bskyblock.api.module.ModuleDescription.ModuleDescriptionBuilder;
import us.tastybento.bskyblock.api.module.exception.InvalidModuleFormatException;
import us.tastybento.bskyblock.api.module.exception.InvalidModuleInheritException;
public class ModuleClassLoader extends URLClassLoader{
public Module module;
public ModuleClassLoader(Map<String, String>data, File path, BufferedReader reader, ClassLoader loaders) throws InvalidModuleInheritException, MalformedURLException, InvalidModuleFormatException {
super(new URL[]{path.toURI().toURL()}, loaders);
Module module = null;
Class<?> javaClass = null;
try {
javaClass = Class.forName(data.get("Main"), true, this);
if(data.get("Main").contains("net.essence")){
throw new InvalidModuleFormatException("Packages declaration cannot start with 'net.essence'");
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
Class<? extends Module> moduleClass;
try{
moduleClass = javaClass.asSubclass(Module.class);
}catch(ClassCastException e){
throw new InvalidModuleInheritException("Main class doesn't not extends super class 'Module'");
}
try {
module = moduleClass.newInstance();
} catch (InstantiationException | IllegalAccessException e) {
e.printStackTrace();
}
module.setDescription(this.asDescription(data));
this.module = module;
}
private ModuleDescription asDescription(Map<String, String> data){
String[] authors = data.get("Author").split("\\,");
return new ModuleDescriptionBuilder(data.get("Name"))
.withVersion(data.get("Version"))
.withAuthor(authors).build();
}
}

View File

@ -0,0 +1,29 @@
package us.tastybento.bskyblock.api.addons.event;
import us.tastybento.bskyblock.api.addons.AddOn;
public class AddOnDisableEvent extends PremadeEvent{
/**
* @author ComminQ_Q
*
* Event active when a addon is being disabled
* // TODO You can disable addon IG
*
*/
private AddOn addon;
public AddOnDisableEvent(AddOn addon){
this.addon = addon;
}
public AddOn getAddon() {
return addon;
}
public void setAddon(AddOn addon) {
this.addon = addon;
}
}

View File

@ -0,0 +1,29 @@
package us.tastybento.bskyblock.api.addons.event;
import us.tastybento.bskyblock.api.addons.AddOn;
public class AddOnEnableEvent extends PremadeEvent{
/**
* @author ComminQ_Q
*
* Event active when a addon is being disabled
* // TODO You can enable Addon IG
*
*/
private AddOn addon;
public AddOnEnableEvent(AddOn addon){
this.addon = addon;
}
public AddOn getAddon() {
return addon;
}
public void setAddon(AddOn addon) {
this.addon = addon;
}
}

View File

@ -0,0 +1,21 @@
package us.tastybento.bskyblock.api.addons.event;
import us.tastybento.bskyblock.api.addons.AddOn;
public class AddOnLoadEvent extends PremadeEvent{
private AddOn addon;
public AddOnLoadEvent(AddOn addon){
this.addon = addon;
}
public AddOn getAddOn() {
return addon;
}
public void setAddOn(AddOn addon) {
this.addon = addon;
}
}

View File

@ -0,0 +1,19 @@
package us.tastybento.bskyblock.api.addons.event;
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;
public abstract class PremadeEvent extends Event{
public static final HandlerList handlers = new HandlerList();
@Override
public HandlerList getHandlers() {
return handlers;
}
public static HandlerList getHandleList(){
return handlers;
}
}

View File

@ -0,0 +1,14 @@
package us.tastybento.bskyblock.api.addons.exception;
public abstract class AddOnException extends Exception{
/**
*
*/
private static final long serialVersionUID = 4203162022348693854L;
public AddOnException(String errorMessage){
super("AddOnException : "+errorMessage);
}
}

View File

@ -0,0 +1,33 @@
package us.tastybento.bskyblock.api.addons.exception;
import java.util.logging.Level;
import org.bukkit.Bukkit;
public class InvalidAddOnFormatException extends AddOnException{
/**
*
*/
private static final long serialVersionUID = 7741502900847049986L;
public InvalidAddOnFormatException(String errorMessage) {
super(errorMessage);
}
@Override
public void printStackTrace(){
super.printStackTrace();
System.out.println("");
Bukkit.getLogger().log(Level.WARNING, " Basic format : (addon.yml)");
Bukkit.getLogger().log(Level.WARNING, " main: path.to.your.MainClass");
Bukkit.getLogger().log(Level.WARNING, " name: <NameOfYourModule>");
Bukkit.getLogger().log(Level.WARNING, " authors: <AuthorA> | <AuthorA, AuthorB>");
Bukkit.getLogger().log(Level.WARNING, " version: YourVersion");
}
}

View File

@ -0,0 +1,14 @@
package us.tastybento.bskyblock.api.addons.exception;
public class InvalidAddOnInheritException extends AddOnException{
/**
*
*/
private static final long serialVersionUID = -5847358994397613244L;
public InvalidAddOnInheritException(String errorMessage) {
super(errorMessage);
}
}

View File

@ -226,7 +226,7 @@ public abstract class CompositeCommand extends Command implements PluginIdentifi
@Override
public BSkyBlock getPlugin() {
return BSkyBlock.getPlugin();
return BSkyBlock.getInstance();
}
/**

View File

@ -73,7 +73,7 @@ public class User {
private final UUID playerUUID;
private final BSkyBlock plugin = BSkyBlock.getPlugin();
private final BSkyBlock plugin = BSkyBlock.getInstance();
private final CommandSender sender;

View File

@ -1,77 +0,0 @@
package us.tastybento.bskyblock.api.module;
import java.util.ArrayList;
import java.util.List;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.scheduler.BukkitRunnable;
import us.tastybento.bskyblock.api.module.command.CommandManager;
import us.tastybento.bskyblock.module.command.ModuleCommand;
import us.tastybento.bskyblock.module.event.ListenerManager;
import us.tastybento.bskyblock.module.event.ModuleListener;
public abstract class Module{
private ListenerManager listenerManager;
private boolean enabled;
private ModuleDescription description;
private List<ModuleCommand> registeredCommands;
public Module(){
this.enabled = false;
this.listenerManager = new ListenerManager();
this.registeredCommands = new ArrayList<>();
}
public abstract void Enable();
public abstract void Disable();
public abstract void Load();
public void registerListener(ModuleListener listener){
this.listenerManager.registerListener(listener);
}
public void registerCommand(ModuleCommand command){
CommandManager.registerCmd(command);
this.registeredCommands.add(command);
}
public JavaPlugin getUsedPlugin(){
return Main.getInstance();
}
public ListenerManager getListenerManager() {
return listenerManager;
}
public void setListenerManager(ListenerManager listenerManager) {
this.listenerManager = listenerManager;
}
public boolean isEnabled() {
return enabled;
}
public void setEnabled(boolean enabled) {
this.enabled = enabled;
}
public ModuleDescription getDescription() {
return description;
}
public void setDescription(ModuleDescription desc){
this.description = desc;
}
public List<ModuleCommand> getRegisteredCommands() {
return registeredCommands;
}
public void setRegisteredCommands(List<ModuleCommand> registeredCommands) {
this.registeredCommands = registeredCommands;
}
}

View File

@ -1,9 +0,0 @@
package us.tastybento.bskyblock.api.module;
public class ModuleDescription {
public class ModuleDescriptionBuilder {
}
}

View File

@ -1,5 +0,0 @@
package us.tastybento.bskyblock.api.module.exception;
public class InvalidModuleFormatException {
}

View File

@ -1,5 +0,0 @@
package us.tastybento.bskyblock.api.module.exception;
public class InvalidModuleInheritException {
}

View File

@ -24,7 +24,7 @@ public class IslandAboutCommand extends CompositeCommand {
@Override
public boolean execute(User user, List<String> args) {
user.sendLegacyMessage("About " + BSkyBlock.getPlugin().getDescription().getName() + " v" + BSkyBlock.getPlugin().getDescription().getVersion() + ":");
user.sendLegacyMessage("About " + BSkyBlock.getInstance().getDescription().getName() + " v" + BSkyBlock.getInstance().getDescription().getVersion() + ":");
user.sendLegacyMessage("Copyright (c) 2017 - 2018 tastybento, Poslovitch");
user.sendLegacyMessage("All rights reserved.");
user.sendLegacyMessage("");

View File

@ -24,7 +24,7 @@ import us.tastybento.bskyblock.util.Util;
public class IslandCache {
private static final boolean DEBUG2 = false;
private static final boolean DEBUG = false;
private BSkyBlock plugin = BSkyBlock.getPlugin();
private BSkyBlock plugin = BSkyBlock.getInstance();
private BiMap<Location, Island> islandsByLocation;
/**
* Every player who is associated with an island is in this map.

View File

@ -23,7 +23,7 @@ import us.tastybento.bskyblock.island.builders.IslandBuilder.IslandType;
*/
public class NewIsland {
private static final boolean DEBUG = false;
private final BSkyBlock plugin = BSkyBlock.getPlugin();
private final BSkyBlock plugin = BSkyBlock.getInstance();
private Island island;
private final Player player;
private final Reason reason;

View File

@ -14,7 +14,7 @@ import us.tastybento.bskyblock.BSkyBlock;
*
*/
public class InventorySave {
private static InventorySave instance = new InventorySave(BSkyBlock.getPlugin());
private static InventorySave instance = new InventorySave(BSkyBlock.getInstance());
private HashMap<UUID, InventoryStore> inventories;
/**

View File

@ -0,0 +1,150 @@
package us.tastybento.bskyblock.managers;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import org.bukkit.Bukkit;
import us.tastybento.bskyblock.BSkyBlock;
import us.tastybento.bskyblock.api.addons.AddOn;
import us.tastybento.bskyblock.api.addons.AddOnClassLoader;
import us.tastybento.bskyblock.api.addons.event.AddOnEnableEvent;
import us.tastybento.bskyblock.api.addons.event.AddOnLoadEvent;
import us.tastybento.bskyblock.api.addons.exception.InvalidAddOnFormatException;
import us.tastybento.bskyblock.api.addons.exception.InvalidAddOnInheritException;
public class AddOnManager {
private static final boolean DEBUG = false;
private List<AddOn> addons;
private List<AddOnClassLoader> loader;
public AddOnManager() {
this.addons = new ArrayList<>();
this.loader = new ArrayList<>();
}
/**
* Loads all the addons from the addons folder
*/
public void loadAddons() {
File f = new File(BSkyBlock.getInstance().getDataFolder(), "addons");
if (f.exists()) {
if (f.isDirectory()) {
for (File file : f.listFiles()) {
if (!file.isDirectory()) {
try {
this.loadAddon(file);
} catch (InvalidAddOnFormatException e) {
e.printStackTrace();
} catch (InvalidAddOnInheritException e) {
e.printStackTrace();
}
}
}
}
} else {
try {
f.mkdir();
} catch (SecurityException e) {
e.printStackTrace();
if (DEBUG) {
Bukkit.getLogger().severe("Cannot create folder 'addons' (Permission ?)");
}
}
}
this.addons.stream().forEach(addon -> {
addon.onEnable();
BSkyBlock.getInstance().getServer().getPluginManager().callEvent(new AddOnEnableEvent(addon));
addon.setEnabled(true);
BSkyBlock.getInstance().getLogger().info("Enabling " + addon.getDescription().getName() + "...");
});
}
public AddOn getAddonByName(String name){
if(name.equals("")) return null;
for(AddOn m : this.addons){
if(m.getDescription().getName().contains(name)) return m;
}
return null;
}
private void loadAddon(File f) throws InvalidAddOnFormatException, InvalidAddOnInheritException {
try {
AddOn addon = null;
if (!f.getName().contains(".jar")) {
return;
}
JarFile jar = new JarFile(f);
JarEntry entry = jar.getJarEntry("addon.yml");
if (entry == null) {
jar.close();
throw new InvalidAddOnFormatException("Addon doesn't contains description file");
}
BufferedReader reader = new BufferedReader(new InputStreamReader(jar.getInputStream(entry)));
Map<String, String> data = this.data(reader);
AddOnClassLoader loader = null;
loader = new AddOnClassLoader(data, f, reader, this.getClass().getClassLoader());
this.loader.add(loader);
addon = loader.addon;
addon.setDataFolder(new File(f.getParent(), f.getName().replace(".jar", "")));
addon.setFile(f);
AddOnLoadEvent event = new AddOnLoadEvent(addon);
Bukkit.getPluginManager().callEvent(event);
this.addons.add(addon);
addon.onLoad();
BSkyBlock.getInstance().getLogger().info("Loading BSkyBlock addon " + addon.getDescription().getName() + "...");
jar.close();
} catch (IOException e) {
if (DEBUG) {
BSkyBlock.getInstance().getLogger().info(f.getName() + "is not a jarfile, ignoring...");
}
}
}
private Map<String, String> data(BufferedReader reader) {
Map<String, String> map = new HashMap<>();
reader.lines().forEach(string -> {
if (DEBUG)
Bukkit.getLogger().info("DEBUG: " + string);
String[] data = string.split("\\: ");
if (data.length > 1) {
map.put(data[0], data[1].substring(0, data[1].length()));
}
});
return map;
}
public List<AddOn> getAddons() {
return addons;
}
public List<AddOnClassLoader> getLoader() {
return loader;
}
public void setLoader(List<AddOnClassLoader> loader) {
this.loader = loader;
}
}

View File

@ -1,15 +0,0 @@
package us.tastybento.bskyblock.managers;
import java.util.ArrayList;
import java.util.List;
import us.tastybento.bskyblock.api.addons.BSBAddon;
public final class AddonsManager {
private final List<BSBAddon> addons = new ArrayList<>();
public List<BSBAddon> getAddons() {
return addons;
}
}

View File

@ -22,7 +22,7 @@ public final class CommandsManager {
cmds.add(command);
commands.put(module, cmds);
BSkyBlock.getPlugin().getNMSHandler().getServerCommandMap().register(command.getLabel(), command);
BSkyBlock.getInstance().getNMSHandler().getServerCommandMap().register(command.getLabel(), command);
}
public void unregisterCommand(Command command) {

View File

@ -39,7 +39,7 @@ import us.tastybento.bskyblock.util.nms.NMSAbstraction;
* @author Poslovitch
*/
public class Util {
private static BSkyBlock plugin = BSkyBlock.getPlugin();
private static BSkyBlock plugin = BSkyBlock.getInstance();
private static String serverVersion = null;
private static NMSAbstraction nmsHandler = null;