Bukkit MySQL 货币系统

Bukkit MySQL Money System

我正在尝试通过 mysql 制作货币系统。每当玩家加入服务器时,都会通过 mysql 创建个人资料,并且完美无瑕。我无法让实际货币系统仅支持 mysql。每当我尝试向玩家充值时,它都会抛出 sql 错误并破坏用户 mysql 的个人资料。

我在 mysql 中有以下字段 UUID、名称、位(钱)、禁令

这是错误

        [14:03:51 INFO]: Crypted lost connection: Disconnected
[14:03:51 ERROR]: Could not pass event PlayerQuitEvent to KnoxHub v1.0
org.bukkit.event.EventException
        at org.bukkit.plugin.java.JavaPluginLoader.execute(JavaPluginLoader.java:310) ~[spigot-1.8.7-R0.1-SNAPSHOT-latest.jar:git-Spigot-f928e7a-e91aed8]
        at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[spigot-1.8.7-R0.1-SNAPSHOT-latest.jar:git-Spigot-f928e7a-e91aed8]
        at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:502) [spigot-1.8.7-R0.1-SNAPSHOT-latest.jar:git-Spigot-f928e7a-e91aed8]
        at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:487) [spigot-1.8.7-R0.1-SNAPSHOT-latest.jar:git-Spigot-f928e7a-e91aed8]
        at net.minecraft.server.v1_8_R3.PlayerList.disconnect(PlayerList.java:346) [spigot-1.8.7-R0.1-SNAPSHOT-latest.jar:git-Spigot-f928e7a-e91aed8]
        at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java:844) [spigot-1.8.7-R0.1-SNAPSHOT-latest.jar:git-Spigot-f928e7a-e91aed8]
        at net.minecraft.server.v1_8_R3.NetworkManager.l(NetworkManager.java:314) [spigot-1.8.7-R0.1-SNAPSHOT-latest.jar:git-Spigot-f928e7a-e91aed8]
        at net.minecraft.server.v1_8_R3.ServerConnection.c(ServerConnection.java:145) [spigot-1.8.7-R0.1-SNAPSHOT-latest.jar:git-Spigot-f928e7a-e91aed8]
        at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:817) [spigot-1.8.7-R0.1-SNAPSHOT-latest.jar:git-Spigot-f928e7a-e91aed8]
        at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:367) [spigot-1.8.7-R0.1-SNAPSHOT-latest.jar:git-Spigot-f928e7a-e91aed8]
        at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:657) [spigot-1.8.7-R0.1-SNAPSHOT-latest.jar:git-Spigot-f928e7a-e91aed8]
        at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:560) [spigot-1.8.7-R0.1-SNAPSHOT-latest.jar:git-Spigot-f928e7a-e91aed8]
        at java.lang.Thread.run(Unknown Source) [?:1.8.0_51]
Caused by: java.sql.SQLException: Can not issue SELECT via executeUpdate().
        at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1073) ~[spigot-1.8.7-R0.1-SNAPSHOT-latest.jar:git-Spigot-f928e7a-e91aed8]
        at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:987) ~[spigot-1.8.7-R0.1-SNAPSHOT-latest.jar:git-Spigot-f928e7a-e91aed8]
        at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:982) ~[spigot-1.8.7-R0.1-SNAPSHOT-latest.jar:git-Spigot-f928e7a-e91aed8]
        at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:927) ~[spigot-1.8.7-R0.1-SNAPSHOT-latest.jar:git-Spigot-f928e7a-e91aed8]
        at com.mysql.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1621) ~[spigot-1.8.7-R0.1-SNAPSHOT-latest.jar:git-Spigot-f928e7a-e91aed8]
        at com.mysql.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1581) ~[spigot-1.8.7-R0.1-SNAPSHOT-latest.jar:git-Spigot-f928e7a-e91aed8]
        at crypted.knoxhub.Libraries.MySQL.Database.updateSQL(Database.java:132) ~[?:?]
        at crypted.knoxhub.Libraries.Economy.Bits.getBits(Bits.java:37) ~[?:?]
        at crypted.knoxhub.Libraries.Economy.Bits.AddBits(Bits.java:50) ~[?:?]
        at crypted.knoxhub.Hub.Utilities.CreateUser.onJoin(CreateUser.java:41) ~[?:?]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_51]
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_51]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_51]
        at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_51]
        at org.bukkit.plugin.java.JavaPluginLoader.execute(JavaPluginLoader.java:306) ~[spigot-1.8.7-R0.1-SNAPSHOT-latest.jar:git-Spigot-f928e7a-e91aed8]
        ... 12 more

