在Bukkit / Spigot插件中实现SQL

时间:2016-01-18 13:37:41

标签: mysql sql minecraft bukkit

首先,我是这些论坛的新手,所以如果(或何时)我在某个地方犯了一个小错误,我很抱歉。

刚刚学习了HTML和CSS之后,我最近开始学习Java。 我是Minecraft服务器网络的开发人员,我制作的其中一个插件是启用/ color [颜色代码或颜色名称]的插件。

现在,我已经建立了一个MySQL数据库,在其中我创建了一个以UUID和Color作为变量的表。 我一直在努力让插件连接到数据库并存储/检索他选择的播放器名称的颜色,因为它需要跨多个服务器工作。

我希望优化插件,因此它只在需要时连接并与SQL数据库断开连接,因此它不会产生太多延迟。 这是我现在的代码:

package nl.live.partyblaze.pbcolor;

import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;

public final class PBColor extends JavaPlugin
{
    @Override
    public void onEnable()
    {
        getLogger().info("[PBColor] onEnable has been invoked!");
    }

    @Override
    public void onDisable()
    {
        getLogger().info("[PBColor] onDisable has been invoked!");
    }

    @Override
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args)
    {
        if (cmd.getName().equalsIgnoreCase("color"))
        {
            if (args.length == 1)
            {
                if (args[0].equalsIgnoreCase("a"))
                {
                    String displ = sender.getName();
                    Player p = (Player) sender;
                    p.setDisplayName(ChatColor.GREEN + displ);
                    sender.sendMessage("§aYour name has been set to green!");
                    return true;
                }

                if (args[0].equalsIgnoreCase("green"))
                {
                    String displ = sender.getName();
                    Player p = (Player) sender;
                    p.setDisplayName(ChatColor.GREEN + displ);
                    sender.sendMessage("§aYour name has been set to green!");
                    return true;
                }

                if (args[0].equalsIgnoreCase("b"))
                {
                    String displ = sender.getName();
                    Player p = (Player) sender;
                    p.setDisplayName(ChatColor.AQUA + displ);
                    sender.sendMessage("§bYour name has been set to aqua!");
                    return true;
                }

                if (args[0].equalsIgnoreCase("aqua"))
                {
                    String displ = sender.getName();
                    Player p = (Player) sender;
                    p.setDisplayName(ChatColor.AQUA + displ);
                    sender.sendMessage("§bYour name has been set to aqua!");
                    return true;
                }

                if (args[0].equalsIgnoreCase("c"))
                {
                    String displ = sender.getName();
                    Player p = (Player) sender;
                    p.setDisplayName(ChatColor.RED + displ);
                    sender.sendMessage("§cYour name has been set to red!");
                    return true;
                }

                if (args[0].equalsIgnoreCase("red"))
                {
                    String displ = sender.getName();
                    Player p = (Player) sender;
                    p.setDisplayName(ChatColor.RED + displ);
                    sender.sendMessage("§cYour name has been set to red!");
                    return true;
                }

                if (args[0].equalsIgnoreCase("d"))
                {
                    String displ = sender.getName();
                    Player p = (Player) sender;
                    p.setDisplayName(ChatColor.LIGHT_PURPLE + displ);
                    sender.sendMessage("§dYour name has been set to light purple!");
                    return true;
                }

                if (args[0].equalsIgnoreCase("lightpurple"))
                {
                    String displ = sender.getName();
                    Player p = (Player) sender;
                    p.setDisplayName(ChatColor.LIGHT_PURPLE + displ);
                    sender.sendMessage("§dYour name has been set to light purple!");
                    return true;
                }

                if (args[0].equalsIgnoreCase("e"))
                {
                    String displ = sender.getName();
                    Player p = (Player) sender;
                    p.setDisplayName(ChatColor.YELLOW + displ);
                    sender.sendMessage("§eYour name has been set to yellow!");
                    return true;
                }

                if (args[0].equalsIgnoreCase("yellow"))
                {
                    String displ = sender.getName();
                    Player p = (Player) sender;
                    p.setDisplayName(ChatColor.YELLOW + displ);
                    sender.sendMessage("§eYour name has been set to yellow!");
                    return true;
                }

                if (args[0].equalsIgnoreCase("f"))
                {
                    String displ = sender.getName();
                    Player p = (Player) sender;
                    p.setDisplayName(ChatColor.WHITE + displ);
                    sender.sendMessage("§fYour name has been set to white!");
                    return true;
                }

                if (args[0].equalsIgnoreCase("white"))
                {
                    String displ = sender.getName();
                    Player p = (Player) sender;
                    p.setDisplayName(ChatColor.WHITE + displ);
                    sender.sendMessage("§fYour name has been set to white!");
                    return true;
                }

                if (args[0].equalsIgnoreCase("1"))
                {
                    String displ = sender.getName();
                    Player p = (Player) sender;
                    p.setDisplayName(ChatColor.DARK_BLUE + displ);
                    sender.sendMessage("§1Your name has been set to dark blue!");
                    return true;
                }

                if (args[0].equalsIgnoreCase("darkblue"))
                {
                    String displ = sender.getName();
                    Player p = (Player) sender;
                    p.setDisplayName(ChatColor.DARK_BLUE + displ);
                    sender.sendMessage("§1Your name has been set to dark blue!");
                    return true;
                }

                if (args[0].equalsIgnoreCase("2"))
                {
                    String displ = sender.getName();
                    Player p = (Player) sender;
                    p.setDisplayName(ChatColor.DARK_GREEN + displ);
                    sender.sendMessage("§2Your name has been set to dark green!");
                    return true;
                }

                if (args[0].equalsIgnoreCase("darkgreen"))
                {
                    String displ = sender.getName();
                    Player p = (Player) sender;
                    p.setDisplayName(ChatColor.DARK_GREEN + displ);
                    sender.sendMessage("§2Your name has been set to dark green!");
                    return true;
                }

                if (args[0].equalsIgnoreCase("3"))
                {
                    String displ = sender.getName();
                    Player p = (Player) sender;
                    p.setDisplayName(ChatColor.DARK_AQUA + displ);
                    sender.sendMessage("§3Your name has been set to dark aqua!");
                    return true;
                }

                if (args[0].equalsIgnoreCase("darkaqua"))
                {
                    String displ = sender.getName();
                    Player p = (Player) sender;
                    p.setDisplayName(ChatColor.DARK_AQUA + displ);
                    sender.sendMessage("§3Your name has been set to dark aqua!");
                    return true;
                }

                if (args[0].equalsIgnoreCase("4"))
                {
                    String displ = sender.getName();
                    Player p = (Player) sender;
                    p.setDisplayName(ChatColor.DARK_RED + displ);
                    sender.sendMessage("§4Your name has been set to dark red!");
                    return true;
                }

                if (args[0].equalsIgnoreCase("darkred"))
                {
                    String displ = sender.getName();
                    Player p = (Player) sender;
                    p.setDisplayName(ChatColor.DARK_RED + displ);
                    sender.sendMessage("§4Your name has been set to dark red!");
                    return true;
                }

                if (args[0].equalsIgnoreCase("5"))
                {
                    String displ = sender.getName();
                    Player p = (Player) sender;
                    p.setDisplayName(ChatColor.DARK_PURPLE + displ);
                    sender.sendMessage("§5Your name has been set to dark purple!");
                    return true;
                }

                if (args[0].equalsIgnoreCase("darkpurple"))
                {
                    String displ = sender.getName();
                    Player p = (Player) sender;
                    p.setDisplayName(ChatColor.DARK_PURPLE + displ);
                    sender.sendMessage("§5Your name has been set to dark purple!");
                    return true;
                }

                if (args[0].equalsIgnoreCase("6"))
                {
                    String displ = sender.getName();
                    Player p = (Player) sender;
                    p.setDisplayName(ChatColor.GOLD + displ);
                    sender.sendMessage("§6Your name has been set to gold!");
                    return true;
                }

                if (args[0].equalsIgnoreCase("gold"))
                {
                    String displ = sender.getName();
                    Player p = (Player) sender;
                    p.setDisplayName(ChatColor.GOLD + displ);
                    sender.sendMessage("§6Your name has been set to gold!");
                    return true;
                }

                if (args[0].equalsIgnoreCase("7"))
                {
                    String displ = sender.getName();
                    Player p = (Player) sender;
                    p.setDisplayName(ChatColor.GRAY + displ);
                    sender.sendMessage("§7Your name has been set to gray!");
                    return true;
                }

                if (args[0].equalsIgnoreCase("gray"))
                {
                    String displ = sender.getName();
                    Player p = (Player) sender;
                    p.setDisplayName(ChatColor.GRAY + displ);
                    sender.sendMessage("§7Your name has been set to gray!");
                    return true;
                }

                if (args[0].equalsIgnoreCase("8"))
                {
                    String displ = sender.getName();
                    Player p = (Player) sender;
                    p.setDisplayName(ChatColor.DARK_GRAY + displ);
                    sender.sendMessage("§8Your name has been set to dark gray!");
                    return true;
                }

                if (args[0].equalsIgnoreCase("darkgray"))
                {
                    String displ = sender.getName();
                    Player p = (Player) sender;
                    p.setDisplayName(ChatColor.DARK_GRAY + displ);
                    sender.sendMessage("§8Your name has been set to dark gray!");
                    return true;
                }

                if (args[0].equalsIgnoreCase("9"))
                {
                    String displ = sender.getName();
                    Player p = (Player) sender;
                    p.setDisplayName(ChatColor.BLUE + displ);
                    sender.sendMessage("§9Your name has been set to blue!");
                    return true;
                }

                if (args[0].equalsIgnoreCase("blue"))
                {
                    String displ = sender.getName();
                    Player p = (Player) sender;
                    p.setDisplayName(ChatColor.BLUE + displ);
                    sender.sendMessage("§9Your name has been set to blue!");
                    return true;
                }
            }
        }
        return false; 
    }
}

