LanAnY
  1. public class saveItemCommand implements CommandExecutor {
  2.     @Override
  3.     public boolean onCommand(CommandSender commandSender, Command command, String s, String[] strings) {
  4.         JavaPlugin plugin= saveItem.getInstance();
  5.         Player player= (Player) commandSender;
  6.         ItemStack itemStack=player.getInventory().getItemInMainHand();
  7.         List<ItemStack> list=new ArrayList<ItemStack>();
  8.         if (command.getName().equalsIgnoreCase("saveItem")){
  9.             if (strings.length==1 && player.isOp() && strings[0].equalsIgnoreCase("save")){
  10.                 if (itemStack.getType().equals(new ItemStack(Material.AIR))){
  11.                     player.sendMessage("§c手中没有物品,无法被保存");
  12.                     return true;
  13.                 }else{
  14.                     list.add(itemStack);
  15.                     plugin.getConfig().set(itemStack.getItemMeta().getDisplayName(),list);
  16.                     plugin.saveConfig();
  17.                     player.sendMessage("§6物品保存成功!");
  18.                     return true;
  19.                 }
  20.             }else if (strings.length==1 && player.isOp() && strings[0].equalsIgnoreCase("get")){
  21.                 player.getInventory().addItem(plugin.getConfig().getItemStack(itemStack.getItemMeta().getDisplayName()));
  22.                 player.sendMessage("§6你获得了一个物品");
  23.                 return true;
  24.             }
  25.         }
  26.         return false;
  27.     }
复制代码
我输入了保存的指令后,后台报了一个null的错误,是我代码哪里错了么

纱夜
可以给出PluginCommand.java
里面完整代码+行数么

报错写了 第46行



另外 手中物品返回的可能是null

LanAnY
本帖最后由 sky295 于 2020-7-14 16:51 编辑
  1. package com.github.sky295.command;

  2. import com.github.sky295.saveItem;
  3. import org.bukkit.Material;
  4. import org.bukkit.command.Command;
  5. import org.bukkit.command.CommandExecutor;
  6. import org.bukkit.command.CommandSender;
  7. import org.bukkit.entity.Player;
  8. import org.bukkit.inventory.ItemStack;
  9. import org.bukkit.plugin.java.JavaPlugin;

  10. import java.util.ArrayList;
  11. import java.util.List;

  12. public class saveItemCommand implements CommandExecutor {
  13.     @Override
  14.     public boolean onCommand(CommandSender commandSender, Command command, String s, String[] strings) {
  15.         JavaPlugin plugin= saveItem.getInstance();
  16.         Player player= (Player) commandSender;
  17.         ItemStack itemStack=player.getInventory().getItemInMainHand();
  18.         List<ItemStack> list=new ArrayList<ItemStack>();
  19.         if (command.getName().equalsIgnoreCase("saveItem")){
  20.             if (strings.length==1 && player.isOp() && strings[0].equalsIgnoreCase("save")){
  21.                 if (itemStack.getType().equals(new ItemStack(Material.AIR))){
  22.                     player.sendMessage("§c手中没有物品,无法被保存");
  23.                     return true;
  24.                 }else{
  25.                     list.add(itemStack);
  26.                     plugin.getConfig().set(itemStack.getItemMeta().getDisplayName(),list);
  27.                     plugin.saveConfig();
  28.                     player.sendMessage("§6物品保存成功!");
  29.                     return true;
  30.                 }
  31.             }else if (strings.length==1 && player.isOp() && strings[0].equalsIgnoreCase("get")){
  32.                 player.getInventory().addItem(plugin.getConfig().getItemStack(itemStack.getItemMeta().getDisplayName()));
  33.                 player.sendMessage("§6你获得了一个物品");
  34.                 return true;
  35.             }
  36.         }
  37.         return false;
  38.     }
  39. }
复制代码
  1. [16:49:43] [Server thread/INFO]: GhostSkyV9 issued server command: /saveitem save
  2. [16:49:44] [Server thread/ERROR]: null
  3. org.bukkit.command.CommandException: Unhandled exception executing command 'saveitem' in plugin saveItem v1.0-Beta
  4.         at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[PluginCommand.class:git-CatServer-1.12.2-2124fc5]
  5.         at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:144) ~[SimpleCommandMap.class:git-CatServer-1.12.2-2124fc5]
  6.         at org.bukkit.craftbukkit.v1_12_R1.CraftServer.dispatchCommand(CraftServer.java:711) ~[CraftServer.class:git-CatServer-1.12.2-2124fc5]
  7.         at net.minecraft.network.NetHandlerPlayServer.func_147361_d(NetHandlerPlayServer.java:1594) [pa.class:?]
  8.         at net.minecraft.network.NetHandlerPlayServer.func_147354_a(NetHandlerPlayServer.java:1439) [pa.class:?]
  9.         at net.minecraft.network.play.client.CPacketChatMessage.func_148833_a(SourceFile:37) [la.class:?]
  10.         at net.minecraft.network.play.client.CPacketChatMessage.func_148833_a(SourceFile:9) [la.class:?]
  11.         at net.minecraft.network.PacketThreadUtil$1.run(SourceFile:13) [hv$1.class:?]
  12.         at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_221]
  13.         at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_221]
  14.         at net.minecraft.util.Util.func_181617_a(SourceFile:46) [h.class:?]
  15.         at net.minecraft.server.MinecraftServer.func_71190_q(MinecraftServer.java:839) [MinecraftServer.class:?]
  16.         at net.minecraft.server.dedicated.DedicatedServer.func_71190_q(DedicatedServer.java:472) [nz.class:?]
  17.         at net.minecraft.server.MinecraftServer.func_71217_p(MinecraftServer.java:776) [MinecraftServer.class:?]
  18.         at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:630) [MinecraftServer.class:?]
  19.         at java.lang.Thread.run(Unknown Source) [?:1.8.0_221]
  20. Caused by: java.lang.NullPointerException
  21.         at com.github.sky295.command.saveItemCommand.onCommand(saveItemCommand.java:29) ~[?:?]
  22.         at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[PluginCommand.class:git-CatServer-1.12.2-2124fc5]
  23.         ... 15 more
