enchanted apple cooldown

This commit is contained in:
ImproperIssues
2023-05-06 16:44:48 -07:00
parent ace526af6a
commit c1691774b9
4 changed files with 26 additions and 28 deletions

View File

@@ -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<UUID> 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();

View File

@@ -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);
}
}
}

View File

@@ -19,6 +19,11 @@ public class Cooldown<T> {
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;
}

View File

@@ -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<Player> 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));
}
}