下面列出了怎么用org.bukkit.inventory.meta.PotionMeta的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;
}
@Override
public @Nullable ItemModifyModule parse(MapFactory factory, Logger logger, Document doc)
throws InvalidXMLException {
List<ItemRule> rules = new ArrayList<>();
for (Element elRule : XMLUtils.flattenElements(doc.getRootElement(), "item-mods", "rule")) {
MaterialMatcher items =
XMLUtils.parseMaterialMatcher(XMLUtils.getRequiredUniqueChild(elRule, "match"));
// Always use a PotionMeta so the rule can have potion effects, though it will only apply
// those to potion items
PotionMeta meta = (PotionMeta) Bukkit.getItemFactory().getItemMeta(Material.POTION);
factory.getKits().parseItemMeta(XMLUtils.getRequiredUniqueChild(elRule, "modify"), meta);
ItemRule rule = new ItemRule(items, meta);
rules.add(rule);
}
return rules.isEmpty() ? null : new ItemModifyModule(rules);
}
public List<String> getName(ItemStack itemStack) {
List<String> possibleMaterials = new ArrayList<>();
String name = itemStack.getType().name();
possibleMaterials.add(name);
if (itemStack.hasItemMeta()) {
ItemMeta itemMeta = itemStack.getItemMeta();
if (itemMeta instanceof PotionMeta) {
PotionMeta potionMeta = (PotionMeta) itemStack.getItemMeta();
PotionData potionData = potionMeta.getBasePotionData();
possibleMaterials.add(potionData.getType().getEffectType().getName() + "_" + name);
if (potionMeta.hasCustomEffects()) {
for (PotionEffect potionEffect : potionMeta.getCustomEffects()) {
possibleMaterials.add(potionEffect.getType().getName() + "_" + name);
}
}
}
}
return possibleMaterials;
}
@Override
public @Nullable ItemModifyModule parse(MapModuleContext context, Logger logger, Document doc) throws InvalidXMLException {
List<ItemRule> rules = new ArrayList<>();
for(Element elRule : XMLUtils.flattenElements(doc.getRootElement(), "item-mods", "rule")) {
MaterialMatcher items = XMLUtils.parseMaterialMatcher(XMLUtils.getRequiredUniqueChild(elRule, "match"));
// Always use a PotionMeta so the rule can have potion effects, though it will only apply those to potion items
final Element elModify = XMLUtils.getRequiredUniqueChild(elRule, "modify");
final PotionMeta meta = (PotionMeta) Bukkit.getItemFactory().getItemMeta(Material.POTION);
context.needModule(ItemParser.class).parseItemMeta(elModify, meta);
final boolean defaultAttributes = XMLUtils.parseBoolean(elModify.getAttribute("default-attributes"), false);
ItemRule rule = new ItemRule(items, meta, defaultAttributes);
rules.add(rule);
}
return rules.isEmpty() ? null : new ItemModifyModule(rules);
}
private void giveTeamReward(Player player){
Wolf wolf = (Wolf) player.getWorld().spawnEntity(player.getLocation(), EntityType.WOLF);
wolf.setTamed(true);
wolf.setOwner(player);
wolf.setAdult();
ItemStack potion = new ItemStack(Material.POTION);
PotionMeta meta = (PotionMeta) potion.getItemMeta();
meta.setMainEffect(PotionEffectType.SPEED);
PotionEffect potionEffect = new PotionEffect(PotionEffectType.SPEED, 8*60*20, 0);
meta.addCustomEffect(potionEffect, true);
meta.setDisplayName(ChatColor.WHITE + "Potion of Swiftness");
potion.setItemMeta(meta);
player.getWorld().dropItem(player.getLocation(), potion);
}
private static ItemMeta parseBasePotionEffect(ItemMeta meta, JsonObject jsonObject) throws ParseException{
PotionMeta potionMeta = (PotionMeta) meta;
PotionType type;
try {
type = PotionType.valueOf(jsonObject.get("type").getAsString());
}catch (IllegalArgumentException ex){
throw new ParseException(ex.getMessage());
}
JsonElement jsonElement;
jsonElement = jsonObject.get("extended");
boolean extended = jsonElement != null && jsonElement.getAsBoolean();
jsonElement = jsonObject.get("upgraded");
boolean upgraded = jsonElement != null && jsonElement.getAsBoolean();
PotionData potionData = new PotionData(type, extended, upgraded);
potionMeta = VersionUtils.getVersionUtils().setBasePotionEffect(potionMeta, potionData);
return potionMeta;
}
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onPlayerItemConsumeEvent(PlayerItemConsumeEvent event)
{
Player player = event.getPlayer();
ItemMeta itemMeta = event.getItem().getItemMeta();
if (itemMeta instanceof PotionMeta)
{
this.plugin.getWorldGuardCommunicator().getSessionManager().get(player).getHandler(GiveEffectsFlagHandler.class).drinkPotion(player, Potion.fromItemStack(event.getItem()).getEffects());
}
else
{
Material material = event.getItem().getType();
if (material == Material.MILK_BUCKET)
{
this.plugin.getWorldGuardCommunicator().getSessionManager().get(player).getHandler(GiveEffectsFlagHandler.class).drinkMilk(player);
}
}
}
public boolean denyPotion(ItemStack result, Player p) {
List<String> Pots = RedProtect.get().config.globalFlagsRoot().worlds.get(p.getWorld().getName()).deny_potions;
if (result != null && Pots.size() > 0 && (result.getType().name().contains("POTION") || result.getType().name().contains("TIPPED"))) {
String potname = "";
if (RedProtect.get().bukkitVersion >= 190) {
PotionMeta pot = (PotionMeta) result.getItemMeta();
potname = pot.getBasePotionData().getType().name();
}
if (RedProtect.get().bukkitVersion <= 180 && Potion.fromItemStack(result) != null) {
potname = Potion.fromItemStack(result).getType().name();
}
if (Pots.contains(potname)) {
RedProtect.get().lang.sendMessage(p, "playerlistener.denypotion");
return true;
}
}
return false;
}
private boolean onPotion(Player sender) {
ItemStack itemStack = new ItemStack(Material.POTION);
PotionMeta itemMeta = Objects.requireNonNull((PotionMeta) itemStack.getItemMeta());
itemMeta.setBasePotionData(new PotionData(potion.getConfig().getRecipe().getBasePotion()));
itemStack.setItemMeta(potion.convert(itemMeta));
sender.getInventory().addItem(itemStack);
return true;
}
public static double getScore(ThrownPotion potion) {
double score = 0;
for (PotionEffect effect :
Iterables.concat(
potion.getEffects(),
((PotionMeta) potion.getItem().getItemMeta()).getCustomEffects())) {
score += getScore(effect);
}
return score;
}
public static Collection<PotionEffect> getEffects(ItemStack potion) {
if (potion.getItemMeta() instanceof PotionMeta) {
PotionMeta meta = (PotionMeta) potion.getItemMeta();
if (meta.hasCustomEffects()) {
return meta.getCustomEffects();
} else {
return Potion.fromItemStack(potion).getEffects();
}
} else {
return Collections.emptyList();
}
}
/**
* Applies the effects of this potion to the given {@link ItemStack}. The
* ItemStack must be a potion.
*
* @param to The itemstack to apply to
*/
public void apply(ItemStack to) {
Validate.notNull(to, "itemstack cannot be null");
Validate.isTrue(to.hasItemMeta(), "given itemstack is not a potion");
Validate.isTrue(to.getItemMeta() instanceof PotionMeta, "given itemstack is not a potion");
PotionMeta meta = (PotionMeta) to.getItemMeta();
meta.setBasePotionData(new PotionData(type, extended, level == 2));
to.setItemMeta(meta);
}
/**
* Converts this potion to an {@link ItemStack} with the specified amount
* and a correct damage value.
*
* @param amount The amount of the ItemStack
* @return The created ItemStack
*/
public ItemStack toItemStack(int amount) {
Material material;
if (isSplash()) {
material = Material.SPLASH_POTION;
} else {
material = Material.POTION;
}
ItemStack itemStack = new ItemStack(material, amount);
PotionMeta meta = (PotionMeta) itemStack.getItemMeta();
meta.setBasePotionData(new PotionData(type, level == 2, extended));
itemStack.setItemMeta(meta);
return itemStack;
}
@Override
public JsonObject getBasePotionEffect(PotionMeta potionMeta) {
PotionData potionData = potionMeta.getBasePotionData();
JsonObject baseEffect = new JsonObject();
baseEffect.addProperty("type", potionData.getType().name());
if (potionData.isUpgraded()) {
baseEffect.addProperty("upgraded", true);
}
if (potionData.isExtended()) {
baseEffect.addProperty("extended", true);
}
return baseEffect;
}
@Nullable
@Override
public Color getPotionColor(PotionMeta potionMeta){
if (potionMeta.hasColor()){
return potionMeta.getColor();
}
return null;
}
@Override
public JsonObject getBasePotionEffect(PotionMeta potionMeta) {
PotionData potionData = potionMeta.getBasePotionData();
JsonObject baseEffect = new JsonObject();
baseEffect.addProperty("type", potionData.getType().name());
if (potionData.isUpgraded()) {
baseEffect.addProperty("upgraded", true);
}
if (potionData.isExtended()) {
baseEffect.addProperty("extended", true);
}
return baseEffect;
}
@Nullable
@Override
public Color getPotionColor(PotionMeta potionMeta){
if (potionMeta.hasColor()){
return potionMeta.getColor();
}
return null;
}
private static ItemMeta parseCustomPotionEffects(ItemMeta meta, JsonArray jsonArray) throws ParseException{
if (meta instanceof PotionMeta){
PotionMeta potionMeta = (PotionMeta) meta;
for (JsonElement jsonElement : jsonArray){
JsonObject effect = jsonElement.getAsJsonObject();
potionMeta.addCustomEffect(parsePotionEffect(effect), true);
}
return potionMeta;
}else{
return VersionUtils.getVersionUtils().applySuspiciousStewEffects(meta, jsonArray);
}
}
private static ItemMeta parseColor(ItemMeta meta, int color){
if (meta instanceof PotionMeta){
PotionMeta potionMeta = (PotionMeta) meta;
VersionUtils.getVersionUtils().setPotionColor(potionMeta, Color.fromRGB(color));
return potionMeta;
}else if (meta instanceof LeatherArmorMeta){
LeatherArmorMeta leatherMeta = (LeatherArmorMeta) meta;
leatherMeta.setColor(Color.fromBGR(color));
return leatherMeta;
}
return meta;
}
@Deprecated
public CustomPotion(String name, int durability, String[] lore, PotionEffect effect) {
super(Material.POTION, name, (ReflectionUtils.getVersion().startsWith("v1_8_")) ? durability: 0, lore);
PotionMeta meta = (PotionMeta) getItemMeta();
if (ReflectionUtils.getVersion().startsWith("v1_8_")) meta.setMainEffect(PotionEffectType.SATURATION);
else meta.setMainEffect(effect.getType());
meta.addCustomEffect(effect, true);
setItemMeta(meta);
}
public CustomPotion(String name, PotionType type, PotionEffect effect, String... lore) {
super(Material.POTION, name, lore);
PotionMeta meta = (PotionMeta) getItemMeta();
meta.setBasePotionData(new PotionData(type));
meta.addCustomEffect(effect, true);
setItemMeta(meta);
}
public CustomPotion(String name, Color color, PotionEffect effect, String... lore) {
super(Material.POTION, name, lore);
PotionMeta meta = (PotionMeta) getItemMeta();
meta.setColor(color);
meta.addCustomEffect(effect, true);
setItemMeta(meta);
}
@SuppressWarnings("unchecked")
private void parsePotionEffects() {
PotionMeta customPotionMeta = (PotionMeta) this.finalStack.getItemMeta();
for (Object potionEffect : (List<Object>) this.linkedSection.get("effects")) {
LinkedHashMap<String, Object> potionEffectSection =
(LinkedHashMap<String, Object>) potionEffect;
if (!potionEffectSection.containsKey("type")) {
continue;
}
PotionEffectType potionEffectType = null;
int duration = 1;
int amplifier = 0;
potionEffectType =
PotionEffectType.getByName(potionEffectSection.get("type").toString().toUpperCase());
if (potionEffectSection.containsKey("duration")) {
duration = Integer.parseInt(potionEffectSection.get("duration").toString()) * 20;
}
if (potionEffectSection.containsKey("amplifier")) {
amplifier = Integer.parseInt(potionEffectSection.get("amplifier").toString()) - 1;
}
if (potionEffectType == null) {
continue;
}
customPotionMeta.addCustomEffect(new PotionEffect(potionEffectType, duration, amplifier),
true);
}
this.finalStack.setItemMeta(customPotionMeta);
}
private VillagerTrade getTradingItem(MerchantCategory category, ItemStack stack, Game game,
Player player) {
for (VillagerTrade trade : category.getOffers()) {
if (trade.getItem1().getType() == Material.AIR
&& trade.getRewardItem().getType() == Material.AIR) {
continue;
}
ItemStack iStack = this.toItemStack(trade, player, game);
if (iStack.getType() == Material.ENDER_CHEST && stack.getType() == Material.ENDER_CHEST) {
return trade;
} else if ((iStack.getType() == Material.POTION
|| (!BedwarsRel.getInstance().getCurrentVersion().startsWith("v1_8")
&& (iStack.getType().equals(Material.valueOf("TIPPED_ARROW"))
|| iStack.getType().equals(Material.valueOf("LINGERING_POTION"))
|| iStack.getType().equals(Material.valueOf("SPLASH_POTION")))))) {
if (BedwarsRel.getInstance().getCurrentVersion().startsWith("v1_8")) {
if (iStack.getItemMeta().equals(stack.getItemMeta())) {
return trade;
}
} else {
PotionMeta iStackMeta = (PotionMeta) iStack.getItemMeta();
PotionMeta stackMeta = (PotionMeta) stack.getItemMeta();
if (iStackMeta.getBasePotionData().equals(stackMeta.getBasePotionData()) && iStackMeta
.getCustomEffects().equals(stackMeta.getCustomEffects())) {
return trade;
}
}
} else if (iStack.equals(stack)) {
return trade;
}
}
return null;
}
public boolean denyPotion(ItemStack result, World world) {
List<String> Pots = RedProtect.get().config.globalFlagsRoot().worlds.get(world.getName()).deny_potions;
if (result != null && Pots.size() > 0 && (result.getType().name().contains("POTION") || result.getType().name().contains("TIPPED"))) {
String potname = "";
if (RedProtect.get().bukkitVersion >= 190) {
PotionMeta pot = (PotionMeta) result.getItemMeta();
potname = pot.getBasePotionData().getType().name();
}
if (RedProtect.get().bukkitVersion < 190) {
potname = Potion.fromItemStack(result).getType().name();
}
return Pots.contains(potname);
}
return false;
}
public static PotionType getPotionEffect(ItemStack itemStack) {
if (itemStack.getItemMeta() instanceof PotionMeta) {
if (Utils.getMajorVersion() < 9) {
return Potion.fromItemStack(itemStack).getType();
} else {
return ((PotionMeta) itemStack.getItemMeta()).getBasePotionData().getType();
}
}
return null;
}
public static boolean isExtendedPotion(ItemStack itemStack) {
if (itemStack.getItemMeta() instanceof PotionMeta) {
if (Utils.getMajorVersion() < 9) {
return Potion.fromItemStack(itemStack).hasExtendedDuration();
} else {
return ((PotionMeta) itemStack.getItemMeta()).getBasePotionData().isExtended();
}
}
return false;
}
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);
}
@EventHandler
public void onConsume(PlayerItemConsumeEvent e) {
Player p = e.getPlayer();
ItemStack item = e.getItem();
SlimefunItem sfItem = SlimefunItem.getByItem(item);
if (sfItem != null) {
if (Slimefun.hasUnlocked(p, sfItem, true)) {
if (sfItem instanceof Juice) {
// Fix for Saturation on potions is no longer working
for (PotionEffect effect : ((PotionMeta) item.getItemMeta()).getCustomEffects()) {
if (effect.getType().equals(PotionEffectType.SATURATION)) {
p.addPotionEffect(new PotionEffect(PotionEffectType.SATURATION, effect.getDuration(), effect.getAmplifier()));
break;
}
}
removeGlassBottle(p, item);
}
else {
sfItem.callItemHandler(ItemConsumptionHandler.class, handler -> handler.onConsume(e, p, item));
}
}
else {
e.setCancelled(true);
}
}
}
private boolean consumeJuice(Player p, PlayerBackpack backpack) {
Inventory inv = backpack.getInventory();
int slot = -1;
for (int i = 0; i < inv.getSize(); i++) {
ItemStack stack = inv.getItem(i);
if (stack != null && stack.getType() == Material.POTION && stack.hasItemMeta() && stack.getItemMeta().hasDisplayName()) {
slot = i;
break;
}
}
if (slot >= 0) {
PotionMeta im = (PotionMeta) inv.getItem(slot).getItemMeta();
for (PotionEffect effect : im.getCustomEffects()) {
p.addPotionEffect(effect);
}
p.setSaturation(6F);
p.playSound(p.getLocation(), Sound.ENTITY_GENERIC_DRINK, 1F, 1F);
inv.setItem(slot, null);
backpack.markDirty();
return true;
}
return false;
}