Java Code Examples for org.bukkit.inventory.meta.ItemMeta#hasDisplayName()

The following examples show how to use org.bukkit.inventory.meta.ItemMeta#hasDisplayName() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CommandTemplate.java    From TrMenu with MIT License 6 votes vote down vote up
private ConfigurationSection display(ItemStack item) {
    ConfigurationSection section = new MemoryConfiguration();
    ItemMeta meta = item.getItemMeta();
    String mat = item.getType().name();
    if (meta instanceof SkullMeta) {
        String texture = Skulls.getTexture(item);
        mat = texture != null ? "<skull:" + Skulls.getTexture(item) + ">" : mat;
    }
    if (item.getAmount() > 1) {
        section.set("amount", item.getAmount());
    }
    if (meta != null) {
        if (meta.hasDisplayName()) {
            section.set("name", meta.getDisplayName());
        }
        if (meta.hasLore()) {
            section.set("lore", meta.getLore());
        }
    }
    section.set("mats", mat);
    return section;
}
 
Example 2
Source File: SlimefunUtils.java    From Slimefun4 with GNU General Public License v3.0 6 votes vote down vote up
private static boolean equalsItemMeta(ItemMeta itemMeta, ItemMeta sfitemMeta, boolean checkLore) {
    if (itemMeta.hasDisplayName() && sfitemMeta.hasDisplayName()) {
        if (itemMeta.getDisplayName().equals(sfitemMeta.getDisplayName())) {
            if (checkLore) {
                if (itemMeta.hasLore() && sfitemMeta.hasLore()) {
                    return equalsLore(itemMeta.getLore(), sfitemMeta.getLore());
                }
                else return !itemMeta.hasLore() && !sfitemMeta.hasLore();
            }
            else return true;
        }
        else return false;
    }
    else if (!itemMeta.hasDisplayName() && !sfitemMeta.hasDisplayName()) {
        if (checkLore) {
            if (itemMeta.hasLore() && sfitemMeta.hasLore()) {
                return equalsLore(itemMeta.getLore(), sfitemMeta.getLore());
            }
            else return !itemMeta.hasLore() && !sfitemMeta.hasLore();
        }
        else return true;
    }
    else return false;
}
 
Example 3
Source File: ItemFactory.java    From TradePlus with GNU General Public License v3.0 6 votes vote down vote up
public ItemFactory(ItemStack stack) {
  damage = stack.getDurability();
  material = stack.getType();
  amount = stack.getAmount();
  data = stack.getData().getData();
  if (stack.hasItemMeta()) {
    ItemMeta meta = stack.getItemMeta();
    if (meta.hasDisplayName()) {
      display = meta.getDisplayName();
    }
    if (meta.hasLore()) {
      lore = meta.getLore();
    }
    if (Sounds.version != 17) flags = new ArrayList<>(meta.getItemFlags());
  }
  if (Sounds.version > 113) {
    customModelData = ItemUtils1_14.getCustomModelData(stack);
  }
}
 
Example 4
Source File: ColorHandler.java    From EnchantmentsEnhance with GNU General Public License v3.0 6 votes vote down vote up
public static ItemStack colorItemWithPermissions(final ItemStack item, final Player p) {
    if (item.hasItemMeta()) {
        final ItemMeta itemMeta = item.getItemMeta();
        if (itemMeta.hasDisplayName()) {
            String coloredName = Util.toColor(itemMeta.getDisplayName());
            for (int i = 0; i < coloredName.length(); ++i) {
                if (coloredName.charAt(i) == '§') {
                    final char c = coloredName.charAt(i + 1);
                    if (ItemManager.getItemEnchantLevel(item) < 1) {
                        coloredName = coloredName.replaceAll("§" + c, "&" + c);
                    }
                }
            }
            itemMeta.setDisplayName(coloredName);
            item.setItemMeta(itemMeta);
        }
    }
    return item;
}
 
Example 5
Source File: DisplayItem.java    From QuickShop-Reremake with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Create a new itemStack with protect flag.
 *
 * @param itemStack Old itemStack
 * @param shop      The shop
 * @return New itemStack with protect flag.
 */
