use of com.elmakers.mine.bukkit.api.block.MaterialAndData in project MagicPlugin by elBukkit.
the class Mage method updateHotbarStatus.
public void updateHotbarStatus() {
Player player = getPlayer();
if (player != null) {
Location location = getLocation();
for (int i = 0; i < Wand.HOTBAR_SIZE; i++) {
ItemStack spellItem = player.getInventory().getItem(i);
String spellKey = Wand.getSpell(spellItem);
String classKey = Wand.getSpellClass(spellItem);
if (spellKey != null) {
Spell spell = getSpell(spellKey);
if (spell != null) {
int targetAmount = 1;
long remainingCooldown = 0;
CastingCost requiredCost = null;
boolean canCastSpell = false;
if (classKey != null && !classKey.isEmpty()) {
MageClass mageClass = getClass(classKey);
if (mageClass != null && spell instanceof BaseSpell) {
BaseSpell baseSpell = (BaseSpell) spell;
baseSpell.setMageClass(mageClass);
remainingCooldown = spell.getRemainingCooldown();
requiredCost = spell.getRequiredCost();
canCastSpell = spell.canCast(location);
baseSpell.setMageClass(null);
}
} else {
remainingCooldown = spell.getRemainingCooldown();
requiredCost = spell.getRequiredCost();
canCastSpell = spell.canCast(location);
}
boolean canCast = canCastSpell;
if (canCastSpell && remainingCooldown == 0 && requiredCost == null) {
targetAmount = 1;
} else if (canCastSpell) {
canCast = remainingCooldown == 0;
targetAmount = Wand.LiveHotbarCooldown ? (int) Math.min(Math.ceil((double) remainingCooldown / 1000), 99) : 99;
if (Wand.LiveHotbarCooldown && requiredCost != null) {
int mana = requiredCost.getMana();
if (mana > 0) {
if (mana <= getEffectiveManaMax() && getEffectiveManaRegeneration() > 0) {
float remainingMana = mana - getMana();
canCast = canCast && remainingMana <= 0;
int targetManaTime = (int) Math.min(Math.ceil(remainingMana / getEffectiveManaRegeneration()), 99);
targetAmount = Math.max(targetManaTime, targetAmount);
} else {
canCastSpell = false;
canCast = false;
}
}
}
}
if (targetAmount == 0)
targetAmount = 1;
boolean setAmount = false;
MaterialAndData disabledIcon = spell.getDisabledIcon();
MaterialAndData spellIcon = spell.getIcon();
String urlIcon = spell.getIconURL();
String disabledUrlIcon = spell.getDisabledIconURL();
boolean usingURLIcon = (controller.isUrlIconsEnabled() || spellIcon == null || spellIcon.getMaterial() == Material.AIR) && urlIcon != null && !urlIcon.isEmpty();
if (disabledIcon != null && spellIcon != null && !usingURLIcon) {
if (!canCast) {
if (disabledIcon.isValid() && (disabledIcon.getMaterial() != spellItem.getType() || disabledIcon.getData() != spellItem.getDurability())) {
disabledIcon.applyToItem(spellItem);
}
if (!canCastSpell) {
if (spellItem.getAmount() != 1) {
spellItem.setAmount(1);
}
setAmount = true;
}
} else {
if (spellIcon.isValid() && (spellIcon.getMaterial() != spellItem.getType() || spellIcon.getData() != spellItem.getDurability())) {
spellIcon.applyToItem(spellItem);
}
}
} else if (usingURLIcon && disabledUrlIcon != null && !disabledUrlIcon.isEmpty() && spellItem.getType() == Material.SKULL_ITEM) {
String currentURL = InventoryUtils.getSkullURL(spellItem);
if (!canCast) {
if (!disabledUrlIcon.equals(currentURL)) {
InventoryUtils.setNewSkullURL(spellItem, disabledUrlIcon);
player.getInventory().setItem(i, spellItem);
}
if (!canCastSpell) {
if (spellItem.getAmount() != 1) {
spellItem.setAmount(1);
}
setAmount = true;
}
} else {
if (!urlIcon.equals(currentURL)) {
InventoryUtils.setNewSkullURL(spellItem, urlIcon);
player.getInventory().setItem(i, spellItem);
}
}
}
if (!setAmount && spellItem.getAmount() != targetAmount) {
spellItem.setAmount(targetAmount);
}
}
}
}
}
}
use of com.elmakers.mine.bukkit.api.block.MaterialAndData in project MagicPlugin by elBukkit.
the class SkillSelectorAction method openInventory.
protected void openInventory() {
MageController apiController = context.getController();
if (!(apiController instanceof MagicController))
return;
MagicController controller = (MagicController) apiController;
HeroesManager heroes = controller.getHeroes();
int inventorySize = 9 * controller.getSkillInventoryRows();
float numSlots = extraSlots + allSkills.size();
int numPages = (int) Math.ceil(numSlots / inventorySize);
if (page < 1)
page = numPages;
else if (page > numPages)
page = 1;
Mage mage = context.getMage();
Player player = mage.getPlayer();
int pageIndex = page - 1;
int startIndex = pageIndex * inventorySize;
int maxIndex = (pageIndex + 1) * inventorySize - 1;
List<SkillDescription> skills = new ArrayList<>();
for (int i = startIndex; i <= maxIndex && i < allSkills.size(); i++) {
skills.add(allSkills.get(i));
}
if (skills.size() == 0) {
String messageTemplate = controller.getMessages().get("skills.none_on_page", "$page");
player.sendMessage(messageTemplate.replace("$page", Integer.toString(page)));
return;
}
int invSize = (int) Math.ceil(numSlots / 9.0f) * 9;
String title = inventoryTitle;
title = title.replace("$pages", Integer.toString(numPages)).replace("$page", Integer.toString(page));
Inventory displayInventory = CompatibilityUtils.createInventory(null, invSize, title);
for (SkillDescription skill : skills) {
ItemStack skillItem = controller.getAPI().createItem(skill.getSkillKey(), mage);
if (skillItem == null)
continue;
InventoryUtils.configureSkillItem(skillItem, classKey, skillsConfig);
if (skill.isHeroes() && heroes != null && !heroes.canUseSkill(player, skill.heroesSkill)) {
String nameTemplate = controller.getMessages().get("skills.item_name_unavailable", "$skill");
String spellName = skill.spell != null ? skill.spell.getName() : skill.heroesSkill;
CompatibilityUtils.setDisplayName(skillItem, nameTemplate.replace("$skill", spellName));
InventoryUtils.setMetaBoolean(skillItem, "unavailable", true);
if (skill.spell != null) {
MaterialAndData disabledIcon = skill.spell.getDisabledIcon();
if (disabledIcon != null) {
disabledIcon.applyToItem(skillItem);
} else {
String disabledIconURL = skill.spell.getDisabledIconURL();
if (disabledIconURL != null && !disabledIconURL.isEmpty()) {
InventoryUtils.setNewSkullURL(skillItem, disabledIconURL);
}
}
}
}
displayInventory.addItem(skillItem);
}
mage.deactivateGUI();
mage.activateGUI(this, displayInventory);
}
Aggregations