Help with ArmorStand- ClickEvent

Discussion in 'Spigot Plugin Development' started by Dantesudante, Jun 2, 2017.

  1. Code (Text):
        @EventHandler
        public void OnInteractAtEntity(PlayerArmorStandManipulateEvent e) {
           
           
            Player player = e.getPlayer();
            ArmorStand as = Pet.armorstands.get(player.getUniqueId());
           
            player.sendMessage("00");
            if (Pet.armorstands.containsValue(as)) {
                player.sendMessage("01");
                if (e.getRightClicked() == as){
                    if (Pet.armorstands.get(e.getPlayer().getUniqueId()) == as) {
                        //e.setCancelled(true);
                        player.sendMessage("1");
                        e.setCancelled(true);
                    }
                    else {
                        player.sendMessage("2");
                        e.setCancelled(true);
                    }
                }
               
            }
        }
    Code (Text):
    static HashMap<UUID, ArmorStand> armorstands = new HashMap<>();




        public void spawnPet(Player player) {
           
           
            ArmorStand as  = (ArmorStand) player.getLocation().getWorld().spawn(player.getLocation().add(0,-0.4,0), ArmorStand.class);
            as.setCustomName(player.getName()+ "'s ChestPet");
            as.setCustomNameVisible(true);
            as.setInvulnerable(true);
            as.setHelmet(head);
            as.setVisible(false);
            as.setCanPickupItems(false);
            as.setCollidable(false);
            as.setSmall(true);
            as.setBasePlate(false);
            as.setGravity(false);
            armorstands.put(player.getUniqueId(), as);
           
           
        }

    How can I disable the Interact for other Players with an ArmorStand owned by a Player? The Owner is not able to remove the items of Armorstand (like head, chestplate etc etc) but other players can.


    I tried also:
    Code (Text):
        @EventHandler
        public void OnInteractAtEntity(PlayerArmorStandManipulateEvent e) {
           
           
            Player player = e.getPlayer();
            ArmorStand as = Pet.armorstands.get(player.getUniqueId());
           
            player.sendMessage("00");
                if (e.getRightClicked() == as){
                    if (Pet.armorstands.get(e.getPlayer().getUniqueId()) == as) {
                        //e.setCancelled(true);
                        player.sendMessage("1");
                        e.setCancelled(true);
                    }
                    else {
                        player.sendMessage("2");
                        e.setCancelled(true);
                    }
            }
        }

    But other Player can still steal items from ArmorStand - Pet
     
  2. Because you are checking if the Owner is the one clicking it.
    if (Pet.armorstands.get(e.getPlayer().getUniqueId()) == as) {
    as uses e.getPlayer() seperately. So change it to:

    Because you are checking if the Owner is the one clicking it.
    if (Pet.armorstands.get(e.getPlayer().getUniqueId()) != as) {
     
  3. Please don't compare entities, use .equals instead.
    Also is "1" or "2" beeing printed in the chat?
    Also first, make sure if your map contains the player instead of directly getting the entity as it could be null.
     
  4. Change it to...
     
  5. Code (Text):
        @EventHandler
        public void OnInteractAtEntity(PlayerInteractEvent e) {
           
           
            Player player = e.getPlayer();
            ArmorStand as = Pet.armorstands.get(player.getUniqueId());
           
            player.sendMessage("00");
           
                if(Pet.armorstands.containsValue(as)){
                    if (e.getClickedBlock() == as){
                   
                    if (Pet.armorstands.get(e.getPlayer().getUniqueId()).equals(as)) {
                        //e.setCancelled(true);
                        player.sendMessage("1");
                        e.setCancelled(true);
                    }
                   
                    if (Pet.armorstands.get(e.getPlayer().getUniqueId()) != as) {
                        player.sendMessage("2");
                        e.setCancelled(true);
                    }
                   
                }
                }
            }
    Something like that?
    }
     
  6. Code (Text):
     @EventHandler
        public void OnInteractAtEntity(PlayerInteractEvent e) {
           
           
            Player player = e.getPlayer();
            ArmorStand as = Pet.armorstands.get(player.getUniqueId());
           
            player.sendMessage("00");
           
                if(Pet.armorstands.containsValue(as)){
                    if (e.getClickedBlock() == as){
                   
                    if (Pet.armorstands.get(e.getPlayer().getUniqueId()).equals(as)) {
                        //e.setCancelled(true);
                        player.sendMessage("1");
                        e.setCancelled(true);
                    }
                   
                    if (Pet.armorstands.get(e.getPlayer().getUniqueId()) != as) {
                        player.sendMessage("2");
                        e.setCancelled(true);
                    }
                   
                }
                }
            }
    Something like that?
     

  7. Yes but change if (Pet.armorstands.get(e.getPlayer().getUniqueId()).equals(as)) {
    to if (!(Pet.armorstands.get(e.getPlayer().getUniqueId()).equals(as))) {
     
  8. Code (Text):

        @EventHandler
        public void OnInteractAtEntity(PlayerArmorStandManipulateEvent e) {
           
           
            Player player = e.getPlayer();
            ArmorStand as = Pet.armorstands.get(player.getUniqueId());
           
            player.sendMessage("00");
           
                if(Pet.armorstands.containsValue(as)){
                    player.sendMessage("01");
                    if (e.getRightClicked()== as){
                   
                    if (Pet.armorstands.get(e.getPlayer().getUniqueId()).equals(as)) {
                        player.sendMessage("02");
                       
                       
                        player.sendMessage("03");
                        e.setCancelled(true);
                   
                   
                    if (!(Pet.armorstands.get(e.getPlayer().getUniqueId()).equals(as))) {
                     
                        player.sendMessage("04");
                        e.setCancelled(true);
                   
                    }
                    }
                }
                }
            }
     
  9. Yes but also above it as well.
     
    • Optimistic Optimistic x 1