Plugin help

Discussion in 'Spigot Help' started by MrXrex, May 4, 2021.

  1. Hello. Does anyone know why, after entering the command, the plugin returns nothing (even in the console there are no errors)

    main: pl.konkom.Test.MysqlMain


    MysqlMain:
    Code (Java):
    package pl.konkom.Test;

    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.SQLException;

    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.event.Listener;
    import org.bukkit.plugin.java.JavaPlugin;

    public class MysqlMain extends JavaPlugin implements Listener {
        private Connection connection;
        public String host, database, username, password;
        public int port;

        public void onEnable() {
            loadConfig();
            mysqlSetup();
         
            this.getServer().getPluginManager().registerEvents(new MysqlSetterGetter(), this);
        }
     
        public void loadConfig(){
            getConfig().options().copyDefaults(true);
            saveConfig();
        }

        public void mysqlSetup() {
            host = this.getConfig().getString("host");
            port = this.getConfig().getInt("port");
            database = this.getConfig().getString("database");
            username = this.getConfig().getString("username");
            password = this.getConfig().getString("password");

            try {

                synchronized (this) {
                    if (getConnection() != null && !getConnection().isClosed()) {
                        return;
                    }

                    Class.forName("com.mysql.jdbc.Driver");
                    setConnection(
                            DriverManager.getConnection("jdbc:mysql://" + this.host + ":" + this.port + "/" + this.database,
                                    this.username, this.password));

                    Bukkit.getConsoleSender().sendMessage(ChatColor.GREEN + "MYSQL CONNECTED");
                }
            } catch (SQLException e) {
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
        }

        public Connection getConnection() {
            return connection;
        }

        public void setConnection(Connection connection) {
            this.connection = connection;
        }

    }
     
    MysqlSetterGetter:

    Code (Java):
    package pl.konkom.Test;

    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;

    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.Listener;

    import net.md_5.bungee.api.ChatColor;


    public class MysqlSetterGetter implements Listener {

        MysqlMain plugin = MysqlMain.getPlugin(MysqlMain.class);

        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if (cmd.getName().equalsIgnoreCase("zgloszenie"))
            {
                if(sender instanceof Player) {
                    Player p = (Player) sender;
                    if(args.length >= 0){
                        StringBuilder sb = new StringBuilder();
                        for(int i = 0; i < args.length; i++){
                            sb.append(args[i]).append(" ");
                        }
                        String msg = sb.toString();
                     
                        p.sendMessage(ChatColor.GREEN+"Wiadomość została wysłana o treści: "+ChatColor.YELLOW+msg);
                        try {
                            PreparedStatement statement = plugin.getConnection()
                                    .prepareStatement("INSERT INTO Zgloszenia(nick, msg) VALUE (?, ?)");
                            statement.setString(1, p.getName());
                            statement.setString(2, sb.toString());
                            statement.executeUpdate();
                        }catch (SQLException e) {
                            e.printStackTrace();
                        }
                    }
                 
                }
                else
                {
                    sender.sendMessage("Konsola nie ma dostępu do tej komendy");
                }
            }
            if (cmd.getName().equalsIgnoreCase("zgloszenia"))
            {
                Player p = (Player) sender;
                p.sendMessage(ChatColor.BLUE+"#ID "+ChatColor.YELLOW+"nick: " +ChatColor.GREEN+"zgłoszenie");
                try {
                    PreparedStatement statement = plugin.getConnection()
                            .prepareStatement("SELECT ID,nick,msg FROM Zgloszenia");
                    ResultSet rs = statement.executeQuery();
                    while(rs.next()) {
                        p.sendMessage(ChatColor.BLUE+"#"+rs.getString("ID")+" "+ChatColor.YELLOW+rs.getString("nick")+": "+ChatColor.GREEN+rs.getString("msg"));
                    }
                }catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            else
            {
             
            }
            if (cmd.getName().equalsIgnoreCase("zgl"))
            {
                Player p = (Player) sender;
                p.sendMessage(ChatColor.BLUE+"#ID "+ChatColor.YELLOW+"nick: " +ChatColor.GREEN+"zgłoszenie");
                try {
                    PreparedStatement statement = plugin.getConnection()
                            .prepareStatement("SELECT * FROM  Zgloszenia WHERE nick = '" + p.getName() + "'");
                    ResultSet rs = statement.executeQuery();
                    while(rs.next()) {
                        p.sendMessage(ChatColor.BLUE+"#"+rs.getString("ID")+" "+ChatColor.YELLOW+rs.getString("nick")+": "+ChatColor.GREEN+rs.getString("msg"));
                    }
                }catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            else
            {
             
            }
            return false;
            }
    }
    and plugin.yml:

    name: Zgloszenia
    version: 1.0
    main: pl.konkom.Test.MysqlMain
    author: konkom

    commands:
    zgloszenie:
    aliases: z
    zgloszenia:
    permission: zgloszenia.admin.list
    aliases: zg
    zgl:
    aliases: zge
     
  2. Strahan

    Benefactor

    Your class for the command is not registered and isn't implementing anything to allow for commands. Also you shouldn't be doing SQL processing on the main thread.

    You're also duplicating effort. Like creating the Player var. Each command uses it, so just do it first before you enter the logic checks for the particular commands that way it's available to each command. You should really be checking if sender is a Player before casting it as well. You did that for zgloszenie, but not for the other two. Since your instanceof check is within the logic block, that doesn't mean it is covering the other two commands.

    Also don't use the player's name to index the data, use UUID. Names can change. You also can distill the msg code significantly, down to one line: String msg = StringUtils.join(args, " ", 0, args.length);. Your append method will also leave an errant space at the end of the string.