use of com.elmakers.mine.bukkit.api.batch.SpellBatch in project MagicPlugin by elBukkit.
the class BaseSpell method cast.
@Override
public boolean cast(ConfigurationSection extraParameters, Location defaultLocation) {
if (mage.isPlayer() && mage.getPlayer().getGameMode() == GameMode.SPECTATOR) {
if (mage.getDebugLevel() > 0 && extraParameters != null) {
mage.sendDebugMessage("Cannot cast in spectator mode.");
}
return false;
}
if (toggle != ToggleType.NONE && isActive()) {
mage.sendDebugMessage(ChatColor.DARK_BLUE + "Deactivating " + ChatColor.GOLD + getName());
deactivate(true, true);
processResult(SpellResult.DEACTIVATE, parameters);
return true;
}
if (mage.getDebugLevel() > 5 && extraParameters != null) {
Collection<String> keys = extraParameters.getKeys(false);
if (keys.size() > 0) {
mage.sendDebugMessage(ChatColor.BLUE + "Cast " + ChatColor.GOLD + getName() + " " + ChatColor.GREEN + ConfigurationUtils.getParameters(extraParameters));
}
}
this.reset();
Location location = mage.getLocation();
if (location != null) {
Set<String> overrides = controller.getSpellOverrides(mage, location);
if (overrides != null && !overrides.isEmpty()) {
if (extraParameters == null) {
extraParameters = new MemoryConfiguration();
}
for (String entry : overrides) {
String[] pieces = StringUtils.split(entry, ' ');
if (pieces.length < 2)
continue;
String fullKey = pieces[0];
String[] key = StringUtils.split(fullKey, '.');
if (key.length == 0)
continue;
if (key.length == 2 && !key[0].equals("default") && !key[0].equals(spellKey.getBaseKey()) && !key[0].equals(spellKey.getKey())) {
continue;
}
fullKey = key.length == 2 ? key[1] : key[0];
ConfigurationUtils.set(extraParameters, fullKey, pieces[1]);
}
}
}
if (this.currentCast == null) {
this.currentCast = new CastContext();
this.currentCast.setSpell(this);
}
this.location = defaultLocation;
workingParameters = new SpellParameters(this);
ConfigurationUtils.addConfigurations(workingParameters, this.parameters);
ConfigurationUtils.addConfigurations(workingParameters, extraParameters);
processParameters(workingParameters);
// Check to see if this is allowed to be cast by a command block
if (!commandBlockAllowed) {
CommandSender sender = mage.getCommandSender();
if (sender != null && sender instanceof BlockCommandSender) {
Block block = mage.getLocation().getBlock();
if (block.getType() == Material.COMMAND) {
block.setType(Material.AIR);
}
return false;
}
}
// Allow other plugins to cancel this cast
PreCastEvent preCast = new PreCastEvent(mage, this);
Bukkit.getPluginManager().callEvent(preCast);
if (preCast.isCancelled()) {
processResult(SpellResult.CANCELLED, workingParameters);
sendCastMessage(SpellResult.CANCELLED, " (no cast)");
return false;
}
// Don't allow casting if the player is confused or weakened
bypassConfusion = workingParameters.getBoolean("bypass_confusion", bypassConfusion);
bypassWeakness = workingParameters.getBoolean("bypass_weakness", bypassWeakness);
LivingEntity livingEntity = mage.getLivingEntity();
if (livingEntity != null && !mage.isSuperPowered()) {
if (!bypassConfusion && livingEntity.hasPotionEffect(PotionEffectType.CONFUSION)) {
processResult(SpellResult.CURSED, workingParameters);
sendCastMessage(SpellResult.CURSED, " (no cast)");
return false;
}
// Don't allow casting if the player is weakened
if (!bypassWeakness && livingEntity.hasPotionEffect(PotionEffectType.WEAKNESS)) {
processResult(SpellResult.CURSED, workingParameters);
sendCastMessage(SpellResult.CURSED, " (no cast)");
return false;
}
}
// Don't perform permission check until after processing parameters, in case of overrides
if (!canCast(getLocation())) {
processResult(SpellResult.INSUFFICIENT_PERMISSION, workingParameters);
sendCastMessage(SpellResult.INSUFFICIENT_PERMISSION, " (no cast)");
if (mage.getDebugLevel() > 1) {
CommandSender messageTarget = mage.getDebugger();
if (messageTarget == null) {
messageTarget = mage.getCommandSender();
}
if (messageTarget != null) {
mage.debugPermissions(messageTarget, this);
}
}
return false;
}
this.preCast();
// PVP override settings
bypassPvpRestriction = workingParameters.getBoolean("bypass_pvp", false);
bypassPvpRestriction = workingParameters.getBoolean("bp", bypassPvpRestriction);
bypassPermissions = workingParameters.getBoolean("bypass_permissions", bypassPermissions);
bypassFriendlyFire = workingParameters.getBoolean("bypass_friendly_fire", false);
onlyFriendlyFire = workingParameters.getBoolean("only_friendly", false);
// Check cooldowns
cooldown = workingParameters.getInt("cooldown", cooldown);
cooldown = workingParameters.getInt("cool", cooldown);
mageCooldown = workingParameters.getInt("cooldown_mage", mageCooldown);
// Color override
color = ConfigurationUtils.getColor(workingParameters, "color", color);
particle = workingParameters.getString("particle", null);
double cooldownRemaining = getRemainingCooldown() / 1000.0;
String timeDescription = "";
if (cooldownRemaining > 0) {
if (cooldownRemaining > 60 * 60) {
long hours = (long) Math.ceil(cooldownRemaining / (60 * 60));
if (hours == 1) {
timeDescription = controller.getMessages().get("cooldown.wait_hour");
} else {
timeDescription = controller.getMessages().get("cooldown.wait_hours").replace("$hours", Long.toString(hours));
}
} else if (cooldownRemaining > 60) {
long minutes = (long) Math.ceil(cooldownRemaining / 60);
if (minutes == 1) {
timeDescription = controller.getMessages().get("cooldown.wait_minute");
} else {
timeDescription = controller.getMessages().get("cooldown.wait_minutes").replace("$minutes", Long.toString(minutes));
}
} else if (cooldownRemaining >= 1) {
long seconds = (long) Math.ceil(cooldownRemaining);
if (seconds == 1) {
timeDescription = controller.getMessages().get("cooldown.wait_second");
} else {
timeDescription = controller.getMessages().get("cooldown.wait_seconds").replace("$seconds", Long.toString(seconds));
}
} else {
timeDescription = controller.getMessages().get("cooldown.wait_moment");
if (timeDescription.contains("$seconds")) {
timeDescription = timeDescription.replace("$seconds", SECONDS_FORMATTER.format(cooldownRemaining));
}
}
castMessage(getMessage("cooldown").replace("$time", timeDescription));
processResult(SpellResult.COOLDOWN, workingParameters);
sendCastMessage(SpellResult.COOLDOWN, " (no cast)");
return false;
}
CastingCost required = getRequiredCost();
if (required != null) {
String baseMessage = getMessage("insufficient_resources");
String costDescription = required.getDescription(controller.getMessages(), mage);
// Send loud messages when items are required.
if (required.isItem()) {
sendMessage(baseMessage.replace("$cost", costDescription));
} else {
castMessage(baseMessage.replace("$cost", costDescription));
}
processResult(SpellResult.INSUFFICIENT_RESOURCES, workingParameters);
sendCastMessage(SpellResult.INSUFFICIENT_RESOURCES, " (no cast)");
return false;
}
if (requiredHealth > 0) {
LivingEntity li = mage.getLivingEntity();
double healthPercentage = li == null ? 0 : 100 * li.getHealth() / li.getMaxHealth();
if (healthPercentage < requiredHealth) {
processResult(SpellResult.INSUFFICIENT_RESOURCES, workingParameters);
sendCastMessage(SpellResult.INSUFFICIENT_RESOURCES, " (no cast)");
return false;
}
}
if (controller.isSpellProgressionEnabled()) {
long progressLevel = getProgressLevel();
for (Entry<String, EquationTransform> entry : progressLevelEquations.entrySet()) {
workingParameters.set(entry.getKey(), entry.getValue().get(progressLevel));
}
}
// Check for cancel-on-cast-other spells, after we have determined that this spell can really be cast.
if (!passive) {
for (Iterator<Batch> iterator = mage.getPendingBatches().iterator(); iterator.hasNext(); ) {
Batch batch = iterator.next();
if (!(batch instanceof SpellBatch))
continue;
SpellBatch spellBatch = (SpellBatch) batch;
Spell spell = spellBatch.getSpell();
if (spell.cancelOnCastOther()) {
spell.cancel();
batch.finish();
iterator.remove();
}
}
}
return finalizeCast(workingParameters);
}
use of com.elmakers.mine.bukkit.api.batch.SpellBatch in project MagicPlugin by elBukkit.
the class Mage method cancelPending.
@Nullable
@Override
public Batch cancelPending(String spellKey, boolean force) {
Batch stoppedPending = null;
if (pendingBatches.size() > 0) {
List<Batch> batches = new ArrayList<>();
batches.addAll(pendingBatches);
for (Batch batch : batches) {
if (spellKey != null || !force) {
if (!(batch instanceof SpellBatch)) {
continue;
}
SpellBatch spellBatch = (SpellBatch) batch;
Spell spell = spellBatch.getSpell();
if (spell == null) {
continue;
}
if (!force && !spell.isCancellable()) {
continue;
}
if (spellKey != null && !spell.getSpellKey().getBaseKey().equalsIgnoreCase(spellKey)) {
continue;
}
}
if (!(batch instanceof UndoBatch)) {
if (batch instanceof SpellBatch) {
SpellBatch spellBatch = (SpellBatch) batch;
Spell spell = spellBatch.getSpell();
if (spell != null) {
spell.cancel();
}
}
batch.finish();
pendingBatches.remove(batch);
stoppedPending = batch;
}
}
}
return stoppedPending;
}
use of com.elmakers.mine.bukkit.api.batch.SpellBatch in project MagicPlugin by elBukkit.
the class Mage method onDamage.
public void onDamage(EntityDamageEvent event) {
String damageType = currentDamageType;
currentDamageType = null;
LivingEntity entity = getLivingEntity();
if (entity == null) {
return;
}
// Send on to any registered spells
List<Listener> active = new ArrayList<>(damageListeners);
for (Listener listener : active) {
callEvent(listener, event);
if (event.isCancelled())
break;
}
EntityDamageEvent.DamageCause cause = event.getCause();
if (cause == EntityDamageEvent.DamageCause.FALL) {
if (fallProtectionCount > 0 && fallProtection > 0 && fallProtection > System.currentTimeMillis()) {
event.setCancelled(true);
fallProtectionCount--;
if (fallingSpell != null) {
double scale = 1;
LivingEntity li = getLivingEntity();
if (li != null) {
scale = event.getDamage() / li.getMaxHealth();
}
fallingSpell.playEffects("land", (float) scale, getLocation().getBlock().getRelative(BlockFace.DOWN));
}
if (fallProtectionCount <= 0) {
fallProtection = 0;
fallingSpell = null;
}
return;
} else {
fallingSpell = null;
}
}
if (isSuperProtected()) {
event.setCancelled(true);
if (entity.getFireTicks() > 0) {
entity.setFireTicks(0);
}
return;
}
if (event.isCancelled()) {
return;
}
// First check for damage reduction
double reduction = 0;
Double overallProtection = protection.get("overall");
if (overallProtection != null) {
reduction = overallProtection * controller.getMaxDamageReduction("overall");
}
// Apply weaknesses
double multiplier = 1;
Double overallWeakness = weakness.get("overall");
if (overallWeakness != null && overallWeakness > 0) {
double defendMultiplier = controller.getMaxDefendMultiplier("overall");
if (defendMultiplier > 1) {
defendMultiplier = 1 + (defendMultiplier - 1) * overallWeakness;
multiplier *= defendMultiplier;
}
}
if (cause == EntityDamageEvent.DamageCause.FIRE_TICK) {
// Also put out fire if they have maxed out fire protection.
double damageReductionFire = getProtection("fire");
if (damageReductionFire >= 1 && entity.getFireTicks() > 0) {
entity.setFireTicks(0);
}
}
if (damageType == null) {
switch(cause) {
case CONTACT:
case ENTITY_ATTACK:
damageType = "physical";
break;
case FIRE:
case FIRE_TICK:
case LAVA:
damageType = "fire";
break;
case BLOCK_EXPLOSION:
case ENTITY_EXPLOSION:
damageType = "explosion";
break;
default:
damageType = cause.name().toLowerCase();
break;
}
}
lastDamageType = damageType;
double protection = getProtection(damageType);
double maxReduction = controller.getMaxDamageReduction(damageType);
reduction += protection * maxReduction;
if (reduction >= 1) {
event.setCancelled(true);
sendDebugMessage(ChatColor.RED + "Damage nullified by " + ChatColor.BLUE + damageType + " (" + cause + ")", 8);
return;
}
double damage = event.getDamage();
sendDebugMessage(ChatColor.RED + "Damaged by " + ChatColor.BLUE + (damageType == null ? "generic" : damageType) + " (" + cause + ")" + ChatColor.RED + " for " + ChatColor.DARK_RED + damage, 10);
if (reduction > 0) {
damage = (1.0 - reduction) * damage;
sendDebugMessage(ChatColor.DARK_RED + "Damage type " + ChatColor.BLUE + damageType + " reduced by " + ChatColor.AQUA + reduction + ChatColor.DARK_RED + " to " + ChatColor.RED + damage, 9);
event.setDamage(damage);
}
double weakness = getWeakness(damageType);
double maxMultiplier = controller.getMaxDefendMultiplier(damageType);
if (maxMultiplier > 1 && weakness > 0) {
weakness = 1 + (maxMultiplier - 1) * weakness;
multiplier *= weakness;
}
if (multiplier > 1) {
damage = multiplier * damage;
sendDebugMessage(ChatColor.DARK_RED + "Damage type " + ChatColor.BLUE + damageType + " multiplied by " + ChatColor.AQUA + multiplier + ChatColor.DARK_RED + " to " + ChatColor.RED + damage, 9);
event.setDamage(damage);
}
if (damage > 0) {
for (Iterator<Batch> iterator = pendingBatches.iterator(); iterator.hasNext(); ) {
Batch batch = iterator.next();
if (!(batch instanceof SpellBatch))
continue;
SpellBatch spellBatch = (SpellBatch) batch;
Spell spell = spellBatch.getSpell();
double cancelOnDamage = spell.cancelOnDamage();
if (cancelOnDamage > 0 && cancelOnDamage < damage) {
spell.cancel();
batch.finish();
iterator.remove();
}
}
}
}
use of com.elmakers.mine.bukkit.api.batch.SpellBatch in project MagicPlugin by elBukkit.
the class UndoSpell method onCast.
@Override
public SpellResult onCast(ConfigurationSection parameters) {
Target target = getTarget();
int timeout = parameters.getInt("target_timeout", 0);
boolean targetSelf = parameters.getBoolean("target_up_self", false);
boolean targetDown = parameters.getBoolean("target_down_block", false);
Entity targetEntity = target.getEntity();
SpellResult result = SpellResult.CAST;
if (targetSelf && isLookingUp()) {
targetEntity = mage.getEntity();
getCurrentCast().setTargetName(mage.getName());
result = SpellResult.ALTERNATE_UP;
}
if (targetEntity != null && controller.isMage(targetEntity)) {
Mage targetMage = controller.getMage(targetEntity);
Batch batch = targetMage.cancelPending();
if (batch != null) {
undoListName = (batch instanceof SpellBatch) ? ((SpellBatch) batch).getSpell().getName() : null;
return SpellResult.ALTERNATE;
}
UndoQueue queue = targetMage.getUndoQueue();
UndoList undoList = queue.undoRecent(timeout);
if (undoList != null) {
undoListName = undoList.getName();
}
return undoList != null ? result : SpellResult.NO_TARGET;
}
if (!parameters.getBoolean("target_blocks", true)) {
return SpellResult.NO_TARGET;
}
Block targetBlock = target.getBlock();
if (targetDown && isLookingDown()) {
targetBlock = getLocation().getBlock();
}
if (targetBlock != null) {
boolean targetAll = mage.isSuperPowered();
if (targetAll) {
UndoList undid = controller.undoRecent(targetBlock, timeout);
if (undid != null) {
Mage targetMage = undid.getOwner();
undoListName = undid.getName();
getCurrentCast().setTargetName(targetMage.getName());
return result;
}
} else {
getCurrentCast().setTargetName(mage.getName());
UndoList undoList = mage.undo(targetBlock);
if (undoList != null) {
undoListName = undoList.getName();
return result;
}
}
}
return SpellResult.NO_TARGET;
}
Aggregations