[Bukkit Coding Help] How would I do this?

Status
This thread has been locked.

Plus.

livin
Supreme
Feedback score
16
Posts
311
Reactions
158
Resources
0
Hey everyone,

So I'm trying to make it so my plugin can hold every player's stats. Like some sort of player stat holder in a file. Like e.g. each player has their own "balance" or "energy count" etc.
What would I use for this? MySQL? HashMaps, what would I use?
 
PebbleHost
High performance, consistent uptime and fast support. Minecraft hosting that just works.

Oahu - Aidan

Developer
Supreme
Feedback score
13
Posts
144
Reactions
65
Resources
0
If you want to do a File database for some reason, this is how you would do it. The FileConfigiration variable is static so you can access the config and set strings, etc.

Code:
public static FileConfiguration playerFileConfig;

    @EventHandler
    public void onJoin(PlayerJoinEvent e) {
        Player p = e.getPlayer();
        File dataFolder = new File(getDataFolder() + File.separator + "data");
        File playerFile = new File(getDataFolder() + File.separator + "data" + p.getUniqueId() + ".yml");
        playerFileConfig = YamlConfiguration.loadConfiguration(playerFile);

        // Creating data folder if it doesnt already exist
        if (!dataFolder.exists()) {
            try {
                dataFolder.createNewFile();
            } catch (IOException io) {
                io.printStackTrace();
            }
        }

        // Creating player file if player does not already have a file
        if (!playerFile.exists()) {
            try {
                playerFile.createNewFile();
            } catch (IOException io) {
                io.printStackTrace();
            }
        }
    }
 

Plus.

livin
Supreme
Feedback score
16
Posts
311
Reactions
158
Resources
0
I would recommend the one build into java I'm just saying it could be easier for him to use an API to do it, sometimes that can be easier for new users to use.
I've never used this but I've seen people use this before to integrate with MySQL: https://dev.bukkit.org/projects/sqlibrary
From the sounds of things you're a beginner which is completely ok, welcome to programming.

Store all the data that you'd want in a YML file to start with, however do not get and modify all the data from the file in real time as events that you're tracking will happen quite quickly.

When the player joins, check that the data storage file contains his UUID, if not, then set up his part in the data storage file.

Have a "PlayerStats" object that stores all this data, and has all the data loaded into it from the data file.
When the player leaves save all his/her data to the file and remove the instance of the PlayerStats object.

If you don't have a clue what I'm on about then I would advise going and reading this book: http://pdf.th7.cn/down/files/1411/Java For Dummies, 6th Edition.pdf from cover to cover, then coming back to the Spigot API, once you've read that book you should be very well equipped to deal with issues like this.
+ a lot more

Thank you for spending your personal time on helping a complete stranger like me, I really appreciate it. :) <3
 

Ghast

Founding Father of Hypocrisy - https://artemis.ac
Supreme
Feedback score
54
Posts
2,096
Reactions
3,285
Resources
79
Can we all congratulate ourselves that we didn't attack a noob programmer for once lol, step in the right direction at least.
Congrats to all for the positive attitude in this thread. be proud of yourselves
 

Plus.

livin
Supreme
Feedback score
16
Posts
311
Reactions
158
Resources
0
If you want to do a File database for some reason, this is how you would do it. The FileConfigiration variable is static so you can access the config and set strings, etc.

Code:
public static FileConfiguration playerFileConfig;

    @EventHandler
    public void onJoin(PlayerJoinEvent e) {
        Player p = e.getPlayer();
        File dataFolder = new File(getDataFolder() + File.separator + "data");
        File playerFile = new File(getDataFolder() + File.separator + "data" + p.getUniqueId() + ".yml");
        playerFileConfig = YamlConfiguration.loadConfiguration(playerFile);

        // Creating data folder if it doesnt already exist
        if (!dataFolder.exists()) {
            try {
                dataFolder.createNewFile();
            } catch (IOException io) {
                io.printStackTrace();
            }
        }

        // Creating player file if player does not already have a file
        if (!playerFile.exists()) {
            try {
                playerFile.createNewFile();
            } catch (IOException io) {
                io.printStackTrace();
            }
        }
    }
Will I need to do anything else for this? Or just paste this in and it works.
 
Status
This thread has been locked.
Top