use of com.elmakers.mine.bukkit.api.magic.ProgressionPath in project MagicPlugin by elBukkit.
the class MagicRequirement method checkRequirement.
public boolean checkRequirement(@Nonnull CastContext context) {
Mage mage = context.getMage();
Player player = mage.getPlayer();
if (permissionNode != null && (player == null || !player.hasPermission(permissionNode))) {
return false;
}
Wand wand = context.getWand();
if (wand == null && requireWand) {
return false;
}
if (requiredTemplate != null) {
String template = wand.getTemplateKey();
if (template == null || !template.equals(requiredTemplate)) {
return false;
}
}
if (requiredTemplates != null) {
String template = wand.getTemplateKey();
if (template == null || !requiredTemplates.contains(template)) {
return false;
}
}
CasterProperties checkProperties = context.getActiveProperties();
ProgressionPath path = checkProperties.getPath();
if (mageClass != null && !mageClass.isEmpty()) {
if (!mage.hasClassUnlocked(mageClass)) {
return false;
}
}
if (requiredPath != null || exactPath != null) {
if (path == null) {
return false;
}
if (requiredPath != null && !path.hasPath(requiredPath)) {
return false;
}
if (exactPath != null && !exactPath.equals(path.getKey())) {
return false;
}
if (requiresCompletedPath != null) {
boolean hasPathCompleted = false;
if (path.hasPath(requiresCompletedPath)) {
if (path.getKey().equals(requiresCompletedPath)) {
hasPathCompleted = !path.canProgress(checkProperties);
} else {
hasPathCompleted = true;
}
}
if (!hasPathCompleted) {
return false;
}
}
}
if (wandProperties != null) {
if (!checkProperties(wand, wandProperties)) {
return false;
}
}
if (classProperties != null) {
MageClass activeClass = mageClass == null ? mage.getActiveClass() : mage.getClass(mageClass);
if (activeClass == null) {
return false;
}
if (!checkProperties(activeClass, classProperties)) {
return false;
}
}
if (attributes != null) {
for (PropertyRequirement requirement : attributes) {
String key = requirement.key;
Double value = mage.getAttribute(key);
if (!checkProperty(requirement, value)) {
return false;
}
}
}
return true;
}
use of com.elmakers.mine.bukkit.api.magic.ProgressionPath in project MagicPlugin by elBukkit.
the class CasterProperties method upgradeInternal.
@Override
protected boolean upgradeInternal(String key, Object value) {
if (key.equals("path")) {
ProgressionPath path = getPath();
if (path != null && path.hasPath(value.toString())) {
return false;
}
setProperty(key, value);
return true;
}
return super.upgradeInternal(key, value);
}
use of com.elmakers.mine.bukkit.api.magic.ProgressionPath in project MagicPlugin by elBukkit.
the class SpellShopAction method getItems.
@Nullable
@Override
public List<ShopItem> getItems(CastContext context) {
Mage mage = context.getMage();
Wand wand = mage.getActiveWand();
CasterProperties caster = getCaster(context);
// Check for auto upgrade, if the player can no longer progress on their current path and is
// eligible for an upgrade, we will upgrade them and skip showing the spell shop so the player
// can see their upgrade rewards.
boolean canProgress = false;
// TODO: Make this work on CasterProperties
if (autoUpgrade && wand != null) {
canProgress = caster.canProgress();
if (!canProgress && wand.checkUpgrade(true) && wand.upgrade(false)) {
return null;
}
}
ProgressionPath currentPath = caster.getPath();
if (!castsSpells && !allowLocked && wand != null && wand.isLocked()) {
context.showMessage(context.getMessage("no_path", getDefaultMessage(context, "no_path")).replace("$wand", wand.getName()));
return null;
}
// Load spells
Map<String, Double> spellPrices = new HashMap<>();
if (spells.size() > 0) {
spellPrices.putAll(spells);
} else {
if (currentPath == null) {
String name = wand == null ? "" : wand.getName();
context.showMessage(context.getMessage("no_path", getDefaultMessage(context, "no_path")).replace("$wand", name));
return null;
}
if (showPath) {
Collection<String> pathSpells = currentPath.getSpells();
for (String pathSpell : pathSpells) {
spellPrices.put(pathSpell, null);
}
}
if (showRequired) {
Collection<String> requiredSpells = currentPath.getRequiredSpells();
for (String requiredSpell : requiredSpells) {
spellPrices.put(requiredSpell, null);
}
}
if (showUpgrades) {
Collection<String> spells = caster.getSpells();
for (String spellKey : spells) {
MageSpell spell = mage.getSpell(spellKey);
SpellTemplate upgradeSpell = spell.getUpgrade();
if (upgradeSpell != null) {
spellPrices.put(upgradeSpell.getKey(), null);
}
}
}
}
List<ShopItem> shopItems = new ArrayList<>();
for (Map.Entry<String, Double> spellValue : spellPrices.entrySet()) {
ShopItem shopItem = createShopItem(spellValue.getKey(), spellValue.getValue(), context);
if (shopItem != null) {
shopItems.add(shopItem);
}
}
Collections.sort(shopItems);
if (spells.size() == 0 && showExtra && !castsSpells && currentPath != null) {
Collection<String> extraSpells = currentPath.getExtraSpells();
List<ShopItem> extraItems = new ArrayList<>();
for (String spellKey : extraSpells) {
ShopItem shopItem = createShopItem(spellKey, null, context);
if (shopItem != null) {
ItemStack spellItem = shopItem.getItem();
ItemMeta meta = spellItem.getItemMeta();
List<String> itemLore = meta.getLore();
itemLore.add(context.getMessage("extra_spell", getDefaultMessage(context, "extra_spell")));
meta.setLore(itemLore);
spellItem.setItemMeta(meta);
extraItems.add(shopItem);
}
}
Collections.sort(extraItems);
shopItems.addAll(extraItems);
}
// If there is nothing here for us to do, check for upgrades being blocked
// we will upgrade the wand here, but in theory that should never happen since we
// checked for upgrades above.
// TODO: Make this work for caster...
mage.sendDebugMessage(ChatColor.GOLD + "Spells to buy: " + shopItems.size(), 2);
if (wand != null && shopItems.size() == 0) {
boolean canUpgrade = autoUpgrade && wand.checkUpgrade(false);
boolean hasUpgrade = autoUpgrade && wand.hasUpgrade();
if (!canProgress && !hasUpgrade) {
context.showMessage(context.getMessage("no_upgrade", getDefaultMessage(context, "no_upgrade")).replace("$wand", wand.getName()));
return null;
} else if (canUpgrade) {
wand.upgrade(false);
return null;
} else {
return null;
}
}
return shopItems;
}
use of com.elmakers.mine.bukkit.api.magic.ProgressionPath in project MagicPlugin by elBukkit.
the class SpellShopAction method createShopItem.
@Nullable
private ShopItem createShopItem(String key, Double worth, CastContext context) {
CasterProperties caster = getCaster(context);
Mage mage = context.getMage();
MageController controller = context.getController();
ProgressionPath currentPath = caster.getPath();
key = context.parameterize(key);
String spellKey = key.split(" ", 2)[0];
if (!castsSpells && caster.hasSpell(spellKey)) {
mage.sendDebugMessage(ChatColor.GRAY + " Skipping " + spellKey + ", already have it", 3);
return null;
}
SpellTemplate spell = controller.getSpellTemplate(spellKey);
if (spell == null) {
mage.sendDebugMessage(ChatColor.RED + " Skipping " + spellKey + ", invalid spell", 0);
return null;
}
if (worth == null) {
worth = spell.getWorth();
}
if (worth <= 0 && !showFree) {
mage.sendDebugMessage(ChatColor.YELLOW + " Skipping " + spellKey + ", is free (worth " + worth + ")", 3);
return null;
}
if (!spell.hasCastPermission(mage.getCommandSender())) {
mage.sendDebugMessage(ChatColor.YELLOW + " Skipping " + spellKey + ", no permission", 3);
return null;
}
ItemStack spellItem = controller.createSpellItem(key, castsSpells);
if (!castsSpells) {
Spell mageSpell = caster.getSpell(spellKey);
String requiredPathKey = mageSpell != null ? mageSpell.getRequiredUpgradePath() : null;
Set<String> requiredPathTags = mageSpell != null ? mageSpell.getRequiredUpgradeTags() : null;
long requiredCastCount = mageSpell != null ? mageSpell.getRequiredUpgradeCasts() : 0;
long castCount = mageSpell != null ? Math.min(mageSpell.getCastCount(), requiredCastCount) : 0;
Collection<PrerequisiteSpell> missingSpells = PrerequisiteSpell.getMissingRequirements(caster, spell);
ItemMeta meta = spellItem.getItemMeta();
List<String> itemLore = meta.getLore();
if (itemLore == null) {
mage.sendDebugMessage(ChatColor.RED + " Skipping " + spellKey + ", spell item is invalid", 0);
return null;
}
List<String> lore = new ArrayList<>();
if (spell.getSpellKey().getLevel() > 1 && itemLore.size() > 0) {
lore.add(itemLore.get(0));
}
String upgradeDescription = spell.getUpgradeDescription();
if (showUpgrades && upgradeDescription != null && !upgradeDescription.isEmpty()) {
upgradeDescription = controller.getMessages().get("spell.upgrade_description_prefix") + upgradeDescription;
InventoryUtils.wrapText(upgradeDescription, lore);
}
String unpurchasableMessage = null;
Collection<Requirement> requirements = spell.getRequirements();
String requirementMissing = controller.checkRequirements(context, requirements);
if (requirementMissing != null) {
unpurchasableMessage = requirementMissing;
InventoryUtils.wrapText(unpurchasableMessage, lore);
}
if ((requiredPathKey != null && !currentPath.hasPath(requiredPathKey)) || (requiresCastCounts && requiredCastCount > 0 && castCount < requiredCastCount) || (requiredPathTags != null && !currentPath.hasAllTags(requiredPathTags)) || !missingSpells.isEmpty()) {
if (mageSpell != null && !spell.getName().equals(mageSpell.getName())) {
lore.add(context.getMessage("upgrade_name_change", getDefaultMessage(context, "upgrade_name_change")).replace("$name", mageSpell.getName()));
}
if (requiredPathKey != null && !currentPath.hasPath(requiredPathKey)) {
requiredPathKey = currentPath.translatePath(requiredPathKey);
com.elmakers.mine.bukkit.wand.WandUpgradePath upgradePath = com.elmakers.mine.bukkit.wand.WandUpgradePath.getPath(requiredPathKey);
if (upgradePath != null) {
unpurchasableMessage = context.getMessage("level_requirement", getDefaultMessage(context, "level_requirement")).replace("$path", upgradePath.getName());
InventoryUtils.wrapText(unpurchasableMessage, lore);
}
} else if (requiredPathTags != null && !requiredPathTags.isEmpty() && !currentPath.hasAllTags(requiredPathTags)) {
Set<String> tags = currentPath.getMissingTags(requiredPathTags);
unpurchasableMessage = context.getMessage("tags_requirement", getDefaultMessage(context, "tags_requirement")).replace("$tags", controller.getMessages().formatList("tags", tags, "name"));
InventoryUtils.wrapText(unpurchasableMessage, lore);
}
if (requiresCastCounts && requiredCastCount > 0 && castCount < requiredCastCount) {
unpurchasableMessage = ChatColor.RED + context.getMessage("cast_requirement", getDefaultMessage(context, "cast_requirement")).replace("$current", Long.toString(castCount)).replace("$required", Long.toString(requiredCastCount));
InventoryUtils.wrapText(unpurchasableMessage, lore);
}
if (!missingSpells.isEmpty()) {
List<String> spells = new ArrayList<>(missingSpells.size());
for (PrerequisiteSpell s : missingSpells) {
SpellTemplate template = context.getController().getSpellTemplate(s.getSpellKey().getKey());
String spellMessage = context.getMessage("prerequisite_spell_level", getDefaultMessage(context, "prerequisite_spell_level")).replace("$name", template.getName());
if (s.getProgressLevel() > 1) {
spellMessage += context.getMessage("prerequisite_spell_progress_level", getDefaultMessage(context, "prerequisite_spell_progress_level")).replace("$level", Long.toString(s.getProgressLevel())).replace("$max_level", Long.toString(Math.max(1, template.getMaxProgressLevel())));
}
spells.add(spellMessage);
}
unpurchasableMessage = ChatColor.RED + context.getMessage("required_spells", getDefaultMessage(context, "required_spells")).replace("$spells", StringUtils.join(spells, ", "));
InventoryUtils.wrapText(ChatColor.GOLD + unpurchasableMessage, lore);
}
}
for (int i = (spell.getSpellKey().getLevel() > 1 ? 1 : 0); i < itemLore.size(); i++) {
lore.add(itemLore.get(i));
}
meta.setLore(lore);
spellItem.setItemMeta(meta);
if (unpurchasableMessage != null)
InventoryUtils.setMeta(spellItem, "unpurchasable", unpurchasableMessage);
}
return new ShopItem(spellItem, worth);
}
use of com.elmakers.mine.bukkit.api.magic.ProgressionPath in project MagicPlugin by elBukkit.
the class PlaceholderAPIManager method onPlaceholderRequest.
@Override
public String onPlaceholderRequest(Player player, String placeholder) {
Mage mage = controller.getMage(player);
MageClass activeClass = mage.getActiveClass();
Wand wand = mage.getActiveWand();
Spell spell = wand == null ? null : wand.getActiveSpell();
if (spell == null) {
ItemStack item = player.getInventory().getItemInMainHand();
String spellKey = controller.getSpell(item);
if (spellKey != null) {
spell = mage.getSpell(spellKey);
}
}
if (spell == null) {
ItemStack item = player.getInventory().getItemInOffHand();
String spellKey = controller.getSpell(item);
if (spellKey != null) {
spell = mage.getSpell(spellKey);
}
}
CasterProperties casterProperties = mage.getActiveProperties();
switch(placeholder) {
case "path":
ProgressionPath path = casterProperties.getPath();
return path == null ? "" : path.getName();
case "class":
return activeClass == null ? "" : activeClass.getName();
case "wand":
return wand == null ? "" : wand.getName();
case "spell":
return spell == null ? "" : spell.getName();
}
return "";
}
Aggregations