mirror of
https://github.com/BentoBoxWorld/BentoBox.git
synced 2024-12-28 03:57:39 +01:00
Fixed code smells & added some more NonNull/Nullable annotations
This commit is contained in:
parent
d57f9544f3
commit
4d9a65151f
@ -21,6 +21,8 @@ import org.bukkit.Bukkit;
|
|||||||
import org.bukkit.configuration.InvalidConfigurationException;
|
import org.bukkit.configuration.InvalidConfigurationException;
|
||||||
import org.bukkit.configuration.file.YamlConfiguration;
|
import org.bukkit.configuration.file.YamlConfiguration;
|
||||||
|
|
||||||
|
import org.eclipse.jdt.annotation.NonNull;
|
||||||
|
import org.eclipse.jdt.annotation.Nullable;
|
||||||
import world.bentobox.bentobox.BentoBox;
|
import world.bentobox.bentobox.BentoBox;
|
||||||
import world.bentobox.bentobox.api.addons.Addon;
|
import world.bentobox.bentobox.api.addons.Addon;
|
||||||
import world.bentobox.bentobox.api.addons.AddonClassLoader;
|
import world.bentobox.bentobox.api.addons.AddonClassLoader;
|
||||||
@ -33,15 +35,19 @@ import world.bentobox.bentobox.api.events.addon.AddonEvent;
|
|||||||
*/
|
*/
|
||||||
public class AddonsManager {
|
public class AddonsManager {
|
||||||
|
|
||||||
|
@NonNull
|
||||||
private List<Addon> addons;
|
private List<Addon> addons;
|
||||||
private Map<Addon, AddonClassLoader> loaders;
|
@NonNull
|
||||||
private final Map<String, Class<?>> classes = new HashMap<>();
|
private Map<@NonNull Addon, @Nullable AddonClassLoader> loaders;
|
||||||
|
@NonNull
|
||||||
|
private final Map<String, Class<?>> classes;
|
||||||
private BentoBox plugin;
|
private BentoBox plugin;
|
||||||
|
|
||||||
public AddonsManager(BentoBox plugin) {
|
public AddonsManager(@NonNull BentoBox plugin) {
|
||||||
this.plugin = plugin;
|
this.plugin = plugin;
|
||||||
addons = new ArrayList<>();
|
addons = new ArrayList<>();
|
||||||
loaders = new HashMap<>();
|
loaders = new HashMap<>();
|
||||||
|
classes = new HashMap<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -62,7 +68,7 @@ public class AddonsManager {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void loadAddon(File f) {
|
private void loadAddon(@NonNull File f) {
|
||||||
Addon addon;
|
Addon addon;
|
||||||
AddonClassLoader addonClassLoader;
|
AddonClassLoader addonClassLoader;
|
||||||
try (JarFile jar = new JarFile(f)) {
|
try (JarFile jar = new JarFile(f)) {
|
||||||
@ -151,7 +157,7 @@ public class AddonsManager {
|
|||||||
* @param addon instance of the Addon.
|
* @param addon instance of the Addon.
|
||||||
* @since 1.1
|
* @since 1.1
|
||||||
*/
|
*/
|
||||||
private void handleAddonIncompatibility(Addon addon) {
|
private void handleAddonIncompatibility(@NonNull Addon addon) {
|
||||||
// Set the AddonState as "INCOMPATIBLE".
|
// Set the AddonState as "INCOMPATIBLE".
|
||||||
addon.setState(Addon.State.INCOMPATIBLE);
|
addon.setState(Addon.State.INCOMPATIBLE);
|
||||||
plugin.log("Skipping " + addon.getDescription().getName() + " as it is incompatible with the current version of BentoBox or of server software...");
|
plugin.log("Skipping " + addon.getDescription().getName() + " as it is incompatible with the current version of BentoBox or of server software...");
|
||||||
@ -162,16 +168,17 @@ public class AddonsManager {
|
|||||||
/**
|
/**
|
||||||
* Handles an addon which failed to load due to an error.
|
* Handles an addon which failed to load due to an error.
|
||||||
* @param addon instance of the Addon.
|
* @param addon instance of the Addon.
|
||||||
* @param throwable Throwable that was thrown and which lead to the error.
|
* @param throwable Throwable that was thrown and which led to the error.
|
||||||
* @since 1.1
|
* @since 1.1
|
||||||
*/
|
*/
|
||||||
private void handleAddonError(Addon addon, Throwable throwable) {
|
private void handleAddonError(@NonNull Addon addon, @NonNull Throwable throwable) {
|
||||||
// Set the AddonState as "ERROR".
|
// Set the AddonState as "ERROR".
|
||||||
addon.setState(Addon.State.ERROR);
|
addon.setState(Addon.State.ERROR);
|
||||||
plugin.logError("Skipping " + addon.getDescription().getName() + " due to an unhandled exception...");
|
plugin.logError("Skipping " + addon.getDescription().getName() + " due to an unhandled exception...");
|
||||||
plugin.logError("STACKTRACE: " + throwable.getClass().getSimpleName() + " - " + throwable.getMessage() + " - " + throwable.getCause());
|
plugin.logError("STACKTRACE: " + throwable.getClass().getSimpleName() + " - " + throwable.getMessage() + " - " + throwable.getCause());
|
||||||
if (plugin.getConfig().getBoolean("debug")) {
|
if (plugin.getConfig().getBoolean("debug")) {
|
||||||
throwable.printStackTrace();
|
plugin.logDebug(throwable.toString());
|
||||||
|
plugin.logDebug(throwable.getStackTrace());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -191,14 +198,16 @@ public class AddonsManager {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the addon by name
|
* Gets the addon by name
|
||||||
* @param name - addon name
|
* @param name addon name, not null
|
||||||
* @return Optional addon object
|
* @return Optional addon object
|
||||||
*/
|
*/
|
||||||
public Optional<Addon> getAddonByName(String name){
|
@NonNull
|
||||||
|
public Optional<Addon> getAddonByName(@NonNull String name){
|
||||||
return addons.stream().filter(a -> a.getDescription().getName().contains(name)).findFirst();
|
return addons.stream().filter(a -> a.getDescription().getName().contains(name)).findFirst();
|
||||||
}
|
}
|
||||||
|
|
||||||
private YamlConfiguration addonDescription(JarFile jar) throws InvalidAddonFormatException, IOException, InvalidConfigurationException {
|
@NonNull
|
||||||
|
private YamlConfiguration addonDescription(@NonNull JarFile jar) throws InvalidAddonFormatException, IOException, InvalidConfigurationException {
|
||||||
// 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) {
|
||||||
@ -238,6 +247,7 @@ public class AddonsManager {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@NonNull
|
||||||
public List<Addon> getAddons() {
|
public List<Addon> getAddons() {
|
||||||
return addons;
|
return addons;
|
||||||
}
|
}
|
||||||
@ -246,6 +256,7 @@ public class AddonsManager {
|
|||||||
* @return List of enabled game mode addons
|
* @return List of enabled game mode addons
|
||||||
* @since 1.1
|
* @since 1.1
|
||||||
*/
|
*/
|
||||||
|
@NonNull
|
||||||
public List<GameModeAddon> getGameModeAddons() {
|
public List<GameModeAddon> getGameModeAddons() {
|
||||||
return getEnabledAddons().stream()
|
return getEnabledAddons().stream()
|
||||||
.filter(GameModeAddon.class::isInstance)
|
.filter(GameModeAddon.class::isInstance)
|
||||||
@ -258,6 +269,7 @@ public class AddonsManager {
|
|||||||
* @return list of loaded Addons.
|
* @return list of loaded Addons.
|
||||||
* @since 1.1
|
* @since 1.1
|
||||||
*/
|
*/
|
||||||
|
@NonNull
|
||||||
public List<Addon> getLoadedAddons() {
|
public List<Addon> getLoadedAddons() {
|
||||||
return addons.stream().filter(addon -> addon.getState().equals(Addon.State.LOADED)).collect(Collectors.toList());
|
return addons.stream().filter(addon -> addon.getState().equals(Addon.State.LOADED)).collect(Collectors.toList());
|
||||||
}
|
}
|
||||||
@ -267,30 +279,33 @@ public class AddonsManager {
|
|||||||
* @return list of enabled Addons.
|
* @return list of enabled Addons.
|
||||||
* @since 1.1
|
* @since 1.1
|
||||||
*/
|
*/
|
||||||
|
@NonNull
|
||||||
public List<Addon> getEnabledAddons() {
|
public List<Addon> getEnabledAddons() {
|
||||||
return addons.stream().filter(addon -> addon.getState().equals(Addon.State.ENABLED)).collect(Collectors.toList());
|
return addons.stream().filter(addon -> addon.getState().equals(Addon.State.ENABLED)).collect(Collectors.toList());
|
||||||
}
|
}
|
||||||
|
|
||||||
public AddonClassLoader getLoader(final Addon addon) {
|
@Nullable
|
||||||
|
public AddonClassLoader getLoader(@NonNull final Addon addon) {
|
||||||
return loaders.get(addon);
|
return loaders.get(addon);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Finds a class by name that has been loaded by this loader
|
* Finds a class by name that has been loaded by this loader
|
||||||
* @param name - name of the class
|
* @param name name of the class, not null
|
||||||
* @return Class - the class
|
* @return Class the class
|
||||||
*/
|
*/
|
||||||
public Class<?> getClassByName(final String name) {
|
@Nullable
|
||||||
|
public Class<?> getClassByName(@NonNull final String name) {
|
||||||
return classes.getOrDefault(name, loaders.values().stream().map(l -> l.findClass(name, false)).filter(Objects::nonNull).findFirst().orElse(null));
|
return classes.getOrDefault(name, loaders.values().stream().map(l -> l.findClass(name, false)).filter(Objects::nonNull).findFirst().orElse(null));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets a class that this loader should know about
|
* Sets a class that this loader should know about
|
||||||
*
|
*
|
||||||
* @param name - name of the class
|
* @param name name of the class, not null
|
||||||
* @param clazz - the class
|
* @param clazz the class, not null
|
||||||
*/
|
*/
|
||||||
public void setClass(final String name, final Class<?> clazz) {
|
public void setClass(@NonNull final String name, @NonNull final Class<?> clazz) {
|
||||||
classes.putIfAbsent(name, clazz);
|
classes.putIfAbsent(name, clazz);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -336,5 +351,4 @@ public class AddonsManager {
|
|||||||
addons.clear();
|
addons.clear();
|
||||||
addons.addAll(sortedAddons.values());
|
addons.addAll(sortedAddons.values());
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -41,7 +41,7 @@ public class IslandCache {
|
|||||||
* @param island island to add, not null
|
* @param island island to add, not null
|
||||||
* @return true if successfully added, false if not
|
* @return true if successfully added, false if not
|
||||||
*/
|
*/
|
||||||
public boolean addIsland(Island island) {
|
public boolean addIsland(@NonNull Island island) {
|
||||||
if (island.getCenter() == null || island.getWorld() == null) {
|
if (island.getCenter() == null || island.getWorld() == null) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user