Cancelling the book GUI open event?

Discussion in 'Plugin Development' started by Hanii Puppy, Jun 7, 2013.

Thread Status:
Not open for further replies.
  1. My plugin revolves around being able to cast spells by using books containing instructions for how the spell is supposed to work. Part of the problem though, is that I didn't know how to cancel the book GUI open event.

    At the moment, I'm listening to the player interaction event and when it fires, immediately closing the GUI afterwords, but that still introduces some problems like the book GUI appearing for a second on screen, the GUI cancelling people running/flying/etc, and so forth.

    I tried cancelling the player interaction event when the player is supposed to be using a spell book, but the GUI still opens. Is there anything I can do? Thanks for any help ^_^

    Current code:


    Code:
    public class PlayerInteractionListener implements Listener
    {
        @EventHandler
        public void onPlayerInteract(PlayerInteractEvent event)
        {
            Player player = event.getPlayer();
            Action action = event.getAction();
           
            if(action.equals(Action.RIGHT_CLICK_BLOCK))
            {
                if(player.getItemInHand().getTypeId() == 387) // If player is holding a written book.
                {
                    event.setCancelled(true);
                    onBookRead(event);
                }
                else
                {
                    < unrelated code >
                }
            }
            else if(action.equals(Action.LEFT_CLICK_BLOCK))
            {
                < unrelated code >
            }
            else if(action.equals(Action.RIGHT_CLICK_AIR))
            {
                if(player.getItemInHand().getTypeId() == 387) // If player is holding a written book.
                {
                    event.setCancelled(true);
                    onBookRead(event);
                }
            }
        }
     
        void onBookRead(PlayerInteractEvent event)
        {
            Player player = event.getPlayer();
            BookMeta book = (BookMeta)event.getItem().getItemMeta();
           
            if(SpellInterpreter.isSpellBook(book))
            {
                player.closeInventory();
                SpellInterpreter.InterpretBook(book, event.getPlayer());
            }
        }
    }
     
  2. Offline

    NoLiver92

    try inventoryopenevent()
    check what inventory was opened then cancel it
     
  3. Offline

    desht

    Nope. You can't cancel book reading, because it's done entirely client side.
     
    Hoolean likes this.
  4. Offline

    kreashenz

    desht Couldn't you open it and set the text inside it completely blank? Or is almost all book stuff done client side?
     
  5. I just tried that now and didn't stop the GUI from appearing on screen :\
     
  6. Offline

    desht

    You could blank out the book, yeah, but that wouldn't stop the GUI appearing, and I suspect the client will show the book's existing text before the server gets a chance to respond to the PlayerInteractEvent and update the book's contents - I don't know if changing the item's metadata while the GUI is open will update the GUI, but I'm guessing not.

    Book reading is done entirely client-side (book editing & signing does send packets to the server, but there aren't any Bukkit events for that... yet).
    No, it wouldn't.
     
  7. Offline

    Technius

    desht
    Actually, I cancelled a PlayerInteractEvent in one of my plugins and it happened to close the book for me. See this.

    Hanii Puppy
    Your checking method should return a boolean value of whether or not the event should be cancelled. For example:
    Code:
    if(checkBook(player.getItemInHand())event.setCancelled(true);
    //Unrelated sections
     
    public boolean checkBook(ItemStack item)
    {
         //EXAMPLE
        if(!item.hasItemMeta())return false;
        BookMeta book = (BookMeta)item.getItemMeta();
        runSpell(book);
        return true;
    }
    
     
  8. I'm needing the UI to not appear at all :\ The UI appearing interrupts players running/flying/etc., and given that one of the applications of spells in my plugin is combat, this is somewhat of a disadvantage.

    And @ the second bit: I should have thought about that <.<; I'll replace the cancellation to be in the block of code along with the closing of the inventory and the book interpretation. Thankyou ^^;
     
  9. Offline

    Technius

    Hanii Puppy
    Well, this is one of those limitations in Minecraft that we can't overcome... yet. You could use normal books instead. That might work. Except that they don't have BookMeta.
     
  10. Offline

    skore87

    Just thought of another hackish way to maybe get the desired effect. You could swap the item in-hand with a normal book when the event happens and with a 1 tick delay restore it to the original item.
     
  11. o3o I like that idea if it works! I haven't implemented cool-downs yet and I was going to have just one cool-down for all effects, but if I do that, I could kill two birds with one stone and also have a per-spell cooldown.
     
  12. Offline

    skore87

    Lets hope then ;).
     
  13. I got a chance to try it. Even if the first thing I do in the listener is replace the item, the GUI still opens :\
     
  14. Offline

    rsod

    No way, it's controlled by client
     
Thread Status:
Not open for further replies.

Share This Page