提前致谢!

1 个答案:

答案 0 :(得分:1)

在我的插件中,我使用了这个名为MySQL的类:

package me.Nightfighter001.Minepedia-System.Methoden;

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

import org.bukkit.Bukkit;
import org.bukkit.command.ConsoleCommandSender;

public class MySQL {

    public static String host = "minepedia.eu";
    public static String port = "3306";
    public static String database = "GlobalData";
    public static String username = "******";
    public static String password = "*****";
    public static Connection con;

    static ConsoleCommandSender console = Bukkit.getConsoleSender();

    // connect
    public static void connect() {
        if (!isConnected()) {
            try {
                con = DriverManager.getConnection("jdbc:mysql://" + host + ":" + port + "/" + database, username, password);
                console.sendMessage("\247c[\2476Minepedia-System\247c] \247bMySQL-Verbindung wurde aufgebaut!");
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    // disconnect
    public static void disconnect() {
        if (isConnected()) {
            try {
                con.close();
                console.sendMessage("\247c[\2476Minepedia-System\247c]\247bMySQL-Verbindung wurde geschlossen!");
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    // isConnected
    public static boolean isConnected() {
        return (con == null ? false : true);
    }

    // getConnection
    public static Connection getConnection() {
        return con;
    }
}

onEnable()中,您应致电MySQL.connect();,然后在onDisable()致电MySQL.disconnect();。这将正确连接和断开MySQL实例。

这段代码的编写假设你缩进在Bukkit / Spigot上运行它,而不是在BungeeCord中运行它,你应该用等效的BungeeCord替换CommandSender

PreparedStatements的一些示例:

创建Table

PreparedStatement ps = MySQL.getConnection().prepareStatement("CREATE TABLE IF NOT EXISTS Players (Name VARCHAR(100),UUID VARCHAR(100),Coins INT(100),PRIMARY KEY (Name))");
ps.executeUpdate();

PRIMARY KEY表示Name无法复制。 VARCHARString几乎相同。

清空Table

PreparedStatement ps = MySQL.getConnection().prepareStatement("TRUNCATE Players");
ps.executeUpdate();

将某些内容插入Table

PreparedStatement ps = MySQL.getConnection().prepareStatement("INSERT IGNORE INTO Players (Name,UUID,Coins) VALUES (?,?,?)");
ps.setString(1, Player.getName());
ps.setString(2, Player.getUniqueId());
ps.setInt(3, 10);
ps.executeUpdate();

使用setString() / setInt(),我会替换问号。

Table删除内容:

PreparedStatement ps = MySQL.getConnection().prepareStatement("DELETE FROM Players WHERE Name = ?");
ps.setString(1, Player.getName());
ps.executeUpdate();

Table阅读:

PreparedStatement ps = MySQL.getConnection().prepareStatement("SELECT Coins FROM Players WHERE Name = ?");
ps.setString(1, Player.getName());
ResultSet rs = ps.executeQuery();
int coins = 0;
if (rs.next() == true) {
    coins = rs.getInt("Coins");
}

如果您希望从Players等于Name的{​​{1}}获取所有内容,则可以使用Player.getName()代替*

我希望这会有所帮助。抱歉我的英语不好了。