Check a boolean in a YAML file

Discussion in 'Spigot Plugin Development' started by Whosker, Jun 2, 2015.

  1. Hi.
    I'm learning to develop plugins with Spigot and for a couple of days I've been working on my first plugin.
    What this plugin mainly does is to search player names in chat messages, then changes the name's color and plays a sound to the called player (I call it mentioning).
    Recently I tried to add a command to disable this for a player, so they can type '/mentions off' if they don't want to be warned every time someone is naming them.
    The command is fine, I managed to save a file that the plugin edits when the player enables or disables this, but the problem comes when the plugin reads the file when a player sends a message with a player name, the console shows a nullPointerException in line 42, the line that checks if the player has disabled the function.

    This is the file:
    Code (datafile (Unknown Language)):
    Ker:
      Mentions: false
      Nicks:
      - Whos
      - Whosker
    Maincraft:
      Mentions: true

    This is the exception shown every time a player says a name:
    Code (Text):
    [15:22:56] [Async Chat Thread - #3/ERROR]: Could not pass event AsyncPlayerChatEvent to ChatManager v1.0
    org.bukkit.event.EventException
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:305) ~[spigot-1.8.jar:git-Spigot-4c7d0c0-96c2c39]
        at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[spigot-1.8.jar:git-Spigot-4c7d0c0-96c2c39]
        at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:502) [spigot-1.8.jar:git-Spigot-4c7d0c0-96c2c39]
        at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:484) [spigot-1.8.jar:git-Spigot-4c7d0c0-96c2c39]
        at net.minecraft.server.v1_8_R2.PlayerConnection.chat(PlayerConnection.java:1059) [spigot-1.8.jar:git-Spigot-4c7d0c0-96c2c39]
        at net.minecraft.server.v1_8_R2.PlayerConnection.a(PlayerConnection.java:997) [spigot-1.8.jar:git-Spigot-4c7d0c0-96c2c39]
        at net.minecraft.server.v1_8_R2.PacketPlayInChat$1.run(PacketPlayInChat.java:39) [spigot-1.8.jar:git-Spigot-4c7d0c0-96c2c39]
        at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_45]
        at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_45]
        at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) [?:1.8.0_45]
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) [?:1.8.0_45]
        at java.lang.Thread.run(Unknown Source) [?:1.8.0_45]
    Caused by: java.lang.NullPointerException
        at me.whosker.chatmanager.Events.onChat(Events.java:42) ~[?:?]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_45]
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_45]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_45]
        at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_45]
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:301) ~[spigot-1.8.jar:git-Spigot-4c7d0c0-96c2c39]
        ... 11 more
    [15:22:56] [Async Chat Thread - #3/INFO]: [m[0;31;22m[Owner][m [0;31;22m[0;31;22mKer[m[0;37;22m:[0;33;22m a[m

    This is my Event class:
    Code (Text):

    package me.whosker.chatmanager;

    import java.util.Timer;
    import java.util.TimerTask;

    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Instrument;
    import org.bukkit.Note;
    import org.bukkit.Note.Tone;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.AsyncPlayerChatEvent;



    import com.earth2me.essentials.Essentials;

    public class Events implements Listener
    {
        private Plugin plugin;
        private Essentials ess;
       
        @EventHandler
        public void onChat(AsyncPlayerChatEvent evento)
        {
            Player pSender = evento.getPlayer();
            ChatColor color = ChatColor.WHITE;
            if(!pSender.hasPermission(Config.perm_chat))
            {
                pSender.sendMessage(Config.msg_Unauthorized_ToChat);
                evento.setCancelled(true);
            } else if(evento.getPlayer().hasPermission(Config.perm_mentions))
                {
                    for(Player called: Bukkit.getServer().getOnlinePlayers())
                    {
                        if(!called.equals(evento.getPlayer()))
                        {
                            if(called != null && evento.getMessage().toLowerCase().contains(called.getName().toLowerCase()));
                            {
                                if(plugin.settings.getData().getBoolean(called.getName() + ".Mentions"))
                                {
                                    boolean estaEnVanish = false;
                                    if(called.getPlayer().hasPermission(Config.perm_staff))
                                    {
                                        color = ChatColor.GOLD;
                                        if(ess.getUser(called).isVanished())
                                        {
                                            estaEnVanish=true;
                                        }
                                    }
                                    String mes = evento.getMessage();
                                    if(!estaEnVanish && org.apache.commons.lang.StringUtils.containsIgnoreCase(mes, " " + called.getName() + " "))
                                    {
                                        mes = mes.replaceAll(" (?i)" + called.getName() + " ", " " + ChatColor.BLUE+called.getName()+ " " + color);
                                        playSound(called);
                                    } else if(!estaEnVanish && org.apache.commons.lang.StringUtils.equalsIgnoreCase(mes, called.getName()))
                                    {
                                        mes = mes.replaceAll("(?i)" + called.getName(), ChatColor.BLUE+called.getName() + color);
                                        playSound(called);
                                    } else if(!estaEnVanish && org.apache.commons.lang.StringUtils.endsWithIgnoreCase(mes, " " + called.getName()))
                                    {
                                        mes = mes.replaceAll(" (?i)" + called.getName(), " " + ChatColor.BLUE+called.getName()+ color);
                                        playSound(called);
                                    } else if(!estaEnVanish && org.apache.commons.lang.StringUtils.startsWithIgnoreCase(mes, called.getName() + " "))
                                    {
                                        mes = mes.replaceFirst("(?i)" + called.getName(), ChatColor.BLUE+called.getName() + color);
                                        mes = mes.replaceAll(" (?i)" + called.getName() + " ", ChatColor.BLUE+called.getName() + color);
                                        playSound(called);
                                    }
                                    if(!estaEnVanish){
                                       evento.setMessage(mes);
                                    }
                                }
                            }
                        }
                    }
                }
        }
       
        private void playSound(final Player p)
        {
            Timer t = new Timer();
            t.schedule(new TimerTask() {
                @Override
                public void run() {
                    p.playNote(p.getLocation(), Instrument.PIANO, Note.natural(1, Tone.A));
                    p.playNote(p.getLocation(), Instrument.PIANO, Note.natural(1, Tone.A));
                    p.playNote(p.getLocation(), Instrument.PIANO, Note.natural(1, Tone.A));

                }
            }, 200);
            p.playNote(p.getLocation(), Instrument.PIANO, Note.natural(1, Tone.D));
            p.playNote(p.getLocation(), Instrument.PIANO, Note.natural(1, Tone.D));
            p.playNote(p.getLocation(), Instrument.PIANO, Note.natural(1, Tone.D));
        }
    }

    Thank you so much for reading, I hope you don't have a heart attack for the errors I made. :p
     
  2. Add a System.out.println("READ HERE"+ called.getName() + ".Mentions"); And tell me what it comes back with here.
     
  3. [15:46:45] [Async Chat Thread - #1/INFO]: READ HEREMaincraft.Mentions
     
    #3 Whosker, Jun 2, 2015
    Last edited: Jun 2, 2015
  4. Your plugin variable was never initialized.
     
  5. I added this to my Events class:
    Code (Text):
        public Events(Plugin plugin)
        {
            this.plugin = plugin;
        }
    Is it right?
    Now I got another NullPointerException in line 58, which checks if the player is vanished. I suppose I have to initialize my ess variable, but I dont know how.

    Thank you for your help :)
     
  6. Check the documentation to see how to use the API for essentials.
     
  7. I'll check it out. Thank you.
     
  8. You seem to lack the required minimum amount of Java knowledge. Please return to the java tutorials and return when you practised a little more (this is highly advised)