ItemStack的奇怪行为

时间:2017-06-09 03:28:10

标签: java nullpointerexception minecraft bukkit

出于某些淫秽的原因,当奖励=“DIAMOND”且金额= 10

时,这是有效的
public ItemStack giveReward() {

    return new ItemStack(Material.matchMaterial(reward), amount);
}

p.getInventory().addItem(o.giveReward()); //gives the player 10 DIAMONDS

但是当reward =“ACACIA_DOOR”且amount = 1时,同样的方法会给玩家带来NOTHING并且不会抛出任何错误。我不知道为什么。还

System.out.println(Material.getMaterial("ACACIA_DOOR"))

打印ACACIA_DOOR所以上面的代码不应该有用吗?

这是代码的其余部分:

//imports omitted
public class ObjectivesRPG extends JavaPlugin implements Listener {

//TODO
//add view command
//implement rewards and requirements
//test for completeness
//future - allow ops to modify player data


public static void main(String args[]) {
    Objective o = new Objective("Spider", 1, 3, "DIAMOND", 1);
    Material m = Material.getMaterial("ACACIA_DOOR");
    System.out.println(m);

    //meta.setDisplayName(ChatColor.GOLD + "Excaliber");
    //meta.setLore(Arrays.asList(ChatColor.AQUA + "The legendary sword", ChatColor.GREEN + "Wow"));
    //sword.setItemMeta(meta);
    //System.out.println(o.getName());
    /*
    System.out.println(Material.DIAMOND_SWORD);

    ItemStack stack = new ItemStack(Material.DIAMOND_SWORD, 1);
    ItemMeta meta = stack.getItemMeta();
    stack.setItemMeta(meta);*/
}

private ArrayList<Objective> objectives = null;
private HashMap<String, ArrayList<Objective>> loadedPlayerData = null;

@SuppressWarnings("unchecked")
@Override
public void onEnable() {
    System.out.println("ObjectivesRPG loaded");
    loadedPlayerData = new HashMap<>();
    File dir = getDataFolder();
    if (!dir.exists()) {
        Bukkit.getConsoleSender().sendMessage(ChatColor.YELLOW + "[ObjectivesRPG] Could not find data directory, creating it");
        if (!dir.mkdir()) {
            System.out.println("Error: Could not create data directory");
        }
    }

    objectives = (ArrayList<Objective>) load(new File(getDataFolder(), "objectives.dat"));

    if (objectives == null) {
        objectives = new ArrayList<>();
    }

    getServer().getPluginManager().registerEvents(this, this); // ParamListener,
                                                                // ParamPlugin
}

@Override
public boolean onCommand(CommandSender sender, Command cmd, String label, String args[]) {

    if (label.equalsIgnoreCase("objectives")) {
        if (sender instanceof Player) {
            Player p = (Player) sender;

            if(args.length == 0) {
                for(Objective o: loadedPlayerData.get(p.getName())) {
                    p.sendMessage(o.getName() + " " + o.getTillComplete() + " ");
                }
            }


            if (args.length > 0) {
                if (args[0].equals("create")) {
                    if (!p.isOp()) {
                        p.sendMessage(ChatColor.RED + "You must be op to use this command");
                    } else if (args.length == 6) {

                        Objective objective = new Objective(args[1] ,Integer.parseInt(args[2]), Integer.parseInt(args[3]), args[4], Integer.parseInt(args[5]));
                        objectives.add(objective);
                        save(objectives, new File(getDataFolder(), "objectives.dat"));

                    } else {
                        p.sendMessage(ChatColor.RED + "Error: Bad arguments.");
                    }
                }


            }
        } 

    }

    return true;
}

public void onDisable() {
    save(objectives, new File(getDataFolder(), "objectives.dat"));
}

public void save(Object o, File f) {
    try {
        if (!f.exists()) {
            f.createNewFile();
        }

        ObjectOutputStream os = new ObjectOutputStream(new FileOutputStream(f));
        os.writeObject(o);
        Bukkit.getConsoleSender().sendMessage("[ObjectivesRPG] Saved objective");
        os.flush();
        os.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

public Object load(File f) {
    try {
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream(f));
        Object result = ois.readObject();
        ois.close();
        return result;
    } catch (Exception e) {
        return null;
    }
}


@EventHandler
private void checkKills(EntityDeathEvent e) {
    Entity killed = e.getEntity();
    Entity killer = e.getEntity().getKiller();

    if(killer instanceof Player) {
        Player p = (Player) killer;
        for(Objective o: loadedPlayerData.get(p.getName())) {
            if(o.isComplete()) {
                continue;
            }

            if(!o.isComplete() && (o.getRequirement() == Requirement.kill_Spiders && killed instanceof Spider ||
                                   o.getRequirement() == Requirement.kill_Zombies && killed instanceof Zombie) ||
                                   o.getRequirement() == Requirement.kill_Skeletons && killed instanceof Skeleton
                                   ) {


                o.setTillComplete(o.getTillComplete() - 1);
                if(o.getTillComplete() == 0) {
                    p.sendMessage(ChatColor.GREEN + "Congragulations! You completed objective " + o.getName() + "! Here is your reward!");
                    p.getInventory().addItem(o.giveReward());
                    o.setComplete(true);
                }
            }

        }
    }
}

@EventHandler
private void onQuit(PlayerQuitEvent event) {
    Player player = event.getPlayer();
    File f = new File(getDataFolder(), player.getName());
    save(loadedPlayerData.get(player.getName()), f);
    loadedPlayerData.remove(player.getName());
}


@EventHandler
private void onJoin(PlayerJoinEvent event) {
    Player player = event.getPlayer();
    File f = new File(getDataFolder(), player.getName());
    ArrayList<Objective> playerObjectives = null;
    try {
        if(!f.exists()) {
            f.createNewFile();
            Bukkit.getConsoleSender().sendMessage("[ObjectivesRPG] Could not find " + player.getName() + "'s objective data, creating it");
            playerObjectives = new ArrayList<>();
            for(Objective objective: objectives) {
                playerObjectives.add(objective);
            }
            player.sendMessage(ChatColor.GREEN + "You have new objective(s) to complete! Type /objectives to view them.");
            save(playerObjectives, f);
        } else {
            playerObjectives = (ArrayList<Objective>) load(f);
            System.out.println(objectives.size() + " " + playerObjectives.size());
            //If server objective list is larger than playerObjectives new objectives must be added to player list
            if(objectives.size() > playerObjectives.size()) {
                player.sendMessage(ChatColor.GREEN + "You have new objective(s) to complete! Type /objectives to view them.");
                for(int i = 0; i < objectives.size(); i++) {
                    boolean objectiveAdded = false;
                    for(int j = 0; j < playerObjectives.size(); j++) {
                        if(objectives.get(i).getName().equals(playerObjectives.get(j).getName())) {
                            objectiveAdded = true;
                            //break;
                        }
                    }
                    if(!objectiveAdded) {
                        playerObjectives.add(objectives.get(i));
                    }
                }
                save(playerObjectives, f);

            }
        }
        loadedPlayerData.put(player.getName(), playerObjectives);

    } catch(Exception e) {
        e.printStackTrace();
    }

}

}

public class Objective implements Serializable {

private static final long serialVersionUID = -2018456670240873538L;

private static ArrayList<Requirement> requirements = new ArrayList<>();

private String name;
private Requirement requirement;
private String reward;
private int amount;

private int tillComplete;

private boolean complete;

public Objective(String name, int requirementIndex, int tillComplete, String reward, int amount) {

    if(requirements.isEmpty()) {
        requirements.add(Requirement.kill_Skeletons);
        requirements.add(Requirement.kill_Spiders);
        requirements.add(Requirement.kill_Zombies);

    }


    this.name = name;
    this.requirement = requirements.get(requirementIndex);
    this.tillComplete = tillComplete;
    this.reward = reward;
    this.amount = amount;
    complete = false;
}

public ItemStack giveReward() {

    return new ItemStack(Material.matchMaterial(reward), amount);
}

public String getName() {
    return name;
}

public Object getRequirement() {
    return requirement;
}

public static ArrayList<Requirement> getRequirements() {
    return requirements;
}

public static void setRequirements(ArrayList<Requirement> requirements) {
    Objective.requirements = requirements;
}

public int getTillComplete() {
    return tillComplete;
}

public void setTillComplete(int tillComplete) {
    this.tillComplete = tillComplete;
}

public void setName(String name) {
    this.name = name;
}

public void setRequirement(Requirement requirement) {
    this.requirement = requirement;
}

public void setReward(String reward) {
    this.reward = reward;
}

public void setComplete(boolean complete) {
    this.complete = complete;
}

public String getReward() {
    return reward;
}

public boolean isComplete() {
    return complete;
}

}

1 个答案:

答案 0 :(得分:4)

这使人不止一次。门是双组分物品。 ACACIA_DOOR代表门的顶部,而ACACIA_DOOR_ITEM代表底部,也代表项ID。创建ACACIA_DOOR_ITEM时使用ItemStack

提示:如果您不确定某个商品ID,请以创意模式启动Minecraft,然后按 F3 + 高级工具提示 >ħ。当您将鼠标悬停在广告素材库存中的项目上时,实际商品ID将显示在工具提示中。例如,将悬停在Acacia Door上方显示

Acacia Door (#0430)

使用此信息在Material中查找相应的org.bukkit.Material枚举,在本例中为ACACIA_DOOR_ITEM