Reactivate PlayerInteractEvent

Discussion in 'Plugin Development' started by bennie3211, Jul 26, 2014.

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

    bennie3211

    Hi bukkit, I've a question, i tried to create a automatic gun, but the PlayerInteractEvent only works the time it gets activated :s Is there a way to check if a player is still pressing left/right button outside the PlayerInteractEvent or is there a way to let it check every tick the playerInteractEvent?
     
  2. Offline

    k9rosie

    i don't understand. it sounds like all of your code for the automatic gun is inside the method where the listener fires the PlayerInteractEvent. make a separate class (ex. Gun.java) or something, move all of your code inside the class and you can reference whatever code you want from the class.
     
  3. Offline

    xTigerRebornx

    bennie3211 (AFAIK) The PlayerInteractEvent will continually fire if their mouse is held down. You can combine this with a simple cooldown system (a proper one, not a scheduler one, scheduler will destroy the server if used like this) to add like a small 300~ ms cooldown to put small gaps between each shot.
     
  4. Offline

    bennie3211

    xTigerRebornx k9rosie Why will this not work then?

    this is the gunMethode, and the playerinteractevent. I even added last time some debug messages, but they didn't get spammed in the console when i hold the left mouse button :s

    Code:java
    1. public void onShootGun(final Player pl) {
    2.  
    3. final String name = pl.getName();
    4.  
    5. if (!shooting.contains(name)) {
    6. return;
    7. }
    8.  
    9. if (!hasGun(pl)) {
    10. shooting.remove(name);
    11. return;
    12. }
    13.  
    14. Guns gun = getGun(pl);
    15.  
    16. Double precision = gun.getPrecision();
    17. Integer firedBullets = gun.getFiredBullets();
    18. long shootTime = gun.getShootTime();
    19. Short weaponDamage = gun.getWeaponDamage();
    20. Sound sound = gun.getSound();
    21. Vector v = pl.getLocation().getDirection();
    22.  
    23. for (int i = 0; i < firedBullets; i++) {
    24. Snowball b = pl.launchProjectile(Snowball.class);
    25. Vector vo = v.multiply(5);
    26. vo.add(new Vector(Math.random() * precision - precision, Math.random() * precision - precision, Math.random() * precision - precision));
    27. b.setVelocity(vo);
    28. }
    29.  
    30. pl.sendMessage(weaponDamage + "");
    31.  
    32. pl.getWorld().playSound(pl.getLocation(), sound, 1.2F, 1.2F);
    33.  
    34. Plugin plugin = Bukkit.getServer().getPluginManager().getPlugin("WarZ");
    35.  
    36. plugin.getServer().getScheduler().scheduleSyncDelayedTask(plugin,
    37. new Runnable() {
    38.  
    39. @Override
    40. public void run() {
    41.  
    42. shooting.remove(name);
    43. }
    44.  
    45. }, shootTime);
    46. }
    47.  
    48. @EventHandler
    49. public void onShoot(PlayerInteractEvent event) {
    50.  
    51. Player pl = event.getPlayer();
    52.  
    53. if ((event.getAction() != Action.LEFT_CLICK_AIR) && (event.getAction() != Action.LEFT_CLICK_BLOCK)) {
    54. return;
    55. }
    56.  
    57. if (!hasGun(pl)) {
    58. return;
    59. }
    60.  
    61. if (!shooting.contains(pl.getName())) {
    62. shooting.add(pl.getName());
    63. onShootGun(pl);
    64. }
    65. }


    And yes, this is a small part of the plugin, it works, but not the way I thought it would be :L I have to press the left button again before i can shoot again.
     
  5. Offline

    xTigerRebornx

    bennie3211 Left click can not be held down unless you are left clicking a block (normal minecraft behavior). Right click is repeatedly fired if you hold it down.
    If you want to make an "automatic" gun, I'd suggest using right click.
    And please use proper cooldowns. If you do plan on having an automatic gun, you are going to be creating a massive number of new tasks trying to cooldown.
    http://forums.bukkit.org/threads/easycooldown.128219/ Read the comment made by Comphenix, it shows a proper way of creating cooldowns.
     
    bennie3211 likes this.
  6. Offline

    bennie3211

    Thnx :) I will remake the cooldown system tomorrow :)
     
Thread Status:
Not open for further replies.

Share This Page