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
|
* 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.
|
* 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();
|
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.
|
* that only Island owner can complete challenge.
|
||||||
* By default it is set to Visitor.
|
* 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();
|
new Flag.Builder("CHALLENGES_ISLAND_PROTECTION", Material.COMMAND_BLOCK).defaultRank(RanksManager.VISITOR_RANK).build();
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -43,14 +43,14 @@ public class ChallengesImportManager
|
||||||
* @param challengesAddon
|
* @param challengesAddon
|
||||||
*/
|
*/
|
||||||
public ChallengesImportManager(ChallengesAddon challengesAddon)
|
public ChallengesImportManager(ChallengesAddon challengesAddon)
|
||||||
{
|
{
|
||||||
this.addon = challengesAddon;
|
this.addon = challengesAddon;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// ---------------------------------------------------------------------
|
// ---------------------------------------------------------------------
|
||||||
// Section: Default Challenge Loader
|
// Section: Default Challenge Loader
|
||||||
// ---------------------------------------------------------------------
|
// ---------------------------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -79,45 +79,46 @@ public class ChallengesImportManager
|
||||||
}
|
}
|
||||||
|
|
||||||
// default configuration should be removed.
|
// default configuration should be removed.
|
||||||
// user made configuration should not!.
|
// user made configuration should not!.
|
||||||
boolean removeAtEnd =
|
boolean removeAtEnd =
|
||||||
!Files.exists(Paths.get(this.addon.getDataFolder().getPath() + "/default.json"));
|
!Files.exists(Paths.get(this.addon.getDataFolder().getPath() + "/default.json"));
|
||||||
|
|
||||||
// Safe json configuration to Challenges folder.
|
// Safe json configuration to Challenges folder.
|
||||||
this.addon.saveResource("default.json", false);
|
this.addon.saveResource("default.json", false);
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
// This prefix will be used to all challenges. That is a unique way how to separate challenged for
|
// This prefix will be used to all challenges. That is a unique way how to separate challenged for
|
||||||
// each game mode.
|
// each game mode.
|
||||||
String uniqueIDPrefix = Utils.getGameMode(world) + "_";
|
String uniqueIDPrefix = Utils.getGameMode(world) + "_";
|
||||||
DefaultDataHolder defaultChallenges = new DefaultJSONHandler(this.addon).loadObject();
|
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
|
||||||
|
challenge.setUniqueId(uniqueIDPrefix + challenge.getUniqueId());
|
||||||
|
// Set up correct level ID if it is necessary
|
||||||
|
if (!challenge.getLevel().isEmpty())
|
||||||
|
{
|
||||||
|
challenge.setLevel(uniqueIDPrefix + challenge.getLevel());
|
||||||
|
}
|
||||||
|
// Load challenge in memory
|
||||||
|
manager.loadChallenge(challenge, false, user, user == null);
|
||||||
|
});
|
||||||
|
|
||||||
// All new challenges should get correct ID. So we need to map it to loaded challenges.
|
defaultChallenges.getLevelList().forEach(challengeLevel -> {
|
||||||
defaultChallenges.getChallengeList().forEach(challenge -> {
|
// Set correct level ID
|
||||||
// Set correct challenge ID
|
challengeLevel.setUniqueId(uniqueIDPrefix + challengeLevel.getUniqueId());
|
||||||
challenge.setUniqueId(uniqueIDPrefix + challenge.getUniqueId());
|
// Set correct world name
|
||||||
// Set up correct level ID if it is necessary
|
challengeLevel.setWorld(Util.getWorld(world).getName());
|
||||||
if (!challenge.getLevel().isEmpty())
|
// Reset names for all challenges.
|
||||||
{
|
challengeLevel.setChallenges(challengeLevel.getChallenges().stream().
|
||||||
challenge.setLevel(uniqueIDPrefix + challenge.getLevel());
|
map(challenge -> uniqueIDPrefix + challenge).
|
||||||
}
|
collect(Collectors.toSet()));
|
||||||
// Load challenge in memory
|
// Load level in memory
|
||||||
manager.loadChallenge(challenge, false, user, user == null);
|
manager.loadLevel(challengeLevel, false, user, user == null);
|
||||||
});
|
});
|
||||||
|
}
|
||||||
defaultChallenges.getLevelList().forEach(challengeLevel -> {
|
|
||||||
// Set correct level ID
|
|
||||||
challengeLevel.setUniqueId(uniqueIDPrefix + challengeLevel.getUniqueId());
|
|
||||||
// Set correct world name
|
|
||||||
challengeLevel.setWorld(Util.getWorld(world).getName());
|
|
||||||
// Reset names for all challenges.
|
|
||||||
challengeLevel.setChallenges(challengeLevel.getChallenges().stream().
|
|
||||||
map(challenge -> uniqueIDPrefix + challenge).
|
|
||||||
collect(Collectors.toSet()));
|
|
||||||
// Load level in memory
|
|
||||||
manager.loadLevel(challengeLevel, false, user, user == null);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
|
@ -128,132 +129,132 @@ public class ChallengesImportManager
|
||||||
this.addon.getChallengesManager().save();
|
this.addon.getChallengesManager().save();
|
||||||
|
|
||||||
if (removeAtEnd)
|
if (removeAtEnd)
|
||||||
{
|
{
|
||||||
// Remove default.yml file from resources to avoid interacting with it.
|
// 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;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This method loads downloaded challenges into memory.
|
* This method loads downloaded challenges into memory.
|
||||||
* @param user User who calls downloaded challenge loading
|
* @param user User who calls downloaded challenge loading
|
||||||
* @param world Target world.
|
* @param world Target world.
|
||||||
* @param downloadString String that need to be loaded via DefaultDataHolder.
|
* @param downloadString String that need to be loaded via DefaultDataHolder.
|
||||||
* @return <code>true</code> if everything was successful, otherwise <code>false</code>.
|
* @return <code>true</code> if everything was successful, otherwise <code>false</code>.
|
||||||
*/
|
*/
|
||||||
public boolean loadDownloadedChallenges(User user, World world, String downloadString)
|
public boolean loadDownloadedChallenges(User user, World world, String downloadString)
|
||||||
{
|
{
|
||||||
ChallengesManager manager = this.addon.getChallengesManager();
|
ChallengesManager manager = this.addon.getChallengesManager();
|
||||||
|
|
||||||
// If exist any challenge or level that is bound to current world, then do not load default challenges.
|
// If exist any challenge or level that is bound to current world, then do not load default challenges.
|
||||||
if (manager.hasAnyChallengeData(world.getName()))
|
if (manager.hasAnyChallengeData(world.getName()))
|
||||||
{
|
{
|
||||||
if (user.isPlayer())
|
if (user.isPlayer())
|
||||||
{
|
{
|
||||||
user.sendMessage("challenges.errors.exist-challenges-or-levels");
|
user.sendMessage("challenges.errors.exist-challenges-or-levels");
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
this.addon.logWarning("challenges.errors.exist-challenges-or-levels");
|
this.addon.logWarning("challenges.errors.exist-challenges-or-levels");
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
// This prefix will be used to all challenges. That is a unique way how to separate challenged for
|
// This prefix will be used to all challenges. That is a unique way how to separate challenged for
|
||||||
// each game mode.
|
// each game mode.
|
||||||
String uniqueIDPrefix = Utils.getGameMode(world) + "_";
|
String uniqueIDPrefix = Utils.getGameMode(world) + "_";
|
||||||
DefaultDataHolder downloadedChallenges = new DefaultJSONHandler(this.addon).loadWebObject(downloadString);
|
DefaultDataHolder downloadedChallenges = new DefaultJSONHandler(this.addon).loadWebObject(downloadString);
|
||||||
|
|
||||||
// All new challenges should get correct ID. So we need to map it to loaded challenges.
|
// All new challenges should get correct ID. So we need to map it to loaded challenges.
|
||||||
downloadedChallenges.getChallengeList().forEach(challenge -> {
|
downloadedChallenges.getChallengeList().forEach(challenge -> {
|
||||||
// Set correct challenge ID
|
// Set correct challenge ID
|
||||||
challenge.setUniqueId(uniqueIDPrefix + challenge.getUniqueId());
|
challenge.setUniqueId(uniqueIDPrefix + challenge.getUniqueId());
|
||||||
// Set up correct level ID if it is necessary
|
// Set up correct level ID if it is necessary
|
||||||
if (!challenge.getLevel().isEmpty())
|
if (!challenge.getLevel().isEmpty())
|
||||||
{
|
{
|
||||||
challenge.setLevel(uniqueIDPrefix + challenge.getLevel());
|
challenge.setLevel(uniqueIDPrefix + challenge.getLevel());
|
||||||
}
|
}
|
||||||
// Load challenge in memory
|
// Load challenge in memory
|
||||||
manager.loadChallenge(challenge, false, user, user == null);
|
manager.loadChallenge(challenge, false, user, user == null);
|
||||||
});
|
});
|
||||||
|
|
||||||
downloadedChallenges.getLevelList().forEach(challengeLevel -> {
|
downloadedChallenges.getLevelList().forEach(challengeLevel -> {
|
||||||
// Set correct level ID
|
// Set correct level ID
|
||||||
challengeLevel.setUniqueId(uniqueIDPrefix + challengeLevel.getUniqueId());
|
challengeLevel.setUniqueId(uniqueIDPrefix + challengeLevel.getUniqueId());
|
||||||
// Set correct world name
|
// Set correct world name
|
||||||
challengeLevel.setWorld(Util.getWorld(world).getName());
|
challengeLevel.setWorld(Util.getWorld(world).getName());
|
||||||
// Reset names for all challenges.
|
// Reset names for all challenges.
|
||||||
challengeLevel.setChallenges(challengeLevel.getChallenges().stream().
|
challengeLevel.setChallenges(challengeLevel.getChallenges().stream().
|
||||||
map(challenge -> uniqueIDPrefix + challenge).
|
map(challenge -> uniqueIDPrefix + challenge).
|
||||||
collect(Collectors.toSet()));
|
collect(Collectors.toSet()));
|
||||||
// Load level in memory
|
// Load level in memory
|
||||||
manager.loadLevel(challengeLevel, false, user, user == null);
|
manager.loadLevel(challengeLevel, false, user, user == null);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
e.printStackTrace();
|
addon.getPlugin().logStacktrace(e);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
this.addon.getChallengesManager().save();
|
this.addon.getChallengesManager().save();
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// ---------------------------------------------------------------------
|
// ---------------------------------------------------------------------
|
||||||
// Section: Default generation
|
// Section: Default generation
|
||||||
// ---------------------------------------------------------------------
|
// ---------------------------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create method that can generate default challenge file from existing challenges in given world.
|
* Create method that can generate default challenge file from existing challenges in given world.
|
||||||
* This method will create default.json file in Challenges folder.
|
* This method will create default.json file in Challenges folder.
|
||||||
* @param user User who calls this method.
|
* @param user User who calls this method.
|
||||||
* @param world from which challenges must be stored.
|
* @param world from which challenges must be stored.
|
||||||
* @param overwrite indicates if existing default.json file can be overwritten.
|
* @param overwrite indicates if existing default.json file can be overwritten.
|
||||||
* @return <code>true</code> if everything was successful, otherwise <code>false</code>
|
* @return <code>true</code> if everything was successful, otherwise <code>false</code>
|
||||||
*/
|
*/
|
||||||
public boolean generateDefaultChallengeFile(User user, World world, boolean overwrite)
|
public boolean generateDefaultChallengeFile(User user, World world, boolean overwrite)
|
||||||
{
|
{
|
||||||
File defaultFile = new File(this.addon.getDataFolder(), "default.json");
|
File defaultFile = new File(this.addon.getDataFolder(), "default.json");
|
||||||
|
|
||||||
if (defaultFile.exists())
|
if (defaultFile.exists())
|
||||||
{
|
{
|
||||||
if (overwrite)
|
if (overwrite)
|
||||||
{
|
{
|
||||||
if (user.isPlayer())
|
if (user.isPlayer())
|
||||||
{
|
{
|
||||||
user.sendMessage("challenges.messages.defaults-file-overwrite");
|
user.sendMessage("challenges.messages.defaults-file-overwrite");
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
this.addon.logWarning("challenges.messages.defaults-file-overwrite");
|
this.addon.logWarning("challenges.messages.defaults-file-overwrite");
|
||||||
}
|
}
|
||||||
|
|
||||||
defaultFile.delete();
|
defaultFile.delete();
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
if (user.isPlayer())
|
if (user.isPlayer())
|
||||||
{
|
{
|
||||||
user.sendMessage("challenges.errors.defaults-file-exist");
|
user.sendMessage("challenges.errors.defaults-file-exist");
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
this.addon.logWarning("challenges.errors.defaults-file-exist");
|
this.addon.logWarning("challenges.errors.defaults-file-exist");
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
@ -262,102 +263,102 @@ public class ChallengesImportManager
|
||||||
String replacementString = Utils.getGameMode(world) + "_";
|
String replacementString = Utils.getGameMode(world) + "_";
|
||||||
ChallengesManager manager = this.addon.getChallengesManager();
|
ChallengesManager manager = this.addon.getChallengesManager();
|
||||||
|
|
||||||
List<Challenge> challengeList = manager.getAllChallenges(world).
|
List<Challenge> challengeList = manager.getAllChallenges(world).
|
||||||
stream().
|
stream().
|
||||||
map(challenge -> {
|
map(challenge -> {
|
||||||
// Use clone to avoid any changes in existing challenges.
|
// Use clone to avoid any changes in existing challenges.
|
||||||
Challenge clone = challenge.clone();
|
Challenge clone = challenge.clone();
|
||||||
// Remove world name from challenge id.
|
// Remove world name from challenge id.
|
||||||
clone.setUniqueId(challenge.getUniqueId().replaceFirst(replacementString, ""));
|
clone.setUniqueId(challenge.getUniqueId().replaceFirst(replacementString, ""));
|
||||||
// Remove world name from level id.
|
// Remove world name from level id.
|
||||||
clone.setLevel(challenge.getLevel().replaceFirst(replacementString, ""));
|
clone.setLevel(challenge.getLevel().replaceFirst(replacementString, ""));
|
||||||
|
|
||||||
return clone;
|
return clone;
|
||||||
}).
|
}).
|
||||||
collect(Collectors.toList());
|
collect(Collectors.toList());
|
||||||
|
|
||||||
List<ChallengeLevel> levelList = manager.getLevels(world).
|
List<ChallengeLevel> levelList = manager.getLevels(world).
|
||||||
stream().
|
stream().
|
||||||
map(challengeLevel -> {
|
map(challengeLevel -> {
|
||||||
// Use clone to avoid any changes in existing levels.
|
// Use clone to avoid any changes in existing levels.
|
||||||
ChallengeLevel clone = challengeLevel.clone();
|
ChallengeLevel clone = challengeLevel.clone();
|
||||||
// Remove world name from level ID.
|
// Remove world name from level ID.
|
||||||
clone.setUniqueId(challengeLevel.getUniqueId().replaceFirst(replacementString, ""));
|
clone.setUniqueId(challengeLevel.getUniqueId().replaceFirst(replacementString, ""));
|
||||||
// Remove world name.
|
// Remove world name.
|
||||||
clone.setWorld("");
|
clone.setWorld("");
|
||||||
// Challenges must be reassign, as they also contains world name.
|
// Challenges must be reassign, as they also contains world name.
|
||||||
clone.setChallenges(challengeLevel.getChallenges().stream().
|
clone.setChallenges(challengeLevel.getChallenges().stream().
|
||||||
map(challenge -> challenge.replaceFirst(replacementString, "")).
|
map(challenge -> challenge.replaceFirst(replacementString, "")).
|
||||||
collect(Collectors.toSet()));
|
collect(Collectors.toSet()));
|
||||||
|
|
||||||
return clone;
|
return clone;
|
||||||
}).
|
}).
|
||||||
collect(Collectors.toList());
|
collect(Collectors.toList());
|
||||||
|
|
||||||
DefaultDataHolder defaultChallenges = new DefaultDataHolder();
|
DefaultDataHolder defaultChallenges = new DefaultDataHolder();
|
||||||
defaultChallenges.setChallengeList(challengeList);
|
defaultChallenges.setChallengeList(challengeList);
|
||||||
defaultChallenges.setLevelList(levelList);
|
defaultChallenges.setLevelList(levelList);
|
||||||
defaultChallenges.setVersion(this.addon.getDescription().getVersion());
|
defaultChallenges.setVersion(this.addon.getDescription().getVersion());
|
||||||
|
|
||||||
BufferedWriter writer = new BufferedWriter(
|
try (BufferedWriter writer = new BufferedWriter(
|
||||||
new OutputStreamWriter(new FileOutputStream(defaultFile), StandardCharsets.UTF_8));
|
new OutputStreamWriter(new FileOutputStream(defaultFile), StandardCharsets.UTF_8))) {
|
||||||
writer.write(Objects.requireNonNull(
|
writer.write(Objects.requireNonNull(
|
||||||
new DefaultJSONHandler(this.addon).toJsonString(defaultChallenges)));
|
new DefaultJSONHandler(this.addon).toJsonString(defaultChallenges)));
|
||||||
writer.close();
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (IOException e)
|
catch (IOException e)
|
||||||
{
|
{
|
||||||
if (user.isPlayer())
|
if (user.isPlayer())
|
||||||
{
|
{
|
||||||
user.sendMessage("challenges.errors.defaults-file-error");
|
user.sendMessage("challenges.errors.defaults-file-error");
|
||||||
}
|
}
|
||||||
|
|
||||||
this.addon.logError("Could not save json file: " + e.getMessage());
|
this.addon.logError("Could not save json file: " + e.getMessage());
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
finally
|
finally
|
||||||
{
|
{
|
||||||
if (user.isPlayer())
|
if (user.isPlayer())
|
||||||
{
|
{
|
||||||
user.sendMessage("challenges.messages.defaults-file-completed", "[world]", world.getName());
|
user.sendMessage("challenges.messages.defaults-file-completed", "[world]", world.getName());
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
this.addon.logWarning("challenges.messages.defaults-file-completed");
|
this.addon.logWarning("challenges.messages.defaults-file-completed");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// ---------------------------------------------------------------------
|
// ---------------------------------------------------------------------
|
||||||
// Section: Private classes for default challenges
|
// Section: Private classes for default challenges
|
||||||
// ---------------------------------------------------------------------
|
// ---------------------------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This Class allows to load default challenges and their levels as objects much easier.
|
* This Class allows to load default challenges and their levels as objects much easier.
|
||||||
*/
|
*/
|
||||||
private static final class DefaultJSONHandler
|
private static final class DefaultJSONHandler
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* This constructor inits JSON builder that will be used to parse challenges.
|
* This constructor inits JSON builder that will be used to parse challenges.
|
||||||
* @param addon Challenges Adddon
|
* @param addon Challenges Adddon
|
||||||
*/
|
*/
|
||||||
DefaultJSONHandler(ChallengesAddon addon)
|
DefaultJSONHandler(ChallengesAddon addon)
|
||||||
{
|
{
|
||||||
GsonBuilder builder = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().enableComplexMapKeySerialization();
|
GsonBuilder builder = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().enableComplexMapKeySerialization();
|
||||||
// Register adapters
|
// Register adapters
|
||||||
builder.registerTypeAdapterFactory(new BentoboxTypeAdapterFactory(addon.getPlugin()));
|
builder.registerTypeAdapterFactory(new BentoboxTypeAdapterFactory(addon.getPlugin()));
|
||||||
// Keep null in the database
|
// Keep null in the database
|
||||||
builder.serializeNulls();
|
builder.serializeNulls();
|
||||||
// Allow characters like < or > without escaping them
|
// Allow characters like < or > without escaping them
|
||||||
builder.disableHtmlEscaping();
|
builder.disableHtmlEscaping();
|
||||||
|
|
||||||
this.addon = addon;
|
this.addon = addon;
|
||||||
this.gson = builder.setPrettyPrinting().create();
|
this.gson = builder.setPrettyPrinting().create();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -366,7 +367,7 @@ public class ChallengesImportManager
|
||||||
* @param instance Instance that must be parsed to json string.
|
* @param instance Instance that must be parsed to json string.
|
||||||
* @return String that contains JSON information from instance object.
|
* @return String that contains JSON information from instance object.
|
||||||
*/
|
*/
|
||||||
String toJsonString(DefaultDataHolder instance)
|
String toJsonString(DefaultDataHolder instance)
|
||||||
{
|
{
|
||||||
// Null check
|
// Null check
|
||||||
if (instance == null)
|
if (instance == null)
|
||||||
|
@ -375,193 +376,193 @@ public class ChallengesImportManager
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
return this.gson.toJson(instance);
|
return this.gson.toJson(instance);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This method creates and adds to list all objects from default.json file.
|
* This method creates and adds to list all objects from default.json file.
|
||||||
* @return List of all objects from default.json that is with T instance.
|
* @return List of all objects from default.json that is with T instance.
|
||||||
*/
|
*/
|
||||||
DefaultDataHolder loadObject()
|
DefaultDataHolder loadObject()
|
||||||
{
|
{
|
||||||
File defaultFile = new File(this.addon.getDataFolder(), "default.json");
|
File defaultFile = new File(this.addon.getDataFolder(), "default.json");
|
||||||
|
|
||||||
try (InputStreamReader reader = new InputStreamReader(new FileInputStream(defaultFile), StandardCharsets.UTF_8))
|
try (InputStreamReader reader = new InputStreamReader(new FileInputStream(defaultFile), StandardCharsets.UTF_8))
|
||||||
{
|
{
|
||||||
DefaultDataHolder object = this.gson.fromJson(reader, DefaultDataHolder.class);
|
DefaultDataHolder object = this.gson.fromJson(reader, DefaultDataHolder.class);
|
||||||
|
|
||||||
reader.close(); // NOSONAR Required to keep OS file handlers low and not rely on GC
|
reader.close(); // NOSONAR Required to keep OS file handlers low and not rely on GC
|
||||||
|
|
||||||
return object;
|
return object;
|
||||||
}
|
}
|
||||||
catch (FileNotFoundException e)
|
catch (FileNotFoundException e)
|
||||||
{
|
{
|
||||||
this.addon.logError("Could not load file '" + defaultFile.getName() + "': File not found.");
|
this.addon.logError("Could not load file '" + defaultFile.getName() + "': File not found.");
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
this.addon.logError("Could not load objects " + defaultFile.getName() + " " + e.getMessage());
|
this.addon.logError("Could not load objects " + defaultFile.getName() + " " + e.getMessage());
|
||||||
}
|
}
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This method creates and adds to list all objects from default.json file.
|
* This method creates and adds to list all objects from default.json file.
|
||||||
* @return List of all objects from default.json that is with T instance.
|
* @return List of all objects from default.json that is with T instance.
|
||||||
*/
|
*/
|
||||||
DefaultDataHolder loadWebObject(String downloadedObject)
|
DefaultDataHolder loadWebObject(String downloadedObject)
|
||||||
{
|
{
|
||||||
return this.gson.fromJson(downloadedObject, DefaultDataHolder.class);
|
return this.gson.fromJson(downloadedObject, DefaultDataHolder.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// ---------------------------------------------------------------------
|
// ---------------------------------------------------------------------
|
||||||
// Section: Variables
|
// Section: Variables
|
||||||
// ---------------------------------------------------------------------
|
// ---------------------------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Holds JSON builder object.
|
* Holds JSON builder object.
|
||||||
*/
|
*/
|
||||||
private Gson gson;
|
private Gson gson;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Holds ChallengesAddon object.
|
* Holds ChallengesAddon object.
|
||||||
*/
|
*/
|
||||||
private ChallengesAddon addon;
|
private ChallengesAddon addon;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This is simple object that will allow to store all current challenges and levels
|
* This is simple object that will allow to store all current challenges and levels
|
||||||
* in single file.
|
* in single file.
|
||||||
*/
|
*/
|
||||||
private static final class DefaultDataHolder implements DataObject
|
private static final class DefaultDataHolder implements DataObject
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* Default constructor. Creates object with empty lists.
|
* Default constructor. Creates object with empty lists.
|
||||||
*/
|
*/
|
||||||
DefaultDataHolder()
|
DefaultDataHolder()
|
||||||
{
|
{
|
||||||
this.challengeList = Collections.emptyList();
|
this.challengeList = Collections.emptyList();
|
||||||
this.challengeLevelList = Collections.emptyList();
|
this.challengeLevelList = Collections.emptyList();
|
||||||
this.version = "";
|
this.version = "";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This method returns stored challenge list.
|
* This method returns stored challenge list.
|
||||||
* @return list that contains default challenges.
|
* @return list that contains default challenges.
|
||||||
*/
|
*/
|
||||||
List<Challenge> getChallengeList()
|
List<Challenge> getChallengeList()
|
||||||
{
|
{
|
||||||
return challengeList;
|
return challengeList;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This method sets given list as default challenge list.
|
* This method sets given list as default challenge list.
|
||||||
* @param challengeList new default challenge list.
|
* @param challengeList new default challenge list.
|
||||||
*/
|
*/
|
||||||
void setChallengeList(List<Challenge> challengeList)
|
void setChallengeList(List<Challenge> challengeList)
|
||||||
{
|
{
|
||||||
this.challengeList = challengeList;
|
this.challengeList = challengeList;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This method returns list of default challenge levels.
|
* This method returns list of default challenge levels.
|
||||||
* @return List that contains default challenge levels.
|
* @return List that contains default challenge levels.
|
||||||
*/
|
*/
|
||||||
List<ChallengeLevel> getLevelList()
|
List<ChallengeLevel> getLevelList()
|
||||||
{
|
{
|
||||||
return challengeLevelList;
|
return challengeLevelList;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This method sets given list as default challenge level list.
|
* This method sets given list as default challenge level list.
|
||||||
* @param levelList new default challenge level list.
|
* @param levelList new default challenge level list.
|
||||||
*/
|
*/
|
||||||
void setLevelList(List<ChallengeLevel> levelList)
|
void setLevelList(List<ChallengeLevel> levelList)
|
||||||
{
|
{
|
||||||
this.challengeLevelList = levelList;
|
this.challengeLevelList = levelList;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This method returns the version value.
|
* This method returns the version value.
|
||||||
* @return the value of version.
|
* @return the value of version.
|
||||||
*/
|
*/
|
||||||
public String getVersion()
|
public String getVersion()
|
||||||
{
|
{
|
||||||
return version;
|
return version;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This method sets the version value.
|
* This method sets the version value.
|
||||||
* @param version the version new value.
|
* @param version the version new value.
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public void setVersion(String version)
|
public void setVersion(String version)
|
||||||
{
|
{
|
||||||
this.version = version;
|
this.version = version;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return default.json
|
* @return default.json
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public String getUniqueId()
|
public String getUniqueId()
|
||||||
{
|
{
|
||||||
return "default.json";
|
return "default.json";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param uniqueId - unique ID the uniqueId to set
|
* @param uniqueId - unique ID the uniqueId to set
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void setUniqueId(String uniqueId)
|
public void setUniqueId(String uniqueId)
|
||||||
{
|
{
|
||||||
// method not used.
|
// method not used.
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// ---------------------------------------------------------------------
|
// ---------------------------------------------------------------------
|
||||||
// Section: Variables
|
// Section: Variables
|
||||||
// ---------------------------------------------------------------------
|
// ---------------------------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Holds a list with default challenges.
|
* Holds a list with default challenges.
|
||||||
*/
|
*/
|
||||||
@Expose
|
@Expose
|
||||||
private List<Challenge> challengeList;
|
private List<Challenge> challengeList;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Holds a list with default levels.
|
* Holds a list with default levels.
|
||||||
*/
|
*/
|
||||||
@Expose
|
@Expose
|
||||||
private List<ChallengeLevel> challengeLevelList;
|
private List<ChallengeLevel> challengeLevelList;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Holds a variable that stores in which addon version file was made.
|
* Holds a variable that stores in which addon version file was made.
|
||||||
*/
|
*/
|
||||||
@Expose
|
@Expose
|
||||||
private String version;
|
private String version;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// ---------------------------------------------------------------------
|
// ---------------------------------------------------------------------
|
||||||
// Section: Variables
|
// Section: Variables
|
||||||
// ---------------------------------------------------------------------
|
// ---------------------------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
private ChallengesAddon addon;
|
private ChallengesAddon addon;
|
||||||
}
|
}
|
|
@ -265,13 +265,6 @@ public class ChallengesManager
|
||||||
User user,
|
User user,
|
||||||
boolean silent)
|
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 (this.challengeCacheData.containsKey(challenge.getUniqueId()))
|
||||||
{
|
{
|
||||||
if (!overwrite)
|
if (!overwrite)
|
||||||
|
@ -333,13 +326,6 @@ public class ChallengesManager
|
||||||
User user,
|
User user,
|
||||||
boolean silent)
|
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 (!this.isValidLevel(level))
|
||||||
{
|
{
|
||||||
if (user != null)
|
if (user != null)
|
||||||
|
|
|
@ -2,6 +2,7 @@ package world.bentobox.challenges.commands;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
import world.bentobox.bentobox.api.addons.GameModeAddon;
|
||||||
import world.bentobox.bentobox.api.commands.CompositeCommand;
|
import world.bentobox.bentobox.api.commands.CompositeCommand;
|
||||||
import world.bentobox.bentobox.api.user.User;
|
import world.bentobox.bentobox.api.user.User;
|
||||||
import world.bentobox.challenges.ChallengesAddon;
|
import world.bentobox.challenges.ChallengesAddon;
|
||||||
|
@ -39,7 +40,9 @@ public class ChallengesCommand extends CompositeCommand
|
||||||
// Show admin better explanation.
|
// Show admin better explanation.
|
||||||
if (user.isOp() || user.hasPermission(this.getPermissionPrefix() + "admin.challenges"))
|
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");
|
user.sendMessage("challenges.errors.no-challenges-admin", "[command]", topLabel + " challenges");
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -349,7 +349,7 @@ public class TryToComplete
|
||||||
if (this.addon.isEconomyProvided())
|
if (this.addon.isEconomyProvided())
|
||||||
{
|
{
|
||||||
this.addon.getEconomyProvider().deposit(this.user,
|
this.addon.getEconomyProvider().deposit(this.user,
|
||||||
this.challenge.getRepeatMoneyReward() * rewardFactor);
|
(double)this.challenge.getRepeatMoneyReward() * rewardFactor);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Experience Repeat Reward
|
// Experience Repeat Reward
|
||||||
|
|
|
@ -126,7 +126,6 @@ public class WebManager
|
||||||
* @param user User who inits request.
|
* @param user User who inits request.
|
||||||
* @param world Target world where challenges should be loaded.
|
* @param world Target world where challenges should be loaded.
|
||||||
* @param entry Entry that contains information about requested object.
|
* @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)
|
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