How to create a getConfig() in another class.

Discussion in 'Plugin Development' started by TheDaannn, Apr 16, 2020.

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

    TheDaannn

    Hello, I am a starting developer and I am still learning a lot.

    I have a problem, I want to add a config in another class, how do I do this? I can't add JavaPlugin in another class.

    Event Class:

    Code:
    package me.thedaann.testplugin.events;
    
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerJoinEvent;
    
    public class PlayerJoin implements Listener {
    
        @EventHandler
        public void onJoin(PlayerJoinEvent e) {
            Player p = e.getPlayer();
            if (p.getPlayer().hasPermission("admin.join")) {
                e.setJoinMessage("§f" + p.getName() + " §7joined the game!");
            }
        }
    }
    

    Main Class:

    Code:
    package me.thedaann.testplugin;
    
    import me.thedaann.testplugin.commands.FeedCommand;
    import me.thedaann.testplugin.commands.HealCommand;
    import me.thedaann.testplugin.commands.TeleportCommand;
    import me.thedaann.testplugin.events.BlockBreak;
    import me.thedaann.testplugin.events.BlockPlace;
    import me.thedaann.testplugin.events.PlayerJoin;
    import org.bukkit.Bukkit;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.event.Listener;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class Main extends JavaPlugin implements Listener {
    
    
        public void onEnable() {
            getLogger().info("TestPlugin v" + this.getDescription().getVersion() + " turned off!");
            getCommand("heal").setExecutor(new HealCommand());
            getCommand("feed").setExecutor(new FeedCommand());
            getCommand("tp").setExecutor(new TeleportCommand());
            Bukkit.getServer().getPluginManager().registerEvents(new BlockBreak(), this);
            Bukkit.getServer().getPluginManager().registerEvents(new BlockPlace(), this);
            Bukkit.getServer().getPluginManager().registerEvents(new PlayerJoin(), this);
        }
    
        public void onDisable() {
            getLogger().info("TestPlugin v" + this.getDescription().getVersion() + " staat uit!");
        }
    
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
    
    
            return true;
    
        }
    
    
    }
    
     
  2. Online

    timtower Administrator Administrator Moderator

    @TheDaannn Pass the main class to the listener class using the constructor.
    And don't log your own plugins, Bukkit does that for you.
     
  3. Offline

    TheDaannn

    @timtower

    Okay thanks, how exactly does a constructor work? Again I am a novice developer
     
  4. Online

    timtower Administrator Administrator Moderator

  5. Offline

    TheDaannn

    ^^ Do I have to change that in my main class or my event class?

    @timtower
     
  6. Online

    timtower Administrator Administrator Moderator

    @TheDaannn You need to change things in both.
     
  7. Offline

    TheDaannn

    @timtower The problem is, I don't really know much about this yet, so I don't really understand what to do.
     
  8. Offline

    Strahan

Thread Status:
Not open for further replies.

Share This Page