Using mysql with tokens plugin.

Discussion in 'Spigot Plugin Development' started by avighnash, Jun 8, 2016.

  1. I want to create a tokens plugin for my server that could be accessed throughout my bungecord instance. For that, I decided to use MySQL. But the only problem is that I do not know how to start. I have never used SQL before, so could someone explain to me how I should use it here, and what are the advantages of it? Also, how do I implement it into my code? I saw several tutorials but they use HikaryCP (wtf is dat) and other stuff.

    Thanks!
     
  2. I wouldn't use mysql for tokens. Mysql is for me too slow. Especially if you're going to have a lot of players.

    What I did for the network where I was administration on, was that I got a Plugin on the bungee coord instance that has a list with all the tokens. It also listened to the Plugin messaging channel for a string for example that is basically a command. "Set player 1000" that would set the coins to 1000. And the same thing for add and remove. Then I would also have a get which sends back the coins.
    In the spigot plugin I keep a list of coins for myself. I listen for the coins returnEd from the bungee coord instance and save it.
    The only problem with this is that getting the coins takes about a tick. So you would have to work out a way to keep the coins accurate. You could do that bungee and spigot only sync coins when a player joins or leaves the server.
     
  3. Have you even googled?

    https://www.google.nl/webhp?sourcei...TF-8#q=how to use mysql in your plugin spigot
     
  4. There are ways you can use JDBC with your own custom methods but making them as efficient as some libraries is going to be difficult, MySQL itself is a database management software. One of which i use on a daily basis, here are some refrences to learn the commands for MySQL

    https://dev.mysql.com/doc/refman/5.7/en/what-is-mysql.html
    http://www.w3schools.com/sql/

    remember these commands directly apply in the same way in code, so strings are queries. Something you should use to practice is setting up a local server, i will list then below. And use the myPHPAdmin page to enter in MySQL queries manually to test out commands yourself.

    Windows - WAMP: www.wampserver.com/en/
    Mac - MAMP: https://www.mamp.info/ (Non pro is fine for what you need)
    Linux - LAMP: https://bitnami.com/stack/lamp/installer

    I would stick to windows or mac, but if you must use the linux version just note there might be extra steps to install.


    Once all of the above is done some quick tutorials on implementing this in java can be found at:

    http://zetcode.com/db/mysqljava/
    http://www.vogella.com/tutorials/MySQLJava/article.html
    https://dev.mysql.com/doc/connector-j/5.1/en/connector-j-usagenotes-connect-drivermanager.html

    I would reccommend the last option as it comes directly from the MySQL documentation. All of them use JDBC (Java Database Connectivity) so if you want to use HicaryCP or otherwise consider using maven to build your plugins to include it as a dependancy.

    MySQL can be as complicated or as easy as you want it to be, start out small and then move onto secondary keys, connecting tables, unique identifiers, and other things that can be found in the documentation. Hope this post helped :) because for me i had to learn MySQL alone so these links i have provided should provide you with some help as to which direction you should go when learning it.

    as for applying all of this to your plugin, well that is entirely up to you, whether you want to create one table with all the data in there or multiple tables that link is completely up to you, personally i would make it as simple as possible and aim to keep most of the data stored local, that way you can reduce the lag between saving and getting.

    EDIT: Make sure on your final build you add TONS of security to your system, the amount of times people put "root" as username and "password" as password makes it too easy for people to change values in the database and give themselves free stuff. For the most security only allow your servers IP access to the database.

    DOUBLE EDIT: For the least latency have a local storage of whatever you want to store and update the local one (so it seems as if the change is instant) and then in the background load the new values into the database. So in short, have a queue of things to be updated in the database and continue normal function with that queue running in the background.
     
    • Winner Winner x 2
    • Funny Funny x 1
  5. Yes I have googled. Couldn't find anything that did not use another library. I will definitely try what you said @bram0101.

    Thanks!
     
  6. I made a bungeecord plugin, but now how do I do the bungeecord plugin messaging? I always had trouble with that.
     
  7. Then do some research! You need a basic understanding of how that all works if you want to use it. :p
     
  8. The only thing slow about SQL is establishing a connection to a database, and if you load your information on the AsyncPlayerJoinEvent, there shouldn't be nearly any problem executing it on a different thread with an async BukkitRunnable.
     
  9. Okay, so here is my credits class for spigot:

    Code (Text):
    package us.universalpvp.te.utils;

    import java.io.ByteArrayInputStream;
    import java.io.ByteArrayOutputStream;
    import java.io.DataInputStream;
    import java.io.DataOutputStream;
    import java.io.IOException;

    import org.bukkit.entity.Player;

    public class Credits {

        public void getCredits(Player p) {
            ByteArrayOutputStream stream = new ByteArrayOutputStream();
            DataOutputStream out = new DataOutputStream(stream);
            try {
                out.writeUTF("Get credits from " + p.getName());
            } catch (IOException e) {
                e.printStackTrace();
            }

        }

        public void addCredits(Player p, int amount) {
            ByteArrayOutputStream stream = new ByteArrayOutputStream();
            DataOutputStream out = new DataOutputStream(stream);
            try {
                out.writeUTF("Add credits for " + amount + " " + p.getName());
            } catch (IOException e) {
                e.printStackTrace();
            }

        }

        public void removeCredits(Player p, int amount) {
            ByteArrayOutputStream stream = new ByteArrayOutputStream();
            DataOutputStream out = new DataOutputStream(stream);
            try {
                out.writeUTF("Remove credits " + amount + " from " + p.getName());
            } catch (IOException e) {
                e.printStackTrace();
            }

        }

        public void onPluginMessageReceived(String channel, Player player, byte[] bytes) throws IOException {
            if (!channel.equals("BungeeCord")) {
                return;
            }

            ByteArrayInputStream stream = new ByteArrayInputStream(bytes);
            DataInputStream in = new DataInputStream(stream);
            in.readUTF();

        }

    }
     
    and my bungeecord class:

    Code (Text):
    ackage net.hypixel.credits;

    import java.util.HashMap;
    import java.util.UUID;

    import net.md_5.bungee.api.connection.ProxiedPlayer;
    import net.md_5.bungee.api.plugin.Plugin;

    public class CreditAPI extends Plugin {
        @Override
        public void onEnable() {

        }

        private static HashMap<UUID, Integer> coins = new HashMap<>();

        public static void addCoins(ProxiedPlayer p, int amount) {
            coins.put(p.getUniqueId(), amount);
        }

        public static void removeCoins(ProxiedPlayer p, int amount) {
            coins.put(p.getUniqueId(), coins.get(p.getUniqueId()) - amount);
        }

        public static int getCoins(ProxiedPlayer p) {
            if (!coins.containsKey(p.getUniqueId())) {
                return 0;
            } else {
                return coins.get(p.getUniqueId());
            }

        }
       


    }
    so what do I do now?
     
  10. net.hypixel.credits? I'm quite certain for some reason that you're not writing code for hypixel, lol. Also, remove the static from your class, there's no good reason for those methods to be static.
     
    • Agree Agree x 3
    • Like Like x 2
    • Funny Funny x 2
  11. You need to have a map with the credits in the spigot plugin to actually save the data you get so you can access it without having to ask the bungee Plugin.
    And in the bungee plugin you need to check for the messages and do what those messages say. That's about it.
     
  12. Lol, I made this resource b4 I had my own domain lol
    Will do!
     
  13. How do I listen to the messages? Do I use PluginMessageEvent in bungeecord?
     
  14. Any ideas? And which is better, plugin messaging or sql?
     
  15. I'd say you're starting out, and whether you like it or not you're going to have to learn SQL one day or another. MySQL, when used properly is neither slow nor unreliable. If I were you, I'd make this plugin using MySQL because not only will it work perfectly but it will also be a great learning experience for you. Plugin messaging channels are very useful too, but also more complex. You need a lot of time to do proper research about it and release something potable, and that knowledge might be barely applicable in other frameworks than Spigot. Overall it's your choice, but in my opinion SQL would be the way to go.
     
  16. Just look at the wiki of bungeecoord and there's a sticky post in the bungee plugin development forum
     
  17. Sql is better to learn if your planning on making stuff that would be public for a lot of people.
    Plugin messaging is better to learn if you're doing spigot-bungee stuff.
    They're both not that easy to learn. But I think that Plugin messaging is easier because all you need to really know is how to register and how to read and write data. That's about it.
    The advantage of Plugin messaging is that the connection is already there. And once you know how to write and read stuff. It's not that hard.
    It all does depend on what you think would be best. In my opinion for something like this, Plugin messaging would be the best.