Villager Menu

Discussion in 'Plugin Development' started by PolarCraft, Mar 25, 2014.

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

    PolarCraft

    Okay so i am trying to make a villager menu. So when you right click on the villager it opens a new inventory. But I really dont know how to cancel the villagers menu and open the inventory.
    Also everything spawns but how could i stop one from spawning if their is a villager already spawned with that name?

    Code:java
    1. package mctempleruins.com;
    2.  
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.ChatColor;
    5. import org.bukkit.Location;
    6. import org.bukkit.Material;
    7. import org.bukkit.World;
    8. import org.bukkit.entity.Player;
    9. import org.bukkit.entity.Villager;
    10. import org.bukkit.event.Listener;
    11. import org.bukkit.event.player.PlayerInteractEntityEvent;
    12. import org.bukkit.inventory.Inventory;
    13. import org.bukkit.inventory.ItemStack;
    14. import org.bukkit.inventory.meta.ItemMeta;
    15. import org.bukkit.plugin.java.JavaPlugin;
    16.  
    17. public class Main extends JavaPlugin implements Listener {
    18. public static Inventory shop;
    19.  
    20. public void shop() {
    21. shop = Bukkit.getServer().createInventory(null, 9, "Teleportation");
    22. shop.setItem(2, new ItemStack(Material.GRASS, 1));
    23. shop.setItem(3, new ItemStack(Material.DIAMOND_SWORD, 1));
    24. shop.setItem(4, new ItemStack(Material.ENDER_PEARL));
    25. shop.setItem(5, new ItemStack(Material.FISHING_ROD));
    26. shop.setItem(6, new ItemStack(Material.IRON_SWORD, 1));
    27.  
    28. ItemMeta cs = shop.getItem(2).getItemMeta();
    29. ItemMeta factions = shop.getItem(3).getItemMeta();
    30. ItemMeta kitpvp = shop.getItem(4).getItemMeta();
    31. ItemMeta minigames = shop.getItem(4).getItemMeta();
    32. ItemMeta survival = shop.getItem(6).getItemMeta();
    33.  
    34. cs.setDisplayName(ChatColor.GREEN + "Join Creative");
    35. factions.setDisplayName(ChatColor.DARK_RED + "Join Factions");
    36. kitpvp.setDisplayName(ChatColor.AQUA + "Join KitPVP");
    37. minigames.setDisplayName(ChatColor.BLUE + "Join MiniGames");
    38. survival.setDisplayName(ChatColor.GREEN + "Join Survival");
    39. shop.getItem(2).setItemMeta(cs);
    40. shop.getItem(3).setItemMeta(factions);
    41. shop.getItem(4).setItemMeta(kitpvp);
    42. shop.getItem(5).setItemMeta(minigames);
    43. shop.getItem(6).setItemMeta(survival);
    44. }
    45.  
    46. Villager v;
    47. public void onEnable(){
    48. Bukkit.getServer().getPluginManager().registerEvents(this, this);
    49. World w = Bukkit.getWorld("serv");
    50. Location l = new Location(w, 88.60019, 64, 34.32772);
    51. Villager v = w.spawn(l, Villager.class);
    52. v.setCustomName("Bank Shop");
    53. }
    54.  
    55. public void npcEvent(PlayerInteractEntityEvent e){
    56. Player p = e.getPlayer();
    57. if (e.getRightClicked().equals(v)){
    58. if(v.getCustomName() == "Bank Shop") {
    59. p.openInventory(shop);
    60. }
    61. }
    62. }
    63. }
    64.  
     
  2. Offline

    GotRice

    I did a one tick delay before opening a custom inventory, not sure how others did it.
     
  3. Offline

    TeeePeee

    You're event isn't being cancelled because you're comparing Strings with == while you should be using .equals().

    You'll have to iterate through the world entities to make sure you don't spawn another with the same name. for(Entity e : world.getEntitiesByClass(Villager.class)) { // ... Check here } or some similarly named method.
     
  4. Offline

    leet4044

    Cancel the event, then open your inventory on your PlayerInteractWithEntityEvent
     
  5. Offline

    PolarCraft

    Okay so i tried cancelling the event then running a delayed task but nothing works.
    Code:java
    1. public void npcEvent(final PlayerInteractEntityEvent e){
    2. if (e.getRightClicked().equals(v)){
    3. if(v.getCustomName() == "Bank Shop") {
    4. e.setCancelled(true);
    5. pl.getServer().getScheduler().scheduleSyncDelayedTask(pl, new Runnable() {
    6. public void run() {
    7. Player p = e.getPlayer();
    8. p.openInventory(shop);
    9. }
    10. }, 100L);
    11. }
    12. }
    13. }
     
  6. Offline

    leet4044

    Don't use the delay task, and change the follor line.

    Code:java
    1. if(v.getCustomName().equalsIgnoreCase("Bank Shop")) {
     
  7. Offline

    AoH_Ruthless

    PolarCraft
    Forget the delay. You forgot your @EventHandler, and you are comparing strings with ==
     
  8. Offline

    SkyleTyler1337

    It doesn't need to be canceled
     
  9. Offline

    leet4044

    If you don't cancel it (This is based on my experience), it sometimes flashes between the villagers inventory and your own, especially if the server lags a bit.
     
  10. Offline

    SkyleTyler1337

    You forgot your EventHandler before your event
     
Thread Status:
Not open for further replies.

Share This Page