Bukkit插件:java.lang.NullPointerException

时间:2014-07-30 22:42:37

标签: java nullpointerexception bukkit

所以我正在尝试加载我制作的这个新插件,我搜索了所有内容,但我找不到错误!请帮助,这是永远的。

这是我的主要课程:

package com.hugcraft.hugkitpvp;


import org.bukkit.Bukkit;
import org.bukkit.block.Sign;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.Action;
import org.bukkit.event.block.SignChangeEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.plugin.java.JavaPlugin;

public class HugKitPvP extends JavaPlugin implements Listener {


private InventoryMenuManager invmenu;

public void onEnable() {
    invmenu = new InventoryMenuManager(this);
    Bukkit.getServer().getPluginManager().registerEvents(this, this);
}

    @EventHandler     
    public void onPlayerInteract(PlayerInteractEvent e) {
    if (!(e.getAction() == Action.RIGHT_CLICK_BLOCK)) return;
    if (e.getClickedBlock().getState() instanceof Sign) {
        Sign s = (Sign) e.getClickedBlock().getState();
        if (s.getLine(0).equalsIgnoreCase("§3[Kits]")) {
            invmenu.show(e.getPlayer());

        }
    }
}

        @EventHandler
        public void onSignChange(SignChangeEvent e) {
            if (e.getLine(0).equalsIgnoreCase("[Kits]")) {
                e.setLine(0, "§b[Kit]");
                e.setLine(1, "§3Click Here");
                e.setLine(2, "§3To Choose");
                e.setLine(3, "§3Your Kit!");
    }
  }
}

这是我的听众课程:

package com.hugcraft.hugkitpvp;



import java.util.Arrays;

import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Color;
import org.bukkit.Material;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.PlayerInventory;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.inventory.meta.LeatherArmorMeta;
import org.bukkit.plugin.Plugin;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;

