Solved Problems with .getStringList()

Discussion in 'Plugin Development' started by ClintonM0, Dec 7, 2012.

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

    ClintonM0

    if(event.getMessage().toLowerCase().contains(plugin.getConfig().getStringList("test")))
    It doesn't work :(

    Anyone help?
     
  2. Offline

    RealDope

    getStringList() returns a List<String>

    You can't use .contains(List<String>) I'm pretty sure. You have to get a string out of the List<String> and use that.
     
  3. Offline

    ClintonM0

    Hmmm... That's what I noticed. .getString("example") works but not .getStringList("example").

    I guess List<String> censor = plugin.getConfig().getStringList("warning"); would work...
     
  4. Try looping through everything in the List and seeing if any of there strings match a string from the List...
     
  5. Offline

    ClintonM0

    Nope idk how you get a String out of a StringList. Anyone help?

    Anyone here to help at all?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 30, 2016
  6. Offline

    JazzaG

    Code:
    YAML:
    list:
     - string1
     - string2
     - string3
     
    JAVA:
    bool one = getConfig().getStringList("list").contains("string1"); // true
    bool two = getConfig().getStringList("list").contains("string4"); // false;
     
    // In your case
    if(plugin.getConfig().getStringList("test").contains(event.getMessage().toLowerCase())) {}
    
     
  7. Offline

    ClintonM0

    Doesn't work
     
  8. Offline

    JazzaG

    Errors? What does it do? What doesn't it do? What do you want it to do?
     
  9. Offline

    ClintonM0

    Entire Listener:
    Code:
        @EventHandler
        public void onPlayerChat(AsyncPlayerChatEvent event) {
            Player player = event.getPlayer();
            if(plugin.getConfig().getStringList("censored").contains(event.getMessage().toLowerCase())) {
                event.setCancelled(true);
                player.sendMessage(ChatColor.RED + "Do not swear!");
            }
     
        }
    Error code:
    Code:
    12-07 21:30:48 [SEVERE] Could not pass event AsyncPlayerChatEvent to ******* ****
    org.bukkit.event.EventException
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:341)
        at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62)
        at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:477)
        at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:459)
        at net.minecraft.server.NetServerHandler.chat(NetServerHandler.java:908)
        at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:858)
        at net.minecraft.server.Packet3Chat.handle(Packet3Chat.java:44)
        at net.minecraft.server.NetworkManager.i(NetworkManager.java:208)
        at net.minecraft.server.NetworkManager.c(NetworkManager.java:344)
        at net.minecraft.server.NetworkReaderThread.run(SourceFile:93)
    Caused by: java.lang.NullPointerException
        at com.github.ClintMinecraft.YouSwore.YouSworePlayerListener.onPlayerChat(YouSworePlayerListener.java:15)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.lang.reflect.Method.invoke(Unknown Source)
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:339)
        ... 9 more
     
  10. Offline

    ZeusAllMighty11

    Stop double posting...
     
    JazzaG likes this.
  11. Offline

    JazzaG

    Can you now actually post your entire listener?
     
  12. Offline

    ClintonM0

    Code:
    package com.github.ClintMinecraft.YouSwore;
     
    import org.bukkit.ChatColor;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.AsyncPlayerChatEvent;
     
    public class YouSworePlayerListener implements Listener {
        public static YouCore plugin;
       
        @EventHandler
        public void onPlayerChat(AsyncPlayerChatEvent event) {
            Player player = event.getPlayer();
            if(plugin.getConfig().getStringList("censored").contains(event.getMessage().toLowerCase())) {
                event.setCancelled(true);
                player.sendMessage(ChatColor.RED + "Do not swear!");
            }
     
        }
           
    }
    
     
  13. Offline

    ZeusAllMighty11

    Code:
    package com.github.ClintMinecraft.YouSwore;
     
    import org.bukkit.ChatColor;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.AsyncPlayerChatEvent;
     
    public class YouSworePlayerListener implements Listener {
        public static YouCore plugin;
     
        @EventHandler
        public void onPlayerChat(AsyncPlayerChatEvent event) {
            Player player = event.getPlayer();
            if(event.getMessage().toLowerCase().contains(plugin.getConfig().getStringList("censored"))) {
                event.setCancelled(true);
                player.sendMessage(ChatColor.RED + "Do not swear!");
            }
     
        }
         
    }
    
    Try that.
    ?
     
  14. Offline

    ClintonM0

    Tried that before, but .contain() is for CharSequences and .getStringList() is a list
     
  15. Offline

    ZeusAllMighty11

    Remove .toLowerCase()....

    I just copy and pasted...
     
  16. Offline

    ClintonM0

    Doesn't work...

    Would it be something wrong here?
    Code:
        @Override
        public void onEnable() {
            PluginDescriptionFile pdffile = this.getDescription();
            this.logger.info("[YouSwore] YouSwore build" + pdffile.getVersion() + " has been enabled!");
            PluginManager pm = getServer().getPluginManager();
            pm.registerEvents(this.pl, this);
            getConfig().options().copyDefaults();
            saveConfig();
        }
     
  17. Offline

    ZeusAllMighty11

    copyDefaults(true); ?
     
  18. Offline

    ClintonM0

    Still doesn't work
     
  19. Offline

    JazzaG

    Oh I see what you are doing now, silly me. Try using this

    Code:
    private boolean containsSwear(String m) {
        for(String s : plugin.getConfig().getStringList("censored")) {
            return m.contains(s) == true;
        }
     
        return false;
    }
     
  20. Offline

    Wolvereness Bukkit Team Member

    His plugin reference is null
    Code:
    public static YouCore plugin;
     
  21. Offline

    lenis0012

    This works:
    Code:
    String message = event.getMessage;
    for(Stirng word : plugin.getCOnfig().getStringList("censored")
    {
        if(message.toLowerCase().contains(word.toLowerCase())
        {
            event.setCanceled(true);
         }
    }
    but this has to be on the top of your listener on the top:
    Code:
    private YouSwore plugin;
    public YouSworePlayerListener(YouSwore i){ plugin = i; }
    
    and in the YouSore main class replac:
    Code:
    this.getServer().getPluginManager().registerEvents(new YouSworePlayerListener(), this);
    
    with:
    Code:
    this.getServer().getPluginManager().registerEvents(new YouSworePlayerListener(this), this);
    
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 30, 2016
  22. Offline

    ClintonM0

    You're my hero :D
     
Thread Status:
Not open for further replies.

Share This Page