I have previously written a plugin (in Bukkit) to override the vanilla sleeping mechanics. In summary, my plugin would make the night pass faster depending on the ratio of sleeping to non-ignored waking players. In order for this to work, however, I’d need to keep players in beds for the duration of the modified night.
Vanilla mechanics have (if I recall) a 100 tick counter called SleepTimer in the player’s NBT. In Bukkit, I needed to use some reflection to modify that value every few ticks to A) Prevent it from reaching 100 before time to wake up and B) Set it to 100 when it was time to wake up.
So I’ve been thinking about porting this plugin to Sponge, but I’m wondering if Sponge has an API available to modify this SleepTimer value without reflection. From reading through some docs, I had though it might be part of the Data Manipulators, and I even saw the SleepingData class, however, this only seems to set the player as sleeping or not, not allowing to set the timer. Am I mistaken in thinking (if it was included in the API) that it should be here? Thanks in advance for any pointers!
Also, sorry if this is somehow redundant. It’s been a while since I’ve been involved with the forums here, I’ve likely missed plenty of discussion relevant to this.
If it were present in the API, there would be a Key for it in the Keys class, along with a DataManipulator which handles it.
There is in fact not a Key for sleep timing. So you should file an issue on the Sponge API GitHub.
I mean, you could for effect.
But it would be just as easy to cache their status as asleep even though they have popped out of the bed. Having to stay in a bed, even though you have voted for sleeping is kinda a drag.
I didn’t want to do the whole ‘vote for sleep’ business. I wanted to create a plugin that transitioned from night to day, avoiding the whole concept of instant-day. It is, after all, the effect I was going for when I set out to write the plugin. There was no real point in coding a vote for sleep plugin as there were already so many. I specifically wanted a plugin that would require a player to stay in bed in order to speed up night, otherwise, just survive it.
Back to the original question, however; I opened an issue on GitHub, and the response implies that it is possible to edit the NBT in question with the API in place using DataFormat. I’m still completely unfamiliar with the Sponge API, so I’ve no idea how to do so yet, or if the addition to a DataManipulator would still be applicable.
That GitHub response does not seem to really know what they are talking about, or what you are talking about. DataFormats.NBT is a class for writing DataContainers to NBT files. Not relevant at all.
Now, you could technically call toContainer on a Player and check the tag yourself via a DataQuery. However, this has about the same gravity as numerical item IDs; i.e. something that should be replaced with a proper API swiftly.
I wasn’t talking about instant-day, but that they wouldn’t need to remain in bed.
My proposed idea was sleeping in the bed keeps them there for 100t, then your plugin has them marked as sleeping and uses that towards sleep calculation until morning.
Otherwise it’s going to result in servers with some people in bed, nagging everyone else as they are bored.
Just my opinion. Not necessarily an argument against including it in the API.
Sorry for the late reply.
If you’re inclined, I’d appreciate if you could clarify on the github issue. With my lack of knowledge of the API, it may be difficult to explain exactly what I’m looking for. Can’t say one way or the other if a response is valid.
@ryantheleach Sponge may be easier, but I recall on Bukkit’s API, it was rather problematic to reliably prevent a user from moving, and since movement was rendered client-side until a server update, it was quite jumpy and glitchy. At the time, it seemed easier and smoother to use NBT to keep the player in bed. Perhaps I wouldn’t need to keep them in place, but I’d rather it feel like they were trading in some time to skip night faster. If it were free, seems like I might as well just disable time entirely and only have time change on a vote.
I suspect the nagging would always be a thing, even with instant-day voting, people would nag others to vote. Don’t see a way around that. The way mine is designed would make it so that each player that participates in sleeping has an instant effect on the duration of night, rather than needing a majority to participate.
All that aside, I run a really low-pop server. If one person sleeps, it usually speeds up night a good deal. Not to shoot down your suggestions, but I was quite pleased with how my Bukkit plugin turned out, at least to the extent that I’ve seen it in use so far.
What you are looking for is “a key and associated DataManipulator for the number of ticks a player has been in bed for”.
However, doesn’t vanilla keep them in bed until the night is over, regardless of who else is sleeping?
Yeah I understood what you were trying to do, it’s a good idea, I hope that it makes it into the DataAPI.