Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package remvn.xenchantmentpack.armor;
- import org.bukkit.Sound;
- import org.bukkit.configuration.file.YamlConfiguration;
- import org.bukkit.entity.Player;
- import org.bukkit.event.entity.EntityDamageEvent;
- import org.bukkit.inventory.ItemStack;
- import org.bukkit.potion.PotionEffect;
- import org.bukkit.potion.PotionEffectType;
- import remvn.xenchantment.XEnchant;
- import remvn.xenchantment.XEnchantment;
- import remvn.xenchantment.api.EnchantEntry;
- import remvn.xlibrary.itemstack.ItemSet;
- import java.util.List;
- import java.util.Map;
- import java.util.concurrent.ConcurrentHashMap;
- import java.util.stream.Collectors;
- public class Startle extends XEnchant {
- protected double baseCooldown = 8.5, decrementCooldown = 0.5, minCooldown = 1;
- private double duration = 1, incrementDuration = 0.5, maxDuration = 7;
- private final Map<String, Long> lastUsed = new ConcurrentHashMap<>();
- public Startle() {
- super("Startle");
- setDisplayName(getName());
- addNaturalItems(ItemSet.HELMETS.getItems());
- setStackable(true);
- }
- @Override
- public void loadConfig(YamlConfiguration config) {
- super.loadConfig(config);
- baseCooldown = config.getDouble("baseCooldown", baseCooldown);
- decrementCooldown = config.getDouble("decrementCooldown", decrementCooldown);
- minCooldown = config.getDouble("minCooldown", minCooldown);
- duration = config.getDouble("duration", duration);
- maxDuration = config.getDouble("maxDuration", maxDuration);
- incrementDuration = config.getDouble("incrementDuration", incrementDuration);
- setEnchantable(config.getBoolean("enable"));
- }
- public double getDuration(int level) {
- return Math.min(maxDuration, duration + incrementDuration * (level - 1));
- }
- @Override
- public List<String> getDescription(int level) {
- List<String> desc = super.getDescription(level);
- double time = getCooldown(level);
- return desc.stream().map(x -> x.replace("%cooldown%", time + "")
- .replace("%duration%", duration + ""))
- .collect(Collectors.toList());
- }
- @Override
- public void applyDamaged(Player user, EnchantEntry entry, EntityDamageEvent event) {
- int count = 0;
- int duration = 0;
- long cooldown = 0;
- for (ItemStack item : user.getInventory().getArmorContents()) {
- EnchantEntry e = XEnchantment.getXEnchantAPI().getEnchant(item, getID());
- if (e != null && e.getLevel() > 0) {
- count++;
- int subDuration = (int) (getDuration(e.getLevel()) * 20);
- long subCooldown = getCooldown(e.getLevel());
- duration += subDuration;
- cooldown += subCooldown;
- }
- }
- if (count < 1) return;
- duration /= count;
- cooldown /= count;
- boolean used = use(user,entry, cooldown);
- if (!used) {
- lastUsed.put(user.getUniqueId().toString(), System.currentTimeMillis());
- }
- if (used) {
- user.addPotionEffect(new PotionEffect(PotionEffectType.ABSORPTION, duration,1));
- user.addPotionEffect(new PotionEffect(PotionEffectType.DAMAGE_RESISTANCE, duration,1));
- user.playSound(user.getEyeLocation(), Sound.ENTITY_FOX_HURT, 1f, 1f);
- }
- }
- public long getCooldown(int level) {
- double coolDown = baseCooldown - (decrementCooldown * (level - 1));
- if (coolDown < minCooldown) {
- coolDown = minCooldown;
- }
- return (long) (coolDown * 1000);
- }
- public boolean use(Player user, EnchantEntry entry, long value) {
- String uuid = user.getUniqueId().toString();
- long lastUsed = this.lastUsed.getOrDefault(uuid, 0L);
- long coolDown = value;
- if (System.currentTimeMillis() - lastUsed < coolDown) return false;
- this.lastUsed.put(uuid, System.currentTimeMillis());
- return true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment