Add GoatHornEvent listener (stops spamming goat horns in spawn)

Implement GoatHornEvent to handle player interactions with goat horns. This feature sets a cooldown for goat horn items, if the player is near spawn or already has an active cooldown.
This commit is contained in:
Teriuihi 2024-08-11 23:48:35 +02:00
parent 4ed83b6376
commit 0c305e79e5

View File

@ -0,0 +1,43 @@
package com.alttd.playerutils.event_listeners;
import org.bukkit.Location;
import org.bukkit.Tag;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.inventory.ItemStack;
import java.util.concurrent.TimeUnit;
public class GoatHornEvent implements Listener {
@EventHandler
public void onPlayerInteract(PlayerInteractEvent event) {
ItemStack item = event.getItem();
if (item == null) {
return;
}
if (!Tag.SNAPS_GOAT_HORN.isTagged(event.getItem().getType())) {
return;
}
Player player = event.getPlayer();
if (player.getCooldown(item.getType()) > 0) {
event.setCancelled(true);
return;
}
Location spawn = player.getLocation();
spawn.set(0, 0, 0);
if (player.getLocation().distance(spawn) > 250) {
return;
}
Tag.SNAPS_GOAT_HORN.getValues().forEach(horn ->
player.setCooldown(horn, (int) TimeUnit.MINUTES.toSeconds(5) * 20));
}
}