use of com.elmakers.mine.bukkit.api.magic.MageController in project MagicPlugin by elBukkit.
the class GiveItemAction method perform.
@Override
public SpellResult perform(CastContext context) {
if (item == null) {
return SpellResult.FAIL;
}
Entity targetEntity = context.getTargetEntity();
if (targetEntity == null) {
return SpellResult.NO_TARGET;
}
if (!(targetEntity instanceof Player)) {
return SpellResult.PLAYER_REQUIRED;
}
MageController controller = context.getController();
Player player = (Player) targetEntity;
if (permissionNode != null && !player.hasPermission(permissionNode)) {
return SpellResult.INSUFFICIENT_PERMISSION;
}
if (requireItem != null && !controller.takeItem(player, requireItem)) {
context.showMessage("insufficient_resources", "You must have a $requires");
return SpellResult.INSUFFICIENT_RESOURCES;
}
Mage mage = controller.getMage(player);
mage.giveItem(InventoryUtils.getCopy(item));
DeprecatedUtils.updateInventory(player);
return SpellResult.CAST;
}
use of com.elmakers.mine.bukkit.api.magic.MageController in project MagicPlugin by elBukkit.
the class UndoAction method perform.
@Override
public SpellResult perform(CastContext context) {
// Start of new functionality
if (undoOldest > 0 || undoToSize > 0) {
return performNew(context);
}
// Old functionality- this should be converted into an action that processes
// blocks instead of creating a separate batch.
Entity targetEntity = context.getTargetEntity();
SpellResult result = SpellResult.CAST;
Mage mage = context.getMage();
if (targetSelf) {
targetEntity = context.getEntity();
context.setTargetName(mage.getName());
result = SpellResult.ALTERNATE_UP;
}
MageController controller = context.getController();
if (targetEntity != null && controller.isMage(targetEntity)) {
Mage targetMage = controller.getMage(targetEntity);
mage.sendDebugMessage(ChatColor.AQUA + "Undo checking last spell of " + ChatColor.GOLD + targetMage + ChatColor.AQUA + " with timeout of " + ChatColor.YELLOW + timeout + ChatColor.AQUA + " for target spellKey" + ChatColor.BLUE + targetSpellKey, 2);
Batch batch = targetMage.cancelPending(targetSpellKey);
if (batch != null) {
undoListName = batch.getName();
if (cancel) {
return SpellResult.DEACTIVATE;
}
}
UndoQueue queue = targetMage.getUndoQueue();
UndoList undoList = queue.undoRecent(timeout, targetSpellKey);
if (undoList != null) {
undoListName = undoList.getName();
}
return undoList != null ? result : SpellResult.NO_TARGET;
}
if (!targetBlocks) {
return SpellResult.NO_TARGET;
}
Block targetBlock = context.getTargetBlock();
if (targetDown) {
targetBlock = context.getLocation().getBlock();
}
if (targetBlock != null) {
boolean undoAny = targetOtherBlocks;
undoAny = undoAny || (adminPermission != null && context.getController().hasPermission(context.getMage().getCommandSender(), adminPermission));
undoAny = undoAny || mage.isSuperPowered();
if (undoAny) {
mage.sendDebugMessage(ChatColor.AQUA + "Looking for recent cast at " + ChatColor.GOLD + targetBlock + ChatColor.AQUA + " with timeout of " + ChatColor.YELLOW + blockTimeout, 2);
UndoList undid = controller.undoRecent(targetBlock, blockTimeout);
if (undid != null) {
Mage targetMage = undid.getOwner();
undoListName = undid.getName();
if (targetMage != null) {
context.setTargetName(targetMage.getName());
}
return result;
}
} else {
mage.sendDebugMessage(ChatColor.AQUA + "Looking for recent self-cast at " + ChatColor.GOLD + targetBlock, 2);
context.setTargetName(mage.getName());
UndoList undoList = mage.undo(targetBlock);
if (undoList != null) {
undoListName = undoList.getName();
return result;
}
}
}
return SpellResult.NO_TARGET;
}
use of com.elmakers.mine.bukkit.api.magic.MageController in project MagicPlugin by elBukkit.
the class StashWandAction method perform.
@Override
public SpellResult perform(CastContext context) {
Entity entity = context.getTargetEntity();
if (entity == null) {
if (!context.getTargetsCaster())
return SpellResult.NO_TARGET;
entity = context.getEntity();
}
if (entity == null || !(entity instanceof Player)) {
return SpellResult.NO_TARGET;
}
Player player = (Player) entity;
MageController controller = context.getController();
Mage mage = controller.getMage(player);
Wand activeWand = mage.getActiveWand();
Wand offhandWand = mage.getOffhandWand();
// Check for trying to stash an item in the offhand slot
ItemStack activeItem = null;
if (offhandWand == context.getWand()) {
isOffhand = true;
activeWand = offhandWand;
activeItem = player.getInventory().getItemInOffHand();
} else if (activeWand != context.getWand()) {
return SpellResult.NO_TARGET;
} else {
isOffhand = false;
activeItem = player.getInventory().getItemInMainHand();
}
if (InventoryUtils.isEmpty(activeItem)) {
return SpellResult.FAIL;
}
if (activeWand != null) {
activeWand.deactivate();
}
slotNumber = player.getInventory().getHeldItemSlot();
if (isOffhand) {
stashedItem = player.getInventory().getItemInOffHand();
player.getInventory().setItemInOffHand(new ItemStack(Material.AIR));
} else {
stashedItem = player.getInventory().getItemInMainHand();
player.getInventory().setItemInMainHand(new ItemStack(Material.AIR));
}
targetMage = mage;
context.registerForUndo(new StashWandUndoAction());
return SpellResult.CAST;
}
use of com.elmakers.mine.bukkit.api.magic.MageController in project MagicPlugin by elBukkit.
the class MagicRequirement method getRequirementDescription.
@Nullable
public String getRequirementDescription(@Nonnull CastContext context) {
Mage mage = context.getMage();
MageController controller = mage.getController();
Player player = mage.getPlayer();
if (permissionNode != null && (player == null || !player.hasPermission(permissionNode))) {
return context.getMessage(SpellResult.INSUFFICIENT_PERMISSION.name().toLowerCase());
}
Wand wand = context.getWand();
if (wand == null && requireWand) {
return getMessage(context, "no_wand");
}
if (requiredTemplate != null) {
String template = wand.getTemplateKey();
if (template == null || !template.equals(requiredTemplate)) {
return getMessage(context, "no_template").replace("$wand", wand.getName());
}
}
if (requiredTemplates != null) {
String template = wand.getTemplateKey();
if (template == null || !requiredTemplates.contains(template)) {
return getMessage(context, "no_template").replace("$wand", wand.getName());
}
}
if (mageClass != null && !mageClass.isEmpty()) {
if (mage.hasClassUnlocked(mageClass)) {
return getMessage(context, "no_class").replace("$class", mageClass);
}
}
CasterProperties checkProperties = context.getActiveProperties();
ProgressionPath path = checkProperties.getPath();
if (requiredPath != null || exactPath != null) {
if (path == null) {
return getMessage(context, "no_path");
}
if (requiredPath != null && !path.hasPath(requiredPath)) {
WandUpgradePath requiresPath = controller.getPath(requiredPath);
String pathName = requiredPath;
if (requiresPath != null) {
pathName = requiresPath.getName();
} else {
context.getLogger().warning("Invalid path specified in requirement " + requiredPath);
}
return getMessage(context, "no_required_path").replace("$path", pathName);
}
if (exactPath != null && !exactPath.equals(path.getKey())) {
WandUpgradePath requiresPath = controller.getPath(exactPath);
String pathName = exactPath;
if (requiresPath != null) {
pathName = requiresPath.getName();
} else {
context.getLogger().warning("Invalid path specified in requirement: " + exactPath);
}
return getMessage(context, "no_path_exact").replace("$path", pathName);
}
if (requiresCompletedPath != null) {
boolean hasPathCompleted = false;
if (path.hasPath(requiresCompletedPath)) {
if (path.getKey().equals(requiresCompletedPath)) {
hasPathCompleted = !path.canProgress(checkProperties);
} else {
hasPathCompleted = true;
}
}
if (!hasPathCompleted) {
WandUpgradePath requiresPath = controller.getPath(requiresCompletedPath);
String pathName = requiresCompletedPath;
if (requiresPath != null) {
pathName = requiresPath.getName();
} else {
context.getLogger().warning("Invalid path specified in requirement: " + exactPath);
}
return getMessage(context, "no_path_end").replace("$path", pathName);
}
}
}
if (wandProperties != null) {
String message = getRequiredProperty(context, wand, wandProperties);
if (message != null) {
return message;
}
}
if (classProperties != null) {
MageClass activeClass = mageClass == null ? mage.getActiveClass() : mage.getClass(mageClass);
if (activeClass == null) {
return getMessage(context, "no_path");
}
String message = getRequiredProperty(context, activeClass, classProperties);
if (message != null) {
return message;
}
}
if (attributes != null) {
for (PropertyRequirement requirement : attributes) {
String key = requirement.key;
Double value = mage.getAttribute(key);
String message = checkRequiredProperty(context, requirement, key, value);
if (message != null) {
return message;
}
}
}
return null;
}
use of com.elmakers.mine.bukkit.api.magic.MageController in project MagicPlugin by elBukkit.
the class WandCommandExecutor method onWandSave.
public boolean onWandSave(CommandSender sender, Player player, String[] parameters) {
if (parameters.length < 1) {
sender.sendMessage("Use: /wand save <filename>");
return true;
}
Wand wand = checkWand(sender, player);
if (wand == null) {
return true;
}
MageController controller = api.getController();
String template = parameters[0];
WandTemplate existing = controller.getWandTemplate(template);
if (existing != null && !player.hasPermission("Magic.wand.overwrite")) {
String creatorId = existing.getCreatorId();
boolean isCreator = creatorId != null && creatorId.equalsIgnoreCase(player.getUniqueId().toString());
if (!player.hasPermission("Magic.wand.overwrite_own") || !isCreator) {
sender.sendMessage(ChatColor.RED + "The " + template + " wand already exists and you don't have permission to overwrite it.");
return true;
}
}
String inheritTemplate = wand.getTemplateKey();
YamlConfiguration wandConfig = new YamlConfiguration();
ConfigurationSection wandSection = wandConfig.createSection(template);
wand.save(wandSection, true);
wandSection.set("creator_id", player.getUniqueId().toString());
wandSection.set("creator", player.getName());
// inheriting from itself.
if (inheritTemplate != null && inheritTemplate.equals(template)) {
String oldTemplate = null;
if (existing != null) {
// This gives us the collapsed configuration, including inherited values.
// We just want the ones changed by the template we are replacing, though.
ConfigurationSection templateConfig = existing.getConfiguration();
WandTemplate parent = existing.getParent();
if (parent != null) {
oldTemplate = parent.getKey();
ConfigurationSection parentConfig = parent.getConfiguration();
templateConfig = ConfigurationUtils.subtractConfiguration(templateConfig, parentConfig);
}
ConfigurationUtils.addConfigurations(wandSection, templateConfig, false);
}
wandSection.set("inherit", oldTemplate);
}
File wandFolder = new File(controller.getConfigFolder(), "wands");
File wandFile = new File(wandFolder, template + ".yml");
wandFolder.mkdirs();
try {
wandConfig.save(wandFile);
} catch (IOException ex) {
ex.printStackTrace();
sender.sendMessage(ChatColor.RED + "Can't write to file " + wandFile.getName());
return true;
}
String inherit = wandSection.getString("inherit", "");
if (!inherit.isEmpty()) {
WandTemplate inheritConfiguration = controller.getWandTemplate(inherit);
ConfigurationUtils.addConfigurations(wandSection, inheritConfiguration.getConfiguration(), false);
}
controller.loadWandTemplate(template, wandSection);
String message = "Wand saved as " + template;
if (existing != null) {
message = message + ChatColor.GOLD + " (Replaced Existing)";
}
sender.sendMessage(message);
return true;
}
Aggregations