Skip to content

Handling screen interactions

Maximilian Dorn edited this page Sep 2, 2022 · 1 revision

Handling screen interactions

maps 3.3.0 comes with support for handling screen interactions / screen clicks. This feature requires the maps plugin to be installed and enable-click-listener to be set to true in plugins/maps/maps_config.yml.

How it works

maps will inject a custom packet listener into each player connection. When the player sends certain interact packets, maps will intercept them and check if the player interacted with a screen. If maps detects a screen interaction it will fire a PlayerClickScreenEvent and wait for the result. If the event is cancelled, maps will drop the packet and prevent the server from ever knowing that the player sent this packet.

Listening for screen interactions

Listing for screen interactions is very simple: All you have to do is to listen to the PlayerClickScreenEvent.

Warning: This event is asynchronous. Most of Bukkits API can not be used asynchronously, such as modifying blocks or teleporting players.

public class MyListener implements Listener {

    @EventHandler
    public void onScreenClick(PlayerClickScreenEvent event) {
        Player player = event.getPlayer();
        MapScreen screen = event.getClickedScreen();
        Vec2 clickPos = event.getClickPos();
        int x = clickPos.x;
        int y = clickPos.y;
        
        player.sendMessage(String.format("You have clicked screen #%d @ %d, %d", screen.getId(), x, y));

        event.setCancelled(true);
    }

}