@NotNull
public static ItemStack createGuardItemStack(@NotNull ItemStack itemStack, @NotNull Shop shop) {
    itemStack = itemStack.clone();
    //itemStack.setAmount(1);
    ItemMeta iMeta = itemStack.getItemMeta();
    if (QuickShop.instance.getConfig().getBoolean("shop.display-item-use-name")) {
        if (iMeta.hasDisplayName()) {
            iMeta.setDisplayName(iMeta.getDisplayName());
        } else {
            iMeta.setDisplayName(Util.getItemStackName(itemStack));
        }
    } else {
        iMeta.setDisplayName(null);
    }
    ShopProtectionFlag shopProtectionFlag = createShopProtectionFlag(itemStack, shop);
    String protectFlag = gson.toJson(shopProtectionFlag);
    iMeta.setLore(Lists.newArrayList(protectFlag));
    itemStack.setItemMeta(iMeta);
    return itemStack;
}
 
Example 6
Source File: ShopInventory.java    From BedWars with GNU Lesser General Public License v3.0 5 votes vote down vote up
private static String getNameOrCustomNameOfItem(ItemStack stack) {
	try {
		if (stack.hasItemMeta()) {
			ItemMeta meta = stack.getItemMeta();
			if (meta == null) {
				return "";
			}

			if (meta.hasDisplayName()) {
				return meta.getDisplayName();
			}
			if (meta.hasLocalizedName()) {
				return meta.getLocalizedName();
			}
		}
	} catch (Throwable ignored) {
	}

	String normalItemName = stack.getType().name().replace("_", " ").toLowerCase();
	String[] sArray = normalItemName.split(" ");
	StringBuilder stringBuilder = new StringBuilder();

	for (String s : sArray) {
		stringBuilder.append(Character.toUpperCase(s.charAt(0))).append(s.substring(1)).append(" ");
	}
	return stringBuilder.toString().trim();
}
 
Example 7
Source File: SimpleI18n.java    From TabooLib with MIT License 5 votes vote down vote up
public static String getCustomName(ItemStack item) {
    if (item != null) {
        ItemMeta itemMeta = item.getItemMeta();
        return itemMeta != null && itemMeta.hasDisplayName() ? itemMeta.getDisplayName() : getName(item);
    }
    return getName(item);
}
 
Example 8
Source File: MapRatingsMatchModule.java    From ProjectAres with GNU Affero General Public License v3.0 5 votes vote down vote up
@EventHandler
public void onButtonClick(final InventoryClickEvent event) {
    ItemStack stack = event.getCurrentItem();
    final MatchPlayer player = this.getMatch().getPlayer(event.getWhoClicked());

    if(stack == null || player == null) return;
    if(stack.getType() != Material.WOOL && stack.getType() != Material.CARPET) return;
    ItemMeta meta = stack.getItemMeta();
    if(!meta.hasDisplayName()) return;
    String name = meta.getDisplayName();
    if(!name.startsWith(BUTTON_PREFIX)) return;

    event.setCancelled(true);

    final int score = stack.getAmount();
    if(!isScoreValid(score)) return;

    this.getMatch().getScheduler(MatchScope.LOADED).createTask(() -> {
        Integer oldScore = playerRatings.get(player);
        if(oldScore == null || oldScore != score) {
            player.playSound(Sound.UI_BUTTON_CLICK, 1, 2);
            rate(player, score);
        }
        else {
            player.getBukkit().closeInventory();
        }
    });
}
 
Example 9
Source File: ItemUtils.java    From AdditionsAPI with MIT License 5 votes vote down vote up
public static String getVisibleName(ItemStack item) {
	if (item.hasItemMeta()) {
		ItemMeta meta = item.getItemMeta();
		if (meta.hasDisplayName()) {
			return meta.getDisplayName();
		}
	}

	return item.getType().toString().toLowerCase();
}
 
Example 10
Source File: ItemData.java    From Skript with GNU General Public License v3.0 5 votes vote down vote up
public String toString(final boolean debug, final boolean plural) {
	StringBuilder builder = new StringBuilder(Aliases.getMaterialName(this, plural));
	ItemMeta meta = stack.getItemMeta();
	if (meta != null && meta.hasDisplayName()) {
		builder.append(" ").append(m_named).append(" ");
		builder.append(meta.getDisplayName());
	}
	return builder.toString();
}
 
