MySQL value is always -1

Discussion in 'Spigot Plugin Development' started by DotRar, Jun 13, 2016.

  1. So I have this tables in my database:
    Code (Text):
    | NAME         | SECONDS |
    | Nex_Perkelle |    3600 |
    | Nex_Perkelle |    3600 |
    However the values of "seconds" are apparently always -1
    I have this method with debug lines to help:
    Code (Text):
    public void start() {
            System.out.print("is booster:"+db.isBooster());
            if(!db.isBooster()) return;
            if(inProgress) return;
            inProgress = true;

            seconds = db.getSeconds();
            name = db.getBoosterActivator();

            task = new BukkitRunnable() {
                public void run() {
                    if(seconds % 60 == 0) {
                        db.setSeconds(name, seconds);

                    if(seconds <= 0) {
            task.runTaskTimerAsynchronously(pl, 20L, 20L);
    However I immediately get this all at once in the console:
    Code (Text):

    [19:11:41 INFO]: 1
    [19:11:41 INFO]: boop:true
    [19:11:41 INFO]: 2
    [19:11:41 INFO]: 3
    [19:11:41 INFO]: 4
    [19:11:42 INFO]: 5:-1
    [19:11:42 INFO]: 7
    [19:11:42 INFO]: 6
    the line 5:-1 shows that the seconds values are not coming through or something.

    Here's my getSeconds method:
    Code (Text):
    public int getSeconds() {
            Callable<Integer> task = new Callable<Integer>() {
                public Integer call() throws Exception {
                    try  {
                        PreparedStatement ps = conn.prepareStatement("SELECT * FROM doubleCoins;");
                        ResultSet resultSet = ps.executeQuery();
                        int toReturn;

                        if(resultSet.first()) {
                            try {
                                toReturn =  Integer.parseInt(resultSet.getString("SECONDS"));
                            } catch(NumberFormatException e) {
                                toReturn = 0;

                        toReturn =  0;
                        return toReturn;
                    catch(SQLException e) {
                        return 0;
            try {
                return Executors.newSingleThreadExecutor().submit(task).get();
            } catch(ExecutionException e) {
                return 0;
            } catch(InterruptedException e) {
                return 0;
    My only theory is that because I am using async database methods the code is just setting the value to -1 before the database has a chance to respond

    Thanks :D
  2. You can't do a countdown like that, you are going to end up corrupting your database
  3. How will it corrupt it?
  4. The AS3 class I posted on your last thread uses a buffer (a queue) to post queries to the database one at a time, solving any async issues. There's a MySQL version here and it wouldn't be difficult to port from AS3 to Java since they're both ECMAScript dialects. Try running queries synchronously, then see what the results are. Async operations tend to interfere with one another and your debug output is useless with them.

    Also, yeah. On your last thread there was mention of keeping everything on memory until onDisable and then dumping what's left into the database. Much, much better.
  5. I would use that but I would like a thing in the bungee saying how long is left (Like hypixel has on double coins weekends)
  6. The ServiceLocator, Registry, and Command design patterns are your friend. I haven't worked with Bungee, but I assume there's a way to tell it (within the codebase) what to do/say when/how, just like any other API. My (reasonable) assumption is that it doesn't use MySQL databases to tell it what to do, and that you're making things more difficult by trying to do everything in one superclass.
  7. Try using if( {
  8. There is no way this will lead to a return of anything other than 0...
  9. JamesJ


    rip database.

    Save a timestamp in the database. Calculate the remaining time until that timestamp is reached. This is how Hypixel, and most other plugins, will do it.
  10. That's what we told him in his old thread but I guess he doesn't care about making something that will work.
  11. Ok I will give doing it in memory a shot.

    Also will change that. Somehow, it gives -1 though.
  12. Ok I have got it working w/ the memory thing
  13. That is because you are decrementing the variable right before printing its value.