Problem/Bug Classes in Main class

Discussion in 'Bukkit Help' started by ZeusReksYou_, May 1, 2016.

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

    ZeusReksYou_

    Hello guys,

    I am trying to code a ChatControl plugin, which already has a clearchat function. Now I coded an AntiSwear class, but I don't know how to add it so it will work. I thought I had to add it into the main class but I don't know how to.

    This is my main class, and I want to make sure my AntiSwear part works as well. The other classes of the ClearChat plugin are all working with its commands.

    Code:
    package mcplugin.ZeusReksYou_.main;
    
    import org.bukkit.Bukkit;
    import org.bukkit.plugin.java.JavaPlugin;
    
    import mcplugin.ZeusReksYou_.chatcontrol.ChatControlHelp;
    import mcplugin.ZeusReksYou_.clearchat.ClearGlobal;
    import mcplugin.ZeusReksYou_.clearchat.ClearOwn;
    
    public class Main extends JavaPlugin  {
    
        public void onEnable() {
           
            Bukkit.getServer().getLogger().info("ChatControl enabled. Plugin by ZeusReksYou_");
            getConfig().options().copyDefaults(true);
            saveConfig();
    
            // Global Part:
           
            getCommand("chatcontrolhelp").setExecutor(new ChatControlHelp());
           
            // ClearChat Part:
    
            getCommand("clearchatglobal").setExecutor(new ClearGlobal());
            getCommand("clearchatown").setExecutor(new ClearOwn());
    
            // AntiSwear Part:
                }     }
    
    AntiSwear Code:

    Code:
    package mcplugin.ZeusReksYou_.antiswear;
    
    import org.bukkit.ChatColor;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.AsyncPlayerChatEvent;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class AntiSwearMain extends JavaPlugin implements Listener {
    
        @EventHandler
        public void onPlayerChat(AsyncPlayerChatEvent e) {
            for (String word : e.getMessage().split(" ")) {
                if (getConfig().getStringList("Not allowed words").contains(word)) {
                    e.setCancelled(true);
                    e.getPlayer().sendMessage(ChatColor.RED + "You are not allowed to curse !");     
                }        }    }   
    }
    Can anyone help me ??

    Sincerely,

    ZeusReksYou_
     
  2. Offline

    tkuiyeager1

    I don't think you can make spaces in config strings, try to change it to something without spaces.
    Example:
    AntiSwearWords.
     
  3. Offline

    ZeusReksYou_

    This doesn't seem to be the problem. The plugin does generate a config.yml, with or without spaces in the config string.
    I made 4 packages in the plugin,

    1. a main package with the onEnable() and the command-imports
    2. a package for the ClearChat-Commands
    3. a package with the global help commands
    4. a pacakge with the antiswear plugin (without a command in it)

    Now I imported the command-classes of the ClearChat & Global Help packages like you can see in my first message (the getCommand("").setExector..... (()part)). All those commands are working, but only the AntiSwear part without a command isn't working.

    I already tried to put the AntiSwear class into the main package but also that didn't work.
    What should I do now?
     
  4. Offline

    I Al Istannen

    @ZeusReksYou_
    Stopping to have two classes that extend JavaPlugin in one jar would be a good start. You can register an event with Bukkit.getPluginManager()#registerEvents(Listener, Plugin). Use this method and remove the "extends JavaPlugin" from the AntiswearMain class. Then as Listener pass on an instance of the AntiSwear class.

    If you really want to classes extending JavaPlugin, make two jar files.
     
    DoggyCodeâ„¢ likes this.
  5. Offline

    tkuiyeager1

    Have you tried calling the stringlist something without spaces? If that is not in the answer then i don't know how to help you, but try to do what i told you.
     
  6. Offline

    ZeusReksYou_

    I also added a class + package for the staffchat part, this also doesn't work...
     
  7. Offline

    tkuiyeager1

    Have you tried my idea?
     
  8. Offline

    timtower Administrator Administrator Moderator

    @ZeusReksYou_ You can't have multiple classes extending JavaPlugin in the same plugin.
     
    MasterDoctor01 and tkuiyeager1 like this.
  9. Offline

    ZeusReksYou_

    Yes but it didn't work
     
  10. Offline

    tkuiyeager1

    You are right i didn't notice that.

    Edit:
    Also @timtower stringlist at the config cant be with spaces right?
     
  11. Offline

    timtower Administrator Administrator Moderator

    Depends on the location of the spaces. A regular list with spaces is fine.
     
  12. Offline

    tkuiyeager1

    Ok, thanks.

    @ZeusReksYou_ You need to put the "extends JavaPlugin" only in your main class.
     
  13. Offline

    ZeusReksYou_

    @tkuiyeager1 @timtower @I Al Istannen Okay thanks. So now once I removed the "extends JavaPlugin" at my AntiSwearMain class, I get an error at the getConfig() part. I found this error before so that is why I added "extends JavaPlugin" to the class.
    The error says I got to create a method 'getConfig()' which I don't understand how to do that.

    I edited my mainclass a bit to the following:

    Code:
    public class Main extends JavaPlugin implements Listener  {
    
        private AntiSwearMain plugin;
    
        public void onEnable() {
          
            this.plugin = new AntiSwearMain();
          
            Bukkit.getPluginManager().registerEvents(this, (Plugin) plugin);
            Bukkit.getServer().getLogger().info("ChatControl Enabled. Plugin by ZeusReksYou_");
            getConfig().options().copyDefaults(true);
            saveConfig();
    
    // And here all the imports of the commands
    And in the AntiSwearMain I added this:

    Code:
    public class AntiSwearMain implements Listener {
    
    // instead of public class AntiSwearMain {
    // like I had before ^
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited: May 1, 2016
  14. Offline

    timtower Administrator Administrator Moderator

  15. Offline

    ZeusReksYou_

    @ZeusReksYou_
    Well, I just started Bukkit Coding 2 weeks ago, and I do not always understand everything. What do you mean by 'Constructors are you friends here'? How am I supposed to do this?
     
  16. Offline

    timtower Administrator Administrator Moderator

    MasterDoctor01 likes this.
  17. Offline

    ZeusReksYou_

    Alright like that, so I got to make a constructor in the main class which defines other classes?
    I don't exactly know where to place it.

    I made the following constructor:

    Code:
    AntiSwearMain AntiSwear = new AntiSwearMain();
    
    
    And this is what I did in the Main class

    Code:
     
    AntiSwearMain AntiSwear = new AntiSwearMain();
    
        public void onEnable() {
    
            this.AntiSwear = new AntiSwearMain();
    
            Bukkit.getPluginManager().registerEvents(this, (Plugin) AntiSwear);
    
    I am not sure wether to paste it in my AntiSwear class, in my Main class or in both, what to do?
     
    Last edited: May 1, 2016
  18. Offline

    timtower Administrator Administrator Moderator

    @ZeusReksYou_ Now you are initializing it twice, I am talking about the parameters for constructors.
    You can pass an instance of the main class to the listener class.
    You also shouldn't cast it.
     
  19. Offline

    MasterDoctor01

    That's wrong, you call it from the main class and use a constructor in the other classes

    Sent from my SM-T710 using Tapatalk
     
Thread Status:
Not open for further replies.

Share This Page