public class InventoryMenuManager implements Listener {

private Inventory inv;
private ItemStack w, ww, s, pp, sb, a, t, c, r;

public InventoryMenuManager(Plugin ppp) {
    inv = Bukkit.getServer().createInventory(null, 9, "Choose your kit!");


    w = createItem(Material.IRON_SWORD, ChatColor.GREEN + "Warrior");
    ww = createItem(Material.STICK, ChatColor.BLUE + "Wizard");
    s = createItem(Material.NETHER_STAR, ChatColor.YELLOW + "Ninja");
    pp = createItem(Material.FIRE, ChatColor.RED + "Pyro");
    sb = createItem(Material.SNOW_BALL, ChatColor.GRAY + "Demoman");
    a = createItem(Material.BOW, ChatColor.AQUA + "Archer");
    t = createItem(Material.IRON_CHESTPLATE, ChatColor.WHITE + "Tank");
    c = createItem(Material.ENDER_PEARL, ChatColor.BLACK + "Chicken");
    r = createItem(Material.GOLD_BOOTS, ChatColor.GREEN + "Runner");

    inv.setItem(1, w);
    inv.setItem(2, ww);
    inv.setItem(3, s);
    inv.setItem(4, pp);
    inv.setItem(5, sb);
    inv.setItem(6, a);
    inv.setItem(7, t);
    inv.setItem(8, c);
    inv.setItem(9, r);

    Bukkit.getServer().getPluginManager().registerEvents(this, ppp);

}

private ItemStack createItem(Material dc, String name ) {
    ItemStack i = new ItemStack(dc);
    ItemMeta im = i.getItemMeta();
    im.setDisplayName(name);
    im.setLore(Arrays.asList("Choose kit " + name.toLowerCase() + "!"));
    i.setItemMeta(im);
    return i;


}

public void show(Player p) {
    p.openInventory(inv);
}

Player p;
PlayerInventory pi = p.getInventory();



//Warrior Kit
@EventHandler
public void onInventoryClick(InventoryClickEvent e) {
    if (!e.getInventory().getName().equalsIgnoreCase(inv.getName())) return;
    if (e.getCurrentItem().getItemMeta() == null) return;
    if (e.getCurrentItem().getItemMeta().getDisplayName().contains("Warrior")) {
        e.setCancelled(true);
        e.getWhoClicked();pi.addItem(new ItemStack(Material.STONE_SWORD, 1));
        e.getWhoClicked();pi.setHelmet(new ItemStack(Material.CHAINMAIL_HELMET, 1));
        e.getWhoClicked();pi.setChestplate(new ItemStack(Material.CHAINMAIL_CHESTPLATE, 1));
        e.getWhoClicked();pi.setLeggings(new ItemStack(Material.CHAINMAIL_LEGGINGS, 1));
        e.getWhoClicked();pi.setBoots(new ItemStack(Material.LEATHER_BOOTS, 1));
        e.getWhoClicked();pi.addItem(new ItemStack(Material.COOKED_BEEF, 8));
        e.getWhoClicked().closeInventory();

        //Wizard Kit
        }
    if (e.getCurrentItem().getItemMeta().getDisplayName().contains("Wizard")) {
        e.setCancelled(true);
         e.getWhoClicked(); {
             ItemStack stick = new ItemStack(Material.STICK, 1);
             stick.addUnsafeEnchantment(Enchantment.KNOCKBACK, 5);
         }
         e.getWhoClicked(); {
             ItemStack wand = new ItemStack(Material.BLAZE_ROD, 1);
             wand.addUnsafeEnchantment(Enchantment.FIRE_ASPECT, 1);
         }
            e.getWhoClicked(); {
                ItemStack helmet = new ItemStack(Material.LEATHER_HELMET, 1);
                LeatherArmorMeta meta = (LeatherArmorMeta) helmet.getItemMeta();
                meta.setDisplayName(ChatColor.DARK_PURPLE + "Wizard Helmet");
                meta.setColor(Color.PURPLE);
                pi.setHelmet(helmet);
                helmet.setItemMeta(meta);
            }
            e.getWhoClicked(); {
                ItemStack cp = new ItemStack(Material.LEATHER_CHESTPLATE, 1);
                LeatherArmorMeta meta = (LeatherArmorMeta) cp.getItemMeta();
                meta.setDisplayName(ChatColor.DARK_PURPLE + "Wizard Chestplate");
                meta.setColor(Color.PURPLE);
                pi.setChestplate(cp);
                cp.setItemMeta(meta);
            }
            e.getWhoClicked(); {
                ItemStack legs = new ItemStack(Material.LEATHER_LEGGINGS, 1);
                LeatherArmorMeta meta = (LeatherArmorMeta) legs.getItemMeta();
                meta.setDisplayName(ChatColor.DARK_PURPLE + "Wizard Leggings");
                meta.setColor(Color.PURPLE);
                pi.setLeggings(legs);
                legs.setItemMeta(meta);
            }
            e.getWhoClicked(); {
                ItemStack boots = new ItemStack(Material.LEATHER_BOOTS, 1);
                LeatherArmorMeta meta = (LeatherArmorMeta) boots.getItemMeta();
                meta.setDisplayName(ChatColor.DARK_PURPLE + "Wizard Leggings");
                meta.setColor(Color.PURPLE);
                pi.setBoots(boots);
                boots.setItemMeta(meta);
            }
            e.getWhoClicked();pi.addItem(new ItemStack(Material.COOKED_BEEF, 8));
        e.getWhoClicked().closeInventory();

        //Ninja Kit
    }
    if (e.getCurrentItem().getItemMeta().getDisplayName().contains("Ninja")) {
        e.setCancelled(true);
         e.getWhoClicked(); {
             ItemStack sword = new ItemStack(Material.STONE_SWORD, 1);
             sword.addUnsafeEnchantment(Enchantment.DAMAGE_ALL, 1);
         }
            e.getWhoClicked(); {
                ItemStack nc = new ItemStack(Material.LEATHER_CHESTPLATE, 1);
                LeatherArmorMeta meta = (LeatherArmorMeta) nc.getItemMeta();
                meta.setDisplayName(ChatColor.BLACK + "Ninja Chestplate");
                meta.setColor(Color.GRAY);
                pi.setChestplate(nc);
                nc.setItemMeta(meta);
            }

            e.getWhoClicked(); {
                ItemStack nl = new ItemStack(Material.LEATHER_LEGGINGS, 1);
                LeatherArmorMeta meta = (LeatherArmorMeta) nl.getItemMeta();
                meta.setDisplayName(ChatColor.DARK_PURPLE + "Ninja Leggings");
                meta.setColor(Color.GRAY);
                pi.setLeggings(nl);
                nl.setItemMeta(meta);
            }
            e.getWhoClicked();pi.addItem(new ItemStack(Material.COOKED_BEEF, 8));
        e.getWhoClicked().closeInventory();


        //Pyro Kit
    }
    if (e.getCurrentItem().getItemMeta().getDisplayName().contains("Pyro")) {
        e.setCancelled(true);
        e.getWhoClicked(); {
            ItemStack sword = new ItemStack(Material.GOLD_SWORD, 1);
            sword.addEnchantment(Enchantment.FIRE_ASPECT, 1);
        }
        e.getWhoClicked();pi.setHelmet(new ItemStack(Material.CHAINMAIL_HELMET, 1));
        e.getWhoClicked();pi.setChestplate(new ItemStack(Material.CHAINMAIL_CHESTPLATE, 1));
        e.getWhoClicked();pi.setLeggings(new ItemStack(Material.CHAINMAIL_LEGGINGS, 1));
        e.getWhoClicked();pi.setBoots(new ItemStack(Material.CHAINMAIL_BOOTS, 1));
        e.getWhoClicked();pi.addItem(new ItemStack(Material.COOKED_BEEF, 8));
        e.getWhoClicked().closeInventory();

        //Demoman Kit
    }

    if (e.getCurrentItem().getItemMeta().getDisplayName().contains("Demoman")) {
        e.setCancelled(true);
        e.getWhoClicked();pi.addItem(new ItemStack(Material.STONE_SWORD));
        e.getWhoClicked();pi.setHelmet(new ItemStack(Material.CHAINMAIL_HELMET, 1));
        e.getWhoClicked();pi.setChestplate(new ItemStack(Material.CHAINMAIL_CHESTPLATE, 1));
        e.getWhoClicked();pi.setLeggings(new ItemStack(Material.CHAINMAIL_LEGGINGS, 1));
        e.getWhoClicked();pi.setBoots(new ItemStack(Material.CHAINMAIL_BOOTS, 1));
        e.getWhoClicked();pi.addItem(new ItemStack(Material.SNOW_BALL, 10));
        e.getWhoClicked();pi.addItem(new ItemStack(Material.COOKED_BEEF, 8));
        e.getWhoClicked().closeInventory();


        //Archer Kit
    }

    if (e.getCurrentItem().getItemMeta().getDisplayName().contains("Archer")) {
        e.setCancelled(true);
        e.getWhoClicked();pi.addItem(new ItemStack(Material.GOLD_SWORD));
        e.getWhoClicked();pi.addItem(new ItemStack(Material.BOW));
        e.getWhoClicked(); {
            ItemStack ah = new ItemStack(Material.LEATHER_HELMET, 1);
            LeatherArmorMeta meta = (LeatherArmorMeta) ah.getItemMeta();
            meta.setDisplayName(ChatColor.DARK_AQUA + "Archer Helmet");
            meta.setColor(Color.BLUE);
            pi.setHelmet(ah);
            ah.setItemMeta(meta);
        }
        e.getWhoClicked(); {
            ItemStack ac = new ItemStack(Material.LEATHER_CHESTPLATE, 1);
            LeatherArmorMeta meta = (LeatherArmorMeta) ac.getItemMeta();
            meta.setDisplayName(ChatColor.DARK_AQUA + "Archer Chestplate");
            meta.setColor(Color.BLUE);
            pi.setChestplate(ac);
            ac.setItemMeta(meta);
        }
        e.getWhoClicked(); {
            ItemStack al = new ItemStack(Material.LEATHER_LEGGINGS, 1);
            LeatherArmorMeta meta = (LeatherArmorMeta) al.getItemMeta();
            meta.setDisplayName(ChatColor.DARK_AQUA + "Archer Leggings");
            meta.setColor(Color.BLUE);
            pi.setLeggings(al);
            al.setItemMeta(meta);
        }
        e.getWhoClicked(); {
            ItemStack ab = new ItemStack(Material.LEATHER_BOOTS, 1);
            LeatherArmorMeta meta = (LeatherArmorMeta) ab.getItemMeta();
            meta.setDisplayName(ChatColor.DARK_AQUA + "Archer Boots");
            meta.setColor(Color.BLUE);
            pi.setLeggings(ab);
            ab.setItemMeta(meta);
        }
        e.getWhoClicked();pi.addItem(new ItemStack(Material.ARROW, 32));
        e.getWhoClicked();pi.addItem(new ItemStack(Material.COOKED_BEEF, 8));
        e.getWhoClicked().closeInventory();


        //Tank Kit
    }

    if (e.getCurrentItem().getItemMeta().getDisplayName().contains("Tank")) {
        e.setCancelled(true);
        e.getWhoClicked();pi.addItem(new ItemStack(Material.WOOD_SWORD));
        e.getWhoClicked();pi.setHelmet(new ItemStack(Material.IRON_HELMET, 1));
        e.getWhoClicked();pi.setChestplate(new ItemStack(Material.IRON_CHESTPLATE, 1));
        e.getWhoClicked();pi.setLeggings(new ItemStack(Material.IRON_LEGGINGS, 1));
        e.getWhoClicked();pi.setBoots(new ItemStack(Material.IRON_BOOTS, 1));
        e.getWhoClicked();pi.addItem(new ItemStack(Material.COOKED_BEEF, 8));
        e.getWhoClicked().closeInventory();


        //Chicken Kit
    }

    if (e.getCurrentItem().getItemMeta().getDisplayName().contains("Chicken")) {
        e.setCancelled(true);
        e.getWhoClicked();pi.addItem(new ItemStack(Material.STONE_SWORD));
        e.getWhoClicked();pi.setHelmet(new ItemStack(Material.LEATHER_HELMET, 1));
        e.getWhoClicked();pi.setChestplate(new ItemStack(Material.CHAINMAIL_CHESTPLATE, 1));
        e.getWhoClicked();pi.setLeggings(new ItemStack(Material.CHAINMAIL_LEGGINGS, 1));
        e.getWhoClicked();pi.setBoots(new ItemStack(Material.LEATHER_BOOTS, 1));
        e.getWhoClicked();pi.addItem(new ItemStack(Material.ENDER_PEARL, 10));
        e.getWhoClicked();pi.addItem(new ItemStack(Material.COOKED_BEEF, 8));
        e.getWhoClicked().closeInventory();


        //Runner Kit
    }

    if (e.getCurrentItem().getItemMeta().getDisplayName().contains("Runner")) {
        e.setCancelled(true);
        e.getWhoClicked();pi.addItem(new ItemStack(Material.STONE_SWORD));
        e.getWhoClicked(); {
            ItemStack rb = new ItemStack(Material.DIAMOND_BOOTS);
            rb.addEnchantment(Enchantment.PROTECTION_ENVIRONMENTAL, 1);
        }
        e.getWhoClicked(); {
            p.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, 100000, 2));
        }
        e.getWhoClicked();pi.addItem(new ItemStack(Material.COOKED_BEEF, 8));
        e.getWhoClicked().closeInventory();

      }
   }
}

希望我能尽快解决这个问题。 如果您回答,谢谢。

1 个答案:

答案 0 :(得分:1)

看起来您的错误是:

public void show(Player p) {
    p.openInventory(inv);
}

Player p; //<<<<< HERE
PlayerInventory pi = p.getInventory();

永远不会初始化Player p,因此它默认为null。当您尝试在空NullPointerException上调用方法时,会得到Object,因此,当您尝试运行NullPointerException时,会得到p.getInventory(),这是null.getInventory() 1}},抛出NullPointerException

要解决此问题,您应该初始化p

Player p;
public void show(Player pl){
    p = pl; //p is no longer null, unless the method show(null) is called, otherwise, you're good to go!
    pl.openInventory(inv);
}

PlayerInventory pi = p.getInventory();