Remove various code smells.

This commit is contained in:
tastybento 2022-04-16 15:54:49 -07:00
parent 6c0915544e
commit 9cccfa02ba
19 changed files with 1155 additions and 1225 deletions

View File

@ -4,7 +4,6 @@ package world.bentobox.challenges.commands;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import world.bentobox.bentobox.api.commands.CompositeCommand;
import world.bentobox.bentobox.api.user.User;
@ -115,11 +114,10 @@ public class CompleteChallengeCommand extends CompositeCommand
// Create suggestions with all challenges that is available for users.
returnList.addAll(this.<ChallengesAddon>getAddon().getChallengesManager().getAllChallengesNames(this.getWorld()).
stream().
filter(challenge -> challenge.startsWith(Utils.getGameMode(this.getWorld()) + "_") ||
challenge.startsWith(Utils.getGameMode(this.getWorld()).toLowerCase() + "_")).
map(challenge -> challenge.substring(Utils.getGameMode(this.getWorld()).length() + 1)).
collect(Collectors.toList()));
stream().
filter(challenge -> challenge.startsWith(Utils.getGameMode(this.getWorld()) + "_") ||
challenge.startsWith(Utils.getGameMode(this.getWorld()).toLowerCase() + "_")).
map(challenge -> challenge.substring(Utils.getGameMode(this.getWorld()).length() + 1)).toList());
break;
case 4:
// Suggest a number of completions.

View File

@ -2,11 +2,9 @@ package world.bentobox.challenges.commands.admin;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
import java.util.stream.Collectors;
import world.bentobox.bentobox.api.addons.Addon;
import world.bentobox.bentobox.api.commands.CompositeCommand;
@ -101,7 +99,7 @@ public class CompleteCommand extends CompositeCommand
Challenge challenge = this.addon.getChallengesManager().getChallenge(challengeName);
User target = User.getInstance(targetUUID);
if (challenge != null && target != null)
if (challenge != null)
{
if (!this.addon.getChallengesManager().isChallengeComplete(targetUUID, this.getWorld(), challenge))
{
@ -174,10 +172,9 @@ public class CompleteCommand extends CompositeCommand
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()));
map(challenge -> challenge.substring(Utils.getGameMode(this.getWorld()).length() + 1)).toList());
default ->
returnList.addAll(Collections.singletonList("help"));
returnList.add("help");
}
return Optional.of(Util.tabLimit(returnList, lastString));

View File

@ -2,11 +2,9 @@ package world.bentobox.challenges.commands.admin;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
import java.util.stream.Collectors;
import world.bentobox.bentobox.api.addons.Addon;
import world.bentobox.bentobox.api.commands.CompositeCommand;
@ -191,12 +189,11 @@ public class ResetCommand extends CompositeCommand
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()));
map(challenge -> challenge.substring(Utils.getGameMode(this.getWorld()).length() + 1)).toList());
returnList.add("all");
}
default ->
returnList.addAll(Collections.singletonList("help"));
returnList.add("help");
}
return Optional.of(Util.tabLimit(returnList, lastString));

View File

@ -56,20 +56,18 @@ public class StatisticRequirements extends Requirements
return false;
}
switch (this.statistic.getType())
return switch (this.statistic.getType())
{
case ITEM -> {
return this.material != null && this.material.isItem();
}
case BLOCK -> {
return this.material != null && this.material.isBlock();
}
case ENTITY -> {
return this.entity != null;
}
}
case ITEM -> this.material != null && this.material.isItem();
case BLOCK -> this.material != null && this.material.isBlock();
case ENTITY -> this.entity != null;
case UNTYPED -> true;
};
return true;
}

View File

@ -27,7 +27,6 @@ import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.annotations.Expose;
import lv.id.bonne.panelutils.PanelUtils;
import world.bentobox.bentobox.api.addons.GameModeAddon;
import world.bentobox.bentobox.api.localization.TextVariables;
import world.bentobox.bentobox.api.user.User;

View File

