Solved Problem with getting player's within line of sight

Discussion in 'Plugin Development' started by ZeusAllMighty11, Mar 10, 2013.

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

    ZeusAllMighty11

    It works until I add the permission node ... That's all I can say

    Code:java
    1.  
    2.  
    3. package me.zeus.CrosshairCommand;
    4.  
    5.  
    6. import java.io.File;
    7. import java.util.ArrayList;
    8. import java.util.List;
    9.  
    10. import org.bukkit.ChatColor;
    11. import org.bukkit.Location;
    12. import org.bukkit.block.Block;
    13. import org.bukkit.entity.Entity;
    14. import org.bukkit.entity.Player;
    15. import org.bukkit.event.EventHandler;
    16. import org.bukkit.event.Listener;
    17. import org.bukkit.event.player.PlayerCommandPreprocessEvent;
    18. import org.bukkit.plugin.java.JavaPlugin;
    19.  
    20.  
    21.  
    22. public class Main extends JavaPlugin implements Listener {
    23.  
    24. List<Entity>nearby;
    25.  
    26. @Override
    27. public void onEnable()
    28. {
    29.  
    30. final File config = new File(getDataFolder() + File.separator + "config.yml");
    31. if (!config.exists())
    32. {
    33. final List<String> cmdlist = new ArrayList<String>();
    34. getConfig().addDefault("Commands", cmdlist);
    35. getConfig().options().copyDefaults(true);
    36. saveConfig();
    37. }
    38.  
    39. getServer().getPluginManager().registerEvents(this, this);
    40. }
    41.  
    42. @EventHandler
    43. public void onUseCommand(final PlayerCommandPreprocessEvent e)
    44. {
    45. final Player player = e.getPlayer();
    46. final String command = e.getMessage();
    47.  
    48. for (final String cmds : getConfig().getStringList("Commands"))
    49. {
    50. if (command.startsWith(cmds))
    51. {
    52. if(!hasPlayerInSight(player)){
    53. if(!player.hasPermission("CrosshairCommand.Bypass")){
    54. player.sendMessage(ChatColor.RED + "You must be looking at the player to use this command!");
    55. e.setCancelled(true);
    56. } else {
    57. player.sendMessage(ChatColor.GREEN + "Bypassed regular standards for commands!");
    58. }
    59. }
    60. }
    61. }
    62. }
    63. /* Credits to Firefly */
    64. public boolean hasPlayerInSight(Player p){
    65. Block targetBlock = p.getTargetBlock(null, 50);
    66. Location blockLoc = targetBlock.getLocation();
    67. double bx = blockLoc.getX();
    68. double by = blockLoc.getY();
    69. double bz = blockLoc.getZ();
    70. nearby = p.getNearbyEntities(100, 100, 100);
    71.  
    72. for (Entity entity : nearby) {
    73. if(entity instanceof Player){
    74. Location loc = entity.getLocation();
    75. double ex = loc.getX();
    76. double ey = loc.getY();
    77. double ez = loc.getZ();
    78.  
    79. if ((bx-1.5 <= ex && ex <= bx+2) && (bz-1.5 <= ez && ez <= bz+2) && (by-1 <= ey && ey <= by+2.5)) {
    80. return true;
    81. }
    82. }
    83. }
    84. return false;
    85. }
    86.  
    87.  
    88. }
    89.  
    90. [/CODE]
    91.  
    92. Solved, for anyone that wants the code I use that works:
    93.  
    94. [CODE]
    95.  
    96. package me.zeus.CrosshairCommand;
    97.  
    98.  
    99. import java.io.File;
    100. import java.util.ArrayList;
    101. import java.util.List;
    102.  
    103. import org.bukkit.ChatColor;
    104. import org.bukkit.Location;
    105. import org.bukkit.block.Block;
    106. import org.bukkit.entity.Entity;
    107. import org.bukkit.entity.Player;
    108. import org.bukkit.event.EventHandler;
    109. import org.bukkit.event.Listener;
    110. import org.bukkit.event.player.PlayerCommandPreprocessEvent;
    111. import org.bukkit.plugin.java.JavaPlugin;
    112.  
    113.  
    114.  
    115. public class Main extends JavaPlugin implements Listener {
    116.  
    117. List<Entity>nearby;
    118.  
    119. @Override
    120. public void onEnable()
    121. {
    122.  
    123. final File config = new File(getDataFolder() + File.separator + "config.yml");
    124. if (!config.exists())
    125. {
    126. final List<String> cmdlist = new ArrayList<String>();
    127. getConfig().addDefault("Commands", cmdlist);
    128. getConfig().options().copyDefaults(true);
    129. saveConfig();
    130. }
    131.  
    132. getServer().getPluginManager().registerEvents(this, this);
    133. }
    134.  
    135. @EventHandler
    136. public void onUseCommand(final PlayerCommandPreprocessEvent e)
    137. {
    138. final Player player = e.getPlayer();
    139. final String command = e.getMessage();
    140.  
    141. for (final String cmds : getConfig().getStringList("Commands"))
    142. {
    143. if (command.startsWith(cmds))
    144. {
    145. if(!hasPlayerInSight(player)){
    146. if(!player.hasPermission("CrosshairCommand.Override")){
    147. e.setCancelled(true);
    148. player.sendMessage(ChatColor.RED + "You must be looking at the player to use this command!");
    149. } else {
    150. player.sendMessage(ChatColor.GREEN + "Bypassed!");
    151. }
    152.  
    153. } else {
    154. e.setCancelled(false);
    155. }
    156. }
    157. }
    158. }
    159.  
    160.  
    161. public boolean hasPlayerInSight(Player p){
    162. Block targetBlock = p.getTargetBlock(null, 50);
    163. Location blockLoc = targetBlock.getLocation();
    164. double bx = blockLoc.getX();
    165. double by = blockLoc.getY();
    166. double bz = blockLoc.getZ();
    167. nearby = p.getNearbyEntities(100, 100, 100);
    168.  
    169. for (Entity entity : nearby) {
    170. if(entity instanceof Player){
    171. Location loc = entity.getLocation();
    172. double ex = loc.getX();
    173. double ey = loc.getY();
    174. double ez = loc.getZ();
    175.  
    176. if ((bx-1.5 <= ex && ex <= bx+2) && (bz-1.5 <= ez && ez <= bz+2) && (by-1 <= ey && ey <= by+2.5)) {
    177. return true;
    178. }
    179. }
    180. }
    181. return false;
    182. }
    183.  
    184.  
    185. }
    186.  
    187.  
    188. [/CODE]
    189.  
    190. EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 31, 2016
  2. Offline

    i4evercode

    So would this work if you wanted to get all of the players in your line of sight and edit any and all of the player's health?

    ZeusAllMighty11 If you think you could help with that, it would be real nice :)

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 31, 2016
  3. Offline

    ZeusAllMighty11

    i4evercode

    1) No
    2) You'd need some more math, creating a field of view of entities dynamically
     
  4. Offline

    i4evercode

Thread Status:
Not open for further replies.

Share This Page