Fix code smells from sonarcloud analysis
This commit is contained in:
parent
c53fea9f6a
commit
96d94fa16e
|
@ -88,7 +88,7 @@ public class ChallengesAddon extends Addon {
|
|||
* This flag allows to complete challenges in any part of the world. It will not limit
|
||||
* player to their island. Useful for skygrid without protection flags.
|
||||
*/
|
||||
public static Flag CHALLENGES_WORLD_PROTECTION =
|
||||
public static final Flag CHALLENGES_WORLD_PROTECTION =
|
||||
new Flag.Builder("CHALLENGES_WORLD_PROTECTION", Material.GRASS_BLOCK).type(Flag.Type.WORLD_SETTING).defaultSetting(true).build();
|
||||
|
||||
/**
|
||||
|
@ -96,7 +96,7 @@ public class ChallengesAddon extends Addon {
|
|||
* that only Island owner can complete challenge.
|
||||
* By default it is set to Visitor.
|
||||
*/
|
||||
public static Flag CHALLENGES_ISLAND_PROTECTION =
|
||||
public static final Flag CHALLENGES_ISLAND_PROTECTION =
|
||||
new Flag.Builder("CHALLENGES_ISLAND_PROTECTION", Material.COMMAND_BLOCK).defaultRank(RanksManager.VISITOR_RANK).build();
|
||||
|
||||
|
||||
|
|
|
@ -48,9 +48,9 @@ public class ChallengesImportManager
|
|||
}
|
||||
|
||||
|
||||
// ---------------------------------------------------------------------
|
||||
// Section: Default Challenge Loader
|
||||
// ---------------------------------------------------------------------
|
||||
// ---------------------------------------------------------------------
|
||||
// Section: Default Challenge Loader
|
||||
// ---------------------------------------------------------------------
|
||||
|
||||
|
||||
/**
|
||||
|
@ -92,7 +92,7 @@ public class ChallengesImportManager
|
|||
// each game mode.
|
||||
String uniqueIDPrefix = Utils.getGameMode(world) + "_";
|
||||
DefaultDataHolder defaultChallenges = new DefaultJSONHandler(this.addon).loadObject();
|
||||
|
||||
if (defaultChallenges != null) {
|
||||
// All new challenges should get correct ID. So we need to map it to loaded challenges.
|
||||
defaultChallenges.getChallengeList().forEach(challenge -> {
|
||||
// Set correct challenge ID
|
||||
|
@ -119,6 +119,7 @@ public class ChallengesImportManager
|
|||
manager.loadLevel(challengeLevel, false, user, user == null);
|
||||
});
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
|
@ -130,7 +131,7 @@ public class ChallengesImportManager
|
|||
if (removeAtEnd)
|
||||
{
|
||||
// Remove default.yml file from resources to avoid interacting with it.
|
||||
new File(this.addon.getDataFolder(), "default.json").delete();
|
||||
return new File(this.addon.getDataFolder(), "default.json").delete();
|
||||
}
|
||||
|
||||
return true;
|
||||
|
@ -198,7 +199,7 @@ public class ChallengesImportManager
|
|||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
addon.getPlugin().logStacktrace(e);
|
||||
return false;
|
||||
}
|
||||
|
||||
|
@ -208,9 +209,9 @@ public class ChallengesImportManager
|
|||
}
|
||||
|
||||
|
||||
// ---------------------------------------------------------------------
|
||||
// Section: Default generation
|
||||
// ---------------------------------------------------------------------
|
||||
// ---------------------------------------------------------------------
|
||||
// Section: Default generation
|
||||
// ---------------------------------------------------------------------
|
||||
|
||||
|
||||
/**
|
||||
|
@ -299,11 +300,11 @@ public class ChallengesImportManager
|
|||
defaultChallenges.setLevelList(levelList);
|
||||
defaultChallenges.setVersion(this.addon.getDescription().getVersion());
|
||||
|
||||
BufferedWriter writer = new BufferedWriter(
|
||||
new OutputStreamWriter(new FileOutputStream(defaultFile), StandardCharsets.UTF_8));
|
||||
try (BufferedWriter writer = new BufferedWriter(
|
||||
new OutputStreamWriter(new FileOutputStream(defaultFile), StandardCharsets.UTF_8))) {
|
||||
writer.write(Objects.requireNonNull(
|
||||
new DefaultJSONHandler(this.addon).toJsonString(defaultChallenges)));
|
||||
writer.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (IOException e)
|
||||
|
@ -332,9 +333,9 @@ public class ChallengesImportManager
|
|||
}
|
||||
|
||||
|
||||
// ---------------------------------------------------------------------
|
||||
// Section: Private classes for default challenges
|
||||
// ---------------------------------------------------------------------
|
||||
// ---------------------------------------------------------------------
|
||||
// Section: Private classes for default challenges
|
||||
// ---------------------------------------------------------------------
|
||||
|
||||
|
||||
/**
|
||||
|
@ -558,9 +559,9 @@ public class ChallengesImportManager
|
|||
}
|
||||
|
||||
|
||||
// ---------------------------------------------------------------------
|
||||
// Section: Variables
|
||||
// ---------------------------------------------------------------------
|
||||
// ---------------------------------------------------------------------
|
||||
// Section: Variables
|
||||
// ---------------------------------------------------------------------
|
||||
|
||||
|
||||
private ChallengesAddon addon;
|
||||
|
|
|
@ -265,13 +265,6 @@ public class ChallengesManager
|
|||
User user,
|
||||
boolean silent)
|
||||
{
|
||||
if (challenge == null)
|
||||
{
|
||||
this.addon.logError(
|
||||
"Tried to load NULL element from Database. One challenge is broken and will not work.");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (this.challengeCacheData.containsKey(challenge.getUniqueId()))
|
||||
{
|
||||
if (!overwrite)
|
||||
|
@ -333,13 +326,6 @@ public class ChallengesManager
|
|||
User user,
|
||||
boolean silent)
|
||||
{
|
||||
if (level == null)
|
||||
{
|
||||
this.addon.logError(
|
||||
"Tried to load NULL element from Database. One level is broken and will not work.");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!this.isValidLevel(level))
|
||||
{
|
||||
if (user != null)
|
||||
|
|
|
@ -2,6 +2,7 @@ package world.bentobox.challenges.commands;
|
|||
|
||||
import java.util.List;
|
||||
|
||||
import world.bentobox.bentobox.api.addons.GameModeAddon;
|
||||
import world.bentobox.bentobox.api.commands.CompositeCommand;
|
||||
import world.bentobox.bentobox.api.user.User;
|
||||
import world.bentobox.challenges.ChallengesAddon;
|
||||
|
@ -39,7 +40,9 @@ public class ChallengesCommand extends CompositeCommand
|
|||
// Show admin better explanation.
|
||||
if (user.isOp() || user.hasPermission(this.getPermissionPrefix() + "admin.challenges"))
|
||||
{
|
||||
String topLabel = getIWM().getAddon(this.getWorld()).get().getAdminCommand().orElseGet(this::getParent).getTopLabel();
|
||||
String topLabel = getIWM().getAddon(this.getWorld())
|
||||
.map(GameModeAddon::getAdminCommand)
|
||||
.map(optionalAdminCommand -> optionalAdminCommand.map(ac -> ac.getTopLabel()).orElse(this.getTopLabel())).orElse(this.getTopLabel());
|
||||
user.sendMessage("challenges.errors.no-challenges-admin", "[command]", topLabel + " challenges");
|
||||
}
|
||||
else
|
||||
|
|
|
@ -12,6 +12,7 @@ import org.bukkit.Material;
|
|||
import org.bukkit.event.inventory.InventoryClickEvent;
|
||||
import org.bukkit.event.inventory.InventoryCloseEvent;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.eclipse.jdt.annotation.Nullable;
|
||||
|
||||
import world.bentobox.bentobox.api.panels.PanelItem;
|
||||
import world.bentobox.bentobox.api.panels.PanelListener;
|
||||
|
@ -69,9 +70,9 @@ public class EditLoreGUI extends CommonGUI
|
|||
}
|
||||
|
||||
|
||||
// ---------------------------------------------------------------------
|
||||
// Section: Methods
|
||||
// ---------------------------------------------------------------------
|
||||
// ---------------------------------------------------------------------
|
||||
// Section: Methods
|
||||
// ---------------------------------------------------------------------
|
||||
|
||||
|
||||
/**
|
||||
|
@ -95,7 +96,7 @@ public class EditLoreGUI extends CommonGUI
|
|||
|
||||
// TODO: Need 2 View Buttons
|
||||
// One for closes / One for opened.
|
||||
// panelBuilder.item(6, this.getButton(Button.VIEW));
|
||||
// panelBuilder.item(6, this.getButton(Button.VIEW));
|
||||
|
||||
panelBuilder.item(44, this.returnButton);
|
||||
|
||||
|
@ -242,6 +243,7 @@ public class EditLoreGUI extends CommonGUI
|
|||
* @param loreElement String that represents current lore element.
|
||||
* @return PanelItem.
|
||||
*/
|
||||
@Nullable
|
||||
private PanelItem getLoreButton(String loreElement)
|
||||
{
|
||||
switch (this.lore)
|
||||
|
@ -418,9 +420,9 @@ public class EditLoreGUI extends CommonGUI
|
|||
}
|
||||
|
||||
|
||||
// ---------------------------------------------------------------------
|
||||
// Section: Select GUI
|
||||
// ---------------------------------------------------------------------
|
||||
// ---------------------------------------------------------------------
|
||||
// Section: Select GUI
|
||||
// ---------------------------------------------------------------------
|
||||
|
||||
|
||||
/**
|
||||
|
@ -460,7 +462,7 @@ public class EditLoreGUI extends CommonGUI
|
|||
for (String value : values)
|
||||
{
|
||||
PanelItem item = EditLoreGUI.this.getLoreButton(value);
|
||||
|
||||
if (item != null) {
|
||||
item.setClickHandler((panel, user1, clickType, slot) -> {
|
||||
selectedElement.accept(value);
|
||||
return true;
|
||||
|
@ -468,6 +470,7 @@ public class EditLoreGUI extends CommonGUI
|
|||
|
||||
panelBuilder.item(currentIndex++, item);
|
||||
|
||||
|
||||
// Border element
|
||||
if (currentIndex % 9 == 8)
|
||||
{
|
||||
|
@ -486,6 +489,7 @@ public class EditLoreGUI extends CommonGUI
|
|||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
panelBuilder.build();
|
||||
}
|
||||
|
@ -515,7 +519,7 @@ public class EditLoreGUI extends CommonGUI
|
|||
|
||||
String value = values.get(counter);
|
||||
PanelItem item = EditLoreGUI.this.getLoreButton(value);
|
||||
|
||||
if (item != null) {
|
||||
item.setClickHandler((panel, user1, clickType, slot) -> {
|
||||
selectedElement.accept(value, counter);
|
||||
return true;
|
||||
|
@ -541,15 +545,16 @@ public class EditLoreGUI extends CommonGUI
|
|||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
panelBuilder.build();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// ---------------------------------------------------------------------
|
||||
// Section: Private classes
|
||||
// ---------------------------------------------------------------------
|
||||
// ---------------------------------------------------------------------
|
||||
// Section: Private classes
|
||||
// ---------------------------------------------------------------------
|
||||
|
||||
|
||||
/**
|
||||
|
@ -581,9 +586,9 @@ public class EditLoreGUI extends CommonGUI
|
|||
}
|
||||
|
||||
|
||||
// ---------------------------------------------------------------------
|
||||
// Section: Enums
|
||||
// ---------------------------------------------------------------------
|
||||
// ---------------------------------------------------------------------
|
||||
// Section: Enums
|
||||
// ---------------------------------------------------------------------
|
||||
|
||||
|
||||
/**
|
||||
|
@ -609,9 +614,9 @@ public class EditLoreGUI extends CommonGUI
|
|||
}
|
||||
|
||||
|
||||
// ---------------------------------------------------------------------
|
||||
// Section: Variables
|
||||
// ---------------------------------------------------------------------
|
||||
// ---------------------------------------------------------------------
|
||||
// Section: Variables
|
||||
// ---------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Lore that will be edited with current GUI.
|
||||
|
@ -624,9 +629,9 @@ public class EditLoreGUI extends CommonGUI
|
|||
private List<String> activeValues;
|
||||
|
||||
|
||||
// ---------------------------------------------------------------------
|
||||
// Section: Constants
|
||||
// ---------------------------------------------------------------------
|
||||
// ---------------------------------------------------------------------
|
||||
// Section: Constants
|
||||
// ---------------------------------------------------------------------
|
||||
|
||||
|
||||
private final static String REFERENCE_DESCRIPTION = "challenges.gui.descriptions.admin.";
|
||||
|
|
|
@ -349,7 +349,7 @@ public class TryToComplete
|
|||
if (this.addon.isEconomyProvided())
|
||||
{
|
||||
this.addon.getEconomyProvider().deposit(this.user,
|
||||
this.challenge.getRepeatMoneyReward() * rewardFactor);
|
||||
(double)this.challenge.getRepeatMoneyReward() * rewardFactor);
|
||||
}
|
||||
|
||||
// Experience Repeat Reward
|
||||
|
|
|
@ -126,7 +126,6 @@ public class WebManager
|
|||
* @param user User who inits request.
|
||||
* @param world Target world where challenges should be loaded.
|
||||
* @param entry Entry that contains information about requested object.
|
||||
* @return {@code true} if request was successful, {@code false} otherwise.
|
||||
*/
|
||||
public void requestEntryGitHubData(User user, World world, LibraryEntry entry)
|
||||
{
|
||||
|
|
|
@ -1,114 +0,0 @@
|
|||
package world.bentobox.challenges;
|
||||
|
||||
import static org.mockito.Matchers.any;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
import org.bukkit.Server;
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.enchantments.Enchantment;
|
||||
import org.bukkit.inventory.ItemFactory;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.inventory.meta.PotionMeta;
|
||||
import org.bukkit.plugin.PluginManager;
|
||||
import org.bukkit.potion.PotionData;
|
||||
import org.bukkit.potion.PotionType;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mockito;
|
||||
import org.powermock.modules.junit4.PowerMockRunner;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
|
||||
import world.bentobox.challenges.database.object.Challenge;
|
||||
import world.bentobox.challenges.database.object.Challenge.ChallengeType;
|
||||
|
||||
/**
|
||||
* @author tastybento
|
||||
*
|
||||
*/
|
||||
@RunWith(PowerMockRunner.class)
|
||||
public class ChallengesAddonTest {
|
||||
|
||||
/**
|
||||
* @throws java.lang.Exception
|
||||
*/
|
||||
@BeforeClass
|
||||
public static void setUpBeforeClass() throws Exception {
|
||||
Server server = mock(Server.class);
|
||||
World world = mock(World.class);
|
||||
world = mock(World.class);
|
||||
Mockito.when(server.getLogger()).thenReturn(Logger.getAnonymousLogger());
|
||||
Mockito.when(server.getWorld("world")).thenReturn(world);
|
||||
Mockito.when(server.getVersion()).thenReturn("BSB_Mocking");
|
||||
|
||||
PluginManager pluginManager = mock(PluginManager.class);
|
||||
when(server.getPluginManager()).thenReturn(pluginManager);
|
||||
|
||||
ItemFactory itemFactory = mock(ItemFactory.class);
|
||||
when(server.getItemFactory()).thenReturn(itemFactory);
|
||||
|
||||
Bukkit.setServer(server);
|
||||
|
||||
PotionMeta potionMeta = mock(PotionMeta.class);
|
||||
when(itemFactory.getItemMeta(any())).thenReturn(potionMeta);
|
||||
|
||||
OfflinePlayer offlinePlayer = mock(OfflinePlayer.class);
|
||||
when(Bukkit.getOfflinePlayer(any(UUID.class))).thenReturn(offlinePlayer);
|
||||
when(offlinePlayer.getName()).thenReturn("tastybento");
|
||||
|
||||
when(Bukkit.getItemFactory()).thenReturn(itemFactory);
|
||||
when(Bukkit.getLogger()).thenReturn(Logger.getAnonymousLogger());
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
Gson gson = new GsonBuilder().setPrettyPrinting().create();
|
||||
Challenge challenges = new Challenge();
|
||||
challenges.setChallengeType(ChallengeType.ISLAND);
|
||||
Map<Material, Integer> map = new HashMap<>();
|
||||
map.put(Material.DIRT, 5);
|
||||
map.put(Material.ACACIA_FENCE_GATE, 3);
|
||||
challenges.setRequiredBlocks(map);
|
||||
challenges.setIcon(new ItemStack(Material.ACACIA_FENCE_GATE));
|
||||
List<ItemStack> requiredItems = new ArrayList<>();
|
||||
ItemStack result = new ItemStack(Material.POTION, 55);
|
||||
ItemStack result2 = new ItemStack(Material.SPLASH_POTION, 22);
|
||||
ItemStack result3 = new ItemStack(Material.LINGERING_POTION, 11);
|
||||
|
||||
PotionMeta potionMeta = (PotionMeta) result.getItemMeta();
|
||||
PotionData potionData = new PotionData(PotionType.FIRE_RESISTANCE, true, false);
|
||||
potionMeta.setBasePotionData(potionData);
|
||||
result.setItemMeta(potionMeta);
|
||||
|
||||
PotionMeta potionMeta2 = (PotionMeta) result2.getItemMeta();
|
||||
PotionData potionData2 = new PotionData(PotionType.SPEED, true, false);
|
||||
potionMeta2.setBasePotionData(potionData2);
|
||||
potionMeta2.addEnchant(Enchantment.BINDING_CURSE, 1, true);
|
||||
result2.setItemMeta(potionMeta2);
|
||||
|
||||
requiredItems.add(result);
|
||||
requiredItems.add(result2);
|
||||
requiredItems.add(result3);
|
||||
challenges.setRequiredItems(requiredItems);
|
||||
String json = gson.toJson(challenges);
|
||||
|
||||
Logger.getAnonymousLogger().info(json);
|
||||
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue