MySQL question :)

Discussion in 'Plugin Development' started by Funergy, Oct 31, 2014.

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

    Funergy

    Hello guys,
    I'm creating a custom OP-PVP plugin.
    And when the plugin gets disabled I upload every players kit delay to mysql so if the server enables the plugin again. I get the all the players kit delay and I start the runnable counting down.
    but this is the problem
    I have my table structure like this
    UUID|KITNAME|SECONDS

    but in the onEnable I'm getting every UUIDs's seconds and kit by getting every player with the WeeklyKit. but when I get that it gives me a Weekly kit player but not every player that used the Weekly kit(I think), if it isn't like that.

    how can I get every player that is in the MySQL database that is using the Kit Weekly
    And put it in a HashMap<Player,Integer> the Integer is seconds so I'm getting the seconds from the mysql database
     
  2. Offline

    Gnat008

    Funergy
    First, can I just say to not use Runnables for cooldowns? Use HashMaps with the current time in milliseconds, and check it against the time they try to perform the action again.
     
  3. Offline

    fireblast709

    Funergy use UUID keys for the Map. You can't use Player objects for persistence past a join session (a join session starts when a player joins, and ends when he leaves).
     
  4. Offline

    Funergy

    Gnat008 fireblast709 None of you guys are answering my question -.-
    lets say it again
    "how can I get every player that is in the MySQL database that is using the Kit Weekly
    And put it in a HashMap<Player,Integer> the Integer is seconds so I'm getting the seconds from the mysql database"

    How.
    and if I use a run task later I never actually get how long it should take to refresh the kit.
    like I can't say "You can use your kit again in 10 min 5 sec".

    fireblast709 I delete them from the list when leaving so theres no problem :)

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

    fireblast709

    Nonono I thought you said you were loading Players onEnable :p (in which case you won't have Players online unless it was a reload).

    Anyway, Gnat008 was somewhat answering your question. Instead of the amount of seconds they have to wait / waited, why not save/load a timestamp (whether it is when they last used it, or when they can use it again). I assume you have method to determine what the kit weekly is. If not (ex. when the weekly kits are personal) you could add a column which tracks if the kit is weekly or not.
     
  6. Offline

    Unica

    Query: SELECT uuid FROM table WHERE uuid IN anotherTable

    Then u can UUID.fromString()
     
  7. Offline

    Funergy

    Unica No you are wrong I mean I get all the UUIDs from a database that are using the kit Weekly.
    what you did is just getting 1 player when he joins or something.

    fireblast709 Okay how should I set in my MySQL when he can use it again. and how can I set when he tries to choose the kit again it says how long it would take to refresh
    I use this for now
    Code:
    public static String getTimeWeeklyTimer(String uuid){
            String time = "";
            String.format("Can only use kit in %d min, %d sec!",
                    TimeUnit.SECONDS.toMinutes(weeks.get(uuid)),
                    weeks.get(uuid) -
                    TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(weeks.get(uuid))));
            return time;
        }
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 10, 2016
  8. Offline

    fireblast709

    Funergy I would personally go with a BIGINT column (or one of the day / time types MySQL provides). The column would just hold System.currentTimeMillis() + <amount of milliseconds till refresh>. In the case you want to know how long that is, take the difference between that value and System.currentTimeMillis (which gives you the amount of ms between 'now' and the next refresh) and convert that to minutes and seconds.
     
  9. Offline

    Funergy

    fireblast709 Okay I understand almost everything what you've said. how could I use the BIGINT thing?
     
  10. Offline

    Unica

    Funergy
    no?

    the 'IN' is because it will return multiple rows WHERE kit = weekly
     
  11. Offline

    Funergy

    Last edited by a moderator: Jun 10, 2016
  12. Offline

    fireblast709

    Funergy It's a MySQL data type, I wasn't referring to BigInteger :p. BIGINT is pretty much the MySQL variant of long.
     
  13. Offline

    Funergy

    fireblast709 I'm having a little problem with getting the BigInteger
    Code:
    public BigInteger getTime(String UUID,String KIT){
            try {
                PreparedStatement statement = connection.prepareStatement("select TIME from OPPVPKitTimers where UUID='" +UUID+ " KIT='"+KIT+"'");
                ResultSet result = statement.executeQuery();
     
                 
                    if (result.next()) {
                            return result.getbig;//Here, theres no result.getBigInteger("Time")
                    } else {
                            return null;
                    }
            } catch (Exception e) {
                    e.printStackTrace();
                    return null;
            }
        }
    fireblast709 Is this how I should check if he can use the kit again?
    Code:
    if(gd.getTime(p.getUniqueId().toString(), "NORMAL")<System.currentTimeMillis()){
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 10, 2016
  14. Offline

    fireblast709

    I refer to my previous post :p (use getLong() to get BIGINT columns)
    Assuming you store System.currentTimeMillis() + <milliseconds until refresh>, yes.
     
Thread Status:
Not open for further replies.

Share This Page