Solved How would i get the villagers name?

Discussion in 'Plugin Development' started by xAstraah, Mar 26, 2014.

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

    xAstraah

    So im making a plugin and its working great other than i want my custom villager inventory only to open when the villagers name is == to myInventory for example.

    How would i go about grabbing the villagers name?

    Thanks
    Stephen Ropke
     
  2. Offline

    Barinade

    getCustomName()?
     
  3. Offline

    xAstraah

    Yes a custom name Barinade
     
  4. Offline

    Barinade

    "How would i go about grabbing the villagers name?"

    With "getCustomName()"
     
    ean521 and Konkz like this.
  5. Offline

    xAstraah

    Barinade oh God, Im such a noob :D Sorry i didn't notice you said that, Im going to test it now. Thanks

    Edit: So i tried doing stuff like Villager.getCustomName() But Villagers methods don't like support it even checked the javadocs. Could you give me a example?
     
  6. Offline

    PolarCraft

    xAstraah
    This will spawn the villager.
    Code:java
    1. Villager v = w.spawn(loc, Villager.class);
    2. v.setCustomName("NAME HERE");
     
  7. Offline

    Barinade

    Are you trying to do it staticly?
     
  8. Offline

    xAstraah

    PolarCraft, Doesn't matter where i put the line of code it all ways gives me this error: http://prntscr.com/34fdbo

    Barinade, How would i go about doing that?

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

    Barinade

    Show more of the code, and that line is entirely wrong lol
     
  10. Offline

    GotRice

    xAstraah You need to create a villager using
    Code:java
    1. Villager v = w.spawn(loc, Villager.class);

    credit to PolarCraft
    then set the custom name using
    Code:java
    1. v.setCustomName("NAME HERE");

    credit to PolarCraft
    The method "setCustomName" does not return a Villager object.
     
  11. Offline

    Barinade

    I was neglecting to show him how to spawn a villager because it seems like a villager already exists and he's trying to get it's name, that's why I asked for more code.
     
  12. Offline

    xAstraah

    Code:
    package io.github.xAstraah;
     
    import org.bukkit.Bukkit;
    import org.bukkit.Material;
    import org.bukkit.entity.Player;
    import org.bukkit.entity.Villager;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.inventory.InventoryClickEvent;
    import org.bukkit.event.player.PlayerInteractEntityEvent;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class VillagerNetwork extends JavaPlugin implements Listener
    {
     
        public static Inventory myInventory = Bukkit.createInventory(null, 9, "myInventory");
     
        public void onEnable()
        {
            this.getServer().getPluginManager().registerEvents(this, this);
        }
     
        @EventHandler
        public void Villager(PlayerInteractEntityEvent event)
        {
         
            Player player = event.getPlayer();
         
            if(event.getRightClicked() instanceof Villager)
            {
                event.setCancelled(true);
                player.openInventory(myInventory);
                myInventory.setItem(0, new ItemStack(Material.CAKE));
                myInventory.setItem(8, new ItemStack(Material.COOKIE));
            }
        }
     
        @EventHandler
        public void Inventorys(InventoryClickEvent event)
        {
            Player player = (Player) event.getWhoClicked();
            ItemStack clicked = event.getCurrentItem();
            Inventory inventory = event.getInventory();
         
            if(inventory.getName().equals(myInventory.getName()))
            {
                event.setCancelled(true);
            }
         
            if(clicked.getType() == Material.COOKIE)
            {
                player.sendMessage("Cookies, Such Desire, Such Disappointment!");
            }
         
        }
     
    }
    
    Barinade GotRice PolarCraft

    I want to make it so on ( instanceof Villager ) it then checks to see the villagers name then continues or follows a else statement
     
  13. Offline

    GotRice

    xAstraah Cast the entity to a villager, and then check for its custom name.
     
  14. Offline

    Barinade

    ^ what he said
    ((Villager) e.getRightClicked()).getCustomName()
     
  15. Offline

    PolarCraft

    Barinade Oh i didnt know that lol i just posted how to spawn it and how to get the name. Also xAstraah you have to put the spawning bit into the onenable if you want it to spawn when the plugin loads.
     
  16. Offline

    1Rogue

    Also, check the type, rather than using instanceof:

    Code:java
    1. if (event.getEntity().getType() == EntityType.VILLAGER) {
    2. Villager v = (Villager) event.getEntity();
    3. String name = v.getCustomName();
    4. }
     
  17. Offline

    xAstraah

    1Rogue, So i've tried doing the whole if method and it just gives me a error on getEntity(), i don't think getEntity() exists.. Help?! Im building my project with CraftBukkit and Bukkit but still none references getEntity in PlayerInteractEntityEvent?
     
  18. Offline

    GeorgeeeHD


    lol post what you have xD
     
Thread Status:
Not open for further replies.

Share This Page