Problem with Configuration Files

Discussion in 'Plugin Development' started by RickRaider, Mar 24, 2014.

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

    RickRaider

    Hi.

    I am having a problem getting values from a config and using them in an event, something to do with the file setup.

    Main Code:
    Code:java
    1. package me.Rick.overpoweredmc;
    2.  
    3. import java.io.File;
    4. import java.util.logging.Logger;
    5.  
    6. import org.bukkit.ChatColor;
    7. import org.bukkit.command.Command;
    8. import org.bukkit.command.CommandSender;
    9. import org.bukkit.configuration.file.FileConfiguration;
    10. import org.bukkit.entity.Player;
    11. import org.bukkit.plugin.java.JavaPlugin;
    12.  
    13. public class OverpoweredMC extends JavaPlugin {
    14.  
    15. Logger log = Logger.getLogger("Minecraft");
    16. FileConfiguration cfg;
    17.  
    18. @Override
    19. public void onEnable() {
    20. log.info(ChatColor.YELLOW + "All OverpoweredMC aspects enabled.");
    21. this.getCommand("opmc").setExecutor(new OPMC());
    22. this.getServer().getPluginManager().registerEvents(new DropEvents(this), this);
    23. loadConfig();
    24. }
    25.  
    26. @Override
    27. public void onDisable() {
    28. log.info(ChatColor.RED + "All OverpoweredMC aspects disabled.");
    29. }
    30.  
    31. //public void initialiseConfig() {
    32. //FileConfiguration config = getConfig();
    33. //config.addDefault("Item ID", 1);
    34. //config.options().copyDefaults(true);
    35. //saveConfig();
    36. //}
    37.  
    38. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    39.  
    40. if(sender instanceof Player) {
    41. Player p = (Player) sender;
    42. if(label.equalsIgnoreCase("getint")) {
    43. p.sendMessage(getConfig().getInt("Stone.ID") + "");
    44. }
    45. }
    46. return false;
    47. }
    48.  
    49. public void loadConfig() {
    50. try
    51. {
    52. File file = new File("plugins/OverpoweredMC/config.yml");
    53. cfg = getConfig();
    54. if(!file.exists())
    55. {
    56. cfg.addDefault("Stone.ID", 1);
    57. cfg.addDefault("Stone.Drop Chance", 100);
    58. cfg.options().copyDefaults(true);
    59. saveConfig();
    60. }
    61. }
    62. catch(Exception e){ }
    63. }
    64.  
    65. }
    66.  


    Event Handler

    Code:java
    1. package me.Rick.overpoweredmc;
    2.  
    3. import org.bukkit.Location;
    4. import org.bukkit.Material;
    5. import org.bukkit.block.Block;
    6. import org.bukkit.event.EventHandler;
    7. import org.bukkit.event.Listener;
    8. import org.bukkit.event.block.BlockBreakEvent;
    9. import org.bukkit.inventory.ItemStack;
    10.  
    11. public class DropEvents implements Listener {
    12.  
    13. OverpoweredMC plugin;
    14.  
    15. public DropEvents (OverpoweredMC plugin) {
    16. this.plugin = plugin;
    17. }
    18.  
    19. @SuppressWarnings("deprecation")
    20. @EventHandler
    21. public void onLeafBreak(BlockBreakEvent event) {
    22. Block block = event.getBlock();
    23. Location dropLocation = block.getLocation();
    24. ItemStack droppedItem;
    25. if(block.getType().equals(Material.LEAVES)) {
    26. droppedItem = new ItemStack(plugin.cfg.getInt("Item ID"), 1);
    27. block.getWorld().dropItem(dropLocation, new ItemStack(droppedItem));
    28. }
    29. }
    30.  
    31. }
    32.  
     
  2. Offline

    acecheesecr14

    In your main class change it to
    Code:
    public static FileConfiguration cfg;
    Where ever you need the variable do OverPoweredMC.cfg.____ if that doesn't work
     
  3. Offline

    RickRaider

    Nope, getting this error:

    [21:37:28 ERROR]: Could not pass event BlockBreakEvent to OverpoweredMC v0.1
    org.bukkit.event.EventException
    at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:427) ~[craftbukkit.jar:git-Bukkit-1.6.4-R2.0-26-g31d7c5f-b2943jnks]
    at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.jav
    a:62) ~[craftbukkit.jar:git-Bukkit-1.6.4-R2.0-26-g31d7c5f-b2943jnks]
    at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.j
    ava:477) [craftbukkit.jar:git-Bukkit-1.6.4-R2.0-26-g31d7c5f-b2943jnks]
    at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.j
    ava:462) [craftbukkit.jar:git-Bukkit-1.6.4-R2.0-26-g31d7c5f-b2943jnks]
    at net.minecraft.server.v1_7_R1.PlayerInteractManager.breakBlock(PlayerI
    nteractManager.java:263) [craftbukkit.jar:git-Bukkit-1.6.4-R2.0-26-g31d7c5f-b294
    3jnks]
    at net.minecraft.server.v1_7_R1.PlayerInteractManager.dig(PlayerInteract
    Manager.java:118) [craftbukkit.jar:git-Bukkit-1.6.4-R2.0-26-g31d7c5f-b2943jnks]
    at net.minecraft.server.v1_7_R1.PlayerConnection.a(PlayerConnection.java
    :534) [craftbukkit.jar:git-Bukkit-1.6.4-R2.0-26-g31d7c5f-b2943jnks]
    at net.minecraft.server.v1_7_R1.PacketPlayInBlockDig.a(SourceFile:53) [c
    raftbukkit.jar:git-Bukkit-1.6.4-R2.0-26-g31d7c5f-b2943jnks]
    at net.minecraft.server.v1_7_R1.PacketPlayInBlockDig.handle(SourceFile:8
    ) [craftbukkit.jar:git-Bukkit-1.6.4-R2.0-26-g31d7c5f-b2943jnks]
    at net.minecraft.server.v1_7_R1.NetworkManager.a(NetworkManager.java:146
    ) [craftbukkit.jar:git-Bukkit-1.6.4-R2.0-26-g31d7c5f-b2943jnks]
    at net.minecraft.server.v1_7_R1.ServerConnection.c(SourceFile:134) [craf
    tbukkit.jar:git-Bukkit-1.6.4-R2.0-26-g31d7c5f-b2943jnks]
    at net.minecraft.server.v1_7_R1.MinecraftServer.u(MinecraftServer.java:6
    51) [craftbukkit.jar:git-Bukkit-1.6.4-R2.0-26-g31d7c5f-b2943jnks]
    at net.minecraft.server.v1_7_R1.DedicatedServer.u(DedicatedServer.java:2
    50) [craftbukkit.jar:git-Bukkit-1.6.4-R2.0-26-g31d7c5f-b2943jnks]
    at net.minecraft.server.v1_7_R1.MinecraftServer.t(MinecraftServer.java:5
    41) [craftbukkit.jar:git-Bukkit-1.6.4-R2.0-26-g31d7c5f-b2943jnks]
    at net.minecraft.server.v1_7_R1.MinecraftServer.run(MinecraftServer.java
    :453) [craftbukkit.jar:git-Bukkit-1.6.4-R2.0-26-g31d7c5f-b2943jnks]
    at net.minecraft.server.v1_7_R1.ThreadServerApplication.run(SourceFile:6
    17) [craftbukkit.jar:git-Bukkit-1.6.4-R2.0-26-g31d7c5f-b2943jnks]
    Caused by: java.lang.IllegalArgumentException: Cannot drop AIR.
    at org.apache.commons.lang.Validate.isTrue(Validate.java:157) ~[craftbuk
    kit.jar:git-Bukkit-1.6.4-R2.0-26-g31d7c5f-b2943jnks]
    at org.bukkit.craftbukkit.v1_7_R1.CraftWorld.dropItem(CraftWorld.java:29
    5) ~[craftbukkit.jar:git-Bukkit-1.6.4-R2.0-26-g31d7c5f-b2943jnks]
    at me.Rick.overpoweredmc.DropEvents.onLeafBreak(DropEvents.java:21) ~[?:
    ?]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.7.0
    _25]
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.7.0
    _25]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1
    .7.0_25]
    at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.7.0_25]
    at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:425) ~[craftbukkit.jar:git-Bukkit-1.6.4-R2.0-26-g31d7c5f-b2943jnks]
    ... 15 more
    >
     
  4. Offline

    MCPvPCraft

    I think the error message sais it all: Caused by: java.lang.IllegalArgumentException: Cannot drop AIR.
     
Thread Status:
Not open for further replies.

Share This Page