Example 11
Source File: Utils.java    From Shopkeepers with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Checks if the given item matches the specified attributes.
 * 
 * @param item
 *            the item
 * @param type
 *            The item type.
 * @param data
 *            The data value/durability. If -1 is is ignored.
 * @param displayName
 *            The displayName. If null or empty it is ignored.
 * @param lore
 *            The item lore. If null or empty it is ignored.
 * @return <code>true</code> if the item has similar attributes
 */
public static boolean isSimilar(ItemStack item, Material type, short data, String displayName, List<String> lore) {
	if (item == null) return false;
	if (item.getType() != type) return false;
	if (data != -1 && item.getDurability() != data) return false;

	ItemMeta itemMeta = null;
	// compare display name:
	if (displayName != null && !displayName.isEmpty()) {
		if (!item.hasItemMeta()) return false;
		itemMeta = item.getItemMeta();
		if (itemMeta == null) return false;

		if (!itemMeta.hasDisplayName() || !displayName.equals(itemMeta.getDisplayName())) {
			return false;
		}
	}

	// compare lore:
	if (lore != null && !lore.isEmpty()) {
		if (itemMeta == null) {
			if (!item.hasItemMeta()) return false;
			itemMeta = item.getItemMeta();
			if (itemMeta == null) return false;
		}

		if (!itemMeta.hasLore() || !lore.equals(itemMeta.getLore())) {
			return false;
		}
	}

	return true;
}
 
Example 12
Source File: BlockPlacer.java    From Slimefun4 with GNU General Public License v3.0 5 votes vote down vote up
private void placeBlock(ItemStack item, Block facedBlock, Dispenser dispenser) {
    facedBlock.setType(item.getType());

    if (item.hasItemMeta()) {
        ItemMeta meta = item.getItemMeta();

        if (meta.hasDisplayName()) {
            BlockState blockState = facedBlock.getState();

            if ((blockState instanceof Nameable)) {
                ((Nameable) blockState).setCustomName(meta.getDisplayName());
            }

            // Update block state after changing name
            blockState.update();
        }

    }

    facedBlock.getWorld().playEffect(facedBlock.getLocation(), Effect.STEP_SOUND, item.getType());

    if (dispenser.getInventory().containsAtLeast(item, 2)) {
        dispenser.getInventory().removeItem(new CustomItem(item, 1));
    }
    else {
        Slimefun.runSync(() -> dispenser.getInventory().removeItem(item), 2L);
    }
}
 
Example 13
Source File: KitUtils.java    From AnnihilationPro with MIT License 5 votes vote down vote up
public static boolean itemHasName(ItemStack stack, String name)
{
	if(stack == null)
		return false;
	ItemMeta meta = stack.getItemMeta();
	if(meta == null)
		return false;
	if(!meta.hasDisplayName())
		return false;
	return meta.getDisplayName().equalsIgnoreCase(name);
}
 
Example 14
Source File: JsonItem.java    From TrMenu with MIT License 5 votes vote down vote up
public static String toJson(ItemStack item) {
    JsonObject json = new JsonObject();
    String type = item.getType().name();
    byte data = item.getData().getData();
    int amount = item.getAmount();

    json.addProperty("type", item.getType().name());
    if (data > 0) {
        json.addProperty("data", data);
    }
    if (amount > 1) {
        json.addProperty("amount", amount);
    }
    if (item.hasItemMeta()) {
        // Uncolor
        ItemMeta meta = item.getItemMeta();
        if (meta.hasDisplayName()) {
            meta.setDisplayName(meta.getDisplayName().replace('§', '&'));
        }
        if (meta.hasLore()) {
            List<String> lore = meta.getLore();
            lore.replaceAll(s -> s.replace('§', '&'));
            meta.setLore(lore);
        }
        item.setItemMeta(meta);
        json.add("meta", new JsonParser().parse(NMS.handle().loadNBT(item).toJson()));
    }
    return json.toString();
}
 
