use of com.elmakers.mine.bukkit.api.spell.Spell 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.spell.Spell in project MagicPlugin by elBukkit.
the class PlayerController method onPlayerInteract.
@EventHandler(priority = EventPriority.HIGHEST)
public void onPlayerInteract(PlayerInteractEvent event) {
if (!controller.isLoaded())
return;
Action action = event.getAction();
boolean isLeftClick = action == Action.LEFT_CLICK_AIR || action == Action.LEFT_CLICK_BLOCK;
boolean isRightClick = (action == Action.RIGHT_CLICK_AIR || action == Action.RIGHT_CLICK_BLOCK);
// We only care about left click and right click.
if (!isLeftClick && !isRightClick)
return;
// Note that an interact on air event will arrive pre-cancelled
// So this is kind of useless. :\
// if (event.isCancelled()) return;
// Block block = event.getClickedBlock();
// controller.getLogger().info("INTERACT: " + event.getAction() + " on " + (block == null ? "NOTHING" : block.getType()) + " cancelled: " + event.isCancelled());
Player player = event.getPlayer();
// Don't allow interacting while holding spells, brushes or upgrades
ItemStack itemInHand = player.getInventory().getItemInMainHand();
boolean isSkill = Wand.isSkill(itemInHand);
boolean isSpell = !isSkill && Wand.isSpell(itemInHand);
if (isSpell || Wand.isBrush(itemInHand) || Wand.isUpgrade(itemInHand)) {
event.setCancelled(true);
return;
}
boolean isOffhandSkill = false;
ItemStack itemInOffhand = player.getInventory().getItemInOffHand();
if (isRightClick) {
isOffhandSkill = Wand.isSkill(itemInOffhand);
boolean isOffhandSpell = !isOffhandSkill && Wand.isSpell(itemInOffhand);
if (isOffhandSpell || Wand.isBrush(itemInOffhand) || Wand.isUpgrade(itemInOffhand)) {
event.setCancelled(true);
return;
}
}
Mage mage = controller.getMage(player);
Wand wand = mage.checkWand();
if (action == Action.RIGHT_CLICK_BLOCK) {
Material material = event.getClickedBlock().getType();
isRightClick = !controller.isInteractable(event.getClickedBlock());
// This is to prevent Essentials signs from giving you an item in your wand inventory.
if (wand != null && (material == Material.SIGN_POST || material == Material.WALL_SIGN)) {
wand.closeInventory();
}
}
if (!isLeftClick && !mage.checkLastClick(clickCooldown)) {
return;
}
// Prefer wand right-click if wand is active
if (isOffhandSkill && wand != null) {
if (wand.getRightClickAction() != WandAction.NONE) {
isOffhandSkill = false;
}
}
if (isRightClick && (isOffhandSkill || isSkill)) {
if (isSkill) {
mage.useSkill(itemInHand);
} else {
mage.useSkill(itemInOffhand);
}
event.setCancelled(true);
return;
}
// Check for offhand casting
if (isRightClick) {
if (allowOffhandCasting && mage.offhandCast()) {
// which in the offhand case are right-click actions.
if (cancelInteractOnLeftClick) {
event.setCancelled(true);
}
return;
}
}
// Special-case here for skulls, which actually are not wearable via right-click.
if (itemInHand != null && isRightClick && controller.isWearable(itemInHand) && itemInHand.getType() != Material.SKULL_ITEM) {
if (wand != null) {
wand.deactivate();
}
controller.onArmorUpdated(mage);
return;
}
if (wand == null)
return;
Messages messages = controller.getMessages();
if (!controller.hasWandPermission(player)) {
return;
}
// Check for region or wand-specific permissions
if (!controller.hasWandPermission(player, wand)) {
wand.deactivate();
mage.sendMessage(messages.get("wand.no_permission").replace("$wand", wand.getName()));
return;
}
// Check for enchantment table click
Block clickedBlock = event.getClickedBlock();
if (clickedBlock != null && clickedBlock.getType() != Material.AIR && enchantBlockMaterial != null && enchantBlockMaterial.is(clickedBlock)) {
Spell spell = null;
if (player.isSneaking()) {
spell = enchantSneakClickSpell != null ? mage.getSpell(enchantSneakClickSpell) : null;
} else {
spell = enchantClickSpell != null ? mage.getSpell(enchantClickSpell) : null;
}
if (spell != null) {
spell.cast();
event.setCancelled(true);
return;
}
}
if (isLeftClick && !wand.isUpgrade() && wand.getLeftClickAction() != WandAction.NONE && cancelInteractOnLeftClick) {
event.setCancelled(true);
}
if (isRightClick && wand.performAction(wand.getRightClickAction()) && cancelInteractOnRightClick) {
event.setCancelled(true);
}
}
use of com.elmakers.mine.bukkit.api.spell.Spell in project MagicPlugin by elBukkit.
the class AnimateSpell method onCast.
@Override
public SpellResult onCast(ConfigurationSection parameters) {
if (parameters.getString("animate", null) != null) {
return super.onCast(parameters);
}
final Block targetBlock = getTargetBlock();
if (targetBlock == null) {
return SpellResult.NO_TARGET;
}
if (!hasBuildPermission(targetBlock)) {
return SpellResult.INSUFFICIENT_PERMISSION;
}
int seedRadius = parameters.getInt("seed_radius", 0);
MaterialAndData targetMaterial = new MaterialAndData(targetBlock);
List<String> materials = ConfigurationUtils.getStringList(parameters, "materials");
if (seedRadius > 0 && materials != null && !materials.isEmpty()) {
targetMaterial = new MaterialAndData(RandomUtils.getRandom(materials));
} else if (parameters.contains("material")) {
targetMaterial = ConfigurationUtils.getMaterialAndData(parameters, "material", targetMaterial);
if (targetMaterial.isValid()) {
addDestructible(targetMaterial);
}
}
if (!mage.isSuperPowered() && seedRadius == 0) {
MaterialSetManager materialSets = controller.getMaterialSetManager();
MaterialSet restricted = materialSets.getMaterialSet("restricted");
if (restricted != null && restricted.testMaterialAndData(targetMaterial)) {
return SpellResult.FAIL;
}
if (parameters.contains("restricted")) {
String customRestricted = parameters.getString("restricted");
if (customRestricted != null && !customRestricted.equals("restricted")) {
restricted = materialSets.fromConfigEmpty(customRestricted);
if (restricted.testMaterialAndData(targetMaterial)) {
return SpellResult.FAIL;
}
}
}
}
if (!isDestructible(targetBlock)) {
return SpellResult.INSUFFICIENT_PERMISSION;
}
registerForUndo(targetBlock);
if (seedRadius > 0) {
for (int dx = -seedRadius; dx < seedRadius; dx++) {
for (int dz = -seedRadius; dz < seedRadius; dz++) {
for (int dy = -seedRadius; dy < seedRadius; dy++) {
Block seedBlock = targetBlock.getRelative(dx, dy, dz);
if (isDestructible(seedBlock)) {
registerForUndo(seedBlock);
targetMaterial.modify(seedBlock);
}
}
}
}
}
// Look for randomized levels
int level = 0;
if (parameters.contains("level")) {
level = parameters.getInt("level", level);
} else if (levelWeights != null) {
level = RandomUtils.weightedRandom(levelWeights);
}
boolean simCheckDestructible = parameters.getBoolean("sim_check_destructible", true);
simCheckDestructible = parameters.getBoolean("scd", simCheckDestructible);
final ConfigurationSection automataParameters = new MemoryConfiguration();
automataParameters.set("target", "self");
automataParameters.set("cooldown", 0);
automataParameters.set("m", targetMaterial.getKey());
automataParameters.set("cd", (simCheckDestructible ? true : false));
automataParameters.set("level", level);
String automataName = parameters.getString("name", "Automata");
Messages messages = controller.getMessages();
String automataType = parameters.getString("message_type", "evil");
List<String> prefixes = messages.getAll("automata." + automataType + ".prefixes");
List<String> suffixes = messages.getAll("automata." + automataType + ".suffixes");
automataName = prefixes.get(random.nextInt(prefixes.size())) + " " + automataName + " " + suffixes.get(random.nextInt(suffixes.size()));
if (level > 1) {
automataName += " " + escapeLevel(messages, "automata.level", level);
}
String message = getMessage("cast_broadcast").replace("$name", automataName);
if (message.length() > 0) {
controller.sendToMages(message, targetBlock.getLocation());
}
automataParameters.set("animate", automataName);
String automataId = UUID.randomUUID().toString();
final Mage mage = controller.getAutomaton(automataId, automataName);
mage.setLocation(targetBlock.getLocation());
mage.setQuiet(true);
mage.addTag(getKey());
final Spell spell = mage.getSpell(getKey());
Bukkit.getScheduler().runTaskLater(controller.getPlugin(), new Runnable() {
@Override
public void run() {
spell.cast(automataParameters);
}
}, 1);
registerForUndo();
return SpellResult.CAST;
}
use of com.elmakers.mine.bukkit.api.spell.Spell in project MagicPlugin by elBukkit.
the class Mage method useSkill.
public boolean useSkill(ItemStack skillItem) {
Spell spell = getSpell(Wand.getSpell(skillItem));
if (spell == null)
return false;
boolean canUse = true;
String skillClass = Wand.getSpellClass(skillItem);
if (skillClass != null && !skillClass.isEmpty()) {
if (!setActiveClass(skillClass)) {
canUse = false;
sendMessage(controller.getMessages().get("mage.no_class").replace("$name", spell.getName()));
}
}
if (canUse) {
// TODO: Maybe find a better way to handle this.
// There is also an issue of an active wand taking over the hotbar display.
Wand activeWand = this.activeWand;
this.activeWand = null;
spell.cast();
this.activeWand = activeWand;
}
return canUse;
}
use of com.elmakers.mine.bukkit.api.spell.Spell 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;
}
Aggregations