Imports levels.

This commit is contained in:
Tastybento 2018-04-08 16:28:56 -07:00
parent 823dcbafd8
commit 4e6d7d73d2
4 changed files with 253 additions and 127 deletions

View File

@ -44,51 +44,26 @@ public class ChallengesManager {
load();
}
/**
* Check if a challenge exists - case insensitive
* @param name - name of challenge
* @return true if it exists, otherwise false
*/
public boolean isChallenge(String name) {
for (Set<Challenges> ch : challengeList.values()) {
if (ch.stream().filter(c -> c.getUniqueId().equalsIgnoreCase(name)).findFirst().isPresent()) {
return true;
}
}
return false;
}
/**
* Get challenge by name
* @param name - unique name of challenge
* @return - challenge or null if it does not exist
*/
public Challenges getChallenge(String name) {
for (Set<Challenges> ch : challengeList.values()) {
Optional<Challenges> challenge = ch.stream().filter(c -> c.getUniqueId().equalsIgnoreCase(name)).findFirst();
if (challenge.isPresent()) {
return challenge.get();
}
}
return null;
}
/**
* Get the list of all challenge unique names.
* Used for checking admin commands and tab complete
* @return List of challenge names
*/
public List<String> getAllChallengesList() {
List<String> result = new ArrayList<>();
challengeList.values().forEach(ch -> ch.forEach(c -> result.add(c.getUniqueId())));
return result;
}
public long checkChallengeTimes(User user, Challenges challenge) {
// TODO Auto-generated method stub
return 0;
}
/**
* Creates a simple example description of the requirements
* @param user - user of this command
* @param requiredItems - list of items
* @return Description list
*/
private List<String> createDescription(User user, List<ItemStack> requiredItems) {
List<String> result = new ArrayList<>();
result.add(user.getTranslation("challenges.admin.create.description"));
for (ItemStack item : requiredItems) {
result.add(user.getTranslation("challenges.admin.create.description-item-color") + item.getAmount() + " x " + Util.prettifyText(item.getType().toString()));
}
return result;
}
/**
* Creates an inventory challenge
* @param user - the user who is making the challenge
@ -135,21 +110,6 @@ public class ChallengesManager {
return true;
}
/**
* Creates a simple example description of the requirements
* @param user - user of this command
* @param requiredItems - list of items
* @return Description list
*/
private List<String> createDescription(User user, List<ItemStack> requiredItems) {
List<String> result = new ArrayList<>();
result.add(user.getTranslation("challenges.admin.create.description"));
for (ItemStack item : requiredItems) {
result.add(user.getTranslation("challenges.admin.create.description-item-color") + item.getAmount() + " x " + Util.prettifyText(item.getType().toString()));
}
return result;
}
/**
* Create a surrounding challenge
* @param challengeInfo - info on the challenge from the builder
@ -178,6 +138,32 @@ public class ChallengesManager {
return true;
}
/**
* Get the list of all challenge unique names.
* Used for checking admin commands and tab complete
* @return List of challenge names
*/
public List<String> getAllChallengesList() {
List<String> result = new ArrayList<>();
challengeList.values().forEach(ch -> ch.forEach(c -> result.add(c.getUniqueId())));
return result;
}
/**
* Get challenge by name
* @param name - unique name of challenge
* @return - challenge or null if it does not exist
*/
public Challenges getChallenge(String name) {
for (Set<Challenges> ch : challengeList.values()) {
Optional<Challenges> challenge = ch.stream().filter(c -> c.getUniqueId().equalsIgnoreCase(name)).findFirst();
if (challenge.isPresent()) {
return challenge.get();
}
}
return null;
}
/**
* Get the status on every level
* @param user
@ -194,6 +180,13 @@ public class ChallengesManager {
return result;
}
/**
* @return the challengeList
*/
public LinkedHashMap<ChallengeLevels, Set<Challenges>> getChallengeList() {
return challengeList;
}
/**
* Get the set of challenges for this level
* @param level - the level required
@ -226,6 +219,20 @@ public class ChallengesManager {
return result;
}
/**
* Check if a challenge exists - case insensitive
* @param name - name of challenge
* @return true if it exists, otherwise false
*/
public boolean isChallenge(String name) {
for (Set<Challenges> ch : challengeList.values()) {
if (ch.stream().filter(c -> c.getUniqueId().equalsIgnoreCase(name)).findFirst().isPresent()) {
return true;
}
}
return false;
}
/**
* Checks if a challenge is complete or not
* @param uniqueId - unique ID - player's UUID
@ -237,6 +244,14 @@ public class ChallengesManager {
return false;
}
/**
* Checks number of challenges
* @return true if no challenges
*/
public boolean isFirstTime() {
return challengeList.isEmpty();
}
public boolean isLevelAvailable(User user, String level) {
// TODO
return false;
@ -260,6 +275,42 @@ public class ChallengesManager {
}
sortChallenges();
}
private void save() {
challengeList.entrySet().forEach(en -> {
lvConfig.saveConfigObject(en.getKey());
en.getValue().forEach(chConfig::saveConfigObject);
});
}
/**
* Save to the database
* @param async - if true, saving will be done async
*/
public void save(boolean async) {
if (async) {
BSkyBlock.getInstance().getServer().getScheduler().runTaskAsynchronously(BSkyBlock.getInstance(), () -> save());
} else {
save();
}
}
/**
* Sets the challenge as complete and increments the number of times it has been completed
* @param user
* @param uniqueId
*/
public void setChallengeComplete(User user, String uniqueId) {
// TODO Auto-generated method stub
}
/**
* @param challengeList the challengeList to set
*/
public void setChallengeList(LinkedHashMap<ChallengeLevels, Set<Challenges>> challengeList) {
this.challengeList = challengeList;
}
public void sortChallenges() {
// Sort the challenge list into level order
@ -304,55 +355,12 @@ public class ChallengesManager {
}
}
private void save() {
challengeList.entrySet().forEach(en -> {
lvConfig.saveConfigObject(en.getKey());
en.getValue().forEach(chConfig::saveConfigObject);
});
}
/**
* Save to the database
* @param async - if true, saving will be done async
* Store a challenge level
* @param level the challenge level
*/
public void save(boolean async) {
if (async) {
BSkyBlock.getInstance().getServer().getScheduler().runTaskAsynchronously(BSkyBlock.getInstance(), () -> save());
} else {
save();
}
}
/**
* Sets the challenge as complete and increments the number of times it has been completed
* @param user
* @param uniqueId
*/
public void setChallengeComplete(User user, String uniqueId) {
// TODO Auto-generated method stub
}
/**
* Checks number of challenges
* @return true if no challenges
*/
public boolean isFirstTime() {
return challengeList.isEmpty();
}
/**
* @return the challengeList
*/
public LinkedHashMap<ChallengeLevels, Set<Challenges>> getChallengeList() {
return challengeList;
}
/**
* @param challengeList the challengeList to set
*/
public void setChallengeList(LinkedHashMap<ChallengeLevels, Set<Challenges>> challengeList) {
this.challengeList = challengeList;
public void storeLevel(ChallengeLevels level) {
lvConfig.saveConfigObject(level);
}
}

