Solved How to properly save the Content of Item Frames

Discussion in 'Plugin Development' started by TheA13X, May 22, 2016.

Thread Status:
Not open for further replies.
  1. Offline

    TheA13X

    While working on my new big plugin, I ran into a pretty weird problem.
    I need to secure the contents of Item Frames, so only certain people can get them.
    I noticed, that ItemFrames are Entities, so "EntityDamagedByEntityEvent" (for punching) and "PlayerInteractEntityEvent" (for inserting and rotating) should work. And it does.

    The thing is, that when punching a frame even if the event was cancelled, the block inside the frame pops out.

    Code:
    Code:
        @EventHandler
        public void onPlayerPunchFrame(EntityDamageByEntityEvent bbe) {
            if (bbe.getEntityType() == EntityType.ITEM_FRAME) {
                if (bbe.getDamager() instanceof Player) {
                    try {
                        if ( szm.isInZone(bbe.getEntity().getLocation(), (Player) bbe.getDamager()))) { //setting zone variables
                            if (checkBlockRight(Material.ITEM_FRAME, (Player) bbe.getDamager(), owner, subzonename, name, streetzone)) { //checking block break rights
                                bbe.setCancelled(false);//allowed
                            } else {
                                bbe.setCancelled(true);//forbidden
                                if (subzonename != null) {
                                    ((Player) bbe.getDamager())
                                            .sendMessage(
                                                    ChatColor.RED + "You are not allowed to destroy this block on subzone " + subzonename.getName() + "!");//the message is shown
                                } else if (streetzone != null) {
                                    ((Player) bbe.getDamager())
                                            .sendMessage(
                                                    ChatColor.RED + "This block belongs to a street (" + streetzone.getName()+"). You are not allowed to destroy it");
                                } else {
                                    ((Player) bbe.getDamager())
                                            .sendMessage(
                                                    ChatColor.RED + "You are not allowed to destroy this block on zone " + name.getName() + "!");
                                }
    
                            }
                        } else {
                            bbe.setCancelled(false);//not in Zone
                        }
                    } catch (ClassNotFoundException e) {
                        ((Player) bbe.getDamager()).sendMessage(ChatColor.RED + "ClassFinder-Fehler! Bitte informiere einen Dev!");
                        Logger.getLogger(SzmOnClick.class.getName()).log(Level.SEVERE, null, e);
                        bbe.setCancelled(true);
                    } catch (IOException e) {
                        ((Player) bbe.getDamager()).sendMessage(ChatColor.RED + "IO-Fehler! Bitte informiere einen Dev!");
                        Logger.getLogger(SzmOnClick.class.getName()).log(Level.SEVERE, null, e);
                        bbe.setCancelled(true);
                    } finally {
                        subzonename = null;
                        name = null;
                        owner = null;
                        streetzone = null;
                    }
                }
            }
        }
    The Messages are shown, so the code is executed until the end.
    Am I missing something or is there another (better?) way to to that?


    EDIT:
    Yupp, The Event just doesn't work.
    I removed ALL the code and just make the event cancel:
    Code:
    @EventHandler
        public void onPlayerPunchFrame(EntityDamageByEntityEvent bbe) {
            if (bbe.getEntityType() == EntityType.ITEM_FRAME) {
                 bbe.setCanceled(true);
            }
        }
    And the Item's still jumping out of the frame...


    REAL [EDIT]
    I've got the Answer: Event iterference!
    don't ever use
    Code:
    setCanceled(false);
    if your goal isn't to let the event happen.
     
    Last edited: May 23, 2016
  2. Offline

    mine-care

    @TheA13X
    I hope this thread is solved, if it is please mark it as "Solved".
    Also thanks for providing the solution to your own question for future reference! People seem to forget this lately :p
     
Thread Status:
Not open for further replies.

Share This Page