PlayerArmorStandManipulateEvent

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

  1. Code (Text):
        @EventHandler
        public void OnInteractAtEntity(PlayerArmorStandManipulateEvent e) {
           
            Player player = e.getPlayer();

            ArmorStand as = Pet.armorstands.get(player.getUniqueId());
            if (e.getRightClicked() == as) {
               
                e.setCancelled(true);
                player.sendMessage("1");
            }

            }
    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.
     
  2. Youre putting player UUIDs with armorstand in a hashmap, Im not sure but I think it is better to put the armorstand UUID instead of the armorstand object itself, and then compare and check if the armorstand uuid matches the clicked armorstand uuid, thats one thing.
    second, check if the hashmap contains the value of your armorstand, and if so, cancel it, because what you are doing is canceling it if the owner touches it.

    ie:
    Code (Text):
    if(hashmap.containsValue(armorstand)){
    //checking if the hashmap contains the armorstand

    if(hashmap.get(e.getPlayer.getUUID()) == armorstand){
    //checking if the player uuid is associated with the armorstand

    --do wahtever needs to do if the player owns the armorstand
    }else{
    -- do whatever you want to do if the player does not own the armorstand, ie e.setCanceled()
    }
    *I wrote the code here so some brackets are missing and such.
     
    • Like Like x 1
    • your map does not need to be static
    • you should really take a more OOP styled approach to this and create 'pet' objects and such to store
     
  3. What about this?
    Check before if player click on as and after if the uuid of player equals to uuid owner of ArmorStand
    Code (Text):
            Player player = e.getPlayer();

            ArmorStand as = Pet.armorstands.get(player.getUniqueId());
    if (e.getRightClicked() == as) {
            if (e.getRightClicked().getUniqueUUID() == as.getUniqueUUID) {
               
                e.setCancelled(true);
                player.sendMessage("1");
            }
         Else {
        E.setCancelled(true);
    }
    }


    //Written by Phone, sorry if something is wrong
     
  4. try checking it, youre cancelling the event on both cases, it seems like it should work.
     
  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. Why dont you test it? theres a limit to how much I can run through my head (especially when its 1:45 AM )
    one thing though : why are you comparing armorstand (entity) to a block,
    also try to be more consistent with your code, you leave random empty lines between closed bracket lines, whats the use? makes it harder to check it (personal opinion,)
     
  7. Yeah, i changed with Play
    I tried with other codes, but the problem (i think) is that the Hashmap is configurated with a Player UUID and ArmorStand
     
  8. then change the hashmap?... im not quite sure what do you mean,
    if its hashmap<uuid, armorstand> simply change it to <uuid, uuid> where the first uuid is the players, and the second is of the armourstand.
    edit: what are you trying to do? lock the armorstand so that noone will be able to interact it? is it some sort of a pet? if so there are many easier approaches to do so,
     
  9. I'm trying to do a Pet where the Owner and other players can't steal items from it
     
  10. ok, perhaps make a pet object? make a custom entity which extends NMS entity? if you want to stick to your code, then it should work, does it?
     
  11. That could be a good way
     
  12. ok, anything else you need?