Database directory read

  • I have a routine that stores items in a database that I want another plug in to read for display. What is the best method to tell the plugin where that database is. I don't want to duplicate the information I just want one or two big server databases with the information stored that I need so they may not be in the plugin directory that is looking. If you understand this request I appreciate in advance the help :S .

  • You know where the DB is, don't you? So, one solution is to hard code that (relative) path into the reading plug-in.


    If you really want the writing plug-in to be free to create the DB wherever it likes, you have to code the reading plug-in to connect to the writing plug-in and ask for the DB path.


    But at that point, I think it would be cleaner to let the writing plug-in to do all the DB work and have the 'reading' plug-in to directly ask for the information it needs to the 'writing' plug-in, without bothering with the DB path at all.

  • as @Miwarre said if you know all the paths and how the other plugin handles its database hard coding the relative path should be the easiest solution i.e. getPluginByName(Name_Of_Plugin).getPath() + "Path_To_Database_From_That_Plugin's_Directory"


    Then if you are talking about a back end plugin then these usually have in built public methods to get you access to their database. I think Miwarre's Bank plugin is written this way and I am pretty sure iConomy has such a method too but I would have to check. Then you would get the database with something like getPluginByName(Name_Of_Plugin).getDatabase() or getPluginByName(Name_Of_Plugin).addMoney(float Amount) etc. you would need to check the exact description of the other plugin for this.

  • Just to complement Minotorious' great description, "Name_of_Plugin" is the "name:" entry of the plug-in plugin.yml.


    As plug-ins are unlikely to go or change while RW is running, you may rather safely get the target plug-in at your plug-in OnEnable method and keep it in a global variable:

    Java
    @Override
    public void onEnable()
    {
    targetPlugin = (TargetPlugin)getPluginByName(targetPluginName);
    // more initialisation...
    }

    You may need to add the target plug-in in your IDE as an external Library / JAR / ... in order for the target plug-in specific methods to be known and callable.

  • No, that infor will work for now. It is that I have several different plugins and the information is needed for a couple of common requirements I am developing to some degree like the server and world information which there are routines to access. I have not graduated to the all encompassing single plugin that does it all which might be more efficient but none-the-less I wanted to start to consolidate the information amongst them. The intent is not to use a plugin's routine to read or write to the DB but to directly read/write to the DB. My routines are not currently storing the information they are just in future upgrade concept development and in testing for the execution of the game interface mechanics.


    One idea, I have on the sketching block would require this method involved your Lulascript Area protection routine database was providing an ingame way to add Shoppers to a common area without the need for an Admin or Owner. This can be triggered by either a group permission or a physical action by the player in game. <patent pending :rolleyes: >. A simple read and update to the database directly is all that is needed similar to a manual update to the DB by the server admins when a correction is needed. There is no need to tie into or use the plugin routines which seems very complicated to me.


    The SQL databases are simple tables with key parameters that are not very hard to read from and write too using the game plugin SQL command features if you know the db structure but as you point out the routine has to know where they are or it would create a blank one. I was putting an un-tested hard code routine in netbeans when it threw up a bunch of illegal escape errors from using a reverse slash\ in the address. I did not know what this meant but they were red so I am sure it would not compile.


    I was hoping the SQL routines in Rising World could handle wild cards like a tilde~/plugin/datab.db or something like to find the DB by unique name instead of the full path like, C:\Pingperfect\Users\RJ233\190000/plugins/routine/database.db needed on this commercial server, I am using. (Notice the shift in slashes needed which I don't understand - btw.). I do have an issue with Pingperfect liking to name all the DBs similarly which this, I believe, is a because I opted in for the advertisement of their service for a discount. Even if I could change it I am not going to now unless it is a problem.


    I will try what you suggest. Red already warned that SQL does not like multithreading but I am not that advanced. I could get a conflict if two people do the same action creating a write or read like he said he had trouble in the game dev with in the World DB and had to somehow line up the routines. I have no idea how to do that so knock-on-wood I dont have the activity for that though it might be nice.


    Thanks for the suggestions and direction. I will keep plug-in(g) away.. ... :D

  • One idea, I have on the sketching block would require this method involved your Lulascript Area protection routine database was providing an ingame way to add Shoppers to a common area without the need for a Admin or Owner. This can be triggered by either a group permission or a physical action by the player in game. <patent pending >. A simple read and update to the database directly is all that is needed similar to a manual update to the DB by the server admins when a correction is needed. There is no need to tie into or use the plugin routines which seems very complicated to me.

    This is true and would be really nice as a consept, but you need to be careful, as most plugins use SQLite databases that means that only one thread can edit them at any one time, in a scenario as described by you above if two players enter the area at the same time and your plugin tries to write to the database for both that would crash the database and sever the plugin's connection to it. Thus a server restart or a reinitialisation of the database in the plugin would be needed to access it again.

    I was hoping the SQL routines in Rising World could handle wild cards like a tilde~/plugin/datab.db or something like to find the DB by unique name instead of the full path like, C:\Pingperfect\Users\RJ233\190000/plugins/routine/database.db needed on this commercial server, I am using. (Notice the shift in slashes needed which I don't understand - btw.). I do have an issue with Pingperfect like to name all the DBs similarly which this, I believe, is a because I opted in for the advertisement of their service for a discount. Even if I could change it I am not going to now unless it is a problem.

    in your java class that extends plugin you need to use the getPath() method to get the path to your plugin's directory automatically thus no hard coding of paths is necessary. The you just extend the path with + "database.db".


    The shift in slashes is probably caused because the initial partition is using windows while the server partition is using linux.

  • This is true and would be really nice as a consept, but you need to be careful, as most plugins use SQLite databases that means that only one thread can edit them at any one time, in a scenario as described by you above if two players enter the area at the same time and your plugin tries to write to the database for both that would crash the database and sever the plugin's connection to it. Thus a server restart or a reinitialisation of the database in the plugin would be needed to access it again.

    in your java class that extends plugin you need to use the getPath() method to get the path to your plugin's directory automatically thus no hard coding of paths is necessary. The you just extend the path with + "database.db".
    The shift in slashes is probably caused because the initial partition is using windows while the server partition is using linux.

    Well they told me that they are windows servers.. but who knows.


    Yes RED warned in another thread about double threading to a DB. Right now well there is not allot of traffic on my server so not allot of risk but maybe by then I will have come up with a better solution. I dont like the thousands of DBs concept for the same function .. storing one thing here and one thing there is not very good for performance and interconnectability/useability in functions for a server.

  • Yes RED warned in another thread about double threading to a DB

    yep unless you are using MySQL then you can have as many threads as you want (with some realistic limits ofc don't go and open 1000000 connections because I said it is possible :D)

    I dont like the thousands of DBs concept for the same function .. storing one thing here and one thing there is not very good for performance and interconnectability/useability in functions for a server.

    I don't really understand this, why do you have more than one database for the same function to begin with? which plugins are you using that have the same function and are storing things in different databases? and tbh if both plugins have the same function then why not just get rid of one of them?


    From my point of view every server "should" have:
    1) its World database
    2) an Area Protection Database separate from any other to make sure it doesn't crash and anyone can destroy other people's stuff
    3) one extra database for each plugin they are using that requires one unless you home build all your plugins thus needing only one big database for it

  • What you dont understand was explained as several plugins I am developing are around a common theme they all contribute to an overall single purpose all provide information on player interactions. To have one item stored in one database that can contribute to a decision in another plugin is counterproductive but if I can read that interaction then I can use it. To make a huge plugin that does everything is counter productive to easy maintenance and change of the plugins.


    There are a few common things plugins track no matter who wrote them. They have a a key and usually single function in gameplay. Area Protection being one stores who can do things and who cannot and how much you can do that too. I have an idea I am running with that requires me to interface with that database to accomplish the flow of rights and enhance game mechanics for any player. Having to push these through a Lua script routine that I know nothing about is not as easy as pulling or pushing the information at the database outside of Area Protection routine. Area Protection does not seem to care who closed the database or who put the information there as long as it is in order and keyed correctly. I have had to manually edit it several times to correct issues as I did with another plugin database issue before I removed it from my server and went another direction.


    Area Protection is a necessary plugin because I cant/dont want to write that type of routine right now but I have a routine that will build stats on how much land they have protected. Waiting for a third party plugin writer to alter the routine they have no use would be problematic. I want to create a safe area there a shoppers can get access without an Admin or Owner based on certain interaction but not a public free access. These are all game play mechanics. (No it is not about player self claims either as I know there is a prewritten routine that helps with that)


    I have several routines I have written that accomplish completely unrelated mechanics but would benefit from common database to create a common game mechanic theme on my server. However they are in different plugin subdirectories to function. Hence the main question about database being stored in different spot than a single plugin subdirectory.

  • oh ok I see your point. I am not a fan of many small plugins, I prefer to have a big one that takes care of everything.


    But your point to keep the classes and the plugin easy to bugfix and readable is really important as well. Thus you can split your classes into smaller classes with more specific names and even put them into subpackages in your main package to keep things separate and easy to find bugfix ;)


    For example my ServerTools plugin has a main package called ServerTools then two subpackages Listeners which includes all the different listeners one class for each of them (i.e. one class Commands, other class Player Interactions, other class GuiClicks) and GuiComponents which includes all my Gui classes i.e. one class for the main panel, and one class for each tab of that panel. (you can see it all on my github)


    This way I have a big plugin doing everything but it is structured as to help me bugfix and read/develop it clearly ;)

  • Just to complement Minotorious' great description, "Name_of_Plugin" is the "name:" entry of the plug-in plugin.yml.


    As plug-ins are unlikely to go or change while RW is running, you may rather safely get the target plug-in at your plug-in OnEnable method and keep it in a global variable:

    Java
    @Override
    public void onEnable()
    {
    targetPlugin = (TargetPlugin)getPluginByName(targetPluginName);
    // more initialisation...
    }

    You may need to add the target plug-in in your IDE as an external Library / JAR / ... in order for the target plug-in specific methods to be known and callable.

    There in lies an issue if I access some third party plug in. The name is likely to be similar to all the others since many use the world name and on my commercial server the World Database is pingperfect.db and many other plugins are pingperfect.db.


    Not using either Iconomy or Mirrwares bank backend on my server. This is other plugins and luascripts.

  • There in lies an issue if I access some third party plug in. The name is likely to be similar to all the others since many use the world name and on my commercial server the World Database is pingperfect.db and many other plugins are pingperfect.db.

    The name we are referring to is the name of the plugin not the name of the database of the plugin. i.e. if you are using my Portals plugin to access it you can use portalsPlugin = getPluginByName("Portals") and the name of the database my plugin should create is portals_pingperfect.db

  • Thanks got something similar and I trying to run it right now.. had a typo and got a null exception


    OOPS not finding it


    so from your example
    Database db1 =((getSQLiteConnection(getPluginByName("Portals").getPath()) + "portals_pingperfect.db"));


    Correct?


    Seems to get the connectin but I get bumped in an exception when I try to read. I will let you know.

  • So yes it works. It gets the path. I open the connection and now having some sort of query exception catch issue that I should not have .


    oh ok.. here is the trick. You probably know this but for this NOOB to the sport i have to learn the hard way. The path only returns the path. If the DB is in the main plugin subdirectory you have to add the last / in front of the DB name.


    Database db1 =((getSQLiteConnection(getPluginByName("Portals").getPath()) + "/portals_pingperfect.db"));


    Thought I would elucidate for any casual readers here.



    Works like a charm guys.. THANKS !!! 8|:thumbsup:


    PS the rest of the path on my server has all those horrible \ that netbeans hates to the getpath thing worked just boss!

  • Just a minor correction, the line above is almost correct, but some brackets are at the wrong position (so it won't work this way). Probably you wanted it to look like this :)
    Database db1 = getSQLiteConnection(getPluginByName("Portals").getPath() + "/portals_pingperfect.db");

  • Just a minor correction, the line above is almost correct, but some brackets are at the wrong position (so it won't work this way). Probably you wanted it to look like this :)
    Database db1 = getSQLiteConnection(getPluginByName("Portals").getPath() + "/portals_pingperfect.db");

    Thanks, the extra parentheses (round brackets) are in a different place surrounding the getplugin statement. Maybe superfluous but is working.


    Database db1 =(getSQLiteConnection((getPluginByName("Portals").getPath()) + "/Portals_Pingperfect.db"));

  • I don't think extra brackets can cause any problems unless you haven't closed them so that is why your code still works with them there. ;)


    Having said that Java is not Lisp thus it is not good practice to add extra unnecessary brackets of any kind be they round (), square [], or curly {}. Anyway I think most IDEs will underline (probably yellow or green but not red as they are not "in error") unnecessary brackets.

  • You probably are most correct but I am a voodoo programmer... if it works dont muss with it, just keep holding your mouth sideways to make it work. Not much for semantics in programs just syntax though in communication I am.


    No yellow error other may than split text when highlighted.


    I have another routine similar that needed the brackets to execute the look up task as a complete variable, so I probably put them in when I was searching for the forward slash (/) solution.

  • I have another routine similar that needed the brackets to execute the look up task as a complete variable

    I am not sure there is any case in Java needing such weirdly placed brackets :/ the most complex bracket structure you can find in Java is either a lambda expression (i.e. how you specify the runnable task of a timer) or a casting structure i.e. int a = (int) myTimer.getTick()


    but glad you have managed to get it working in the end :)

Participate now!

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