use of com.elmakers.mine.bukkit.api.magic.MageController in project MagicPlugin by elBukkit.
the class SkillSelectorAction method perform.
@Override
public SpellResult perform(CastContext context) {
this.context = context;
MageController apiController = context.getController();
Player player = context.getMage().getPlayer();
if (player == null) {
return SpellResult.PLAYER_REQUIRED;
}
if (!(apiController instanceof MagicController))
return SpellResult.FAIL;
MagicController controller = (MagicController) apiController;
Messages messages = controller.getMessages();
HeroesManager heroes = controller.getHeroes();
allSkills.clear();
if (controller.useHeroesSkills() && heroes != null) {
@Nonnull String classString = heroes.getClassName(player);
@Nonnull String class2String = heroes.getSecondaryClassName(player);
String messageKey = !class2String.isEmpty() ? "skills.inventory_title_secondary" : "skills.inventory_title";
inventoryTitle = controller.getMessages().get(messageKey, "Skills ($page/$pages)");
inventoryTitle = inventoryTitle.replace("$class2", class2String).replace("$class", classString);
List<String> heroesSkills = heroes.getSkillList(player, true, true);
for (String heroesSkill : heroesSkills) {
allSkills.add(new SkillDescription(heroes, controller, player, heroesSkill));
}
} else {
inventoryTitle = messages.get("skills.inventory_title");
}
if (controller.usePermissionSkills()) {
boolean bypassHidden = player.hasPermission("Magic.bypass_hidden");
Collection<SpellTemplate> spells = controller.getSpellTemplates(bypassHidden);
for (SpellTemplate spell : spells) {
SpellKey key = spell.getSpellKey();
if (key.isVariant())
continue;
if (key.getBaseKey().startsWith("heroes*"))
continue;
if (!spell.hasCastPermission(player))
continue;
allSkills.add(new SkillDescription(spell));
}
} else {
Mage mage = controller.getMage(player);
MageClass activeClass = mage.getActiveClass();
if (activeClass != null) {
// gather spells in player's inventory to avoid showing
Set<String> hasSpells = new HashSet<>();
for (ItemStack item : player.getInventory().getContents()) {
String spellKey = controller.getSpell(item);
if (spellKey != null) {
hasSpells.add(spellKey);
}
}
classKey = activeClass.getKey();
skillsConfig = activeClass.getProperty("skills", ConfigurationSection.class);
inventoryLimit = activeClass.getProperty("skills.skill_limit", 0);
Collection<String> spells = activeClass.getSpells();
for (String spellKey : spells) {
if (hasSpells.contains(spellKey)) {
extraSlots++;
continue;
}
SpellTemplate spell = controller.getSpellTemplate(spellKey);
if (spell != null) {
allSkills.add(new SkillDescription(spell));
}
}
}
}
if (allSkills.size() == 0) {
player.sendMessage(messages.get("skills.none"));
return SpellResult.NO_ACTION;
}
Collections.sort(allSkills);
openInventory();
return SpellResult.CAST;
}
use of com.elmakers.mine.bukkit.api.magic.MageController in project MagicPlugin by elBukkit.
the class SkillSelectorAction method openInventory.
protected void openInventory() {
MageController apiController = context.getController();
if (!(apiController instanceof MagicController))
return;
MagicController controller = (MagicController) apiController;
HeroesManager heroes = controller.getHeroes();
int inventorySize = 9 * controller.getSkillInventoryRows();
float numSlots = extraSlots + allSkills.size();
int numPages = (int) Math.ceil(numSlots / inventorySize);
if (page < 1)
page = numPages;
else if (page > numPages)
page = 1;
Mage mage = context.getMage();
Player player = mage.getPlayer();
int pageIndex = page - 1;
int startIndex = pageIndex * inventorySize;
int maxIndex = (pageIndex + 1) * inventorySize - 1;
List<SkillDescription> skills = new ArrayList<>();
for (int i = startIndex; i <= maxIndex && i < allSkills.size(); i++) {
skills.add(allSkills.get(i));
}
if (skills.size() == 0) {
String messageTemplate = controller.getMessages().get("skills.none_on_page", "$page");
player.sendMessage(messageTemplate.replace("$page", Integer.toString(page)));
return;
}
int invSize = (int) Math.ceil(numSlots / 9.0f) * 9;
String title = inventoryTitle;
title = title.replace("$pages", Integer.toString(numPages)).replace("$page", Integer.toString(page));
Inventory displayInventory = CompatibilityUtils.createInventory(null, invSize, title);
for (SkillDescription skill : skills) {
ItemStack skillItem = controller.getAPI().createItem(skill.getSkillKey(), mage);
if (skillItem == null)
continue;
InventoryUtils.configureSkillItem(skillItem, classKey, skillsConfig);
if (skill.isHeroes() && heroes != null && !heroes.canUseSkill(player, skill.heroesSkill)) {
String nameTemplate = controller.getMessages().get("skills.item_name_unavailable", "$skill");
String spellName = skill.spell != null ? skill.spell.getName() : skill.heroesSkill;
CompatibilityUtils.setDisplayName(skillItem, nameTemplate.replace("$skill", spellName));
InventoryUtils.setMetaBoolean(skillItem, "unavailable", true);
if (skill.spell != null) {
MaterialAndData disabledIcon = skill.spell.getDisabledIcon();
if (disabledIcon != null) {
disabledIcon.applyToItem(skillItem);
} else {
String disabledIconURL = skill.spell.getDisabledIconURL();
if (disabledIconURL != null && !disabledIconURL.isEmpty()) {
InventoryUtils.setNewSkullURL(skillItem, disabledIconURL);
}
}
}
}
displayInventory.addItem(skillItem);
}
mage.deactivateGUI();
mage.activateGUI(this, displayInventory);
}
use of com.elmakers.mine.bukkit.api.magic.MageController in project MagicPlugin by elBukkit.
the class PowerBlockAction method perform.
@SuppressWarnings("deprecation")
@Override
public SpellResult perform(CastContext context) {
Block block = context.getTargetBlock();
if (!context.hasBuildPermission(block)) {
return SpellResult.INSUFFICIENT_PERMISSION;
}
if (!context.isDestructible(block)) {
return SpellResult.NO_TARGET;
}
context.getUndoList().setApplyPhysics(true);
Material material = block.getType();
BlockState blockState = block.getState();
MaterialData data = blockState.getData();
MageController controller = context.getController();
boolean powerBlock = false;
if (data instanceof Button) {
Button powerData = (Button) data;
context.registerForUndo(block);
powerData.setPowered(!powerData.isPowered());
powerBlock = true;
} else if (data instanceof Lever) {
Lever powerData = (Lever) data;
context.registerForUndo(block);
powerData.setPowered(!powerData.isPowered());
powerBlock = true;
} else if (data instanceof PistonBaseMaterial) {
PistonBaseMaterial powerData = (PistonBaseMaterial) data;
context.registerForUndo(block);
powerData.setPowered(!powerData.isPowered());
powerBlock = true;
} else if (data instanceof PoweredRail) {
PoweredRail powerData = (PoweredRail) data;
context.registerForUndo(block);
powerData.setPowered(!powerData.isPowered());
powerBlock = true;
} else if (data instanceof RedstoneWire) {
RedstoneWire wireData = (RedstoneWire) data;
context.registerForUndo(block);
wireData.setData((byte) (15 - wireData.getData()));
powerBlock = true;
} else if (material == Material.REDSTONE_BLOCK) {
context.registerForUndo(block);
block.getWorld().playEffect(block.getLocation(), Effect.STEP_SOUND, material.getId());
controller.getRedstoneReplacement().modify(block, applyPhysics);
} else if (material == Material.REDSTONE_TORCH_OFF) {
context.registerForUndo(block);
block.setType(Material.REDSTONE_TORCH_ON);
} else if (material == Material.REDSTONE_TORCH_ON) {
context.registerForUndo(block);
block.setType(Material.REDSTONE_TORCH_OFF);
} else if (material == Material.TNT) {
context.registerForUndo(block);
block.setType(Material.AIR);
// Kaboomy time!
context.registerForUndo(block.getLocation().getWorld().spawnEntity(block.getLocation(), EntityType.PRIMED_TNT));
}
if (powerBlock) {
blockState.update();
}
return SpellResult.CAST;
}
use of com.elmakers.mine.bukkit.api.magic.MageController in project MagicPlugin by elBukkit.
the class RecallAction method getUnknownWarp.
@Nullable
protected Waypoint getUnknownWarp(String warpKey) {
MageController controller = context.getController();
Location warpLocation = controller.getWarp(warpKey);
if (warpLocation == null || warpLocation.getWorld() == null) {
return null;
}
String castMessage = context.getMessage("cast_warp").replace("$name", warpKey);
String failMessage = context.getMessage("no_target_warp").replace("$name", warpKey);
return new Waypoint(RecallType.WARP, warpLocation, warpKey, castMessage, failMessage, "", null, null);
}
use of com.elmakers.mine.bukkit.api.magic.MageController in project MagicPlugin by elBukkit.
the class DamageAction method perform.
@Override
public SpellResult perform(CastContext context) {
Entity entity = context.getTargetEntity();
if (entity == null || !(entity instanceof Damageable) || entity.isDead()) {
return SpellResult.NO_TARGET;
}
double damage = 1;
Damageable targetEntity = (Damageable) entity;
LivingEntity livingTarget = (entity instanceof LivingEntity) ? (LivingEntity) entity : null;
context.registerDamaged(targetEntity);
Mage mage = context.getMage();
MageController controller = context.getController();
double previousKnockbackResistance = 0D;
try {
if (knockbackResistance != null && livingTarget != null) {
AttributeInstance knockBackAttribute = livingTarget.getAttribute(Attribute.GENERIC_KNOCKBACK_RESISTANCE);
previousKnockbackResistance = knockBackAttribute.getBaseValue();
knockBackAttribute.setBaseValue(knockbackResistance);
}
if (controller.isElemental(entity)) {
damage = elementalDamage;
controller.damageElemental(entity, damage * mage.getDamageMultiplier(), 0, mage.getCommandSender());
} else {
if (percentage != null) {
damage = percentage * targetEntity.getMaxHealth();
} else if (targetEntity instanceof Player) {
damage = playerDamage;
} else {
damage = entityDamage;
}
String multiplierType = damageType;
if (multiplierType == null) {
multiplierType = magicDamage ? "magic" : "physical";
}
double mageMultiplier = mage.getDamageMultiplier(multiplierType);
damage *= mageMultiplier;
if (maxDistanceSquared > 0) {
double distanceSquared = context.getLocation().distanceSquared(entity.getLocation());
if (distanceSquared > maxDistanceSquared) {
return SpellResult.NO_TARGET;
}
if (distanceSquared > 0) {
damage = damage * (1 - distanceSquared / maxDistanceSquared);
}
}
if (damageMultiplier != null) {
damage *= damageMultiplier;
mageMultiplier *= damageMultiplier;
}
if (damageType != null) {
Mage targetMage = controller.getRegisteredMage(targetEntity);
String targetAnnotation = "";
if (targetMage != null) {
targetMage.setLastDamageType(damageType);
} else {
targetAnnotation = "*";
}
mage.sendDebugMessage(ChatColor.RED + "Damaging (" + ChatColor.DARK_RED + damageType + ChatColor.RED + ") x " + ChatColor.DARK_RED + mageMultiplier + ChatColor.RED + " to " + ChatColor.BLUE + targetEntity.getType() + targetAnnotation + ": " + ChatColor.RED + damage, 5);
// mages since other plugins may be tracking kills.
if (mage.isPlayer() && controller.getDamageTypes().contains(damageType)) {
CompatibilityUtils.magicDamage(targetEntity, damage, mage.getEntity());
} else {
CompatibilityUtils.damage(targetEntity, damage, mage.getEntity(), damageType);
}
} else if (magicDamage && (magicEntityDamage || targetEntity instanceof Player)) {
mage.sendDebugMessage(ChatColor.RED + "Damaging (Magic) x " + ChatColor.DARK_RED + mageMultiplier + ChatColor.RED + " to " + ChatColor.BLUE + targetEntity.getType() + ": " + damage, 5);
CompatibilityUtils.magicDamage(targetEntity, damage, mage.getEntity());
} else {
mage.sendDebugMessage(ChatColor.RED + "Damaging x " + ChatColor.DARK_RED + mageMultiplier + ChatColor.RED + " to " + ChatColor.BLUE + targetEntity.getType() + ": " + damage, 5);
CompatibilityUtils.damage(targetEntity, damage, mage.getEntity());
}
if (damage == (int) damage) {
context.addMessageParameter("damage", Integer.toString((int) damage));
} else {
context.addMessageParameter("damage", Double.toString(damage));
}
if (damageType != null && !damageType.isEmpty()) {
String typeDescription = context.getController().getMessages().get("damage_types." + damageType, damageType);
context.addMessageParameter("damage_type", typeDescription);
}
}
} finally {
if (knockbackResistance != null && livingTarget != null) {
AttributeInstance knockBackAttribute = livingTarget.getAttribute(Attribute.GENERIC_KNOCKBACK_RESISTANCE);
knockBackAttribute.setBaseValue(previousKnockbackResistance);
}
}
return SpellResult.CAST;
}
Aggregations