diff --git a/src/main/java/fun/ogre/ogredupealias/events/InteractionListener.java b/src/main/java/fun/ogre/ogredupealias/events/InteractionListener.java index e1c3866..5bc8c83 100644 --- a/src/main/java/fun/ogre/ogredupealias/events/InteractionListener.java +++ b/src/main/java/fun/ogre/ogredupealias/events/InteractionListener.java @@ -2,49 +2,22 @@ package fun.ogre.ogredupealias.events; import fun.ogre.ogredupealias.data.PlacedStructures; 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.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; -import org.bukkit.event.block.Action; import org.bukkit.event.player.PlayerInteractEvent; -import org.bukkit.inventory.ItemStack; - -import java.util.UUID; public class InteractionListener implements Listener { - private static final Cooldown aAppleCooldown = new Cooldown<>(); - @EventHandler public void onClick(PlayerInteractEvent e) { try { this.processTable(e); - this.handleEApples(e); } 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) { final Player p = e.getPlayer(); final Block b = e.getClickedBlock(); diff --git a/src/main/java/fun/ogre/ogredupealias/events/PlayerEventListener.java b/src/main/java/fun/ogre/ogredupealias/events/PlayerEventListener.java index 799c842..a1722ae 100644 --- a/src/main/java/fun/ogre/ogredupealias/events/PlayerEventListener.java +++ b/src/main/java/fun/ogre/ogredupealias/events/PlayerEventListener.java @@ -3,6 +3,7 @@ package fun.ogre.ogredupealias.events; import fun.ogre.ogredupealias.data.Config; import fun.ogre.ogredupealias.utils.ItemUtils; import fun.ogre.ogredupealias.plugin.ItemPresets; +import org.bukkit.Material; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; @@ -30,9 +31,22 @@ public class PlayerEventListener implements Listener { @EventHandler public void onConsume(PlayerItemConsumeEvent e) { + try { + this.handleFood(e); + } + catch (Exception ignore) {} + } + + private void handleFood(PlayerItemConsumeEvent e) { final ItemStack item = e.getItem(); 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); + } } } diff --git a/src/main/java/fun/ogre/ogredupealias/utils/Cooldown.java b/src/main/java/fun/ogre/ogredupealias/utils/Cooldown.java index aa29583..f4549b6 100644 --- a/src/main/java/fun/ogre/ogredupealias/utils/Cooldown.java +++ b/src/main/java/fun/ogre/ogredupealias/utils/Cooldown.java @@ -19,6 +19,11 @@ public class Cooldown { 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) { return getCooldown(obj) > 0L; } diff --git a/src/main/java/fun/ogre/ogredupealias/utils/ServerUtils.java b/src/main/java/fun/ogre/ogredupealias/utils/ServerUtils.java index 2b5b79f..c78a827 100644 --- a/src/main/java/fun/ogre/ogredupealias/utils/ServerUtils.java +++ b/src/main/java/fun/ogre/ogredupealias/utils/ServerUtils.java @@ -1,5 +1,7 @@ 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.entity.Player; @@ -44,4 +46,8 @@ public class ServerUtils { public static void forEachSpecified(Consumer consumer, Player... players) { Arrays.stream(players).forEach(consumer); } + + public static void sendActionBar(Player p, String msg) { + p.spigot().sendMessage(ChatMessageType.ACTION_BAR, TextComponent.fromLegacyText(msg)); + } }