Prevent players from looking up/down

Discussion in 'Plugin Development' started by KingFaris11, Mar 30, 2015.

Thread Status:
Not open for further replies.
  1. This is probably the most simple thing you could do, but I just can't figure it at this current moment in time.

    I'm trying to prevent players from looking up or down, but they can still move and look left/right.
    I've tried:
    Code:
    @EventHandler(priority = EventPriority.LOWEST)
    public void onPlayerMove(PlayerMoveEvent event) {
        Location to = event.getTo();
        to.setPitch(0F);
        event.setTo(to);
    }
    
    This doesn't work unfortunately, I'm not sure why.
     
    Last edited: Mar 31, 2015
  2. Offline

    teej107

  3. Yes 100% sure, even tested using System.out.println(). This is part of a huge player listener class and all events also work :p
     
  4. Offline

    caseif

    I don't see anything inherently wrong with it. Maybe try changing the priority? I'll test your snippet when I get home.
     
  5. Offline

    sablednah

    Does onPlayerMove trigger on pitch/yaw changes?
     
  6. Offline

    teej107

  7. Offline

    sablednah

    @teej107 I'm even more glad i check for XYZ changes and discard if not "different enough" now :)
     
    Last edited: Mar 30, 2015
  8. Offline

    BrickBoy55

    Maybe because you spelled event wrong in your constructor?
     
  9. Offline

    teej107

    You don't know that. He hasn't shown his constructor. If you are talking about the variable name in the parameters of his method, variable names don't make a difference anyway.
     
  10. But they can use F5 to look down... You cant prevent it
     
  11. Offline

    xMrPoi

    @teej107 he called a method from his event though used a different spelling.

    Try changing the variable name of your event from "evnet" to "event"
     
    teej107 likes this.
  12. Offline

    teej107

    Ah. Well there should've been syntax issues. Unless it's a bad copy/paste.
     
  13. Offline

    BrickBoy55

    Yeah I meant method, sorry.
     
  14. ^ It was that xD

    But yeah, it's still not working... :/ I don't get why... the priority's LOWEST therefore should run almost first...
     
  15. Offline

    teej107

    @KingFaris11 The priority of an event handler doesn't make a difference unless there are other plugins and you specifically need to have yours run in a certain order. If you want your plugin to have the final say in an event, use the HIGHEST priority. Do not use MONITOR as that should not modify the outcome of the event. Do you have any other plugins that you are using while you are testing yours?

    What do you mean? Does the PlayerMoveEvent not fire when a player moves his head in F5 mode? I would assume it still does.
     
  16. Oh right, I just set it to LOWEST just incase things run first, I want this to run pretty much first as the method cancels PlayerMoveEvent in some cases, I'll post the full code for it below, but no I don't have any plugins that should affect it, i.e.

    [​IMG]

    Code:
        @EventHandler(priority = EventPriority.LOWEST)
        public void onPlayerMove(PlayerMoveEvent event) {
            try {
                if (this.loadingCaches.containsKey(event.getPlayer().getUniqueId())) {
                    event.setTo(new Location(event.getFrom().getWorld(), event.getFrom().getX(), (int) event.getFrom().getY(), event.getFrom().getZ(), event.getFrom().getYaw(),  0F));
                    return;
                }
                final Location moveTo = event.getTo();
                moveTo.setPitch(0F);
                event.setTo(moveTo);
    
                final Location moveFrom = event.getFrom();
                int fromX = (int) moveFrom.getX(), fromY = (int) moveFrom.getY(), fromZ = (int) moveFrom.getZ();
                int toX = (int) moveTo.getX(), toY = (int) moveTo.getY(), toZ = (int) moveTo.getZ();
                boolean boundaryPlayer = false;
                if (fromX != toX || fromY != toY || fromZ != toZ) {
                    if (toX > Main.maxBoundary || toZ > Main.maxBoundary) {
                        event.getPlayer().sendMessage(ChatColor.RED + "You cannot go past the boundaries!");
                        if (RealmUtilities.inRealm(event.getPlayer()))
                            event.setTo(RealmUtilities.getRealm(event.getPlayer()).getSpawn());
                        else event.setCancelled(true);
                        boundaryPlayer = true;
                    } else if (toX == Main.maxBoundary || toZ == Main.maxBoundary) {
                        event.getPlayer().sendMessage(ChatColor.RED + "You are on the boundary line.");
                        boundaryPlayer = true;
                    } else if (toX < -Main.maxBoundary || toZ < -Main.maxBoundary) {
                        event.getPlayer().sendMessage(ChatColor.RED + "You cannot go past the boundaries!");
                        if (RealmUtilities.inRealm(event.getPlayer()))
                            event.setTo(RealmUtilities.getRealm(event.getPlayer()).getSpawn());
                        else event.setCancelled(true);
                        boundaryPlayer = true;
                    } else if (toX == -Main.maxBoundary || toZ == -Main.maxBoundary) {
                        event.getPlayer().sendMessage(ChatColor.RED + "You are on the boundary line.");
                        boundaryPlayer = true;
                    }
                }
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    
    Note: All getSpawn() methods have a pitch of 0F.

    Edit: Just realised, if I want this to make sure the pitch is 0F, I should make priority HIGHEST so it runs last (over all other plugins). I'll split this method into two PlayerMoveEvents, one LOWEST and one HIGHEST and post results.

    I ran this:
    Code:
        @EventHandler(priority = EventPriority.HIGHEST)
        public void onPlayerMove2(PlayerMoveEvent event) {
            try {
                if (this.loadingCaches.containsKey(event.getPlayer().getUniqueId())) {
                    event.setTo(new Location(event.getFrom().getWorld(), event.getFrom().getX(), (int) event.getFrom().getY(), event.getFrom().getZ(), event.getFrom().getYaw(), 0F));
                    return;
                }
                System.out.println("Running...");
                Location to = event.getTo();
                to.setPitch(0F);
                event.setTo(to);
                System.out.println("New pitch: " + event.getTo().getPitch());
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    
    It says "New pitch: 0.0" implying the setTo() did run...

    Edit: I even tried to.setX(2000D) and it did nothing... weird... maybe my plugin's cancelling it. I'll check.

    Edit 2: No, it isn't being cancelled... It just means that event.getTo() is not being read correctly... :/ When using event.getPlayer().teleport() it works, but not event.setTo()...

    Edit 3: I made a separate plugin with:
    Code:
    public class Main extends JavaPlugin implements Listener {
    
        public void onEnable() {
            this.getServer().getPluginManager().registerEvents(this, this);
        }
    
        @EventHandler
        public void onPlayerMove(PlayerMoveEvent event) {
            Location to = event.getTo();
            to.setPitch(0F);
            event.setTo(to);
        }
    
    }
    
    I've disabled all other plugins except this and PlugMan, still doesn't work.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 12, 2016
  17. Ah right, well thanks but my purpose isn't so they can't see down, just not look down. The reason is because I'm re-creating a game in Minecraft, and that game is a 2D game based off 2D logic therefore I want to make the game here sort of 2D, i.e. the player can't look up/down and only shoot forwards.
     
  18. Offline

    MrJossy

    You should do:

    Code:
    @EventHandler(priority = EventPriority.LOWEST)
    public void onPlayerMove(PlayerMoveEvent event) {
        event.setTo(event.getFrom());
    }
    
     
  19. @MrJossy That will cancel any movement :p
     
    KingFaris11 likes this.
  20. I'm guessing this is just a bug, I already reported it on Jira.

    event.getTo() was never being read.
     
  21. Offline

    MrJossy

    Isn't that what he wants? :D
     
  22. No, this is definitely not what he wants! Did you even read the title?
    Hint (open)
    Prevent players from looking up/down
     
  23. Offline

    JUSTCAMH

    Ok, so I'm not certain how this would work, but possibly just get if getTo() has a pitch higher/lower than 0 and if it does, cancel it.
     
  24. @JUSTCAMH or just set the pitch to 0 when the player moves
     
  25. Offline

    CheesyFreezy

    Just set the player's location
    Code:
    @EventHandler
    public void onPlayerMove(PlayerMoveEvent event) {
       Player player = event.getPlayer();
    
       float minPitch = 15f;
       float maxPitch = -15f;
    
       if(player.getLocation().getPitch() >= minPitch && player.getLocation().getPitch() <= maxPitch) {
          Location loc = player.getLocation();
      
          loc.setPitch(0f);
          player.teleport(loc);
       }
    }
     
  26. Offline

    WesJD

    @CheesyFreezy And what will happen when a player sprints/moves? They will get teleported back some, which will be super annoying.
     
  27. Offline

    CheesyFreezy

    @WesJD, no it won't because i'm checking first if the pitch is above or below a certain integer, if it's not between these variables the pitch will be set to 0, and you won't be teleported back.
     
  28. That seems good, I'll test that. I wanted to avoid using teleport() as if another plugin cancels PlayerMoveEvent, this'll still teleport them. However, I guess I can set the priority to HIGHEST.
     
  29. Offline

    CheesyFreezy

    You can't change the pitch of a player without teleporting them
     
Thread Status:
Not open for further replies.

Share This Page