Solved Help with Config File?

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

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

    Ricecutter0

    I was making this plugin for someone, and everything works fine.. except for the message. I have a message in my config file, and I'm trying to make it run when someone kills someone. Also if anyone notices any errors or has any tips I would really appreciate it.

    Main Class:
    Code:
    package me.Ricecutter0.KillCommand;
    
    import java.util.logging.Logger;
    
    import org.bukkit.Bukkit;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.event.Listener;
    import org.bukkit.plugin.PluginDescriptionFile;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class Main extends JavaPlugin implements Listener
    {
        public final Logger logger = Logger.getLogger("Minecraft");
        public static Main plugin;
      
    
    
        Killing killing= new Killing(null);
        @Override
        public void onEnable()
        {
            Bukkit.getPluginManager().registerEvents(new Killing(null), this);
            PluginDescriptionFile pdfFile = this.getDescription();
            this.logger.info(pdfFile.getName() + " Version " + pdfFile.getVersion() + " has been enabled.");
            final FileConfiguration config = this.getConfig();
            config.options().copyDefaults(true);
            saveConfig();
        }
    
        @Override
        public void onDisable()
        {
            PluginDescriptionFile pdfFile = this.getDescription();
            this.logger.info(pdfFile.getName() + " has been disable.");
        }
      
    }
    
    Killing Class:
    Code:
    package me.Ricecutter0.KillCommand;
    
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.PlayerDeathEvent;
    
    public final class Killing implements Listener
    {
        Main plugin;
    
        public Killing(Main instance)
        {
            plugin = instance;
        }
    
        @EventHandler
        public void OnPlayerDeath(PlayerDeathEvent e)
        {
            Player victim = e.getEntity();
            Player killer = victim.getKiller();
    
    
            if(!(killer instanceof Player))
            {
                @SuppressWarnings("unused")
                boolean isCancelled = true;
            }
            else
            {
                @SuppressWarnings("unused")
                boolean isCancelled = false;
    
                victim.sendMessage(plugin.getConfig().getString("Message"));
            }
    
    
    
    
        }
    
    
    }
    
    Config.yml:
    Code:
    #Set your death message here:
    Message: test
    Plugin.yml:
    Code:
    name: KillCommand
    version: 1.0
    main: me.Ricecutter0.KillCommand.Main
    description: What
    Error:
    Code:
    [09:23:46 ERROR]: Could not pass event PlayerDeathEvent to KillCommand v1.0
    org.bukkit.event.EventException
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:302) ~[craftbukkit.jar:git-Spigot-1.7.9-R0.2-207-g03373bb]
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.jav
    a:62) ~[craftbukkit.jar:git-Spigot-1.7.9-R0.2-207-g03373bb]
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.j
    ava:509) [craftbukkit.jar:git-Spigot-1.7.9-R0.2-207-g03373bb]
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.j
    ava:494) [craftbukkit.jar:git-Spigot-1.7.9-R0.2-207-g03373bb]
            at org.bukkit.craftbukkit.v1_7_R4.event.CraftEventFactory.callPlayerDeat
    hEvent(CraftEventFactory.java:397) [craftbukkit.jar:git-Spigot-1.7.9-R0.2-207-g0
    3373bb]
            at net.minecraft.server.v1_7_R4.EntityPlayer.die(EntityPlayer.java:386)
    [craftbukkit.jar:git-Spigot-1.7.9-R0.2-207-g03373bb]
            at net.minecraft.server.v1_7_R4.EntityLiving.damageEntity(EntityLiving.j
    ava:745) [craftbukkit.jar:git-Spigot-1.7.9-R0.2-207-g03373bb]
            at net.minecraft.server.v1_7_R4.EntityHuman.damageEntity(EntityHuman.jav
    a:765) [craftbukkit.jar:git-Spigot-1.7.9-R0.2-207-g03373bb]
            at net.minecraft.server.v1_7_R4.EntityPlayer.damageEntity(EntityPlayer.j
    ava:465) [craftbukkit.jar:git-Spigot-1.7.9-R0.2-207-g03373bb]
            at net.minecraft.server.v1_7_R4.EntityArrow.h(EntityArrow.java:243) [cra
    ftbukkit.jar:git-Spigot-1.7.9-R0.2-207-g03373bb]
            at net.minecraft.server.v1_7_R4.World.entityJoinedWorld(World.java:1620)
    [craftbukkit.jar:git-Spigot-1.7.9-R0.2-207-g03373bb]
            at net.minecraft.server.v1_7_R4.World.playerJoinedWorld(World.java:1595)
    [craftbukkit.jar:git-Spigot-1.7.9-R0.2-207-g03373bb]
            at net.minecraft.server.v1_7_R4.World.tickEntities(World.java:1460) [cra
    ftbukkit.jar:git-Spigot-1.7.9-R0.2-207-g03373bb]
            at net.minecraft.server.v1_7_R4.WorldServer.tickEntities(WorldServer.jav
    a:516) [craftbukkit.jar:git-Spigot-1.7.9-R0.2-207-g03373bb]
            at net.minecraft.server.v1_7_R4.MinecraftServer.v(MinecraftServer.java:7
    06) [craftbukkit.jar:git-Spigot-1.7.9-R0.2-207-g03373bb]
            at net.minecraft.server.v1_7_R4.DedicatedServer.v(DedicatedServer.java:2
    89) [craftbukkit.jar:git-Spigot-1.7.9-R0.2-207-g03373bb]
            at net.minecraft.server.v1_7_R4.MinecraftServer.u(MinecraftServer.java:5
    84) [craftbukkit.jar:git-Spigot-1.7.9-R0.2-207-g03373bb]
            at net.minecraft.server.v1_7_R4.MinecraftServer.run(MinecraftServer.java
    :490) [craftbukkit.jar:git-Spigot-1.7.9-R0.2-207-g03373bb]
            at net.minecraft.server.v1_7_R4.ThreadServerApplication.run(SourceFile:6
    28) [craftbukkit.jar:git-Spigot-1.7.9-R0.2-207-g03373bb]
    Caused by: java.lang.NullPointerException
            at me.Ricecutter0.KillCommand.Killing.OnPlayerDeath(Killing.java:34) ~[?
    :?]
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.7.0
    _51]
            at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.7.0
    _51]
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1
    .7.0_51]
            at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.7.0_51]
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:298) ~[craftbukkit.jar:git-Spigot-1.7.9-R0.2-207-g03373bb]
            ... 18 more
     
  2. Offline

    Ruptur

    @Ricecutter0

    Stacktrace
    Code:
    [09:23:46 ERROR]: Could not pass event PlayerDeathEvent to KillCommand v1.0
    org.bukkit.event.EventException
    
    Main class doesn't need to implement listener

    Code:
    Killing killing= new Killing(null);
    
    needs to be
    Code:
    Killing killing= new Killing(this);
    
     
  3. Online

    timtower Moderator Moderator

    Moved to Bukkit alternatives
     
  4. Offline

    Ricecutter0

    I still get this error:

    Code:
    [10:52:14 ERROR]: Could not pass event PlayerDeathEvent to KillCommand v1.0
    org.bukkit.event.EventException
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:302) ~[craftbukkit.jar:git-Spigot-1.7.9-R0.2-207-g03373bb]
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.jav
    a:62) ~[craftbukkit.jar:git-Spigot-1.7.9-R0.2-207-g03373bb]
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.j
    ava:509) [craftbukkit.jar:git-Spigot-1.7.9-R0.2-207-g03373bb]
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.j
    ava:494) [craftbukkit.jar:git-Spigot-1.7.9-R0.2-207-g03373bb]
            at org.bukkit.craftbukkit.v1_7_R4.event.CraftEventFactory.callPlayerDeat
    hEvent(CraftEventFactory.java:397) [craftbukkit.jar:git-Spigot-1.7.9-R0.2-207-g0
    3373bb]
            at net.minecraft.server.v1_7_R4.EntityPlayer.die(EntityPlayer.java:386)
    [craftbukkit.jar:git-Spigot-1.7.9-R0.2-207-g03373bb]
            at net.minecraft.server.v1_7_R4.EntityLiving.damageEntity(EntityLiving.j
    ava:745) [craftbukkit.jar:git-Spigot-1.7.9-R0.2-207-g03373bb]
            at net.minecraft.server.v1_7_R4.EntityHuman.damageEntity(EntityHuman.jav
    a:765) [craftbukkit.jar:git-Spigot-1.7.9-R0.2-207-g03373bb]
            at net.minecraft.server.v1_7_R4.EntityPlayer.damageEntity(EntityPlayer.j
    ava:465) [craftbukkit.jar:git-Spigot-1.7.9-R0.2-207-g03373bb]
            at net.minecraft.server.v1_7_R4.EntityArrow.h(EntityArrow.java:243) [cra
    ftbukkit.jar:git-Spigot-1.7.9-R0.2-207-g03373bb]
            at net.minecraft.server.v1_7_R4.World.entityJoinedWorld(World.java:1620)
     [craftbukkit.jar:git-Spigot-1.7.9-R0.2-207-g03373bb]
            at net.minecraft.server.v1_7_R4.World.playerJoinedWorld(World.java:1595)
     [craftbukkit.jar:git-Spigot-1.7.9-R0.2-207-g03373bb]
            at net.minecraft.server.v1_7_R4.World.tickEntities(World.java:1460) [cra
    ftbukkit.jar:git-Spigot-1.7.9-R0.2-207-g03373bb]
            at net.minecraft.server.v1_7_R4.WorldServer.tickEntities(WorldServer.jav
    a:516) [craftbukkit.jar:git-Spigot-1.7.9-R0.2-207-g03373bb]
            at net.minecraft.server.v1_7_R4.MinecraftServer.v(MinecraftServer.java:7
    06) [craftbukkit.jar:git-Spigot-1.7.9-R0.2-207-g03373bb]
            at net.minecraft.server.v1_7_R4.DedicatedServer.v(DedicatedServer.java:2
    89) [craftbukkit.jar:git-Spigot-1.7.9-R0.2-207-g03373bb]
            at net.minecraft.server.v1_7_R4.MinecraftServer.u(MinecraftServer.java:5
    84) [craftbukkit.jar:git-Spigot-1.7.9-R0.2-207-g03373bb]
            at net.minecraft.server.v1_7_R4.MinecraftServer.run(MinecraftServer.java
    :490) [craftbukkit.jar:git-Spigot-1.7.9-R0.2-207-g03373bb]
            at net.minecraft.server.v1_7_R4.ThreadServerApplication.run(SourceFile:6
    28) [craftbukkit.jar:git-Spigot-1.7.9-R0.2-207-g03373bb]
    Caused by: java.lang.NullPointerException
            at me.Ricecutter0.KillCommand.Killing.OnPlayerDeath(Killing.java:34) ~[?
    :?]
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.7.0
    _51]
            at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.7.0
    _51]
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1
    .7.0_51]
            at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.7.0_51]
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:298) ~[craftbukkit.jar:git-Spigot-1.7.9-R0.2-207-g03373bb]
            ... 18 more
    @Ruptur

    It says the error is happening on line 34?

    Code:
            at me.Ricecutter0.KillCommand.Killing.OnPlayerDeath(Killing.java:34) ~[?
    EDIT by Timtower: merged posts
     
    Last edited by a moderator: Feb 14, 2015
  5. Offline

    Ruptur

    @Ricecutter0
    Comment out this line
    Code:
    victim.sendMessage(plugin.getConfig().getString("Message"));
    
    Does it still crash?
     
  6. Offline

    Ricecutter0

    @Ruptur

    If I change it to:


    Code:
    victim.sendMessage("Testing");
    It works, but I want it to work from the Config.yml file.
     
  7. Offline

    Ruptur

    @Ricecutter0
    Alright
    Code:
    String msg = getConfig().getString("message");
    victim.sendMessage(msg);
    
    public FileConfiguration getConfig(){
        return plugin.getConfig();
    
     
  8. Offline

    Ricecutter0

    What class and where does that go?
     
  9. Offline

    Ruptur

    @Ricecutter0
    First 2 lines need to do where you want to send the victim a messsage.

    The method getConfig() needs to go in the same class as ^

    If this doesnt work then its because of your config settings
     
  10. Offline

    Ricecutter0

    @Ruptur

    I don't get the error now, but I don't get the message
     
  11. Offline

    Ruptur

    @Ricecutter0

    Dont copy what i wrote word for word
    In this case the
    Code:
    String msg = getConfig().getString("message");
    
    needs to be replaced with
    Code:
    String msg = getConfig().getString("Message"); // Cap M - since that was in your config
    
     
    DaanSander likes this.
  12. Offline

    Ricecutter0

    @Ruptur

    Now when I reload my server.. I get this error:

    Code:
    [11:31:52 ERROR]: Could not load 'plugins\KillingCommand.jar' in folder 'plugins
    '
    org.bukkit.plugin.InvalidPluginException: java.lang.NullPointerException
            at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.j
    ava:133) ~[craftbukkit.jar:git-Spigot-1.7.9-R0.2-207-g03373bb]
            at org.bukkit.plugin.SimplePluginManager.loadPlugin(SimplePluginManager.
    java:336) ~[craftbukkit.jar:git-Spigot-1.7.9-R0.2-207-g03373bb]
            at org.bukkit.plugin.SimplePluginManager.loadPlugins(SimplePluginManager
    .java:258) [craftbukkit.jar:git-Spigot-1.7.9-R0.2-207-g03373bb]
            at org.bukkit.craftbukkit.v1_7_R4.CraftServer.loadPlugins(CraftServer.ja
    va:369) [craftbukkit.jar:git-Spigot-1.7.9-R0.2-207-g03373bb]
            at org.bukkit.craftbukkit.v1_7_R4.CraftServer.reload(CraftServer.java:86
    4) [craftbukkit.jar:git-Spigot-1.7.9-R0.2-207-g03373bb]
            at org.bukkit.Bukkit.reload(Bukkit.java:301) [craftbukkit.jar:git-Spigot
    -1.7.9-R0.2-207-g03373bb]
            at org.bukkit.command.defaults.ReloadCommand.execute(ReloadCommand.java:
    23) [craftbukkit.jar:git-Spigot-1.7.9-R0.2-207-g03373bb]
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:18
    1) [craftbukkit.jar:git-Spigot-1.7.9-R0.2-207-g03373bb]
            at org.bukkit.craftbukkit.v1_7_R4.CraftServer.dispatchCommand(CraftServe
    r.java:767) [craftbukkit.jar:git-Spigot-1.7.9-R0.2-207-g03373bb]
            at org.bukkit.craftbukkit.v1_7_R4.CraftServer.dispatchServerCommand(Craf
    tServer.java:753) [craftbukkit.jar:git-Spigot-1.7.9-R0.2-207-g03373bb]
            at net.minecraft.server.v1_7_R4.DedicatedServer.aB(DedicatedServer.java:
    326) [craftbukkit.jar:git-Spigot-1.7.9-R0.2-207-g03373bb]
            at net.minecraft.server.v1_7_R4.DedicatedServer.v(DedicatedServer.java:2
    90) [craftbukkit.jar:git-Spigot-1.7.9-R0.2-207-g03373bb]
            at net.minecraft.server.v1_7_R4.MinecraftServer.u(MinecraftServer.java:5
    84) [craftbukkit.jar:git-Spigot-1.7.9-R0.2-207-g03373bb]
            at net.minecraft.server.v1_7_R4.MinecraftServer.run(MinecraftServer.java
    :490) [craftbukkit.jar:git-Spigot-1.7.9-R0.2-207-g03373bb]
            at net.minecraft.server.v1_7_R4.ThreadServerApplication.run(SourceFile:6
    28) [craftbukkit.jar:git-Spigot-1.7.9-R0.2-207-g03373bb]
    Caused by: java.lang.NullPointerException
            at me.Ricecutter0.KillCommand.Killing.<init>(Killing.java:17) ~[?:?]
            at me.Ricecutter0.KillCommand.Main.<init>(Main.java:17) ~[?:?]
            at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
     ~[?:1.7.0_51]
            at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
     ~[?:1.7.0_51]
            at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Sou
    rce) ~[?:1.7.0_51]
            at java.lang.reflect.Constructor.newInstance(Unknown Source) ~[?:1.7.0_5
    1]
            at java.lang.Class.newInstance(Unknown Source) ~[?:1.7.0_51]
            at org.bukkit.plugin.java.PluginClassLoader.<init>(PluginClassLoader.jav
    a:76) ~[craftbukkit.jar:git-Spigot-1.7.9-R0.2-207-g03373bb]
            at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.j
    ava:129) ~[craftbukkit.jar:git-Spigot-1.7.9-R0.2-207-g03373bb]
            ... 14 more
     
  13. Offline

    Ruptur

    @Ricecutter0

    I went ahead and fixed your code and made some improvements

    main class:
    Code:
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class Test extends JavaPlugin{
        public static Test plugin;
      
        ConfigManager cm = ConfigManager.getInstance();
      
        @Override
        public void onEnable(){
            ConfigManager.setup(this);
            Bukkit.getPluginManager().registerEvents(new KillListener(), this);
            this.getServer().getConsoleSender().sendMessage(ChatColor.GREEN + " ----------------------------------------------------------- ");
            this.getServer().getConsoleSender().sendMessage(ChatColor.AQUA + "  ");
            this.getServer().getConsoleSender().sendMessage(ChatColor.AQUA+ "[" + this.getDescription().getName() + "] Plugin : " + ChatColor.BOLD + this.getDescription().getName());
            this.getServer().getConsoleSender().sendMessage(ChatColor.AQUA  + "[" + this.getDescription().getName() + "] " + ChatColor.GREEN + " Enabled!");
            this.getServer().getConsoleSender().sendMessage(ChatColor.AQUA + "[" + this.getDescription().getName() + "] Version : " + this.getDescription().getVersion());
            this.getServer().getConsoleSender().sendMessage(ChatColor.AQUA  + "  ");
            this.getServer().getConsoleSender().sendMessage(ChatColor.GREEN  + " ----------------------------------------------------------- ");
    
        }
    
        @Override
        public void onDisable(){
            this.getServer().getConsoleSender().sendMessage(ChatColor.RED + " ----------------------------------------------------------- ");
            this.getServer().getConsoleSender().sendMessage(ChatColor.AQUA + "  ");
            this.getServer().getConsoleSender().sendMessage(ChatColor.AQUA+ "[" + this.getDescription().getName() + "] Plugin : " + ChatColor.BOLD + this.getDescription().getName());
            this.getServer().getConsoleSender().sendMessage(ChatColor.AQUA  + "[" + this.getDescription().getName() + "] " + ChatColor.RED + " Disabled!");
            this.getServer().getConsoleSender().sendMessage(ChatColor.AQUA + "[" + this.getDescription().getName() + "] Version : " + this.getDescription().getVersion());
            this.getServer().getConsoleSender().sendMessage(ChatColor.AQUA  + "  ");
            this.getServer().getConsoleSender().sendMessage(ChatColor.RED  + " ----------------------------------------------------------- ");
    
        }
    
    }
    
    
    The killListener class

    Code:
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.PlayerDeathEvent;
    
    public class KillListener implements Listener{
    
        public static FileConfiguration config;
        boolean isCancelled;
      
        ConfigManager cm = ConfigManager.getInstance();
      
       @SuppressWarnings("unused")
    @EventHandler
       public void OnPlayerDeath(PlayerDeathEvent e)
       { 
          Player victim = e.getEntity().getPlayer();
          Player killer = e.getEntity().getKiller();
    
          if(!(killer instanceof Player)){
              isCancelled = true;
              return;
          }
        isCancelled = false;
      
        try{
            Bukkit.getConsoleSender().sendMessage(cm.getConfig().getString("message"));
        }catch (Exception ex){
            Bukkit.getLogger().severe(ChatColor.RED + "Unable to get config file!");
        }
       }
      
    
    }
    
    The ConfigManager class
    Code:
    import java.io.File;
    import java.io.IOException;
    
    
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.configuration.file.YamlConfiguration;
    import org.bukkit.plugin.Plugin;
    
    public class ConfigManager {
    
        private ConfigManager() {}
      
        static ConfigManager instance = new ConfigManager();
      
    
        public static ConfigManager getInstance(){
            return instance;
        }
      
        static FileConfiguration config;
        static File cfile;
      
        Plugin p;
      
        public static void setup(Plugin p){
            cfile = new File(p.getDataFolder(), "config.yml");
            config = p.getConfig();
            config.options().copyDefaults(true);
            p.saveDefaultConfig();
          
    
            }
      
        public FileConfiguration getConfig(){
            return config;
        }
      
        public void saveConfig(){
            try {
                config.save(cfile);
            } catch (IOException e) {
                Bukkit.getServer().getLogger().severe(ChatColor.RED + "Could not save config!");
            }
        }
      
        public void reloadConfig(){
            try{
                config = YamlConfiguration.loadConfiguration(cfile);
            }catch (Exception ex){
                Bukkit.getServer().getLogger().severe(ChatColor.RED + "Unable to reload config!");
            }
        }
    }
    
    If this solved your problem then set the thread prefix as [Solved]
     
    Last edited by a moderator: Feb 14, 2015
    DaanSander likes this.
Thread Status:
Not open for further replies.

Share This Page