Is there anyway to Listen NPC click Event?

Discussion in 'Plugin Development' started by gidonyou, Feb 26, 2014.

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

    gidonyou

    Is there any way to create Villager, or use Citizen 2 NPC Trait to Listen Event Click Event? and do event.setcanceled(true);?

    Because I want to open costume GUI Screen..

    P.S Not all the Villager/NPC but Specific one.
     
  2. you could try using the inventoryOpen event, check if the type is MerchantInventory and if you name the NPC then you can check if the inv's name is the same as whatever the NPC is named, if it is then cancel the open event.
    No idea if this would work or not, just throwing around ideas. Check out the apidocs, they may be useful to you http://jd.bukkit.org/rb/apidocs/
     
  3. Offline

    CaLxCyMru

    Use the EntityInteractEvent.
    Check if the Entity is a instaceof a Villager, then cancel the event.
     
  4. Offline

    gidonyou

    How do you get name of Villager, so it won't be every villager in the server..?

    Do somebody know to how to make Citizen 2 Trait?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 6, 2016
  5. Offline

    CoderRyan

    @EventHandler
    public void onVillagerClick(PlayerInteractEntityEvent event) {
    Villager villager = (Villager) event.getRightClicked();
    if (event.getRightClicked() instanceof Villager) {
    if (villager.getCustomName() == "Villagers name") {
    event.setCancelled(true);
    }
    }
    }
     
  6. Offline

    Captain Dory

    This'll throw huge errors. You have to check if the entity is a villager before declaring it one.
     
  7. Offline

    gidonyou

    Then What Should I do?

    should i do try and catch method?
     
  8. Offline

    Captain Dory

    Use this (Modified version of CoderRyan's code:
    PHP:
    @EventHandler
    public void onVillagerClick(PlayerInteractEntityEvent event) {
     
      if (
    e.getEntity instanceof Villager) {
     
      
    Villager v = (Villagee.getEntity();
     
          if (
    v.getCustomName().equals("Villager name") {
         
          
    e.getPlayer.closeInventory();
          
    e.setCancelled(true);
         
          }
     
      }
    }
     
  9. Offline

    Desle

    CoderRyan
    I love how you still check if it's a villager... even after casting it to one.
     
    Harmings likes this.
  10. Offline

    CoderRyan

    Desle if you didn't have the if(event.getEntity() instanceof Villager) { the code will cause a memory leak because its still going to do all those checks even if you click a spider for example and it will cause huge console errors etc. so before you judge my code please learn java :)
     
  11. Offline

    Harmings

    Don't tell him to learn Java... He is absolutely right. You are casting the entity as a Villager prior to checking if it's even a Villager
     
  12. Offline

    Wolfey

    If the entity isn't a Villager, then errors will be thrown, cause you're casting the entity as a Villager before checking if it is one. Before you tell others to learn Java, why don't you learn it yourself.
     
  13. Offline

    CoderRyan

    Wolfey you check if the entity is instanceof a villager to check if the entity your clicking is a villager, then you are going to also get the villager as the entity so you can get the villagers custom name etc.
     
  14. Offline

    Wolfey

    I don't think you understood what I said.

    If you cast the entity as a villager before checking if it is one, then you're going to get errors.
     
  15. Offline

    CoderRyan

    aha im dumb :p i did it wrong way around lol it should have been if (event.getRightClicked() instanceof Villager) {
    then Villager villager = (Villager) event.getRightClicked();
    :p i see wt u were saying lol
     
  16. Offline

    gidonyou

    Wolfey CoderRyan or Anyone.. Is there any way to listen to Citizen 2 NPC click Event?

    or Create Custom NPC (With skin) or whatever..

    Also, how to make freeze Villager? so they won't move around and get damage? even by zombies, TNT, Fire Etc


    Thanks for your Supports
     
  17. Offline

    gidonyou

Thread Status:
Not open for further replies.

Share This Page