use of com.elmakers.mine.bukkit.api.spell.SpellTemplate 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.spell.SpellTemplate 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.spell.SpellTemplate in project MagicPlugin by elBukkit.
the class Wand method takeOwnership.
public void takeOwnership(Player player) {
Mage mage = this.mage;
if (mage == null) {
mage = controller.getMage(player);
}
if ((ownerId == null || ownerId.length() == 0) && quietLevel < 2) {
mage.sendMessage(getMessage("bound_instructions", "").replace("$wand", getName()));
String spellKey = getActiveSpellKey();
SpellTemplate spellTemplate = spellKey != null && !spellKey.isEmpty() ? controller.getSpellTemplate(spellKey) : null;
if (spellTemplate != null) {
String message = getMessage("spell_instructions", "").replace("$wand", getName());
mage.sendMessage(message.replace("$spell", spellTemplate.getName()));
}
if (spells.size() > 1) {
String controlKey = getControlKey(WandAction.TOGGLE);
if (controlKey != null) {
controlKey = controller.getMessages().get("controls." + controlKey);
mage.sendMessage(getMessage("inventory_instructions", "").replace("$wand", getName()).replace("$toggle", controlKey));
}
}
com.elmakers.mine.bukkit.api.wand.WandUpgradePath path = getPath();
if (path != null) {
String message = getMessage("enchant_instructions", "").replace("$wand", getName());
mage.sendMessage(message);
}
}
owner = ChatColor.stripColor(player.getDisplayName());
ownerId = mage.getId();
setProperty("owner", owner);
setProperty("owner_id", ownerId);
updateLore();
saveState();
}
use of com.elmakers.mine.bukkit.api.spell.SpellTemplate in project MagicPlugin by elBukkit.
the class Wand method getLore.
@SuppressWarnings("unchecked")
protected List<String> getLore() {
Object customLore = getProperty("lore");
if (customLore != null && customLore instanceof Collection) {
return getCustomLore((Collection<String>) customLore);
}
List<String> lore = new ArrayList<>();
int spellCount = getSpells().size();
int materialCount = getBrushes().size();
String pathName = getPathName();
if (description.length() > 0) {
if (randomizeOnActivate) {
String randomDescription = getMessage("randomized_lore");
String randomTemplate = controller.getMessages().get(getMessageKey("randomized_description"), "");
if (randomDescription.length() > 0 && !randomTemplate.isEmpty()) {
InventoryUtils.wrapText(randomTemplate.replace("$description", randomDescription), lore);
return lore;
}
}
if (description.contains("$") && !description.contains("$path")) {
String newDescription = controller.getMessages().escape(description);
if (!newDescription.equals(description)) {
this.description = newDescription;
setProperty("description", description);
}
}
String descriptionTemplate = controller.getMessages().get(getMessageKey("description_lore"), "");
if (description.contains("$path") && !descriptionTemplate.isEmpty()) {
String description = ChatColor.translateAlternateColorCodes('&', this.description);
description = description.replace("$path", pathName == null ? "Unknown" : pathName);
InventoryUtils.wrapText(descriptionTemplate.replace("$description", description), lore);
} else if (description.contains("$")) {
String randomDescription = getMessage("randomized_lore");
String randomTemplate = controller.getMessages().get(getMessageKey("randomized_description"), "");
if (randomDescription.length() > 0 && !randomTemplate.isEmpty()) {
randomDescription = ChatColor.translateAlternateColorCodes('&', randomDescription);
InventoryUtils.wrapText(randomTemplate.replace("$description", randomDescription), lore);
return lore;
}
} else if (!descriptionTemplate.isEmpty()) {
String description = ChatColor.translateAlternateColorCodes('&', this.description);
InventoryUtils.wrapText(descriptionTemplate.replace("$description", description), lore);
}
}
String pathTemplate = getMessage("path_lore", "");
if (pathName != null && !pathTemplate.isEmpty()) {
lore.add(pathTemplate.replace("$path", pathName));
}
if (!isUpgrade) {
addOwnerDescription(lore);
}
SpellTemplate spell = controller.getSpellTemplate(getActiveSpellKey());
Messages messages = controller.getMessages();
// This is here specifically for a wand that only has
// one spell now, but may get more later. Since you
// can't open the inventory in this state, you can not
// otherwise see the spell lore.
boolean isSingleSpell = spell != null && spellCount == 1 && !hasInventory && !isUpgrade;
if (isSingleSpell) {
addSpellLore(messages, spell, lore, getActiveMage(), this);
}
if (materialCount == 1 && activeBrush != null && activeBrush.length() > 0) {
lore.add(getBrushDisplayName(messages, MaterialBrush.parseMaterialKey(activeBrush)));
}
if (spellCount > 0) {
if (isUpgrade) {
ConfigurationUtils.addIfNotEmpty(getMessage("upgrade_spell_count").replace("$count", Integer.toString(spellCount)), lore);
} else if (spellCount > 1) {
ConfigurationUtils.addIfNotEmpty(getMessage("spell_count").replace("$count", Integer.toString(spellCount)), lore);
}
}
if (materialCount > 0) {
if (isUpgrade) {
ConfigurationUtils.addIfNotEmpty(getMessage("upgrade_material_count").replace("$count", Integer.toString(materialCount)), lore);
} else if (materialCount > 1) {
ConfigurationUtils.addIfNotEmpty(getMessage("material_count").replace("$count", Integer.toString(materialCount)), lore);
}
}
addUseLore(lore);
addPropertyLore(lore, isSingleSpell);
if (isUpgrade) {
ConfigurationUtils.addIfNotEmpty(getMessage("upgrade_item_description"), lore);
}
return lore;
}
use of com.elmakers.mine.bukkit.api.spell.SpellTemplate in project MagicPlugin by elBukkit.
the class Wand method getWorth.
@Override
public long getWorth() {
long worth = 0;
// TODO: Item properties, brushes, etc
Set<String> spells = getSpells();
for (String spellKey : spells) {
SpellTemplate spell = controller.getSpellTemplate(spellKey);
if (spell != null) {
worth = (long) (worth + spell.getWorth());
}
}
return worth;
}
Aggregations