Example 15
Source File: ItemLine.java    From Holograms with MIT License 5 votes vote down vote up
static String itemstackToRaw(ItemStack itemstack) {
    StringBuilder sb = new StringBuilder();
    sb.append(itemstack.getType().toString()); // append type
    sb.append(':').append(itemstack.getDurability()); // append durability
    sb.append(' ').append(itemstack.getAmount()); // append amount

    if (itemstack.hasItemMeta()) {
        ItemMeta meta = itemstack.getItemMeta();

        // append name
        if (meta.hasDisplayName()) {
            sb.append(' ').append("name:").append(meta.getDisplayName().replace(' ', '_'));
        }

        // append lore
        if (meta.hasLore()) {
            sb.append(' ').append("lore:");
            Iterator<String> iterator = meta.getLore().iterator();
            while (iterator.hasNext()) {
                sb.append(iterator.next().replace(' ', '_'));
                if (iterator.hasNext()) {
                    sb.append('|');
                }
            }
        }

        // append enchantments
        meta.getEnchants().forEach((ench, level) -> {
            sb.append(' ').append(ench.getName()).append(':').append(level);
        });
    }

    return sb.toString();
}
 
Example 16
Source File: ShopInventory.java    From BedWars with GNU Lesser General Public License v3.0 5 votes vote down vote up
private static String getNameOrCustomNameOfItem(ItemStack stack) {
	try {
		if (stack.hasItemMeta()) {
			ItemMeta meta = stack.getItemMeta();
			if (meta == null) {
				return "";
			}

			if (meta.hasDisplayName()) {
				return meta.getDisplayName();
			}
			if (meta.hasLocalizedName()) {
				return meta.getLocalizedName();
			}
		}
	} catch (Throwable ignored) {
	}

	String normalItemName = stack.getType().name().replace("_", " ").toLowerCase();
	String[] sArray = normalItemName.split(" ");
	StringBuilder stringBuilder = new StringBuilder();

	for (String s : sArray) {
		stringBuilder.append(Character.toUpperCase(s.charAt(0))).append(s.substring(1)).append(" ");
	}
	return stringBuilder.toString().trim();
}
 
Example 17
Source File: GunUtil.java    From QualityArmory with GNU General Public License v3.0 4 votes vote down vote up
public static void basicReload(final Gun g, final Player player, boolean doNotRemoveAmmo, double seconds) {

		final ItemStack temp = player.getInventory().getItemInHand();
		ItemMeta im = temp.getItemMeta();

		if (Gun.getAmount(temp) == g.getMaxBullets()) {
			return;
		}
		if (im == null || !im.hasDisplayName())
			return;
		if (im.getLore() != null && im.getDisplayName().contains(QAMain.S_RELOADING_MESSAGE)) {

		} else {
			try {
				player.getWorld().playSound(player.getLocation(), WeaponSounds.RELOAD_MAG_OUT.getSoundName(), 1, 1f);
			} catch (Error e2) {
				try {
					player.getWorld().playSound(player.getLocation(), Sound.valueOf("CLICK"), 5, 1);
				} catch (Error | Exception e3) {
					player.getWorld().playSound(player.getLocation(), Sound.valueOf("BLOCK_LEVER_CLICK"), 5, 1);
				}
			}
			final int slot = player.getInventory().getHeldItemSlot();

			Ammo ammo = g.getAmmoType();

			final int initialAmount = Gun.getAmount(temp);
			final int reloadAmount = doNotRemoveAmmo ? g.getMaxBullets()
					: Math.min(g.getMaxBullets(), initialAmount + QualityArmory.getAmmoInInventory(player, ammo));
			final int subtractAmount = reloadAmount - initialAmount;
			if (!doNotRemoveAmmo)
				QualityArmory.removeAmmoFromInventory(player, ammo, subtractAmount);

			if (g.getReloadingingVal() != null) {
				seconds = g.getReloadingingVal().reload(player, g, subtractAmount);
			}
			QAMain.toggleNightvision(player, g, false);
			//Gun.updateAmmo(g, im, initialAmount);
			im.setDisplayName(g.getDisplayName() + QAMain.S_RELOADING_MESSAGE);
			temp.setItemMeta(im);
			player.getInventory().setItem(slot, temp);

			new GunRefillerRunnable(player, temp, g, slot, initialAmount, reloadAmount, seconds);

		}

	}
 
