Bukkit MySQL资金系统

时间:2015-08-02 17:16:41

标签: java minecraft bukkit

我试图通过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$1.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$1.execute(JavaPluginLoader.java:306) ~[spigot-1.8.7-R0.1-SNAPSHOT-latest.jar:git-Spigot-f928e7a-e91aed8]
        ... 12 more

Bits(Money)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");

}

2 个答案:

答案 0 :(得分:2)

错误消息显示

  

引起:java.sql.SQLException:无法通过executeUpdate()发出SELECT。

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

public int getBits() throws Exception{

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

    return bits;
}

您还应该查看PreparedStatements。虽然这与错误无关,但在不使用PreparedStatement的情况下接受用户输入可能会导致SQL Injection,这会让攻击者运行他们想要的任何查询。

例如,如果您在提供有关迷你游戏的反馈时接受了用户输入,例如,使用查询

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

用户只需输入

即可
  

'反馈'&#34); DROP TABLE`反馈`; -

哪会产生两个陈述

  

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

     

DROP TABLE`reference`

答案 1 :(得分:0)

您似乎忘记在Bits类中设置UUID u字段,从而导致nullpointer

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

如果要阻止SQLInjection,可以使用PreparedStatement类: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方法用于运行更新查询并返回受影响的行数。

我认为你的目标是:

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文档。