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 package mctempleruins.com; import org.bukkit.Bukkit;import org.bukkit.ChatColor;import org.bukkit.Location;import org.bukkit.Material;import org.bukkit.World;import org.bukkit.entity.Player;import org.bukkit.entity.Villager;import org.bukkit.event.Listener;import org.bukkit.event.player.PlayerInteractEntityEvent;import org.bukkit.inventory.Inventory;import org.bukkit.inventory.ItemStack;import org.bukkit.inventory.meta.ItemMeta;import org.bukkit.plugin.java.JavaPlugin; public class Main extends JavaPlugin implements Listener { public static Inventory shop; public void shop() { shop = Bukkit.getServer().createInventory(null, 9, "Teleportation"); shop.setItem(2, new ItemStack(Material.GRASS, 1)); shop.setItem(3, new ItemStack(Material.DIAMOND_SWORD, 1)); shop.setItem(4, new ItemStack(Material.ENDER_PEARL)); shop.setItem(5, new ItemStack(Material.FISHING_ROD)); shop.setItem(6, new ItemStack(Material.IRON_SWORD, 1)); ItemMeta cs = shop.getItem(2).getItemMeta(); ItemMeta factions = shop.getItem(3).getItemMeta(); ItemMeta kitpvp = shop.getItem(4).getItemMeta(); ItemMeta minigames = shop.getItem(4).getItemMeta(); ItemMeta survival = shop.getItem(6).getItemMeta(); cs.setDisplayName(ChatColor.GREEN + "Join Creative"); factions.setDisplayName(ChatColor.DARK_RED + "Join Factions"); kitpvp.setDisplayName(ChatColor.AQUA + "Join KitPVP"); minigames.setDisplayName(ChatColor.BLUE + "Join MiniGames"); survival.setDisplayName(ChatColor.GREEN + "Join Survival"); shop.getItem(2).setItemMeta(cs); shop.getItem(3).setItemMeta(factions); shop.getItem(4).setItemMeta(kitpvp); shop.getItem(5).setItemMeta(minigames); shop.getItem(6).setItemMeta(survival); } Villager v; public void onEnable(){ Bukkit.getServer().getPluginManager().registerEvents(this, this); World w = Bukkit.getWorld("serv"); Location l = new Location(w, 88.60019, 64, 34.32772); Villager v = w.spawn(l, Villager.class); v.setCustomName("Bank Shop"); } public void npcEvent(PlayerInteractEntityEvent e){ Player p = e.getPlayer(); if (e.getRightClicked().equals(v)){ if(v.getCustomName() == "Bank Shop") { p.openInventory(shop); } } }}
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.
Okay so i tried cancelling the event then running a delayed task but nothing works. Code:java public void npcEvent(final PlayerInteractEntityEvent e){ if (e.getRightClicked().equals(v)){ if(v.getCustomName() == "Bank Shop") { e.setCancelled(true); pl.getServer().getScheduler().scheduleSyncDelayedTask(pl, new Runnable() { public void run() { Player p = e.getPlayer(); p.openInventory(shop); } }, 100L); } } }
Don't use the delay task, and change the follor line. Code:java if(v.getCustomName().equalsIgnoreCase("Bank Shop")) {
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.