Example 18
Source File: ItemRenameListener.java    From EnchantmentsEnhance with GNU General Public License v3.0 4 votes vote down vote up
@EventHandler(priority = EventPriority.MONITOR)
    public void onInventoryClick(InventoryClickEvent e) {
// check if the event has been cancelled by another plugin
        if (!e.isCancelled()) {
            HumanEntity ent = e.getWhoClicked();

// not really necessary
            if (ent instanceof Player) {
                Player player = (Player) ent;
                Inventory inv = e.getInventory();

// see if the event is about an anvil
                if (inv instanceof AnvilInventory) {
                    InventoryView view = e.getView();
                    int rawSlot = e.getRawSlot();

// compare the raw slot with the inventory view to make sure we are talking about the upper inventory
                    if (rawSlot == view.convertSlot(rawSlot)) {
/*
slot 0 = left item slot
slot 1 = right item slot
slot 2 = result item slot

see if the player clicked in the result item slot of the anvil inventory
*/
                        if (rawSlot == 2) {
/*
get the current item in the result slot
I think inv.getItem(rawSlot) would be possible too
*/
                            ItemStack item = e.getCurrentItem();

// check if there is an item in the result slot
                            if (item != null) {
                                ItemMeta meta = item.getItemMeta();

// it is possible that the item does not have meta data
                                if (meta != null) {
// see whether the item is beeing renamed
                                    if (meta.hasDisplayName()) {
                                        String displayName = meta.getDisplayName();
                                        ItemManager.setName(item, displayName);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
 
Example 19
Source File: ItemData.java    From Skript with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Compares {@link ItemMeta}s for {@link #matchAlias(ItemData)}.
 * Note that this does NOT compare everything; only the most
 * important bits.
 * @param first Meta of this item.
 * @param second Meta of given item.
 * @return Match quality of metas.
 * Lowest is {@link MatchQuality#SAME_MATERIAL}.
 */
private static MatchQuality compareItemMetas(ItemMeta first, ItemMeta second) {
	MatchQuality quality = MatchQuality.EXACT; // Lowered as we go on
	
	// Display name
	String ourName = first.hasDisplayName() ? first.getDisplayName() : null;
	String theirName = second.hasDisplayName() ? second.getDisplayName() : null;
	if (!Objects.equals(ourName, theirName)) {
		quality = ourName != null ? MatchQuality.SAME_MATERIAL : quality;
	}
	
	// Lore
	List<String> ourLore = first.hasLore() ? first.getLore() : null;
	List<String> theirLore = second.hasLore() ? second.getLore() : null;
	if (!Objects.equals(ourLore, theirLore)) {
		quality = ourLore != null ? MatchQuality.SAME_MATERIAL : quality;
	}
	
	// Enchantments
	Map<Enchantment, Integer> ourEnchants = first.getEnchants();
	Map<Enchantment, Integer> theirEnchants = second.getEnchants();
	if (!Objects.equals(ourEnchants, theirEnchants)) {
		quality = !ourEnchants.isEmpty() ? MatchQuality.SAME_MATERIAL : quality;
	}
	
	// Item flags
	Set<ItemFlag> ourFlags = first.getItemFlags();
	Set<ItemFlag> theirFlags = second.getItemFlags();
	if (!Objects.equals(ourFlags, theirFlags)) {
		quality = !ourFlags.isEmpty() ? MatchQuality.SAME_MATERIAL : quality;
	}
	
	// Potion data
	if (second instanceof PotionMeta) {
		if (!(first instanceof PotionMeta)) {
			return MatchQuality.DIFFERENT; // Second is a potion, first is clearly not
		}
		// Compare potion type, including extended and level 2 attributes
		PotionData ourPotion = ((PotionMeta) first).getBasePotionData();
		PotionData theirPotion = ((PotionMeta) second).getBasePotionData();
		return !Objects.equals(ourPotion, theirPotion) ? MatchQuality.SAME_MATERIAL : quality;
	}
	
	return quality;
}
 
Example 20
Source File: GoldenHeadsModule.java    From UHC with MIT License 3 votes vote down vote up
public boolean isGoldenHead(ItemStack itemStack) {
    if (itemStack.getType() != Material.GOLDEN_APPLE) return false;

    final ItemMeta im = itemStack.getItemMeta();

    return im.hasDisplayName() && im.getDisplayName().equals(HEAD_NAME);
}