Skip to content

Commit

Permalink
closes #59 editmode itemframes and equipable entities
Browse files Browse the repository at this point in the history
  • Loading branch information
bale1017 committed Oct 31, 2023
1 parent 06449ea commit a974b0d
Showing 1 changed file with 26 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerInteractAtEntityEvent;
import org.bukkit.event.player.PlayerInteractEvent;

import java.util.*;
Expand All @@ -48,6 +49,7 @@ public class DefaultNodeGroupEditor implements NodeGroupEditor<Player>, GraphRen

private final Collection<GraphRenderer<Player>> renderers;
private final Collection<de.cubbossa.pathapi.event.Listener<?>> listeners;
private final EntityInteractListener entityInteractListener = new EntityInteractListener();

private final ExecutorService renderExecutor;

Expand All @@ -67,6 +69,7 @@ public DefaultNodeGroupEditor(NodeGroup group) {
listeners.add(eventDispatcher.listen(NodeCreateEvent.class, e -> renderAll(e.getNode())));
listeners.add(eventDispatcher.listen(NodeSaveEvent.class, e -> renderAll(e.getNode())));
listeners.add(eventDispatcher.listen(NodeDeleteEvent.class, e -> eraseAll(e.getNode())));
Bukkit.getPluginManager().registerEvents(entityInteractListener, PathFinderPlugin.getInstance());

eventDispatcher.listen(NodeGroupDeleteEvent.class, event -> {
if (!event.getGroup().getKey().equals(groupKey)) {
Expand Down Expand Up @@ -122,6 +125,7 @@ public void onInteract(PlayerInteractEvent event) {
public void dispose() {
PlayerInteractEvent.getHandlerList().unregister(this);
listeners.forEach(pathFinder.getEventDispatcher()::drop);
entityInteractListener.close();

for (GraphRenderer<Player> renderer : renderers) {
renderer.close();
Expand Down Expand Up @@ -230,4 +234,26 @@ public CompletableFuture<Void> eraseNodes(PathPlayer<Player> player, Collection<
return null;
});
}

private class EntityInteractListener implements Listener, AutoCloseable {
@EventHandler
public void onInteract(PlayerInteractAtEntityEvent e) {
PathPlayer<Player> player = BukkitUtils.wrap(e.getPlayer());
if (!editingPlayers.containsKey(player)) {
return;
}
int slot = e.getPlayer().getInventory().getHeldItemSlot();
// slots 0-5 are part of the editmode -> cancel interaction
// it does not affect interacting with nodes or edges, those are handled beforehand
// and are no valid entities, hence they don't trigger actual Events
if (slot < 5) {
e.setCancelled(true);
}
}

@Override
public void close() throws Exception {
PlayerInteractAtEntityEvent.getHandlerList().unregister(this);
}
}
}

0 comments on commit a974b0d

Please sign in to comment.