Solved The method addItem(ItemStack...) in the type Inventory is not applicable......

Discussion in 'Spigot Plugin Development' started by Zendovo, Jun 1, 2017.

  1. Sorry, this is my second post of the day.....:oops:

    But, after I fixed other stuff. Another error popped up when I tried to use
    Code (Text):
    player.getInventory().addItem(new ItemStack(bID), fortune);
    So the error says The method addItem(ItemStack...) in the type Inventory is not applicable for the arguments (ItemStack, int)

    And now I am stuck here.:(

    Here is my code:
    Code (Text):
    package me.zendovio.fortuneblocks;

    import java.io.File;

    import org.bukkit.GameMode;
    import org.bukkit.Material;
    import org.bukkit.block.Block;
    import org.bukkit.enchantments.Enchantment;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.BlockBreakEvent;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.inventory.ItemStack;

    public class Main
      extends JavaPlugin
      implements Listener
    {
      public void onEnable(){
        getServer().getPluginManager().registerEvents((this), this);
        if (!new File(getDataFolder(), "config.yml").exists())
        {
        saveDefaultConfig();
      }
    }
     
      public void onDisable(){
        getServer().getLogger().info("FortuneBlocks has been disabled!");
        saveConfig();
        reloadConfig();
      }
     
      @EventHandler
      public void onBreak(BlockBreakEvent e){
          Player player = e.getPlayer();
          Block block = (Block) e.getBlock();
          String blockid = String.valueOf(block);
          if ((player.hasPermission("fortuneblocks.use")) &&
                  (player.getGameMode().equals(GameMode.SURVIVAL))) {
              if(player.getWorld().getName().equalsIgnoreCase(getConfig().getString("WorldName")))
              {
                  if(player.getItemInHand().containsEnchantment(Enchantment.LOOT_BONUS_BLOCKS))
                  {
                      int fortune = player.getItemInHand().getEnchantmentLevel(Enchantment.LOOT_BONUS_BLOCKS);
                      e.getBlock().setType(Material.AIR);
                      e.getBlock().getDrops().clear();
                      Material bID = Material.valueOf(blockid);
                      player.getInventory().addItem(new ItemStack(bID), fortune);
                  }
                  }
              }
          }
         
      }

     
     
  2. ScarabCoder

    ScarabCoder Retired Resource Staff
    Retired

    Change
    Code (Text):
    player.getInventory().addItem(new ItemStack(bID), fortune);
    To
    Code (Text):
    player.getInventory().addItem(new ItemStack(bID, fortune));
    You were putting the fortune variable (assuming it's the amount of items) into the addItem function rather then the ItemStack function. ItemStacks are what they are called; stacks of items.
     
    • Like Like x 1
  3. First Thanks for helping me fix it! :)
    Now I ran the plugin and an error came
    [21:35:07 ERROR]: Could not pass event BlockBreakEvent to FortuneBlocks v1.0
    org.bukkit.event.EventException
    at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:310) ~[spigot.jar:git-Spigot-21fe707-e1ebe52]
    at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[spigot.jar:git-Spigot-21fe707-e1ebe52]
    at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:502) [spigot.jar:git-Spigot-21fe707-e1ebe52]
    at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:487) [spigot.jar:git-Spigot-21fe707-e1ebe52]
    at net.minecraft.server.v1_8_R3.PlayerInteractManager.breakBlock(PlayerInteractManager.java:286) [spigot.jar:git-Spigot-21fe707-e1ebe52]
    at net.minecraft.server.v1_8_R3.PlayerInteractManager.a(PlayerInteractManager.java:215) [spigot.jar:git-Spigot-21fe707-e1ebe52]
    at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java:637) [spigot.jar:git-Spigot-21fe707-e1ebe52]
    at net.minecraft.server.v1_8_R3.PacketPlayInBlockDig.a(SourceFile:40) [spigot.jar:git-Spigot-21fe707-e1ebe52]
    at net.minecraft.server.v1_8_R3.PacketPlayInBlockDig.a(SourceFile:10) [spigot.jar:git-Spigot-21fe707-e1ebe52]
    at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$1.run(SourceFile:13) [spigot.jar:git-Spigot-21fe707-e1ebe52]
    at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_112]
    at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_112]
    at net.minecraft.server.v1_8_R3.SystemUtils.a(SourceFile:44) [spigot.jar:git-Spigot-21fe707-e1ebe52]
    at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:715) [spigot.jar:git-Spigot-21fe707-e1ebe52]
    at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:374) [spigot.jar:git-Spigot-21fe707-e1ebe52]
    at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:654) [spigot.jar:git-Spigot-21fe707-e1ebe52]
    at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:557) [spigot.jar:git-Spigot-21fe707-e1ebe52]
    at java.lang.Thread.run(Unknown Source) [?:1.8.0_112]
    Caused by: java.lang.IllegalArgumentException: No enum constant org.bukkit.Material.CraftBlock{chunk=CraftChunk{x=-1z=0},x=-5,y=111,z=5,type=DIAMOND_BLOCK,data=0}
    at java.lang.Enum.valueOf(Unknown Source) ~[?:1.8.0_112]
    at org.bukkit.Material.valueOf(Material.java:1) ~[spigot.jar:git-Spigot-21fe707-e1ebe52]
    at me.zendovio.fortuneblocks.Main.onBreak(Main.java:48) ~[?:?]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_112]
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_112]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_112]
    at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_112]
    at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:306) ~[spigot.jar:git-Spigot-21fe707-e1ebe52]
    ... 17 more
    Code (Text):
    package me.zendovio.fortuneblocks;

    import java.io.File;

    import org.bukkit.GameMode;
    import org.bukkit.Material;
    import org.bukkit.block.Block;
    import org.bukkit.enchantments.Enchantment;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.BlockBreakEvent;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.inventory.ItemStack;

    public class Main
      extends JavaPlugin
      implements Listener
    {
      public void onEnable(){
        getServer().getPluginManager().registerEvents((this), this);
        if (!new File(getDataFolder(), "config.yml").exists())
        {
        saveDefaultConfig();
      }
    }

      public void onDisable(){
        getServer().getLogger().info("FortuneBlocks has been disabled!");
        saveConfig();
        reloadConfig();
      }

      @EventHandler
      public void onBreak(BlockBreakEvent e){
          Player player = e.getPlayer();
          Block block = (Block) e.getBlock();
          String blockid = String.valueOf(block);
          if ((player.hasPermission("fortuneblocks.use")) &&
                  (player.getGameMode().equals(GameMode.SURVIVAL))) {
              if(player.getWorld().getName().equalsIgnoreCase(getConfig().getString("WorldName")))
              {
                  if(player.getItemInHand().containsEnchantment(Enchantment.LOOT_BONUS_BLOCKS))
                  {
                      int fortune = player.getItemInHand().getEnchantmentLevel(Enchantment.LOOT_BONUS_BLOCKS);
                      e.getBlock().setType(Material.AIR);
                      e.getBlock().getDrops().clear();
                      Material bID = Material.valueOf(blockid);
                      player.getInventory().addItem(new ItemStack(bID, fortune));
                  }
                  }
              }
          }
       
      }

     
     
  4. You're taking a String directly from a block. That doesn't make sense. You need to get the type of the block and then convert it into a string.
     
  5. BTW, I really don't know how to do that.....Can you give me more details. :p
    Thanks in advance
     
  6. Change this:
    Code (Text):
    String blockid = String.valueOf(block);
    To this:
    Code (Text):
    Material blockType = block.getType();
    getType() will give you the Enum Material. For example if you mined an emerald ore, this would be EMERALD_ORE. There's no reason to convert this Material to String and later on convert the String back to Material. (This is what you're currently trying to do.)

    -----------------------------------

    Also, change this:
    Code (Text):
    Material bID = Material.valueOf(blockid);
    player.getInventory().addItem(new ItemStack(bID, fortune));
    To this:
    Code (Text):
    player.getInventory().addItem(new ItemStack(blockType, fortune));
    This will make a new ItemStack with the Material we got from the block before, and add it to the player's inventory.

    -----------------------------------

    Edit: There's no need to cast e.getBlock() to Block, because e.getBlock already returns Block. Just change this:
    Code (Text):
    Block block = (Block) e.getBlock();
    To this:
    Code (Text):
    Block block = e.getBlock();
     
    • Agree Agree x 3
  7. Thanks for helping me out!