use of com.elmakers.mine.bukkit.api.spell.SpellCategory in project MagicPlugin by elBukkit.
the class SpellsCommandExecutor method listCategories.
public void listCategories(Player player) {
HashMap<String, Integer> spellCounts = new HashMap<>();
List<SpellCategory> spellGroups = new ArrayList<>();
Collection<SpellTemplate> spellVariants = api.getSpellTemplates(player == null || player.hasPermission("Magic.bypass_hidden"));
for (SpellTemplate spell : spellVariants) {
if (player != null && !spell.hasCastPermission(player))
continue;
if (spell.getCategory() == null)
continue;
Integer spellCount = spellCounts.get(spell.getCategory().getKey());
if (spellCount == null || spellCount == 0) {
spellCounts.put(spell.getCategory().getKey(), 1);
spellGroups.add(spell.getCategory());
} else {
spellCounts.put(spell.getCategory().getKey(), spellCount + 1);
}
}
if (spellGroups.size() == 0) {
player.sendMessage(api.getMessages().get("general.no_spells"));
return;
}
Collections.sort(spellGroups);
for (SpellCategory group : spellGroups) {
player.sendMessage(group.getName() + " [" + spellCounts.get(group.getKey()) + "]");
}
}
use of com.elmakers.mine.bukkit.api.spell.SpellCategory in project MagicPlugin by elBukkit.
the class SpellsCommandExecutor method listSpells.
public void listSpells(CommandSender sender, int pageNumber, String category) {
if (category != null) {
listSpellsByCategory(sender, category);
return;
}
Player player = sender instanceof Player ? (Player) sender : null;
Collection<SpellTemplate> spellVariants = api.getSpellTemplates(sender.hasPermission("Magic.bypass_hidden"));
int spellCount = 0;
for (SpellTemplate spell : spellVariants) {
if (player != null && !spell.hasCastPermission(player)) {
continue;
}
if (spell.getCategory() == null)
continue;
spellCount++;
}
// Kinda hacky internals-reaching
Collection<SpellCategory> allCategories = api.getController().getCategories();
List<SpellCategory> sortedGroups = new ArrayList<>(allCategories);
Collections.sort(sortedGroups);
int maxLines = -1;
if (pageNumber >= 0) {
maxLines = 5;
int maxPages = spellCount / maxLines + 1;
if (pageNumber > maxPages) {
pageNumber = maxPages;
}
String message = api.getMessages().get("general.spell_list_page");
message = message.replace("$count", Integer.toString(spellCount));
message = message.replace("$pages", Integer.toString(maxPages));
message = message.replace("$page", Integer.toString(pageNumber));
sender.sendMessage(message);
} else {
String message = api.getMessages().get("general.spell_list");
message = message.replace("$count", Integer.toString(spellCount));
sender.sendMessage(message);
}
int currentPage = 1;
int lineCount = 0;
int printedCount = 0;
for (SpellCategory group : sortedGroups) {
if (printedCount > maxLines && maxLines > 0)
break;
boolean isFirst = true;
Collection<SpellTemplate> spells = group.getSpells();
for (SpellTemplate spell : spells) {
if (printedCount > maxLines && maxLines > 0)
break;
if (!spell.hasCastPermission(sender))
continue;
if (currentPage == pageNumber || maxLines < 0) {
if (isFirst) {
sender.sendMessage(group.getName() + ":");
isFirst = false;
}
String name = spell.getName();
String description = spell.getDescription();
if (!name.equals(spell.getKey())) {
description = name + " : " + description;
}
MaterialAndData spellIcon = spell.getIcon();
Material material = spellIcon == null ? null : spellIcon.getMaterial();
String icon = material == null ? "None" : material.name().toLowerCase();
sender.sendMessage(ChatColor.AQUA + spell.getKey() + ChatColor.BLUE + " [" + icon + "] : " + ChatColor.YELLOW + description);
printedCount++;
}
lineCount++;
if (lineCount == maxLines) {
lineCount = 0;
currentPage++;
}
}
}
}
use of com.elmakers.mine.bukkit.api.spell.SpellCategory in project MagicPlugin by elBukkit.
the class MagicItemDb method get.
@Nullable
@Override
public ItemStack get(final String id) throws Exception {
if (id.startsWith("m:")) {
String itemId = id.replace("m:", "");
return controller.createItem(itemId);
}
if (id.startsWith("magic:")) {
String itemId = id.replace("magic:", "");
return controller.createItem(itemId);
} else if (id.equals("wand")) {
Wand wand = Wand.createWand(controller, "");
if (wand != null) {
return wand.getItem();
}
} else if (id.startsWith("wand:")) {
String wandId = id.replace("wand:", "");
Wand wand = Wand.createWand(controller, wandId.trim());
if (wand != null) {
return wand.getItem();
}
} else if (id.startsWith("w:")) {
String wandId = id.replace("w:", "");
Wand wand = Wand.createWand(controller, wandId.trim());
if (wand != null) {
return wand.getItem();
}
} else if (id.startsWith("book:")) {
String bookCategory = id.replace("book:", "");
SpellCategory category = null;
if (bookCategory.length() > 0 && !bookCategory.equalsIgnoreCase("all")) {
category = controller.getCategory(bookCategory);
}
ItemStack bookItem = controller.getSpellBook(category, 1);
if (bookItem != null) {
return bookItem;
}
} else if (id.startsWith("spell:")) {
String spellKey = id.replace("spell:", "");
ItemStack itemStack = Wand.createSpellItem(spellKey, controller, null, true);
if (itemStack != null) {
return itemStack;
}
} else if (id.startsWith("s:")) {
String spellKey = id.replace("s:", "");
ItemStack itemStack = Wand.createSpellItem(spellKey, controller, null, true);
if (itemStack != null) {
return itemStack;
}
} else if (id.startsWith("brush:")) {
String brushKey = id.replace("brush:", "");
ItemStack itemStack = Wand.createBrushItem(brushKey, controller, null, true);
if (itemStack != null) {
return itemStack;
}
} else if (id.startsWith("upgrade:")) {
String wandId = id.replace("upgrade:", "");
Wand wand = Wand.createWand(controller, wandId.trim());
if (wand != null) {
wand.makeUpgrade();
return wand.getItem();
}
} else if (id.startsWith("item:")) {
String wandId = id.replace("item:", "");
return controller.createItem(wandId);
}
return super.get(id);
}
use of com.elmakers.mine.bukkit.api.spell.SpellCategory 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.spell.SpellCategory in project MagicPlugin by elBukkit.
the class BaseSpell method finish.
@Override
public void finish(com.elmakers.mine.bukkit.api.action.CastContext context) {
SpellResult result = context.getResult();
// Notify other plugins of this spell cast
CastEvent castEvent = new CastEvent(mage, this, result);
Bukkit.getPluginManager().callEvent(castEvent);
// Message targets
if (result.isSuccess() && (loud || (!mage.isQuiet() && !quiet))) {
messageTargets("cast_player_message");
}
// Clear cooldown on miss
if (result.shouldRefundCooldown(castOnNoTarget)) {
clearCooldown();
}
if (cancelEffects) {
context.cancelEffects();
}
// Track cast counts
if (result.isSuccess() && !passive) {
spellData.addCast();
if (template != null && template.spellData != null) {
template.spellData.addCast();
SpellCategory category = template.getCategory();
if (category != null) {
category.addCast();
}
}
// Reward SP
Wand wand = context.getWand();
Wand activeWand = mage.getActiveWand();
if (activeWand != null && wand != null && activeWand.getItem() != null && wand.getItem() != null && !InventoryUtils.isSameInstance(wand.getItem(), activeWand.getItem()) && activeWand.getItem().equals(wand.getItem())) {
wand = activeWand;
}
Wand offhandWand = mage.getOffhandWand();
if (offhandWand != null && wand != null && offhandWand.getItem() != null && wand.getItem() != null && !InventoryUtils.isSameInstance(wand.getItem(), offhandWand.getItem()) && offhandWand.getItem().equals(wand.getItem())) {
wand = offhandWand;
}
WandUpgradePath path = wand == null ? null : wand.getPath();
if (earns > 0 && wand != null && path != null && path.earnsSP() && controller.isSPEnabled() && controller.isSPEarnEnabled() && !mage.isAtMaxSkillPoints()) {
long now = System.currentTimeMillis();
int scaledEarn = earns;
if (spellData.getLastEarn() > 0 && earnCooldown > 0 && now < spellData.getLastEarn() + earnCooldown) {
scaledEarn = (int) Math.floor((double) earns * (now - spellData.getLastEarn()) / earnCooldown);
if (scaledEarn > 0) {
context.playEffects("earn_scaled_sp");
}
} else {
context.playEffects("earn_sp");
}
if (scaledEarn > 0) {
mage.addSkillPoints((int) Math.floor(mage.getSPMultiplier() * scaledEarn));
spellData.setLastEarn(now);
}
}
// This currently only works on wands.
if (wand != null && wand.upgradesAllowed() && wand.getSpellLevel(spellKey.getBaseKey()) == spellKey.getLevel()) {
if (controller.isSpellUpgradingEnabled()) {
SpellTemplate upgrade = getUpgrade();
long requiredCasts = getRequiredUpgradeCasts();
String upgradePath = getRequiredUpgradePath();
WandUpgradePath currentPath = wand.getPath();
Set<String> upgradeTags = getRequiredUpgradeTags();
if ((upgrade != null && requiredCasts > 0 && getCastCount() >= requiredCasts) && (upgradePath == null || upgradePath.isEmpty() || (currentPath != null && currentPath.hasPath(upgradePath))) && (upgradeTags == null || upgradeTags.isEmpty() || (currentPath != null && currentPath.hasAllTags(upgradeTags)))) {
if (PrerequisiteSpell.hasPrerequisites(wand, upgrade)) {
MageSpell newSpell = mage.getSpell(upgrade.getKey());
if (isActive()) {
deactivate(true, true);
if (newSpell != null) {
newSpell.activate();
}
}
wand.forceAddSpell(upgrade.getKey());
playEffects("upgrade");
if (controller.isPathUpgradingEnabled()) {
wand.checkAndUpgrade(true);
}
// return so progress upgrade doesn't also happen
return;
}
}
}
if (maxLevels > 0 && controller.isSpellProgressionEnabled()) {
long previousLevel = getPreviousCastProgressLevel();
long currentLevel = getProgressLevel();
if (currentLevel != previousLevel) {
wand.addSpell(getKey());
if (currentLevel > previousLevel) {
Messages messages = controller.getMessages();
String progressDescription = getProgressDescription();
playEffects("progress");
if (progressDescription != null && !progressDescription.isEmpty()) {
mage.sendMessage(messages.get("wand.spell_progression").replace("$name", getName()).replace("$wand", getName()).replace("$level", Long.toString(getProgressLevel())).replace("$max_level", Long.toString(maxLevels)));
}
}
if (controller.isPathUpgradingEnabled()) {
wand.checkAndUpgrade(true);
}
}
}
}
}
}
Aggregations