mirror of
https://github.com/BentoBoxWorld/Challenges.git
synced 2025-01-17 21:51:47 +01:00
WIP: Stubbed out a lot of the classes.
Compiles, but still a lot of functions need to be added.
This commit is contained in:
parent
38a98043d5
commit
3b74b17283
@ -6,3 +6,51 @@
|
||||
### Credits ###
|
||||
# Tastybento: maintainer
|
||||
|
||||
challenges:
|
||||
complete: "Complete"
|
||||
expReward: "Exp reward"
|
||||
firstTimeRewards: "First time reward(s)"
|
||||
guititle: "BSkyBlock Challenges"
|
||||
help1: "Use /c <name> to view information about a challenge."
|
||||
help2: "Use /c complete <name> to attempt to complete that challenge."
|
||||
incomplete: "Incomplete"
|
||||
invalidChallengeName: "Invalid challenge name! Use /c help for more information"
|
||||
itemTakeWarning: "All required items are taken when you complete this challenge!"
|
||||
level: "Level"
|
||||
maxreached: "Max reached [donetimes] out of [maxtimes]"
|
||||
moneyReward: "Money reward"
|
||||
name: "Challenge Name"
|
||||
nameHasCompleted: "[name] has completed the [challenge] challenge!"
|
||||
navigation: "Click to see [level] challenges!"
|
||||
notRepeatable: "This Challenge is not repeatable!"
|
||||
repeatRewards: "Repeat reward(s)"
|
||||
rewards: "Reward(s)"
|
||||
toComplete: "Complete [challengesToDo] more [thisLevel] challenges to unlock this level!"
|
||||
toCompleteUse: "To complete this challenge, use"
|
||||
unknownChallenge: "Unknown challenge name (check spelling)!"
|
||||
youHaveCompleted: "You have completed the [challenge] challenge!"
|
||||
youHaveNotUnlocked: "You have not unlocked this challenge yet!"
|
||||
youRepeated: "You repeated the [challenge] challenge!"
|
||||
completechallenge:
|
||||
challangeCompleted: "Challenge: [challengename] has been completed for [name]"
|
||||
errorChallengeDoesNotExist: "Challenge doesn't exist or is already completed"
|
||||
error:
|
||||
IslandLevel: "Your island must be level [level] to complete this challenge!"
|
||||
ItemsNotThere: "All required items must be close to you on your island!"
|
||||
NotCloseEnough: "You must be standing within [number] blocks of all required items."
|
||||
NotEnoughItems: "You do not have enough required item(s)"
|
||||
NotOnIsland: "You must be on your island to do that!"
|
||||
RewardProblem: "There was a problem giving your reward. Ask Admin to check log!"
|
||||
YouAreMissing: "You are missing"
|
||||
help:
|
||||
command: "/challenges: &fshow challenges"
|
||||
configReloaded: "Configuration reloaded from file."
|
||||
resetAllChallenges: "resets all of the player's challenges"
|
||||
resetChallenge: "marks a challenge as incomplete"
|
||||
resetChallengeForAll: "globally resets a challenge for every player with an optional repetition"
|
||||
resetallchallenges:
|
||||
success: "[name] has had all challenges reset."
|
||||
resetchallenge:
|
||||
challengeReset: "Challenge: [challengename] has been reset for [name]"
|
||||
errorChallengeDoesNotExist: "Challenge doesn't exist or isn't yet completed"
|
||||
|
||||
|
@ -10,8 +10,6 @@ import bskyblock.addin.challenges.config.LocaleManager;
|
||||
import bskyblock.addin.challenges.config.PluginConfig;
|
||||
import us.tastybento.bskyblock.BSkyBlock;
|
||||
import us.tastybento.bskyblock.config.BSBLocale;
|
||||
import us.tastybento.bskyblock.database.BSBDatabase;
|
||||
import us.tastybento.bskyblock.database.flatfile.FlatFileDatabase;
|
||||
|
||||
/**
|
||||
* Addin to BSkyBlock that enables challenges
|
||||
@ -20,24 +18,14 @@ import us.tastybento.bskyblock.database.flatfile.FlatFileDatabase;
|
||||
*/
|
||||
public class Challenges extends JavaPlugin {
|
||||
|
||||
|
||||
private static final boolean DEBUG = true;
|
||||
|
||||
// The BSkyBlock plugin instance.
|
||||
private BSkyBlock bSkyBlock;
|
||||
|
||||
// Locale manager for this plugin
|
||||
private LocaleManager localeManager;
|
||||
|
||||
// The BSkyBlock database object
|
||||
private BSBDatabase database;
|
||||
|
||||
private ChallengesManager manager;
|
||||
|
||||
private FlatFileDatabase flatFile;
|
||||
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public void onEnable() {
|
||||
// Load the plugin's config
|
||||
@ -58,7 +46,7 @@ public class Challenges extends JavaPlugin {
|
||||
// Register commands
|
||||
new ChallengesCommand(this);
|
||||
// Register Listener
|
||||
getServer().getPluginManager().registerEvents(manager, this);
|
||||
getServer().getPluginManager().registerEvents(manager.getChallengesPanels(), this);
|
||||
// Done
|
||||
}
|
||||
|
||||
|
@ -5,44 +5,36 @@ import java.lang.reflect.InvocationTargetException;
|
||||
import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.UUID;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.EventPriority;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.inventory.ClickType;
|
||||
import org.bukkit.event.inventory.InventoryClickEvent;
|
||||
import org.bukkit.event.inventory.InventoryCloseEvent;
|
||||
import org.bukkit.event.inventory.InventoryOpenEvent;
|
||||
import org.bukkit.event.player.PlayerQuitEvent;
|
||||
import org.bukkit.inventory.Inventory;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.inventory.meta.ItemMeta;
|
||||
|
||||
import bskyblock.addin.challenges.database.object.ChallengesDO;
|
||||
import bskyblock.addin.challenges.database.object.LevelsDO;
|
||||
import bskyblock.addin.challenges.panel.Panel;
|
||||
import bskyblock.addin.challenges.panel.Panel.PanelBuilder;
|
||||
import bskyblock.addin.challenges.panel.Panel.PanelItem;
|
||||
import bskyblock.addin.challenges.panel.ChallengesPanels;
|
||||
import us.tastybento.bskyblock.database.flatfile.FlatFileDatabase;
|
||||
import us.tastybento.bskyblock.database.managers.AbstractDatabaseHandler;
|
||||
import us.tastybento.bskyblock.util.Util;
|
||||
|
||||
public class ChallengesManager implements Listener {
|
||||
public class ChallengesManager {
|
||||
|
||||
private static final boolean DEBUG = false;
|
||||
//private static final boolean DEBUG = false;
|
||||
private Challenges plugin;
|
||||
private HashMap<UUID, Panel> challengePanels;
|
||||
private LinkedHashMap<LevelsDO, List<ChallengesDO>> challengeList;
|
||||
|
||||
private AbstractDatabaseHandler<ChallengesDO> chHandler;
|
||||
private HashMap<String,ChallengesDO> challenges;
|
||||
private AbstractDatabaseHandler<LevelsDO> lvHandler;
|
||||
private HashMap<String,LevelsDO> levels;
|
||||
|
||||
private ChallengesPanels challengesPanels;
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public ChallengesManager(Challenges plugin) {
|
||||
@ -50,12 +42,19 @@ public class ChallengesManager implements Listener {
|
||||
// Set up the database handler to store and retrieve Challenges
|
||||
chHandler = (AbstractDatabaseHandler<ChallengesDO>) new FlatFileDatabase().getHandler(plugin, ChallengesDO.class);
|
||||
lvHandler = (AbstractDatabaseHandler<LevelsDO>) new FlatFileDatabase().getHandler(plugin, LevelsDO.class);
|
||||
challenges = new HashMap<>();
|
||||
levels = new HashMap<>();
|
||||
challengePanels = new HashMap<>();
|
||||
challengeList = new LinkedHashMap<>();
|
||||
// Start panels
|
||||
challengesPanels = new ChallengesPanels(plugin, this);
|
||||
load();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the challengesPanels
|
||||
*/
|
||||
public ChallengesPanels getChallengesPanels() {
|
||||
return challengesPanels;
|
||||
}
|
||||
|
||||
public AbstractDatabaseHandler<ChallengesDO> getHandler() {
|
||||
return chHandler;
|
||||
}
|
||||
@ -64,28 +63,40 @@ public class ChallengesManager implements Listener {
|
||||
* Clear and reload all challenges
|
||||
*/
|
||||
public void load() {
|
||||
levels.clear();
|
||||
try {
|
||||
for (LevelsDO level : lvHandler.loadObjects()) {
|
||||
levels.put(level.getUniqueId(), level);
|
||||
}
|
||||
} catch (InstantiationException | IllegalAccessException | IllegalArgumentException
|
||||
| InvocationTargetException | SecurityException | ClassNotFoundException | IntrospectionException
|
||||
| SQLException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
challenges.clear();
|
||||
// Load the challenges
|
||||
challengeList.clear();
|
||||
try {
|
||||
for (ChallengesDO challenge : chHandler.loadObjects()) {
|
||||
challenges.put(challenge.getUniqueId(), challenge);
|
||||
// See if we have this level already
|
||||
LevelsDO level = new LevelsDO();
|
||||
if (lvHandler.objectExits(challenge.getLevel())) {
|
||||
// Get it from the database
|
||||
level = lvHandler.loadObject(challenge.getLevel());
|
||||
} else {
|
||||
// Make it
|
||||
level.setUniqueId(challenge.getLevel());
|
||||
lvHandler.saveObject(level);
|
||||
}
|
||||
if (challengeList.containsKey(level)) {
|
||||
challengeList.get(level).add(challenge);
|
||||
} else {
|
||||
// First challenge of this level type
|
||||
List<ChallengesDO> challenges = new ArrayList<>();
|
||||
challenges.add(challenge);
|
||||
challengeList.put(level, challenges);
|
||||
}
|
||||
}
|
||||
} catch (InstantiationException | IllegalAccessException | IllegalArgumentException
|
||||
| InvocationTargetException | SecurityException | ClassNotFoundException | IntrospectionException
|
||||
| SQLException e) {
|
||||
| SQLException | NoSuchMethodException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
// 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));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -95,35 +106,55 @@ public class ChallengesManager implements Listener {
|
||||
public void save(boolean async){
|
||||
if(async){
|
||||
Runnable save = () -> {
|
||||
int index = 1;
|
||||
for(ChallengesDO challenge : challenges.values()){
|
||||
plugin.getLogger().info("DEBUG: saving challenges async " + index++);
|
||||
for (Entry<LevelsDO, List<ChallengesDO>> 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 (ChallengesDO challenge : en.getValue()) {
|
||||
try {
|
||||
chHandler.saveObject(challenge);
|
||||
} catch (Exception e) {
|
||||
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException
|
||||
| SecurityException | InstantiationException | NoSuchMethodException
|
||||
| IntrospectionException | SQLException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
plugin.getServer().getScheduler().runTaskAsynchronously(plugin, save);
|
||||
} else {
|
||||
int index = 1;
|
||||
for(ChallengesDO challenge : challenges.values()){
|
||||
plugin.getLogger().info("DEBUG: saving challenges sync " + index++);
|
||||
for (Entry<LevelsDO, List<ChallengesDO>> 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 (ChallengesDO challenge : en.getValue()) {
|
||||
try {
|
||||
chHandler.saveObject(challenge);
|
||||
} catch (Exception e) {
|
||||
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException
|
||||
| SecurityException | InstantiationException | NoSuchMethodException
|
||||
| IntrospectionException | SQLException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Metrics-related methods //
|
||||
}
|
||||
|
||||
public void shutdown(){
|
||||
save(false);
|
||||
challenges.clear();
|
||||
challengeList.clear();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -175,83 +206,102 @@ public class ChallengesManager implements Listener {
|
||||
return;
|
||||
}
|
||||
Util.sendMessage(player, "Challenge accepted!");
|
||||
challenges.put(newChallenge.getUniqueId(), newChallenge);
|
||||
}
|
||||
|
||||
public Inventory getChallenges(Player player) {
|
||||
// TODO build the panel that is customized to the player
|
||||
// Build panel
|
||||
PanelBuilder panelBuilder = Panel.builder(plugin)
|
||||
.name(plugin.getLocale(player).get("challenges.guiTitle"));
|
||||
for (ChallengesDO challenge: challenges.values()) {
|
||||
plugin.getLogger().info("Adding challenge " + challenge.getUniqueId());
|
||||
PanelItem item = Panel.panelItemBuilder()
|
||||
.setIcon(challenge.getIcon())
|
||||
.setName(challenge.getFriendlyName().isEmpty() ? challenge.getUniqueId() : challenge.getFriendlyName())
|
||||
.setDescription(challenge.getDescription())
|
||||
.setSlot(challenge.getSlot())
|
||||
.build();
|
||||
plugin.getLogger().info("requested slot" + item.getSlot());
|
||||
panelBuilder.addItem(item);
|
||||
}
|
||||
Panel panel = panelBuilder.build();
|
||||
challengePanels.put(player.getUniqueId(), panel);
|
||||
plugin.getLogger().info("DEBUG: added inv " + challengePanels.size());
|
||||
return panel.getPanel();
|
||||
}
|
||||
|
||||
@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());
|
||||
// TODO ADD CHALLENGE
|
||||
//challenges.put(newChallenge.getUniqueId(), newChallenge);
|
||||
}
|
||||
|
||||
/**
|
||||
* Clean up the hashmap should the player open up another inventory
|
||||
* @param event
|
||||
* 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
|
||||
*/
|
||||
@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());
|
||||
}
|
||||
public List<ChallengesDO> getChallenges(String level) {
|
||||
return challengeList.getOrDefault(level, challengeList.isEmpty() ? new ArrayList<ChallengesDO>() : challengeList.values().iterator().next());
|
||||
}
|
||||
|
||||
@EventHandler(priority = EventPriority.NORMAL)
|
||||
public void onLogOut(PlayerQuitEvent event) {
|
||||
challengePanels.remove(event.getPlayer().getUniqueId());
|
||||
plugin.getLogger().info("DEBUG: removing inv " + challengePanels.size());
|
||||
/**
|
||||
* Checks if a challenge is complete or not
|
||||
* @param uniqueId - player's UUID
|
||||
* @param uniqueId2 - Challenge id
|
||||
* @return - true if completed
|
||||
*/
|
||||
public boolean isChallengeComplete(UUID uniqueId, String uniqueId2) {
|
||||
// TODO Auto-generated method stub
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean isLevelComplete(UUID uniqueId, LevelsDO otherLevel) {
|
||||
// TODO Auto-generated method stub
|
||||
return false;
|
||||
}
|
||||
|
||||
public LevelsDO getPreviousLevel(LevelsDO otherLevel) {
|
||||
LevelsDO result = null;
|
||||
|
||||
for (LevelsDO level : challengeList.keySet()) {
|
||||
if (level.equals(otherLevel)) {
|
||||
return result;
|
||||
}
|
||||
result = level;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the status on every level
|
||||
* @param player
|
||||
* @return Level name, how many challenges still to do on which level
|
||||
*/
|
||||
public List<LevelStatus> getChallengeLevelStatus(Player player) {
|
||||
List<LevelStatus> result = new ArrayList<>();
|
||||
LevelsDO previousLevel = null;
|
||||
for (Entry<LevelsDO, List<ChallengesDO>> 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 LevelsDO level;
|
||||
private final LevelsDO previousLevel;
|
||||
private final int numberOfChallengesStillToDo;
|
||||
private final boolean complete;
|
||||
|
||||
public LevelStatus(LevelsDO level, LevelsDO previousLevel, int numberOfChallengesStillToDo, boolean complete) {
|
||||
super();
|
||||
this.level = level;
|
||||
this.previousLevel = previousLevel;
|
||||
this.numberOfChallengesStillToDo = numberOfChallengesStillToDo;
|
||||
this.complete = complete;
|
||||
}
|
||||
/**
|
||||
* @return the level
|
||||
*/
|
||||
public LevelsDO getLevel() {
|
||||
return level;
|
||||
}
|
||||
/**
|
||||
* @return the previousLevel
|
||||
*/
|
||||
public LevelsDO getPreviousLevel() {
|
||||
return previousLevel;
|
||||
}
|
||||
/**
|
||||
* @return the numberOfChallengesStillToDo
|
||||
*/
|
||||
public int getNumberOfChallengesStillToDo() {
|
||||
return numberOfChallengesStillToDo;
|
||||
}
|
||||
/**
|
||||
* @return the complete
|
||||
*/
|
||||
public boolean isComplete() {
|
||||
return complete;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -43,7 +43,7 @@ public class ChallengesCommand extends AbstractCommand implements Listener {
|
||||
public void execute(CommandSender sender, String[] args) {
|
||||
// Open up the challenges GUI
|
||||
if (isPlayer) {
|
||||
player.openInventory(plugin.getManager().getChallenges(player));
|
||||
player.openInventory(plugin.getManager().getChallengesPanels().getChallenges(player));
|
||||
} else {
|
||||
// TODO
|
||||
}
|
||||
|
@ -138,7 +138,6 @@ public class ChallengesDO extends DataObject {
|
||||
private int searchRadius = 10;
|
||||
/**
|
||||
* Inventory slot where this challenge should be placed. 0 to 49.
|
||||
*
|
||||
*/
|
||||
private int slot;
|
||||
/**
|
||||
|
@ -5,7 +5,7 @@ import java.util.List;
|
||||
|
||||
import us.tastybento.bskyblock.database.objects.DataObject;
|
||||
|
||||
public class LevelsDO extends DataObject {
|
||||
public class LevelsDO extends DataObject implements Comparable<LevelsDO> {
|
||||
|
||||
/**
|
||||
* A friendly name for the level. If blank, level name is used.
|
||||
@ -24,6 +24,11 @@ public class LevelsDO extends DataObject {
|
||||
*/
|
||||
private int waiveramount = 1;
|
||||
|
||||
/**
|
||||
* The ordering of the levels, lowest to highest
|
||||
*/
|
||||
private int order = 0;
|
||||
|
||||
public String getFriendlyName() {
|
||||
return friendlyName;
|
||||
}
|
||||
@ -58,4 +63,17 @@ public class LevelsDO extends DataObject {
|
||||
this.waiveramount = waiveramount;
|
||||
}
|
||||
|
||||
public int getOrder() {
|
||||
return order;
|
||||
}
|
||||
|
||||
public void setOrder(int order) {
|
||||
this.order = order;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int compareTo(LevelsDO o) {
|
||||
return Integer.compare(this.order, o.order);
|
||||
}
|
||||
|
||||
}
|
||||
|
176
src/bskyblock/addin/challenges/panel/ChallengesPanels.java
Normal file
176
src/bskyblock/addin/challenges/panel/ChallengesPanels.java
Normal file
@ -0,0 +1,176 @@
|
||||
package bskyblock.addin.challenges.panel;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.EventPriority;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.inventory.ClickType;
|
||||
import org.bukkit.event.inventory.InventoryClickEvent;
|
||||
import org.bukkit.event.inventory.InventoryCloseEvent;
|
||||
import org.bukkit.event.inventory.InventoryOpenEvent;
|
||||
import org.bukkit.event.player.PlayerQuitEvent;
|
||||
import org.bukkit.inventory.Inventory;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
|
||||
import bskyblock.addin.challenges.Challenges;
|
||||
import bskyblock.addin.challenges.ChallengesManager;
|
||||
import bskyblock.addin.challenges.ChallengesManager.LevelStatus;
|
||||
import bskyblock.addin.challenges.database.object.ChallengesDO;
|
||||
import bskyblock.addin.challenges.panel.Panel.PanelBuilder;
|
||||
import bskyblock.addin.challenges.panel.Panel.PanelItem;
|
||||
|
||||
public class ChallengesPanels implements Listener {
|
||||
private static final boolean DEBUG = false;
|
||||
private static final String CHALLENGE_COMMAND = "challenges";
|
||||
private Challenges plugin;
|
||||
private HashMap<UUID, Panel> challengePanels;
|
||||
private ChallengesManager manager;
|
||||
|
||||
public ChallengesPanels(Challenges plugin, ChallengesManager manager){
|
||||
this.plugin = plugin;
|
||||
this.manager = manager;
|
||||
challengePanels = new HashMap<>();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param player
|
||||
* @return
|
||||
*/
|
||||
public Inventory getChallenges(Player player) {
|
||||
return getChallenges(player, "");
|
||||
}
|
||||
|
||||
/**
|
||||
* Dynamically creates an inventory of challenges for the player showing the
|
||||
* level
|
||||
*
|
||||
* @param player
|
||||
* @param level
|
||||
* @return inventory
|
||||
*/
|
||||
public Inventory getChallenges(Player player, String level) {
|
||||
plugin.getLogger().info("DEBUG: level requested = " + level);
|
||||
PanelBuilder panelBuilder = Panel.builder(plugin)
|
||||
.name(plugin.getLocale(player).get("challenges.guiTitle"));
|
||||
List<ChallengesDO> levelChallenges = manager.getChallenges(level);
|
||||
// Do some checking
|
||||
if (DEBUG)
|
||||
plugin.getLogger().info("DEBUG: Opening level " + level);
|
||||
// Only show a control panel for the level requested.
|
||||
for (ChallengesDO challenge : levelChallenges) {
|
||||
plugin.getLogger().info("Adding challenge " + challenge.getUniqueId());
|
||||
boolean completed = manager.isChallengeComplete(player.getUniqueId(), challenge.getUniqueId());
|
||||
if (completed && challenge.isRemoveWhenCompleted())
|
||||
continue;
|
||||
PanelItem item = Panel.panelItemBuilder()
|
||||
.setIcon(challenge.getIcon())
|
||||
.setName(challenge.getFriendlyName().isEmpty() ? challenge.getUniqueId() : challenge.getFriendlyName())
|
||||
.setDescription(challenge.getDescription())
|
||||
.setSlot(challenge.getSlot())
|
||||
.setGlow(completed)
|
||||
.setCommand(CHALLENGE_COMMAND + " c " + challenge.getUniqueId())
|
||||
.build();
|
||||
plugin.getLogger().info("requested slot" + item.getSlot());
|
||||
panelBuilder.addItem(item);
|
||||
}
|
||||
// Add navigation to other levels
|
||||
for (LevelStatus status: manager.getChallengeLevelStatus(player)) {
|
||||
String name = ChatColor.GOLD + (status.getLevel().getFriendlyName().isEmpty() ? status.getLevel().getUniqueId() : status.getLevel().getFriendlyName());
|
||||
if (status.isComplete() || status.getPreviousLevel() == null) {
|
||||
// Clicking on this icon will open up this level's challenges
|
||||
PanelItem item = Panel.panelItemBuilder()
|
||||
.setIcon(new ItemStack(Material.BOOK_AND_QUILL))
|
||||
.setName(name)
|
||||
.setDescription(plugin.getLocale(player).get("challenges.navigation").replace("[level]",name))
|
||||
.setCommand(CHALLENGE_COMMAND + " c " + status.getLevel().getUniqueId())
|
||||
.build();
|
||||
panelBuilder.addItem(item);
|
||||
} else {
|
||||
// Clicking on this icon will do nothing because the challenge is not unlocked yet
|
||||
String previousLevelName = ChatColor.GOLD + (status.getPreviousLevel().getFriendlyName().isEmpty() ? status.getPreviousLevel().getUniqueId() : status.getPreviousLevel().getFriendlyName());
|
||||
PanelItem item = Panel.panelItemBuilder()
|
||||
.setIcon(new ItemStack(Material.BOOK))
|
||||
.setName(name)
|
||||
.setDescription((plugin.getLocale(player).get("challenges.toComplete").replace("[challengesToDo]",String.valueOf(status.getNumberOfChallengesStillToDo()))).replace("[thisLevel]", previousLevelName))
|
||||
.build();
|
||||
panelBuilder.addItem(item);
|
||||
}
|
||||
}
|
||||
/*
|
||||
// Add the free challenges if not already shown (which can happen if all of the challenges are done!)
|
||||
if (!level.equals("") && challengeList.containsKey("")) {
|
||||
for (String freeChallenges: challengeList.get("")) {
|
||||
CPItem item = createItem(freeChallenges, player);
|
||||
if (item != null) {
|
||||
cp.add(item);
|
||||
}
|
||||
}
|
||||
}*/
|
||||
// Create the panel
|
||||
Panel panel = panelBuilder.build();
|
||||
challengePanels.put(player.getUniqueId(), panel);
|
||||
return panel.getPanel();
|
||||
}
|
||||
|
||||
@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());
|
||||
}
|
||||
|
||||
}
|
@ -87,19 +87,22 @@ public class Panel {
|
||||
}
|
||||
|
||||
public static class PanelItem {
|
||||
private final Integer slot;
|
||||
private final int slot;
|
||||
// The current index of the icon
|
||||
private int index = 0;
|
||||
// There is a list of icons for every toggle option
|
||||
private final List<ItemStack> icon;
|
||||
// Command to run when clicked
|
||||
private final List<String> commands;
|
||||
|
||||
public PanelItem(ItemStack icon, String description, String name, Integer slot, List<String> toggleItems, boolean glow) {
|
||||
public PanelItem(ItemStack icon, String description, String name, int slot, List<String> toggleItems, boolean glow, List<String> commands) {
|
||||
this.slot = slot;
|
||||
this.commands = commands;
|
||||
List<ItemStack> result = new ArrayList<>();
|
||||
if (toggleItems.isEmpty()) {
|
||||
// Create the icon
|
||||
ItemMeta meta = icon.getItemMeta();
|
||||
meta.setDisplayName(name);
|
||||
meta.setDisplayName(ChatColor.translateAlternateColorCodes('&', name));
|
||||
meta.setLore(chop(description));
|
||||
// Set flags to neaten up the view
|
||||
meta.addItemFlags(ItemFlag.HIDE_ATTRIBUTES);
|
||||
@ -115,7 +118,7 @@ public class Panel {
|
||||
for (int i = 0; i < toggleItems.size(); i++) {
|
||||
// Create the icon(s)
|
||||
ItemMeta meta = icon.getItemMeta();
|
||||
meta.setDisplayName(name);
|
||||
meta.setDisplayName(ChatColor.translateAlternateColorCodes('&', name));
|
||||
List<String> desc = chop(description);
|
||||
desc.addAll(chop(toggleItems.get(i)));
|
||||
meta.setLore(desc);
|
||||
@ -151,15 +154,19 @@ public class Panel {
|
||||
return slot;
|
||||
}
|
||||
|
||||
public List<String> getCommands() {
|
||||
return commands;
|
||||
}
|
||||
}
|
||||
|
||||
public static class PanelItemBuilder {
|
||||
private ItemStack icon;
|
||||
private String description;
|
||||
private String name;
|
||||
private Integer slot;
|
||||
private int slot;
|
||||
private List<String> toggleItems = new ArrayList<>();
|
||||
private boolean glow;
|
||||
private List<String> command = new ArrayList<>();
|
||||
|
||||
public PanelItemBuilder setIcon(ItemStack icon) {
|
||||
this.icon = icon;
|
||||
@ -173,7 +180,7 @@ public class Panel {
|
||||
this.name = name;
|
||||
return this;
|
||||
}
|
||||
public PanelItemBuilder setSlot(Integer slot) {
|
||||
public PanelItemBuilder setSlot(int slot) {
|
||||
this.slot = slot;
|
||||
return this;
|
||||
}
|
||||
@ -185,8 +192,12 @@ public class Panel {
|
||||
this.glow = glow;
|
||||
return this;
|
||||
}
|
||||
public PanelItemBuilder setCommand(String command) {
|
||||
this.command.add(command);
|
||||
return this;
|
||||
}
|
||||
public PanelItem build() {
|
||||
return new PanelItem(icon, description, name, slot, toggleItems, glow);
|
||||
return new PanelItem(icon, description, name, slot, toggleItems, glow, command);
|
||||
}
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user