Fixed some code smells (Sonarcloud)

Put some fields as "final"
Made all abstract classes' constructors "protected" (them being public serves no purpose since they cannot be instantiated outside of child classes)
Did some minor code prettifying
This commit is contained in:
Florian CUNY 2021-01-16 11:27:12 +01:00
parent 9e62f831ff
commit b8d67a653c
15 changed files with 73 additions and 74 deletions

View File

@ -42,7 +42,7 @@ public abstract class Addon {
private File file;
private Map<String, AddonRequestHandler> requestHandlers = new HashMap<>();
public Addon() {
protected Addon() {
state = State.DISABLED;
}

View File

@ -7,7 +7,7 @@ public abstract class AddonException extends Exception {
*/
private static final long serialVersionUID = 4203162022348693854L;
public AddonException(String errorMessage){
protected AddonException(String errorMessage){
super("AddonException : " + errorMessage);
}

View File

@ -3,11 +3,10 @@ package world.bentobox.bentobox.api.addons.request;
import java.util.Locale;
import java.util.Map;
public abstract class AddonRequestHandler
{
private String label;
public abstract class AddonRequestHandler {
private final String label;
public AddonRequestHandler(String label) {
protected AddonRequestHandler(String label) {
this.label = label.toLowerCase(Locale.ENGLISH);
}

View File

@ -122,7 +122,7 @@ public abstract class CompositeCommand extends Command implements PluginIdentifi
* @param label - string for this command
* @param aliases - aliases
*/
public CompositeCommand(Addon addon, String label, String... aliases) {
protected CompositeCommand(Addon addon, String label, String... aliases) {
super(label, "", "", Arrays.asList(aliases));
this.addon = addon;
this.topLabel = label;
@ -154,7 +154,7 @@ public abstract class CompositeCommand extends Command implements PluginIdentifi
* @param label - string for this command
* @param aliases - aliases for this command
*/
public CompositeCommand(String label, String... aliases) {
protected CompositeCommand(String label, String... aliases) {
this((Addon)null, label, aliases);
}
@ -164,7 +164,7 @@ public abstract class CompositeCommand extends Command implements PluginIdentifi
* @param label - string label for this subcommand
* @param aliases - aliases for this subcommand
*/
public CompositeCommand(CompositeCommand parent, String label, String... aliases) {
protected CompositeCommand(CompositeCommand parent, String label, String... aliases) {
this(parent.getAddon(), parent, label, aliases);
}
@ -174,7 +174,7 @@ public abstract class CompositeCommand extends Command implements PluginIdentifi
* @param parent - parent command
* @param aliases - aliases for this command
*/
public CompositeCommand(Addon addon, CompositeCommand parent, String label, String... aliases ) {
protected CompositeCommand(Addon addon, CompositeCommand parent, String label, String... aliases ) {
super(label, "", "", Arrays.asList(aliases));
this.topLabel = parent.getTopLabel();
this.plugin = BentoBox.getInstance();
@ -213,7 +213,7 @@ public abstract class CompositeCommand extends Command implements PluginIdentifi
}
}
/*
/**
* This method deals with the command execution. It traverses the tree of
* subcommands until it finds the right object and then runs execute on it.
*/

View File

@ -28,7 +28,7 @@ public abstract class ConfirmableCommand extends CompositeCommand {
* @param label - string for this command
* @param aliases - aliases
*/
public ConfirmableCommand(Addon addon, String label, String... aliases) {
protected ConfirmableCommand(Addon addon, String label, String... aliases) {
super(addon, label, aliases);
}
@ -38,12 +38,17 @@ public abstract class ConfirmableCommand extends CompositeCommand {
* @param parent - parent command
* @param aliases - aliases for this command
*/
public ConfirmableCommand(Addon addon, CompositeCommand parent, String label, String... aliases ) {
protected ConfirmableCommand(Addon addon, CompositeCommand parent, String label, String... aliases ) {
super(addon, parent, label, aliases);
}
public ConfirmableCommand(CompositeCommand parent, String label, String... aliases) {
/**
*
* @param parent
* @param label
* @param aliases
*/
protected ConfirmableCommand(CompositeCommand parent, String label, String... aliases) {
super(parent, label, aliases);
}

View File

@ -45,7 +45,7 @@ public abstract class DelayedTeleportCommand extends CompositeCommand implements
* @param label - string for this command
* @param aliases - aliases
*/
public DelayedTeleportCommand(Addon addon, String label, String... aliases) {
protected DelayedTeleportCommand(Addon addon, String label, String... aliases) {
super(addon, label, aliases);
Bukkit.getPluginManager().registerEvents(this, getPlugin());
}
@ -56,13 +56,18 @@ public abstract class DelayedTeleportCommand extends CompositeCommand implements
* @param parent - parent command
* @param aliases - aliases for this command
*/
public DelayedTeleportCommand(Addon addon, CompositeCommand parent, String label, String... aliases ) {
protected DelayedTeleportCommand(Addon addon, CompositeCommand parent, String label, String... aliases ) {
super(addon, parent, label, aliases);
Bukkit.getPluginManager().registerEvents(this, getPlugin());
}
public DelayedTeleportCommand(CompositeCommand parent, String label, String... aliases) {
/**
*
* @param parent
* @param label
* @param aliases
*/
protected DelayedTeleportCommand(CompositeCommand parent, String label, String... aliases) {
super(parent, label, aliases);
Bukkit.getPluginManager().registerEvents(this, getPlugin());
}

View File

@ -1,6 +1,5 @@
package world.bentobox.bentobox.api.commands.island;
import java.util.Collections;
import java.util.List;
@ -10,35 +9,31 @@ import world.bentobox.bentobox.api.commands.island.team.IslandTeamCommand;
import world.bentobox.bentobox.api.localization.TextVariables;
import world.bentobox.bentobox.api.user.User;
/**
* This is default player command class. It contains all necessary parts for main /[gamemode] command.
* @since 1.13.0
* @author BONNe
*/
public abstract class DefaultPlayerCommand extends CompositeCommand
{
public abstract class DefaultPlayerCommand extends CompositeCommand {
/**
* This is the top-level command constructor for commands that have no parent.
*
* @param addon - GameMode addon
*/
public DefaultPlayerCommand(GameModeAddon addon)
{
protected DefaultPlayerCommand(GameModeAddon addon) {
// Register command with alias from config.
super(addon,
addon.getWorldSettings().getPlayerCommandAliases().split(" ")[0],
addon.getWorldSettings().getPlayerCommandAliases().split(" "));
}
/**
* Setups anything that is necessary for default main user command.
* @see world.bentobox.bentobox.api.commands.BentoBoxCommand#setup()
*/
@Override
public void setup()
{
public void setup() {
// Description
this.setDescription("commands.island.help.description");
// Limit to player
@ -84,52 +79,43 @@ public abstract class DefaultPlayerCommand extends CompositeCommand
/**
* Defines what will be executed when this command is run.
* @see world.bentobox.bentobox.api.commands.BentoBoxCommand#execute(User, String, List&lt;String&gt;)
* @see world.bentobox.bentobox.api.commands.BentoBoxCommand#execute(User, String, List)
*/
@Override
public boolean execute(User user, String label, List<String> args)
{
if (user == null)
{
public boolean execute(User user, String label, List<String> args) {
if (user == null) {
return false;
}
if (!args.isEmpty())
{
if (!args.isEmpty()) {
user.sendMessage("general.errors.unknown-command", TextVariables.LABEL, this.getTopLabel());
return false;
}
// Check if user has an island.
if (this.getIslands().getIsland(this.getWorld(), user.getUniqueId()) != null)
{
if (this.getIslands().getIsland(this.getWorld(), user.getUniqueId()) != null) {
// Default command if user has an island.
String command = this.<GameModeAddon>getAddon().getWorldSettings().getDefaultPlayerAction();
// Perform command or use "go" command.
if (command != null && user.performCommand(label + " " + command))
{
if (command != null && user.performCommand(label + " " + command)) {
return true;
}
else
{
else {
return this.getSubCommand("go").
map(goCmd -> goCmd.call(user, goCmd.getLabel(), Collections.emptyList())).
orElse(false);
}
}
else
{
else {
// Default command if user does not have an island.
String command = this.<GameModeAddon>getAddon().getWorldSettings().getDefaultNewPlayerAction();
// Perform command or use "create" command.
if (command != null && user.performCommand(label + " " + command))
{
if (command != null && user.performCommand(label + " " + command)) {
return true;
}
else
{
else {
return this.getSubCommand("create").
map(createCmd -> createCmd.call(user, createCmd.getLabel(), Collections.emptyList())).
orElse(false);

View File

@ -34,7 +34,7 @@ public abstract class BentoBoxEvent extends Event {
* The default constructor is defined for cleaner code.
* This constructor assumes the BentoBoxEvent is synchronous.
*/
public BentoBoxEvent() {
protected BentoBoxEvent() {
this(false);
}
@ -43,7 +43,7 @@ public abstract class BentoBoxEvent extends Event {
* @param async - true indicates the event will fire asynchronously, false
* by default from default constructor
*/
public BentoBoxEvent(boolean async) {
protected BentoBoxEvent(boolean async) {
super(async);
}
@ -87,7 +87,9 @@ public abstract class BentoBoxEvent extends Event {
if (value != null) {
map.put(pd.getName(), value);
}
} catch (Exception ignore) {}
} catch (Exception ignore) {
// Ignored.
}
});
return map;
} catch (IntrospectionException e) {
@ -115,7 +117,9 @@ public abstract class BentoBoxEvent extends Event {
}
}
});
} catch (IntrospectionException ignore) {}
} catch (IntrospectionException ignore) {
// Ignored.
}
}
}

View File

@ -19,7 +19,7 @@ public abstract class FlagChangeEvent extends BentoBoxEvent {
* @param player - player changing the flag
* @param editedFlag - flag that has changed
*/
public FlagChangeEvent(UUID player, Flag editedFlag) {
protected FlagChangeEvent(UUID player, Flag editedFlag) {
this.player = player;
this.editedFlag = editedFlag;
}

View File

@ -11,10 +11,12 @@ import org.eclipse.jdt.annotation.Nullable;
*/
public abstract class Hook {
private @NonNull String pluginName;
private @NonNull Material icon;
@NonNull
private final String pluginName;
@NonNull
private final Material icon;
public Hook(@NonNull String pluginName, @NonNull Material icon) {
protected Hook(@NonNull String pluginName, @NonNull Material icon) {
if (pluginName.isEmpty()) {
throw new IllegalArgumentException("Plugin name cannot be empty.");
}

View File

@ -19,7 +19,7 @@ public abstract class SQLDatabaseConnector implements DatabaseConnector {
protected static Connection connection = null;
protected static Set<Class<?>> types = new HashSet<>();
public SQLDatabaseConnector(DatabaseConnectionSettingsImpl dbSettings, String connectionUrl) {
protected SQLDatabaseConnector(DatabaseConnectionSettingsImpl dbSettings, String connectionUrl) {
this.dbSettings = dbSettings;
this.connectionUrl = connectionUrl;
}

View File

@ -14,7 +14,7 @@ import world.bentobox.bentobox.api.placeholders.PlaceholderReplacer;
*/
public abstract class PlaceholderHook extends Hook {
public PlaceholderHook(@NonNull String pluginName) {
protected PlaceholderHook(@NonNull String pluginName) {
super(pluginName, Material.NAME_TAG);
}

View File

@ -101,14 +101,12 @@ public class AddonsManager {
YamlConfiguration data = addonDescription(jar);
// Check if the addon is already loaded (duplicate version?)
String main = data.getString("main");
if (main != null) {
if (this.getAddonByMainClassName(main).isPresent()) {
getAddonByMainClassName(main).ifPresent(a -> {
plugin.logError("Duplicate addon! Addon " + a.getDescription().getName() + " " + a.getDescription().getVersion() + " has already been loaded!");
plugin.logError("Remove the duplicate and restart!");
});
return;
}
if (main != null && this.getAddonByMainClassName(main).isPresent()) {
getAddonByMainClassName(main).ifPresent(a -> {
plugin.logError("Duplicate addon! Addon " + a.getDescription().getName() + " " + a.getDescription().getVersion() + " has already been loaded!");
plugin.logError("Remove the duplicate and restart!");
});
return;
}
// Load the addon
addonClassLoader = new AddonClassLoader(this, data, f, this.getClass().getClassLoader());
@ -439,7 +437,9 @@ public class AddonsManager {
public Class<?> getClassByName(@NonNull final String name) {
try {
return classes.getOrDefault(name, loaders.values().stream().map(l -> l.findClass(name, false)).filter(Objects::nonNull).findFirst().orElse(null));
} catch (Exception ignored) {}
} catch (Exception ignored) {
// Ignored.
}
return null;
}

View File

@ -1630,10 +1630,8 @@ public class IslandsManager {
}
// Fix island ownership in cache
// Correct island cache
if (highestRank == RanksManager.OWNER_RANK) {
if (islandCache.getIslandById(highestIsland.getUniqueId()) != null) {
islandCache.setOwner(islandCache.getIslandById(highestIsland.getUniqueId()), en.getKey());
}
if (highestRank == RanksManager.OWNER_RANK && islandCache.getIslandById(highestIsland.getUniqueId()) != null) {
islandCache.setOwner(islandCache.getIslandById(highestIsland.getUniqueId()), en.getKey());
}
// Fix all the entries that are not the highest
for (Island island : en.getValue()) {

View File

@ -180,9 +180,9 @@ public class BlueprintManagementPanel {
// Buttons for non-default bundle
if (bb.getUniqueId().equals(BlueprintsManager.DEFAULT_BUNDLE_NAME)) {
// Panel has a No Trash icon. If right clicked it is discarded
pb.item(36, getNoTrashIcon(addon, bb));
pb.item(36, getNoTrashIcon());
// Toggle permission - default is always allowed
pb.item(39, getNoPermissionIcon(addon, bb));
pb.item(39, getNoPermissionIcon());
} else {
// Panel has a Trash icon. If right clicked it is discarded
pb.item(36, getTrashIcon(addon, bb));
@ -283,7 +283,7 @@ public class BlueprintManagementPanel {
.build();
}
private PanelItem getNoTrashIcon(@NonNull GameModeAddon addon, BlueprintBundle bb) {
private PanelItem getNoTrashIcon() {
return new PanelItemBuilder()
.name(t("no-trash"))
.description(t("no-trash-instructions"))
@ -307,7 +307,7 @@ public class BlueprintManagementPanel {
}).build();
}
private PanelItem getNoPermissionIcon(@NonNull GameModeAddon addon, BlueprintBundle bb) {
private PanelItem getNoPermissionIcon() {
return new PanelItemBuilder().icon(Material.PAINTING).name(t("no-permission"))
.description(t("no-perm-required"))
.build();