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

10
pom.xml
View File

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

View File

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

View File

@ -14,10 +14,12 @@ import java.util.stream.Collectors;
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.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.panel.ChallengesPanels;
import us.tastybento.bskyblock.BSkyBlock;
@ -28,20 +30,20 @@ import us.tastybento.bskyblock.database.managers.AbstractDatabaseHandler;
public class ChallengesManager {
//private static final boolean DEBUG = false;
private Challenges plugin;
private LinkedHashMap<LevelsDO, List<ChallengesDO>> challengeList;
private Challenges addon;
private LinkedHashMap<LevelsDO, List<ChallengesData>> challengeList;
private AbstractDatabaseHandler<ChallengesDO> chHandler;
private AbstractDatabaseHandler<ChallengesData> chHandler;
private AbstractDatabaseHandler<LevelsDO> lvHandler;
private ChallengesPanels challengesPanels;
@SuppressWarnings("unchecked")
public ChallengesManager(Challenges plugin) {
this.plugin = plugin;
this.addon = plugin;
// Set up the database handler to store and retrieve Challenges
chHandler = (AbstractDatabaseHandler<ChallengesDO>) new FlatFileDatabase().getHandler(BSkyBlock.getInstance(), ChallengesDO.class);
lvHandler = (AbstractDatabaseHandler<LevelsDO>) new FlatFileDatabase().getHandler(BSkyBlock.getInstance(), LevelsDO.class);
chHandler = (AbstractDatabaseHandler<ChallengesData>) new FlatFileDatabase().getHandler(ChallengesData.class);
lvHandler = (AbstractDatabaseHandler<LevelsDO>) new FlatFileDatabase().getHandler(LevelsDO.class);
challengeList = new LinkedHashMap<>();
// Start panels
challengesPanels = new ChallengesPanels(plugin, this);
@ -55,7 +57,7 @@ public class ChallengesManager {
return challengesPanels;
}
public AbstractDatabaseHandler<ChallengesDO> getHandler() {
public AbstractDatabaseHandler<ChallengesData> getHandler() {
return chHandler;
}
@ -66,10 +68,10 @@ public class ChallengesManager {
// Load the challenges
challengeList.clear();
try {
for (ChallengesDO challenge : chHandler.loadObjects()) {
for (ChallengesData challenge : chHandler.loadObjects()) {
// See if we have this level already
LevelsDO level;
if (lvHandler.objectExits(challenge.getLevel())) {
if (lvHandler.objectExists(challenge.getLevel())) {
// Get it from the database
level = lvHandler.loadObject(challenge.getLevel());
} else {
@ -82,7 +84,7 @@ public class ChallengesManager {
challengeList.get(level).add(challenge);
} else {
// First challenge of this level type
List<ChallengesDO> challenges = new ArrayList<>();
List<ChallengesData> challenges = new ArrayList<>();
challenges.add(challenge);
challengeList.put(level, challenges);
}
@ -107,7 +109,7 @@ public class ChallengesManager {
public void save(boolean async){
if(async){
Runnable save = () -> {
for (Entry<LevelsDO, List<ChallengesDO>> en : challengeList.entrySet()) {
for (Entry<LevelsDO, List<ChallengesData>> en : challengeList.entrySet()) {
try {
lvHandler.saveObject(en.getKey());
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException
@ -116,7 +118,7 @@ public class ChallengesManager {
// TODO Auto-generated catch block
e.printStackTrace();
}
for (ChallengesDO challenge : en.getValue()) {
for (ChallengesData challenge : en.getValue()) {
try {
chHandler.saveObject(challenge);
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException
@ -130,7 +132,7 @@ public class ChallengesManager {
};
BSkyBlock.getInstance().getServer().getScheduler().runTaskAsynchronously(BSkyBlock.getInstance(), save);
} else {
for (Entry<LevelsDO, List<ChallengesDO>> en : challengeList.entrySet()) {
for (Entry<LevelsDO, List<ChallengesData>> en : challengeList.entrySet()) {
try {
lvHandler.saveObject(en.getKey());
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException
@ -139,7 +141,7 @@ public class ChallengesManager {
// TODO Auto-generated catch block
e.printStackTrace();
}
for (ChallengesDO challenge : en.getValue()) {
for (ChallengesData challenge : en.getValue()) {
try {
chHandler.saveObject(challenge);
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException
@ -166,7 +168,7 @@ public class ChallengesManager {
// Get the main icon
ItemStack icon = user.getInventory().getItemInOffHand();
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.setAmount(1);
@ -189,12 +191,12 @@ public class ChallengesManager {
meta.setDisplayName(name);
meta.setLore(lore);
icon.setItemMeta(meta);
ChallengesDO newChallenge = new ChallengesDO();
ChallengesData newChallenge = new ChallengesData();
newChallenge.setRequiredItems(contents);
newChallenge.setUniqueId(name);
newChallenge.setIcon(icon);
if (chHandler.objectExits(name)) {
user.sendLegacyMessage(ChatColor.RED + "Challenge already exists! Use /c replace <name>");
if (chHandler.objectExists(name)) {
user.sendRawMessage(ChatColor.RED + "Challenge already exists! Use /c replace <name>");
return;
}
try {
@ -203,10 +205,10 @@ public class ChallengesManager {
| InstantiationException | NoSuchMethodException | IntrospectionException | SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
user.sendLegacyMessage(ChatColor.RED + "Challenge creation failed! " + e.getMessage());
user.sendRawMessage(ChatColor.RED + "Challenge creation failed! " + e.getMessage());
return;
}
user.sendLegacyMessage("Challenge accepted!");
user.sendRawMessage("Challenge accepted!");
// TODO ADD CHALLENGE
//challenges.put(newChallenge.getUniqueId(), newChallenge);
}
@ -216,13 +218,13 @@ public class ChallengesManager {
* @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<ChallengesDO> getChallenges(String level) {
return challengeList.getOrDefault(level, challengeList.isEmpty() ? new ArrayList<ChallengesDO>() : challengeList.values().iterator().next());
public List<ChallengesData> getChallenges(String level) {
return challengeList.getOrDefault(level, challengeList.isEmpty() ? new ArrayList<ChallengesData>() : challengeList.values().iterator().next());
}
/**
* Checks if a challenge is complete or not
* @param uniqueId - player's UUID
* @param uniqueId - unique ID - player's UUID
* @param uniqueId2 - Challenge id
* @return - true if completed
*/
@ -256,7 +258,7 @@ public class ChallengesManager {
public List<LevelStatus> getChallengeLevelStatus(User user) {
List<LevelStatus> result = new ArrayList<>();
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
boolean complete = false; // TODO
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 bskyblock.addon.challenges.Challenges;
import us.tastybento.bskyblock.Constants;
import us.tastybento.bskyblock.api.commands.CompositeCommand;
import us.tastybento.bskyblock.api.commands.User;
import us.tastybento.bskyblock.config.Settings;
public class ChallengesCommand extends CompositeCommand {
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");
this.plugin = plugin;
this.addon = addon;
// Set up create command
new CreateChallenge(addon, this);
}
@Override
public boolean execute(User user, List<String> args) {
// Open up the challenges GUI
if (user.isPlayer()) {
plugin.getManager().getChallengesPanels().getChallenges(user);
addon.getChallengesManager().getChallengesPanels().getChallenges(user);
return true;
}
return false;
@ -29,12 +31,10 @@ public class ChallengesCommand extends CompositeCommand {
@Override
public void setup() {
this.setOnlyPlayer(true);
this.setPermission(Settings.PERMPREFIX + "challenges");
this.setDescription("addon.challenges.help");
this.setUsage("addon.challenges.usage");
// Set up create command
new CreateChallenge(plugin, this);
this.setPermission(Constants.PERMPREFIX + "challenges");
this.setParameters("challaneges.parameters");
this.setDescription("challenges.description");
this.setOnlyPlayer(true);
}

View File

@ -1,40 +1,49 @@
package bskyblock.addon.challenges.commands;
import java.util.List;
import java.util.UUID;
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.User;
import us.tastybento.bskyblock.config.Settings;
import us.tastybento.bskyblock.api.panels.builders.PanelBuilder;
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");
this.plugin = plugin;
this.addon = addon;
}
@Override
public void setup() {
this.setOnlyPlayer(true);
this.setPermission(Settings.PERMPREFIX + "challenges");
this.setDescription("addon.challenges.create.description");
this.setUsage("addon.challenges.create.usage");
this.setPermission(Constants.PERMPREFIX + "admin.challenges");
this.setParameters("challaneges.admin.create.parameters");
this.setDescription("challenges.admin.create.description");
}
@Override
public boolean execute(User user, List<String> args) {
// Create a copy of items in the player's main inventory
String name = UUID.randomUUID().toString();
if (args.size() > 0) {
name = args.get(0);
if (args.isEmpty()) {
user.sendRawMessage("not enough args");
return false;
}
plugin.getManager().createChallenge(user, name);
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;
import java.util.HashMap;
import org.apache.commons.lang.StringUtils;
import org.bukkit.Material;
import org.bukkit.material.MaterialData;
import bskyblock.addon.challenges.Challenges;
public class PluginConfig {
private static final boolean DEBUG = false;
/**
* Loads the various settings from the config.yml file into the plugin
@ -17,79 +10,15 @@ public class PluginConfig {
public PluginConfig(Challenges plugin) {
plugin.saveDefaultConfig();
// Island level cool down time
Settings.levelWait = plugin.getConfig().getInt("levelwait", 60);
if (Settings.levelWait < 0) {
Settings.levelWait = 0;
}
// Settings
Settings.resetChallenges = plugin.getConfig().getBoolean("resetchallenges");
// Challenge completion broadcast
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
}
}

View File

@ -1,22 +1,28 @@
package bskyblock.addon.challenges.config;
import java.util.HashMap;
import org.bukkit.material.MaterialData;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
public class Settings {
public static boolean sumTeamDeaths;
public static int seaHeight;
public static HashMap<MaterialData, Integer> blockLimits;
public static HashMap<MaterialData, Integer> blockValues;
public static double underWaterMultiplier;
public static int deathpenalty;
public static long levelCost;
public static Object defaultLanguage;
public static int levelWait;
public static int maxDeaths;
public static boolean islandResetDeathReset;
public static boolean teamJoinDeathReset;
// Storage
// Challenge List
public static Set<String> challengeList;
// Waiver amount
public static int waiverAmount;
// List of challenge levels
public static List<String> challengeLevels;
// Free levels
public static List<String> freeLevels = new ArrayList<String>();
// Settings
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;
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.
*/

View File

@ -5,7 +5,7 @@ import java.util.List;
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.

View File

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