mirror of
https://github.com/BentoBoxWorld/Challenges.git
synced 2024-12-31 21:48:00 +01:00
Added comments to challenges.
This commit is contained in:
parent
763a8de8e4
commit
7d22e9bfc5
@ -28,6 +28,7 @@ challenges:
|
||||
completechallenge:
|
||||
challange-completed: "Challenge: [challengename] has been completed for [name]"
|
||||
error:
|
||||
could-not-save: "&cCould not save the challenge!"
|
||||
island-level: "&cYour island must be level [level] to complete this challenge!"
|
||||
items-not-there: "&cAll required items must be close to you on your island!"
|
||||
no-items-clicked: "&cYou did not click on anything. Cancelling."
|
||||
@ -47,4 +48,19 @@ challenges:
|
||||
resetchallenge:
|
||||
challenge-reset: "Challenge: [challengename] has been reset for [name]"
|
||||
error-challenge-does-not-exist: "Challenge doesn't exist or isn't yet completed"
|
||||
|
||||
admin:
|
||||
parameters: ""
|
||||
description: "admin command to create challenges"
|
||||
create:
|
||||
inventory:
|
||||
parameters: "[challenge name]"
|
||||
description: "create an inventory challenge"
|
||||
surrounding:
|
||||
parameters: "[challenge name]"
|
||||
description: "create a surrounding challenge"
|
||||
hit-things: "Hit things to add them to the list of things required. Right click when done."
|
||||
error:
|
||||
no-name: "You must include a challenge name"
|
||||
challenge-created: "[challenge] created!"
|
||||
you-added: "You added one [thing] to the challenge"
|
||||
|
@ -37,8 +37,6 @@ public class ChallengesAddon extends Addon {
|
||||
|
||||
@Override
|
||||
public void onDisable(){
|
||||
if (challengesManager != null)
|
||||
challengesManager.save(false);
|
||||
}
|
||||
|
||||
public ChallengesManager getChallengesManager() {
|
||||
|
@ -1,10 +1,6 @@
|
||||
package bskyblock.addon.challenges;
|
||||
|
||||
import java.beans.IntrospectionException;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@ -16,7 +12,6 @@ import org.bukkit.ChatColor;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.inventory.Inventory;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.inventory.meta.ItemMeta;
|
||||
|
||||
import bskyblock.addon.challenges.commands.admin.SurroundChallengeBuilder;
|
||||
import bskyblock.addon.challenges.database.object.ChallengeLevels;
|
||||
@ -25,308 +20,39 @@ import bskyblock.addon.challenges.database.object.Challenges.ChallengeType;
|
||||
import bskyblock.addon.challenges.panel.ChallengesPanels;
|
||||
import us.tastybento.bskyblock.BSkyBlock;
|
||||
import us.tastybento.bskyblock.api.commands.User;
|
||||
import us.tastybento.bskyblock.database.flatfile.FlatFileDatabase;
|
||||
import us.tastybento.bskyblock.database.managers.AbstractDatabaseHandler;
|
||||
import us.tastybento.bskyblock.api.configuration.BSBConfig;
|
||||
|
||||
public class ChallengesManager {
|
||||
|
||||
//private static final boolean DEBUG = false;
|
||||
public static final String FREE = "Free";
|
||||
|
||||
private ChallengesAddon addon;
|
||||
private LinkedHashMap<ChallengeLevels, List<Challenges>> challengeList;
|
||||
|
||||
private AbstractDatabaseHandler<Challenges> chHandler;
|
||||
private AbstractDatabaseHandler<ChallengeLevels> lvHandler;
|
||||
|
||||
|
||||
private BSBConfig<Challenges> chConfig;
|
||||
private BSBConfig<ChallengeLevels> lvConfig;
|
||||
private ChallengesPanels challengesPanels;
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public ChallengesManager(ChallengesAddon plugin) {
|
||||
this.addon = plugin;
|
||||
// Set up the database handler to store and retrieve Challenges
|
||||
chHandler = (AbstractDatabaseHandler<Challenges>) new FlatFileDatabase().getHandler(Challenges.class);
|
||||
lvHandler = (AbstractDatabaseHandler<ChallengeLevels>) new FlatFileDatabase().getHandler(ChallengeLevels.class);
|
||||
public ChallengesManager(ChallengesAddon addon) {
|
||||
// Set up the configs
|
||||
chConfig = new BSBConfig<Challenges>(addon, Challenges.class);
|
||||
lvConfig = new BSBConfig<ChallengeLevels>(addon, ChallengeLevels.class);
|
||||
challengeList = new LinkedHashMap<>();
|
||||
// Start panels
|
||||
challengesPanels = new ChallengesPanels(plugin, this);
|
||||
challengesPanels = new ChallengesPanels(addon, this);
|
||||
load();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the challengesPanels
|
||||
*/
|
||||
public ChallengesPanels getChallengesPanels() {
|
||||
return challengesPanels;
|
||||
}
|
||||
|
||||
public AbstractDatabaseHandler<Challenges> getHandler() {
|
||||
return chHandler;
|
||||
}
|
||||
|
||||
/**
|
||||
* Clear and reload all challenges
|
||||
*/
|
||||
public void load() {
|
||||
// Load the challenges
|
||||
challengeList.clear();
|
||||
try {
|
||||
for (Challenges challenge : chHandler.loadObjects()) {
|
||||
Bukkit.getLogger().info("DEBUG: Loading challenge " + challenge.getFriendlyName() + " level " + challenge.getLevel());
|
||||
// See if we have this level already
|
||||
ChallengeLevels level;
|
||||
if (lvHandler.objectExists(challenge.getLevel())) {
|
||||
Bukkit.getLogger().info("DEBUG: Level contains level " + challenge.getLevel());
|
||||
// Get it from the database
|
||||
level = lvHandler.loadObject(challenge.getLevel());
|
||||
} else {
|
||||
Bukkit.getLogger().info("DEBUG: Level does not contains level " + challenge.getLevel());
|
||||
// Make it
|
||||
level = new ChallengeLevels();
|
||||
level.setUniqueId(challenge.getLevel());
|
||||
Bukkit.getLogger().info("DEBUG: Level unique Id set to " + level.getUniqueId());
|
||||
lvHandler.saveObject(level);
|
||||
}
|
||||
if (challengeList.containsKey(level)) {
|
||||
Bukkit.getLogger().info("DEBUG: Challenge contains level " + level.getUniqueId());
|
||||
challengeList.get(level).add(challenge);
|
||||
} else {
|
||||
Bukkit.getLogger().info("DEBUG: No key found");
|
||||
// First challenge of this level type
|
||||
List<Challenges> challenges = new ArrayList<>();
|
||||
challenges.add(challenge);
|
||||
challengeList.put(level, challenges);
|
||||
}
|
||||
}
|
||||
} catch (InstantiationException | IllegalAccessException | IllegalArgumentException
|
||||
| InvocationTargetException | SecurityException | ClassNotFoundException | IntrospectionException
|
||||
| SQLException | NoSuchMethodException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
Bukkit.getLogger().info("DEBUG: " + challengeList.size());
|
||||
// Sort the challenge list into level order
|
||||
challengeList = challengeList.entrySet().stream()
|
||||
.sorted(Map.Entry.comparingByKey())
|
||||
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue,
|
||||
(oldValue, newValue) -> oldValue, LinkedHashMap::new));
|
||||
Bukkit.getLogger().info("DEBUG: " + challengeList.size());
|
||||
}
|
||||
|
||||
/**
|
||||
* Save to the database
|
||||
* @param async - if true, saving will be done async
|
||||
*/
|
||||
public void save(boolean async){
|
||||
if(async){
|
||||
Runnable save = () -> {
|
||||
for (Entry<ChallengeLevels, List<Challenges>> en : challengeList.entrySet()) {
|
||||
try {
|
||||
lvHandler.saveObject(en.getKey());
|
||||
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException
|
||||
| SecurityException | InstantiationException | NoSuchMethodException
|
||||
| IntrospectionException | SQLException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
for (Challenges challenge : en.getValue()) {
|
||||
try {
|
||||
chHandler.saveObject(challenge);
|
||||
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException
|
||||
| SecurityException | InstantiationException | NoSuchMethodException
|
||||
| IntrospectionException | SQLException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
BSkyBlock.getInstance().getServer().getScheduler().runTaskAsynchronously(BSkyBlock.getInstance(), save);
|
||||
} else {
|
||||
for (Entry<ChallengeLevels, List<Challenges>> en : challengeList.entrySet()) {
|
||||
try {
|
||||
lvHandler.saveObject(en.getKey());
|
||||
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException
|
||||
| SecurityException | InstantiationException | NoSuchMethodException | IntrospectionException
|
||||
| SQLException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
for (Challenges challenge : en.getValue()) {
|
||||
try {
|
||||
chHandler.saveObject(challenge);
|
||||
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException
|
||||
| SecurityException | InstantiationException | NoSuchMethodException
|
||||
| IntrospectionException | SQLException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void shutdown(){
|
||||
save(false);
|
||||
challengeList.clear();
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a challenge from the inventory contents
|
||||
* @param contents
|
||||
*/
|
||||
public void createChallenge(User user, String name) {
|
||||
// Get the main icon
|
||||
ItemStack icon = user.getInventory().getItemInOffHand();
|
||||
if (icon == null || icon.getType().equals(Material.AIR)) {
|
||||
user.sendRawMessage("Hold something in your off-hand to make it the icon. Icon will be paper be default.");
|
||||
icon = new ItemStack(Material.PAPER);
|
||||
}
|
||||
icon.setAmount(1);
|
||||
ItemMeta meta = icon.getItemMeta();
|
||||
meta.setDisplayName(name);
|
||||
List<String> lore = new ArrayList<>();
|
||||
lore.add("Required items:");
|
||||
|
||||
List<ItemStack> inv = Arrays.asList(user.getInventory().getStorageContents());
|
||||
List<ItemStack> contents = new ArrayList<>();
|
||||
for (ItemStack item : inv) {
|
||||
if (item != null && !item.getType().equals(Material.AIR)) {
|
||||
contents.add(item);
|
||||
lore.add(item.getType() + " x " + item.getAmount());
|
||||
}
|
||||
}
|
||||
if (lore.size() == 1) {
|
||||
lore.add("No items");
|
||||
}
|
||||
meta.setDisplayName(name);
|
||||
meta.setLore(lore);
|
||||
icon.setItemMeta(meta);
|
||||
Challenges newChallenge = new Challenges();
|
||||
newChallenge.setRequiredItems(contents);
|
||||
newChallenge.setUniqueId(name);
|
||||
newChallenge.setIcon(icon);
|
||||
if (chHandler.objectExists(name)) {
|
||||
user.sendRawMessage(ChatColor.RED + "Challenge already exists! Use /c replace <name>");
|
||||
return;
|
||||
}
|
||||
try {
|
||||
chHandler.saveObject(newChallenge);
|
||||
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException | SecurityException
|
||||
| InstantiationException | NoSuchMethodException | IntrospectionException | SQLException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
user.sendRawMessage(ChatColor.RED + "Challenge creation failed! " + e.getMessage());
|
||||
return;
|
||||
}
|
||||
user.sendRawMessage("Challenge accepted!");
|
||||
// TODO ADD CHALLENGE
|
||||
//challenges.put(newChallenge.getUniqueId(), newChallenge);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the list of challenges for this level
|
||||
* @param level - the level required
|
||||
* @return the list of challenges for this level, or the first set of challenges if level is blank, or a blank list if there are no challenges
|
||||
*/
|
||||
public List<Challenges> getChallenges(String level) {
|
||||
return challengeList.getOrDefault(level, challengeList.isEmpty() ? new ArrayList<Challenges>() : challengeList.values().iterator().next());
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if a challenge is complete or not
|
||||
* @param uniqueId - unique ID - player's UUID
|
||||
* @param uniqueId2 - Challenge id
|
||||
* @return - true if completed
|
||||
*/
|
||||
public boolean isChallengeComplete(User user, String uniqueId2) {
|
||||
public long checkChallengeTimes(User user, Challenges challenge) {
|
||||
// TODO Auto-generated method stub
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean isLevelComplete(User user, ChallengeLevels otherLevel) {
|
||||
// TODO Auto-generated method stub
|
||||
return false;
|
||||
}
|
||||
|
||||
public ChallengeLevels getPreviousLevel(ChallengeLevels otherLevel) {
|
||||
ChallengeLevels result = null;
|
||||
|
||||
for (ChallengeLevels level : challengeList.keySet()) {
|
||||
if (level.equals(otherLevel)) {
|
||||
return result;
|
||||
}
|
||||
result = level;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the status on every level
|
||||
* @param user
|
||||
* @return Level name, how many challenges still to do on which level
|
||||
*/
|
||||
public List<LevelStatus> getChallengeLevelStatus(User user) {
|
||||
List<LevelStatus> result = new ArrayList<>();
|
||||
ChallengeLevels previousLevel = null;
|
||||
for (Entry<ChallengeLevels, List<Challenges>> en : challengeList.entrySet()) {
|
||||
int challsToDo = 0; // TODO - calculate how many challenges still to do for this player
|
||||
boolean complete = false; // TODO
|
||||
result.add(new LevelStatus(en.getKey(), previousLevel, challsToDo, complete));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public class LevelStatus {
|
||||
private final ChallengeLevels level;
|
||||
private final ChallengeLevels previousLevel;
|
||||
private final int numberOfChallengesStillToDo;
|
||||
private final boolean complete;
|
||||
|
||||
public LevelStatus(ChallengeLevels level, ChallengeLevels previousLevel, int numberOfChallengesStillToDo, boolean complete) {
|
||||
super();
|
||||
this.level = level;
|
||||
this.previousLevel = previousLevel;
|
||||
this.numberOfChallengesStillToDo = numberOfChallengesStillToDo;
|
||||
this.complete = complete;
|
||||
}
|
||||
/**
|
||||
* @return the level
|
||||
*/
|
||||
public ChallengeLevels getLevel() {
|
||||
return level;
|
||||
}
|
||||
/**
|
||||
* @return the previousLevel
|
||||
*/
|
||||
public ChallengeLevels getPreviousLevel() {
|
||||
return previousLevel;
|
||||
}
|
||||
/**
|
||||
* @return the numberOfChallengesStillToDo
|
||||
*/
|
||||
public int getNumberOfChallengesStillToDo() {
|
||||
return numberOfChallengesStillToDo;
|
||||
}
|
||||
/**
|
||||
* @return the complete
|
||||
*/
|
||||
public boolean isComplete() {
|
||||
return complete;
|
||||
}
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates an inventory challenge
|
||||
* @param user
|
||||
* @param inventory
|
||||
* @param user - the user who is making the challenge
|
||||
* @param inventory - the inventory that will be used to make the challenge
|
||||
*/
|
||||
public void createInvChallenge(User user, Inventory inventory) {
|
||||
public boolean createInvChallenge(User user, Inventory inventory) {
|
||||
if (inventory.getContents().length == 0) {
|
||||
return;
|
||||
return false;
|
||||
}
|
||||
Challenges newChallenge = new Challenges();
|
||||
newChallenge.setChallengeType(ChallengeType.INVENTORY);
|
||||
@ -357,17 +83,100 @@ public class ChallengesManager {
|
||||
});
|
||||
|
||||
// Save the challenge
|
||||
try {
|
||||
chHandler.saveObject(newChallenge);
|
||||
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException | SecurityException
|
||||
| InstantiationException | NoSuchMethodException | IntrospectionException | SQLException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
user.sendRawMessage(ChatColor.RED + "Challenge creation failed! " + e.getMessage());
|
||||
return;
|
||||
if (!chConfig.saveConfigObject(newChallenge)) {
|
||||
user.sendRawMessage(ChatColor.RED + "Challenge creation failed!");
|
||||
return false;
|
||||
}
|
||||
|
||||
user.sendRawMessage("Success");
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a surrounding challenge
|
||||
* @param challengeInfo - info on the challenge from the builder
|
||||
* @return true if successful, false if not
|
||||
*/
|
||||
public boolean createSurroundingChallenge(SurroundChallengeBuilder challengeInfo) {
|
||||
if (challengeInfo.getReqBlocks().isEmpty() && challengeInfo.getReqEntities().isEmpty()) {
|
||||
challengeInfo.getOwner().sendMessage("challenges.error.no-items-clicked");
|
||||
return false;
|
||||
}
|
||||
Challenges newChallenge = new Challenges();
|
||||
newChallenge.setChallengeType(ChallengeType.SURROUNDING);
|
||||
newChallenge.setFriendlyName(challengeInfo.getName());
|
||||
newChallenge.setDeployed(true);
|
||||
newChallenge.setRequiredBlocks(challengeInfo.getReqBlocks());
|
||||
newChallenge.setRequiredEntities(challengeInfo.getReqEntities());
|
||||
newChallenge.setUniqueId(challengeInfo.getName());
|
||||
newChallenge.setIcon(new ItemStack(Material.ARMOR_STAND));
|
||||
newChallenge.setFreeChallenge(true);
|
||||
newChallenge.setLevel(FREE);
|
||||
|
||||
// Save the challenge
|
||||
if (!chConfig.saveConfigObject(newChallenge)) {
|
||||
challengeInfo.getOwner().sendMessage("challenges.error.could-not-save");
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the status on every level
|
||||
* @param user
|
||||
* @return Level name, how many challenges still to do on which level
|
||||
*/
|
||||
public List<LevelStatus> getChallengeLevelStatus(User user) {
|
||||
List<LevelStatus> result = new ArrayList<>();
|
||||
ChallengeLevels previousLevel = null;
|
||||
for (Entry<ChallengeLevels, List<Challenges>> en : challengeList.entrySet()) {
|
||||
int challsToDo = 0; // TODO - calculate how many challenges still to do for this player
|
||||
boolean complete = false; // TODO
|
||||
result.add(new LevelStatus(en.getKey(), previousLevel, challsToDo, complete));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the list of challenges for this level
|
||||
* @param level - the level required
|
||||
* @return the list of challenges for this level, or the first set of challenges if level is blank, or a blank list if there are no challenges
|
||||
*/
|
||||
public List<Challenges> getChallenges(String level) {
|
||||
return challengeList.getOrDefault(level, challengeList.isEmpty() ? new ArrayList<Challenges>() : challengeList.values().iterator().next());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the challengesPanels
|
||||
*/
|
||||
public ChallengesPanels getChallengesPanels() {
|
||||
return challengesPanels;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the previous level to the one supplied
|
||||
* @param currentLevel - the current level
|
||||
* @return the previous level, or null if there is none
|
||||
*/
|
||||
public ChallengeLevels getPreviousLevel(ChallengeLevels currentLevel) {
|
||||
ChallengeLevels result = null;
|
||||
for (ChallengeLevels level : challengeList.keySet()) {
|
||||
if (level.equals(currentLevel)) {
|
||||
return result;
|
||||
}
|
||||
result = level;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if a challenge is complete or not
|
||||
* @param uniqueId - unique ID - player's UUID
|
||||
* @param uniqueId2 - Challenge id
|
||||
* @return - true if completed
|
||||
*/
|
||||
public boolean isChallengeComplete(User user, String uniqueId2) {
|
||||
// TODO Auto-generated method stub
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean isLevelAvailable(User user, String level) {
|
||||
@ -375,13 +184,70 @@ public class ChallengesManager {
|
||||
return false;
|
||||
}
|
||||
|
||||
public long checkChallengeTimes(User user, Challenges challenge) {
|
||||
public boolean isLevelComplete(User user, ChallengeLevels otherLevel) {
|
||||
// TODO Auto-generated method stub
|
||||
return 0;
|
||||
return false;
|
||||
}
|
||||
|
||||
public ChallengesAddon getAddon() {
|
||||
return addon;
|
||||
/**
|
||||
* Clear and reload all challenges
|
||||
*/
|
||||
public void load() {
|
||||
// Load the challenges
|
||||
challengeList.clear();
|
||||
for (Challenges challenge : chConfig.loadConfigObjects()) {
|
||||
Bukkit.getLogger().info("Loading challenge " + challenge.getFriendlyName() + " level " + challenge.getLevel());
|
||||
// See if we have this level already
|
||||
ChallengeLevels level;
|
||||
if (lvConfig.configObjectExists(challenge.getLevel())) {
|
||||
//Bukkit.getLogger().info("DEBUG: Level contains level " + challenge.getLevel());
|
||||
// Get it from the database
|
||||
level = lvConfig.loadConfigObject(challenge.getLevel());
|
||||
} else {
|
||||
//Bukkit.getLogger().info("DEBUG: Level does not contains level " + challenge.getLevel());
|
||||
// Make it
|
||||
level = new ChallengeLevels();
|
||||
level.setUniqueId(challenge.getLevel());
|
||||
//Bukkit.getLogger().info("DEBUG: Level unique Id set to " + level.getUniqueId());
|
||||
lvConfig.saveConfigObject(level);
|
||||
}
|
||||
if (challengeList.containsKey(level)) {
|
||||
//Bukkit.getLogger().info("DEBUG: Challenge contains level " + level.getUniqueId());
|
||||
challengeList.get(level).add(challenge);
|
||||
} else {
|
||||
//Bukkit.getLogger().info("DEBUG: No key found");
|
||||
// First challenge of this level type
|
||||
List<Challenges> challenges = new ArrayList<>();
|
||||
challenges.add(challenge);
|
||||
challengeList.put(level, challenges);
|
||||
}
|
||||
}
|
||||
//Bukkit.getLogger().info("DEBUG: " + challengeList.size());
|
||||
// Sort the challenge list into level order
|
||||
challengeList = challengeList.entrySet().stream()
|
||||
.sorted(Map.Entry.comparingByKey())
|
||||
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue,
|
||||
(oldValue, newValue) -> oldValue, LinkedHashMap::new));
|
||||
//Bukkit.getLogger().info("DEBUG: " + challengeList.size());
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@ -394,30 +260,4 @@ public class ChallengesManager {
|
||||
|
||||
}
|
||||
|
||||
public boolean createSurroundingChallenge(SurroundChallengeBuilder inProgress) {
|
||||
if (inProgress.getReqBlocks().isEmpty() && inProgress.getReqEntities().isEmpty()) {
|
||||
inProgress.getOwner().sendMessage("challenges.error.no-items-clicked");
|
||||
return false;
|
||||
}
|
||||
Challenges newChallenge = new Challenges();
|
||||
newChallenge.setChallengeType(ChallengeType.SURROUNDING);
|
||||
newChallenge.setFriendlyName(inProgress.getName());
|
||||
newChallenge.setDeployed(true);
|
||||
newChallenge.setRequiredBlocks(inProgress.getReqBlocks());
|
||||
newChallenge.setRequiredEntities(inProgress.getReqEntities());
|
||||
newChallenge.setUniqueId(inProgress.getName());
|
||||
newChallenge.setIcon(new ItemStack(Material.ARMOR_STAND));
|
||||
newChallenge.setFreeChallenge(true);
|
||||
newChallenge.setLevel(FREE);
|
||||
|
||||
// Save the challenge
|
||||
try {
|
||||
chHandler.saveObject(newChallenge);
|
||||
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException | SecurityException
|
||||
| InstantiationException | NoSuchMethodException | IntrospectionException | SQLException e) {
|
||||
addon.getLogger().severe("Could not save challenge!");
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
49
src/bskyblock/addon/challenges/LevelStatus.java
Normal file
49
src/bskyblock/addon/challenges/LevelStatus.java
Normal file
@ -0,0 +1,49 @@
|
||||
package bskyblock.addon.challenges;
|
||||
|
||||
import bskyblock.addon.challenges.database.object.ChallengeLevels;
|
||||
|
||||
/**
|
||||
* Level status class
|
||||
* @author tastybento
|
||||
*
|
||||
*/
|
||||
public class LevelStatus {
|
||||
private final ChallengeLevels level;
|
||||
private final ChallengeLevels previousLevel;
|
||||
private final int numberOfChallengesStillToDo;
|
||||
private final boolean complete;
|
||||
|
||||
public LevelStatus(ChallengeLevels level, ChallengeLevels previousLevel, int numberOfChallengesStillToDo, boolean complete) {
|
||||
super();
|
||||
this.level = level;
|
||||
this.previousLevel = previousLevel;
|
||||
this.numberOfChallengesStillToDo = numberOfChallengesStillToDo;
|
||||
this.complete = complete;
|
||||
}
|
||||
/**
|
||||
* @return the level
|
||||
*/
|
||||
public ChallengeLevels getLevel() {
|
||||
return level;
|
||||
}
|
||||
/**
|
||||
* @return the numberOfChallengesStillToDo
|
||||
*/
|
||||
public int getNumberOfChallengesStillToDo() {
|
||||
return numberOfChallengesStillToDo;
|
||||
}
|
||||
/**
|
||||
* @return the previousLevel
|
||||
*/
|
||||
public ChallengeLevels getPreviousLevel() {
|
||||
return previousLevel;
|
||||
}
|
||||
/**
|
||||
* @return the complete
|
||||
*/
|
||||
public boolean isComplete() {
|
||||
return complete;
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -20,7 +20,13 @@ import bskyblock.addon.challenges.ChallengesAddon;
|
||||
import us.tastybento.bskyblock.Constants;
|
||||
import us.tastybento.bskyblock.api.commands.CompositeCommand;
|
||||
import us.tastybento.bskyblock.api.commands.User;
|
||||
import us.tastybento.bskyblock.util.Util;
|
||||
|
||||
/**
|
||||
* Command to create a surrounding type challenge
|
||||
* @author tastybento
|
||||
*
|
||||
*/
|
||||
public class CreateSurrounding extends CompositeCommand implements Listener {
|
||||
|
||||
|
||||
@ -48,11 +54,11 @@ public class CreateSurrounding extends CompositeCommand implements Listener {
|
||||
@Override
|
||||
public boolean execute(User user, List<String> args) {
|
||||
if (args.isEmpty()) {
|
||||
user.sendRawMessage("not enough args");
|
||||
user.sendMessage("challenges.admin.error.no-name");
|
||||
return false;
|
||||
}
|
||||
// Tell user to hit objects to add to the surrounding object requirements
|
||||
user.sendRawMessage("Hit things to add them to the list of things required. Right click when done");
|
||||
user.sendMessage("challenges.admin.create.surrounding.hit-things");
|
||||
inProgress.put(user.getUniqueId(), new SurroundChallengeBuilder(addon).owner(user).name(args.get(0)));
|
||||
return true;
|
||||
}
|
||||
@ -70,17 +76,15 @@ public class CreateSurrounding extends CompositeCommand implements Listener {
|
||||
@EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true)
|
||||
public boolean onPlayerInteract(PlayerInteractEvent e) {
|
||||
if (e.getAction().equals(Action.LEFT_CLICK_BLOCK)) {
|
||||
addon.getLogger().info("DEBUG: left click");
|
||||
if (inProgress.containsKey(e.getPlayer().getUniqueId())) {
|
||||
// Prevent damage
|
||||
e.setCancelled(true);
|
||||
inProgress.get(e.getPlayer().getUniqueId()).addBlock(e.getClickedBlock().getType());
|
||||
User.getInstance(e.getPlayer()).sendRawMessage("You added one " + e.getClickedBlock().getType());
|
||||
User.getInstance(e.getPlayer()).sendMessage("challenges.admin.you-added", "[thing]", Util.prettifyText(e.getClickedBlock().getType().toString()));
|
||||
return true;
|
||||
}
|
||||
}
|
||||
if (e.getAction().equals(Action.RIGHT_CLICK_BLOCK)) {
|
||||
addon.getLogger().info("DEBUG: right click");
|
||||
return finished(e, e.getPlayer().getUniqueId());
|
||||
}
|
||||
return false;
|
||||
@ -90,6 +94,9 @@ public class CreateSurrounding extends CompositeCommand implements Listener {
|
||||
if (inProgress.containsKey(uuid)) {
|
||||
e.setCancelled(true);
|
||||
boolean status = inProgress.get(uuid).build();
|
||||
if (status) {
|
||||
inProgress.get(uuid).getOwner().sendMessage("challenges.admin.challenge-created", "[challenge]", inProgress.get(uuid).getName());
|
||||
}
|
||||
inProgress.remove(uuid);
|
||||
return status;
|
||||
}
|
||||
@ -98,13 +105,11 @@ public class CreateSurrounding extends CompositeCommand implements Listener {
|
||||
|
||||
@EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true)
|
||||
public boolean onPlayerInteract(PlayerInteractAtEntityEvent e) {
|
||||
addon.getLogger().info("DEBUG: right click entity");
|
||||
return finished(e, e.getPlayer().getUniqueId());
|
||||
}
|
||||
|
||||
@EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true)
|
||||
public boolean onLeft(EntityDamageByEntityEvent e) {
|
||||
addon.getLogger().info("DEBUG: left click entity");
|
||||
if (!(e.getDamager() instanceof Player)) {
|
||||
return false;
|
||||
}
|
||||
@ -113,7 +118,7 @@ public class CreateSurrounding extends CompositeCommand implements Listener {
|
||||
// Prevent damage
|
||||
e.setCancelled(true);
|
||||
inProgress.get(player.getUniqueId()).addEntity(e.getEntityType());
|
||||
User.getInstance(player).sendRawMessage("You added one " + e.getEntityType());
|
||||
User.getInstance(player).sendMessage("challenges.admin.you-added", "[thing]", Util.prettifyText(e.getEntityType().toString()));
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
|
@ -12,159 +12,124 @@ import org.bukkit.entity.EntityType;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
|
||||
import bskyblock.addon.challenges.ChallengesManager;
|
||||
import us.tastybento.bskyblock.api.configuration.ConfigComment;
|
||||
import us.tastybento.bskyblock.database.objects.DataObject;
|
||||
|
||||
public class Challenges implements DataObject {
|
||||
|
||||
public enum ChallengeType {
|
||||
/**
|
||||
* This challenge only shows and icon in the GUI and doesn't do anything.
|
||||
*/
|
||||
ICON,
|
||||
/**
|
||||
* The player must have the items on them.
|
||||
*/
|
||||
INVENTORY,
|
||||
/**
|
||||
* The island level has to be equal or over this amount. Only works if there's an island level plugin installed.
|
||||
*/
|
||||
LEVEL,
|
||||
/**
|
||||
* Items or required entities have to be within x blocks of the player.
|
||||
*/
|
||||
SURROUNDING
|
||||
/**
|
||||
* This challenge only shows and icon in the GUI and doesn't do anything.
|
||||
*/
|
||||
ICON,
|
||||
/**
|
||||
* The player must have the items on them.
|
||||
*/
|
||||
INVENTORY,
|
||||
/**
|
||||
* The island level has to be equal or over this amount. Only works if there's an island level plugin installed.
|
||||
*/
|
||||
LEVEL,
|
||||
/**
|
||||
* Items or required entities have to be within x blocks of the player.
|
||||
*/
|
||||
SURROUNDING
|
||||
}
|
||||
|
||||
/**
|
||||
* Required blocks for SURROUNDING challenges
|
||||
*/
|
||||
|
||||
@ConfigComment("This is a map of the blocks required in a SURROUNDING challenge. Material, Integer")
|
||||
private Map<Material, Integer> requiredBlocks;
|
||||
|
||||
/**
|
||||
* Type of challenge
|
||||
*/
|
||||
|
||||
@ConfigComment("Challenge type can be ICON, INVENTORY, LEVEL or SURROUNDING.")
|
||||
private ChallengeType challengeType;
|
||||
/**
|
||||
* Whether this challenge is deployed or not
|
||||
*/
|
||||
|
||||
@ConfigComment("Whether this challenge is deployed or not")
|
||||
private boolean deployed;
|
||||
/**
|
||||
* Description of the challenge. Will become the lore on the icon. Can include color codes.
|
||||
*/
|
||||
|
||||
@ConfigComment("Description of the challenge. Will become the lore on the icon. Can include & color codes. String List.")
|
||||
private List<String> description = new ArrayList<>();
|
||||
/**
|
||||
* Experience point reward
|
||||
*/
|
||||
|
||||
@ConfigComment("Experience point reward")
|
||||
private int expReward;
|
||||
/**
|
||||
* This challenge can be completed at any time
|
||||
*/
|
||||
|
||||
@ConfigComment("This challenge can be completed at any time")
|
||||
private boolean freeChallenge;
|
||||
/**
|
||||
* Name of the icon and challenge. May include color codes.
|
||||
*/
|
||||
|
||||
@ConfigComment("Name of the icon and challenge. May include color codes. Single line.")
|
||||
private String friendlyName = "";
|
||||
/**
|
||||
* The icon in the GUI for this challenge
|
||||
*/
|
||||
|
||||
@ConfigComment("The icon in the GUI for this challenge. ItemStack.")
|
||||
private ItemStack icon = new ItemStack(Material.PAPER);
|
||||
/**
|
||||
* The challenge level. Default is Free
|
||||
*/
|
||||
|
||||
@ConfigComment("Challenge level. Default is Free")
|
||||
private String level = ChallengesManager.FREE;
|
||||
/**
|
||||
* Maximum number of times the challenge can be repeated
|
||||
*/
|
||||
|
||||
@ConfigComment("Maximum number of times the challenge can be repeated")
|
||||
private int maxTimes = 1;
|
||||
/**
|
||||
* Money reward
|
||||
*/
|
||||
|
||||
@ConfigComment("Money reward")
|
||||
private int moneyReward;
|
||||
/**
|
||||
* If true, the challenge will disappear from the GUI when completed
|
||||
*/
|
||||
|
||||
@ConfigComment("If true, the challenge will disappear from the GUI when completed")
|
||||
private boolean removeWhenCompleted;
|
||||
/**
|
||||
* True if the challenge is repeatable
|
||||
*/
|
||||
|
||||
@ConfigComment("True if the challenge is repeatable")
|
||||
private boolean repeatable;
|
||||
/**
|
||||
* Repeat exp award
|
||||
*/
|
||||
|
||||
@ConfigComment("Repeat exp award")
|
||||
private int repeatExpReward;
|
||||
/**
|
||||
* Reward items for repeating the challenge
|
||||
*/
|
||||
|
||||
@ConfigComment("Reward items for repeating the challenge. List of ItemStacks.")
|
||||
private List<ItemStack> repeatItemReward = new ArrayList<>();
|
||||
/**
|
||||
* Repeat money award
|
||||
*/
|
||||
|
||||
@ConfigComment("Repeat money award")
|
||||
private int repeatMoneyReward;
|
||||
/**
|
||||
* Commands to run when challenge is repeated
|
||||
*/
|
||||
|
||||
@ConfigComment("Commands to run when challenge is repeated. String List.")
|
||||
private List<String> repeatRewardCommands = new ArrayList<>();
|
||||
/**
|
||||
* Description of the repeat rewards. If blank, it will be autogenerated.
|
||||
*/
|
||||
|
||||
@ConfigComment("Description of the repeat rewards. If blank, it will be autogenerated.")
|
||||
private String repeatRewardText = "";
|
||||
/**
|
||||
* Minimum amount of player experience
|
||||
*/
|
||||
|
||||
@ConfigComment("Minimum amount of player experience")
|
||||
private int reqExp;
|
||||
/**
|
||||
* Require level for this challenge.
|
||||
*/
|
||||
|
||||
@ConfigComment("Require level for this challenge. Only works if Level Addon is being used.")
|
||||
private long reqIslandlevel;
|
||||
/**
|
||||
* Minimum amount of money required
|
||||
*/
|
||||
|
||||
@ConfigComment("Minimum amount of money required")
|
||||
private int reqMoney;
|
||||
/**
|
||||
* The required permissions to see this challenge
|
||||
*/
|
||||
|
||||
@ConfigComment("The required permissions to see this challenge. String Set.")
|
||||
private Set<String> reqPerms = new HashSet<>();
|
||||
/**
|
||||
* The items that must be in the inventory to complete the challenge.
|
||||
*/
|
||||
|
||||
@ConfigComment("The items that must be in the inventory to complete the challenge. ItemStack List.")
|
||||
private List<ItemStack> requiredItems = new ArrayList<>();
|
||||
/**
|
||||
* Any entities that must be in the area for SURROUNDING type challenges
|
||||
*/
|
||||
|
||||
@ConfigComment("Any entities that must be in the area for SURROUNDING type challenges. Map EntityType, Number")
|
||||
private Map<EntityType, Integer> requiredEntities = new HashMap<>();
|
||||
/**
|
||||
* Commands to run when the player completes the challenge for the first time
|
||||
*/
|
||||
|
||||
@ConfigComment("Commands to run when the player completes the challenge for the first time. String List")
|
||||
private List<String> rewardCommands = new ArrayList<>();
|
||||
/**
|
||||
* List of items the player will receive first time
|
||||
*/
|
||||
|
||||
@ConfigComment("List of items the player will receive first time. ItemStack List.")
|
||||
private List<ItemStack> itemReward = new ArrayList<>();
|
||||
/**
|
||||
* If this is blank, the reward text will be auto-generated, otherwise this will be used.
|
||||
*/
|
||||
|
||||
@ConfigComment("If this is blank, the reward text will be auto-generated, otherwise this will be used.")
|
||||
private String rewardText = "";
|
||||
/**
|
||||
* The number of blocks around the player to search for items on an island
|
||||
*/
|
||||
|
||||
@ConfigComment("The number of blocks around the player to search for items on an island")
|
||||
private int searchRadius = 10;
|
||||
/**
|
||||
* Inventory slot where this challenge should be placed. 0 to 49.
|
||||
* A negative value means "do not care"
|
||||
*/
|
||||
|
||||
@ConfigComment("Inventory slot where this challenge should be placed. 0 to 49. A negative value means any slot")
|
||||
private int slot = -1;
|
||||
/**
|
||||
* Take the required items from the player
|
||||
*/
|
||||
|
||||
@ConfigComment("Take the required items from the player")
|
||||
private boolean takeItems = true;
|
||||
/**
|
||||
* Take the money from the player
|
||||
*/
|
||||
|
||||
@ConfigComment("Take the money from the player")
|
||||
private boolean takeMoney = false;
|
||||
|
||||
/**
|
||||
* Unique name of the challenge
|
||||
*/
|
||||
|
||||
@ConfigComment("Unique name of the challenge")
|
||||
private String uniqueId = "";
|
||||
|
||||
|
||||
|
@ -9,7 +9,7 @@ import org.bukkit.inventory.ItemStack;
|
||||
|
||||
import bskyblock.addon.challenges.ChallengesAddon;
|
||||
import bskyblock.addon.challenges.ChallengesManager;
|
||||
import bskyblock.addon.challenges.ChallengesManager.LevelStatus;
|
||||
import bskyblock.addon.challenges.LevelStatus;
|
||||
import bskyblock.addon.challenges.database.object.Challenges;
|
||||
import bskyblock.addon.challenges.database.object.Challenges.ChallengeType;
|
||||
import us.tastybento.bskyblock.api.commands.User;
|
||||
@ -150,61 +150,4 @@ public class ChallengesPanels {
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
@EventHandler(priority = EventPriority.LOWEST)
|
||||
public void onInventoryClick(InventoryClickEvent event) {
|
||||
Player player = (Player) event.getWhoClicked(); // The player that
|
||||
// clicked the item
|
||||
//UUID playerUUID = player.getUniqueId();
|
||||
Inventory inventory = event.getInventory(); // The inventory that was
|
||||
// clicked in
|
||||
// Check this is the right panel
|
||||
if (inventory.getName() == null || !inventory.getName().equals(plugin.getLocale(player).get("challenges.guiTitle"))) {
|
||||
return;
|
||||
}
|
||||
event.setCancelled(true);
|
||||
if (!event.getClick().equals(ClickType.LEFT)) {
|
||||
inventory.clear();
|
||||
player.closeInventory();
|
||||
player.updateInventory();
|
||||
return;
|
||||
}
|
||||
int slot = event.getRawSlot();
|
||||
if (slot == -999) {
|
||||
inventory.clear();
|
||||
player.closeInventory();
|
||||
event.setCancelled(true);
|
||||
return;
|
||||
}
|
||||
// TODO: Deal with the clicking
|
||||
}
|
||||
|
||||
@EventHandler(priority = EventPriority.LOWEST)
|
||||
public void onInventoryClose(InventoryCloseEvent event) {
|
||||
challengePanels.remove(event.getPlayer().getUniqueId());
|
||||
plugin.getLogger().info("DEBUG: removing inv " + challengePanels.size());
|
||||
}
|
||||
|
||||
/**
|
||||
* Clean up the hashmap should the player open up another inventory
|
||||
* @param event
|
||||
*/
|
||||
/*
|
||||
@EventHandler(priority = EventPriority.LOWEST)
|
||||
public void onInventoryOpen(InventoryOpenEvent event) {
|
||||
Player player = (Player) event.getPlayer();
|
||||
UUID playerUUID = player.getUniqueId();
|
||||
Inventory inventory = event.getInventory(); // The inventory that was
|
||||
if (inventory.getName() == null || !inventory.getName().equals(plugin.getLocale(player).get("challenges.guiTitle"))) {
|
||||
challengePanels.remove(playerUUID);
|
||||
plugin.getLogger().info("DEBUG: removing inv " + challengePanels.size());
|
||||
}
|
||||
}
|
||||
|
||||
@EventHandler(priority = EventPriority.NORMAL)
|
||||
public void onLogOut(PlayerQuitEvent event) {
|
||||
challengePanels.remove(event.getPlayer().getUniqueId());
|
||||
plugin.getLogger().info("DEBUG: removing inv " + challengePanels.size());
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
@ -1,6 +1,5 @@
|
||||
package bskyblock.addon.challenges.panel;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.event.inventory.InventoryClickEvent;
|
||||
import org.bukkit.event.inventory.InventoryCloseEvent;
|
||||
|
||||
@ -23,16 +22,12 @@ public class CreateChallengeListener implements PanelListener {
|
||||
|
||||
@Override
|
||||
public void onInventoryClose(InventoryCloseEvent event) {
|
||||
Bukkit.getLogger().info("DEBUG: event = " + event);
|
||||
Bukkit.getLogger().info("DEBUG: addon = " + addon);
|
||||
Bukkit.getLogger().info("DEBUG: challenge manager = " + addon.getChallengesManager());
|
||||
addon.getChallengesManager().createInvChallenge(user, event.getInventory());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onInventoryClick(User user, InventoryClickEvent event) {
|
||||
// Allow drag and drop
|
||||
Bukkit.getLogger().info("DEBUG: setting cancelled to false");
|
||||
event.setCancelled(false);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user