每当我使用Bukkit" containsEnchantment()"时,我都会得到nullpointerexception。方法,据说返回一个布尔值。此代码段的目的是检查播放器广告资源中的某个项目是否具有高于5的结界。
我的日志告诉在第408行抛出了一个nullpointer异常。在我的代码中,我在第408行留下了一条评论。
我试着用其他方法来检查该物品是否具有结界;但每当我使用" getEnchantments()时,我都会出现导出错误。"我在Eclipse中编译并导出我的插件,然后使用jd-GUI对其进行反编译以检查是否存在损坏。当我使用getEnchantments():java.util.Map时,jd-GUI显示// INTERNAL ERROR //当我反编译我的.jar时。使用地图不适用于Bukkit。
我不明白为什么我的代码不起作用。变量"播放器"是类型org.Bukkit.entity.Player。
以下是我的一些代码:
boolean illegalEnchants = false;
ArrayList<ItemStack> stacks = new ArrayList<ItemStack>();
Inventory i = player.getInventory();
if(i.getContents() != null){
for(ItemStack inven : i.getContents()){
ArrayList<Integer> enchants = new ArrayList<Integer>();
if(inven.containsEnchantment(Enchantment.ARROW_DAMAGE)){ //LINE 408
enchants.add(inven.getEnchantmentLevel(Enchantment.ARROW_DAMAGE));
}
if(inven.containsEnchantment(Enchantment.ARROW_FIRE)){
enchants.add(inven.getEnchantmentLevel(Enchantment.ARROW_FIRE));
}
if(inven.containsEnchantment(Enchantment.ARROW_INFINITE)){
enchants.add(inven.getEnchantmentLevel(Enchantment.ARROW_INFINITE));
}
if(inven.containsEnchantment(Enchantment.ARROW_KNOCKBACK)){
enchants.add(inven.getEnchantmentLevel(Enchantment.ARROW_KNOCKBACK));
}
if(inven.containsEnchantment(Enchantment.DAMAGE_ALL)){
enchants.add(inven.getEnchantmentLevel(Enchantment.DAMAGE_ALL));
}
if(inven.containsEnchantment(Enchantment.DAMAGE_ARTHROPODS)){
enchants.add(inven.getEnchantmentLevel(Enchantment.DAMAGE_ARTHROPODS));
}
if(inven.containsEnchantment(Enchantment.DAMAGE_UNDEAD)){
enchants.add(inven.getEnchantmentLevel(Enchantment.DAMAGE_UNDEAD));
}
if(inven.containsEnchantment(Enchantment.DEPTH_STRIDER)){
enchants.add(inven.getEnchantmentLevel(Enchantment.DEPTH_STRIDER));
}
if(inven.containsEnchantment(Enchantment.DIG_SPEED)){
enchants.add(inven.getEnchantmentLevel(Enchantment.DIG_SPEED));
}
if(inven.containsEnchantment(Enchantment.DURABILITY)){
enchants.add(inven.getEnchantmentLevel(Enchantment.DURABILITY));
}
if(inven.containsEnchantment(Enchantment.FIRE_ASPECT)){
enchants.add(inven.getEnchantmentLevel(Enchantment.FIRE_ASPECT));
}
if(inven.containsEnchantment(Enchantment.KNOCKBACK)){
enchants.add(inven.getEnchantmentLevel(Enchantment.KNOCKBACK));
}
if(inven.containsEnchantment(Enchantment.LOOT_BONUS_BLOCKS)){
enchants.add(inven.getEnchantmentLevel(Enchantment.LOOT_BONUS_BLOCKS));
}
if(inven.containsEnchantment(Enchantment.LOOT_BONUS_MOBS)){
enchants.add(inven.getEnchantmentLevel(Enchantment.LOOT_BONUS_MOBS));
}
if(inven.containsEnchantment(Enchantment.LUCK)){
enchants.add(inven.getEnchantmentLevel(Enchantment.LUCK));
}
if(inven.containsEnchantment(Enchantment.OXYGEN)){
enchants.add(inven.getEnchantmentLevel(Enchantment.OXYGEN));
}
if(inven.containsEnchantment(Enchantment.LURE)){
enchants.add(inven.getEnchantmentLevel(Enchantment.LURE));
}
if(inven.containsEnchantment(Enchantment.PROTECTION_ENVIRONMENTAL)){
enchants.add(inven.getEnchantmentLevel(Enchantment.PROTECTION_ENVIRONMENTAL));
}
if(inven.containsEnchantment(Enchantment.PROTECTION_EXPLOSIONS)){
enchants.add(inven.getEnchantmentLevel(Enchantment.PROTECTION_EXPLOSIONS));
}
if(inven.containsEnchantment(Enchantment.PROTECTION_FALL)){
enchants.add(inven.getEnchantmentLevel(Enchantment.PROTECTION_FALL));
}
if(inven.containsEnchantment(Enchantment.PROTECTION_FIRE)){
enchants.add(inven.getEnchantmentLevel(Enchantment.PROTECTION_FIRE));
}
if(inven.containsEnchantmen(tEnchantment.PROTECTION_PROJECTILE)){
enchants.add(inven.getEnchantmentLevel(Enchantment.PROTECTION_PROJECTILE));
}
if(inven.containsEnchantment(Enchantment.SILK_TOUCH)){
enchants.add(inven.getEnchantmentLevel(Enchantment.SILK_TOUCH));
}
if(inven.containsEnchantment(Enchantment.THORNS)){
enchants.add(inven.getEnchantmentLevel(Enchantment.THORNS));
}
if(inven.containsEnchantment(Enchantment.WATER_WORKER)){
enchants.add(inven.getEnchantmentLevel(Enchantment.WATER_WORKER));
}
for(int j : enchants){
if(j > 5){
illegalEnchants = true;
stacks.add(inven);
}
}
}
}
for(ItemStack is : stacks){
i.removeItem(is);
}
这是我的服务器日志:
10.03 21:54:54 [服务器] INFO at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) 〜[craftbukkit.jar:GIT-Bukkit-880a532] 10.03 21:54:54 [服务器] INFO at me.pyro.main.Main.onCommand(Main.java:408)〜[?:?] 10.03 21:54:54 [服务器] INFO引起:java.lang.NullPointerException 10.03 21:54:54 [服务器] INFO at java.lang.Thread.run(Thread.java:745)[?:1.8.0_45] 10.03 21:54:54 [服务器] INFO at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:536) [craftbukkit.jar:GIT-Bukkit-880a532] 10.03 21:54:54 [服务器] INFO at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:628) [craftbukkit.jar:GIT-Bukkit-880a532] 10.03 21:54:54 [服务器] INFO at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:335) [craftbukkit.jar:GIT-Bukkit-880a532] 10.03 21:54:54 [服务器] INFO at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:672) [craftbukkit.jar:GIT-Bukkit-880a532] 10.03 21:54:54 [服务器] INFO at net.minecraft.server.v1_8_R3.SystemUtils.a(SourceFile:44) [craftbukkit.jar:GIT-Bukkit-880a532] 10.03 21:54:54 [服务器] INFO at java.util.concurrent.FutureTask.run(FutureTask.java:266)[?:1.8.0_45] 10.03 21:54:54 [服务器] INFO at java.util.concurrent.Executors $ RunnableAdapter.call(Executors.java:511) [:?1.8.0_45] 10.03 21:54:54 [服务器] INFO at net.minecraft.server.v1_8_R3.PlayerConnectionUtils $ 1.run(SourceFile:13) [craftbukkit.jar:GIT-Bukkit-880a532] 10.03 21:54:54 [服务器] INFO at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(SourceFile:9) [craftbukkit.jar:GIT-Bukkit-880a532] 10.03 21:54:54 [服务器] INFO at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(SourceFile:37) [craftbukkit.jar:GIT-Bukkit-880a532] 10.03 21:54:54 [服务器] INFO at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java:966) [craftbukkit.jar:GIT-Bukkit-880a532] 10.03 21:54:54 [服务器] INFO at net.minecraft.server.v1_8_R3.PlayerConnection.handleCommand(PlayerConnection.java:1106) [craftbukkit.jar:GIT-Bukkit-880a532] 10.03 21:54:54 [服务器] INFO at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchCommand(CraftServer.java:620) 〜[craftbukkit.jar:GIT-Bukkit-880a532] 10.03 21:54:54 [服务器] INFO at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:140) 〜[craftbukkit.jar:GIT-Bukkit-880a532] 10.03 21:54:54 [服务器] INFO at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) 〜[craftbukkit.jar:GIT-Bukkit-880a532] 10.03 21:54:54 [服务器] INFO org.bukkit.command.CommandException:未处理的异常执行命令&#39; removeitems&#39;在插件中 AntiInventoryCheat v1.8 10.03 21:54:54 [服务器] ERROR null
答案 0 :(得分:2)
代码中的第408行(以及引用inven
的所有其他行)可以抛出NullPointerException
,因为您重复执行的广告资源中的ItemStack
可能是{{1}在空槽的情况下。空检查将解决此问题,例如:
null
答案 1 :(得分:0)
int getEnchantmentLevel(Enchantment ench) Gets the level of the specified enchantment on this item stack
getEnchantmentLevel 返回一个 int,但您的数组列表是 Integer。会不会是这样?