Skip to content

Commit

Permalink
Address some code quality reports from SonarCloud.
Browse files Browse the repository at this point in the history
Most of the errors are just sanity checks, as the most of null-pointers were already checked in other ways.
  • Loading branch information
BONNe committed Dec 1, 2021
1 parent e7b8476 commit 7fbffbb
Show file tree
Hide file tree
Showing 38 changed files with 336 additions and 343 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
public class ChallengesGlobalPlayerCommand extends CompositeCommand
{
/**
* Constructor that inits command with given string.
* Constructor that init command with given string.
* @param addon Challenges Addon
* @param gameModeAddons List with GameModes where challenges addon operates.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@


/**
* This command allows to complete challenges without a gui.
* This command allows completing challenges without a gui.
*/
public class CompleteChallengeCommand extends CompositeCommand
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@

public class ChallengesAdminCommand extends CompositeCommand
{

/**
* Admin command for challenges
* Instantiates a new Challenges' admin command.
*
* @param parent
* @param addon the addon
* @param parent the parent
*/
public ChallengesAdminCommand(ChallengesAddon addon, CompositeCommand parent)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import world.bentobox.bentobox.api.addons.GameModeAddon;
import world.bentobox.bentobox.api.commands.CompositeCommand;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.util.Util;
import world.bentobox.challenges.ChallengesAddon;
import world.bentobox.challenges.panel.user.GameModePanel;
import world.bentobox.challenges.utils.Constants;
Expand All @@ -19,7 +18,7 @@
public class ChallengesGlobalAdminCommand extends CompositeCommand
{
/**
* Constructor that inits command with given string.
* Constructor that init command with given string.
* @param addon Challenges Addon
* @param gameModeAddons List with GameModes where challenges addon operates.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@


/**
* This command allows to complete challenges without a gui.
* This command allows completing challenges without a gui.
*/
public class CompleteCommand extends CompositeCommand
{
Expand Down Expand Up @@ -99,24 +99,26 @@ else if (!args.get(1).isEmpty())
// Add world name back at the start
String challengeName = Utils.getGameMode(this.getWorld()) + "_" + args.get(1);
Challenge challenge = this.addon.getChallengesManager().getChallenge(challengeName);
User target = User.getInstance(targetUUID);

if (challenge != null)
if (challenge != null && target != null)
{
if (!this.addon.getChallengesManager().isChallengeComplete(targetUUID, this.getWorld(), challenge))
{
this.addon.getChallengesManager().setChallengeComplete(
targetUUID, this.getWorld(), challenge, user.getUniqueId());


if (user.isPlayer())
{
Utils.sendMessage(user, user.getTranslation("challenges.messages.completed",
Constants.PARAMETER_NAME, challenge.getFriendlyName(),
Constants.PARAMETER_PLAYER, User.getInstance(targetUUID).getName()));
Constants.PARAMETER_PLAYER, target.getName()));
}
else
{
this.addon.log("Challenge " + challenge.getFriendlyName() + " completed for player " +
User.getInstance(targetUUID).getName());
target.getName());
}
}
else
Expand Down Expand Up @@ -166,23 +168,16 @@ public Optional<List<String>> tabComplete(User user, String alias, List<String>

switch (size)
{
case 3:
case 3 ->
// Create suggestions with all challenges that is available for users.

returnList.addAll(Util.getOnlinePlayerList(user));
break;
case 4:
case 4 ->
// Create suggestions with all challenges that is available for users.
returnList.addAll(this.addon.getChallengesManager().getAllChallengesNames(this.getWorld()).stream().
map(challenge -> challenge.substring(Utils.getGameMode(this.getWorld()).length() + 1)).
collect(Collectors.toList()));

break;
default:
{
default ->
returnList.addAll(Collections.singletonList("help"));
break;
}
}

return Optional.of(Util.tabLimit(returnList, lastString));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,32 @@
import world.bentobox.challenges.ChallengesAddon;


public class MigrateCommand extends CompositeCommand {

public class MigrateCommand extends CompositeCommand
{
/**
* Migrates challenges
* @param addon
* @param cmd
* Instantiates a new Migrate command command.
*
* @param addon the addon
* @param cmd the cmd
*/
public MigrateCommand(Addon addon, CompositeCommand cmd) {
public MigrateCommand(Addon addon, CompositeCommand cmd)
{
super(addon, cmd, "migrate");
}


@Override
public boolean execute(User user, String label, List<String> args) {
((ChallengesAddon)getAddon()).getChallengesManager().migrateDatabase(user, getWorld());
public boolean execute(User user, String label, List<String> args)
{
((ChallengesAddon) getAddon()).getChallengesManager().migrateDatabase(user, getWorld());

return true;
}


@Override
public void setup() {
public void setup()
{
this.setPermission("challenges.admin");
this.setParametersHelp("challenges.commands.admin.migrate.parameters");
this.setDescription("challenges.commands.admin.migrate.description");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@


/**
* This class allows to reload challenges addon.
* This class allows reloading challenges addon.
*/
public class ReloadChallenges extends CompositeCommand
{
/**
* Admin command to reloads challenges addon.
* @param parent
* Instantiates a new Reload challenges command.
*
* @param addon the addon
* @param parent the parent
*/
public ReloadChallenges(Addon addon, CompositeCommand parent)
{
Expand Down Expand Up @@ -69,5 +71,8 @@ else if (args.get(0).equalsIgnoreCase("hard"))
// ---------------------------------------------------------------------


private ChallengesManager manager;
/**
* Addon Manager instance.
*/
private final ChallengesManager manager;
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@


/**
* This command allows to reset challenges without a gui.
* This command allows resetting challenges without a gui.
*/
public class ResetCommand extends CompositeCommand
{
Expand Down Expand Up @@ -79,8 +79,9 @@ else if (args.size() < 2)
else if (!args.get(1).isEmpty())
{
UUID targetUUID = this.getPlayers().getUUID(args.get(0));
User target = User.getInstance(targetUUID);

if (targetUUID == null)
if (targetUUID == null || target == null)
{
if (user.isPlayer())
{
Expand All @@ -104,12 +105,11 @@ else if (!args.get(1).isEmpty())
if (user.isPlayer())
{
Utils.sendMessage(user, user.getTranslation("challenges.messages.reset-all",
Constants.PARAMETER_PLAYER, User.getInstance(targetUUID).getName()));
Constants.PARAMETER_PLAYER, target.getName()));
}
else
{
this.addon.log("All challenges for user " +
User.getInstance(targetUUID).getName() + " was reset!");
this.addon.log("All challenges for user " + target.getName() + " was reset!");
}

return true;
Expand All @@ -129,12 +129,12 @@ else if (!args.get(1).isEmpty())
{
Utils.sendMessage(user, user.getTranslation("challenges.messages.reset",
Constants.PARAMETER_NAME, challenge.getFriendlyName(),
Constants.PARAMETER_PLAYER, User.getInstance(targetUUID).getName()));
Constants.PARAMETER_PLAYER, target.getName()));
}
else
{
this.addon.log("Challenge " + challenge.getFriendlyName() + " was reset for player " +
User.getInstance(targetUUID).getName());
target.getName());
}
}
else
Expand Down Expand Up @@ -185,25 +185,18 @@ public Optional<List<String>> tabComplete(User user, String alias, List<String>

switch (size)
{
case 3:
case 3 ->
// Create suggestions with all challenges that is available for users.

returnList.addAll(Util.getOnlinePlayerList(user));
break;
case 4:
case 4 -> {
// Create suggestions with all challenges that is available for users.
returnList.addAll(this.addon.getChallengesManager().getAllChallengesNames(this.getWorld()).stream().
map(challenge -> challenge.substring(Utils.getGameMode(this.getWorld()).length() + 1)).
collect(Collectors.toList()));

returnList.add("all");

break;
default:
{
returnList.addAll(Collections.singletonList("help"));
break;
}
default ->
returnList.addAll(Collections.singletonList("help"));
}

return Optional.of(Util.tabLimit(returnList, lastString));
Expand All @@ -217,5 +210,5 @@ public Optional<List<String>> tabComplete(User user, String alias, List<String>
/**
* Variable that holds challenge addon. Single casting.
*/
private ChallengesAddon addon;
private final ChallengesAddon addon;
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,32 @@
import world.bentobox.bentobox.api.user.User;
import world.bentobox.challenges.ChallengesAddon;

public class ShowChallenges extends CompositeCommand {


public class ShowChallenges extends CompositeCommand
{
/**
* Admin command to show challenges and manage them
* @param parent
* Instantiates a new Show challenges command.
*
* @param addon the addon
* @param parent the parent
*/
public ShowChallenges(Addon addon, CompositeCommand parent) {
public ShowChallenges(Addon addon, CompositeCommand parent)
{
super(addon, parent, "show");
}


@Override
public void setup() {
public void setup()
{
this.setPermission("admin.challenges");
this.setParametersHelp("challenges.commands.admin.show.parameters");
this.setDescription("challenges.commands.admin.show.description");

}


@Override
public boolean execute(User user, String label, List<String> args) {
public boolean execute(User user, String label, List<String> args)
{
if (user.isPlayer())
{
((ChallengesAddon) getAddon()).getChallengesManager().
Expand All @@ -40,7 +45,5 @@ public boolean execute(User user, String label, List<String> args) {
}

return true;

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.entity.EntityType;
import org.bukkit.inventory.ItemStack;
import org.eclipse.jdt.annotation.NonNull;

Expand All @@ -16,7 +15,6 @@
import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.database.objects.DataObject;
import world.bentobox.bentobox.database.objects.Table;
import world.bentobox.challenges.database.object.adapters.EntityCompatibilityAdapter;
import world.bentobox.challenges.database.object.adapters.RequirementsAdapter;
import world.bentobox.challenges.database.object.adapters.TypeMigrationAdapter;
import world.bentobox.challenges.database.object.requirements.Requirements;
Expand Down Expand Up @@ -838,7 +836,7 @@ public Challenge clone()
clone.setEnvironment(new HashSet<>(this.environment));
clone.setLevel(this.level);
clone.setRemoveWhenCompleted(this.removeWhenCompleted);
clone.setRequirements(this.requirements.clone());
clone.setRequirements(this.requirements.copy());
clone.setRewardText(this.rewardText);
clone.setRewardItems(
this.rewardItems.stream().
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.NotNull;

import com.google.gson.annotations.Expose;

Expand Down Expand Up @@ -449,7 +450,7 @@ public boolean matchGameMode(String gameMode)
* {@inheritDoc}
*/
@Override
public int compareTo(ChallengeLevel o)
public int compareTo(@NotNull ChallengeLevel o)
{
if (this.equals(o))
{
Expand Down Expand Up @@ -501,13 +502,11 @@ public boolean equals(Object obj)
return true;
}

if (!(obj instanceof ChallengeLevel))
if (!(obj instanceof ChallengeLevel other))
{
return false;
}

ChallengeLevel other = (ChallengeLevel) obj;

if (uniqueId == null)
{
return other.uniqueId == null;
Expand Down Expand Up @@ -542,8 +541,7 @@ public boolean isValid()
* Clone method that returns clone of current challengeLevel.
* @return ChallengeLevel that is cloned from current object.
*/
@Override
public ChallengeLevel clone()
public ChallengeLevel copy()
{
ChallengeLevel clone = new ChallengeLevel();

Expand Down

0 comments on commit 7fbffbb

Please sign in to comment.