View File

@ -19,33 +19,63 @@ import org.bukkit.inventory.meta.SpawnEggMeta;
import org.bukkit.potion.PotionData;
import org.bukkit.potion.PotionType;
import bskyblock.addon.challenges.database.object.ChallengeLevels;
import bskyblock.addon.challenges.database.object.Challenges;
public class FreshSqueezedChallenges {
private static final boolean DEBUG = false;
ChallengesAddon addon;
YamlConfiguration chal;
public FreshSqueezedChallenges(ChallengesAddon challengesAddon) {
this.addon = challengesAddon;
makeChallenges();
addon.getChallengesManager().save(true);
}
/**
* Makes the level 1 challenges
*/
private void makeChallenges() {
File challengeFile = new File(addon.getDataFolder(), "challenges.yml");
if (!challengeFile.exists()) {
addon.saveResource("challenges.yml",false);
}
YamlConfiguration chal = new YamlConfiguration();
chal = new YamlConfiguration();
try {
chal.load(challengeFile);
} catch (IOException | InvalidConfigurationException e) {
addon.getLogger().severe("Could not set up initial challenges");
}
makeLevels();
makeChallenges();
addon.getChallengesManager().save(true);
}
private void makeLevels() {
// Parse the levels
String levels = chal.getString("challenges.levels", "");
if (!levels.isEmpty()) {
String[] lvs = levels.split(" ");
int order = 0;
for (String level : lvs) {
ChallengeLevels challengeLevel = new ChallengeLevels();
challengeLevel.setFriendlyName(level);
challengeLevel.setUniqueId(level);
challengeLevel.setOrder(order++);
challengeLevel.setWaiveramount(chal.getInt("challenges.waiveramount"));
// Check if there is a level reward
ConfigurationSection unlock = chal.getConfigurationSection("challenges.levelUnlock." + level);
if (unlock != null) {
challengeLevel.setUnlockMessage(unlock.getString("message"));
challengeLevel.setRewardDescription(unlock.getString("rewardDesc",""));
challengeLevel.setRewardItems(parseItems(unlock.getString("itemReward","")));
challengeLevel.setMoneyReward(unlock.getInt("moneyReward"));
challengeLevel.setExpReward(unlock.getInt("expReward"));
challengeLevel.setRewardCommands(unlock.getStringList("commands"));
}
addon.getChallengesManager().storeLevel(challengeLevel);
}
}
}
/**
* Imports challenges
*/
private void makeChallenges() {
// Parse the challenge file
ConfigurationSection chals = chal.getConfigurationSection("challenges.challengeList");
for (String challenge : chals.getKeys(false)) {

View File

@ -3,33 +3,50 @@ package bskyblock.addon.challenges.database.object;
import java.util.ArrayList;
import java.util.List;
import org.bukkit.inventory.ItemStack;
import bskyblock.addon.challenges.ChallengesManager;
import us.tastybento.bskyblock.api.configuration.ConfigComment;
import us.tastybento.bskyblock.database.objects.DataObject;
/**
* Represent a challenge level
* @author tastybento
*
*/
public class ChallengeLevels implements DataObject, Comparable<ChallengeLevels> {
/**
* A friendly name for the level. If blank, level name is used.
*/
@ConfigComment("A friendly name for the level. If blank, level name is used.")
private String friendlyName = "";
/**
* Commands to run when this level is completed
*/
@ConfigComment("Commands to run when this level is completed")
private List<String> rewardCommands = new ArrayList<>();
/**
* Level name
*/
@ConfigComment("Level name")
private String uniqueId = ChallengesManager.FREE;
/**
* The number of undone challenges that can be left on this level before unlocking next level
*/
@ConfigComment("The number of undone challenges that can be left on this level before unlocking next level")
private int waiveramount = 1;
/**
* The ordering of the levels, lowest to highest
*/
@ConfigComment("The ordering of the levels, lowest to highest")
private int order = 0;
@ConfigComment("The message shown when unlocking this level")
private String unlockMessage = "";
@ConfigComment("Unlock reward description")
private String rewardDescription = "";
@ConfigComment("List of reward itemstacks")
private List<ItemStack> rewardItems;
@ConfigComment("Unlock experience reward")
private int expReward;
@ConfigComment("Unlock money reward")
private int moneyReward;
public String getFriendlyName() {
return friendlyName;
}
@ -112,5 +129,75 @@ public class ChallengeLevels implements DataObject, Comparable<ChallengeLevels>
}
return true;
}
/**
* @return the rewardDescription
*/
public String getRewardDescription() {
return rewardDescription;
}
/**
* @param rewardDescription the rewardDescription to set
*/
public void setRewardDescription(String rewardDescription) {
this.rewardDescription = rewardDescription;
}
/**
* @return the rewardItems
*/
public List<ItemStack> getRewardItems() {
return rewardItems;
}
/**
* @param rewardItems the rewardItems to set
*/
public void setRewardItems(List<ItemStack> rewardItems) {
this.rewardItems = rewardItems;
}
/**
* @return the expReward
*/
public int getExpReward() {
return expReward;
}
/**
* @param expReward the expReward to set
*/
public void setExpReward(int expReward) {
this.expReward = expReward;
}
/**
* @return the moneyReward
*/
public int getMoneyReward() {
return moneyReward;
}
/**
* @param moneyReward the moneyReward to set
*/
public void setMoneyReward(int moneyReward) {
this.moneyReward = moneyReward;
}
/**
* @return the unlockMessage
*/
public String getUnlockMessage() {
return unlockMessage;
}
/**
* @param unlockMessage the unlockMessage to set
*/
public void setUnlockMessage(String unlockMessage) {
this.unlockMessage = unlockMessage;
}
}

View File

@ -35,6 +35,7 @@ public class ChallengesPanels {
* @return
*/
public void getChallenges(User user) {
// Get the challenge level this player is on
getChallenges(user, "");
}