Solved Repeating Task Help.

Discussion in 'Plugin Help/Development/Requests' started by oceantheskatr, Apr 10, 2015.

Thread Status:
Not open for further replies.
  1. Offline

    oceantheskatr

    Hi, I'm working on an RPG-style plugin. I have made it so that every second 1 hunger is regenerated (half a hunger thing techincally, as full would be 20). I am trying to make it so that if a player is wearing a leather helmet with a display name that contains "Mana Boost" then it will give + 8 hunger. I chose +8 to be able to easily see if it's working or not. In my code you'll also see it checking for an apple, this was to ensure that having items in your inventory can affect how much hunger you get. This works.

    However for the helmet I get an error in my console: http://hastebin.com/upayofexoz.avrasm
    (Line 51 is line with the words "Mana Boost" below.)
    I do not get any errors in eclipse.

    Code:
        @Override
        public void onEnable() {
            PluginManager pm = getServer().getPluginManager();
            pm.addPermission(wandsDrain);
            Bukkit.getServer().getPluginManager().registerEvents(this, this);
            this.getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
                  public void run() {
                      Player[] players = Bukkit.getServer().getOnlinePlayers();
                      for (int i = 0; i < players.length; i++) {
                          players[i].setFoodLevel(players[i].getFoodLevel() + 1);
                          if (players[i].getInventory().getHelmet().getItemMeta().getDisplayName().contains("Mana Boost")) {
                              players[i].setFoodLevel(players[i].getFoodLevel() + 8);
                          }
                         
                          if (players[i].getInventory().contains(Material.APPLE)) {
                              players[i].setFoodLevel(players[i].getFoodLevel() + 2);
                          }
                         
                        }
                  }
                }, 0L, 20L);
        }
    I hope someone is able to help me. If you need any more clarification just let me know :)
     
  2. Offline

    BizarrePlatinum

    @oceantheskatr might be wrong here, can't check over myself at the moment, but I believe display name isn't saved in item meta, trying changing it to getHelmet().getDisplayName().
     
  3. @oceantheskatr Check of they have a helmet and it has an item meta
     
  4. Offline

    oceantheskatr

    Thank you for your reply, though it didn't seem to work.

    I have added this:

    if (players.getInventory().getHelmet().hasItemMeta() == true) {

    How would I check to ensure the item meta is "Mana Boost"?
     
    Last edited by a moderator: Apr 10, 2015
  5. Offline

    nverdier

    @oceantheskatr The item meta is never equal to "Mana Boost". First you check if it has the meta, like you have done. Then, if it does, you get the item meta and get the display name.
     
    oceantheskatr likes this.
  6. Offline

    oceantheskatr

    YES! Thank you so much! This worked perfectly :D

    Also thank you to @bwfcwalshy and @BizarrePlatinum for your contributions to help me :)
     
  7. Offline

    nverdier

    oceantheskatr likes this.
Thread Status:
Not open for further replies.

Share This Page