PlayerInteract right click with empty map

Discussion in 'Spigot Plugin Development' started by joeleoli, Jun 8, 2016.

  1. I'm trying to make a GUI, which is working fine, but when a player joins, they receive an empty map and should be able to right-click it to open the GUI instead of entering a command.

    The problem is that the map will actually be created, even if the event is canceled.

    Code (Text):

    if ((event.getAction() == Action.RIGHT_CLICK_AIR) || (event.getAction() == Action.RIGHT_CLICK_BLOCK)) {
        if (event.getPlayer().getInventory().getItemInMainHand().equals(ItemManager.mapSelector())) {
            event.getPlayer().performCommand("map");
            event.setCancelled(true);
        } else if (event.getPlayer().getInventory().getItemInMainHand() == ItemManager.mapSelector()) {
            event.getPlayer().performCommand("map");
            event.setCancelled(true);
        }
    }
     
     
  2. Well the map creation is not part of the click event, so cancelling the event won't stop the map. However, the event should fire before the game updates the map. This mean you could remove the map from their inventory and give them a new one giving the illusion that nothing happened at all.

    Another option is using a different event to cancel the map creation. This would mean you would need two events, one to detect a player using the map, and another to stop the map from being created. There is an event called MapInitializeEvent(); you can use that to stop the map from being created. Here is the link to the Spigot page for the map: https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/server/MapInitializeEvent.html#getMap()



    Here is a link to all the Events offered in the Spigot API for later reference. Just taking a little look through, you can usually find any event you're looking for: https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/Event.html