Problem with a my plugin

Discussion in 'Plugin Development' started by HexaFun, May 11, 2021.

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

    HexaFun

    hello, I’m trying to make a minecraft plugin that connects the minecraft chat and a discord channel, but I get an error that I don’t understand, can you please help me?
    Code:
    package listener; 
     import org.bukkit.event.EventException; 
    import org.bukkit.event.EventHandler;
     import org.bukkit.event.Listener;
     import org.bukkit.event.player.AsyncPlayerChatEvent;
     import fr.ultired.discordmclink.Main; 
     public class BukkitListener implements Listener 
    {
    private final Main main;
     public BukkitListener(Main main)
     {
     this.main = main;
     }
     @EventHandler private void onPlayerChat(AsyncPlayerChatEvent e) throws EventException
     {
     main.debugDiscord();
     }
     }
     

    Attached Files:

    Last edited by a moderator: May 11, 2021
  2. Online

    timtower Administrator Administrator Moderator

    @HexaFun What is on line 69 of debugDiscord?
     
  3. Offline

    Strahan

    Yea, you need to read your stacktraces and post the code from the actual thing breaking. After the "Caused by" line it will tell you where in the code it happened. In your case:
    Code:
    Caused by: java.lang.NullPointerException
        at fr.ultired.discordmclink.Main.debugDiscord(Main.java:69) ~[?:?]
    The main class's debugDiscord method, line 69 is where the problem is so showing us the listener class doesn't help at all.
     
  4. Offline

    HexaFun

    here’s the second part of the code, sorry, I didn’t think you’d need it :
    Code:
    package fr.ultired.discordmclink; 
     import javax.security.auth.login.LoginException;
     import org.bukkit.Bukkit;
     import org.bukkit.entity.Player;
     import org.bukkit.plugin.java.JavaPlugin;
     import listener.BukkitListener;
     import listener.DiscordListener;
     import net.dv8tion.jda.api.AccountType;
     import net.dv8tion.jda.api.JDA;
     import net.dv8tion.jda.api.JDABuilder;
     import net.dv8tion.jda.api.entities.Message;
     public class Main extends JavaPlugin
     {
     private JDA jda;
     private final long channelId = 839492187703083039L; 
    @SuppressWarnings("deprecation") @Override public void onLoad()
     {
     try 
    {
     jda = new JDABuilder(AccountType.BOT).setToken("").addEventListeners(new DiscordListener(this)).build();
     }
     catch (LoginException e) 
    {
     e.printStackTrace(); 
     }
     super.onLoad();
     }
    
    
    
    @Override public void onEnable() { Bukkit.getPluginManager().registerEvents(new BukkitListener(this), this); super.onEnable(); } 
    @Override public void onDisable() { jda.shutdown(); super.onDisable(); } 
    public JDA getJda() { return jda; } 
    public long getChannelId() { return channelId; } 
    public void sendMessageToMinecraft(Message message) { Bukkit.broadcastMessage("<"+message.getAuthor().getName()+"> "+message.getContentDisplay()); } 
    public void sendMessageToDiscord(Player player, String message) { jda.getTextChannelById(channelId).sendMessage(player.getName()+" : "+message).queue(); } 
    public void debugDiscord() { jda.getTextChannelById(channelId).sendMessage("Debug !").queue(); } 
    }
     
    Last edited by a moderator: May 11, 2021
  5. Online

    timtower Administrator Administrator Moderator

  6. Offline

    HexaFun

    I managed to understand my mistake, it works, thank you for your help.
     
  7. Offline

    OkayName

    Please mark this thread as solved then :)
     
Thread Status:
Not open for further replies.

Share This Page