Freezing Players

Discussion in 'Plugin Development' started by david123718, Aug 25, 2014.

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

    david123718

    How do I freeze players? So they cant move their mouse and I can toggle to freeze a player or not
     
  2. Offline

    Zettelkasten

  3. Offline

    TheMcScavenger

    • Add frozen player's UUID to a list
    • Cancel the PlayerMoveEvent for all players whose UUID are in the list
     
  4. Offline

    Zettelkasten

  5. Offline

    TheMcScavenger

    Doesn't matter, as long as he stores them somewhere. I never specifically said what he had to store them in.. I could be an ArrayList, a HashMap (although that'd be pointless unless you also want to store the username...), a HashSet, whatever. His choice.
     
  6. Offline

    Skye

    I would like to also add that setting the player's walk speed (and fly speed, if needed) to zero will make for a much smoother freeze effect, in addition to the teleporting.
     
    Zettelkasten and megasaad44 like this.
  7. Offline

    david123718

    Can I have a Example?
     
  8. Offline

    Zettelkasten

    david123718 OK, here is an example with the UUID of Notch:
    * Add Notchs UUID ("069a79f4-44e9-4726-a5be-fca90e38aaf5") to a list
    * Cancel the PlayerMoveEvent for all players whose UUID is "069a79f4-44e9-4726-a5be-fca90e38aaf5"
     
  9. Offline

    xMakerx

    You can add something like this to your code.

    Code:java
    1. private List<UUID> frozenPlayers = new ArrayList<UUID>();
    2.  
    3. public void freeze(Player player) {
    4. frozenPlayers.add(player.getUniqueId());
    5. }
    6.  
    7. public void unfreeze(Player player) {
    8. frozenPlayers.delete(player.getUniqueId());
    9. }
    10.  
    11. @EventHandler
    12. public void onPlayerMove(PlayerMoveEvent evt) {
    13. Player player = evt.getPlayer();
    14. if(frozenPlayers.contains(player.getUniqueId())) {
    15. evt.setCancelled(true);
    16. }
    17. }
     
  10. Offline

    JBoss925

    Ummmm no. Here's how you should do it.
    * Add Notchs UUID ("069a79f4-44e9-4726-a5be-fca90e38aaf5") to a list called frozenPlayers.
    * Cancel the PlayerMoveEvent for all players whose UUID is in the list frozenPlayers.

    Ninja'd.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 10, 2016
  11. Offline

    xMakerx

    Great minds think alike.
     
    hintss and JBoss925 like this.
  12. Offline

    Anrza

    Though adding Players to lists and maps is deprecated, that's mainly for long term storage. For simplicity's sake, I'd put the Player itself, rather than its UUID in the list.

    I've also had issues with cancelling player move event earlier. Instead of not letting them move, moving becomes very slow and laggy, but they can still move.
    You might wanna teleport the player to where they were when they froze and repeatedly set the player's fallDistance to what it was when he was frozen, in order to avoid the unpleasant and deadly fall damage that this would generate if the player is frozen in the middle of a fall, even if it's a half block high.
     
  13. Offline

    xMakerx


    With the upcoming release of changeable names, this is not a good idea.
     
  14. Offline

    teej107

    Just make sure you remove the player name from the list when they leave. Then it doesn't matter if it's a good idea or not.
     
  15. Offline

    xMakerx

    teej107
    Indeed. I just coded the part he needed. He can do that part himself. This forum is for getting help, not for getting a complete plugin that does what you want.
     
  16. Offline

    teej107

    Yep, unfortunately some people spoonfeed. Programming code is very versatile. There are tons of ways you can achieve the same results.
     
    hintss and xMakerx like this.
  17. Offline

    Anrza


    Storing players in hashmaps is a VERY BAD idea if you're gonna use it for long-term storage, but it's not like their names are gonna change every five minutes, meaning that having a list keeping track of very temporary stuff like this is fine.
     
  18. Offline

    xMakerx


    I'm not storing players in a hashmap, I'm storing UUIDs. I chose UUID over Player because someone might change their name because they got frozen and they could get around being frozen, the player object stores their name.
     
  19. Offline

    teej107

    Player object also stores their UUID. However it is safer to use UUIDs since you could cause a memory leak by storing players in a collection. You could always remove the player from the collection when they leave but I would prefer to let Bukkit handle it.
     
    xMakerx likes this.
  20. Offline

    Anrza


    If they get frozen, and they then bother themselves with changing their name, I'd say good game, well played, and let them be unfrozen.

    And really, are we talking about having players constantly frozen? I got the impression that this was some small thing that you might troll someone with, or maybe to keep a player from moving for two minutes.
    But if it's about having players frozen for weeks or months at the time, sure, then you should use UUIDs. Not that it will be needed, as they wont be on your server anyway.
     
    xMakerx likes this.
  21. Offline

    david123718

    Im still confused of all this can someond give me a clear example?
     
  22. Offline

    teej107

  23. Offline

    fireblast709

    You cannot completely set it to zero :p. Add a slowness potion with amplifier 7 and a jump boost potion with amplifier 128 to immobilize them (as long as they are using no mods).

    Anrza using Player objects in Collections is a) not deprecated and b) will create memory leaks when you don't clean them up onQuit.

    xMakerx their name is set during a session (a session runs from the moment they login - AsyncPlayerPreLoginEvent - till they quit - PlayerQuitEvent). Storing Player objects during that timespan has 0 negative effects.

    teej107 storing Player objects provides efficiency (of course with the tradeoff that you must remove them onQuit) :p

    david123718 Let's go over all of this again, shall we?
    • First, define a
      Code:
      private final Set<UUID> frozen
      and initialize it.
    • Now, we would like to be able to freeze people, right? We need to do a few things to pull that off:
      • Add their UUID to the Set<UUID> frozen.
      • Give them a Slowness 7 potion with duration Integer.MAX_VALUE*.
      • Give them a Jump boost 128 potion with duration Integer.MAX_VALUE*.
    • Of course there are always people that are not so fair and have some kind of mod to bypass this effect. Simply check if their UUID is in the Set<UUID> frozen, and if so:
      • Listen to the PlayerMoveEvent and set 'to' to 'from' (the methods in the event should be clear what these values are)
        • If you want to allow them to look around, only set the x, y and z of the 'to' to the x, y and z from 'from'
    • Now that they are frozen, we want to be able to unfreeze them, right?
      • Remove their UUID from Set<UUID> frozen.
      • Remove their Slowness and Jump boost potion effects
    • For persistence (if you reload/restart, it will wipe the Set<UUID> frozen and unfreeze them), we would like to save/load this.
      • For saving:
        • Create a new List<String> (you can just do this in the save method)
        • Loop over the Set<UUID> frozen (foreach loop would be great here. If you don't know what a foreach is, either google it or go over some Java tutorials)
        • For each UUID, call toString() and add that to the List<String>
        • Set the List<String> in the config, you can just do this with the set(key, value) method that the API gives you
      • For loading:
        • Get the List<String> from the config (there is a single method for this!)
        • Loop over the List (once more, foreach loops :p)
        • For each String in the List, call UUID.fromString(String) to create a UUID object, add this to the Set<UUID> frozen
    If you are able to follow the steps, you should be able to create a full fledged player freezing plugin. If you have issues with a part of the list, feel free to tahg me for pointers.
    *If I recall correctly, Integer.MAX_VALUE as duration will give you an infinite potion effect
     
  24. Offline

    Skye

    fireblast709 Have you tried it? Plugin in my sig sets speed to zero and it works fine, except the player can still jump. It might be that the speed is just so low that combined with the teleportation it looks like the player is standing still, but it works... the player is virtually frozen.
     
  25. Offline

    fireblast709

    Skye It sets it to a small number, so it most certainly works out fine, no doubts.
     
  26. Offline

    david123718

    This is what I did and it not work please tell me what im doing wrong.
    Code:
        //code            
    }}else{
                        if(cmd.getName().equalsIgnoreCase("freeze")) {
                        if(!player.hasPermission("prickledpvp.freeze")) {
                            player.sendMessage(ChatColor.RED + "You do not have access to this command.");
                           
                        }else{
                            player.sendMessage(ChatColor.AQUA + "You are now frozen.");
                            frozen.add(player);
                           
                        }}else{
                            if(cmd.getName().equalsIgnoreCase("unfreeze")) {
                                if(!player.hasPermission("prickledpvp.unfreeze")) {
                                    frozen.remove(player);
                                    player.sendMessage(ChatColor.AQUA + "You are not frozen.");
    Event: (yes I implemented/registered listener.)
    Code:
    //code   
    @EventHandler
        public void onPlayerMove(PlayerMoveEvent e) {
            Player p =e.getPlayer();
            if (frozen.contains(p.getName())) {
                e.setTo(e.getFrom());
                p.sendMessage(ChatColor.RED + "You are frozen.");
    //code
    Array List:
    Code:
    //code
        ArrayList<Player> frozen = new ArrayList<Player>();
    //code
    Bump

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 10, 2016
  27. Offline

    coasterman10

    Never cancel PlayerMoveEvent on its own, it just makes it glitchy and look unprofessional. The proper way is to set their walking speed to 0, which I have had good results with.

    EDIT: Cancelling PlayerMoveEvent also prevents hackers from moving but it is not a good idea to cancel it on its own. I would suggest using it in tandem with a movement speed of 0.
     
  28. Offline

    redytedy

    david123718 Make the arraylist contain strings, and when you add the player to the arraylist use getName().
     
  29. Offline

    coasterman10

    For storing players I would recommend using a Set, NOT a List, as Lists become slower and slower to run the contains() method on as they grow. HashSet is a good implementation for this kind of work. Also, store the UUIDs of the players.
     
    hintss likes this.
  30. Offline

    fireblast709

    coasterman10 inb4 hacks ;3. PlayerMoveEvent is a good way to ensure that the player doesn't move.
     
Thread Status:
Not open for further replies.

Share This Page