比特(金钱)Class

public Bits(Core p, UUID u)
{
    this.u = u;
    this.plugin = p;
    this.MySQL = new PlayerSQL(plugin);
}


public int getBits() throws Exception
{

  int bits2 =  MySQL.sql.updateSQL("SELECT FROM `profiles` WHERE `UUID`= '" + u + "';");

    return bits2;
}

public void setBits( int amount ) throws Exception{

    MySQL.sql.updateSQL("UPDATE FROM `profiles` SET `Bits`= '" + amount + "' WHERE `UUID`='" + u + "'");

}

public void AddBits( int amount ) throws Exception{

    MySQL.sql.updateSQL("UPDATE FROM `profiles` SET `Bits`= '" + getBits() + amount  + "' WHERE `UUID`='" + u + "'");

}

public void TakeBits( int amount ) throws Exception {

    MySQL.sql.updateSQL("UPDATE FROM `profiles` SET `Bits`= '" + getBits() + -amount  + "' WHERE `UUID`='" + u + "'");

}

在 PlayerQuitEvent 上添加钱

    @EventHandler
public void onJoin(PlayerQuitEvent e) throws Exception {
    Player player = e.getPlayer();
    Bits bits = new Bits(plugin, player.getUniqueId());
    bits.AddBits(5000); 
    Bukkit.getConsoleSender().sendMessage(ChatColor.GREEN + "Debug");

}

您似乎忘记在您的位 class 中设置 UUID u 字段,导致空指针

public Bits(Core p, UUID u)
{
    this.plugin = p;
    this.u = u;
    this.MySQL = new PlayerSQL(plugin);
}

如果你想防止 SQLInjection,你可以使用 PreparedStatement class:http://docs.oracle.com/javase/7/docs/api/java/sql/PreparedStatement.html

编辑:

你用新的错误更新了问题,我认为问题出在这个方法中:

public int getBits() throws Exception
{

  int bits2 =  MySQL.sql.updateSQL("SELECT FROM `profiles` WHERE `UUID`= '" + u + "';");

    return bits2;
}

updateSQL 方法用于 运行 更新查询和 returns 受影响的行数。

我认为您的目标是:

public int getBits() throws Exception
{

  ResultSet set =  MySQL.sql.executeQuery("SELECT bits FROM `profiles` WHERE `UUID`= '" + u + "';");

  if(set.next())
      return set.getInt("bits");

  return 0;
}

我建议您阅读 bukkit 文档。

错误消息说

Caused by: java.sql.SQLException: Can not issue SELECT via executeUpdate().

因此,问题是您不能将 SELECT 关键字与 .executeUpdate 一起使用。您需要使用 .executeQuery,或者在您的情况下使用 .execute。因此,您的 getBits() 方法应该如下所示

public int getBits() throws Exception{

    int bits =  MySQL.sql.execute("SELECT FROM `profiles` WHERE `UUID`= '" + u + "';");

    return bits;
}

您还应该研究 PreparedStatements. Although this has nothing to do with the error, taking a user input without using a PreparedStatement could result in SQL Injection,它允许攻击者 运行 他们想要的任何查询。

例如,如果您在用户对小游戏给出反馈时获取了用户输入,例如,使用查询

"INSERT INTO `feedback` (id, userid, feedback) VALUES (NULL, " +  userid + ", " + feedback + ");"

用户只需输入

'feedback'"); DROP TABLE `feedback`; --

这将导致两个语句

INSERT INTO `feedback` (id, userid, feedback) VALUES (NULL, 'userid', 'feedback');

DROP TABLE `feedback`