Inventory open when you clicked on a villager dont work

Discussion in 'Plugin Development' started by marijn0909, Jun 5, 2014.

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

    marijn0909

    Why is this not working?

    Code:
    package me.planktogoly.main;
     
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.Material;
    import org.bukkit.World;
    import org.bukkit.entity.EntityType;
    import org.bukkit.entity.Player;
    import org.bukkit.entity.Villager;
    import org.bukkit.entity.Zombie;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.EntityDamageEvent;
    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 rules;
       
        public void rules() {
            rules = Bukkit.getServer().createInventory(null, 9, "Rules");   
            rules.setItem(2, new ItemStack(Material.BOOK));
            rules.setItem(3, new ItemStack(Material.BOOK));
            rules.setItem(4, new ItemStack(Material.BOOK));
            rules.setItem(5, new ItemStack(Material.BOOK));
            rules.setItem(6, new ItemStack(Material.BOOK));
           
            ItemMeta rule2 = rules.getItem(2).getItemMeta();
            ItemMeta rule3 = rules.getItem(3).getItemMeta();
            ItemMeta rule4 = rules.getItem(4).getItemMeta();
            ItemMeta rule5 = rules.getItem(5).getItemMeta();
            ItemMeta rule6 = rules.getItem(6).getItemMeta();
           
            rule2.setDisplayName(ChatColor.DARK_GRAY + "Rule 1");
            rule3.setDisplayName(ChatColor.DARK_GRAY + "Rule 2");
            rule4.setDisplayName(ChatColor.DARK_GRAY + "Rule 3");
            rule5.setDisplayName(ChatColor.DARK_GRAY + "Rule 4");
            rule6.setDisplayName(ChatColor.DARK_GRAY + "Rule 5");
           
            rules.getItem(2).setItemMeta(rule2);
            rules.getItem(3).setItemMeta(rule3);
            rules.getItem(4).setItemMeta(rule4);
            rules.getItem(5).setItemMeta(rule5);
            rules.getItem(6).setItemMeta(rule6);
           
        }
     
        villager v;
        public void onEnable(){
            Bukkit.getServer().getPluginManager().registerEvents(this, this);
            World w = Bukkit.getWorld("server");
            Location loc = new Location(w, -1984.30000, 30, 2376.70000);
            v = (Villager) w.spawnEntity(loc, EntityType.VILLAGER);
            v.setCustomName(ChatColor.RED + "info");
        }
       
        @EventHandler
        public void npcEvent(PlayerInteractEntityEvent e){
            Player p = e.getPlayer();
            if (e.getRightClicked().equals(z)){
                if(v.getCustomName() == "info"){
                    p.openInventory(rules);
                }
                }
            }   
               
        public void onDamage(EntityDamageEvent z){
            v.setCancelled(true);
        }
           
       
        }
           
    
     
  2. When comparing strings, use .equals(""), not ==
     
  3. Offline

    LordFox

    just saying, if its a problem with the plugin not working in game, try putting in [email protected] and registering the events, this was pasted from the plugin tutorial:
    Code:java
    1. public final class {$PluginName} extends JavaPlugin {
    2. @Override
    3. public void onEnable() {
    4. bukkit.getserver().getPluginManager() .registerEvents (this, this);
    5.  
    6. }
    7.  
    8. @Override
    9. public void onDisable() {
    10. // TODO Insert logic to be performed when the plugin is disabled
    11. }
    12. }

    If im wrong, sorry
     
  4. Offline

    Dubehh

    marijn0909

    Code:java
    1. public void onDamage(EntityDamageEvent z){
    2. v.setCancelled(true);


    - Missing @EventHandler
    - EntityDamageEvent z , and then v.setCancelled(true);
    That doesn't make sense at all..

    Code:java
    1. @EventHandler
    2. public void onDamage(EntityDamageEvent e){
    3. if(!(e.getEntity instanceof Villager)) return;
    4. e.setCancelled(true);
    5. }


    And 'Villager v;' should be capitalized,

    You have:
    Code:java
    1. villager v;


    So i guess you got an error there?
     
  5. LordFox He did register the events and the Override annotation is purely for the programmer's benefit - doesn't change anything at runtime.
     
Thread Status:
Not open for further replies.

Share This Page