Add new consumable stats, Max Consume

Use it if you want to allow x amount of consume before the item disappears

Please add these lines to your stats.yml and lore-format.yml when updating
lore-format.yml
- '#max-consume#'

stats.yml
max-consume: '&7■ Max Consume: &f# &7uses'
This commit is contained in:
Blackdigits 2020-01-12 14:16:43 +07:00
parent 11d60fdc50
commit 37ce185514
4 changed files with 71 additions and 81 deletions

View File

@ -1,14 +1,5 @@
package net.Indyuce.mmoitems.api.interaction; package net.Indyuce.mmoitems.api.interaction;
import java.util.List;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.Sound;
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.inventory.ItemStack;
import net.Indyuce.mmoitems.MMOItems; import net.Indyuce.mmoitems.MMOItems;
import net.Indyuce.mmoitems.MMOUtils; import net.Indyuce.mmoitems.MMOUtils;
import net.Indyuce.mmoitems.api.ItemTier; import net.Indyuce.mmoitems.api.ItemTier;
@ -23,7 +14,17 @@ import net.Indyuce.mmoitems.stat.Upgrade_Stat.UpgradeData;
import net.Indyuce.mmoitems.stat.data.EffectListData; import net.Indyuce.mmoitems.stat.data.EffectListData;
import net.Indyuce.mmoitems.stat.type.ItemStat; import net.Indyuce.mmoitems.stat.type.ItemStat;
import net.mmogroup.mmolib.MMOLib; import net.mmogroup.mmolib.MMOLib;
import net.mmogroup.mmolib.api.item.ItemTag;
import net.mmogroup.mmolib.api.item.NBTItem; import net.mmogroup.mmolib.api.item.NBTItem;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.Sound;
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import java.util.List;
public class Consumable extends UseItem { public class Consumable extends UseItem {
public Consumable(Player player, NBTItem item, Type type) { public Consumable(Player player, NBTItem item, Type type) {
@ -215,27 +216,29 @@ public class Consumable extends UseItem {
} }
public boolean useWithoutItem(boolean consume) { public boolean useWithoutItem(boolean consume) {
if (getNBTItem().getBoolean("MMOITEMS_INEDIBLE")) NBTItem nbtItem = getNBTItem();
if (nbtItem.getBoolean("MMOITEMS_INEDIBLE"))
return false; return false;
double health = getNBTItem().getStat("RESTORE_HEALTH"); double health = nbtItem.getStat("RESTORE_HEALTH");
if (health > 0) if (health > 0)
MMOUtils.heal(player, health); MMOUtils.heal(player, health);
double food = getNBTItem().getStat("RESTORE_FOOD"); double food = nbtItem.getStat("RESTORE_FOOD");
if (food > 0) if (food > 0)
MMOUtils.feed(player, (int) food); MMOUtils.feed(player, (int) food);
double saturation = getNBTItem().getStat("RESTORE_SATURATION"); double saturation = nbtItem.getStat("RESTORE_SATURATION");
saturation = saturation == 0 ? 6 : saturation; saturation = saturation == 0 ? 6 : saturation;
if (saturation > 0) if (saturation > 0)
MMOUtils.saturate(player, (float) saturation); MMOUtils.saturate(player, (float) saturation);
double mana = getNBTItem().getStat("RESTORE_MANA"); double mana = nbtItem.getStat("RESTORE_MANA");
if (mana > 0) if (mana > 0)
playerData.getRPG().giveMana(mana); playerData.getRPG().giveMana(mana);
double stamina = getNBTItem().getStat("RESTORE_STAMINA"); double stamina = nbtItem.getStat("RESTORE_STAMINA");
if (stamina > 0) if (stamina > 0)
playerData.getRPG().giveStamina(stamina); playerData.getRPG().giveStamina(stamina);
@ -246,7 +249,46 @@ public class Consumable extends UseItem {
player.addPotionEffect(effect.toEffect()); player.addPotionEffect(effect.toEffect());
}); });
return consume && !getNBTItem().getBoolean("MMOITEMS_DISABLE_RIGHT_CLICK_CONSUME");
int maxConsume = (int) nbtItem.getStat("MAX_CONSUME");
if(maxConsume > 1) {
ItemStack item = nbtItem.toItem().clone();
String configMaxConsumeLore = ChatColor.translateAlternateColorCodes('&', MMOItems.plugin.getLanguage().getStatFormat("max-consume"));
String maxConsumeLore = configMaxConsumeLore.replace("#", Integer.toString(maxConsume));
maxConsume -= 1;
nbtItem.removeTag("MMOITEMS_MAX_CONSUME");
nbtItem.addTag(new ItemTag("MMOITEMS_MAX_CONSUME", maxConsume));
ItemStack usedItem = nbtItem.toItem().clone();
usedItem.setAmount(1);
ItemMeta usedItemMeta = usedItem.getItemMeta();
List<String> itemLores = usedItemMeta.getLore();
for(int i = 0; i < itemLores.size() ; i++) {
if(itemLores.get(i).equals(maxConsumeLore)) {
maxConsumeLore = configMaxConsumeLore.replace("#", Integer.toString(maxConsume));
itemLores.set(i, maxConsumeLore);
usedItemMeta.setLore(itemLores);
usedItem.setItemMeta(usedItemMeta);
break;
}
}
player.getInventory().setItemInMainHand(usedItem);
if(item.getAmount() > 1) {
item.setAmount(item.getAmount() - 1);
MMOUtils.giveOrDrop(player, item);
}
return false;
}
return consume && !nbtItem.getBoolean("MMOITEMS_DISABLE_RIGHT_CLICK_CONSUME");
} }
public boolean hasVanillaEating() { public boolean hasVanillaEating() {

View File

@ -1,81 +1,26 @@
package net.Indyuce.mmoitems.stat.type; package net.Indyuce.mmoitems.stat.type;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.bukkit.Material;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.inventory.ItemStack;
import net.Indyuce.mmoitems.MMOItems; import net.Indyuce.mmoitems.MMOItems;
import net.Indyuce.mmoitems.api.ConfigFile; import net.Indyuce.mmoitems.api.ConfigFile;
import net.Indyuce.mmoitems.api.item.MMOItem; import net.Indyuce.mmoitems.api.item.MMOItem;
import net.Indyuce.mmoitems.api.item.build.MMOItemBuilder; import net.Indyuce.mmoitems.api.item.build.MMOItemBuilder;
import net.Indyuce.mmoitems.gui.edition.EditionInventory; import net.Indyuce.mmoitems.gui.edition.EditionInventory;
import net.Indyuce.mmoitems.stat.Abilities; import net.Indyuce.mmoitems.stat.*;
import net.Indyuce.mmoitems.stat.Advanced_Crafting_Recipe;
import net.Indyuce.mmoitems.stat.Advanced_Crafting_Recipe_Permission;
import net.Indyuce.mmoitems.stat.Armor;
import net.Indyuce.mmoitems.stat.Armor_Toughness;
import net.Indyuce.mmoitems.stat.Arrow_Particles;
import net.Indyuce.mmoitems.stat.Attack_Damage;
import net.Indyuce.mmoitems.stat.Attack_Speed;
import net.Indyuce.mmoitems.stat.Commands;
import net.Indyuce.mmoitems.stat.Compatible_Types;
import net.Indyuce.mmoitems.stat.Crafting_Recipe;
import net.Indyuce.mmoitems.stat.CustomSounds;
import net.Indyuce.mmoitems.stat.Custom_Model_Data;
import net.Indyuce.mmoitems.stat.Disable_AdvancedEnchantments;
import net.Indyuce.mmoitems.stat.Display_Name;
import net.Indyuce.mmoitems.stat.Dye_Color;
import net.Indyuce.mmoitems.stat.Effects;
import net.Indyuce.mmoitems.stat.Elements;
import net.Indyuce.mmoitems.stat.Enchants;
import net.Indyuce.mmoitems.stat.Furnace_Recipe;
import net.Indyuce.mmoitems.stat.Gem_Color;
import net.Indyuce.mmoitems.stat.Gem_Sockets;
import net.Indyuce.mmoitems.stat.Hide_Enchants;
import net.Indyuce.mmoitems.stat.Hide_Potion_Effects;
import net.Indyuce.mmoitems.stat.Inedible;
import net.Indyuce.mmoitems.stat.Item_Particles;
import net.Indyuce.mmoitems.stat.Item_Set;
import net.Indyuce.mmoitems.stat.Item_Tier;
import net.Indyuce.mmoitems.stat.Item_Type_Restriction;
import net.Indyuce.mmoitems.stat.Knockback_Resistance;
import net.Indyuce.mmoitems.stat.Lore;
import net.Indyuce.mmoitems.stat.Lute_Attack_Effect;
import net.Indyuce.mmoitems.stat.Lute_Attack_Sound;
import net.Indyuce.mmoitems.stat.MaterialStat;
import net.Indyuce.mmoitems.stat.Max_Custom_Durability;
import net.Indyuce.mmoitems.stat.Max_Health;
import net.Indyuce.mmoitems.stat.Movement_Speed;
import net.Indyuce.mmoitems.stat.Perm_Effects;
import net.Indyuce.mmoitems.stat.Permission;
import net.Indyuce.mmoitems.stat.Pickaxe_Power;
import net.Indyuce.mmoitems.stat.Potion_Color;
import net.Indyuce.mmoitems.stat.Potion_Effects;
import net.Indyuce.mmoitems.stat.Required_Class;
import net.Indyuce.mmoitems.stat.Required_Level;
import net.Indyuce.mmoitems.stat.Restore;
import net.Indyuce.mmoitems.stat.Shapeless_Recipe;
import net.Indyuce.mmoitems.stat.Shield_Pattern;
import net.Indyuce.mmoitems.stat.Skull_Texture;
import net.Indyuce.mmoitems.stat.Soulbound;
import net.Indyuce.mmoitems.stat.Soulbound_Level;
import net.Indyuce.mmoitems.stat.Staff_Spirit;
import net.Indyuce.mmoitems.stat.Unbreakable;
import net.Indyuce.mmoitems.stat.Upgrade_Stat;
import net.Indyuce.mmoitems.stat.Vanilla_Eating_Animation;
import net.Indyuce.mmoitems.stat.Will_Break;
import net.Indyuce.mmoitems.stat.data.StatData; import net.Indyuce.mmoitems.stat.data.StatData;
import net.Indyuce.mmoitems.version.durability.stat.DefaultDurability; import net.Indyuce.mmoitems.version.durability.stat.DefaultDurability;
import net.Indyuce.mmoitems.version.durability.stat.LegacyDurability; import net.Indyuce.mmoitems.version.durability.stat.LegacyDurability;
import net.mmogroup.mmolib.MMOLib; import net.mmogroup.mmolib.MMOLib;
import net.mmogroup.mmolib.api.item.NBTItem; import net.mmogroup.mmolib.api.item.NBTItem;
import net.mmogroup.mmolib.version.VersionMaterial; import net.mmogroup.mmolib.version.VersionMaterial;
import org.bukkit.Material;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.inventory.ItemStack;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public abstract class ItemStat { public abstract class ItemStat {
public static final ItemStat MATERIAL = new MaterialStat(), DURABILITY = MMOLib.plugin.getVersion().isBelowOrEqual(1, 12) ? new LegacyDurability() : new DefaultDurability(), CUSTOM_MODEL_DATA = new Custom_Model_Data(), MAX_CUSTOM_DURABILITY = new Max_Custom_Durability(), WILL_BREAK = new Will_Break(); public static final ItemStat MATERIAL = new MaterialStat(), DURABILITY = MMOLib.plugin.getVersion().isBelowOrEqual(1, 12) ? new LegacyDurability() : new DefaultDurability(), CUSTOM_MODEL_DATA = new Custom_Model_Data(), MAX_CUSTOM_DURABILITY = new Max_Custom_Durability(), WILL_BREAK = new Will_Break();
@ -139,6 +84,7 @@ public abstract class ItemStat {
public static final ItemStat ITEM_COOLDOWN = new DoubleStat(new ItemStack(Material.COOKED_CHICKEN), "Item Cooldown", new String[] { "This cooldown applies for consumables", "as well as for item commands." }, "item-cooldown", new String[] { "!armor", "!gem_stone", "all" }); public static final ItemStat ITEM_COOLDOWN = new DoubleStat(new ItemStack(Material.COOKED_CHICKEN), "Item Cooldown", new String[] { "This cooldown applies for consumables", "as well as for item commands." }, "item-cooldown", new String[] { "!armor", "!gem_stone", "all" });
public static final ItemStat DISABLE_RIGHT_CLICK_CONSUME = new DisableStat(Material.BARRIER, "right-click-consume", "Disable Right Click Consume", new String[] { "consumable" }, "This item will not be consumed", "when eaten by players."); public static final ItemStat DISABLE_RIGHT_CLICK_CONSUME = new DisableStat(Material.BARRIER, "right-click-consume", "Disable Right Click Consume", new String[] { "consumable" }, "This item will not be consumed", "when eaten by players.");
public static final ItemStat VANILLA_EATING_ANIMATION = new Vanilla_Eating_Animation(), INEDIBLE = new Inedible(), GEM_COLOR = new Gem_Color(), ITEM_TYPE_RESTRICTION = new Item_Type_Restriction(); public static final ItemStat VANILLA_EATING_ANIMATION = new Vanilla_Eating_Animation(), INEDIBLE = new Inedible(), GEM_COLOR = new Gem_Color(), ITEM_TYPE_RESTRICTION = new Item_Type_Restriction();
public static final ItemStat MAX_CONSUME = new DoubleStat(new ItemStack(Material.BLAZE_POWDER), "Max Consume", new String[] {"Max amount of usage before", "item disappears."}, "max-consume", new String[] {"consumable"});
public static final ItemStat SUCCESS_RATE = new DoubleStat(new ItemStack(Material.EMERALD), "Success Rate", new String[] { "The chance of your gem to successfully", "apply onto an item. This value is 100%", "by default. If it is not successfully", "applied, the gem stone will be lost." }, "success-rate", new String[] { "gem_stone", "skin" }); public static final ItemStat SUCCESS_RATE = new DoubleStat(new ItemStack(Material.EMERALD), "Success Rate", new String[] { "The chance of your gem to successfully", "apply onto an item. This value is 100%", "by default. If it is not successfully", "applied, the gem stone will be lost." }, "success-rate", new String[] { "gem_stone", "skin" });
public static final ItemStat COMPATIBLE_TYPES = new Compatible_Types(); public static final ItemStat COMPATIBLE_TYPES = new Compatible_Types();

View File

@ -85,6 +85,7 @@ lore-format:
- '#skill-cooldown-reduction#' - '#skill-cooldown-reduction#'
- '#mana-cost#' - '#mana-cost#'
- '#stamina-cost#' - '#stamina-cost#'
- '#max-consume#'
# - '{bar}&8&m--------&2&l &nElements&8 &m--------------' # - '{bar}&8&m--------&2&l &nElements&8 &m--------------'
- '{bar}' - '{bar}'
- '#fire-damage#' - '#fire-damage#'

View File

@ -74,6 +74,7 @@ repair: '&7■ Repair: &f#'
can-identify: '&7■ Can identify items.' can-identify: '&7■ Can identify items.'
can-deconstruct: '&7■ Can deconstruct tiered items.' can-deconstruct: '&7■ Can deconstruct tiered items.'
success-rate: '&7■ Success Rate: &a&l#%' success-rate: '&7■ Success Rate: &a&l#%'
max-consume: '&7■ Max Consume: &f# &7uses'
# Gem Stones # Gem Stones
empty-gem-socket: '&a◆ Empty # Gem Socket' empty-gem-socket: '&a◆ Empty # Gem Socket'