#Ways to store data for players
1 messages · Page 1 of 1 (latest)
use a db then
but
dont use yaml for large datasets
how should i store the data
Create an abstract layer of an interface
do i use PersistentDataContainers?
then have different implementations?
in that case you can easily try an implementation of PDC as well as flat files or databases
yes
pdc
ok
but the rest of your code would simply rely on your interface
but
^^
no
or store it on a map
cache it
^^
like Map<Player, Data>
I believe pdc is cached?
you can use an IdentityHashMap there
whats that
its a special hash map
personally i use this
@Getter
private static transient ExpiringMap<String, IslandCache> cacheMap = ExpiringMap.builder()
.expiration(10, TimeUnit.MINUTES)
.expirationPolicy(ExpirationPolicy.ACCESSED)
.asyncExpirationListener((k, v) ->
{
try {
((IslandCache) v).save();
} catch (IOException e) {
e.printStackTrace();
}
}).build();
this is from a library of mine. but you can maybe find similar options
so its not Serialized with json
ah fair
is it java or bukkit/spigot
its in the jdk
if and only if instance a is instance b then a == b
thats how the identity hash map identifies its keys
(well not really) but in theory
and you'd use it because it doesnt have to go through any hashing computations
so 2 identic maps (in content) are not equal?
like a normal hash map would have to do
no
thing is a regular HashMap uses the implemented hashCode()
to determine if two keys are the same or not
but why isnt that a good implementation
so why shouldnt i just use HashMap
just because it needs to hash a key?
no because it uses the memory address of objects as its keys
which requires literal to no computation
and can i use IdentityHashMaps the same way as a regular HashMap?
nope
its not that you cant go with a regular HashMap
its just that an IdentityHashMap is more suitable for storing Player as key given that you remove the entry on disconnect
but you would have to do that with a normal HashMap also
defo shoulnt store username
which is less performant
yeah usernames are changeable
UUIDs require proper hash computation
arent uuid's Strings
well that
but in Java they're objects
dont
what are you using for keys on your identityhashmap suggestion conclure
why?
the player object?
for the express purpose of having one player be able to have multiple instances of itself?
given that he only needs the key during the session
in my map player keys are unique, a player can only appear once
there will only be one valid player instance valid during that players time online on the server instance
but i can store it while the server is online
unless you'd for some bizarre reason run a weird fork of spigot which changes that
i run paper
then it should be fine
yeah this sounds like you're just trying to implement possible mem leaks in really fun and interesting ways by mistake
;-;
I wouldn't rely on identity specifically because one player could accidentally create a never ending amounts of itself on the server if one thing violates your assumptions of how the player sessions will run
fake players for one have caused me lots of fun issues in the past
then why shouldnt i just store uuids on a regular hashmap
it's marginally less performant so maybe you'll see a tps drop at 10,000 logins per tick instead of 100,000 logins per tick directly due to this code
player logins?
is this not for caching on login?
I have never seen this
but that's definitely not the case
player login?
well yeah
you've never had issues with fake russian players?
lucky
data is retrieved from DB to map
no
and on disable or when data needs to be written i write to DB
that the player reproduces a lot of instances from them mere selves
if the fake players come with a fake login they might never log out, at which point you'd never have a reason to remove them from your map
and they might log in as many times as someone decided they'd be doing that for
i can make it so it gets removed from map on logout
or it checking for an instance of the player in the map before adding it
by using uuid hashmap
wouldnt that login be rejected? I thought spigot as well as bungeecord or any other proxy/server software had the ability to handle it
you misunderstand me, this is not an attack vector, this is a feature of some software out there
I have never seen it happen when working with newer versions so Idk but if what you assert is true then Ig it would be bad
last russian report of such issues I had was in... I want to say 2019
but I patched the issue so it could still be going on right now, it's just not visible to me anymore
fair then
I will say these issues tend to only pop up in russian and east asian servers for whatever reason, they have some trippy plugins and forks
if this is for a more personal use then don't worry too much about it
its for a private server with no russian players and less than 20 players in total
yeah you're fine
so what should i use then
either will work, conclure's version is slightly more performant and a regular hashmap is slightly safer
but i shoudnt really worry abt performance if it has less than 20 players right?
by the time you realize you have to start worrying about performance you've already monumentally fucked up in a way that might be near impossible to fix
i want to make an api that fetches data from any player that has entered the server
Believe that's fully possible with pdc?
or you are making a plugin like worldedit that needs to do a lot of code to fill big chunks of blocks and want it as performant as possible
or nvm
oh
OfflinePlayer doesnt extend PersistentDataHolder
and what about if server is offline
is there any way to retrieve pdc from server files?
just use a database at that point
probs too complex
thats why i want to use a db
but i dont want to use the DB EVERY time i need to access data from players
thats too expensive
on player login, cache the data
on player logout, make sure the data is synced and clear it out
if you need to query the db for offline players during runtime you will need to query the db
doesn't matter, I prefer maps
specifically I prefer keeping objects for my players with data relevant for that player for the plugin I'm working on
that fetches data from the DB
for offline players, even when the main server is offline
kk ill use maps
be careful about concurrent modifications of your db
?
if you are not just reading but modifying data externally
nono
just reading
to display the custom data of players
i may make like a discord bot where you can see a player's custom data
you'll probably want to make sure that you are keeping some degree of parity between your server and your db
yes thats the thing
i might check whether the player is online
then if he is retrieve data from map
if not from db?
yes
ok now time to learn how to use a DB yay