use of org.bukkit.inventory.meta.ItemMeta in project MagicPlugin by elBukkit.
the class WearAction 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;
MaterialAndData material = this.material;
MageController controller = context.getController();
Mage mage = controller.getMage(player);
if (useItem) {
Wand activeWand = mage.getActiveWand();
// Not handling this for now.
if (activeWand != context.getWand()) {
return SpellResult.NO_TARGET;
}
if (activeWand != null) {
activeWand.deactivate();
}
ItemStack itemInHand = player.getInventory().getItemInMainHand();
if (itemInHand == null || itemInHand.getType() == Material.AIR) {
return SpellResult.FAIL;
}
ItemStack[] armor = player.getInventory().getArmorContents();
ItemStack currentItem = armor[slotNumber];
armor[slotNumber] = itemInHand;
player.getInventory().setArmorContents(armor);
if (!InventoryUtils.isTemporary(currentItem)) {
player.getInventory().setItemInMainHand(currentItem);
} else {
player.getInventory().setItemInMainHand(new ItemStack(Material.AIR));
}
if (mage instanceof com.elmakers.mine.bukkit.magic.Mage) {
((com.elmakers.mine.bukkit.magic.Mage) mage).armorUpdated();
}
return SpellResult.CAST;
}
ItemStack wearItem = null;
String materialName = null;
if (item == null) {
if (material == null && (context.getSpell().usesBrush() || context.getSpell().hasBrushOverride())) {
material = context.getBrush();
}
if (material == null) {
Block targetBlock = context.getTargetBlock();
if (targetBlock != null) {
material = new com.elmakers.mine.bukkit.block.MaterialAndData(targetBlock);
// Check for Banners with 1.7 support
// TODO: this is outdated?
Material baseMaterial = material.getMaterial();
if (DeprecatedUtils.getId(baseMaterial) == 176 || DeprecatedUtils.getId(baseMaterial) == 177) {
((com.elmakers.mine.bukkit.block.MaterialAndData) material).setMaterialId(425);
}
}
}
if (material == null || material.getMaterial() == Material.AIR) {
return SpellResult.NO_TARGET;
}
wearItem = material.getItemStack(1);
materialName = material.getName();
} else {
wearItem = InventoryUtils.getCopy(item);
materialName = context.getController().describeItem(wearItem);
}
ItemMeta meta = wearItem.getItemMeta();
// Legacy support
String displayName = context.getMessage("hat_name", "");
displayName = context.getMessage("wear_name", displayName);
if (materialName == null || materialName.isEmpty()) {
materialName = "?";
}
if (displayName != null && !displayName.isEmpty()) {
meta.setDisplayName(displayName.replace("$hat", materialName).replace("$item", materialName));
}
List<String> lore = new ArrayList<>();
String loreLine = context.getMessage("hat_lore");
loreLine = context.getMessage("wear_lore", loreLine);
lore.add(loreLine);
meta.setLore(lore);
wearItem.setItemMeta(meta);
wearItem = InventoryUtils.makeReal(wearItem);
NMSUtils.makeTemporary(wearItem, context.getMessage("removed").replace("$hat", materialName).replace("$item", materialName));
if (enchantments != null) {
wearItem.addUnsafeEnchantments(enchantments);
}
if (unbreakable) {
CompatibilityUtils.makeUnbreakable(wearItem);
}
ItemStack[] armor = player.getInventory().getArmorContents();
ItemStack itemStack = armor[slotNumber];
if (itemStack != null && itemStack.getType() != Material.AIR) {
if (NMSUtils.isTemporary(itemStack)) {
ItemStack replacement = NMSUtils.getReplacement(itemStack);
if (replacement != null) {
itemStack = replacement;
}
}
NMSUtils.setReplacement(wearItem, itemStack);
}
armor[slotNumber] = wearItem;
player.getInventory().setArmorContents(armor);
// Sanity check to make sure the block was allowed to be created
armor = player.getInventory().getArmorContents();
ItemStack helmetItem = armor[slotNumber];
if (!NMSUtils.isTemporary(helmetItem)) {
armor[slotNumber] = itemStack;
player.getInventory().setArmorContents(armor);
return SpellResult.NO_TARGET;
}
targetMage = mage;
context.registerForUndo(new WearUndoAction());
if (mage instanceof com.elmakers.mine.bukkit.magic.Mage) {
((com.elmakers.mine.bukkit.magic.Mage) mage).armorUpdated();
}
return SpellResult.CAST;
}
use of org.bukkit.inventory.meta.ItemMeta in project MagicPlugin by elBukkit.
the class ShrinkBlockAction method dropHead.
@SuppressWarnings("deprecation")
protected void dropHead(Location location, String ownerName, String itemName, byte data) {
ItemStack shrunkenHead = new ItemStack(Material.SKULL_ITEM, 1, (short) 0, data);
ItemMeta meta = shrunkenHead.getItemMeta();
if (itemName != null) {
meta.setDisplayName(itemName);
}
if (meta instanceof SkullMeta && ownerName != null) {
SkullMeta skullData = (SkullMeta) meta;
skullData.setOwner(ownerName);
}
shrunkenHead.setItemMeta(meta);
location.getWorld().dropItemNaturally(location, shrunkenHead);
}
use of org.bukkit.inventory.meta.ItemMeta in project MagicPlugin by elBukkit.
the class ShrinkEntityAction method dropHead.
@SuppressWarnings("deprecation")
protected void dropHead(Location location, String ownerName, String itemName, byte data) {
ItemStack shrunkenHead = new ItemStack(Material.SKULL_ITEM, 1, (short) 0, data);
ItemMeta meta = shrunkenHead.getItemMeta();
if (itemName != null) {
meta.setDisplayName(itemName);
}
if (meta instanceof SkullMeta && ownerName != null) {
SkullMeta skullData = (SkullMeta) meta;
skullData.setOwner(ownerName);
}
shrunkenHead.setItemMeta(meta);
location.getWorld().dropItemNaturally(location, shrunkenHead);
}
use of org.bukkit.inventory.meta.ItemMeta in project MagicPlugin by elBukkit.
the class Messages method describeItem.
@Override
public String describeItem(ItemStack item) {
String displayName = null;
if (item.hasItemMeta()) {
ItemMeta meta = item.getItemMeta();
displayName = meta.getDisplayName();
if ((displayName == null || displayName.isEmpty()) && meta instanceof BookMeta) {
BookMeta book = (BookMeta) meta;
displayName = book.getTitle();
}
}
if (displayName == null || displayName.isEmpty()) {
MaterialAndData material = new MaterialAndData(item);
displayName = material.getName();
}
return displayName;
}
use of org.bukkit.inventory.meta.ItemMeta in project MagicPlugin by elBukkit.
the class NMSUtils method removeGlow.
public static void removeGlow(ItemStack stack) {
if (NMSUtils.isEmpty(stack))
return;
try {
ItemMeta meta = stack.getItemMeta();
if (meta.hasEnchant(Enchantment.LUCK)) {
meta.removeEnchant(Enchantment.LUCK);
stack.setItemMeta(meta);
}
} catch (Throwable ex) {
ex.printStackTrace();
}
}
Aggregations