复制代码



LanAnY
阴阳师元素祭祀 发表于 2020-7-14 16:05
可以给出PluginCommand.java
里面完整代码+行数么

代码我重新改了一下,现在是第29行报错

南柯郡守
sky295 发表于 2020-7-14 16:52
代码我重新改了一下,现在是第29行报错

ItemStack不一定有DisplayName 要先判断

LanAnY
南柯郡守 发表于 2020-7-14 17:37
ItemStack不一定有DisplayName 要先判断

是因为这个错误么

Freeze_Dolphin
本帖最后由 Freeze_Dolphin 于 2020-7-14 18:42 编辑
sky295 发表于 2020-7-14 17:51
是因为这个错误么

还不一定有ItemMeta呢
可以这样:
public static String getDisplayName(ItemStack item) {
    if (!item.hasItemMeta()) return null;
    if (!item.getItemMeta().hasDisplayName()) return null;
    return item.getItemMeta().getDisplayName();
}

使用这个方法的时候先判断是不是null

String ds = getDisplayName(item);
if (ds != null) { ... }


William_Shi
Freeze_Dolphin 发表于 2020-7-14 18:38
还不一定有ItemMeta呢
可以这样:
public static String getDisplayName(ItemStack item) {

实际开发不怎么去判断是否有ItemMeta
因为调用getItemMeta时,就算是没有元数据的物品
Bukkit的Factory也会先新建一个ItemMeta然后返回给你
所以简洁一点写的话就直接getItemMeta hasDisplayName就可以了
其实也可以getOrCreateTag取到NBT混合数据包,在判断有没有display键,再找displayname,但是过于复杂不多说了免得把别人带进去