Help with directions!

Discussion in 'Plugin Development' started by OlofGrandmaster, Nov 8, 2013.

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

    OlofGrandmaster

    Hi!
    I've tried to make a farting plugin and it works but one thing is "wrong". :p
    Here's the code!
    Code:
    public void onPlayerToggleSneakevent(PlayerToggleSneakEvent event) {
        Player player = event.getPlayer();
        World world = player.getWorld();
        world.playEffect(player.getLocation(), Effect.SMOKE, 2);
        }
    And you see at the syntax: "world.playEffect(player.getLocation(), Effect.SMOKE, 2);"
    I make so the effect is going to SOUTH. But i want to so it makes the Effect is going the opposite side
    where the player is facing! Please help me to fix that!
    I'm still a NOOB :rolleyes:
     
  2. It's not world.playerEffect, it's player.playEffect
     
  3. Offline

    sd5

    OlofGrandmaster I would get the "player.getLocation().getYaw()" and divide that in multiple sections, 90 degrees each (or maybe with 45 degrees each) and then associate every section with the corresponding direction value.
     
  4. Offline

    OlofGrandmaster

    Can you show it in
    Code:
    Code
    sorry i'm NOOBY D:
     
  5. Offline

    fromgate

  6. Offline

    sd5

    OlofGrandmaster Okay before going to sleep I wrote some code:
    Code:java
    1. private int parseDirection(double yaw, boolean reverse) {
    2. double absYaw = (yaw < 0) ? (360 + yaw) : yaw;
    3. int section = (int) (absYaw - ((absYaw + 22.5) % 45.0) + 22.5);
    4. if(reverse) {
    5. section = ((section + 180) > 360) ? (section - 180) : (section + 180);
    6. }
    7.  
    8. if(section == 0 || section == 360) return 1; //SOUTH
    9. if(section == 45) return 2; //SOUTHWEST
    10. if(section == 90) return 5; //WEST
    11. if(section == 135) return 8; //NORTHWEST
    12. if(section == 180) return 7; //NORTH
    13. if(section == 225) return 6; //NORTHEAST
    14. if(section == 270) return 3; //EAST
    15. if(section == 315) return 0; //SOUTHEAST;
    16. return -1;
    17. }

    This converts the players yaw into the direction value.
    I added the possibility to reverse the resulting value because you want the smoke to go into the other direction.
    You could end up with this:
    Code:java
    1. @EventHandler
    2. public void onPlayerToggleSneakevent(PlayerToggleSneakEvent event) {
    3. Player player = event.getPlayer();
    4. World world = player.getWorld();
    5. int direction = parseDirection(player.getLocation().getYaw(), true);
    6. world.playEffect(player.getLocation(), Effect.SMOKE, direction);
    7. }

    All of the code is unchecked but it should work anyways :)
     
    fromgate likes this.
  7. Offline

    OlofGrandmaster

    THANKS!
    It works but I just needed to change reverse to false!
     
Thread Status:
Not open for further replies.

Share This Page