Refactor interact event handling and add trial spawner logic.
Renamed the event handler for clarity and added logic to handle trial spawner interactions with cooldown feedback for players. Introduced functional interfaces for cleaner and reusable code when processing armor stand counts and trial spawner timers.
This commit is contained in:
parent
7c04361ea5
commit
7cfdc39f75
|
|
@ -3,10 +3,15 @@ package com.alttd.playerutils.event_listeners;
|
|||
import com.alttd.playerutils.PlayerUtils;
|
||||
import com.alttd.playerutils.config.Config;
|
||||
import com.alttd.playerutils.util.Logger;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.minimessage.MiniMessage;
|
||||
import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder;
|
||||
import net.kyori.adventure.title.Title;
|
||||
import org.bukkit.Chunk;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.NamespacedKey;
|
||||
import org.bukkit.block.Block;
|
||||
import org.bukkit.block.TrialSpawner;
|
||||
import org.bukkit.entity.EntityType;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.EventHandler;
|
||||
|
|
@ -35,8 +40,13 @@ public class LimitArmorStands implements Listener {
|
|||
@NotNull PersistentDataContainer persistentDataContainer);
|
||||
}
|
||||
|
||||
@FunctionalInterface
|
||||
private interface TrialSpawnerTimerConsumer {
|
||||
void apply(int ticks, @NotNull Player player);
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onArmorStandSpawn(PlayerInteractEvent event) {
|
||||
public void onPlayerInteractEvent(PlayerInteractEvent event) {
|
||||
handleArmorStandPlacing(event, (armorStandCount, player, namespacedKey, persistentDataContainer) -> {
|
||||
int max = 0;
|
||||
for (String permission : Config.ARMOR_STAND_LIMIT.LIMIT.keySet()) {
|
||||
|
|
@ -52,6 +62,16 @@ public class LimitArmorStands implements Listener {
|
|||
}
|
||||
persistentDataContainer.set(namespacedKey, PersistentDataType.INTEGER, ++armorStandCount);
|
||||
});
|
||||
|
||||
if (event.getPlayer().hasPermission("pu.trial_spawner.cooldown")) {
|
||||
handleRightClickTrialSpawner(event, (ticks, player) -> {
|
||||
MiniMessage miniMessage = MiniMessage.miniMessage();
|
||||
Title title = Title.title(Component.empty(),
|
||||
miniMessage.deserialize("<green>Cooldown <red><seconds></red></green>",
|
||||
Placeholder.parsed("seconds", String.valueOf(ticks / 20))));
|
||||
player.showTitle(title);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
private void handleArmorStandPlacing(PlayerInteractEvent event, ArmorStandCountConsumer consumer) {
|
||||
|
|
@ -89,6 +109,36 @@ public class LimitArmorStands implements Listener {
|
|||
consumer.apply(armorStandCount, event.getPlayer(), namespacedKey, persistentDataContainer);
|
||||
}
|
||||
|
||||
private void handleRightClickTrialSpawner(PlayerInteractEvent event, TrialSpawnerTimerConsumer consumer) {
|
||||
if (event.getAction() != Action.RIGHT_CLICK_BLOCK) {
|
||||
return;
|
||||
}
|
||||
if (!event.getMaterial().equals(Material.AIR)) {
|
||||
return;
|
||||
}
|
||||
if (event.getClickedBlock() == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
Block block = event.getClickedBlock();
|
||||
if (!block.getType().equals(Material.TRIAL_SPAWNER)) {
|
||||
return;
|
||||
}
|
||||
if (!(block instanceof TrialSpawner trialSpawner)) {
|
||||
return;
|
||||
}
|
||||
if (!(trialSpawner.getBlockData() instanceof org.bukkit.block.data.type.TrialSpawner trialSpawnerData)) {
|
||||
return;
|
||||
}
|
||||
Player player = event.getPlayer();
|
||||
if (!trialSpawnerData.getTrialSpawnerState().equals(org.bukkit.block.data.type.TrialSpawner.State.INACTIVE)) {
|
||||
consumer.apply(0, player);
|
||||
return;
|
||||
}
|
||||
|
||||
consumer.apply(trialSpawner.getCooldownLength(), player);
|
||||
}
|
||||
|
||||
private int countArmorStands(Chunk chunk) {
|
||||
return (int) Arrays.stream(chunk.getEntities())
|
||||
.filter(entity -> entity.getType().equals(EntityType.ARMOR_STAND))
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user