[ASAP] How would I put something in the config into my code?

Discussion in 'Plugin Development' started by ThePluginMaker, Aug 2, 2014.

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

    ThePluginMaker

    This is my config:
    Code:
    mysql:
     host: localhost
     port: 3306
     username: root
     passowrd: password
    region: {}
    StartArmor:
     Helmet: IRON_HELMET
     Chestplate: IRON_CHESTPLATE
     Leggings: IRON_LEGGINGS
     Boots: IRON_BOOTS
    This is what i want this to replace:
    Code:java
    1. public void addArmor(Player p) {
    2. PlayerInventory pInv = p.getInventory();
    3. pInv.setChestplate(new ItemStack(Material.IRON_CHESTPLATE, 1));
    4. pInv.setBoots(new ItemStack(Material.IRON_BOOTS, 1));
    5. pInv.setHelmet(new ItemStack(Material.IRON_HELMET, 1));
    6. pInv.setLeggings(new ItemStack(Material.IRON_LEGGINGS, 1));
    7. }

    And no this is not in my main class file, just to let you know. My main class file is: FreeForAll
     
  2. ThePluginMaker
    Code:
    Material.getMaterial(FreeForAll.getConfig().getString("StartArmor.Helmet"));
     
    ThePluginMaker likes this.
  3. Offline

    ThePluginMaker

  4. Offline

    ZodiacTheories

    ThePluginMaker

    Make an instance of the FreeForAll class and pass it through a Constructor
     
  5. Offline

    ThePluginMaker

    ZodiacTheories
    Ik a Constructor is like: blah = blah;
    but how would i do it for mine, sorry to ask like a noob, haven't dealt much with config...
     
  6. ThePluginMaker
    With an instance you could use something like FreeForAll.instance().getConfig() (etc.)
    Here's an example of setting an instance.
    Code:java
    1. import org.bukkit.event.EventHandler;
    2. import org.bukkit.event.Listener;
    3. import org.bukkit.event.player.PlayerJoinEvent;
    4. import org.bukkit.plugin.java.JavaPlugin;
    5.  
    6. public class Main extends JavaPlugin implements Listener {
    7.  
    8. private static JavaPlugin instance;
    9.  
    10. @Override
    11. public void onEnable() {
    12. instance = this;
    13. getServer().getPluginManager().registerEvents(this, this);
    14. }
    15.  
    16. @Override
    17. public void onDisable() {
    18. }
    19.  
    20. @EventHandler
    21. public void onEvent(PlayerJoinEvent event) {
    22. if (!event.getPlayer().hasPlayedBefore()) {
    23. }
    24. }
    25.  
    26. public static JavaPlugin instance() {
    27. return instance;
    28. }
    29. }
    30.  
     
    ThePluginMaker likes this.
  7. Offline

    ThePluginMaker

    This is what i have so far:
    Code:java
    1. public void addArmor(Player p) {
    2. PlayerInventory pInv = p.getInventory();
    3. pInv.setBoots(new ItemStack(Material.getMaterial(FreeForAll.getConfig().getString("StartingArmor.Boots"))));
    4. pInv.setHelmet(new ItemStack(Material.getMaterial(FreeForAll.getConfig().getString("StartingArmor.Helmet"))));
    5. pInv.setLeggings(new ItemStack(Material.getMaterial(FreeForAll.getConfig().getString("StartingArmor.Leggings"))));
    6. }
    7.  
    8. public void addArmor1(Player p) {
    9. PlayerInventory pInv = p.getInventory();
    10. pInv.setChestplate(new ItemStack(Material.getMaterial(FreeForAll.getConfig().getString("ArmorUgrade.Chestplate"))));
    11. pInv.setBoots(new ItemStack(Material.getMaterial(FreeForAll.getConfig().getString("ArmorUgrade.Boots"))));
    12. pInv.setHelmet(new ItemStack(Material.getMaterial(FreeForAll.getConfig().getString("ArmorUgrade.Helmet"))));
    13. pInv.setLeggings(new ItemStack(Material.getMaterial(FreeForAll.getConfig().getString("ArmorUgrade.Leggings"))));
    14. }
    15.  
    16. public void addItems(Player p) {
    17. PlayerInventory pInv = p.getInventory();
    18. pInv.addItem(new ItemStack(Material.getMaterial(FreeForAll.getConfig().getString("StartingInventory.Slot1"))));
    19. pInv.addItem(new ItemStack(Material.getMaterial(FreeForAll.getConfig().getString("StartingInventory.Slot2"))));
    20. pInv.addItem(new ItemStack(Material.getMaterial(FreeForAll.getConfig().getString("StartingInventory.Slot3"))));
    21. int damageval = 16421;
    22. for (int i = 0; i < 6; i++)
    23. pInv.addItem(new ItemStack(Material.POTION, 1, (short)damageval));
    24. }

    Code:code
    1. mysql:
    2. host: localhost
    3. port: 3306
    4. username: root
    5. passowrd: password
    6. region: {}
    7. # This will be their armor they start with.
    8. StartingArmor:
    9. Helmet: IRON_HELMET
    10. Chestplate: IRON_CHESTPLATE
    11. Leggings: IRON_LEGGINGS
    12. Boots: IRON_BOOTS
    13. # This will be their armor they get for the armor upgrade.
    14. ArmorUgrade:
    15. Helmet: DIAMOND_HELMET
    16. Chestplate: DIAMOND_CHESTPLATE
    17. Leggings: DIAMOND_LEGGINGS
    18. Boots: DIAMOND_BOOTS
    19. # This will be their inventory/hotbar
    20. StartingInventory:
    21. Slot1: IRON_SWORD
    22. Amount1: 1
    23. Slot2: IRON_AXE
    24. Amount2: 1
    25. Slot3: COOKED_BEEF
    26. Amount3: 16
    27. # The rest of the 6 slots will be instant splash potion of healing.


    That seemed to fix the errors, let me try and see if it works :)

    DJSkepter OMG sir i love you so much right now.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 9, 2016
  8. ThePluginMaker That's my template for every Bukkit plugin I create. Speeds up the typing process and helps with getting instances easily :)
     
  9. Offline

    ThePluginMaker

    DJSkepter
    How could i make enchantments/more items/potion effects heres my config:
    Code:code
    1. mysql:
    2. host: localhost
    3. port: 3306
    4. username: root
    5. passowrd: password
    6. region: {}
    7. # {StartingArmor} Will be their armor they start with.
    8. StartingArmor:
    9. Helmet: IRON_HELMET
    10. Chestplate: IRON_CHESTPLATE
    11. Leggings: IRON_LEGGINGS
    12. Boots: IRON_BOOTS
    13. # {ArmorUpgrade} Will be their armor they get for the armor upgrade.
    14. ArmorUgrade:
    15. Helmet: DIAMOND_HELMET
    16. Chestplate: DIAMOND_CHESTPLATE
    17. Leggings: DIAMOND_LEGGINGS
    18. Boots: DIAMOND_BOOTS
    19. KillStreaks:
    20. 2ndKillItem1: ARROW
    21. 2ndKillItem2:
    22. 2ndKillItem3:
    23. 2ndKillItem4:
    24. 3rdKillItem1:
    25. 3rdKillItem2:
    26. 3rdKillItem3:
    27. 3rdKillItem4:
    28. 4thKillItem1:
    29. 4thKillItem2:
    30. 4thKillItem3:
    31. 4thKillItem4:
    32. 5thKillItem1:
    33. 5thKillItem2:
    34. 5thKillItem3:
    35. 5thKillItem4:
    36. 6thKillItem1:
    37. 6thKillItem2:
    38. 6thKillItem3:
    39. 6thKillItem4:
    40. 7thKillItem1:
    41. 7thKillItem2:
    42. 7thKillItem3:
    43. 8thKillItem4:
    44. 8thKillItem1:
    45. 8thKillItem2:
    46. 8thKillItem3:
    47. 8thKillItem4:
    48. 9thKillItem1:
    49. 9thKillItem2:
    50. 9thKillItem3:
    51. 9thKillItem4:
    52. 10thKillItem1:
    53. 10thKillItem2:
    54. 10thKillItem3:
    55. 10thKillItem4:

    Here's my code:
    Code:java
    1. if (killStreak == 1) {
    2. pInv.remove(Material.IRON_SWORD);
    3. pInv.remove(Material.IRON_AXE);
    4. rewards.add(new ItemStack(Material.DIAMOND_SWORD));
    5. rewards.add(new ItemStack(Material.DIAMOND_AXE));
    6. pStorage.rewardKillStreak(killStreak);
    7. } else if (killStreak == 2) {
    8. rewards.add(new ItemStack(Material.getMaterial(FreeForAll.instance().getConfig().getString("KillStreaks.2ndKillItem1"))));
    9. rewards.add(new ItemStack(Material.getMaterial(FreeForAll.instance().getConfig().getString("KillStreaks.2ndKillItem2"))));
    10. rewards.add(new ItemStack(Material.getMaterial(FreeForAll.instance().getConfig().getString("KillStreaks.2ndKillItem3"))));
    11. rewards.add(new ItemStack(Material.getMaterial(FreeForAll.instance().getConfig().getString("KillStreaks.2ndKillItem4"))));
    12. pStorage.rewardKillStreak(killStreak);
    13. } else if (killStreak == 3) {
    14. rewards.add(new ItemStack(Material.getMaterial(FreeForAll.instance().getConfig().getString("KillStreaks.3rdKillItem1"))));
    15. rewards.add(new ItemStack(Material.getMaterial(FreeForAll.instance().getConfig().getString("KillStreaks.3rdKillItem2"))));
    16. rewards.add(new ItemStack(Material.getMaterial(FreeForAll.instance().getConfig().getString("KillStreaks.3rdKillItem3"))));
    17. rewards.add(new ItemStack(Material.getMaterial(FreeForAll.instance().getConfig().getString("KillStreaks.3rdKillItem4"))));
    18. p.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, Integer.MAX_VALUE, 1));
    19. pStorage.rewardKillStreak(killStreak);
    20. } else if (killStreak == 4) {
    21. rewards.add(new ItemStack(Material.getMaterial(FreeForAll.instance().getConfig().getString("KillStreaks.4thKillItem1"))));
    22. rewards.add(new ItemStack(Material.getMaterial(FreeForAll.instance().getConfig().getString("KillStreaks.4thKillItem2"))));
    23. rewards.add(new ItemStack(Material.getMaterial(FreeForAll.instance().getConfig().getString("KillStreaks.4thKillItem3"))));
    24. rewards.add(new ItemStack(Material.getMaterial(FreeForAll.instance().getConfig().getString("KillStreaks.4thKillItem4"))));
    25. pStorage.rewardKillStreak(killStreak);
    26. IE.addArmor(p);
    27. } else if (killStreak == 5) {
    28. rewards.add(new ItemStack(Material.getMaterial(FreeForAll.instance().getConfig().getString("KillStreaks.5thKillItem1"))));
    29. rewards.add(new ItemStack(Material.getMaterial(FreeForAll.instance().getConfig().getString("KillStreaks.5thKillItem2"))));
    30. rewards.add(new ItemStack(Material.getMaterial(FreeForAll.instance().getConfig().getString("KillStreaks.5thKillItem3"))));
    31. rewards.add(new ItemStack(Material.getMaterial(FreeForAll.instance().getConfig().getString("KillStreaks.5thKillItem4"))));
    32. pStorage.rewardKillStreak(killStreak);
    33. IE.addArmor(p);
    34. } else if (killStreak == 6) {
    35. rewards.add(new ItemStack(Material.getMaterial(FreeForAll.instance().getConfig().getString("KillStreaks.6thKillItem1"))));
    36. rewards.add(new ItemStack(Material.getMaterial(FreeForAll.instance().getConfig().getString("KillStreaks.6thKillItem2"))));
    37. rewards.add(new ItemStack(Material.getMaterial(FreeForAll.instance().getConfig().getString("KillStreaks.6thKillItem3"))));
    38. rewards.add(new ItemStack(Material.getMaterial(FreeForAll.instance().getConfig().getString("KillStreaks.6thKillItem4"))));
    39. p.addPotionEffect(new PotionEffect(PotionEffectType.INCREASE_DAMAGE, Integer.MAX_VALUE, 1));
    40. pStorage.rewardKillStreak(killStreak);
    41. IE.addArmor(p);
    42. } else if (killStreak == 7) {
    43. rewards.add(new ItemStack(Material.getMaterial(FreeForAll.instance().getConfig().getString("KillStreaks.7thKillItem1"))));
    44. rewards.add(new ItemStack(Material.getMaterial(FreeForAll.instance().getConfig().getString("KillStreaks.7thKillItem2"))));
    45. rewards.add(new ItemStack(Material.getMaterial(FreeForAll.instance().getConfig().getString("KillStreaks.7thKillItem3"))));
    46. rewards.add(new ItemStack(Material.getMaterial(FreeForAll.instance().getConfig().getString("KillStreaks.7thKillItem4"))));
    47. pStorage.rewardKillStreak(killStreak);
    48. IE.addArmor(p);
    49. } else if (killStreak == 8) {
    50. rewards.add(new ItemStack(Material.getMaterial(FreeForAll.instance().getConfig().getString("KillStreaks.8thKillItem1"))));
    51. rewards.add(new ItemStack(Material.getMaterial(FreeForAll.instance().getConfig().getString("KillStreaks.8thKillItem2"))));
    52. rewards.add(new ItemStack(Material.getMaterial(FreeForAll.instance().getConfig().getString("KillStreaks.8thKillItem3"))));
    53. rewards.add(new ItemStack(Material.getMaterial(FreeForAll.instance().getConfig().getString("KillStreaks.8thKillItem4"))));
    54. pStorage.rewardKillStreak(killStreak);
    55. IE.addArmor1(p);
    56. } else if (killStreak == 9) {
    57. rewards.add(new ItemStack(Material.getMaterial(FreeForAll.instance().getConfig().getString("KillStreaks.9thKillItem1"))));
    58. rewards.add(new ItemStack(Material.getMaterial(FreeForAll.instance().getConfig().getString("KillStreaks.9thKillItem2"))));
    59. rewards.add(new ItemStack(Material.getMaterial(FreeForAll.instance().getConfig().getString("KillStreaks.9thKillItem3"))));
    60. rewards.add(new ItemStack(Material.getMaterial(FreeForAll.instance().getConfig().getString("KillStreaks.9thKillItem4"))));
    61. p.addPotionEffect(new PotionEffect(PotionEffectType.REGENERATION, Integer.MAX_VALUE, 1));
    62. pStorage.rewardKillStreak(killStreak);
    63. IE.addArmor1(p);
    64. } else if (killStreak == 10) {
    65. rewards.add(new ItemStack(Material.getMaterial(FreeForAll.instance().getConfig().getString("KillStreaks.10thKillItem1"))));
    66. rewards.add(new ItemStack(Material.getMaterial(FreeForAll.instance().getConfig().getString("KillStreaks.10thKillItem2"))));
    67. rewards.add(new ItemStack(Material.getMaterial(FreeForAll.instance().getConfig().getString("KillStreaks.10thKillItem3"))));
    68. rewards.add(new ItemStack(Material.getMaterial(FreeForAll.instance().getConfig().getString("KillStreaks.10thKillItem4"))));
    69. pStorage.rewardKillStreak(killStreak);
    70. IE.addArmor1(p);
    71. }
     
Thread Status:
Not open for further replies.

Share This Page