PlasmaVNGaming

Untitled

Aug 18th, 2021
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.01 KB | None | 0 0
  1. package remvn.xenchantmentpack.armor;
  2.  
  3. import org.bukkit.Sound;
  4. import org.bukkit.configuration.file.YamlConfiguration;
  5. import org.bukkit.entity.Player;
  6. import org.bukkit.event.entity.EntityDamageEvent;
  7. import org.bukkit.inventory.ItemStack;
  8. import org.bukkit.potion.PotionEffect;
  9. import org.bukkit.potion.PotionEffectType;
  10. import remvn.xenchantment.XEnchant;
  11. import remvn.xenchantment.XEnchantment;
  12. import remvn.xenchantment.api.EnchantEntry;
  13. import remvn.xlibrary.itemstack.ItemSet;
  14.  
  15. import java.util.List;
  16. import java.util.Map;
  17. import java.util.concurrent.ConcurrentHashMap;
  18. import java.util.stream.Collectors;
  19.  
  20. public class Startle extends XEnchant {
  21.  
  22. protected double baseCooldown = 8.5, decrementCooldown = 0.5, minCooldown = 1;
  23. private double duration = 1, incrementDuration = 0.5, maxDuration = 7;
  24. private final Map<String, Long> lastUsed = new ConcurrentHashMap<>();
  25.  
  26. public Startle() {
  27. super("Startle");
  28. setDisplayName(getName());
  29. addNaturalItems(ItemSet.HELMETS.getItems());
  30. setStackable(true);
  31. }
  32.  
  33. @Override
  34. public void loadConfig(YamlConfiguration config) {
  35. super.loadConfig(config);
  36. baseCooldown = config.getDouble("baseCooldown", baseCooldown);
  37. decrementCooldown = config.getDouble("decrementCooldown", decrementCooldown);
  38. minCooldown = config.getDouble("minCooldown", minCooldown);
  39. duration = config.getDouble("duration", duration);
  40. maxDuration = config.getDouble("maxDuration", maxDuration);
  41. incrementDuration = config.getDouble("incrementDuration", incrementDuration);
  42. setEnchantable(config.getBoolean("enable"));
  43. }
  44.  
  45. public double getDuration(int level) {
  46. return Math.min(maxDuration, duration + incrementDuration * (level - 1));
  47. }
  48.  
  49. @Override
  50. public List<String> getDescription(int level) {
  51. List<String> desc = super.getDescription(level);
  52. double time = getCooldown(level);
  53. return desc.stream().map(x -> x.replace("%cooldown%", time + "")
  54. .replace("%duration%", duration + ""))
  55. .collect(Collectors.toList());
  56. }
  57.  
  58. @Override
  59. public void applyDamaged(Player user, EnchantEntry entry, EntityDamageEvent event) {
  60. int count = 0;
  61. int duration = 0;
  62. long cooldown = 0;
  63. for (ItemStack item : user.getInventory().getArmorContents()) {
  64. EnchantEntry e = XEnchantment.getXEnchantAPI().getEnchant(item, getID());
  65. if (e != null && e.getLevel() > 0) {
  66. count++;
  67.  
  68. int subDuration = (int) (getDuration(e.getLevel()) * 20);
  69. long subCooldown = getCooldown(e.getLevel());
  70.  
  71. duration += subDuration;
  72. cooldown += subCooldown;
  73. }
  74. }
  75.  
  76. if (count < 1) return;
  77. duration /= count;
  78. cooldown /= count;
  79.  
  80. boolean used = use(user,entry, cooldown);
  81. if (!used) {
  82. lastUsed.put(user.getUniqueId().toString(), System.currentTimeMillis());
  83. }
  84.  
  85. if (used) {
  86. user.addPotionEffect(new PotionEffect(PotionEffectType.ABSORPTION, duration,1));
  87. user.addPotionEffect(new PotionEffect(PotionEffectType.DAMAGE_RESISTANCE, duration,1));
  88. user.playSound(user.getEyeLocation(), Sound.ENTITY_FOX_HURT, 1f, 1f);
  89. }
  90. }
  91.  
  92. public long getCooldown(int level) {
  93. double coolDown = baseCooldown - (decrementCooldown * (level - 1));
  94. if (coolDown < minCooldown) {
  95. coolDown = minCooldown;
  96. }
  97. return (long) (coolDown * 1000);
  98. }
  99.  
  100. public boolean use(Player user, EnchantEntry entry, long value) {
  101. String uuid = user.getUniqueId().toString();
  102. long lastUsed = this.lastUsed.getOrDefault(uuid, 0L);
  103. long coolDown = value;
  104. if (System.currentTimeMillis() - lastUsed < coolDown) return false;
  105. this.lastUsed.put(uuid, System.currentTimeMillis());
  106. return true;
  107. }
  108.  
  109. }
  110.  
Advertisement
Add Comment
Please, Sign In to add comment