WIP - added /c create (name) command

Opens up an inventory. Drag and drop items into the inventory. Escape
out and the challenge is saved with the items as required items.
This commit is contained in:
Tastybento 2018-02-19 19:50:34 -08:00
parent a9707b2925
commit 0a8ba02d74
14 changed files with 230 additions and 399 deletions

View File

@ -41,9 +41,9 @@
</build> </build>
<dependencies> <dependencies>
<dependency> <dependency>
<groupId>org.spigotmc</groupId> <groupId>org.bukkit</groupId>
<artifactId>spigot-api</artifactId> <artifactId>bukkit</artifactId>
<version>1.12-R0.1-SNAPSHOT</version> <version>1.12.2-R0.1-SNAPSHOT</version>
<scope>provided</scope> <scope>provided</scope>
</dependency> </dependency>
<dependency> <dependency>

View File

@ -1,36 +1,33 @@
package bskyblock.addon.challenges; package bskyblock.addon.challenges;
import org.bukkit.Bukkit;
import bskyblock.addon.challenges.commands.ChallengesCommand; import bskyblock.addon.challenges.commands.ChallengesCommand;
import bskyblock.addon.challenges.config.PluginConfig; import bskyblock.addon.challenges.config.PluginConfig;
import us.tastybento.bskyblock.BSkyBlock;
import us.tastybento.bskyblock.api.addons.Addon; import us.tastybento.bskyblock.api.addons.Addon;
/** /**
* Addin to BSkyBlock that enables challenges * Add-on to BSkyBlock that enables challenges
* @author tastybento * @author tastybento
* *
*/ */
public class Challenges extends Addon { public class Challenges extends Addon {
// The BSkyBlock plugin instance. private ChallengesManager challengesManager;
private BSkyBlock bSkyBlock;
private ChallengesManager manager;
@Override @Override
public void onEnable() { public void onEnable() {
// Load the plugin's config // Load the plugin's config
new PluginConfig(this); new PluginConfig(this);
// Get the BSkyBlock plugin. This will be available because this plugin depends on it in plugin.yml.
bSkyBlock = BSkyBlock.getInstance();
// Check if it is enabled - it might be loaded, but not enabled. // Check if it is enabled - it might be loaded, but not enabled.
if (bSkyBlock == null || !bSkyBlock.isEnabled()) { if (getBSkyBlock() == null || !getBSkyBlock().isEnabled()) {
Bukkit.getLogger().severe("BSkyBlock is not available or disabled!");
this.setEnabled(false); this.setEnabled(false);
return; return;
} }
// Challenges Manager // Challenges Manager
manager = new ChallengesManager(this); challengesManager = new ChallengesManager(this);
// Register commands // Register commands
new ChallengesCommand(this); new ChallengesCommand(this);
// Done // Done
@ -38,12 +35,12 @@ public class Challenges extends Addon {
@Override @Override
public void onDisable(){ public void onDisable(){
if (manager != null) if (challengesManager != null)
manager.save(false); challengesManager.save(false);
} }
public ChallengesManager getManager() { public ChallengesManager getChallengesManager() {
return manager; return challengesManager;
} }
} }

View File

@ -14,10 +14,12 @@ import java.util.stream.Collectors;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta; import org.bukkit.inventory.meta.ItemMeta;
import bskyblock.addon.challenges.database.object.ChallengesDO; import bskyblock.addon.challenges.database.object.ChallengesData;
import bskyblock.addon.challenges.database.object.ChallengesData.ChallengeType;
import bskyblock.addon.challenges.database.object.LevelsDO; import bskyblock.addon.challenges.database.object.LevelsDO;
import bskyblock.addon.challenges.panel.ChallengesPanels; import bskyblock.addon.challenges.panel.ChallengesPanels;
import us.tastybento.bskyblock.BSkyBlock; import us.tastybento.bskyblock.BSkyBlock;
@ -28,20 +30,20 @@ import us.tastybento.bskyblock.database.managers.AbstractDatabaseHandler;
public class ChallengesManager { public class ChallengesManager {
//private static final boolean DEBUG = false; //private static final boolean DEBUG = false;
private Challenges plugin; private Challenges addon;
private LinkedHashMap<LevelsDO, List<ChallengesDO>> challengeList; private LinkedHashMap<LevelsDO, List<ChallengesData>> challengeList;
private AbstractDatabaseHandler<ChallengesDO> chHandler; private AbstractDatabaseHandler<ChallengesData> chHandler;
private AbstractDatabaseHandler<LevelsDO> lvHandler; private AbstractDatabaseHandler<LevelsDO> lvHandler;
private ChallengesPanels challengesPanels; private ChallengesPanels challengesPanels;
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public ChallengesManager(Challenges plugin) { public ChallengesManager(Challenges plugin) {
this.plugin = plugin; this.addon = plugin;
// Set up the database handler to store and retrieve Challenges // Set up the database handler to store and retrieve Challenges
chHandler = (AbstractDatabaseHandler<ChallengesDO>) new FlatFileDatabase().getHandler(BSkyBlock.getInstance(), ChallengesDO.class); chHandler = (AbstractDatabaseHandler<ChallengesData>) new FlatFileDatabase().getHandler(ChallengesData.class);
lvHandler = (AbstractDatabaseHandler<LevelsDO>) new FlatFileDatabase().getHandler(BSkyBlock.getInstance(), LevelsDO.class); lvHandler = (AbstractDatabaseHandler<LevelsDO>) new FlatFileDatabase().getHandler(LevelsDO.class);
challengeList = new LinkedHashMap<>(); challengeList = new LinkedHashMap<>();
// Start panels // Start panels
challengesPanels = new ChallengesPanels(plugin, this); challengesPanels = new ChallengesPanels(plugin, this);
@ -55,7 +57,7 @@ public class ChallengesManager {
return challengesPanels; return challengesPanels;
} }
public AbstractDatabaseHandler<ChallengesDO> getHandler() { public AbstractDatabaseHandler<ChallengesData> getHandler() {
return chHandler; return chHandler;
} }
@ -66,10 +68,10 @@ public class ChallengesManager {
// Load the challenges // Load the challenges
challengeList.clear(); challengeList.clear();
try { try {
for (ChallengesDO challenge : chHandler.loadObjects()) { for (ChallengesData challenge : chHandler.loadObjects()) {
// See if we have this level already // See if we have this level already
LevelsDO level; LevelsDO level;
if (lvHandler.objectExits(challenge.getLevel())) { if (lvHandler.objectExists(challenge.getLevel())) {
// Get it from the database // Get it from the database
level = lvHandler.loadObject(challenge.getLevel()); level = lvHandler.loadObject(challenge.getLevel());
} else { } else {
@ -82,7 +84,7 @@ public class ChallengesManager {
challengeList.get(level).add(challenge); challengeList.get(level).add(challenge);
} else { } else {
// First challenge of this level type // First challenge of this level type
List<ChallengesDO> challenges = new ArrayList<>(); List<ChallengesData> challenges = new ArrayList<>();
challenges.add(challenge); challenges.add(challenge);
challengeList.put(level, challenges); challengeList.put(level, challenges);
} }
@ -107,7 +109,7 @@ public class ChallengesManager {
public void save(boolean async){ public void save(boolean async){
if(async){ if(async){
Runnable save = () -> { Runnable save = () -> {
for (Entry<LevelsDO, List<ChallengesDO>> en : challengeList.entrySet()) { for (Entry<LevelsDO, List<ChallengesData>> en : challengeList.entrySet()) {
try { try {
lvHandler.saveObject(en.getKey()); lvHandler.saveObject(en.getKey());
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException
@ -116,7 +118,7 @@ public class ChallengesManager {
// TODO Auto-generated catch block // TODO Auto-generated catch block
e.printStackTrace(); e.printStackTrace();
} }
for (ChallengesDO challenge : en.getValue()) { for (ChallengesData challenge : en.getValue()) {
try { try {
chHandler.saveObject(challenge); chHandler.saveObject(challenge);
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException
@ -130,7 +132,7 @@ public class ChallengesManager {
}; };
BSkyBlock.getInstance().getServer().getScheduler().runTaskAsynchronously(BSkyBlock.getInstance(), save); BSkyBlock.getInstance().getServer().getScheduler().runTaskAsynchronously(BSkyBlock.getInstance(), save);
} else { } else {
for (Entry<LevelsDO, List<ChallengesDO>> en : challengeList.entrySet()) { for (Entry<LevelsDO, List<ChallengesData>> en : challengeList.entrySet()) {
try { try {
lvHandler.saveObject(en.getKey()); lvHandler.saveObject(en.getKey());
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException
@ -139,7 +141,7 @@ public class ChallengesManager {
// TODO Auto-generated catch block // TODO Auto-generated catch block
e.printStackTrace(); e.printStackTrace();
} }
for (ChallengesDO challenge : en.getValue()) { for (ChallengesData challenge : en.getValue()) {
try { try {
chHandler.saveObject(challenge); chHandler.saveObject(challenge);
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException
@ -166,7 +168,7 @@ public class ChallengesManager {
// Get the main icon // Get the main icon
ItemStack icon = user.getInventory().getItemInOffHand(); ItemStack icon = user.getInventory().getItemInOffHand();
if (icon == null || icon.getType().equals(Material.AIR)) { if (icon == null || icon.getType().equals(Material.AIR)) {
user.sendLegacyMessage("Icon will be paper"); 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 = new ItemStack(Material.PAPER);
} }
icon.setAmount(1); icon.setAmount(1);
@ -189,12 +191,12 @@ public class ChallengesManager {
meta.setDisplayName(name); meta.setDisplayName(name);
meta.setLore(lore); meta.setLore(lore);
icon.setItemMeta(meta); icon.setItemMeta(meta);
ChallengesDO newChallenge = new ChallengesDO(); ChallengesData newChallenge = new ChallengesData();
newChallenge.setRequiredItems(contents); newChallenge.setRequiredItems(contents);
newChallenge.setUniqueId(name); newChallenge.setUniqueId(name);
newChallenge.setIcon(icon); newChallenge.setIcon(icon);
if (chHandler.objectExits(name)) { if (chHandler.objectExists(name)) {
user.sendLegacyMessage(ChatColor.RED + "Challenge already exists! Use /c replace <name>"); user.sendRawMessage(ChatColor.RED + "Challenge already exists! Use /c replace <name>");
return; return;
} }
try { try {
@ -203,10 +205,10 @@ public class ChallengesManager {
| InstantiationException | NoSuchMethodException | IntrospectionException | SQLException e) { | InstantiationException | NoSuchMethodException | IntrospectionException | SQLException e) {
// TODO Auto-generated catch block // TODO Auto-generated catch block
e.printStackTrace(); e.printStackTrace();
user.sendLegacyMessage(ChatColor.RED + "Challenge creation failed! " + e.getMessage()); user.sendRawMessage(ChatColor.RED + "Challenge creation failed! " + e.getMessage());
return; return;
} }
user.sendLegacyMessage("Challenge accepted!"); user.sendRawMessage("Challenge accepted!");
// TODO ADD CHALLENGE // TODO ADD CHALLENGE
//challenges.put(newChallenge.getUniqueId(), newChallenge); //challenges.put(newChallenge.getUniqueId(), newChallenge);
} }
@ -216,13 +218,13 @@ public class ChallengesManager {
* @param level - the level required * @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 * @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<ChallengesDO> getChallenges(String level) { public List<ChallengesData> getChallenges(String level) {
return challengeList.getOrDefault(level, challengeList.isEmpty() ? new ArrayList<ChallengesDO>() : challengeList.values().iterator().next()); return challengeList.getOrDefault(level, challengeList.isEmpty() ? new ArrayList<ChallengesData>() : challengeList.values().iterator().next());
} }
/** /**
* Checks if a challenge is complete or not * Checks if a challenge is complete or not
* @param uniqueId - player's UUID * @param uniqueId - unique ID - player's UUID
* @param uniqueId2 - Challenge id * @param uniqueId2 - Challenge id
* @return - true if completed * @return - true if completed
*/ */
@ -256,7 +258,7 @@ public class ChallengesManager {
public List<LevelStatus> getChallengeLevelStatus(User user) { public List<LevelStatus> getChallengeLevelStatus(User user) {
List<LevelStatus> result = new ArrayList<>(); List<LevelStatus> result = new ArrayList<>();
LevelsDO previousLevel = null; LevelsDO previousLevel = null;
for (Entry<LevelsDO, List<ChallengesDO>> en : challengeList.entrySet()) { for (Entry<LevelsDO, List<ChallengesData>> en : challengeList.entrySet()) {
int challsToDo = 0; // TODO - calculate how many challenges still to do for this player int challsToDo = 0; // TODO - calculate how many challenges still to do for this player
boolean complete = false; // TODO boolean complete = false; // TODO
result.add(new LevelStatus(en.getKey(), previousLevel, challsToDo, complete)); result.add(new LevelStatus(en.getKey(), previousLevel, challsToDo, complete));
@ -305,4 +307,53 @@ public class ChallengesManager {
} }
/**
* Creates an inventory challenge
* @param user
* @param inventory
*/
public void createInvChallenge(User user, Inventory inventory) {
if (inventory.getContents().length == 0) {
return;
}
ChallengesData newChallenge = new ChallengesData();
newChallenge.setChallengeType(ChallengeType.INVENTORY);
newChallenge.setFriendlyName(inventory.getTitle());
newChallenge.setDeployed(false);
List<ItemStack> requiredItems = new ArrayList<>();
inventory.forEach(item -> {
if (item != null && !item.getType().equals(Material.AIR)) {
requiredItems.add(item);
}
});
newChallenge.setRequiredItems(requiredItems);
newChallenge.setTakeItems(true);
newChallenge.setUniqueId(inventory.getTitle());
newChallenge.setIcon(new ItemStack(Material.EMPTY_MAP));
// Move all the items back to the player's inventory
inventory.forEach(item -> {
if (item != null) {
Map<Integer, ItemStack> residual = user.getInventory().addItem(item);
// Drop any residual items at the foot of the player
residual.forEach((k, v) -> {
user.getWorld().dropItem(user.getLocation(), v);
});
}
});
// 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;
}
user.sendRawMessage("Success");
}
} }

View File

@ -3,24 +3,26 @@ package bskyblock.addon.challenges.commands;
import java.util.List; import java.util.List;
import bskyblock.addon.challenges.Challenges; import bskyblock.addon.challenges.Challenges;
import us.tastybento.bskyblock.Constants;
import us.tastybento.bskyblock.api.commands.CompositeCommand; import us.tastybento.bskyblock.api.commands.CompositeCommand;
import us.tastybento.bskyblock.api.commands.User; import us.tastybento.bskyblock.api.commands.User;
import us.tastybento.bskyblock.config.Settings;
public class ChallengesCommand extends CompositeCommand { public class ChallengesCommand extends CompositeCommand {
private static final String CHALLENGE_COMMAND = "challenges"; private static final String CHALLENGE_COMMAND = "challenges";
private Challenges plugin; private Challenges addon;
public ChallengesCommand(Challenges plugin) { public ChallengesCommand(Challenges addon) {
super(CHALLENGE_COMMAND, "c", "challenge"); super(CHALLENGE_COMMAND, "c", "challenge");
this.plugin = plugin; this.addon = addon;
// Set up create command
new CreateChallenge(addon, this);
} }
@Override @Override
public boolean execute(User user, List<String> args) { public boolean execute(User user, List<String> args) {
// Open up the challenges GUI // Open up the challenges GUI
if (user.isPlayer()) { if (user.isPlayer()) {
plugin.getManager().getChallengesPanels().getChallenges(user); addon.getChallengesManager().getChallengesPanels().getChallenges(user);
return true; return true;
} }
return false; return false;
@ -29,12 +31,10 @@ public class ChallengesCommand extends CompositeCommand {
@Override @Override
public void setup() { public void setup() {
this.setOnlyPlayer(true); this.setOnlyPlayer(true);
this.setPermission(Settings.PERMPREFIX + "challenges"); this.setPermission(Constants.PERMPREFIX + "challenges");
this.setDescription("addon.challenges.help"); this.setParameters("challaneges.parameters");
this.setUsage("addon.challenges.usage"); this.setDescription("challenges.description");
this.setOnlyPlayer(true);
// Set up create command
new CreateChallenge(plugin, this);
} }

View File

@ -1,40 +1,49 @@
package bskyblock.addon.challenges.commands; package bskyblock.addon.challenges.commands;
import java.util.List; import java.util.List;
import java.util.UUID;
import bskyblock.addon.challenges.Challenges; import bskyblock.addon.challenges.Challenges;
import bskyblock.addon.challenges.panel.CreateChallengeListener;
import us.tastybento.bskyblock.Constants;
import us.tastybento.bskyblock.api.commands.CompositeCommand; import us.tastybento.bskyblock.api.commands.CompositeCommand;
import us.tastybento.bskyblock.api.commands.User; import us.tastybento.bskyblock.api.commands.User;
import us.tastybento.bskyblock.config.Settings; import us.tastybento.bskyblock.api.panels.builders.PanelBuilder;
public class CreateChallenge extends CompositeCommand { public class CreateChallenge extends CompositeCommand {
private Challenges plugin; private Challenges addon;
public CreateChallenge(Challenges plugin, ChallengesCommand parent) { /**
* Admin command to make challenges
* @param parent
*/
public CreateChallenge(Challenges addon, ChallengesCommand parent) {
super(parent, "create"); super(parent, "create");
this.plugin = plugin; this.addon = addon;
} }
@Override @Override
public void setup() { public void setup() {
this.setOnlyPlayer(true); this.setOnlyPlayer(true);
this.setPermission(Settings.PERMPREFIX + "challenges"); this.setPermission(Constants.PERMPREFIX + "admin.challenges");
this.setDescription("addon.challenges.create.description"); this.setParameters("challaneges.admin.create.parameters");
this.setUsage("addon.challenges.create.usage"); this.setDescription("challenges.admin.create.description");
} }
@Override @Override
public boolean execute(User user, List<String> args) { public boolean execute(User user, List<String> args) {
// Create a copy of items in the player's main inventory if (args.isEmpty()) {
String name = UUID.randomUUID().toString(); user.sendRawMessage("not enough args");
if (args.size() > 0) {
name = args.get(0);
}
plugin.getManager().createChallenge(user, name);
return false; return false;
} }
new PanelBuilder()
.setName(args.get(0))
.setSize(49)
.setListener(new CreateChallengeListener(addon, user))
.setUser(user)
.build();
return true;
}
} }

View File

@ -1,15 +1,8 @@
package bskyblock.addon.challenges.config; package bskyblock.addon.challenges.config;
import java.util.HashMap;
import org.apache.commons.lang.StringUtils;
import org.bukkit.Material;
import org.bukkit.material.MaterialData;
import bskyblock.addon.challenges.Challenges; import bskyblock.addon.challenges.Challenges;
public class PluginConfig { public class PluginConfig {
private static final boolean DEBUG = false;
/** /**
* Loads the various settings from the config.yml file into the plugin * Loads the various settings from the config.yml file into the plugin
@ -17,79 +10,15 @@ public class PluginConfig {
public PluginConfig(Challenges plugin) { public PluginConfig(Challenges plugin) {
plugin.saveDefaultConfig(); plugin.saveDefaultConfig();
// Island level cool down time // Settings
Settings.levelWait = plugin.getConfig().getInt("levelwait", 60); Settings.resetChallenges = plugin.getConfig().getBoolean("resetchallenges");
if (Settings.levelWait < 0) { // Challenge completion broadcast
Settings.levelWait = 0; Settings.broadcastMessages = plugin.getConfig().getBoolean("broadcastmessages", true);
} // Challenges - show or remove completed one-time challenges
Settings.removeCompleteOnetimeChallenges = plugin.getConfig().getBoolean("removecompleteonetimechallenges");
// Add glow to completed challenge icons or not
Settings.addCompletedGlow = plugin.getConfig().getBoolean("addcompletedglow", true);
// Get the under water multiplier
Settings.deathpenalty = plugin.getConfig().getInt("deathpenalty", 0);
Settings.sumTeamDeaths = plugin.getConfig().getBoolean("sumteamdeaths");
Settings.maxDeaths = plugin.getConfig().getInt("maxdeaths", 10);
Settings.islandResetDeathReset = plugin.getConfig().getBoolean("islandresetdeathreset", true);
Settings.teamJoinDeathReset = plugin.getConfig().getBoolean("teamjoindeathreset", true);
Settings.underWaterMultiplier = plugin.getConfig().getDouble("underwater", 1D);
Settings.levelCost = plugin.getConfig().getInt("levelcost", 100);
if (Settings.levelCost < 1) {
Settings.levelCost = 1;
plugin.getLogger().warning("levelcost in blockvalues.yml cannot be less than 1. Setting to 1.");
}
Settings.blockLimits = new HashMap<MaterialData, Integer>();
if (plugin.getConfig().isSet("limits")) {
for (String material : plugin.getConfig().getConfigurationSection("limits").getKeys(false)) {
try {
String[] split = material.split(":");
byte data = 0;
if (split.length>1) {
data = Byte.valueOf(split[1]);
}
Material mat;
if (StringUtils.isNumeric(split[0])) {
mat = Material.getMaterial(Integer.parseInt(split[0]));
} else {
mat = Material.valueOf(split[0].toUpperCase());
}
MaterialData materialData = new MaterialData(mat);
materialData.setData(data);
Settings.blockLimits.put(materialData, plugin.getConfig().getInt("limits." + material, 0));
if (DEBUG) {
plugin.getLogger().info("Maximum number of " + materialData + " will be " + Settings.blockLimits.get(materialData));
}
} catch (Exception e) {
plugin.getLogger().warning("Unknown material (" + material + ") in blockvalues.yml Limits section. Skipping...");
}
}
}
Settings.blockValues = new HashMap<MaterialData, Integer>();
if (plugin.getConfig().isSet("blocks")) {
for (String material : plugin.getConfig().getConfigurationSection("blocks").getKeys(false)) {
try {
String[] split = material.split(":");
byte data = 0;
if (split.length>1) {
data = Byte.valueOf(split[1]);
}
MaterialData materialData = null;
if (StringUtils.isNumeric(split[0])) {
materialData = new MaterialData(Integer.parseInt(split[0]));
} else {
materialData = new MaterialData(Material.valueOf(split[0].toUpperCase()));
}
materialData.setData(data);
Settings.blockValues.put(materialData, plugin.getConfig().getInt("blocks." + material, 0));
if (DEBUG) {
plugin.getLogger().info(materialData.toString() + " value = " + Settings.blockValues.get(materialData));
}
} catch (Exception e) {
// e.printStackTrace();
plugin.getLogger().warning("Unknown material (" + material + ") in blockvalues.yml blocks section. Skipping...");
}
}
} else {
plugin.getLogger().severe("No block values in blockvalues.yml! All island levels will be zero!");
}
// All done // All done
} }
} }

View File

@ -1,22 +1,28 @@
package bskyblock.addon.challenges.config; package bskyblock.addon.challenges.config;
import java.util.HashMap; import java.util.ArrayList;
import java.util.List;
import org.bukkit.material.MaterialData; import java.util.Set;
public class Settings { public class Settings {
public static boolean sumTeamDeaths; // Storage
public static int seaHeight; // Challenge List
public static HashMap<MaterialData, Integer> blockLimits; public static Set<String> challengeList;
public static HashMap<MaterialData, Integer> blockValues; // Waiver amount
public static double underWaterMultiplier; public static int waiverAmount;
public static int deathpenalty; // List of challenge levels
public static long levelCost; public static List<String> challengeLevels;
public static Object defaultLanguage; // Free levels
public static int levelWait; public static List<String> freeLevels = new ArrayList<String>();
public static int maxDeaths;
public static boolean islandResetDeathReset; // Settings
public static boolean teamJoinDeathReset; public static boolean resetChallenges;
// Challenge completion broadcast
public static boolean broadcastMessages;
// Challenges - show or remove completed on-time challenges
public static boolean removeCompleteOnetimeChallenges;
// Add glow to completed challenge icons or not
public static boolean addCompletedGlow;
} }

View File

@ -10,9 +10,9 @@ import org.bukkit.inventory.ItemStack;
import us.tastybento.bskyblock.database.objects.DataObject; import us.tastybento.bskyblock.database.objects.DataObject;
public class ChallengesDO extends DataObject { public class ChallengesData implements DataObject {
enum ChallengeType { public enum ChallengeType {
/** /**
* This challenge only shows and icon in the GUI and doesn't do anything. * This challenge only shows and icon in the GUI and doesn't do anything.
*/ */

View File

@ -5,7 +5,7 @@ import java.util.List;
import us.tastybento.bskyblock.database.objects.DataObject; import us.tastybento.bskyblock.database.objects.DataObject;
public class LevelsDO extends DataObject implements Comparable<LevelsDO> { public class LevelsDO implements DataObject, Comparable<LevelsDO> {
/** /**
* A friendly name for the level. If blank, level name is used. * A friendly name for the level. If blank, level name is used.

View File

@ -1,16 +1,16 @@
package bskyblock.addon.challenges.panel; package bskyblock.addon.challenges.panel;
import java.util.Arrays;
import java.util.List; import java.util.List;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import bskyblock.addon.challenges.Challenges; import bskyblock.addon.challenges.Challenges;
import bskyblock.addon.challenges.ChallengesManager; import bskyblock.addon.challenges.ChallengesManager;
import bskyblock.addon.challenges.ChallengesManager.LevelStatus; import bskyblock.addon.challenges.ChallengesManager.LevelStatus;
import bskyblock.addon.challenges.database.object.ChallengesDO; import bskyblock.addon.challenges.database.object.ChallengesData;
import us.tastybento.bskyblock.api.commands.User; import us.tastybento.bskyblock.api.commands.User;
import us.tastybento.bskyblock.api.panels.ClickType; import us.tastybento.bskyblock.api.panels.ClickType;
import us.tastybento.bskyblock.api.panels.Panel; import us.tastybento.bskyblock.api.panels.Panel;
@ -49,25 +49,25 @@ public class ChallengesPanels {
plugin.getLogger().info("DEBUG: level requested = " + level); plugin.getLogger().info("DEBUG: level requested = " + level);
PanelBuilder panelBuilder = new PanelBuilder() PanelBuilder panelBuilder = new PanelBuilder()
.setName(user.getTranslation("challenges.guiTitle")); .setName(user.getTranslation("challenges.guiTitle"));
List<ChallengesDO> levelChallenges = manager.getChallenges(level); List<ChallengesData> levelChallenges = manager.getChallenges(level);
// Do some checking // Do some checking
if (DEBUG) if (DEBUG)
plugin.getLogger().info("DEBUG: Opening level " + level); plugin.getLogger().info("DEBUG: Opening level " + level);
// Only show a control panel for the level requested. // Only show a control panel for the level requested.
for (ChallengesDO challenge : levelChallenges) { for (ChallengesData challenge : levelChallenges) {
plugin.getLogger().info("Adding challenge " + challenge.getUniqueId()); plugin.getLogger().info("Adding challenge " + challenge.getUniqueId());
boolean completed = manager.isChallengeComplete(user.getUniqueId(), challenge.getUniqueId()); boolean completed = manager.isChallengeComplete(user.getUniqueId(), challenge.getUniqueId());
if (completed && challenge.isRemoveWhenCompleted()) if (completed && challenge.isRemoveWhenCompleted())
continue; continue;
PanelItem item = new PanelItemBuilder() PanelItem item = new PanelItemBuilder()
.setIcon(challenge.getIcon()) .icon(challenge.getIcon())
.setName(challenge.getFriendlyName().isEmpty() ? challenge.getUniqueId() : challenge.getFriendlyName()) .name(challenge.getFriendlyName().isEmpty() ? challenge.getUniqueId() : challenge.getFriendlyName())
.setDescription(challenge.getDescription()) .description(challenge.getDescription())
.setGlow(completed) .glow(completed)
.setClickHandler(new PanelItem.ClickHandler() { .clickHandler(new PanelItem.ClickHandler() {
@Override @Override
public boolean onClick(Player player, ClickType click) { public boolean onClick(User user, ClickType click) {
player.sendMessage("Hi!"); user.sendMessage("Hi!");
return false; return false;
} }
}) })
@ -82,13 +82,14 @@ public class ChallengesPanels {
if (status.isComplete() || status.getPreviousLevel() == null) { if (status.isComplete() || status.getPreviousLevel() == null) {
// Clicking on this icon will open up this level's challenges // Clicking on this icon will open up this level's challenges
PanelItem item = new PanelItemBuilder() PanelItem item = new PanelItemBuilder()
.setIcon(new ItemStack(Material.BOOK_AND_QUILL)) .icon(new ItemStack(Material.BOOK_AND_QUILL))
.setName(name) .name(name)
.setDescription(user.getTranslation("challenges.navigation","[level]",name)) .description(Arrays.asList(user.getTranslation("challenges.navigation","[level]",name)))
.setClickHandler(new PanelItem.ClickHandler() { .clickHandler(new PanelItem.ClickHandler() {
@Override @Override
public boolean onClick(Player player, ClickType click) { public boolean onClick(User user, ClickType click) {
player.sendMessage("Hi!"); // TODO Auto-generated method stub
return false; return false;
} }
}) })
@ -99,9 +100,9 @@ public class ChallengesPanels {
// Clicking on this icon will do nothing because the challenge is not unlocked yet // 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()); String previousLevelName = ChatColor.GOLD + (status.getPreviousLevel().getFriendlyName().isEmpty() ? status.getPreviousLevel().getUniqueId() : status.getPreviousLevel().getFriendlyName());
PanelItem item = new PanelItemBuilder() PanelItem item = new PanelItemBuilder()
.setIcon(new ItemStack(Material.BOOK)) .icon(new ItemStack(Material.BOOK))
.setName(name) .name(name)
.setDescription((user.getTranslation("challenges.toComplete", "[challengesToDo]",String.valueOf(status.getNumberOfChallengesStillToDo()), "[thisLevel]", previousLevelName))) .description(Arrays.asList(user.getTranslation("challenges.toComplete", "[challengesToDo]",String.valueOf(status.getNumberOfChallengesStillToDo()), "[thisLevel]", previousLevelName)))
.build(); .build();
panelBuilder.addItem(item); panelBuilder.addItem(item);
} }

View File

@ -0,0 +1,38 @@
package bskyblock.addon.challenges.panel;
import org.bukkit.Bukkit;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.inventory.InventoryCloseEvent;
import bskyblock.addon.challenges.Challenges;
import us.tastybento.bskyblock.api.commands.User;
import us.tastybento.bskyblock.api.panels.PanelListener;
public class CreateChallengeListener implements PanelListener {
private Challenges addon;
private User user;
public CreateChallengeListener(Challenges addon, User user) {
this.addon = addon;
this.user = user;
}
@Override
public void setup() {}
@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);
}
}

View File

@ -0,0 +1,13 @@
package bskyblock.addon.challenges.panel;
import bskyblock.addon.challenges.Challenges;
import us.tastybento.bskyblock.api.commands.User;
import us.tastybento.bskyblock.api.panels.builders.PanelBuilder;
public class CreateChallengePanel {
public CreateChallengePanel(Challenges addon, User user) {
new PanelBuilder().setSize(49).setListener(new CreateChallengeListener(addon, user)).setUser(user).build();
}
}

View File

@ -1,213 +0,0 @@
package bskyblock.addon.challenges.panel;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map.Entry;
import java.util.TreeMap;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemFlag;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.plugin.Plugin;
public class Panel {
private final Inventory gui;
private final TreeMap<Integer, PanelItem> panelItems;
public Panel(Plugin plugin, String name, TreeMap<Integer, PanelItem> panelItems) {
// Generate gui
this.panelItems = panelItems;
// Create the panel
if (panelItems.lastKey() > 0) {
// Make sure size is a multiple of 9
int size = panelItems.lastKey() + 8;
size -= (size % 9);
gui = Bukkit.createInventory(null, size, name);
// Fill the inventory and return
for (Entry<Integer, PanelItem> en: panelItems.entrySet()) {
gui.setItem(en.getKey(), en.getValue().getIcon());
}
} else {
gui = Bukkit.createInventory(null, 9, name);
}
}
public TreeMap<Integer, PanelItem> getPanelItems() {
return panelItems;
}
public Inventory getPanel() {
return gui;
}
public static PanelBuilder builder(Plugin plugin) {
return new PanelBuilder(plugin);
}
public static class PanelBuilder {
private TreeMap<Integer,PanelItem> panelItems = new TreeMap<>();
private String name;
private Plugin plugin;
public PanelBuilder(Plugin plugin) {
this.plugin = plugin;
}
public PanelBuilder addItem(PanelItem item) {
// Fit into slots. Handle duplicates
int index = item.getSlot();
while (panelItems.containsKey(index) || index == 49) {
index++;
};
panelItems.put(index, item);
Bukkit.getLogger().info("DEBUG: added to slot " + index);
return this;
}
public PanelBuilder name(String name) {
this.name = name;
return this;
}
public Panel build() {
return new Panel(plugin, name, panelItems);
}
}
public static PanelItemBuilder panelItemBuilder() {
return new PanelItemBuilder();
}
public static class PanelItem {
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, 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(ChatColor.translateAlternateColorCodes('&', name));
meta.setLore(chop(description));
// Set flags to neaten up the view
meta.addItemFlags(ItemFlag.HIDE_ATTRIBUTES);
meta.addItemFlags(ItemFlag.HIDE_DESTROYS);
meta.addItemFlags(ItemFlag.HIDE_PLACED_ON);
meta.addItemFlags(ItemFlag.HIDE_ENCHANTS);
if (glow) {
meta.addEnchant(Enchantment.ARROW_DAMAGE, 0, true);
}
icon.setItemMeta(meta);
result.add(icon);
} else {
for (int i = 0; i < toggleItems.size(); i++) {
// Create the icon(s)
ItemMeta meta = icon.getItemMeta();
meta.setDisplayName(ChatColor.translateAlternateColorCodes('&', name));
List<String> desc = chop(description);
desc.addAll(chop(toggleItems.get(i)));
meta.setLore(desc);
// Set flags to neaten up the view
meta.addItemFlags(ItemFlag.HIDE_ATTRIBUTES);
meta.addItemFlags(ItemFlag.HIDE_DESTROYS);
meta.addItemFlags(ItemFlag.HIDE_PLACED_ON);
meta.addItemFlags(ItemFlag.HIDE_ENCHANTS);
if (glow) {
meta.addEnchant(Enchantment.ARROW_DAMAGE, 0, true);
}
icon.setItemMeta(meta);
result.add(icon);
}
}
this.icon = result;
}
public ItemStack getIcon() {
return icon.get(index);
}
public ItemStack toggleIcon() {
if (icon.size() < (index + 1)) {
index++;
} else {
index = 0;
}
return icon.get(index);
}
public Integer getSlot() {
return slot;
}
public List<String> getCommands() {
return commands;
}
}
public static class PanelItemBuilder {
private ItemStack icon;
private String description;
private String name;
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;
return this;
}
public PanelItemBuilder setDescription(String description) {
this.description = description;
return this;
}
public PanelItemBuilder setName(String name) {
this.name = name;
return this;
}
public PanelItemBuilder setSlot(int slot) {
this.slot = slot;
return this;
}
public PanelItemBuilder setToggleItems(List<String> toggleItems) {
this.toggleItems = toggleItems;
return this;
}
public PanelItemBuilder setGlow(boolean glow) {
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, command);
}
}
private static List<String> chop(String longLine) {
longLine = ChatColor.translateAlternateColorCodes('&', longLine);
// Split pip character requires escaping it
String[] split = longLine.split("\\|");
return new ArrayList<String>(Arrays.asList(split));
}
}