Fixed bug where worlds were not existing when islands loaded.

If islands are loaded before the world exists the island's world becomes
null. If an addon is creating an island then it must be loaded before
islands.

Also refactored some of the addon loading code.
This commit is contained in:
tastybento 2018-05-29 16:59:52 -07:00
parent 3b7b7fa557
commit 6b6ec4c697
7 changed files with 47 additions and 79 deletions

View File

@ -4,7 +4,6 @@ import org.bukkit.World;
import org.bukkit.plugin.PluginManager; import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
import us.tastybento.bskyblock.api.addons.Addon;
import us.tastybento.bskyblock.api.configuration.WorldSettings; import us.tastybento.bskyblock.api.configuration.WorldSettings;
import us.tastybento.bskyblock.api.placeholders.PlaceholderHandler; import us.tastybento.bskyblock.api.placeholders.PlaceholderHandler;
import us.tastybento.bskyblock.api.user.Notifier; import us.tastybento.bskyblock.api.user.Notifier;
@ -113,27 +112,26 @@ public class BSkyBlock extends JavaPlugin {
new IslandCommand(); new IslandCommand();
new AdminCommand(); new AdminCommand();
// Locales manager must be loaded before addons
getServer().getScheduler().runTask(instance, () -> {
// Load Flags
flagsManager = new FlagsManager(instance);
// Load islands from database
islandsManager.load();
localesManager = new LocalesManager(instance); localesManager = new LocalesManager(instance);
PlaceholderHandler.register(instance); PlaceholderHandler.register(instance);
// Register Listeners // Load addons. Addons may load worlds, so they must go before islands are loaded.
registerListeners();
// Load addons
addonsManager = new AddonsManager(instance); addonsManager = new AddonsManager(instance);
addonsManager.loadAddons(); addonsManager.loadAddons();
// Enable addons // Enable addons
addonsManager.enableAddons(); addonsManager.enableAddons();
getServer().getScheduler().runTask(instance, () -> {
// Load Flags
flagsManager = new FlagsManager(instance);
// Register Listeners
registerListeners();
// Load islands from database - need to wait until all the worlds are loaded
islandsManager.load();
// Save islands & players data asynchronously every X minutes // Save islands & players data asynchronously every X minutes
getSettings().setDatabaseBackupPeriod(10 * 60 * 20); getSettings().setDatabaseBackupPeriod(10 * 60 * 20);
instance.getServer().getScheduler().runTaskTimer(instance, () -> { instance.getServer().getScheduler().runTaskTimer(instance, () -> {

View File

@ -104,9 +104,9 @@ public class AddonClassLoader extends URLClassLoader {
* @param checkGlobal * @param checkGlobal
* @return Class * @return Class
*/ */
public Class<?> findClass(String name, boolean checkGlobal) throws ClassNotFoundException { public Class<?> findClass(String name, boolean checkGlobal) {
if (name.startsWith("us.tastybento.")) { if (name.startsWith("us.tastybento.")) {
throw new ClassNotFoundException(name); return null;
} }
Class<?> result = classes.get(name); Class<?> result = classes.get(name);
if (result == null) { if (result == null) {
@ -114,7 +114,11 @@ public class AddonClassLoader extends URLClassLoader {
result = loader.getClassByName(name); result = loader.getClassByName(name);
} }
if (result == null) { if (result == null) {
try {
result = super.findClass(name); result = super.findClass(name);
} catch (ClassNotFoundException e) {
return null;
}
if (result != null) { if (result != null) {
loader.setClass(name, result); loader.setClass(name, result);
} }

View File

@ -44,6 +44,7 @@ public class BSBDatabase<T> {
} catch (InstantiationException | IllegalAccessException | IllegalArgumentException } catch (InstantiationException | IllegalAccessException | IllegalArgumentException
| InvocationTargetException | ClassNotFoundException | IntrospectionException e) { | InvocationTargetException | ClassNotFoundException | IntrospectionException e) {
logger.severe(() -> "Could not load objects from database! Error: " + e.getMessage()); logger.severe(() -> "Could not load objects from database! Error: " + e.getMessage());
e.printStackTrace();
} }
return result; return result;
} }

View File

@ -470,6 +470,9 @@ public class Island implements DataObject {
* @param center the center to set * @param center the center to set
*/ */
public void setCenter(Location center) { public void setCenter(Location center) {
if (center != null) {
this.world = center.getWorld();
}
this.center = center; this.center = center;
} }

View File

@ -16,8 +16,6 @@ import java.util.jar.JarFile;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.configuration.serialization.ConfigurationSerializable;
import org.bukkit.configuration.serialization.ConfigurationSerialization;
import org.bukkit.plugin.InvalidDescriptionException; import org.bukkit.plugin.InvalidDescriptionException;
import us.tastybento.bskyblock.BSkyBlock; import us.tastybento.bskyblock.BSkyBlock;
@ -84,16 +82,7 @@ public class AddonsManager {
* @return Optional addon object * @return Optional addon object
*/ */
public Optional<Addon> getAddonByName(String name){ public Optional<Addon> getAddonByName(String name){
if(name.equals("")) { return Optional.ofNullable(addons.stream().filter(a -> a.getDescription().getName().contains(name)).findFirst().orElse(null));
return Optional.empty();
}
for(Addon addon : addons){
if(addon.getDescription().getName().contains(name)) {
return Optional.of(addon);
}
}
return Optional.empty();
} }
private void loadAddon(File f) throws InvalidAddonFormatException, InvalidAddonInheritException, InvalidDescriptionException { private void loadAddon(File f) throws InvalidAddonFormatException, InvalidAddonInheritException, InvalidDescriptionException {
@ -101,10 +90,9 @@ public class AddonsManager {
Addon addon = null; Addon addon = null;
// Check that this is a jar // Check that this is a jar
if (!f.getName().endsWith(".jar")) { if (!f.getName().endsWith(".jar")) {
return; throw new IOException("Filename must end in .jar. Name is '" + f.getName() + "'");
} }
try (JarFile jar = new JarFile(f)) { try (JarFile jar = new JarFile(f)) {
// Obtain the addon.yml file // Obtain the addon.yml file
JarEntry entry = jar.getJarEntry("addon.yml"); JarEntry entry = jar.getJarEntry("addon.yml");
if (entry == null) { if (entry == null) {
@ -133,15 +121,13 @@ public class AddonsManager {
addon.saveResource(localeFile, localeDir, false, true); addon.saveResource(localeFile, localeDir, false, true);
} }
plugin.getLocalesManager().loadLocales(addon.getDescription().getName()); plugin.getLocalesManager().loadLocales(addon.getDescription().getName());
// Fire the load event // Fire the load event
Bukkit.getPluginManager().callEvent(AddonEvent.builder().addon(addon).reason(AddonEvent.Reason.LOAD).build()); Bukkit.getPluginManager().callEvent(AddonEvent.builder().addon(addon).reason(AddonEvent.Reason.LOAD).build());
// Add it to the list of addons // Add it to the list of addons
addons.add(addon); addons.add(addon);
// Inform the console // Inform the console
plugin.log("Loading BSkyBlock addon " + addon.getDescription().getName() + "..."); plugin.log("Loaded BSkyBlock addon " + addon.getDescription().getName() + "...");
} }
} catch (Exception e) { } catch (Exception e) {
@ -149,7 +135,6 @@ public class AddonsManager {
plugin.log(f.getName() + "is not a jarfile, ignoring..."); plugin.log(f.getName() + "is not a jarfile, ignoring...");
} }
} }
} }
/** /**
@ -166,9 +151,7 @@ public class AddonsManager {
loader.forEach(l -> { loader.forEach(l -> {
try { try {
l.close(); l.close();
} catch (IOException e) { } catch (IOException ignore) {}
// Do nothing
}
}); });
} }
@ -191,21 +174,7 @@ public class AddonsManager {
* @return Class - the class * @return Class - the class
*/ */
public Class<?> getClassByName(final String name) { public Class<?> getClassByName(final String name) {
Class<?> cachedClass = classes.get(name); return classes.getOrDefault(name, loader.stream().map(l -> l.findClass(name, false)).filter(c -> c != null).findFirst().orElse(null));
if (cachedClass != null) {
return cachedClass;
} else {
for (AddonClassLoader l : loader) {
try {
cachedClass = l.findClass(name, false);
} catch (ClassNotFoundException cnfe) {}
if (cachedClass != null) {
return cachedClass;
}
}
}
return null;
} }
/** /**
@ -216,6 +185,8 @@ public class AddonsManager {
* @param clazz - the class * @param clazz - the class
*/ */
public void setClass(final String name, final Class<?> clazz) { public void setClass(final String name, final Class<?> clazz) {
classes.putIfAbsent(name, clazz);
/*
if (!classes.containsKey(name)) { if (!classes.containsKey(name)) {
classes.put(name, clazz); classes.put(name, clazz);
@ -223,7 +194,7 @@ public class AddonsManager {
Class<? extends ConfigurationSerializable> serializable = clazz.asSubclass(ConfigurationSerializable.class); Class<? extends ConfigurationSerializable> serializable = clazz.asSubclass(ConfigurationSerializable.class);
ConfigurationSerialization.registerClass(serializable); ConfigurationSerialization.registerClass(serializable);
} }
} }*/
} }
/** /**

View File

@ -619,14 +619,7 @@ public class IslandsManager {
*/ */
public void load(){ public void load(){
islandCache.clear(); islandCache.clear();
spawn = null; handler.loadObjects().forEach(islandCache::addIsland);
try {
for (Island island : handler.loadObjects()) {
islandCache.addIsland(island);
}
} catch (Exception e) {
plugin.logError("Could not load islands to cache! " + e.getMessage());
}
} }
/** /**

View File

@ -42,9 +42,7 @@ public class IslandCache {
islandsByLocation.put(island.getCenter(), island); islandsByLocation.put(island.getCenter(), island);
islandsByUUID.putIfAbsent(island.getWorld(), new HashMap<>()); islandsByUUID.putIfAbsent(island.getWorld(), new HashMap<>());
islandsByUUID.get(island.getWorld()).put(island.getOwner(), island); islandsByUUID.get(island.getWorld()).put(island.getOwner(), island);
for (UUID member: island.getMemberSet()) { island.getMemberSet().forEach(member -> islandsByUUID.get(island.getWorld()).put(member, island));
islandsByUUID.get(island.getWorld()).put(member, island);
}
return addToGrid(island); return addToGrid(island);
} }