- public class saveItemCommand implements CommandExecutor {
- @Override
- public boolean onCommand(CommandSender commandSender, Command command, String s, String[] strings) {
- JavaPlugin plugin= saveItem.getInstance();
- Player player= (Player) commandSender;
- ItemStack itemStack=player.getInventory().getItemInMainHand();
- List<ItemStack> list=new ArrayList<ItemStack>();
- if (command.getName().equalsIgnoreCase("saveItem")){
- if (strings.length==1 && player.isOp() && strings[0].equalsIgnoreCase("save")){
- if (itemStack.getType().equals(new ItemStack(Material.AIR))){
- player.sendMessage("§c手中没有物品,无法被保存");
- return true;
- }else{
- list.add(itemStack);
- plugin.getConfig().set(itemStack.getItemMeta().getDisplayName(),list);
- plugin.saveConfig();
- player.sendMessage("§6物品保存成功!");
- return true;
- }
- }else if (strings.length==1 && player.isOp() && strings[0].equalsIgnoreCase("get")){
- player.getInventory().addItem(plugin.getConfig().getItemStack(itemStack.getItemMeta().getDisplayName()));
- player.sendMessage("§6你获得了一个物品");
- return true;
- }
- }
- return false;
- }
可以给出PluginCommand.java
里面完整代码+行数么
报错写了 第46行
另外 手中物品返回的可能是null
里面完整代码+行数么
报错写了 第46行
另外 手中物品返回的可能是null
本帖最后由 sky295 于 2020-7-14 16:51 编辑
复制代码复制代码
- package com.github.sky295.command;
- import com.github.sky295.saveItem;
- import org.bukkit.Material;
- import org.bukkit.command.Command;
- import org.bukkit.command.CommandExecutor;
- import org.bukkit.command.CommandSender;
- import org.bukkit.entity.Player;
- import org.bukkit.inventory.ItemStack;
- import org.bukkit.plugin.java.JavaPlugin;
- import java.util.ArrayList;
- import java.util.List;
- public class saveItemCommand implements CommandExecutor {
- @Override
- public boolean onCommand(CommandSender commandSender, Command command, String s, String[] strings) {
- JavaPlugin plugin= saveItem.getInstance();
- Player player= (Player) commandSender;
- ItemStack itemStack=player.getInventory().getItemInMainHand();
- List<ItemStack> list=new ArrayList<ItemStack>();
- if (command.getName().equalsIgnoreCase("saveItem")){
- if (strings.length==1 && player.isOp() && strings[0].equalsIgnoreCase("save")){
- if (itemStack.getType().equals(new ItemStack(Material.AIR))){
- player.sendMessage("§c手中没有物品,无法被保存");
- return true;
- }else{
- list.add(itemStack);
- plugin.getConfig().set(itemStack.getItemMeta().getDisplayName(),list);
- plugin.saveConfig();
- player.sendMessage("§6物品保存成功!");
- return true;
- }
- }else if (strings.length==1 && player.isOp() && strings[0].equalsIgnoreCase("get")){
- player.getInventory().addItem(plugin.getConfig().getItemStack(itemStack.getItemMeta().getDisplayName()));
- player.sendMessage("§6你获得了一个物品");
- return true;
- }
- }
- return false;
- }
- }
- [16:49:43] [Server thread/INFO]: GhostSkyV9 issued server command: /saveitem save
- [16:49:44] [Server thread/ERROR]: null
- org.bukkit.command.CommandException: Unhandled exception executing command 'saveitem' in plugin saveItem v1.0-Beta
- at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[PluginCommand.class:git-CatServer-1.12.2-2124fc5]
- at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:144) ~[SimpleCommandMap.class:git-CatServer-1.12.2-2124fc5]
- at org.bukkit.craftbukkit.v1_12_R1.CraftServer.dispatchCommand(CraftServer.java:711) ~[CraftServer.class:git-CatServer-1.12.2-2124fc5]
- at net.minecraft.network.NetHandlerPlayServer.func_147361_d(NetHandlerPlayServer.java:1594) [pa.class:?]
- at net.minecraft.network.NetHandlerPlayServer.func_147354_a(NetHandlerPlayServer.java:1439) [pa.class:?]
- at net.minecraft.network.play.client.CPacketChatMessage.func_148833_a(SourceFile:37) [la.class:?]
- at net.minecraft.network.play.client.CPacketChatMessage.func_148833_a(SourceFile:9) [la.class:?]
- at net.minecraft.network.PacketThreadUtil$1.run(SourceFile:13) [hv$1.class:?]
- at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_221]
- at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_221]
- at net.minecraft.util.Util.func_181617_a(SourceFile:46) [h.class:?]
- at net.minecraft.server.MinecraftServer.func_71190_q(MinecraftServer.java:839) [MinecraftServer.class:?]
- at net.minecraft.server.dedicated.DedicatedServer.func_71190_q(DedicatedServer.java:472) [nz.class:?]
- at net.minecraft.server.MinecraftServer.func_71217_p(MinecraftServer.java:776) [MinecraftServer.class:?]
- at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:630) [MinecraftServer.class:?]
- at java.lang.Thread.run(Unknown Source) [?:1.8.0_221]
- Caused by: java.lang.NullPointerException
- at com.github.sky295.command.saveItemCommand.onCommand(saveItemCommand.java:29) ~[?:?]
- at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[PluginCommand.class:git-CatServer-1.12.2-2124fc5]
- ... 15 more
阴阳师元素祭祀 发表于 2020-7-14 16:05
可以给出PluginCommand.java
里面完整代码+行数么
代码我重新改了一下,现在是第29行报错
sky295 发表于 2020-7-14 16:52
代码我重新改了一下,现在是第29行报错
ItemStack不一定有DisplayName 要先判断
南柯郡守 发表于 2020-7-14 17:37
ItemStack不一定有DisplayName 要先判断
是因为这个错误么
本帖最后由 Freeze_Dolphin 于 2020-7-14 18:42 编辑
还不一定有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) { ... }
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) { ... }
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,但是过于复杂不多说了免得把别人带进去