'Popping' players

Discussion in 'Plugin Development' started by sgavster, Dec 3, 2013.

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

    sgavster

    Hi!

    I was wondering how I could pop players: when you punch a player they dissapear.

    I just need an event, I know the rest of the code.

    I tried EntityDamageByEntityEvent, but pvp had to be on.

    If anyone has an idea for a better event, please let me know

    Thanks!
     
  2. Offline

    MrSnare

    You need VanishNoPacket. When you hit the player, make the player invisible to the hitter and then do the particle effects
     
  3. Offline

    sgavster

    MrSnare (facepalm)


     
  4. Offline

    mattrick

    No you don't. player.hidePlayer(); or something along that lines.

    sgavster
    You can try PlayerInteractEvent and check if a player is in line of sight.
    Also, you can make PvP on and listen for EntityDamageByEntity event. Then cancel the event when you are done.
     
  5. Offline

    sgavster

    mattrick16 How would I do that?

    And yes; I could, but this is for a public plugin (SimplyHub)
    I also tried
    PlayerInteractEntityEvent but you can only use 1 action; which is right_click :(
     
  6. Offline

    Garris0n

    Nah, lazer is just a noob who outsources his vanish code ;) not really please don't hurt me lazer
     
  7. Offline

    mattrick

    sgavster
    Have you tried changing EntityDamageByEntityEvent to monitor?
     
  8. Offline

    masons123456

    Instead, try the projectilehit listener, then check if it was an arrow , then hide the player
     
  9. Offline

    sgavster

  10. Offline

    PolarCraft

  11. Offline

    sgavster

    PolarCraft Why didn't you read my comments?
     
  12. Offline

    The_Doctor_123

    sgavster
    You need to use PlayerInteractEvent and guess the entity the player might have hit based on where they're looking. However, the better way to do it is to enable PVP, listen for EntityDamageByEntityEvent, and cancel damage.
     
  13. Offline

    sgavster

    The_Doctor_123 How would I check for players though? nearbyEntities?

    And yes, but it causes problems, because of world-guard regions and things, it was a 'complaint' on my plugin, so I'm doing my best to fix it
     
  14. Offline

    masons123456

    The projectile hit would make things a lot simpler, and wouldn't have to allow pvp etc. on your server
     
  15. Offline

    The_Doctor_123

    sgavster
    I'm letting you do the code on your own. You should get the player's line of sight and check if there's any extremely close entities by the first few blocks in sight.

    But like I said, turning on PVP would make this way more accurate and easier to code.
     
  16. Offline

    PolarCraft

    sgavster Sorry did not see you said that.
     
  17. Offline

    sgavster

    The_Doctor_123 I was thinking this:
    Code:java
    1. BlockIterator blocksToAdd = new BlockIterator(d.getLocation(), 0.0D, 3);
    2. Location blockToAdd;
    3. while(blocksToAdd.hasNext())
    4. {
    5. blockToAdd = blocksToAdd.next().getLocation();
    6. }

    But I can't figure out how to get nearbyEntities from a location, so I'm sorta stuck
     
  18. Offline

    The_Doctor_123

    sgavster
    Iterate through all the players in the world and check the distance between the Location and all the players' Locations.
     
  19. Offline

    sgavster

    The_Doctor_123 How?.. I know to loop all of the players, but I can't figure out how to get the distance :eek:
     
  20. Offline

    The_Doctor_123

    sgavster
    There's a method called distance() in Location.
     
  21. Offline

    sgavster

    The_Doctor_123 Okay, I tried this:
    Code:java
    1. for(Player o : Bukkit.getOnlinePlayers()){
    2. if(o.getLocation().distance(d.getLocation()) <= 3){
    3. //my code
    4. }
    5. }

    But it doesn't work :(
     
  22. Offline

    The_Doctor_123

    sgavster
    Well, it's probably the code you're not showing that's the issue. But seriously.... just turn PVP on..
     
  23. Offline

    NathanWolf

    That ought to work (I think)- what is "d" though? (one-character variable names for the lose!)

    If d is within 3 blocks of a player (including vertically!) I think that should be working...

    One note, though, since you're looping through all players and potentially doing this a lot (if someone is punching over and over)- use distanceSquared instead of distance. E.g. if (o.getLocation().distanceSquared(d.getLocation()) <= 9) - you have to also square your target distance, but it's much more efficient this way.
     
  24. Offline

    sgavster

    NathanWolf d = a player variable
    The_Doctor_123 PVP:
    This is all my code, I don't think it's my code cause this worked with EntityDamageByEntityEvent.
    Code:java
    1. package me.sgavster.SimplyHub.listeners;
    2.  
    3. import java.util.List;
    4. import java.util.logging.Level;
    5.  
    6. import me.sgavster.SimplyHub.FireworkEffectPlayer;
    7. import me.sgavster.SimplyHub.SimplyHub;
    8.  
    9. import org.bukkit.Bukkit;
    10. import org.bukkit.ChatColor;
    11. import org.bukkit.Color;
    12. import org.bukkit.Effect;
    13. import org.bukkit.FireworkEffect;
    14. import org.bukkit.FireworkEffect.Type;
    15. import org.bukkit.Sound;
    16. import org.bukkit.World;
    17. import org.bukkit.entity.Player;
    18. import org.bukkit.event.EventHandler;
    19. import org.bukkit.event.EventPriority;
    20. import org.bukkit.event.Listener;
    21. import org.bukkit.event.block.Action;
    22. import org.bukkit.event.player.PlayerInteractEvent;
    23. import org.bukkit.event.player.PlayerJoinEvent;
    24.  
    25. public class PopHandler implements Listener
    26. {
    27.  
    28. public static SimplyHub plugin;
    29.  
    30. public PopHandler(SimplyHub instance)
    31. {
    32. plugin = instance;
    33. }
    34.  
    35. FireworkEffectPlayer f = new FireworkEffectPlayer();
    36.  
    37. @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
    38. public void onDamage(PlayerInteractEvent e)
    39. {
    40. if(plugin.getConfig().getBoolean("allow_player_pop"))
    41. {
    42. if(e.getAction() == Action.LEFT_CLICK_AIR)
    43. {
    44. List<String> list = plugin.getConfig().getStringList("Enabled_Worlds");
    45. for(String s : list)
    46. {
    47. try
    48. {
    49. World w = Bukkit.getWorld(s);
    50. Player d = e.getPlayer();
    51. if(d.getWorld().equals(w))
    52. {
    53. try
    54. {
    55. for(Player o : Bukkit.getOnlinePlayers())
    56. {
    57. if(o.getLocation().distance(d.getLocation()) <= 3)
    58. {
    59. if(o.hasPermission("simplyhub.hide.exempt"))
    60. {
    61. d.sendMessage("You can't pop that player!");
    62. }
    63. else
    64. {
    65. f.playFirework(d.getWorld(), o.getLocation(), FireworkEffect.builder().flicker(false).trail(false).with(Type.BURST).withColor(Color.ORANGE).withFade(Color.GREEN).build());
    66. d.getWorld().playEffect(o.getLocation(), Effect.ENDER_SIGNAL, 1);
    67. d.getWorld().playEffect(o.getLocation(), Effect.MOBSPAWNER_FLAMES, 1);
    68. d.getWorld().playEffect(o.getLocation(), Effect.SMOKE, 1);
    69. d.getWorld().playSound(o.getLocation(), Sound.CHICKEN_EGG_POP, 1, 10);
    70. d.hidePlayer(o);
    71. d.sendMessage(ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("pop_player_message").replace("%p%", o.getName())));
    72. }
    73. }
    74. }
    75. }
    76. catch (Exception ex)
    77. {
    78. Bukkit.getLogger().log(Level.SEVERE, "§c[SimplyHub] Could not play firework!");
    79. }
    80. }
    81. }
    82. catch (Exception ex)
    83. {
    84. Bukkit.getLogger().log(Level.SEVERE, "§c[SimplyHub] the config list Enabled_Worlds is wrong!");
    85. }
    86. }
    87. }
    88. }
    89. }
    90.  
    91. @EventHandler
    92. public void onJoin(PlayerJoinEvent e)
    93. {
    94. for(Player p : Bukkit.getOnlinePlayers())
    95. {
    96. e.getPlayer().showPlayer(p);
    97. }
    98. }
    99. }
     
  25. Offline

    NathanWolf

    In all seriousness, those variable names make it much harder to understand what's going on there.

    That said- are you sure this is making it past your Enabled_Worlds check? Stick a log print in there (this is always a good idea if you're not sure how far your code is getting- trace and find out!)

    Otherwise, I can see a problem with this, but not a "it won't work" problem. You should make sure that the player and the target player are both in the same world... meaning I don't think you need to loop through worlds at all. Get the world the player is in, make sure it's in your list of enabled worlds- and then only do the distance check for other players that are in that world. (And really do use distanceSquared)
     
  26. Offline

    sgavster

    NathanWolf The world works. I know it does (I have it in other methods, and this worked before with EntityDamageByEntityEvent)

    How would I do distanceSquared?.
     
  27. Offline

    The_Doctor_123

    sgavster
    distanceSquared() returns a raw distance that hasn't been square rooted. This increases performance.
     
    NathanWolf likes this.
  28. Offline

    NathanWolf

    Hrm- well, I guess I'd suggest you start by refactoring a bit as in my last post. You don't need to loop over worlds, just check for players in the same world as the current player. If it's still broken after that, add some log prints and find out where it's failing- if you're still stuck, post the code and log output.

    When using distanceSquared, make sure to also square your target distance (e.g. 3 blocks == 9)
     
Thread Status:
Not open for further replies.

Share This Page