Filled SleepBuffs, Add a reason for sleeping

Discussion in 'Plugin Requests' started by Dark_Serpent, Aug 2, 2016.

Thread Status:
Not open for further replies.
  1. Offline


    Suggested name: SleepBuffs

    What I want: Current on my server, there's no real reason for people to sleep. I would like to request the following:
    • When a player sleeps in a bed, it will not turn to day and let the player get up.
    • The player will be immobile in the bed for 20 seconds.
    • When the player wakes up, they will receive Absorption 2 for 5 minutes and Haste 1 for 2 minutes.
    • The player must be above Y 50 to sleep, if they aren't, white text in their chat will say "You don't feel comfortable sleeping here."
    • Special note: The player is not considered "invincible" in this state, and must be actually laying in the bed, and not in a psuedo-state.
    If any or all of these are beyond the limitations of plugins, please tell me and I'll mark this thread as Solved!

    When I'd like it by: Whenever possible! Thank you to anyone who picks this up! :D

    Additional Notes: This plugin must be compatible for 1.7.10!
  2. @Dark_Serpent Most of this I could do in 20 minutes, not sure about the Y 50 + part but should be able to, I'll give it go!

    UPDATE: Done the hard part, on to the easy part!
    Last edited: Aug 2, 2016
  3. Offline


    First, thanks so much! :D

    Second, if I may add a very quick addition before you finish, perhaps a 30 minute cooldown before you could sleep again? It just occurred to me that this may be a problem!

    EDIT: Sorry for all these last second edits! But could you also make it say in white text "You feel refreshed!" When you wake up?
  4. @Dark_Serpent Done, about to test!
    I can change the colour, but the cooldown I can't do.
    Also, it will be 1.8, however, because I don't use anything that's 1.8 only it should work fine on 1.7 (I use 1.8 plugins on 1.7 servers)

    EDIT: I can't add the 20 second sleeping in the bed, but have been able to add the 30 minute cooldown! Download:
    Last edited: Aug 2, 2016
  5. Offline


    Don't feel bad about the 20s in bed part. I am not sure if a plugin can actually do that, I think only a Forge mod can.
  6. Offline


    I am new to writing plugins, but is there a way for you to take 20 seconds after the event of an enter bed event, kicking the player out and adding the buffs? Just make a hashmap (public HashMap<Player, Long> sleepTime = new HashMap<Player, Long>() and when someone sleeps do sleepTime.put(player, System.currentTimeMillis());, and then in a method being called every second or so, if it has been 20 seconds, kick the player out and add the buffs. I don't know how to kick the player out, but there has to be a way.
  7. Offline


    I think you misread the request, it is to keep the player in the bed, not to take them out.
  8. Offline

    I Al Istannen

    @Firestar311 @espo1234 @Jezzerdo4 @Dark_Serpent
    From the client code it seems like you can forbid waking up by cancelling the animation packet with an ID of 2.
    If that works it will cause a server-client desync though.

    If you want to make it nicer, you would need this method to return false:
    "this.everyoneDeeplySleeping()" In WorldServer
    It checks for the sleepTimer of the players.

    So, a way to try to achieve this would be getting the NMS player entity and setting the sleepTimer to 90 or so every tick. The field is called "sleepTicks" and is a public int, so no need for reflection!

    Can somebody try it? I don't have time.
    Last edited: Aug 3, 2016
  9. Offline


    I tried it out, I set it to 99 instead of 90 though, I mainly did that to make sure you would wake up right after the 20 seconds were over, I created a HashMap<UUID, Integer> to store the amount of ticks a player had been in bed for, as soon as that reached 400 ticks, I cancelled the task and added the buffs. It seems to work all fine, and I didn't run into any problems.

    I won't release it before I get permission from @Jezzerdo4 though.

    Anyways, great idea!

    Permission please?
    I Al Istannen likes this.
  10. Offline

    I Al Istannen

    That sounds nice! Glad I could contribute something to this :p

    Did you use reflection to set the sleepTicks? If not, it will probably break with every version, due to the imports.

    And I agree, the idea is very creative @Dark_Serpent :)
  11. Offline


    Sorry! I have been completely busy over the last few days and I didn't want to comment before I adequately and completely tested out @Jezzerdo4's plugin

    Thanks so much for helping me out with this request Jezzerdo! I'll get to testing it out ASAP, and @frej4189, I'm very interested to see what you can do :)

    I honestly assumed that the ability to transfix players in a bed would be impossible, but I left it in the request just in case there was some work around that was plausible. And, as @I Al Istannen has once again proven, they are a genius :p

    EDIT: I came across some problems with @Jezzerdo4's plugin.
    • Players cannot sleep during the day, although there's no major problem with this, I would like players to be able to. If that's impossible though, it's alright.
    • Buffs are not given when the player wakes up.
    • There is no 30 minute cooldown.
    Last edited: Aug 4, 2016
  12. @Dark_Serpent There's a 30-minute cooldown for me, I don't think you can sleep in the day and what do you mean wakes up? The player never gets in the bed.
  13. Offline


    Perhaps I have some conflicting plugin on the server that's causing an error, but it appears beds work just as usual. I'll try to hunt down what's causing it, and if I get an error message, I'll edit this post. The plugin does load properly though, and gets put in the plugins list.
  14. Offline

    I Al Istannen

    You gave @frej4819 permission, so I hope I have them too.


    I had some time and made a plugin for this.
    Sleeping during the day would be way too annoying to do, and would probably need to modify the craftbukkit code in order to work somewhat as expected.
    Maybe not, I am open for suggestions!

    I hope it does what you wanted.
    There is only a config, no commands and no permissions and I would encourage you to look into the config :p

    With another plugin you can quite easily hook into this plugin and register a custom "SleepingPlayerFactory" in the "SleepingPlayerManager".
    The "SleepingPlayerFactory" is a "Function<Player, SleepingPlayerBase>".
    It takes a Player and returns a subclass of "SleepingPlayerBase".
    The subclass only needs to handle the 4 events, going to sleep, exiting the bed, becoming invalid and waking up. You can do your own stuff in there then.
    The default one sends messages and executes commands / applies potion effects.
    So, this can be pretty easily extended to do more complex things at waking up/falling asleep/exiting the bed.

    Plase note, that sleeping in creative mode WILL change the night to day. This is because I just hit the player, so that he will wake up. The other ways I found required nms stuff, so I stayed away from them, for compatibilities sake.

    The plugin was made for and tested on 1.8.8, but it should work on lower versions. If it doesn't, just say so and I will see what I can do :)

    Link: Dropbox. Source is packaged within.
  15. Offline


    Sorry! I've been very busy, but I had time to test out both of your plugins.
    @Jezzerdo4 and @I Al Istannen, both of your plugins work great! It appears there are some plugin incompatibility issues I had to sort out. Since I'm really busy though, it'll take me a while to figure out what.

    I'll mark this as Filled, again, thank you so much! :D
    I Al Istannen likes this.
  16. Offline


    If I could find a replacement for getTargetBlock, it would be possible to do the sleeping during the day pretty simple, just check if player right clicks a bed, set his player time to 13000, and as soon as he exits the bed, set his player time back to normal
  17. Offline

    I Al Istannen

    getTargetBlock((HashSet<Material>) null, ...)
    is what you are looking for :p

    But then you would need to do the sleeping animation and so on manually.
  18. Offline


    getTargetBlock((HashSet<Material>) null, ...) is deprecated though.

    And I completely forgot about the sleeping animation thing, but wouldn't PacketPlayOutBed do the job?
  19. Offline

    I Al Istannen

    No, it is not deprecated. Look here.
    If you don't cast the "null" to a Set<Material>, it may use the other method, which is, in fact, deprecated (and has a HashSet parameter).

    Just now realized I am dumb. Cast it to a Set, not a HashSet.

    Yea, the packet would do for the others. Don't know if you will see it too. And it adds another dependency on NMS code.
    frej4189 likes this.
  20. Offline


    @I Al Istannen
    I didn't think about that, I'll test it on sunday or monday, I don't have time today or tommorow, hopefully it will work!

    And also, since the players time was set to night, I'm pretty sure they will see it, not sure though .
    I Al Istannen likes this.
  21. Offline


    @I Al Istannen
    It seems that PacketPlayOutBed causes the server to crash, can't seem to solve the issue, setting the players sleeping property to true didn't help either. I don't really know if I wanna continue working or if I will just give up.
  22. Offline

    I Al Istannen

    This seems like it should work. I can't test it right now though :/

    Understand that feeling too well xD Won't blame you if you don't, and most certain neither will Dark_Serpant ;)
  23. Offline


    @I Al Istannen Apparently, it's undefined, the only thing that's actually there is
    without any parameters, but that causes the crash, eclipse even wants to autocomplete it with the arguments EntityHuman and BlockPosition, but then when I type in an EntityHuman and a BlockPosition, it says that it's undefined, it seems fairly odd, and I can't seem to fix it.
  24. Offline

    I Al Istannen

    This works in the IDE for me:
    Location pLoc = e.getPlayer().getLocation();
    PacketPlayOutBed bedPacket = new PacketPlayOutBed(((CraftPlayer) e.getPlayer()).getHandle(),
        new BlockPosition(pLoc.getBlockX(), pLoc.getBlockY(), pLoc.getBlockZ()));
    Didn't try it in game, but the constructor is certainly defined.

    import net.minecraft.server.v1_8_R3.BlockPosition;
    import net.minecraft.server.v1_8_R3.PacketPlayOutBed;
    import org.bukkit.Location;
    import org.bukkit.craftbukkit.v1_8_R3.entity.CraftPlayer;
  25. Offline


    @I Al Istannen
    Since I do not know anything about reflection, this version has none, if you could add that it would be awesome!
    I am guessing you have a decompiler, if not, please tell me and I'll upload the source on github.

    I changed the packet code you sent me so that you would always end up at the top of the bed, not at the foot of it or anywhere else.
    I no longer use BedEnterEvent and BedLeaveEvent, but I guess you have no problem with that.

    I added support for sleeping during the day!
    Sadly, until someone adds reflection, it's only compatible with 1.8 (Sorry about that).

    I guess that's enough to say, here's the link:

    EDIT: Apparently it wasn't enough to say, I forgot to mention that once you enter a bed, you can't leave it before the 20 seconds has passed.
    I Al Istannen likes this.
  26. @frej4189
    Reflection is actually rather easy to implement, once you get the hang of it. It's also a very good way to learn about how the Java class structure works. If you have any questions about implementing Reflection, I'm sure the people around here can answer them.
    I Al Istannen likes this.
  27. Offline

    I Al Istannen

    Reflection is really rather easy. It is nearly what you normally do :p
    I think I will send you off to the Reflection Trail from Oracle first, but if you have any questions, I will answer them in a more concrete way (to the best of my knowledge :p)
  28. Offline


    @I Al Istannen
    I didn't exactly optimize my code for reflecting, I'll rewrite the entire thing, so it might not be done before friday. Depending on how quick I am.
    I'm gonna add a config simular to the one you created in your version aswell, which might extend the time even further
Thread Status:
Not open for further replies.

Share This Page