Solved Remove From Database

Discussion in 'Plugin Development' started by macboinc, Jul 14, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    macboinc

    Hey,

    I made a banning plugin that uses a database. When I ban someone it works fine but I can't unban them. (Using UUIDS)Here is my unban method:

    Code:
        public void unbanPlayer(Player p) {
            try {
                PreparedStatement statement = connection.prepareStatement("delete from users where uuid='" + p.getUniqueId() + "';");
                statement.executeUpdate();
                statement.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
            
    Here is where I use the method:
    And yes, I returned onCommand boolean.
    Code:java
    1. if (cmd.getName().equalsIgnoreCase("unban")) {
    2. if (sender.hasPermission("punishment.use")) {
    3. if (args.length < 1) {
    4. sender.sendMessage(ChatColor.GREEN + "/unban <player>");
    5. return true;
    6. }
    7.  
    8. Player player = Bukkit.getPlayer(args[0]);
    9.  
    10. mysql.unbanPlayer(player);
    11. return true;
    12. }
    13. And, yes, I returned the boolean.
    14. }


    And the error:
    Code:
    >unban macboinc1
    [09:51:09 WARN]: java.lang.NullPointerException
    [09:51:09 WARN]:        at me.macboinc1.punishment.MySQL.unbanPlayer(MySQL.java:
    51)
    [09:51:09 WARN]:        at me.macboinc1.punishment.Punishment.onCommand(Punishme
    nt.java:74)
    [09:51:09 WARN]:        at org.bukkit.command.PluginCommand.execute(PluginComman
    d.java:44)
    [09:51:09 WARN]:        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCo
    mmandMap.java:180)
    [09:51:09 WARN]:        at org.bukkit.craftbukkit.v1_7_R3.CraftServer.dispatchCo
    mmand(CraftServer.java:701)
    [09:51:09 WARN]:        at org.bukkit.craftbukkit.v1_7_R3.CraftServer.dispatchSe
    rverCommand(CraftServer.java:688)
    [09:51:09 WARN]:        at net.minecraft.server.v1_7_R3.DedicatedServer.aB(Dedic
    atedServer.java:296)
    [09:51:09 WARN]:        at net.minecraft.server.v1_7_R3.DedicatedServer.v(Dedica
    tedServer.java:261)
    [09:51:09 WARN]:        at net.minecraft.server.v1_7_R3.MinecraftServer.u(Minecr
    aftServer.java:558)
    [09:51:09 WARN]:        at net.minecraft.server.v1_7_R3.MinecraftServer.run(Mine
    craftServer.java:469)
    [09:51:09 WARN]:        at net.minecraft.server.v1_7_R3.ThreadServerApplication.
    run(SourceFile:628)
    >unban macboinc1
    [09:51:11 WARN]: java.lang.NullPointerException
    [09:51:11 WARN]:        at me.macboinc1.punishment.MySQL.unbanPlayer(MySQL.java:
    51)
    [09:51:11 WARN]:        at me.macboinc1.punishment.Punishment.onCommand(Punishme
    nt.java:74)
    [09:51:11 WARN]:        at org.bukkit.command.PluginCommand.execute(PluginComman
    d.java:44)
    [09:51:11 WARN]:        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCo
    mmandMap.java:180)
    [09:51:11 WARN]:        at org.bukkit.craftbukkit.v1_7_R3.CraftServer.dispatchCo
    mmand(CraftServer.java:701)
    [09:51:11 WARN]:        at org.bukkit.craftbukkit.v1_7_R3.CraftServer.dispatchSe
    rverCommand(CraftServer.java:688)
    [09:51:11 WARN]:        at net.minecraft.server.v1_7_R3.DedicatedServer.aB(Dedic
    atedServer.java:296)
    [09:51:11 WARN]:        at net.minecraft.server.v1_7_R3.DedicatedServer.v(Dedica
    tedServer.java:261)
    [09:51:11 WARN]:        at net.minecraft.server.v1_7_R3.MinecraftServer.u(Minecr
    aftServer.java:558)
    [09:51:11 WARN]:        at net.minecraft.server.v1_7_R3.MinecraftServer.run(Mine
    craftServer.java:469)
    [09:51:11 WARN]:        at net.minecraft.server.v1_7_R3.ThreadServerApplication.
    run(SourceFile:628)
    >
    Please help!
    -Mac
     
  2. Offline

    Aventium

    First, I just want to point out that I have no idea how you got that code to not throw errors. On to the error, can you tell me what's on line 51 of the MySQL class?
     
  3. Offline

    macboinc

    Sorry for the pasting error on the unbanplayer method,the computer derped out, anyways, this is what is on line 51:
    Code:java
    1. PreparedStatement statement = connection.prepareStatement("delete from users where uuid='" + p.getUniqueId() + "';");
     
  4. Offline

    Wizehh

    Can you show us where you initialize your "connection" variable? It's probably null, hence the NullPointerException.
     
  5. Offline

    macboinc

    private Connection connection;

    Oh, I see the problem now, just, how would I fix this?

    Code:java
    1. package me.macboinc1.punishment;
    2.  
    3. import java.sql.Connection;
    4. import java.sql.DriverManager;
    5. import java.sql.PreparedStatement;
    6. import java.sql.ResultSet;
    7.  
    8. import org.bukkit.entity.Player;
    9.  
    10. public class MySQL {
    11.  
    12. private Connection connection;
    13.  
    14. public MySQL(String ip, String userName, String password, String db) {
    15. try {
    16. Class.forName("com.mysql.jdbc.Driver");
    17. connection = DriverManager.getConnection("jdbc:mysql://" + ip + "/" + db + "?user=" + userName + "&password=" + password);
    18. } catch (Exception e) {
    19. e.printStackTrace();
    20. }
    21. }
    22.  
    23. public String getBannedReason(Player p) {
    24. try {
    25. PreparedStatement statement = connection.prepareStatement("select reason from users where uuid='" + p.getUniqueId() + "'");
    26. ResultSet result = statement.executeQuery();
    27.  
    28. if (result.next()) {
    29. return result.getString("reason");
    30. } else {
    31. return null;
    32. }
    33. } catch (Exception e) {
    34. e.printStackTrace();
    35. return "[[Failed to connect]]";
    36. }
    37. }
    38.  
    39. public void banPlayer(Player p, String reason) {
    40. try {
    41. PreparedStatement statement = connection.prepareStatement("insert into users (uuid, reason)\nvalues ('" + p.getUniqueId() + "', '" + reason + "');");
    42. statement.executeUpdate();
    43. statement.close();
    44. } catch (Exception e) {
    45. e.printStackTrace();
    46. }
    47. }
    48.  
    49. public void unbanPlayer(Player p) {
    50. try {
    51. PreparedStatement statement = connection.prepareStatement("delete from users where uuid='" + p.getUniqueId() + "';");
    52. statement.executeUpdate();
    53. statement.close();
    54. } catch (Exception e) {
    55. e.printStackTrace();
    56. }
    57.  
    58. }
    59. }


    The ban function works, just the unban one doesn't.

    Wizehh

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 9, 2016
  6. Offline

    Wingzzz

    macboinc
    Why not just use a boolean, toggle it for "banned".
     
  7. Offline

    macboinc

    Yes, I removed the check if the player is null because if the player is banned, they are offline, and I can't do that only if they're online. If I had to do this I would need to query the database.
     
  8. Offline

    Wizehh

    I honestly cannot comprehend that sentence. Can you please reiterate that in a more clear manner?
     
  9. Offline

    macboinc

    If I do that, it will say that they are not online.

    So I can't unban them.

    http://imgur.com/eV0DctL

    No because he is banned

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 9, 2016
  10. Offline

    Wizehh

    I can't write an in-depth explanation since I'm using a mobile device, but just change all your Player variables to type OfflinePlayer.
     
  11. Offline

    macboinc

    So instead of Player type 'OfflinePlayer'?
     
Thread Status:
Not open for further replies.

Share This Page