enchanted apple cooldown
This commit is contained in:
@@ -2,49 +2,22 @@ package fun.ogre.ogredupealias.events;
|
|||||||
|
|
||||||
import fun.ogre.ogredupealias.data.PlacedStructures;
|
import fun.ogre.ogredupealias.data.PlacedStructures;
|
||||||
import fun.ogre.ogredupealias.plugin.InventoryPresets;
|
import fun.ogre.ogredupealias.plugin.InventoryPresets;
|
||||||
import fun.ogre.ogredupealias.utils.Cooldown;
|
|
||||||
import fun.ogre.ogredupealias.utils.Text;
|
|
||||||
import org.bukkit.Material;
|
|
||||||
import org.bukkit.block.Block;
|
import org.bukkit.block.Block;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
import org.bukkit.event.EventHandler;
|
import org.bukkit.event.EventHandler;
|
||||||
import org.bukkit.event.Listener;
|
import org.bukkit.event.Listener;
|
||||||
import org.bukkit.event.block.Action;
|
|
||||||
import org.bukkit.event.player.PlayerInteractEvent;
|
import org.bukkit.event.player.PlayerInteractEvent;
|
||||||
import org.bukkit.inventory.ItemStack;
|
|
||||||
|
|
||||||
import java.util.UUID;
|
|
||||||
|
|
||||||
public class InteractionListener implements Listener {
|
public class InteractionListener implements Listener {
|
||||||
|
|
||||||
private static final Cooldown<UUID> aAppleCooldown = new Cooldown<>();
|
|
||||||
|
|
||||||
@EventHandler
|
@EventHandler
|
||||||
public void onClick(PlayerInteractEvent e) {
|
public void onClick(PlayerInteractEvent e) {
|
||||||
try {
|
try {
|
||||||
this.processTable(e);
|
this.processTable(e);
|
||||||
this.handleEApples(e);
|
|
||||||
}
|
}
|
||||||
catch (Exception ignore) {}
|
catch (Exception ignore) {}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void handleEApples(PlayerInteractEvent e) {
|
|
||||||
final Player p = e.getPlayer();
|
|
||||||
final ItemStack item = e.getItem();
|
|
||||||
final Action a = e.getAction();
|
|
||||||
|
|
||||||
if (!a.name().contains("RIGHT_CLICK")) return;
|
|
||||||
if (item == null) return;
|
|
||||||
if (item.getType() != Material.ENCHANTED_GOLDEN_APPLE) return;
|
|
||||||
if (aAppleCooldown.isOnCooldown(p.getUniqueId())) {
|
|
||||||
e.setCancelled(true);
|
|
||||||
p.sendMessage(Text.ofAll("&cEnchanted Golden Apples are on cooldown!"));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
aAppleCooldown.setCooldown(p.getUniqueId(), 30 * 1000);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void processTable(PlayerInteractEvent e) {
|
private void processTable(PlayerInteractEvent e) {
|
||||||
final Player p = e.getPlayer();
|
final Player p = e.getPlayer();
|
||||||
final Block b = e.getClickedBlock();
|
final Block b = e.getClickedBlock();
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package fun.ogre.ogredupealias.events;
|
|||||||
import fun.ogre.ogredupealias.data.Config;
|
import fun.ogre.ogredupealias.data.Config;
|
||||||
import fun.ogre.ogredupealias.utils.ItemUtils;
|
import fun.ogre.ogredupealias.utils.ItemUtils;
|
||||||
import fun.ogre.ogredupealias.plugin.ItemPresets;
|
import fun.ogre.ogredupealias.plugin.ItemPresets;
|
||||||
|
import org.bukkit.Material;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
import org.bukkit.event.EventHandler;
|
import org.bukkit.event.EventHandler;
|
||||||
import org.bukkit.event.Listener;
|
import org.bukkit.event.Listener;
|
||||||
@@ -30,9 +31,22 @@ public class PlayerEventListener implements Listener {
|
|||||||
|
|
||||||
@EventHandler
|
@EventHandler
|
||||||
public void onConsume(PlayerItemConsumeEvent e) {
|
public void onConsume(PlayerItemConsumeEvent e) {
|
||||||
|
try {
|
||||||
|
this.handleFood(e);
|
||||||
|
}
|
||||||
|
catch (Exception ignore) {}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void handleFood(PlayerItemConsumeEvent e) {
|
||||||
final ItemStack item = e.getItem();
|
final ItemStack item = e.getItem();
|
||||||
final Player p = e.getPlayer();
|
final Player p = e.getPlayer();
|
||||||
|
|
||||||
if (ItemUtils.nbtMatches(item,ItemPresets.SNAD_COOKIE)) p.setHealth(0);
|
if (ItemUtils.nbtMatches(item,ItemPresets.SNAD_COOKIE)) {
|
||||||
|
p.setHealth(0);
|
||||||
|
}
|
||||||
|
else if (item.getType() == Material.ENCHANTED_GOLDEN_APPLE) {
|
||||||
|
if (e.isCancelled()) return;
|
||||||
|
p.setCooldown(Material.ENCHANTED_GOLDEN_APPLE, 30 * 20);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -19,6 +19,11 @@ public class Cooldown<T> {
|
|||||||
return Math.max(getOrDefault(timer.get(obj), 0L) - System.currentTimeMillis(), 0L);
|
return Math.max(getOrDefault(timer.get(obj), 0L) - System.currentTimeMillis(), 0L);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public double getCooldownSec(T obj) {
|
||||||
|
final long cooldown = this.getCooldown(obj);
|
||||||
|
return MathUtils.round(cooldown / 1000.0, 100);
|
||||||
|
}
|
||||||
|
|
||||||
public boolean isOnCooldown(T obj) {
|
public boolean isOnCooldown(T obj) {
|
||||||
return getCooldown(obj) > 0L;
|
return getCooldown(obj) > 0L;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
package fun.ogre.ogredupealias.utils;
|
package fun.ogre.ogredupealias.utils;
|
||||||
|
|
||||||
|
import net.md_5.bungee.api.ChatMessageType;
|
||||||
|
import net.md_5.bungee.api.chat.TextComponent;
|
||||||
import org.bukkit.Bukkit;
|
import org.bukkit.Bukkit;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
|
|
||||||
@@ -44,4 +46,8 @@ public class ServerUtils {
|
|||||||
public static void forEachSpecified(Consumer<Player> consumer, Player... players) {
|
public static void forEachSpecified(Consumer<Player> consumer, Player... players) {
|
||||||
Arrays.stream(players).forEach(consumer);
|
Arrays.stream(players).forEach(consumer);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void sendActionBar(Player p, String msg) {
|
||||||
|
p.spigot().sendMessage(ChatMessageType.ACTION_BAR, TextComponent.fromLegacyText(msg));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user