List<String> not working...

Discussion in 'Plugin Help/Development/Requests' started by mariusvnh, Dec 15, 2014.

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

    mariusvnh

    this is my code:
    Code:
    ArrayList<String> toCensore = new ArrayList<String>();
    toCensore.add("string");
    but the toCensore.add("string"); is underline in red.
    i've import:
    Code:
    import java.util.Collection;
    import java.util.List;
    import java.util.ArrayList;
     
  2. Moved to a more appropriate section.
     
  3. Offline

    Prosfis

  4. Offline

    mariusvnh

    Code:
    [18:09:21 ERROR]: Could not pass event PlayerQuitEvent to MidiaCraft v0.1
    org.bukkit.event.EventException
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:302) ~[spigot.jar:git-Spigot-1646]
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.jav
    a:62) ~[spigot.jar:git-Spigot-1646]
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.j
    ava:514) [spigot.jar:git-Spigot-1646]
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.j
    ava:499) [spigot.jar:git-Spigot-1646]
            at net.minecraft.server.v1_7_R4.PlayerList.disconnect(PlayerList.java:32
    1) [spigot.jar:git-Spigot-1646]
            at net.minecraft.server.v1_7_R4.PlayerConnection.a(PlayerConnection.java
    :738) [spigot.jar:git-Spigot-1646]
            at net.minecraft.server.v1_7_R4.ServerConnection.c(ServerConnection.java
    :77) [spigot.jar:git-Spigot-1646]
            at net.minecraft.server.v1_7_R4.MinecraftServer.v(MinecraftServer.java:7
    34) [spigot.jar:git-Spigot-1646]
            at net.minecraft.server.v1_7_R4.DedicatedServer.v(DedicatedServer.java:2
    89) [spigot.jar:git-Spigot-1646]
            at net.minecraft.server.v1_7_R4.MinecraftServer.u(MinecraftServer.java:5
    84) [spigot.jar:git-Spigot-1646]
            at net.minecraft.server.v1_7_R4.MinecraftServer.run(MinecraftServer.java
    :490) [spigot.jar:git-Spigot-1646]
            at net.minecraft.server.v1_7_R4.ThreadServerApplication.run(SourceFile:6
    28) [spigot.jar:git-Spigot-1646]
    Caused by: java.lang.NullPointerException
            at be.drgeek.midiacraft.midiacraftListener.PlayerDisconnect(midiacraftLi
    stener.java:28) ~[?:?]
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.7.0
    _67]
            at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.7.0
    _67]
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1
    .7.0_67]
            at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.7.0_67]
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:298) ~[spigot.jar:git-Spigot-1646]
            ... 11 more
     
  5. Offline

    mrCookieSlime

    Moved to Alternatives Section.
     
  6. Offline

    mariusvnh

    but the lines of the error are toCensure.add("string");
     
  7. Offline

    mrCookieSlime

    @mariusvnh
    Please post your entire Code.
    As well as what the error message in your IDE is saying (The message that gets displayed when you hover over the red underlined text)
     
  8. Offline

    mariusvnh

    Code:
    package be.drgeek.midiacraft;
    
    import java.util.Collection;
    import java.util.List;
    import java.util.ArrayList;
    import org.bukkit.event.Listener;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.player.AsyncPlayerChatEvent;
    import org.bukkit.entity.Player;
    
    public class midiacraftChat implements Listener{
        midiacraftPlugin plugin;
        midiacraftCommands listener;
        //midiacraftPlugin listener1;
        ArrayList<String> toCensore = new ArrayList<String>();
        toCensore.add("string");
      
        public void ChatListener(midiacraftPlugin plugin){
            this.plugin = plugin;
        }
      
      
        @EventHandler
        public void onPlayerChat(AsyncPlayerChatEvent event){
            Player p = event.getPlayer();
            String msg = event.getMessage().toLowerCase();
            for(int i = 0; i < toCensore.size(); i++){
                if(msg.contains(toCensore.get(i))){
                    p.sendMessage(String.format("§cCe mot n'est pas autorisé : %s", toCensore.get(i)));
                    event.setCancelled(true);
                }
            }
        }
      
    }
     
  9. Offline

    mrCookieSlime

    @mariusvnh
    Please tag me otherwise I might not see your post.
    And you cant add an Item to a List in the class itself. You have to do it in a constructor or method etc...
    Simply include the adding of the string into your Constructor and you are good to go.
     
  10. Offline

    mariusvnh

    yes but i want to create a command :
    Code:
    package be.drgeek.midiacraft;
    
    import org.bukkit.ChatColor;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.EventPriority;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerCommandPreprocessEvent;
    
    public class midiacraftCommands implements Listener {
        midiacraftChat chat;
    
        @EventHandler(priority=EventPriority.MONITOR)
        private void onPlayerCommnandPreprocess(PlayerCommandPreprocessEvent msge){
         
            Player p = msge.getPlayer();
            String msg = msge.getMessage();
         
            String[] args = msg.split(" ");
            if(args[0].equalsIgnoreCase("/censure")&&p.hasPermission("midiacraft.CanAddCensure")){
                chat.toCensore.add(args[1]);
                p.sendMessage(ChatColor.GREEN + String.format("Le mot %s a été banni du chat.", args[1]));
                msge.setCancelled(true);
            }
        }
     
     
    }
    
    in a different class
     
  11. Offline

    mrCookieSlime

    @mariusvnh
    Please tag me otherwise I might not see your post.


    It has nothing to do with the command. It's simply how Java works. You cannot call methods outside from a constructor/method/etc...
    Adding it from within the Command will work since it is a method.
    But your over Code runs outside from anything.
     
  12. Offline

    mariusvnh

    @mrCookieSlime
    soory for the tag
    thaks for the info :)
    but the command, i don't know why it not works
     
  13. Offline

    mrCookieSlime

    @mariusvnh
    Did you register your Listener?
    What does it do when you execute the Command?
    What command are you executing?
     
  14. Offline

    mariusvnh

    @mrCookieSlime
    i use the command /censure [word to censure]
    yes i register my listener
    it say "type /help ..."
    and my console say:

     
  15. Offline

    mrCookieSlime

    @mariusvnh
    But you still did not post the needed Code...
    Please post your midiacraftlistener.class and your onEnable method.
     
  16. Offline

    mariusvnh

    @mrCookieSlime
    but there's nothing about the censure in this file:
    Code:
    package be.drgeek.midiacraft;
    
    import org.bukkit.ChatColor;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.EventPriority;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.event.player.PlayerQuitEvent;
    //import org.bukkit.event.block.SignChangeEvent;
    
    public class midiacraftListener implements Listener {
       
       
        @EventHandler(priority = EventPriority.MONITOR)
        public void playerConnectEvent(PlayerJoinEvent e)
        {
            Player p = e.getPlayer();
            String connectMessage = midiacraftPlugin.plugin.getConfig().getString("connect-message").replace(".ply.", p.getName());
           
            e.setJoinMessage(ChatColor.YELLOW + connectMessage);
           
        }
        @EventHandler(priority = EventPriority.MONITOR)
        public void PlayerDisconnect(PlayerQuitEvent e)
        {
            Player p = e.getPlayer();
            String disconnectMessage = midiacraftPlugin.plugin.getConfig().getString("disconnect-message").replace(".ply.", p.getName());
           
            e.setQuitMessage(ChatColor.RED + disconnectMessage);
           
           
        }
       
    }
     
  17. Offline

    mrCookieSlime

    @mariusvnh
    Indeed. But your Error leads to that file.
    And I still need your main class.
     
  18. Offline

    mariusvnh

    @mrCookieSlime
    all my files:
    midiacraftPlugin (main) :
    Code:
    package be.drgeek.midiacraft;
    
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.plugin.PluginManager;
    
    
    
    
    public class midiacraftPlugin extends JavaPlugin /*implements Listener*/{
        public static midiacraftPlugin plugin;
        midiacraftChat listener;
        //midiacraftChat plugin2;
        @Override
        public void onEnable(){
            PluginManager pm = getServer().getPluginManager();
            pm.registerEvents(new midiacraftCommands(), this);
            pm.registerEvents(new midiacraftListener(), this);
            pm.registerEvents(new midiacraftChat(), this);
           
           
           
            plugin = this;
            saveDefaultConfig();
            getLogger().info("Plugin Midiacraft Démarré");
        }
        public void onDisable(){
            plugin = null;
        }
       
    }
    
    midiacraftListener :
    Code:
    package be.drgeek.midiacraft;
    
    import org.bukkit.ChatColor;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.EventPriority;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.event.player.PlayerQuitEvent;
    //import org.bukkit.event.block.SignChangeEvent;
    
    public class midiacraftListener implements Listener {
       
       
        @EventHandler(priority = EventPriority.MONITOR)
        public void playerConnectEvent(PlayerJoinEvent e)
        {
            Player p = e.getPlayer();
            String connectMessage = midiacraftPlugin.plugin.getConfig().getString("connect-message").replace(".ply.", p.getName());
           
            e.setJoinMessage(ChatColor.YELLOW + connectMessage);
           
        }
        @EventHandler(priority = EventPriority.MONITOR)
        public void PlayerDisconnect(PlayerQuitEvent e)
        {
            Player p = e.getPlayer();
            String disconnectMessage = midiacraftPlugin.plugin.getConfig().getString("disconnect-message").replace(".ply.", p.getName());
           
            e.setQuitMessage(ChatColor.RED + disconnectMessage);
           
           
        }
       
        /*@EventHandler(priority = EventPriority.LOWEST)
        public void onSignChange(SignChangeEvent event){
            if (event.getLine(0).equalsIgnoreCase("[marius]")){
                event.setLine(0, "test");
            }
        }*/
       
    }
    midiacraftCommands:
    Code:
    import org.bukkit.ChatColor;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.EventPriority;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerCommandPreprocessEvent;
    
    public class midiacraftCommands implements Listener {
        midiacraftChat chat;
       
        //private midiacraftPlugin plugin;
       
        @EventHandler(priority=EventPriority.MONITOR)
        private void onPlayerCommnandPreprocess(PlayerCommandPreprocessEvent msge){
           
            Player p = msge.getPlayer();
            String msg = msge.getMessage();
           
            String[] args = msg.split(" ");
            if (args[0].equalsIgnoreCase("/midia") && args[1].equalsIgnoreCase("infos")) {
                p.sendMessage(ChatColor.BLUE + "Plugin du serveur MidiaCraft");
                p.sendMessage(ChatColor.BLUE + "Créé par mrcoupobol (DrGEEK)");
                p.sendMessage(ChatColor.BLUE + "v0.1");
                msge.setCancelled(true);
               
               
            }
           
            if(args[0].equalsIgnoreCase("/censure")&&p.hasPermission("midiacraft.CanAddCensure")){
                chat.toCensore.add(args[1]);
                p.sendMessage(ChatColor.GREEN + String.format("Le mot %s a été banni du chat.", args[1]));
                msge.setCancelled(true);
            }
           
            if(args[0].equalsIgnoreCase("/adminmsg")){
                p.sendMessage(ChatColor.RED + "Commande INDEV");
                msge.setCancelled(true);
               
            }
        }
       
       
    }
    
    and midiacraftChat:
    Code:
    package be.drgeek.midiacraft;
    
    //import java.util.Collection;
    //import java.util.List;
    import java.util.ArrayList;
    import org.bukkit.event.Listener;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.player.AsyncPlayerChatEvent;
    import org.bukkit.entity.Player;
    
    public class midiacraftChat implements Listener{
        midiacraftPlugin plugin;
        midiacraftCommands listener;
        ArrayList<String> toCensore = new ArrayList<String>();
    
       
        public void ChatListener(midiacraftPlugin plugin){
            this.plugin = plugin;
        }
       
       
        @EventHandler
        public void onPlayerChat(AsyncPlayerChatEvent event){
            Player p = event.getPlayer();
            String msg = event.getMessage().toLowerCase();
            for(int i = 0; i < toCensore.size(); i++){
                if(msg.contains(toCensore.get(i))){
                    p.sendMessage(String.format("§cCe mot n'est pas autorisé : %s", toCensore.get(i)));
                    event.setCancelled(true);
                }
            }
        }
       
    }
    
     
  19. Offline

    mrCookieSlime

    @mariusvnh
    The Variable chat is null in midiacraftCommands.
    You probably want to set the variable to an instance of midiacraftChat. You can do that using your constructor.
     
  20. Offline

    mariusvnh

  21. Offline

    mrCookieSlime

  22. Offline

    mariusvnh

Thread Status:
Not open for further replies.

Share This Page