c.setItem(Items, amount);

Discussion in 'Plugin Development' started by BrushPainter, Mar 13, 2014.

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

    BrushPainter

    Hey everyone, I'm trying to set the item and amount of item using a random amount from 1-10 and items that the user will set in the config. It has been going pretty well until now, I've been trying to find a way to add the items and amount to a chest that the player can spawn at their location using the command "/setchest1" but I get an error on line 61:
    Code:java
    1. package me.BrushPainter.RandomChests;
    2.  
    3. import java.util.List;
    4. import java.util.Random;
    5.  
    6. import org.bukkit.Location;
    7. import org.bukkit.Material;
    8. import org.bukkit.block.Block;
    9. import org.bukkit.block.Chest;
    10. import org.bukkit.command.Command;
    11. import org.bukkit.command.CommandSender;
    12. import org.bukkit.configuration.file.FileConfiguration;
    13. import org.bukkit.entity.Item;
    14. import org.bukkit.entity.Player;
    15. import org.bukkit.event.EventHandler;
    16. import org.bukkit.event.player.PlayerInteractEvent;
    17. import org.bukkit.inventory.ItemStack;
    18. import org.bukkit.plugin.java.JavaPlugin;
    19.  
    20. public class Main extends JavaPlugin{
    21.  
    22. public void onEnable() {
    23.  
    24. getLogger().info("RandomChests Enabled");
    25.  
    26. FileConfiguration config = getConfig();
    27.  
    28. config.addDefault("RandomChests.SetChest.Chest1.Item", "DIAMOND");
    29.  
    30. config.options().copyDefaults(true);
    31. saveConfig();
    32.  
    33. }
    34.  
    35. public void onDisable() {
    36.  
    37. getLogger().info("RandomChests Disabled");
    38.  
    39. }
    40.  
    41. int min = 1;
    42. int max = 10;
    43.  
    44. Random r = new Random();
    45. int amount = r.nextInt(max - min + 1) + min;
    46.  
    47. List<String> Items = getConfig().getStringList("Chests.SetChest.Chest1.Items");
    48.  
    49. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
    50.  
    51. if (sender.hasPermission("brushpainter.setchest.one")) {
    52. if(cmd.getName().equalsIgnoreCase("setchest1")) {
    53. if (sender instanceof Player) {
    54.  
    55. Player p = (Player) sender;
    56. Location l = p.getLocation();
    57. Chest c = (Chest) l;
    58.  
    59. c.getBlock().setType(Material.CHEST);
    60.  
    61. c.setItem(Items, amount);
    62.  
    63. }
    64. }
    65.  
    66. }
    67. }
    68. }
    The error is "The method setItem(List<String>, int) is undefined for the type Chest". Please help guys, thanks in advance. - Brush :)
     
  2. Offline

    ShadowLAX

    BrushPainter You get that error because the setItem() method does not have a List<String> parameter.
     
  3. Offline

    BrushPainter

    ShadowLAX Oh ok, know any other methods of doing this?
     
  4. Offline

    Shayana

    Here are some things you might want to see :
    http://jd.bukkit.org/rb/apidocs/org/bukkit/block/Chest.html
    http://jd.bukkit.org/rb/apidocs/org/bukkit/inventory/ItemStack.html
    http://jd.bukkit.org/rb/apidocs/org/bukkit/configuration/file/YamlConfiguration.html


    Now that being said, as you can see setItem requires the slot number and an ItemStack as parameters.
    On Bukkit's YamlConfiguration you can save and load ItemStacks directly, the way I commonly use is saving the Slot as the key and the ItemStack as its value. So you can use the setItem method directly with this.
     
  5. Offline

    BrushPainter

    Shayana Hmm I didn't really understand that, but it sounds like it would work. Mind showing me an example of what you mean?
     
  6. Offline

    Shayana

    Well, I didn't read your code when I answered, as I could answer the question. Now that it's done... You're going to have more than this undefined method. The language you are using is undefined too if I can say :/

    For instance, a Location cannot be cast to a Chest. To retrieve a Chest you must first get the Block at this location, and then cast it using (Chest) block.getState()

    My best advise would be to read some Java and Bukkit tutorials before trying to get a plugin done.
     
Thread Status:
Not open for further replies.

Share This Page