@ -232,10 +232,6 @@ public class ChallengesManager
this.savePlayersData();
}
// this.challengeDatabase = new Database<>(addon, Challenge.class);
// this.levelDatabase = new Database<>(addon, ChallengeLevel.class);
// this.playersDatabase = new Database<>(addon, ChallengesPlayerData.class);
this.loadAndValidate();
}
@ -434,12 +430,6 @@ public class ChallengesManager
public void removeFromCache(UUID playerID)
{
// Remove due possible issues with saving... (#246)
// if (!this.settings.isStoreAsIslandData() && this.playerCacheData.containsKey(playerID.toString()))
// {
// // save before remove
// this.savePlayerData(playerID.toString());
// this.playerCacheData.remove(playerID.toString());
// }
this.savePlayerData(playerID.toString());
@ -841,8 +831,6 @@ public class ChallengesManager
{
// Challenges and Levels are saved on modifications only to avoid issues with
// NULL's in data after interrupting server while in saving stage.
// this.saveChallenges();
// this.saveLevels();
this.savePlayersData();
}

View File

@ -288,9 +288,8 @@ public abstract class CommonPanel
{
// Yes list duplication for complete menu.
List<String> missingPermissions = challenge.getRequirements().getRequiredPermissions().stream().
filter(permission -> target == null || !target.hasPermission(permission)).
sorted().
collect(Collectors.toList());
filter(permission -> target == null || !target.hasPermission(permission)).
sorted().toList();
StringBuilder permissionBuilder = new StringBuilder();

View File

@ -582,12 +582,12 @@ public class AdminPanel extends CommonPanel
// ---------------------------------------------------------------------
/**
* This indicate if Reset Challenges must work as reset all.
* This indicates if Reset Challenges must work as reset all.
*/
private boolean resetAllMode;
/**
* This indicate if wipe button should clear all data, or only challenges.
* This indicates if wipe button should clear all data, or only challenges.
*/
private boolean wipeAll;
}

View File

@ -334,13 +334,12 @@ public class ListUsersPanel extends CommonPagedPanel<Player>
if (clickType.isRightClick())
{
this.mode = Utils.getPreviousValue(ViewMode.values(), this.mode);
this.onlineUsers = this.collectUsers(this.mode);
}
else
{
this.mode = Utils.getNextValue(ViewMode.values(), this.mode);
this.onlineUsers = this.collectUsers(this.mode);
}
this.onlineUsers = this.collectUsers(this.mode);
// Reset search
this.searchString = "";

View File

@ -273,7 +273,6 @@ public class TryToComplete
{
Bukkit.getOnlinePlayers().stream().
map(User::getInstance).
filter(Objects::nonNull).
forEach(user -> Utils.sendMessage(user, user.getTranslation(
"challenges.messages.name-has-completed-challenge",
Constants.PARAMETER_NAME, this.user.getName(),
@ -380,7 +379,6 @@ public class TryToComplete
{
Bukkit.getOnlinePlayers().stream().
map(User::getInstance).
filter(Objects::nonNull).
forEach(user -> Utils.sendMessage(user, user.getTranslation(
"challenges.messages.name-has-completed-level",
Constants.PARAMETER_NAME, this.user.getName(),

View File

@ -14,10 +14,9 @@ import static org.mockito.Mockito.when;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
@ -154,7 +153,7 @@ public class ChallengesAddonTest {
File jFile = new File("addon.jar");
List<String> lines = Arrays.asList("# ChallengesAddon Configuration", "uniqueId: config");
Path path = Paths.get("config.yml");
Files.write(path, lines, Charset.forName("UTF-8"));
Files.write(path, lines, StandardCharsets.UTF_8);
try (JarOutputStream tempJarOutputStream = new JarOutputStream(new FileOutputStream(jFile))) {
addToJar(tempJarOutputStream, path);
addToJar(tempJarOutputStream, Paths.get("src/main/resources/panels/gamemode_panel.yml"));
@ -206,7 +205,7 @@ public class ChallengesAddonTest {
}
private void addToJar(JarOutputStream tempJarOutputStream, Path path) throws FileNotFoundException, IOException {
private void addToJar(JarOutputStream tempJarOutputStream, Path path) throws IOException {
//Added the new files to the jar.
try (FileInputStream fis = new FileInputStream(path.toFile())) {

View File

@ -16,7 +16,6 @@ import static org.mockito.Mockito.when;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
@ -105,10 +104,9 @@ public class ChallengesManagerTest {
// Variable fields
private ChallengesManager cm;
private File database;
private String uuid;
private Challenge challenge;
private @NonNull ChallengeLevel level;
private UUID playerID = UUID.randomUUID();
private final UUID playerID = UUID.randomUUID();
private String cName;
private String levelName;
@ -159,7 +157,7 @@ public class ChallengesManagerTest {
// Challenge
challenge = new Challenge();
uuid = UUID.randomUUID().toString();
String uuid = UUID.randomUUID().toString();
challenge.setUniqueId(GAME_MODE_NAME + "_" + uuid);
challenge.setFriendlyName("name");
challenge.setLevel(GAME_MODE_NAME + "_novice");
@ -438,7 +436,7 @@ public class ChallengesManagerTest {
removeLine(check);
}
private boolean removeLine(File inputFile) {
private void removeLine(File inputFile) {
File tempFile = new File("myTempFile.json");
try (BufferedReader reader = new BufferedReader(new FileReader(inputFile))) {
@ -454,13 +452,11 @@ public class ChallengesManagerTest {
writer.write(currentLine + System.getProperty("line.separator"));
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return tempFile.renameTo(inputFile);
tempFile.renameTo(inputFile);
}
/**
* Test method for {@link ChallengesManager#saveLevel(world.bentobox.challenges.database.object.ChallengeLevel)}.

View File

@ -60,7 +60,6 @@ public class ChallengesCommandTest {
@Mock
private CompositeCommand ic;
private UUID uuid;
@Mock
private User user;
@Mock
@ -79,13 +78,10 @@ public class ChallengesCommandTest {
@Mock
private GameModeAddon gameModeAddon;
private Settings settings;
/**
* @throws java.lang.Exception
*/
@Before
public void setUp() throws Exception {
public void setUp() {
// Set up plugin
BentoBox plugin = mock(BentoBox.class);
Whitebox.setInternalState(BentoBox.class, "instance", plugin);
@ -125,7 +121,7 @@ public class ChallengesCommandTest {
Player p = mock(Player.class);
// Sometimes use Mockito.withSettings().verboseLogging()
when(user.isOp()).thenReturn(false);
uuid = UUID.randomUUID();
UUID uuid = UUID.randomUUID();
when(user.getUniqueId()).thenReturn(uuid);
when(user.getPlayer()).thenReturn(p);
when(user.getName()).thenReturn("tastybento");
@ -152,7 +148,7 @@ public class ChallengesCommandTest {
when(ChatColor.translateAlternateColorCodes(any(char.class), anyString())).thenAnswer((Answer<String>) invocation -> invocation.getArgument(1, String.class));
// Settings
settings = new Settings();
Settings settings = new Settings();
when(addon.getChallengesSettings()).thenReturn(settings);
settings.setVisibilityMode(VisibilityMode.VISIBLE);

View File

@ -25,7 +25,6 @@ import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemFactory;
import org.bukkit.inventory.meta.ItemMeta;
import org.eclipse.jdt.annotation.NonNull;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
@ -64,7 +63,6 @@ public class CompleteChallengeCommandTest {
@Mock
private CompositeCommand ic;
private UUID uuid;
@Mock
private User user;
@Mock
@ -84,16 +82,14 @@ public class CompleteChallengeCommandTest {
@Mock
private GameModeAddon gameModeAddon;
private Settings settings;
@Mock
private Challenge challenge;
/**
* @throws java.lang.Exception
*/
@SuppressWarnings("unchecked")
@Before
public void setUp() throws Exception {
public void setUp() {
// Set up plugin
BentoBox plugin = mock(BentoBox.class);
Whitebox.setInternalState(BentoBox.class, "instance", plugin);
@ -129,7 +125,7 @@ public class CompleteChallengeCommandTest {
Player p = mock(Player.class);
// Sometimes use Mockito.withSettings().verboseLogging()
when(user.isOp()).thenReturn(false);
uuid = UUID.randomUUID();
UUID uuid = UUID.randomUUID();
when(user.getUniqueId()).thenReturn(uuid);
when(user.getPlayer()).thenReturn(p);
when(user.getName()).thenReturn("tastybento");
@ -159,7 +155,7 @@ public class CompleteChallengeCommandTest {
when(ChatColor.translateAlternateColorCodes(any(char.class), anyString())).thenAnswer((Answer<String>) invocation -> invocation.getArgument(1, String.class));
// Settings
settings = new Settings();
Settings settings = new Settings();
when(addon.getChallengesSettings()).thenReturn(settings);
settings.setVisibilityMode(VisibilityMode.VISIBLE);
@ -184,13 +180,6 @@ public class CompleteChallengeCommandTest {
cc = new CompleteChallengeCommand(addon, ic);
}
/**
* @throws java.lang.Exception
*/
@After
public void tearDown() throws Exception {
}
/**
* Test method for {@link world.bentobox.challenges.commands.CompleteChallengeCommand#CompleteChallengeCommand(world.bentobox.bentobox.api.addons.Addon, world.bentobox.bentobox.api.commands.CompositeCommand)}.
*/

View File

@ -15,7 +15,7 @@ import world.bentobox.bentobox.api.flags.Flag;
public class TestWorldSetting implements WorldSettings {
private Map<String, Boolean> flags = new HashMap<>();
private final Map<String, Boolean> flags = new HashMap<>();
@Override
public GameMode getDefaultGameMode() {

View File

@ -36,7 +36,6 @@ import org.bukkit.inventory.PlayerInventory;
import org.bukkit.util.BoundingBox;
import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.Nullable;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
@ -86,17 +85,15 @@ public class TryToCompleteTest {
private TryToComplete ttc;
private Challenge challenge;
private @NonNull ChallengeLevel level;
@Mock
private ChallengesAddon addon;
@Mock
private User user;
@Mock
private World world;
private String topLabel = "island";
private String permissionPrefix = "perm.";
private final String topLabel = "island";
private final String permissionPrefix = "perm.";
private String levelName;
@Mock
private ChallengesManager cm;
@Mock
@ -115,18 +112,16 @@ public class TryToCompleteTest {
private Settings settings;
@Mock
private WorldSettings mySettings;
private Map<String, Boolean> map;
@Mock
private @Nullable PlayerInventory inv;
private ItemStack[] contents = {};
private final ItemStack[] contents = {};
@Mock
private BoundingBox bb;
/**
* @throws java.lang.Exception
*/
@Before
public void setUp() throws Exception {
public void setUp() {
// Set up plugin
Whitebox.setInternalState(BentoBox.class, "instance", plugin);
when(addon.getPlugin()).thenReturn(plugin);
@ -143,8 +138,8 @@ public class TryToCompleteTest {
when(gameMode.getDescription()).thenReturn(desc2);
// Challenge Level
level = new ChallengeLevel();
levelName = GAME_MODE_NAME + "_novice";
@NonNull ChallengeLevel level = new ChallengeLevel();
String levelName = GAME_MODE_NAME + "_novice";
level.setUniqueId(levelName);
level.setFriendlyName("Novice");
// Set up challenge
@ -239,19 +234,19 @@ public class TryToCompleteTest {
Map<UUID, String> online = new HashMap<>();
Set<Player> onlinePlayers = new HashSet<>();
for (int j = 0; j < NAMES.length; j++) {
for (String name : NAMES) {
Player p1 = mock(Player.class);
UUID uuid2 = UUID.randomUUID();
when(p1.getUniqueId()).thenReturn(uuid2);
when(p1.getName()).thenReturn(NAMES[j]);
online.put(uuid2, NAMES[j]);
when(p1.getName()).thenReturn(name);
online.put(uuid2, name);
onlinePlayers.add(p1);
}
PowerMockito.mockStatic(Bukkit.class);
when(Bukkit.getOnlinePlayers()).then((Answer<Set<Player>>) invocation -> onlinePlayers);
// World settings
map = new HashMap<>();
Map<String, Boolean> map = new HashMap<>();
when(mySettings.getWorldFlags()).thenReturn(map);
when(iwm.getWorldSettings(any())).thenReturn(mySettings);
ChallengesAddon.CHALLENGES_WORLD_PROTECTION.setSetting(world, true);
@ -265,13 +260,6 @@ public class TryToCompleteTest {
when(ChatColor.stripColor(anyString())).thenAnswer((Answer<String>) invocation -> invocation.getArgument(0, String.class));
}
/**
* @throws java.lang.Exception
*/
@After
public void tearDown() throws Exception {
}
/**
* Test method for {@link world.bentobox.challenges.tasks.TryToComplete#TryToComplete(world.bentobox.challenges.ChallengesAddon, world.bentobox.bentobox.api.user.User, world.bentobox.challenges.database.object.Challenge, org.bukkit.World, java.lang.String, java.lang.String)}.
*/

View File

@ -51,20 +51,18 @@ public class TryToCompleteTestOld {
};
List<ItemStack> required;
private ChallengesAddon addon;
private PlayerInventory inv;
/**
* @throws java.lang.Exception
*/
@Before
public void setUp() throws Exception {
public void setUp() {
Server server = mock(Server.class);
PowerMockito.mockStatic(Bukkit.class);
when(Bukkit.getServer()).thenReturn(server);
when(Bukkit.getBukkitVersion()).thenReturn("1.13.2");
user = mock(User.class);
inv = mock(PlayerInventory.class);
PlayerInventory inv = mock(PlayerInventory.class);
when(inv.getContents()).thenReturn(stacks);
when(user.getInventory()).thenReturn(inv);
addon = mock(ChallengesAddon.class);

View File

@ -1,7 +1,6 @@
package world.bentobox.challenges.utils;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;
@ -18,7 +17,6 @@ import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.inventory.ItemFactory;
import org.bukkit.inventory.ItemStack;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
@ -49,10 +47,9 @@ public class UtilsTest {
/**
* @throws java.lang.Exception
*/
@Before
public void setUp() throws Exception {
public void setUp() {
// Set up plugin
BentoBox plugin = mock(BentoBox.class);
Whitebox.setInternalState(BentoBox.class, "instance", plugin);
@ -71,13 +68,6 @@ public class UtilsTest {
}
/**
* @throws java.lang.Exception
*/
@After
public void tearDown() throws Exception {
}
/**
* Test method for {@link world.bentobox.challenges.utils.Utils#groupEqualItems(java.util.List, java.util.Set)}.
*/