Development Assistance [Help] Getting a list of items from a config, and using it in an Inventory.

Discussion in 'Plugin Help/Development/Requests' started by Ricecutter0, Feb 22, 2015.

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

    Ricecutter0

    Hello, I was making this plugin, and I'm having this problem of getting a list of items from the config.yml file, and then using that data to open an inventory when right clicking a specific sign. If anyone could help me I'd really appreciate it. Thank you, and have a nice day.

    Main Class:
    Code:java
    1. package me.Ricecutter0.RandomRewards;
    2.  
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.ChatColor;
    5. import org.bukkit.plugin.java.JavaPlugin;
    6.  
    7. public class Main extends JavaPlugin {
    8. public static Main plugin;
    9.  
    10. ConfigManager cm = ConfigManager.getInstance();
    11.  
    12.  
    13. String prefix = color("&8[&3RandomRewards&8]");
    14.  
    15. @Override
    16. public void onEnable()
    17. {
    18. ConfigManager.setup(this);
    19. Bukkit.getPluginManager().registerEvents(new Rewards(), this);
    20. Bukkit.getPluginManager().registerEvents(new SignPlacement(), this);
    21.  
    22. }
    23.  
    24. @Override
    25. public void onDisable()
    26. {
    27.  
    28.  
    29. }
    30.  
    31. public static String color(String str){
    32. return str.replaceAll("&(?=[0-9a-fA-F])", Character.toString(ChatColor.COLOR_CHAR));
    33. }
    34.  
    35. }


    Listener Class:

    Code:java
    1. package me.Ricecutter0.RandomRewards;
    2.  
    3.  
    4.  
    5. import org.bukkit.Material;
    6. import org.bukkit.block.Block;
    7. import org.bukkit.block.Sign;
    8. import org.bukkit.configuration.file.FileConfiguration;
    9. import org.bukkit.entity.Player;
    10. import org.bukkit.event.EventHandler;
    11. import org.bukkit.event.Listener;
    12. import org.bukkit.event.block.Action;
    13. import org.bukkit.event.player.PlayerInteractEvent;
    14.  
    15.  
    16. public class Rewards implements Listener{
    17.  
    18. public static FileConfiguration config;
    19.  
    20. ConfigManager cm = ConfigManager.getInstance();
    21.  
    22. CommandManager cmm = CommandManager.getInstance();
    23.  
    24.  
    25.  
    26.  
    27. @EventHandler
    28. public void onPlayerInteract(PlayerInteractEvent e){
    29.  
    30. Player p = e.getPlayer();
    31.  
    32. Action a = e.getAction();
    33.  
    34. Block b = e.getClickedBlock();
    35.  
    36. if(a.equals(Action.RIGHT_CLICK_BLOCK)){
    37.  
    38. if(b.getType() == Material.SIGN_POST || b.getType() == Material.WALL_SIGN){
    39.  
    40. Sign sign = (Sign) b.getState();
    41.  
    42. String[] lines = sign.getLines();
    43.  
    44. if(lines[0].equals(Main.color("&1[Reward]"))){
    45.  
    46. if(!(p.hasPermission("random.open"))){
    47. return;
    48. }
    49.  
    50. cmm.openInv(p);
    51. }
    52. }
    53.  
    54. }
    55. }
    56.  
    57.  
    58.  
    59. }


    Inventory Class:

    Code:java
    1. package me.Ricecutter0.RandomRewards;
    2.  
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.Material;
    5. import org.bukkit.configuration.file.FileConfiguration;
    6. import org.bukkit.entity.Player;
    7. import org.bukkit.event.Listener;
    8. import org.bukkit.inventory.Inventory;
    9. import org.bukkit.inventory.ItemStack;
    10.  
    11. public class CommandManager implements Listener {
    12.  
    13.  
    14. Main plugin;
    15.  
    16. private CommandManager() {}
    17.  
    18. static CommandManager instance = new CommandManager();
    19.  
    20. public static CommandManager getInstance(){
    21. return instance;
    22. }
    23.  
    24. ConfigManager cm = ConfigManager.getInstance();
    25.  
    26. FileConfiguration cmg = cm.getConfig();
    27.  
    28. public void openInv(Player p){
    29. ItemStack item = new ItemStack(Material.DIRT, 1);
    30. Inventory inv = Bukkit.createInventory(null, cmg.getInt("inventorySize"), Main.color("&4Reward"));
    31. inv.addItem(item);
    32. p.openInventory(inv);
    33.  
    34.  
    35.  
    36.  
    37. }
    38. }
     
    Last edited: Feb 22, 2015
  2. Offline

    pie_flavor

    @Ricecutter0 First of all, a minor note: A better thing to type in for color() would be ChatColor.translateAlternateColorCodes('&',str);
    And you have not actually provided us with the class that should create the inventory.
     
  3. Offline

    Ricecutter0

    Oops. Added the class
     
  4. Offline

    pie_flavor

    @Ricecutter0 Oh I see, you just need code.
    You can literally just save a list of itemstacks to the config, and use getList() to get them again, just use a cast to List<ItemStack>.
     
  5. Offline

    Ricecutter0

    Okay.
     
    Last edited: Feb 28, 2015
Thread Status:
Not open for further replies.

Share This Page