Jump to content

[Java] Dynamically named variables?

TheKDub

I'm working on a relatively large plugin for my Tekkit Classic Minecraft server, and I need to be able to access different user's files from different class files and pretty much any time, instead of going through and pulling up each file every single time I need it, then loading it into memory, I want to be able to just load it in once, then access it through it's unique name.

I'm using YAML files for the users, so keep that in mind.

I essentially need it to take a user's name in a string, then load the files in using modified versions of that user's name, such as:

String userfile = event.getPlayer().getName().toLowerCase() + "file";

String useryaml = event.getPlayer().getName().toLowerCase() + "yaml";

File userfile = new File(File location);

YamlConfiguration useryaml = YamlConfiguration.loadConfiguration(useryaml);

(I realized that the setup for that is a little... weird... I basically need to name the file and yamlconfiguration after the user that it's loading the file for, with yaml or file added to the end to differentiate the two)

Then essentially be able to use that naming scheme wherever so I can easily modify the user file from different class files.

I'm not entirely sure if this idea will work at all, but it's a nice learning experience.

Any tips on how to get that working would be greatly appreciated! (I tend to get maybe 10-30 unique users between server reboots, so multiple files being loaded into memory is not that big of a deal, especially with them being as small as they are)

 

Thanks!

 

--Edit--

So I am just confusing the fuck out of myself with this, and I'm sure I'm confusing you as well....

I'm not sure how to make this work at all, and realize some flaws with the stuff I've got up there..

perhaps more of

File event.getPlayer().getName().toLowerCase() + "file" = new File(File location);

YamlConfiguration event.getPlayer().getName().toLowerCase() + "yaml" = YamlConfiguration.loadConfiguration(event.getPlayer().getName().toLowerCase() + "file");

would make a bit more sense than the mess up there...

Specs: CPU - Intel i7 8700K @ 5GHz | GPU - Gigabyte GTX 970 G1 Gaming | Motherboard - ASUS Strix Z370-G WIFI AC | RAM - XPG Gammix DDR4-3000MHz 32GB (2x16GB) | Main Drive - Samsung 850 Evo 500GB M.2 | Other Drives - 7TB/3 Drives | CPU Cooler - Corsair H100i Pro | Case - Fractal Design Define C Mini TG | Power Supply - EVGA G3 850W

Link to comment
Share on other sites

Link to post
Share on other sites

Asked my school's comp science teacher about it, he said that I should use an arraylist..

 

I'll figure this out somehow.. I'm sure I've used either arraylists or hashmaps in the past..

Either one doesn't seem too difficult, and I'm thinking hashmaps will probably work better.

Specs: CPU - Intel i7 8700K @ 5GHz | GPU - Gigabyte GTX 970 G1 Gaming | Motherboard - ASUS Strix Z370-G WIFI AC | RAM - XPG Gammix DDR4-3000MHz 32GB (2x16GB) | Main Drive - Samsung 850 Evo 500GB M.2 | Other Drives - 7TB/3 Drives | CPU Cooler - Corsair H100i Pro | Case - Fractal Design Define C Mini TG | Power Supply - EVGA G3 850W

Link to comment
Share on other sites

Link to post
Share on other sites

But when you use hashmap or even hasharray you can identify object by string chosen by you, when you use arraylist you will know index where object is located in the array but it will be forced by order, not chosen by you. If you want access those data later you need either to remember which thing is on which index in array or use hasharray/hashmap.

Link to comment
Share on other sites

Link to post
Share on other sites

Okay first; you've been here long enough by now to know that when posting code you should be using code tags. Just incase you missed that there's a thread all about it right here:

 

PZ20Fqo.png

 

Secondly:

 

Asked my school's comp science teacher about it, he said that I should use an arraylist..

 

Sorry but the guy is a complete idiot. This is not a good design at all... maybe you're not inclined to care that much though... I don't know but what I do know is that you've been asking questions like this before and have chosen to proceed headless of much of the sound advice given.

 

For the level of complexity that you have got going on here you should abandon the thought process of using files altogether and implement this using a database instead. It will be a lot faster to implement and far more efficient and given the nature of your implementation (which is relational anyway) it will map easily. Moreover it will alleviate the burden of having to worry about concurrency concerns.

 

If I were you I would get it working with some database first and then I would surface up an interface around which I would then write a layer of behaviour based tests. Once you have that you can then swap/extend the implementation however you like at any point in the future without breaking the rest of your system. In other words get it implemented using a database and then decouple that implementation from the rest of the code.

 

Feel free to go right ahead and learn the hard way of course  :rolleyes:

The single biggest problem in communication is the illusion that it has taken place.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×