下面列出了怎么用org.bukkit.inventory.ItemFlag的API类实例代码及写法,或者点击链接到github查看源代码。
public PotionMeta convert(PotionMeta potionMeta) {
Objects.requireNonNull(potionMeta, "potionMeta");
Duration duration = this.config.duration();
String formattedDuration = this.formatDuration(this.config.duration());
if (this.config.color != null) {
potionMeta.setColor(this.config.color);
}
potionMeta.addItemFlags(ItemFlag.HIDE_POTION_EFFECTS);
potionMeta.setDisplayName(ChatColor.AQUA + this.config.name());
potionMeta.setLore(Collections.singletonList(ChatColor.BLUE + MessageFormat.format(this.config.description(), formattedDuration)));
PersistentDataContainer container = potionMeta.getPersistentDataContainer();
container.set(this.potionKey, PersistentDataType.BYTE, TRUE);
container.set(this.durationSecondsKey, PersistentDataType.INTEGER, (int) duration.getSeconds());
return potionMeta;
}
String itemFlagName(ItemFlag flag) {
switch (flag) {
case HIDE_ATTRIBUTES:
return "attributes";
case HIDE_ENCHANTS:
return "enchantments";
case HIDE_UNBREAKABLE:
return "unbreakable";
case HIDE_DESTROYS:
return "can-destroy";
case HIDE_PLACED_ON:
return "can-place-on";
case HIDE_POTION_EFFECTS:
return "other";
}
throw new IllegalStateException("Unknown item flag " + flag);
}
public Item(List<String> names, List<Mat> materials, List<List<String>> lores, List<List<Integer>> slots, List<ItemFlag> itemFlags, NBTCompound nbtCompound, String shiny, String amount) {
this.names = names;
this.materials = materials;
this.lores = lores;
this.rawSlots = slots;
this.itemFlags = itemFlags;
this.nbtCompound = nbtCompound;
this.shiny = shiny;
this.amount = amount;
this.finalShiny = Boolean.parseBoolean(shiny);
this.finalAmount = NumberUtils.toInt(amount, -1);
this.curSlots = new HashMap<>();
this.slots = new HashMap<>();
this.indexMap = new HashMap<>();
this.updateNbt = PlaceholderAPI.containsPlaceholders(nbtCompound.toJsonSimplified());
}
private static ItemStack buildersWandCreator(Material m, String name) { //TODO: Modify to implement Modifiers
ItemStack wand = new ItemStack(m, 1);
ItemMeta meta = wand.getItemMeta();
if (meta != null) {
ArrayList<String> lore = new ArrayList<>();
if (config.getBoolean("OverrideLanguagesystem")) {
lore.add(ChatWriter.addColors(config.getString("Description")));
} else {
lore.add(LanguageManager.getString("Builderswand.Description"));
}
meta.setLore(lore);
meta.setDisplayName(ChatWriter.addColors(name));
meta.addItemFlags(ItemFlag.HIDE_DESTROYS);
wand.setItemMeta(meta);
}
//TODO: DataHandler.setStringList(wand, "CanDestroy", true, "minecraft:air");
DataHandler.setTag(wand, "identifier_builderswand", 0, PersistentDataType.INTEGER, false);
return wand;
}
@Override
public boolean applyMod(Player player, ItemStack tool, boolean isCommand) {
ItemMeta meta = tool.getItemMeta();
if (meta != null) {
meta.addEnchant(Enchantment.DURABILITY, modManager.getModLevel(tool, this), true);
if (modManager.getModLevel(tool, this) == this.getMaxLvl() && this.applyUnbreakableOnMaxLevel) {
meta.setUnbreakable(true);
if (hideUnbreakableFlag) {
meta.addItemFlags(ItemFlag.HIDE_UNBREAKABLE);
}
}
tool.setItemMeta(meta);
}
return true;
}
public @NotNull ItemStack createModifierItem(@NotNull Material m, @NotNull String name, @NotNull String description, @NotNull Modifier mod) {
ItemStack is = new ItemStack(m, 1);
ItemMeta meta = is.getItemMeta();
if (meta != null) {
meta.setDisplayName(name);
ArrayList<String> lore = new ArrayList<>(ChatWriter.splitString(description, 40));
meta.setLore(lore);
meta.addItemFlags(ItemFlag.HIDE_PLACED_ON);
is.setItemMeta(meta);
}
DataHandler.setTag(is, "modifier_item", mod.getKey(), PersistentDataType.STRING, false);
//TODO: DataHandler.setStringList(is, "CanPlaceOn", true, "minecraft:air");
return is;
}
public static ItemStack parseItem(String source) {
TMap map = TMap.parse(source);
ItemBuilder builder = new ItemBuilder(Items.asMaterial(map.get("material", "m", "type", "t")))
.amount(map.getInt(new String[]{"amount", "a"}, 1))
.damage(map.getInt("damage", "data", "d"))
.name(map.get("displayname", "display", "name", "n"));
if (map.getBoolean("shiny", "glowing", "glow")) {
builder.shiny();
}
if (map.get("lore", "l") != null) {
builder.lore(map.get("lore", "l").split("\\n"));
}
if (map.get("flag", "f") != null) {
builder.flags(Arrays.stream(map.get("flag", "f").split("\\n")).map(Items::asItemFlag).filter(Objects::nonNull).toArray(ItemFlag[]::new));
}
return builder.colored().build();
}
private ItemStack createJoinButton(final MatchPlayer player) {
ItemStack stack = new ItemStack(Button.JOIN.material);
ItemMeta meta = stack.getItemMeta();
meta.addItemFlags(ItemFlag.values());
String key;
if(!canOpenWindow(player)) {
key = "ffa.picker.displayName";
} else if(hasTeams && hasClasses) {
key = "teamClass.picker.displayName";
} else if(hasTeams) {
key = "teamSelection.picker.displayName";
} else if(hasClasses) {
key = "class.picker.displayName";
} else {
key = "ffa.picker.displayName";
}
meta.setDisplayName(OPEN_BUTTON_PREFIX + PGMTranslations.t(key, player));
meta.setLore(Lists.newArrayList(ChatColor.DARK_PURPLE + PGMTranslations.t("teamSelection.picker.tooltip", player)));
stack.setItemMeta(meta);
ITEM_TAG.set(stack, "join");
return stack;
}
private ItemStack createClassButton(MatchPlayer viewer, PlayerClass cls) {
ItemStack item = cls.getIcon().toItemStack(1);
ItemMeta meta = item.getItemMeta();
meta.addItemFlags(ItemFlag.values());
meta.setDisplayName((cls.canUse(viewer.getBukkit()) ? ChatColor.GREEN : ChatColor.RED) + cls.getName());
if(getMatch().getMatchModule(ClassMatchModule.class).selectedClass(viewer.getDocument()).equals(cls)) {
meta.addEnchant(Enchantment.ARROW_INFINITE, 1, true);
}
List<String> lore = Lists.newArrayList();
if(cls.getLongDescription() != null) {
ChatUtils.wordWrap(ChatColor.GOLD + cls.getLongDescription(), LORE_WIDTH_PIXELS, lore);
} else if(cls.getDescription() != null) {
lore.add(ChatColor.GOLD + cls.getDescription());
}
if(!cls.canUse(viewer.getBukkit())) {
lore.add(ChatColor.RED + PGMTranslations.t("class.picker.restricted", viewer));
}
meta.setLore(lore);
item.setItemMeta(meta);
return item;
}
public ItemStack getRecipeTypeItem(Player p, RecipeType recipeType) {
Language language = getLanguage(p);
ItemStack item = recipeType.toItem();
NamespacedKey key = recipeType.getKey();
if (language.getRecipeTypesFile() == null || !language.getRecipeTypesFile().contains(key.getNamespace() + "." + key.getKey())) {
language = getLanguage("en");
}
if (!language.getRecipeTypesFile().contains(key.getNamespace() + "." + key.getKey())) {
return item;
}
FileConfiguration config = language.getRecipeTypesFile();
return new CustomItem(item, meta -> {
meta.setDisplayName(ChatColor.AQUA + config.getString(key.getNamespace() + "." + key.getKey() + ".name"));
List<String> lore = config.getStringList(key.getNamespace() + "." + key.getKey() + ".lore");
lore.replaceAll(line -> ChatColor.GRAY + line);
meta.setLore(lore);
meta.addItemFlags(ItemFlag.HIDE_ATTRIBUTES);
meta.addItemFlags(ItemFlag.HIDE_ENCHANTS);
});
}
/**
* Applies an item meta to this item. Currently, it copies the following,
* provided that they exist in given meta:
* <ul>
* <li>Lore
* <li>Display name
* <li>Enchantments
* <li>Item flags
* </ul>
* @param meta Item meta.
*/
public void applyMeta(ItemMeta meta) {
ItemMeta our = getItemMeta();
if (meta.hasLore())
our.setLore(meta.getLore());
if (meta.hasDisplayName())
our.setDisplayName(meta.getDisplayName());
if (meta.hasEnchants()) {
for (Map.Entry<Enchantment, Integer> entry : meta.getEnchants().entrySet()) {
our.addEnchant(entry.getKey(), entry.getValue(), true);
}
}
for (ItemFlag flag : meta.getItemFlags()) {
our.addItemFlags(flag);
}
setItemMeta(meta);
}
private ItemStack addGlow(ItemStack item) {
if (glowing) {
try {
if (item != null) {
if (item.hasItemMeta()) {
if (item.getItemMeta().hasEnchants()) {
return item;
}
}
item.addUnsafeEnchantment(Enchantment.LUCK, 1);
ItemMeta meta = item.getItemMeta();
meta.addItemFlags(ItemFlag.HIDE_ENCHANTS);
item.setItemMeta(meta);
}
return item;
} catch (NoClassDefFoundError e) {
return item;
}
}
return item;
}
public static ItemStack makeItemHidden(XMaterial material, int amount, String name, List<String> lore) {
ItemStack item = material.parseItem(true);
if (item == null) return null;
item.setAmount(amount);
ItemMeta m = item.getItemMeta();
m.setLore(Utils.color(lore));
m.addItemFlags(ItemFlag.HIDE_ATTRIBUTES, ItemFlag.HIDE_DESTROYS, ItemFlag.HIDE_ENCHANTS, ItemFlag.HIDE_PLACED_ON, ItemFlag.HIDE_POTION_EFFECTS, ItemFlag.HIDE_UNBREAKABLE);
m.setDisplayName(ChatColor.translateAlternateColorCodes('&', name));
item.setItemMeta(m);
return item;
}
public ItemStack getItemStack(ItemStack item, List<String> lore, String message) {
ItemStack addItem = item.clone();
ItemMeta addItemMeta = addItem.getItemMeta();
addItemMeta.setDisplayName(message);
addItemMeta.setLore(lore);
addItemMeta.addItemFlags(ItemFlag.HIDE_ATTRIBUTES);
addItemMeta.addItemFlags(ItemFlag.HIDE_DESTROYS);
addItemMeta.addItemFlags(ItemFlag.HIDE_POTION_EFFECTS);
addItemMeta.addItemFlags(ItemFlag.HIDE_PLACED_ON);
addItemMeta.addItemFlags(ItemFlag.HIDE_UNBREAKABLE);
addItemMeta.addItemFlags(ItemFlag.HIDE_ENCHANTS);
addItem.setItemMeta(addItemMeta);
return addItem;
}
public ItemStack getItemStack(Material material, List<String> lore, String message) {
ItemStack addItem = new ItemStack(material, 1);
ItemMeta addItemMeta = addItem.getItemMeta();
addItemMeta.setDisplayName(message);
addItemMeta.setLore(lore);
addItemMeta.addItemFlags(ItemFlag.HIDE_ATTRIBUTES);
addItemMeta.addItemFlags(ItemFlag.HIDE_DESTROYS);
addItemMeta.addItemFlags(ItemFlag.HIDE_POTION_EFFECTS);
addItemMeta.addItemFlags(ItemFlag.HIDE_PLACED_ON);
addItemMeta.addItemFlags(ItemFlag.HIDE_UNBREAKABLE);
addItemMeta.addItemFlags(ItemFlag.HIDE_ENCHANTS);
addItem.setItemMeta(addItemMeta);
return addItem;
}
public ItemStack getItemStack(Material material, List<String> lore, String message) {
ItemStack addItem = new ItemStack(material, 1);
ItemMeta addItemMeta = addItem.getItemMeta();
addItemMeta.setDisplayName(message);
addItemMeta.setLore(lore);
addItemMeta.addItemFlags(ItemFlag.HIDE_ATTRIBUTES);
addItemMeta.addItemFlags(ItemFlag.HIDE_DESTROYS);
addItemMeta.addItemFlags(ItemFlag.HIDE_ENCHANTS);
addItemMeta.addItemFlags(ItemFlag.HIDE_POTION_EFFECTS);
addItemMeta.addItemFlags(ItemFlag.HIDE_PLACED_ON);
addItemMeta.addItemFlags(ItemFlag.HIDE_UNBREAKABLE);
addItem.setItemMeta(addItemMeta);
return addItem;
}
@Override
public void removeMod(ItemStack tool) {
ItemMeta meta = tool.getItemMeta();
if (meta != null) {
meta.removeEnchant(Enchantment.DURABILITY);
if (this.applyUnbreakableOnMaxLevel) {
meta.setUnbreakable(false);
meta.removeItemFlags(ItemFlag.HIDE_UNBREAKABLE);
}
tool.setItemMeta(meta);
}
}
public boolean addMod(Player player, ItemStack item, @NotNull Modifier modifier, boolean fromCommand, boolean fromRandom, boolean silent) {
if (!modifier.getKey().equals(ExtraModifier.instance().getKey())) {
if (!modifier.checkAndAdd(player, item,
modifier.getKey().toLowerCase().replace("-", ""), fromCommand, fromRandom, silent)) {
return false;
}
}
boolean success = modifier.applyMod(player, item, fromCommand);
if (success) {
ItemMeta meta = item.getItemMeta();
if (meta != null) {
if (MineTinker.getPlugin().getConfig().getBoolean("HideEnchants", true)) {
meta.addItemFlags(ItemFlag.HIDE_ENCHANTS);
} else {
meta.removeItemFlags(ItemFlag.HIDE_ENCHANTS);
}
if (MineTinker.getPlugin().getConfig().getBoolean("HideAttributes", true)) {
meta.addItemFlags(ItemFlag.HIDE_ATTRIBUTES);
} else {
meta.removeItemFlags(ItemFlag.HIDE_ATTRIBUTES);
}
item.setItemMeta(meta);
}
}
return success;
}
public ItemStack getItemStack(Material material, List<String> lore, String message) {
ItemStack addItem = new ItemStack(material, 1);
ItemMeta addItemMeta = addItem.getItemMeta();
addItemMeta.setDisplayName(message);
addItemMeta.setLore(lore);
addItemMeta.addItemFlags(ItemFlag.HIDE_ATTRIBUTES);
addItemMeta.addItemFlags(ItemFlag.HIDE_DESTROYS);
addItemMeta.addItemFlags(ItemFlag.HIDE_ENCHANTS);
addItemMeta.addItemFlags(ItemFlag.HIDE_POTION_EFFECTS);
addItemMeta.addItemFlags(ItemFlag.HIDE_PLACED_ON);
addItemMeta.addItemFlags(ItemFlag.HIDE_UNBREAKABLE);
addItem.setItemMeta(addItemMeta);
return addItem;
}
@SuppressWarnings("deprecation")
private void addCategoriesToInventory(Inventory inventory, Player player, Game game) {
for (MerchantCategory category : this.categories) {
if (category.getMaterial() == null) {
BedwarsRel.getInstance().getServer().getConsoleSender()
.sendMessage(ChatWriter.pluginMessage(ChatColor.RED
+ "Careful: Not supported material in shop category '" + category.getName() + "'"));
continue;
}
if (player != null && !player.hasPermission(category.getPermission())) {
continue;
}
ItemStack is = new ItemStack(category.getMaterial(), 1);
ItemMeta im = is.getItemMeta();
if (Utils.isColorable(is)) {
is.setDurability(game.getPlayerTeam(player).getColor().getDyeColor().getWoolData());
}
if (this.currentCategory != null && this.currentCategory.equals(category)) {
im.addEnchant(Enchantment.DAMAGE_ALL, 1, true);
im.addItemFlags(ItemFlag.HIDE_ENCHANTS);
}
im.setDisplayName(category.getName());
im.setLore(category.getLores());
im.addItemFlags(ItemFlag.HIDE_ATTRIBUTES, ItemFlag.HIDE_POTION_EFFECTS);
is.setItemMeta(im);
inventory.addItem(is);
}
}
@Override
public Set<ItemFlag> getItemFlags() {
Set<ItemFlag> currentFlags = EnumSet.noneOf(ItemFlag.class);
for (ItemFlag f : ItemFlag.values()) {
if (hasItemFlag(f)) {
currentFlags.add(f);
}
}
return currentFlags;
}
public ItemStack getItemStack(ItemStack item, List<String> lore, String message) {
ItemStack addItem = item.clone();
ItemMeta addItemMeta = addItem.getItemMeta();
addItemMeta.setDisplayName(message);
addItemMeta.setLore(lore);
addItemMeta.addItemFlags(ItemFlag.HIDE_ATTRIBUTES);
addItemMeta.addItemFlags(ItemFlag.HIDE_DESTROYS);
addItemMeta.addItemFlags(ItemFlag.HIDE_POTION_EFFECTS);
addItemMeta.addItemFlags(ItemFlag.HIDE_PLACED_ON);
addItemMeta.addItemFlags(ItemFlag.HIDE_UNBREAKABLE);
addItemMeta.addItemFlags(ItemFlag.HIDE_ENCHANTS);
addItem.setItemMeta(addItemMeta);
return addItem;
}
public SlimefunItemStack(String id, Color color, PotionEffect effect, String name, String... lore) {
super(Material.POTION, im -> {
if (name != null) {
im.setDisplayName(ChatColor.translateAlternateColorCodes('&', name));
}
if (lore.length > 0) {
List<String> lines = new ArrayList<>();
for (String line : lore) {
lines.add(ChatColor.translateAlternateColorCodes('&', line));
}
im.setLore(lines);
}
if (im instanceof PotionMeta) {
((PotionMeta) im).setColor(color);
((PotionMeta) im).addCustomEffect(effect, true);
if (effect.getType().equals(PotionEffectType.SATURATION)) {
im.addItemFlags(ItemFlag.HIDE_POTION_EFFECTS);
}
}
});
setItemId(id);
}
private void createToggleableItem(boolean flagValue, Material material, int durability, String name, UUID uuid) {
this.flagValue = flagValue;
this.slot = -1;
this.name = name;
this.type = Type.TOGGLE;
description.clear();
item = new ItemStack(material);
item.setDurability((short)durability);
ItemMeta meta = item.getItemMeta();
meta.setDisplayName(ChatColor.WHITE + name);
if (flagValue) {
if (uuid == null)
description.add(ChatColor.GREEN + ASkyBlock.getPlugin().myLocale().igsAllowed);
else
description.add(ChatColor.GREEN + ASkyBlock.getPlugin().myLocale(uuid).igsAllowed);
} else {
if (uuid == null)
description.add(ChatColor.RED + ASkyBlock.getPlugin().myLocale().igsDisallowed);
else
description.add(ChatColor.RED + ASkyBlock.getPlugin().myLocale(uuid).igsDisallowed);
}
meta.setLore(description);
// Remove extraneous info
if (!Bukkit.getServer().getVersion().contains("1.7") && !Bukkit.getServer().getVersion().contains("1.8")) {
meta.addItemFlags(ItemFlag.HIDE_ATTRIBUTES);
meta.addItemFlags(ItemFlag.HIDE_DESTROYS);
meta.addItemFlags(ItemFlag.HIDE_PLACED_ON);
meta.addItemFlags(ItemFlag.HIDE_ENCHANTS);
}
item.setItemMeta(meta);
}
private ItemStack getOpenButton(MatchPlayer player) {
ItemStack stack = new ItemStack(Material.HOPPER);
ItemMeta meta = stack.getItemMeta();
meta.addItemFlags(ItemFlag.values());
meta.setDisplayName(BUTTON_PREFIX + ChatColor.BLUE.toString() + ChatColor.BOLD + PGMTranslations.t("rating.rateThisMap", player));
stack.setItemMeta(meta);
return stack;
}
String itemFlagName(ItemFlag flag) {
switch(flag) {
case HIDE_ATTRIBUTES: return "attributes";
case HIDE_ENCHANTS: return "enchantments";
case HIDE_UNBREAKABLE: return "unbreakable";
case HIDE_DESTROYS: return "can-destroy";
case HIDE_PLACED_ON: return "can-place-on";
case HIDE_POTION_EFFECTS: return "other";
}
throw new IllegalStateException("Unknown item flag " + flag);
}
/**
* Constructs a new {@link Category} with the given {@link NamespacedKey} as an identifier
* and the given {@link ItemStack} as its display item.
*
* @param key
* The {@link NamespacedKey} that is used to identify this {@link Category}
* @param item
* The {@link ItemStack} that is used to display this {@link Category}
* @param tier
* The tier of this {@link Category}, higher tiers will make this {@link Category} appear further down in
* the {@link SlimefunGuide}
*/
public Category(NamespacedKey key, ItemStack item, int tier) {
Validate.notNull(key, "A Category's NamespacedKey must not be null!");
Validate.notNull(item, "A Category's ItemStack must not be null!");
this.item = item;
this.key = key;
ItemMeta meta = item.getItemMeta();
meta.addItemFlags(ItemFlag.HIDE_ATTRIBUTES);
meta.addItemFlags(ItemFlag.HIDE_ENCHANTS);
this.item.setItemMeta(meta);
this.tier = tier;
}
private ItemStack createLeaveButton(final MatchPlayer player) {
ItemStack stack = new ItemStack(Button.LEAVE.material);
ItemMeta meta = stack.getItemMeta();
meta.addItemFlags(ItemFlag.values());
meta.setDisplayName(OPEN_BUTTON_PREFIX + PGMTranslations.t("leave.picker.displayName", player));
meta.setLore(Lists.newArrayList(ChatColor.DARK_PURPLE + PGMTranslations.t("leave.picker.tooltip", player)));
stack.setItemMeta(meta);
ITEM_TAG.set(stack, "leave");
return stack;
}
private ItemStack createAutoJoinButton(MatchPlayer viewer) {
ItemStack autojoin = new ItemStack(Button.AUTO_JOIN.material);
ItemMeta autojoinMeta = autojoin.getItemMeta();
autojoinMeta.addItemFlags(ItemFlag.values());
autojoinMeta.setDisplayName(ChatColor.GRAY.toString() + ChatColor.BOLD + PGMTranslations.t("teamSelection.picker.autoJoin.displayName", viewer));
autojoinMeta.setLore(Lists.newArrayList(this.getTeamSizeDescription(viewer.getMatch().getParticipatingPlayers().size(),
viewer.getMatch().getMaxPlayers()),
ChatColor.AQUA + PGMTranslations.t("teamSelection.picker.autoJoin.tooltip", viewer)));
autojoin.setItemMeta(autojoinMeta);
return autojoin;
}
private ItemStack createTeamJoinButton(final MatchPlayer player, final Team team) {
ItemStack item = new ItemStack(Button.TEAM_JOIN.material);
String capacityMessage = this.getTeamSizeDescription(team.getPlayers().size(), team.getMaxPlayers());
List<String> lore = Lists.newArrayList(capacityMessage);
final JoinResult result = jmm.queryJoin(player, JoinRequest.user(team));
if(result.isAllowed()) {
final String label = result.isRejoin() ? "teamSelection.picker.clickToRejoin"
: "teamSelection.picker.clickToJoin";
lore.add(renderer.renderLegacy(new Component(new TranslatableComponent(label), ChatColor.GREEN), player.getBukkit()));
} else if(result.message().isPresent()) {
lore.add(renderer.renderLegacy(new Component(result.message().get(), ChatColor.RED), player.getBukkit()));
result.extra().forEach(line -> {
lore.add(renderer.renderLegacy(line, player.getBukkit()));
});
}
LeatherArmorMeta meta = (LeatherArmorMeta) item.getItemMeta();
meta.addItemFlags(ItemFlag.values());
meta.setColor(team.getFullColor());
meta.setDisplayName(team.getColor().toString() + ChatColor.BOLD + team.getName());
meta.setLore(lore);
meta.addItemFlags(ItemFlag.values()); // Hides a lot more than potion effects
item.setItemMeta(meta);
return item;
}