use of com.elmakers.mine.bukkit.api.wand.Wand in project MagicPlugin by elBukkit.
the class DisarmAction method perform.
@Override
public SpellResult perform(CastContext context) {
Entity target = context.getTargetEntity();
if (target == null || !(target instanceof LivingEntity)) {
return SpellResult.NO_TARGET;
}
LivingEntity entity = (LivingEntity) target;
EntityEquipment equipment = entity.getEquipment();
ItemStack stack = null;
Integer originalSlot = null;
boolean isMainHand = false;
if (displayName == null) {
stack = equipment.getItemInMainHand();
isMainHand = true;
} else {
// This is not compatible
keepInInventory = false;
// Must be a player in this case
if (!(entity instanceof Player)) {
return SpellResult.PLAYER_REQUIRED;
}
PlayerInventory playerInventory = ((Player) entity).getInventory();
for (originalSlot = 0; originalSlot < playerInventory.getSize(); originalSlot++) {
ItemStack item = playerInventory.getItem(originalSlot);
if (InventoryUtils.isEmpty(item))
continue;
ItemMeta meta = item.getItemMeta();
if (meta == null || !meta.hasDisplayName())
continue;
if (meta.getDisplayName().equals(displayName)) {
stack = item;
isMainHand = originalSlot == playerInventory.getHeldItemSlot();
break;
}
}
}
if (InventoryUtils.isEmpty(stack)) {
return SpellResult.NO_TARGET;
}
// Special case for wands
MageController controller = context.getController();
Mage targetMage = controller.getMage(entity);
if (com.elmakers.mine.bukkit.wand.Wand.isWand(stack) && controller.isMage(entity)) {
Mage mage = context.getMage();
// This gets overridden by superpower...
if (!mage.isSuperPowered() && targetMage.isSuperProtected()) {
return SpellResult.NO_TARGET;
}
Wand activeWand = targetMage.getActiveWand();
if (activeWand != null && isMainHand) {
targetMage.getActiveWand().deactivate();
stack = equipment.getItemInMainHand();
}
}
Integer targetSlot = null;
PlayerInventory targetInventory = null;
ItemStack swapItem = null;
Random random = context.getRandom();
if (entity instanceof Player && keepInInventory) {
Player targetPlayer = (Player) entity;
targetInventory = targetPlayer.getInventory();
originalSlot = targetInventory.getHeldItemSlot();
List<Integer> validSlots = new ArrayList<>();
ItemStack[] contents = targetInventory.getContents();
for (int i = minSlot; i <= maxSlot; i++) {
if (contents[i] == null || contents[i].getType() == Material.AIR) {
validSlots.add(i);
}
}
// Randomly choose a slot if no empty one was found
if (validSlots.size() == 0) {
int swapSlot = random.nextInt(maxSlot - minSlot) + minSlot;
swapItem = targetInventory.getItem(swapSlot);
validSlots.add(swapSlot);
}
int chosen = random.nextInt(validSlots.size());
targetSlot = validSlots.get(chosen);
}
if (displayName != null) {
((Player) entity).getInventory().setItem(originalSlot, null);
} else {
equipment.setItemInMainHand(swapItem);
}
if (targetSlot != null && targetInventory != null) {
targetInventory.setItem(targetSlot, stack);
if (originalSlot != null) {
DisarmUndoAction disarmUndo = new DisarmUndoAction(targetMage, originalSlot, targetSlot);
context.registerForUndo(disarmUndo);
}
} else {
Location location = entity.getLocation();
location.setY(location.getY() + 1);
Item item = entity.getWorld().dropItemNaturally(location, stack);
Vector velocity = item.getVelocity();
velocity.setY(velocity.getY() * 5);
SafetyUtils.setVelocity(item, velocity);
}
return SpellResult.CAST;
}
use of com.elmakers.mine.bukkit.api.wand.Wand in project MagicPlugin by elBukkit.
the class FillWandAction method perform.
@Override
public SpellResult perform(CastContext context) {
Mage mage = context.getMage();
Wand wand = context.getWand();
Player player = mage.getPlayer();
if (player == null) {
return SpellResult.PLAYER_REQUIRED;
}
if (wand == null) {
context.showMessage("no_wand", "You must be holding a wand!");
return SpellResult.FAIL;
}
wand.fill(player, maxLevel);
return SpellResult.CAST;
}
use of com.elmakers.mine.bukkit.api.wand.Wand in project MagicPlugin by elBukkit.
the class InventoryAction method perform.
@Override
public SpellResult perform(CastContext context) {
MageController controller = context.getController();
Entity targetEntity = context.getTargetEntity();
Mage showMage = context.getMage();
Player showPlayer = showMage.getPlayer();
if (showPlayer == null)
return SpellResult.PLAYER_REQUIRED;
// Make sure to close the player's wand
Wand activeWand = showMage.getActiveWand();
if (activeWand != null) {
activeWand.closeInventory();
}
switch(inventoryType) {
case CRAFTING:
if (targetEntity == null || !(targetEntity instanceof Player)) {
return SpellResult.NO_TARGET;
}
Mage mage = controller.getMage(targetEntity);
Inventory craftingInventory = mage.getInventory();
showPlayer.openInventory(craftingInventory);
break;
case ENDER_CHEST:
if (targetEntity == null || !(targetEntity instanceof HumanEntity)) {
return SpellResult.NO_TARGET;
}
HumanEntity humanTarget = (HumanEntity) targetEntity;
Inventory enderInventory = humanTarget.getEnderChest();
showPlayer.openInventory(enderInventory);
break;
case WORKBENCH:
showPlayer.openWorkbench(null, true);
break;
case CHEST:
if (disposal) {
showGenericInventory(showPlayer);
break;
}
Block targetBlock = context.getTargetBlock();
if (targetBlock == null)
return SpellResult.NO_TARGET;
BlockState state = targetBlock.getState();
if (!(state instanceof InventoryHolder))
return SpellResult.NO_TARGET;
InventoryHolder holder = (InventoryHolder) state;
showPlayer.openInventory(holder.getInventory());
break;
default:
// Probably wont' work very well, but sure why not.
showGenericInventory(showPlayer);
break;
}
return SpellResult.CAST;
}
use of com.elmakers.mine.bukkit.api.wand.Wand in project MagicPlugin by elBukkit.
the class ItemShopAction method parseItemKey.
@Nullable
protected ItemStack parseItemKey(MageController controller, String itemKey) {
if (itemKey == null || itemKey.isEmpty() || itemKey.equalsIgnoreCase("none")) {
return null;
}
String[] pieces = StringUtils.split(itemKey, '@');
int amount = 1;
if (pieces.length > 1) {
itemKey = pieces[0];
try {
amount = Integer.parseInt(pieces[1]);
} catch (Exception ex) {
context.getLogger().warning("Invalid item amount in shop: " + pieces[1] + " for item " + itemKey + " shop " + context.getSpell().getKey());
}
}
ItemStack item = controller.createItem(itemKey, castsSpells);
if (item == null)
return null;
if (showActiveIcons && controller.getAPI().isWand(item)) {
Wand wand = controller.getWand(item);
wand.getIcon().applyToItem(item);
}
item.setAmount(amount);
return item;
}
use of com.elmakers.mine.bukkit.api.wand.Wand in project MagicPlugin by elBukkit.
the class BrushSelectAction method perform.
@Override
public SpellResult perform(CastContext context) {
Mage mage = context.getMage();
MageController controller = context.getController();
Wand wand = context.getWand();
schematics.clear();
variants.clear();
this.context = context;
Player player = mage.getPlayer();
if (player == null) {
return SpellResult.PLAYER_REQUIRED;
}
if (wand == null) {
return SpellResult.FAIL;
}
List<String> brushKeys = new ArrayList<>(wand.getBrushes());
Collections.sort(brushKeys);
List<ItemStack> brushes = new ArrayList<>();
List<ItemStack> specials = new ArrayList<>();
MaterialAndData previous = null;
for (String brushKey : brushKeys) {
ItemStack brushItem = com.elmakers.mine.bukkit.wand.Wand.createBrushItem(brushKey, controller, null, false);
if (MaterialBrush.isSchematic(brushKey)) {
schematics.add(brushItem);
continue;
}
if (MaterialBrush.isSpecialMaterialKey(brushKey)) {
specials.add(brushItem);
continue;
}
if (brushItem != null) {
MaterialAndData material = new MaterialAndData(brushKey);
if (previous != null && material.getMaterial() == previous.getMaterial()) {
List<ItemStack> variantList = variants.get(material.getMaterial());
ItemStack lastAdded = brushes.get(brushes.size() - 1);
if (variantList == null) {
String baseName = material.getBaseName();
variantList = new ArrayList<>();
variantList.add(lastAdded);
brushes.remove(brushes.size() - 1);
ItemStack category = InventoryUtils.getCopy(lastAdded);
ItemMeta meta = category.getItemMeta();
String name = context.getMessage("variant_name", "" + ChatColor.AQUA + "$variant");
meta.setDisplayName(name.replace("$variant", baseName));
List<String> lore = new ArrayList<>();
String description = context.getMessage("variant_description", "Choose a type of $variant");
lore.add(description.replace("$variant", baseName));
meta.setLore(lore);
category.setItemMeta(meta);
InventoryUtils.setMeta(category, "brush_set", "variants");
variants.put(material.getMaterial(), variantList);
brushes.add(category);
}
variantList.add(brushItem);
} else {
brushes.add(brushItem);
}
previous = material;
}
}
ItemStack schematicItem = null;
if (schematics.size() == 1) {
schematicItem = schematics.get(0);
} else if (schematics.size() > 0) {
schematicItem = InventoryUtils.getCopy(schematics.get(0));
ItemMeta meta = schematicItem.getItemMeta();
meta.setDisplayName(context.getMessage("schematics_name", "" + ChatColor.AQUA + "Schematics"));
List<String> lore = new ArrayList<>();
lore.add(context.getMessage("schematics_description", "Choose a schematic"));
meta.setLore(lore);
schematicItem.setItemMeta(meta);
InventoryUtils.setMeta(schematicItem, "brush_set", "schematics");
}
if (schematicItem != null) {
brushes.add(schematicItem);
}
brushes.addAll(specials);
if (brushes.size() == 0) {
return SpellResult.NO_TARGET;
}
int inventorySize = 9 * INVENTORY_ROWS;
int numPages = (int) Math.ceil((float) brushes.size() / inventorySize);
if (page < 1)
page = numPages;
else if (page > numPages)
page = 1;
int pageIndex = page - 1;
int startIndex = pageIndex * inventorySize;
int maxIndex = (pageIndex + 1) * inventorySize - 1;
List<ItemStack> showBrushes = new ArrayList<>();
for (int i = startIndex; i <= maxIndex && i < brushes.size(); i++) {
showBrushes.add(brushes.get(i));
}
String inventoryTitle = context.getMessage("title", "Brushes");
if (numPages > 1) {
inventoryTitle += " (" + page + "/" + numPages + ")";
}
int invSize = (int) Math.ceil(showBrushes.size() / 9.0f) * 9;
Inventory displayInventory = CompatibilityUtils.createInventory(null, invSize, inventoryTitle);
for (ItemStack brush : showBrushes) {
displayInventory.addItem(brush);
}
mage.activateGUI(this, displayInventory);
return SpellResult.CAST;
}
Aggregations