Solved I Need Help With Events And Velocity/Vector.

Discussion in 'Plugin Development' started by FrozenBattlesTeam, Jul 13, 2014.

Thread Status:
Not open for further replies.
  1. Hey guys, I am coding a plugin where it test for a player right clicking with a firework. It then launches them in the air depending on the way they are facing, but I am having a few problems and need help.

    Main Problems:
    • I don't know how to test for a right click.
    • I need a way to test the way the player is facing.
    • I need a way to test for a firework in the player's hand.
    • I need to know how to send a player in to the air without getting kicked for "Flying."
    • I need to know how to and if I need to set up a plugin.yml.
    Main Questions:
    • If you right click the firework won't it go flying. Is this a problem?
    • Is there any useful forms to show me this? I have looked a some but threads are a little confusing to me.
    My Code:
    Code:
    package com.frozenbattles.plugin;
     
     
    import org.bukkit.Material;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.EventPriority;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.util.Vector;
     
     
     
    public class kangajump implements Listener  {
     
        @EventHandler(priority=EventPriority.HIGH)
        public void onPlayerUse (PlayerInteractEvent event) {
            Player player = event.getPlayer();
            ItemStack item = player.getItemInHand();
            if (item.getType() == Material.FIREWORK){
                String direction = getDirection(player);
                if(direction == "North"){
                player.setVelocity(new Vector(0,10,-5));
                }
                else if(direction == "South"){
                    player.setVelocity(new Vector(0,10,5));
                }
                else if(direction == "East"){
                    player.setVelocity(new Vector(5,10,0));
                }
                else if(direction == "West"){
                    player.setVelocity(new Vector(-5,10,0));
                }
                else if(direction == "Northeast"){
                    player.setVelocity(new Vector(5,10,-5));
                }
                else if(direction == "Northwest"){
                    player.setVelocity(new Vector(-5,10,-5));
                }
                else if(direction == "Southeast"){
                    player.setVelocity(new Vector(5,10,5));
                }
                else if(direction == "Southwest"){
                    player.setVelocity(new Vector(-5,10,5));
                }
            }
         
        }
        private String getDirection(Player player) {
            int degrees = (Math.round(player.getLocation().getYaw()) + 270) % 360;
            if (degrees <= 22) return "North";
            if (degrees <= 67) return "Northeast";
            if (degrees <= 112) return "East";
            if (degrees <= 157) return "Southeast";
            if (degrees <= 202) return "South";
            if (degrees <= 247) return "Southwest";
            if (degrees <= 292) return "West";
            if (degrees <= 337) return "Northwest";
            if (degrees <= 359) return "North";
            return null;
        }
     
    }
    
    I hope you like my organization of this thread and hope it helps others. If you think you can help please say so. I am grateful for any advice. Thank you for your time.
     
  2. Offline

    TheMcScavenger

    • I don't know how to test for a right click.
    Listen to the PlayerInteractEvent
    • I need a way to test the way the player is facing.
    player.getLocation() gives x, y, z, yaw, pitch. Combination of yaw & pitch is where the player is looking (horizontal and vertical axis).
    • I need a way to test for a firework in the player's hand.
    Code:java
    1. if(player.getItemInHand().getType().equals(Material.FIREWORK)){
    2. // your code
    3. }

    • I need to know how to send a player in to the air without getting kicked for "Flying."
    Code:java
    1. player.setAllowFlight(true);

    • I need to know how to and if I need to set up a plugin.yml.
    Every plugin needs a plugin.yml to load properly. Simply add it to the java project.
    http://wiki.bukkit.org/Plugin_YAML
    • If you right click the firework won't it go flying. Is this a problem?
    If you don't want it to, cancel the PlayerInteractEvent.
    • Is there any useful forms to show me this? I have looked a some but threads are a little confusing to me.
    You have a lot of questions, causing me to believe you did zero to no research on the plugin you're trying to make. Look up tutorials for each of the questions you have, there's plenty of answers.
     
  3. Offline

    xTigerRebornx

  4. Code:java
    1. package com.frozenbattles.plugin;
    2.  
    3.  
    4. import org.bukkit.Bukkit;
    5. import org.bukkit.Material;
    6. import org.bukkit.entity.Player;
    7. import org.bukkit.event.EventHandler;
    8. import org.bukkit.event.EventPriority;
    9. import org.bukkit.event.Listener;
    10. import org.bukkit.event.block.Action;
    11. import org.bukkit.event.player.PlayerInteractEvent;
    12. import org.bukkit.inventory.ItemStack;
    13. import org.bukkit.plugin.Plugin;
    14.  
    15.  
    16.  
    17. public class kangajump implements Listener {
    18.  
    19. public void onEnable(){
    20. Bukkit.getServer().getPluginManager().registerEvents(this,(Plugin) this);
    21. }
    22. @EventHandler(priority=EventPriority.HIGHEST)
    23. public void onPlayerUse (PlayerInteractEvent event) {
    24. Player player = event.getPlayer();
    25. Action action = event.getAction();
    26. double rotation = (player.getLocation().getYaw() - 90) % 360;
    27. ItemStack item = player.getItemInHand();
    28. if(action == Action.RIGHT_CLICK_AIR){
    29. if (item.getType() == Material.FIREWORK){
    30. if (rotation < 0) {
    31. rotation += 360.0;
    32. }
    33. if (0 <= rotation && rotation < 22.5 || 337.5 <= rotation && rotation < 360.0) {
    34. //north
    35. } else if (22.5 <= rotation && rotation < 67.5) {
    36. //north east
    37. } else if (67.5 <= rotation && rotation < 112.5) {
    38. //east
    39. } else if (112.5 <= rotation && rotation < 157.5) {
    40. //south east
    41. } else if (157.5 <= rotation && rotation < 202.5) {
    42. //south
    43. } else if (202.5 <= rotation && rotation < 247.5) {
    44. //south west
    45. } else if (247.5 <= rotation && rotation < 292.5) {
    46. //west
    47. } else if (292.5 <= rotation && rotation < 337.5) {
    48. //north west
    49. }
    50. }
    51. }
    52. else if(action == Action.RIGHT_CLICK_BLOCK){
    53. if(item.getType() == Material.FIREWORK){
    54. event.setCancelled(true);
    55. }
    56. }
    57.  
    58. }
    59. }
    60.  
    61.  

    I think i fixed the event.cancel thing and direction but i looked at other forms and dont understand the vector velocity thing what i really need is just that now
    xTigerRebornx TheMcScavenger

    Code:java
    1. package com.frozenbattles.plugin;
    2.  
    3.  
    4. import org.bukkit.Bukkit;
    5. import org.bukkit.Material;
    6. import org.bukkit.entity.Player;
    7. import org.bukkit.event.EventHandler;
    8. import org.bukkit.event.EventPriority;
    9. import org.bukkit.event.Listener;
    10. import org.bukkit.event.block.Action;
    11. import org.bukkit.event.entity.EntityDamageEvent;
    12. import org.bukkit.event.entity.EntityDamageEvent.DamageCause;
    13. import org.bukkit.event.player.PlayerInteractEvent;
    14. import org.bukkit.inventory.ItemStack;
    15. import org.bukkit.plugin.Plugin;
    16. import org.bukkit.plugin.java.JavaPlugin;
    17.  
    18.  
    19.  
    20. public class kangajump extends JavaPlugin implements Listener {
    21. public Boolean fallDamage = true;
    22. Player player;
    23. public void onEnable(){
    24. Bukkit.getServer().getPluginManager().registerEvents(this,(Plugin) this);
    25. }
    26. @EventHandler(priority=EventPriority.HIGHEST)
    27. public void onPlayerUse (PlayerInteractEvent event) {
    28. player = event.getPlayer();
    29. Action action = event.getAction();
    30. double rotation = (player.getLocation().getYaw() - 90) % 360;
    31. ItemStack item = player.getItemInHand();
    32. int number = player.getItemInHand().getAmount() - 1;
    33. if(action == Action.RIGHT_CLICK_AIR || action == Action.RIGHT_CLICK_BLOCK){
    34. if (item.getType() == Material.FIREWORK){
    35. event.setCancelled(true);
    36. fallDamage=false;
    37. player.setAllowFlight(true);
    38. item.setAmount(number);
    39. player.setItemInHand(item);
    40. if (rotation < 0) {
    41. rotation += 360.0;
    42. }
    43. if (0 <= rotation && rotation < 22.5 || 337.5 <= rotation && rotation < 360.0) {
    44. //north
    45. player.setVelocity(player.getVelocity().setY(1.8));
    46. } else if (22.5 <= rotation && rotation < 67.5) {
    47. //north east
    48. player.setVelocity(player.getVelocity().setY(1.8));
    49. } else if (67.5 <= rotation && rotation < 112.5) {
    50. //east
    51. player.setVelocity(player.getVelocity().setY(1.8));
    52. } else if (112.5 <= rotation && rotation < 157.5) {
    53. //south east
    54. player.setVelocity(player.getVelocity().setY(1.8));
    55. } else if (157.5 <= rotation && rotation < 202.5) {
    56. //south
    57. player.setVelocity(player.getVelocity().setY(1.8));
    58. } else if (202.5 <= rotation && rotation < 247.5) {
    59. //south west
    60. player.setVelocity(player.getVelocity().setY(1.8));
    61. } else if (247.5 <= rotation && rotation < 292.5) {
    62. //west
    63. player.setVelocity(player.getVelocity().setY(1.8));
    64. } else if (292.5 <= rotation && rotation < 337.5) {
    65. //north west
    66. player.setVelocity(player.getVelocity().setY(1.8));
    67. }
    68. }
    69. }
    70. }
    71.  
    72.  
    73. @EventHandler(priority=EventPriority.HIGHEST)
    74. public void onPlayerDamage(final EntityDamageEvent e){
    75. if(e.getCause() == DamageCause.FALL && fallDamage == false){
    76. e.setCancelled(true);
    77. fallDamage = true;
    78. player.setAllowFlight(false);
    79. }
    80. }
    81. }
    82.  

    final code going to implement the directions

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

    JasonDL13

    I don't know why you're using roatations and stuff, just use p.getLocation().getDirection() and modify that velocity.
     
Thread Status:
Not open for further replies.

Share This Page