64 lines
2.1 KiB
Java
64 lines
2.1 KiB
Java
package me.loganb1max.minions.manager;
|
|
|
|
import lombok.Getter;
|
|
import me.loganb1max.minions.MinionsPlugin;
|
|
import me.loganb1max.minions.menu.MinionMenu;
|
|
import me.loganb1max.minions.model.Minion;
|
|
import me.loganb1max.minions.util.Text;
|
|
import org.bukkit.Bukkit;
|
|
import org.bukkit.entity.ArmorStand;
|
|
import org.bukkit.event.EventHandler;
|
|
import org.bukkit.event.EventPriority;
|
|
import org.bukkit.event.HandlerList;
|
|
import org.bukkit.event.Listener;
|
|
import org.bukkit.event.entity.EntityDamageEvent;
|
|
import org.bukkit.event.player.PlayerArmorStandManipulateEvent;
|
|
import org.bukkit.scheduler.BukkitTask;
|
|
import java.util.HashSet;
|
|
import java.util.Objects;
|
|
import java.util.Set;
|
|
|
|
public class MinionManager implements Listener {
|
|
|
|
private final MinionsPlugin plugin;
|
|
private BukkitTask animationTask;
|
|
|
|
@Getter private Set<Minion> minions = new HashSet<>();
|
|
|
|
public MinionManager(final MinionsPlugin plugin) {
|
|
this.plugin = plugin;
|
|
this.animationTask = Bukkit.getScheduler().runTaskTimer(this.plugin, () -> this.minions.forEach(Minion::tick), 10, 10);
|
|
Bukkit.getPluginManager().registerEvents(this, this.plugin);
|
|
}
|
|
|
|
public void close() {
|
|
this.animationTask.cancel();
|
|
HandlerList.unregisterAll(this);
|
|
}
|
|
|
|
@EventHandler(priority = EventPriority.HIGHEST)
|
|
public void onDamage(final EntityDamageEvent e) {
|
|
if (!(e.getEntity() instanceof ArmorStand)) return;
|
|
if (this.minions.stream().map(Minion::getId).filter(Objects::nonNull).anyMatch(uuid -> uuid.equals(e.getEntity().getUniqueId()))) {
|
|
e.setCancelled(true);
|
|
}
|
|
}
|
|
|
|
@EventHandler(priority = EventPriority.HIGHEST)
|
|
public void onRightClick(final PlayerArmorStandManipulateEvent e) {
|
|
this.minions.stream().filter(minion -> minion.getId().equals(e.getRightClicked().getUniqueId())).findFirst().ifPresent(minion -> {
|
|
e.setCancelled(true);
|
|
if (!minion.getOwner().equals(e.getPlayer().getUniqueId())) {
|
|
e.getPlayer().sendMessage(Text.color("&cOnly the owner of the minion can interact with it."));
|
|
return;
|
|
}
|
|
if (e.getPlayer().isSneaking()) {
|
|
minion.pickup(e.getPlayer());
|
|
return;
|
|
}
|
|
new MinionMenu(minion, e.getPlayer());
|
|
});
|
|
}
|
|
|
|
}
|