Technical question of UID from playername without event

  • Ok I have a routine that is not an event. It is looking up something in a database. I have the playername as a string only but only the playername not pulled from get.Player and I want to look up and write the UID to the database. However everything I seem to find is looking for a event to capture the player interacting this is background filling of the database.


    Any idea? I keep getting cant find UID or incompatible type


    Player player = (playername) getName(); - I dont think this is correct but you get the idea that I need.
    long UID = (player.getUID());
    playername is the string I have from another class event.

  • If I understand correctly, you want to retrieve the (Unique) ID of a player from his name, regardless the player is currently connected or not.


    First, you need the plug-in object; if the code is in a class derived from net.risingworld.api.Plugin, the plug-in object is simply this; if it is in a class of a different inheritance, you have to work out how to reach the main plug-in object.


    Assuming plugin is the plug-in object:


    Java
    WorldDatabase db = plugin.getWorldDatabase();
    String queryString = "SELECT ... FROM Player ..."; // construct the query string you actually need
    ResultSet result = db.executeQuery(queryString);
    while(result.next())
    {
    // manage each row in the result set
    // according your query string and your needs
    }
    result.close();

    gives you access to the world data base; the data base can be queried; players are in the Player table; table columns you may be interested in are: ID (world-dependent, but unique and constant within each world), UID (universal; it is a long), Name. This should allow you to build the query string you actually need according to your goals.


    Remember that Database.executeQuery() may raise exceptions, which should be catch-ed (precisely how depends on the context and on your coding style).

  • The player is connected but I have only his playername not his UID. He is just not interacting other than he arrived and I now have his name but no UID in the database. I wanted to use his name which may have changed since his last login and change/update a database adding the players UID as another field. For some reason I am not able to pass the UID from the listener if he does not exist in the database.


    I was hoping do it without opening and reading the world database as that is a complication (not just in code for my neophyte capabilities) but then I would have two databases open and running through them That has got to slow people down or eat of memory.


    I guess I could just blast everyone away and make everyone start over but I thought I could get around that. In this way if he changed his name I dont double up database and I get the UID in if the player kept that same name.

  • What I was looking for is a way to get the UID with only the current online players name without him interacting into an event. The world database keeps an old name associated with a UID regardless of the name changing for the player. I have a database that has a player 4 times with different names. I want to catch him on the 5th name change and place the UID in that database.


    Maybe playerDB ID may be the way to go here but I dont know how to query to get the GUI without the player creating an event.

  • When the player connects to the server two events are called PlayerConnectEvent and then PlayerSpawnEvent, why don't you use any of those two events to get his UID?

    I will try it.. first time working with a database so there are some null exception thingies going on .. so SAD.. X/;( been 18 hours and nothing so far.


    This routine is its own class and I think I am not able to pass the event.. or not doing it correctly more likely

  • you would need to do something like:


    Player player = getServer().getPlayer(playerName)
    UIDtest = player.getUID();


    Though personally I find that solution kinda stupid, if you post your code with a more detailed explanation maybe I can help more :/

    ok the server part I was missing I think in what I was rounding up, it might work. I tried something like that but it didn't flushout .. how about, I switch the entire routine to UID tracking? I am passing via a method and that has been the issue.. so I am scraping the name and going only with UID but i would like the name in the DB for quick reference. So if I have the UID can I use it to get the name without an event? Or do I need to do what you just layed out for me? I dont want to pass it to the method as it is only needed in one of 4 methods.
    I was thinking there was a way to get it in the DB request by imbedding it in the string sort of like



    executeUpdate("INSERT INTO `Bank` (`PlayerName`, `royalchest`,`UID`) VALUES ('" + (getServer().getplayer(playerName) + "',0,'"+ UIDtest +"')") or something like that
    ignore the underline it was only to emphasis it for attention

  • I will give you an example of something similar I have done for my new version of ServerTools (not yet released as it is not ready :D )


    I have a database with two columns PlayerName and PlayerUID. Then I have two methods one that inserts a new entry in the database and one that updates the name of the player based on their UID in case they log in the server with a different name.


    Method 1: Insert a new entry

    Method 2: Update Nam


    in my onSpawnEvent I have these two lines to check the name and update it using method 2 above

    Java
    if (!player.getName().equals(currentplayerName)){ //Note currentplayerName comes from the database i.e. it is the old, already stored name
    changePlayerNameInAutoGM1List(player.getUID(), player.getName());
    }

    And in my onCommandEvent I have this line to add the player to the database once they type my command:

    Java
    setInAutoGM1List(player.getUID(), player.getName());
  • Ok thanks Minos... I will look at it tommorrow.. after being up to 3 am today and waking at 7 I am a bit exhausted. I decided to punt and pad the name with nulls for the moment because the important thing is to get it functioning. The names were just a cross reference and I can always look up people in the world if it becomes important. For server tools I can imagine but for this it was just a nice.. I dont know why my methods wouldnt transfer two things.. but it kept throwing errors.. probably apostrophe when it should have been an accent or some silly syntax thing that makes you eyes blurr... anyway .. got if updating the database with the UID and attaching the useful info.. so I am good for now..


    Your the man when it comes to sounding out these issues.. thanks a bunch. :thumbsup:



    oh just looked at your routines slightly .. not sure I am ready for prepared statements I am jut into string commands right now.. .. ooouf <X

  • oh just looked at your routines slightly .. not sure I am ready for prepared statements I am jut into string commands right now.. .. ooouf

    well a prepared statement is nothing more than a string statement to be executed on an SQLite or MySQL database. The point of using prepared statements is to avoid SQL injection attacks on the database, so it is nothing fancy just a way to stop people from messing with the database

  • Minos did your routine work? I am working on an upgrade for another routine and I was hoping to get a named players UID without having to write a routine to open the World Database and look it up. Seems all the routines to get the uid are for interactions or admins only.

  • What do you mean by if my routine worked? The code I posted above is fully functional and works inside the onPlayerSpawnEvent or the onPlayerCommandEvent.


    If the player you are trying to access is currently online you can get them via:


    Player player = getServer().getPlayer(playerName)
    UIDtest = player.getUID();


    This will work no matter if you call it in an event or a completely unrelated method.


    if the player is not online then there is no way to get their UID without accessing the world database

  • ok i keep getting netbean precompile errors.. will look it from another aspect.


    It is already defined error as I am looking for two different players.. one triggers the even the other is named in the event.

  • Minos when I try to use your PrepareStatement


    PreparedStatement prep = con.prepareStatement("UPDATE


    I get an error cannot find symbol PreparedStatement . Do I have to declare this a string?

    Nevermind I figured it out. I needed to import the java library item
    import java.sql.PreparedStatement;


    This is what frustrates me with examples.. there are hidden things programmers with experience know about that dont show up on examples in you find on the internet. To me once you attach a library it should have all the crap needed not having to import it every time for every class. It is really a wasteful time killing effort in my opinion. REWRITE JAVA NOW TO MAKE ME HAPPY> :cursing:8|:rolleyes:

  • well it all comes down on how you import things, when you do import java.sql.PreparedStatement; that only imports the one Java class called PreparedStatement from the java.sql package. So it makes sense for you not to be able to use say the Connection class for example even though they are both in the same package.


    To be able to use all the classes of a package you need to import it as java.sql.* so that the import includes all the classes of the package and not just one.


    having said that though it is not good practice to import huge packages if you are only going to use 3-4 classes thereof ;)

Participate now!

Don’t have an account yet? Create a new account now and be part of our community!