在 Bukkit/Spigot 插件中实现 SQL
Implementing SQL in a Bukkit/Spigot plugin
首先,我是这些论坛的新手,如果(或何时)我在某处犯了一个小错误,我深表歉意。
前段时间学习了HTML和CSS,最近开始学习Java。
我是 Minecraft 服务器网络的一名开发人员,我制作的插件之一是启用 /color [颜色代码或颜色名称]。
现在,我已经建立了一个 MySQL 数据库,我在其中创建了一个 table 并将 UUID 和 Color 作为变量。
我一直在努力让插件连接到数据库和store/retrieve他选择的玩家名字的颜色,因为它需要跨多个服务器工作。
我希望插件得到优化,所以它只在需要时连接并断开与 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;
}
}
提前致谢!
在我的插件中,我使用这个 Class,我将其命名为 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("7c[76Minepedia-System7c] 7bMySQL-Verbindung wurde aufgebaut!");
} catch (SQLException e) {
e.printStackTrace();
}
}
}
// disconnect
public static void disconnect() {
if (isConnected()) {
try {
con.close();
console.sendMessage("7c[76Minepedia-System7c]7bMySQL-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 中缩进 运行,您应该将 CommandSender
替换为相当于 BungeeCord。
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
不能重复。
VARCHAR
与 String
几乎相同。
清空一个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
等于 Player.getName()
,您可以使用 *
代替 Coins
。
希望对您有所帮助。再次为我糟糕的英语感到抱歉。
首先,我是这些论坛的新手,如果(或何时)我在某处犯了一个小错误,我深表歉意。
前段时间学习了HTML和CSS,最近开始学习Java。 我是 Minecraft 服务器网络的一名开发人员,我制作的插件之一是启用 /color [颜色代码或颜色名称]。
现在,我已经建立了一个 MySQL 数据库,我在其中创建了一个 table 并将 UUID 和 Color 作为变量。 我一直在努力让插件连接到数据库和store/retrieve他选择的玩家名字的颜色,因为它需要跨多个服务器工作。
我希望插件得到优化,所以它只在需要时连接并断开与 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;
}
}
提前致谢!
在我的插件中,我使用这个 Class,我将其命名为 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("7c[76Minepedia-System7c] 7bMySQL-Verbindung wurde aufgebaut!");
} catch (SQLException e) {
e.printStackTrace();
}
}
}
// disconnect
public static void disconnect() {
if (isConnected()) {
try {
con.close();
console.sendMessage("7c[76Minepedia-System7c]7bMySQL-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 中缩进 运行,您应该将 CommandSender
替换为相当于 BungeeCord。
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
不能重复。
VARCHAR
与 String
几乎相同。
清空一个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
等于 Player.getName()
,您可以使用 *
代替 Coins
。
希望对您有所帮助。再次为我糟糕的英语感到抱歉。