use of com.elmakers.mine.bukkit.api.magic.MageController in project MagicPlugin by elBukkit.
the class CondHasItem method check.
@Override
public boolean check(final Event e) {
return entities.check(e, new Checker<Entity>() {
@Override
public boolean check(final Entity entity) {
final MageController controller = MagicPlugin.getAPI().getController();
final Mage mage = controller.getRegisteredMage(entity);
if (mage == null) {
return false;
}
final Wand wand = offhand ? mage.getOffhandWand() : mage.getActiveWand();
if (itemKeys == null) {
return (wand != null) != isNegated();
}
final LivingEntity living = mage.getLivingEntity();
final ItemStack item = wand == null && living != null ? (offhand ? living.getEquipment().getItemInOffHand() : living.getEquipment().getItemInMainHand()) : null;
return itemKeys.check(e, new Checker<String>() {
@Override
public boolean check(final String targetKey) {
if (armor) {
if (living == null)
return false;
for (ItemStack armorItem : living.getEquipment().getArmorContents()) {
if (armorItem == null)
continue;
String key = controller.getWandKey(armorItem);
if (key != null && key.equalsIgnoreCase(targetKey))
return true;
key = controller.getItemKey(armorItem);
if (key != null && key.equalsIgnoreCase(targetKey))
return true;
}
return false;
}
if (wand != null && wand.getTemplateKey().equalsIgnoreCase(targetKey)) {
return true;
}
if (item != null) {
String itemKey = controller.getItemKey(item);
if (itemKey != null && itemKey.equalsIgnoreCase(targetKey)) {
return true;
}
}
return false;
}
}, isNegated());
}
});
}
use of com.elmakers.mine.bukkit.api.magic.MageController in project MagicPlugin by elBukkit.
the class WandOrganizer method organize.
public void organize() {
Map<String, Integer> spells = wand.getSpellInventory();
Map<String, Integer> brushes = wand.getBrushInventory();
removeHotbar(spells, brushes);
// Collect favorite spells
MageController controller = wand.getController();
TreeMap<Long, List<String>> favoriteSpells = new TreeMap<>();
Map<String, Collection<String>> groupedSpells = new TreeMap<>();
for (String spellName : spells.keySet()) {
Spell mageSpell = mage == null ? null : mage.getSpell(spellName);
SpellTemplate spell = mageSpell == null ? controller.getSpellTemplate(spellName) : mageSpell;
if (spell != null) {
// Sum up all levels of this spell:
long castCount = 0;
int spellLevel = 1;
while (mageSpell != null) {
castCount += mageSpell.getCastCount();
spellLevel++;
SpellKey spellKey = new SpellKey(spellName, spellLevel);
String key = spellKey.getKey();
mageSpell = mage.hasSpell(key) ? mage.getSpell(key) : null;
}
spellName = spell.getSpellKey().getBaseKey();
if (castCount > favoriteCastCountThreshold) {
List<String> favorites = null;
if (!favoriteSpells.containsKey(castCount)) {
favorites = new ArrayList<>();
favoriteSpells.put(castCount, favorites);
} else {
favorites = favoriteSpells.get(castCount);
}
favorites.add(spellName);
}
SpellCategory spellCategory = spell.getCategory();
String category = spellCategory == null ? null : spellCategory.getKey();
if (category == null || category.length() == 0) {
category = "default";
}
Collection<String> spellList = groupedSpells.get(category);
if (spellList == null) {
spellList = new TreeSet<>();
groupedSpells.put(category, spellList);
}
spellList.add(spellName);
}
}
Map<String, String> materials = new TreeMap<>();
if (wand.getBrushMode() == WandMode.INVENTORY) {
for (String materialKey : brushes.keySet()) {
if (MaterialBrush.isSpecialMaterialKey(materialKey)) {
materials.put(" " + materialKey, materialKey);
} else {
materials.put(materialKey, materialKey);
}
}
}
currentInventoryIndex = 0;
currentInventoryCount = 0;
// Organize favorites
WandMode mode = wand.getMode();
Set<String> addedFavorites = new HashSet<>();
List<String> favoriteList = new ArrayList<>();
int favoritePageSize = wand.getInventorySize() - favoritePageBuffer;
for (List<String> favorites : favoriteSpells.descendingMap().values()) {
if (addedFavorites.size() >= favoritePageSize)
break;
for (String spellName : favorites) {
addedFavorites.add(spellName);
favoriteList.add(spellName);
if (addedFavorites.size() >= favoritePageSize)
break;
}
}
if (addedFavorites.size() > 0) {
for (String favorite : favoriteList) {
int slot = getNextSlot();
spells.put(favorite, slot);
}
if (mode != WandMode.CHEST && addedFavorites.size() > wand.getInventorySize() - favoriteCountBuffer) {
nextPage();
}
} else {
addedFavorites.clear();
}
// Add unused spells by category
int inventoryOrganizeNewGroupSize = wand.getInventorySize() - inventoryOrganizeNewGroupBuffer;
for (Collection<String> spellGroup : groupedSpells.values()) {
// Start a new inventory for a new group if the previous inventory is over 2/3 full
if (mode != WandMode.CHEST && currentInventoryCount > inventoryOrganizeNewGroupSize) {
nextPage();
}
for (String spellName : spellGroup) {
if (!addedFavorites.contains(spellName)) {
int slot = getNextSlot();
spells.put(spellName, slot);
}
}
}
if (materials.size() > 0) {
nextPage();
for (String materialName : materials.values()) {
brushes.put(materialName, getNextSlot());
}
}
wand.updateSpellInventory(spells);
if (materials.size() > 0) {
wand.updateBrushInventory(brushes);
}
}
use of com.elmakers.mine.bukkit.api.magic.MageController in project MagicPlugin by elBukkit.
the class SelectorAction method clicked.
@Override
public void clicked(InventoryClickEvent event) {
event.setCancelled(true);
ItemStack item = event.getCurrentItem();
Mage mage = context.getMage();
if (item == null || !InventoryUtils.hasMeta(item, "slot")) {
if (!autoClose) {
mage.deactivateGUI();
}
return;
}
int slotIndex = Integer.parseInt(InventoryUtils.getMetaString(item, "slot"));
MageController controller = context.getController();
SelectorOption option = showingItems.get(slotIndex);
if (option == null || option.isPlaceholder()) {
return;
}
String unpurchasableMessage = InventoryUtils.getMetaString(item, "unpurchasable");
if (unpurchasableMessage != null && !unpurchasableMessage.isEmpty()) {
context.showMessage(unpurchasableMessage);
mage.deactivateGUI();
return;
}
Cost required = option.getRequiredCost(this, context);
if (required != null) {
String baseMessage = getMessage("insufficient");
String costDescription = required.getFullDescription(controller.getMessages(), this);
costDescription = baseMessage.replace("$cost", costDescription);
context.showMessage(costDescription);
} else {
String itemName = option.getName();
if (InventoryUtils.hasMeta(item, "confirm")) {
String inventoryTitle = getConfirmTitle(option).replace("$item", itemName);
Inventory confirmInventory = CompatibilityUtils.createInventory(null, 9, inventoryTitle);
InventoryUtils.removeMeta(item, "confirm");
for (int i = 0; i < 9; i++) {
if (i != 4) {
ItemStack filler = confirmFillMaterial.getItemStack(1);
ItemMeta meta = filler.getItemMeta();
if (meta != null) {
meta.setDisplayName(ChatColor.DARK_GRAY + (i < 4 ? "-->" : "<--"));
filler.setItemMeta(meta);
}
confirmInventory.setItem(i, filler);
} else {
confirmInventory.setItem(i, item);
}
}
mage.deactivateGUI();
isActive = true;
mage.activateGUI(this, confirmInventory);
return;
}
finalResult = option.give(this, context);
if (finalResult.isSuccess() && finalResult != SpellResult.NO_TARGET) {
context.showMessage(option.getSelectedMessage(this));
}
}
if (autoClose || finalResult != SpellResult.CAST) {
if (isActive) {
mage.deactivateGUI();
}
} else {
// update title
mage.continueGUI(this, getInventory(context));
}
}
use of com.elmakers.mine.bukkit.api.magic.MageController in project MagicPlugin by elBukkit.
the class ShrinkBlockAction method perform.
@Override
public SpellResult perform(CastContext context) {
Block targetBlock = context.getTargetBlock();
if (targetBlock == null) {
return SpellResult.NO_TARGET;
}
MageController controller = context.getController();
String blockSkin = controller.getBlockSkin(targetBlock.getType());
if (blockSkin == null)
return SpellResult.NO_TARGET;
if (!context.hasBreakPermission(targetBlock)) {
return SpellResult.INSUFFICIENT_PERMISSION;
}
if (!context.isDestructible(targetBlock)) {
return SpellResult.NO_TARGET;
}
context.registerForUndo(targetBlock);
dropHead(targetBlock.getLocation(), blockSkin, targetBlock.getType().name(), (byte) 3);
targetBlock.setType(Material.AIR);
return SpellResult.CAST;
}
use of com.elmakers.mine.bukkit.api.magic.MageController in project MagicPlugin by elBukkit.
the class ShrinkEntityAction method perform.
@Override
public SpellResult perform(CastContext context) {
Entity targetEntity = context.getTargetEntity();
MageController controller = context.getController();
if (controller.isElemental(targetEntity)) {
double elementalSize = controller.getElementalScale(targetEntity);
if (elementalSize < 0.1) {
return super.perform(context);
}
elementalSize /= 2;
controller.setElementalScale(targetEntity, elementalSize);
return SpellResult.CAST;
}
if (!(targetEntity instanceof LivingEntity))
return SpellResult.NO_TARGET;
LivingEntity li = (LivingEntity) targetEntity;
boolean alreadyDead = li.isDead() || li.getHealth() <= 0;
String ownerName = null;
String itemName = null;
byte data = 3;
if (li instanceof Player) {
ownerName = ((Player) li).getName();
} else {
itemName = DeprecatedUtils.getName(li.getType()) + " Head";
switch(li.getType()) {
case CREEPER:
data = 4;
break;
case ZOMBIE:
data = 2;
break;
case SKELETON:
Skeleton skeleton = (Skeleton) li;
data = (byte) (skeleton.getSkeletonType() == SkeletonType.NORMAL ? 0 : 1);
break;
default:
ownerName = controller.getMobSkin(li.getType());
}
}
if (itemName == null && ownerName != null) {
itemName = ownerName + "'s Head";
}
Location targetLocation = targetEntity.getLocation();
if (li instanceof Player) {
super.perform(context);
if (li.isDead() && !alreadyDead) {
dropPlayerHead(targetEntity.getLocation(), (Player) li, itemName);
}
} else if (li.getType() == EntityType.GIANT) {
UndoList spawnedList = com.elmakers.mine.bukkit.block.UndoList.getUndoList(li);
context.registerModified(li);
li.remove();
Entity zombie = targetLocation.getWorld().spawnEntity(targetLocation, EntityType.ZOMBIE);
if (zombie instanceof Zombie) {
((Zombie) zombie).setBaby(false);
}
context.registerForUndo(zombie);
if (spawnedList != null) {
spawnedList.add(zombie);
}
} else if (li instanceof Ageable && ((Ageable) li).isAdult() && !(li instanceof Player)) {
context.registerModified(li);
((Ageable) li).setBaby();
} else if (li instanceof Zombie && !((Zombie) li).isBaby()) {
context.registerModified(li);
((Zombie) li).setBaby(true);
} else if (li instanceof PigZombie && !((PigZombie) li).isBaby()) {
context.registerModified(li);
((PigZombie) li).setBaby(true);
} else if (li instanceof Slime && ((Slime) li).getSize() > 1) {
context.registerModified(li);
Slime slime = (Slime) li;
slime.setSize(slime.getSize() - 1);
} else if (li instanceof Skeleton && skeletons && ((Skeleton) li).getSkeletonType() == SkeletonType.WITHER) {
context.registerModified(li);
Skeleton skeleton = (Skeleton) li;
skeleton.setSkeletonType(SkeletonType.NORMAL);
} else {
super.perform(context);
if ((ownerName != null || data != 3) && (li.isDead() || li.getHealth() == 0) && !alreadyDead) {
dropHead(targetEntity.getLocation(), ownerName, itemName, data);
}
}
return SpellResult.CAST;
}
Aggregations