Guest User

:(

a guest
Sep 12th, 2019
94
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package lb.jerry.Cooldowns.Listeners;
  2.  
  3.  
  4. import lb.jerry.Cooldowns.Cooldowns;
  5. import org.bukkit.ChatColor;
  6. import org.bukkit.Material;
  7. import org.bukkit.entity.Player;
  8. import org.bukkit.event.EventHandler;
  9. import org.bukkit.event.Listener;
  10. import org.bukkit.event.player.PlayerInteractEvent;
  11. import org.bukkit.scheduler.BukkitRunnable;
  12.  
  13. import java.text.DecimalFormat;
  14. import java.util.UUID;
  15.  
  16. public class PearlCooldown implements Listener {
  17.  
  18. private Cooldowns plugin;
  19.  
  20. public PearlCooldown(Cooldowns plugin) {
  21. this.plugin = plugin;
  22. }
  23.  
  24.  
  25. private int timeleftpearl = pearlcooldownseconds();
  26.  
  27.  
  28. private boolean hasCooldown(UUID uuid) {
  29. if (!plugin.activepearlcooldown.containsKey(uuid)) {
  30. return false;
  31. }
  32. long value = plugin.activepearlcooldown.get(uuid);
  33. return value > System.currentTimeMillis();
  34. }
  35.  
  36. private long getMillisecondsLeft(UUID uuid) {
  37. if (!plugin.activepearlcooldown.containsKey(uuid)) {
  38. return -1L;
  39. }
  40. return plugin.activepearlcooldown.get(uuid) - System.currentTimeMillis();
  41. }
  42.  
  43. private void addCooldown(UUID uuid) {
  44. long value = System.currentTimeMillis() + timeleftpearl * 1000L;
  45. plugin.activepearlcooldown.put(uuid, value);
  46. }
  47.  
  48. @EventHandler
  49. public void onUseEnderpearl(PlayerInteractEvent event) {
  50. if (event.getItem() == null) return;
  51. if (event.getItem().getType() != Material.ENDER_PEARL) return;
  52. Player player = event.getPlayer();
  53. if (BlockedItems(event.getItem().getType())) return;
  54. if (hasCooldown(player.getUniqueId())) {
  55. event.setCancelled(true);
  56. player.sendMessage(colour(plugin.getConfig().getString("pearl-cooldown-format").replace("%seconds%", new DecimalFormat("0.0").format(getMillisecondsLeft(player.getUniqueId()) / 1000.0))));
  57. return;
  58. }
  59. addCooldown(player.getUniqueId());
  60. new BukkitRunnable() {
  61. @Override
  62. public void run() {
  63. player.sendMessage(colour(plugin.getConfig().getString("pearl-cooldown-over")));
  64. }
  65. }.runTaskLater(plugin, pearlcooldownseconds());
  66. }
  67.  
  68. private static String colour(String message) {
  69. return ChatColor.translateAlternateColorCodes('&', message);
  70. }
  71.  
  72. private int pearlcooldownseconds() {
  73. assert plugin != null;
  74. return (plugin.getConfig().getInt("pearl-cooldown-time"));
  75. }
  76.  
  77. private boolean BlockedItems(Material type) {
  78. String typeName = type.name();
  79. if (typeName.endsWith("BUTTON")) return true;
  80. if (typeName.equals("WORKBENCH")) return true;
  81. if (typeName.equals("ENDER_CHEST")) return true;
  82. return true;
  83. }
  84. }
RAW Paste Data