use of com.wasteofplastic.acidisland.panels.CPItem in project acidisland by tastybento.
the class Challenges method createItem.
/**
* Creates an inventory item for the challenge
* @param challengeName
* @param player
* @return Control Panel item
*/
private CPItem createItem(String challengeName, Player player) {
CPItem item = null;
// Get the icon
ItemStack icon = null;
String iconName = getChallengeConfig().getString("challenges.challengeList." + challengeName + ".icon", "");
if (!iconName.isEmpty()) {
try {
// Split if required
String[] split = iconName.split(":");
if (split.length == 1) {
// Some material does not show in the inventory
if (iconName.equalsIgnoreCase("potato")) {
iconName = "POTATO_ITEM";
} else if (iconName.equalsIgnoreCase("brewing_stand")) {
iconName = "BREWING_STAND_ITEM";
} else if (iconName.equalsIgnoreCase("carrot")) {
iconName = "CARROT_ITEM";
} else if (iconName.equalsIgnoreCase("cauldron")) {
iconName = "CAULDRON_ITEM";
} else if (iconName.equalsIgnoreCase("lava") || iconName.equalsIgnoreCase("stationary_lava")) {
iconName = "LAVA_BUCKET";
} else if (iconName.equalsIgnoreCase("water") || iconName.equalsIgnoreCase("stationary_water")) {
iconName = "WATER_BUCKET";
} else if (iconName.equalsIgnoreCase("portal")) {
iconName = "OBSIDIAN";
} else if (iconName.equalsIgnoreCase("PUMPKIN_STEM")) {
iconName = "PUMPKIN";
} else if (iconName.equalsIgnoreCase("skull")) {
iconName = "SKULL_ITEM";
} else if (iconName.equalsIgnoreCase("COCOA")) {
iconName = "INK_SACK:3";
} else if (iconName.equalsIgnoreCase("NETHER_WARTS")) {
iconName = "NETHER_STALK";
}
if (StringUtils.isNumeric(iconName)) {
icon = new ItemStack(Integer.parseInt(iconName));
} else {
icon = new ItemStack(Material.valueOf(iconName));
}
// Check POTION for V1.9 - for some reason, it must be declared as WATER otherwise comparison later causes an NPE
if (icon.getType().name().contains("POTION")) {
if (!plugin.getServer().getVersion().contains("(MC: 1.8") && !plugin.getServer().getVersion().contains("(MC: 1.7")) {
PotionMeta potionMeta = (PotionMeta) icon.getItemMeta();
potionMeta.setBasePotionData(new PotionData(PotionType.WATER));
icon.setItemMeta(potionMeta);
}
}
} else if (split.length == 2) {
if (StringUtils.isNumeric(split[0])) {
icon = new ItemStack(Integer.parseInt(split[0]));
} else {
icon = new ItemStack(Material.valueOf(split[0]));
}
// Check POTION for V1.9 - for some reason, it must be declared as WATER otherwise comparison later causes an NPE
if (icon.getType().name().contains("POTION")) {
if (!plugin.getServer().getVersion().contains("(MC: 1.8") && !plugin.getServer().getVersion().contains("(MC: 1.7")) {
PotionMeta potionMeta = (PotionMeta) icon.getItemMeta();
try {
potionMeta.setBasePotionData(new PotionData(PotionType.valueOf(split[1].toUpperCase())));
} catch (Exception e) {
plugin.getLogger().severe("Challenges icon: Potion type of " + split[1] + " is unknown, setting to WATER. Valid types are:");
for (PotionType type : PotionType.values()) {
plugin.getLogger().severe(type.name());
}
potionMeta.setBasePotionData(new PotionData(PotionType.WATER));
}
icon.setItemMeta(potionMeta);
}
} else if (icon.getType().equals(Material.MONSTER_EGG)) {
// Handle monster egg icons
try {
EntityType type = EntityType.valueOf(split[1].toUpperCase());
if (Bukkit.getServer().getVersion().contains("(MC: 1.8") || Bukkit.getServer().getVersion().contains("(MC: 1.7")) {
icon = new SpawnEgg(type).toItemStack();
} else {
try {
icon = new SpawnEgg1_9(type).toItemStack();
} catch (Exception ex) {
plugin.getLogger().severe("Monster eggs not supported with this server version.");
}
}
} catch (Exception e) {
Bukkit.getLogger().severe("Spawn eggs must be described by name. Try one of these (not all are possible):");
for (EntityType type : EntityType.values()) {
if (type.isSpawnable() && type.isAlive()) {
plugin.getLogger().severe(type.toString());
}
}
}
} else {
icon.setDurability(Integer.valueOf(split[1]).shortValue());
}
}
} catch (Exception e) {
// Icon was not well formatted
plugin.getLogger().warning("Error in challenges.yml - icon format is incorrect for " + challengeName + ":" + iconName);
plugin.getLogger().warning("Format should be 'icon: MaterialType:Damage' where Damage is optional");
}
}
if (icon == null) {
icon = new ItemStack(Material.PAPER);
}
// Handle spaces (AIR icon)
if (icon.getType() == Material.AIR) {
return new CPItem(icon, "");
}
String description = ChatColor.GREEN + ChatColor.translateAlternateColorCodes('&', getChallengeConfig().getString("challenges.challengeList." + challengeName + ".friendlyname", challengeName.substring(0, 1).toUpperCase() + challengeName.substring(1)));
// Remove extraneous info
ItemMeta im = icon.getItemMeta();
if (!plugin.getServer().getVersion().contains("1.7")) {
im.addItemFlags(ItemFlag.HIDE_ATTRIBUTES);
im.addItemFlags(ItemFlag.HIDE_DESTROYS);
im.addItemFlags(ItemFlag.HIDE_PLACED_ON);
}
// Check if completed or not
boolean complete = false;
if (Settings.addCompletedGlow && plugin.getPlayers().checkChallenge(player.getUniqueId(), challengeName)) {
// Complete! Make the icon glow
im.addEnchant(Enchantment.ARROW_DAMAGE, 0, true);
if (!plugin.getServer().getVersion().contains("1.7")) {
im.addItemFlags(ItemFlag.HIDE_ENCHANTS);
}
complete = true;
}
icon.setItemMeta(im);
boolean repeatable = false;
if (getChallengeConfig().getBoolean("challenges.challengeList." + challengeName + ".repeatable", false)) {
// Repeatable
repeatable = true;
}
// setting Settings.removeCompleteOntimeChallenges
if (!complete || ((complete && repeatable) || !Settings.removeCompleteOntimeChallenges)) {
// Store the challenge panel item and the command that will be
// called if it is activated.
item = new CPItem(icon, description, Settings.CHALLENGECOMMAND + " c " + challengeName, null);
// Get the challenge description, that changes depending on
// whether the challenge is complete or not.
List<String> lore = challengeDescription(challengeName, player);
item.setLore(lore);
}
return item;
}
use of com.wasteofplastic.acidisland.panels.CPItem in project acidisland by tastybento.
the class Challenges method challengePanel.
/**
* Dynamically creates an inventory of challenges for the player showing the
* level
*
* @param player
* @param level
* @return inventory
*/
public Inventory challengePanel(Player player, String level) {
// plugin.getLogger().info("DEBUG: level requested = " + level);
// Create the challenges control panel
// New panel map
List<CPItem> cp = new ArrayList<CPItem>();
if (level.isEmpty() && !challengeList.containsKey("")) {
if (!Settings.challengeLevels.isEmpty()) {
level = Settings.challengeLevels.get(0);
} else {
// We have no challenges!
plugin.getLogger().severe("There are no challenges to show!");
Inventory error = Bukkit.createInventory(null, 9, plugin.myLocale(player.getUniqueId()).challengesguiTitle);
Util.sendMessage(player, ChatColor.RED + plugin.myLocale(player.getUniqueId()).errorCommandNotReady);
return error;
}
}
if (challengeList.get(level) != null) {
// Only show a control panel for the level requested.
for (String challengeName : challengeList.get(level)) {
CPItem item = createItem(challengeName, player);
if (item != null) {
cp.add(item);
}
}
}
// Add the missing levels so player can navigate to them
int levelDone = 0;
for (; levelDone < Settings.challengeLevels.size(); levelDone++) {
if (checkLevelCompletion(player, Settings.challengeLevels.get(levelDone)) > 0) {
break;
}
}
// plugin.getLogger().info("DEBUG: level done = " + levelDone);
for (int i = 0; i < Settings.challengeLevels.size(); i++) {
if (!level.equalsIgnoreCase(Settings.challengeLevels.get(i))) {
// Add a navigation book
List<String> lore = new ArrayList<String>();
if (i <= levelDone) {
CPItem item = new CPItem(Material.BOOK_AND_QUILL, ChatColor.GOLD + Settings.challengeLevels.get(i), null, null);
lore = Util.chop(ChatColor.WHITE, plugin.myLocale(player.getUniqueId()).challengesNavigation.replace("[level]", Settings.challengeLevels.get(i)), 25);
item.setNextSection(Settings.challengeLevels.get(i));
item.setLore(lore);
cp.add(item);
} else {
// Hint at what is to come
CPItem item = new CPItem(Material.BOOK, ChatColor.GOLD + Settings.challengeLevels.get(i), null, null);
// Add the level
int toDo = checkLevelCompletion(player, Settings.challengeLevels.get(i - 1));
lore = Util.chop(ChatColor.WHITE, plugin.myLocale(player.getUniqueId()).challengestoComplete.replace("[challengesToDo]", String.valueOf(toDo)).replace("[thisLevel]", Settings.challengeLevels.get(i - 1)), 25);
item.setLore(lore);
cp.add(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
if (cp.size() > 0) {
// Make sure size is a multiple of 9
int size = cp.size() + 8;
size -= (size % 9);
Inventory newPanel = Bukkit.createInventory(null, size, plugin.myLocale(player.getUniqueId()).challengesguiTitle);
// Store the panel details for retrieval later
playerChallengeGUI.put(player.getUniqueId(), cp);
// Fill the inventory and return
int index = 0;
for (CPItem i : cp) {
newPanel.setItem(index++, i.getItem());
}
return newPanel;
}
return null;
}
Aggregations