use of com.elmakers.mine.bukkit.api.requirements.Requirement in project MagicPlugin by elBukkit.
the class MagicController method checkRequirements.
@Override
@Nullable
public String checkRequirements(@Nonnull CastContext context, @Nullable Collection<Requirement> requirements) {
if (requirements == null)
return null;
for (Requirement requirement : requirements) {
String type = requirement.getType();
RequirementsProcessor processor = requirementProcessors.get(type);
if (processor != null) {
if (!processor.checkRequirement(context, requirement)) {
String message = processor.getRequirementDescription(context, requirement);
if (message == null || message.isEmpty()) {
message = messages.get("requirements.unknown");
}
return message;
}
}
}
return null;
}
use of com.elmakers.mine.bukkit.api.requirements.Requirement 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.requirements.Requirement in project MagicPlugin by elBukkit.
the class BaseSpell method loadTemplate.
protected void loadTemplate(ConfigurationSection node) {
// Get localizations
String baseKey = spellKey.getBaseKey();
// Message defaults come from the messages.yml file
name = controller.getMessages().get("spells." + baseKey + ".name", baseKey);
description = controller.getMessages().get("spells." + baseKey + ".description", "");
extendedDescription = controller.getMessages().get("spells." + baseKey + ".extended_description", "");
usage = controller.getMessages().get("spells." + baseKey + ".usage", "");
// Upgrade path information
// The actual upgrade spell will be set externally.
requiredUpgradePath = node.getString("upgrade_required_path");
if (requiredUpgradePath != null && requiredUpgradePath.isEmpty()) {
requiredUpgradePath = null;
}
requiredUpgradeCasts = node.getLong("upgrade_required_casts");
List<String> pathTags = ConfigurationUtils.getStringList(node, "upgrade_required_path_tags");
if (pathTags == null || pathTags.isEmpty()) {
requiredUpgradeTags = null;
} else {
requiredUpgradeTags = new HashSet<>(pathTags);
}
requiredSpells = new ArrayList<>();
List<String> removesSpellKeys = ConfigurationUtils.getStringList(node, "removes_spells");
if (removesSpellKeys != null) {
removesSpells = new ArrayList<>(removesSpellKeys.size());
for (String key : removesSpellKeys) {
removesSpells.add(new SpellKey(key));
}
} else {
removesSpells = new ArrayList<>(0);
}
// Inheritance, currently only used to look up messages, and only goes one level deep
inheritKey = node.getString("inherit");
progressDescription = controller.getMessages().get("spell.progress_description", progressDescription);
// Can be overridden by the base spell, or the variant spell
levelDescription = controller.getMessages().get("spells." + baseKey + ".level_description", levelDescription);
progressDescription = controller.getMessages().get("spells." + baseKey + ".progress_description", progressDescription);
upgradeDescription = controller.getMessages().get("spells." + baseKey + ".upgrade_description", upgradeDescription);
// Spell level variants can override
if (spellKey.isVariant()) {
// Level description defaults to pre-formatted text
levelDescription = controller.getMessages().get("spell.level_description", levelDescription);
String variantKey = spellKey.getKey();
name = controller.getMessages().get("spells." + variantKey + ".name", name);
description = controller.getMessages().get("spells." + variantKey + ".description", description);
extendedDescription = controller.getMessages().get("spells." + variantKey + ".extended_description", extendedDescription);
usage = controller.getMessages().get("spells." + variantKey + ".usage", usage);
// Any spell may have a level description, including base spells if chosen.
// Base spells must specify their own level in each spell config though,
// they don't get an auto-generated one.
levelDescription = controller.getMessages().get("spells." + variantKey + ".level_description", levelDescription);
progressDescription = controller.getMessages().get("spells." + variantKey + ".progress_description", progressDescription);
upgradeDescription = controller.getMessages().get("spells." + variantKey + ".upgrade_description", upgradeDescription);
}
// Individual spell configuration overrides all
name = node.getString("name", name);
alias = node.getString("alias", "");
extendedDescription = node.getString("extended_description", extendedDescription);
description = node.getString("description", description);
levelDescription = node.getString("level_description", levelDescription);
progressDescription = node.getString("progress_description", progressDescription);
// Parameterize level description
if (levelDescription != null && !levelDescription.isEmpty()) {
levelDescription = levelDescription.replace("$level", Integer.toString(spellKey.getLevel()));
}
// Load basic properties
icon = ConfigurationUtils.getMaterialAndData(node, "icon", icon);
disabledIcon = ConfigurationUtils.getMaterialAndData(node, "icon_disabled", null);
iconURL = node.getString("icon_url");
iconDisabledURL = node.getString("icon_disabled_url");
color = ConfigurationUtils.getColor(node, "color", null);
worth = node.getDouble("worth", 0);
if (node.contains("worth_sp")) {
worth = node.getDouble("worth_sp", 0) * controller.getWorthSkillPoints();
}
earns = node.getInt("earns_sp", 0);
earnCooldown = node.getInt("earns_cooldown", 0);
category = controller.getCategory(node.getString("category"));
Collection<String> tagList = ConfigurationUtils.getStringList(node, "tags");
if (tagList != null) {
tags = new HashSet<>(tagList);
} else {
tags = null;
}
requiredHealth = node.getDouble("require_health_percentage", 0);
costs = parseCosts(node.getConfigurationSection("costs"));
activeCosts = parseCosts(node.getConfigurationSection("active_costs"));
pvpRestricted = node.getBoolean("pvp_restricted", false);
quickCast = node.getBoolean("quick_cast", false);
disguiseRestricted = node.getBoolean("disguise_restricted", false);
glideRestricted = node.getBoolean("glide_restricted", false);
glideExclusive = node.getBoolean("glide_exclusive", false);
worldBorderRestricted = node.getBoolean("world_border_restricted", false);
usesBrushSelection = node.getBoolean("brush_selection", false);
castOnNoTarget = node.getBoolean("cast_on_no_target", true);
hidden = node.getBoolean("hidden", false);
showUndoable = node.getBoolean("show_undoable", true);
cancellable = node.getBoolean("cancellable", true);
cancelEffects = node.getBoolean("cancel_effects", false);
disableManaRegeneration = node.getBoolean("disable_mana_regeneration", false);
String toggleString = node.getString("toggle", "NONE");
try {
toggle = ToggleType.valueOf(toggleString.toUpperCase());
} catch (Exception ex) {
controller.getLogger().warning("Invalid toggle type: " + toggleString);
}
Collection<ConfigurationSection> requirementConfigurations = ConfigurationUtils.getNodeList(node, "requirements");
if (requirementConfigurations != null) {
requirements = new ArrayList<>();
for (ConfigurationSection requirementConfiguration : requirementConfigurations) {
requirements.add(new Requirement(requirementConfiguration));
}
}
progressLevels = node.getConfigurationSection("progress_levels");
if (progressLevels != null) {
requiredCastsPerLevel = progressLevels.getLong("required_casts_per_level");
maxLevels = progressLevels.getLong("max_levels");
if (requiredCastsPerLevel <= 0 && maxLevels > 0) {
if (requiredUpgradeCasts <= 0) {
maxLevels = 0;
} else {
// auto determine casts per level
requiredCastsPerLevel = requiredUpgradeCasts / maxLevels;
}
}
progressLevelParameters = progressLevels.getConfigurationSection("parameters");
if (progressLevelParameters != null) {
Set<String> keys = progressLevelParameters.getKeys(true);
progressLevelEquations = new HashMap<>(keys.size());
for (String key : keys) {
if (progressLevelParameters.isString(key)) {
String value = progressLevelParameters.getString(key, "");
progressLevelEquations.put(key, EquationStore.getInstance().getTransform(value));
}
}
}
}
// Preload some parameters
parameters.wrap(node.getConfigurationSection("parameters"));
bypassMageCooldown = parameters.getBoolean("bypass_mage_cooldown", false);
warmup = parameters.getInt("warmup", 0);
cooldown = parameters.getInt("cooldown", 0);
cooldown = parameters.getInt("cool", cooldown);
mageCooldown = parameters.getInt("cooldown_mage", 0);
displayCooldown = parameters.getInt("display_cooldown", -1);
bypassPvpRestriction = parameters.getBoolean("bypass_pvp", false);
bypassPvpRestriction = parameters.getBoolean("bp", bypassPvpRestriction);
bypassPermissions = parameters.getBoolean("bypass_permissions", false);
bypassBuildRestriction = parameters.getBoolean("bypass_build", false);
bypassBuildRestriction = parameters.getBoolean("bb", bypassBuildRestriction);
bypassBreakRestriction = parameters.getBoolean("bypass_break", false);
bypassProtection = parameters.getBoolean("bypass_protection", false);
bypassProtection = parameters.getBoolean("bp", bypassProtection);
bypassAll = parameters.getBoolean("bypass", false);
duration = parameters.getInt("duration", 0);
totalDuration = parameters.getInt("total_duration", -1);
effects.clear();
if (node.contains("effects")) {
ConfigurationSection effectsNode = node.getConfigurationSection("effects");
Collection<String> effectKeys = effectsNode.getKeys(false);
for (String effectKey : effectKeys) {
if (effectsNode.isString(effectKey)) {
String referenceKey = effectsNode.getString(effectKey);
if (effects.containsKey(referenceKey)) {
effects.put(effectKey, new ArrayList<>(effects.get(referenceKey)));
}
} else {
effects.put(effectKey, EffectPlayer.loadEffects(controller.getPlugin(), effectsNode, effectKey));
}
}
}
}
Aggregations