Help plz! Potion effects!

Discussion in 'Plugin Development' started by quinster08, Aug 16, 2013.

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

    quinster08

    Code:java
    1. import org.bukkit.entity.Player;
    2. import org.bukkit.event.EventHandler;
    3. import org.bukkit.event.Listener;
    4. import org.bukkit.event.entity.EntityEvent;
    5. import org.bukkit.event.entity.ProjectileHitEvent;
    6. import org.bukkit.plugin.java.JavaPlugin;
    7. import org.bukkit.potion.PotionEffect;
    8. import org.bukkit.potion.PotionEffectType;
    9.  
    10.  
    11. public class PotionArrows extends JavaPlugin
    12. implements Listener
    13. {
    14. Logger log = Logger.getLogger("Minecraft");
    15. private EntityEvent e;
    16.  
    17. @Override
    18. public void onEnable()
    19. {
    20. this.log.info(this + " Plugin Enabled!");
    21. getServer().getPluginManager().registerEvents(this, this);
    22. }
    23.  
    24. @Override
    25. public void onDisable() {
    26. this.log.info(this + " Plugin Disabled");
    27.  
    28. }
    29.  
    30. @EventHandler
    31. public void onProjectileHit(ProjectileHitEvent event) {
    32. if ((event.getEntity() instanceof Player)) {
    33. Player player = (Player) event.getEntity();
    34. player.addPotionEffect(new PotionEffect(PotionEffectType.SLOW, 10, 3));
    35. Player p = (Player) e.getEntity();
    36. if(p.getItemInHand().getItemMeta().getDisplayName().equals("slowbow")) {
    37. }
    38. }
    39. }
    40. }

    Hey guys i want to make a bow that if you hit some1 with a arrow that he gets slowness for a while.
    The code wont work! Some1 can help me plz?
     
  2. Offline

    Tomskied

    The duration is in server ticks, you are applying the effect for half a second. Also, getEntity() in the ProjectileHitEvent returns the projectile, not the thing it hits.

    If you want to do this in this event, grab the entity, and check for all nearby entities.
    event.getEntity().getNearbyEntities (double x, double y, double z)

    Where the parameters are the distance from your event.getEntity(), then just iterate over the List of entities it returns, check if its a player, and add the potion effect.


    An alternate method, would be to listen on EntityDamageByEntity event, check if the damager entity involved is an arrow, and proceed in a similar way to above.
     
  3. Offline

    Quantix

    Like Tomskied mentioned you would need to listen to the EntityDamageByEntity event, check if the entity receiving the damage is a player and if the entity dealing the damage is an arrow. Then get the shooter of the arrow, and if it is a player (not a skeleton etc.) you would get the item the shooter is holding and check the metadata like you did in your code already. The only issue with this method I mentioned just now is it only checks if the shooter is holding the "slowbow" when the arrow hits another entity, not when it gets shot by the shooter. So if the shooter with the "slowbow" switches item slots while the arrow is in the air and the arrow hits an entity afterwards, the player would no longer be holding the "slowbow" and the effect wouldn't be applied. Instead, every time a player shoots an arrow (listen to the EntityShootBowEvent), add their name to a list, and everytime a player gets damaged by an arrow and the shooters name is in the list, apply the effect and remove their name from the list again. Here's the code:
    Code:java
    1. public Set<String> slowBowShooters = new HashSet<String>(); //List of players who shot their bow with a slowbow
    Put this somewhere in your plugin where you can access it
    Code:java
    1. @EventHandler (priority = EventPriority.NORMAL)
    2. public void onEntityShootBow (EntityShootBowEvent event) {
    3. if (!event.isCancelled()) {
    4. if (event.getEntity() instanceof Player) { //if the player shooting the arrow is a player
    5. Player shooter = (Player) event.getEntity();
    6. ItemStack bow = event.getBow();
    7. if (bow.getItemMeta().getDisplayName().equalsIgnoreCase("slowbow")) { //if the bow name is "slowbow"
    8. slowBowShooters.add(shooter.getName()); //add player name to list so we know he shot an arrow with that bow
    9. }
    10. }
    11. }
    12. }
    13.  
    14. @EventHandler (priority = EventPriority.NORMAL)
    15. public void onEntityDamageByEntity (EntityDamageByEntityEvent event) {
    16. if (!event.isCancelled()) {
    17. if (event.getEntity() instanceof Player && event.getDamager() instanceof Arrow) { //If the damaged entity is a player and the damaging entity is an arrow
    18. Player damaged = (Player) event.getEntity();
    19. Arrow arrow = (Arrow) event.getDamager();
    20. if (arrow.getShooter() instanceof Player) { //If the person who shot the arrow is a player
    21. Player shooter = (Player) arrow.getShooter();
    22. if (slowBowShooters.contains(shooter.getName())) { //If the name of the shooter is in the list AKA he shot an arrow with a "slowbow"
    23. //You can customize this any way you want, the potion effect below lasts 3 seconds
    24. PotionEffect slowness = new PotionEffect(PotionEffectType.SLOW, 60, 2);
    25. slowness.apply(damaged); //apply the potion effect to the damaged player
    26. //You can also apply the potion effect to non-player entities, they justt have to extend LivingEntity
    27. slowBowShooters.remove(shooter.getName());
    28. }
    29. }
    30. }
    31. }
    32. }
     
  4. Offline

    quinster08

    Code:java
    1. @EventHandler (priority = EventPriority.NORMAL)
    2. public void onEntityShootBow (EntityShootBowEvent event) {
    3. if (!event.isCancelled()) {
    4. if (event.getEntity() instanceof Player) {
    5. org.bukkit.inventory.ItemStack bow = event.getBow();
    6. if (bow.getItemMeta().getDisplayName().equalsIgnoreCase("slowbow")) {
    7. }
    8. }
    9. }
    10. }
    11.  
    12. @EventHandler (priority = EventPriority.NORMAL)
    13. public void onEntityDamageByEntity (EntityDamageByEntityEvent event) {
    14. if (!event.isCancelled()) {
    15. if (event.getEntity() instanceof Player && event.getDamager() instanceof Arrow) { //If the damaged entity is a player and the damaging entity is an arrow
    16. Player damaged = (Player) event.getEntity();
    17. Arrow arrow = (Arrow) event.getDamager();
    18. if (arrow.getShooter() instanceof Player) { //If the person who shot the arrow is a player
    19. Player shooter = (Player) arrow.getShooter();
    20. if (((String) slowBowShooters).contains(shooter.getName())) {
    21. PotionEffect slowness = new PotionEffect(PotionEffectType.SLOW, 60, 3);
    22. slowness.apply(damaged);
    23. }
    24. }
    25. }
    26. }
    27. }
    28. }

    Hello guys i wrote the code but when i hit some1 it says in console: could not pass event EntityDamagedByEntityEvent to slowbow v1.0
     
  5. Offline

    Quantix

    Can you paste the entire error message from the console? That would help a lot.

    Also, in your code you removed the line which removes the player string after the arrow hits the damaged player. This is important since if you don't remove the string, players can keep shooting with a normal, not "slow", bow and it will still apply the potion effect to the damaged player.
     
  6. Offline

    quinster08

    Code:
    11:19:49 [SEVERE] Could not pass event EntityDamageByEntityEvent to slowbow v1.0
     
    org.bukkit.event.EventException
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:427)
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.jav
    a:62)
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.j
    ava:477)
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.j
    ava:462)
            at org.bukkit.craftbukkit.v1_6_R2.event.CraftEventFactory.callEvent(Craf
    tEventFactory.java:95)
            at org.bukkit.craftbukkit.v1_6_R2.event.CraftEventFactory.callEntityDama
    geEvent(CraftEventFactory.java:383)
            at org.bukkit.craftbukkit.v1_6_R2.event.CraftEventFactory.handleEntityDa
    mageEvent(CraftEventFactory.java:408)
            at net.minecraft.server.v1_6_R2.EntityLiving.damageEntity(EntityLiving.j
    ava:614)
            at net.minecraft.server.v1_6_R2.EntityHuman.damageEntity(EntityHuman.jav
    a:704)
            at net.minecraft.server.v1_6_R2.EntityPlayer.damageEntity(EntityPlayer.j
    ava:370)
            at net.minecraft.server.v1_6_R2.EntityArrow.l_(EntityArrow.java:229)
            at net.minecraft.server.v1_6_R2.World.entityJoinedWorld(World.java:1354)
     
            at net.minecraft.server.v1_6_R2.World.playerJoinedWorld(World.java:1335)
     
            at net.minecraft.server.v1_6_R2.World.tickEntities(World.java:1223)
            at net.minecraft.server.v1_6_R2.WorldServer.tickEntities(WorldServer.jav
    a:480)
            at net.minecraft.server.v1_6_R2.MinecraftServer.t(MinecraftServer.java:5
    72)
            at net.minecraft.server.v1_6_R2.DedicatedServer.t(DedicatedServer.java:2
    26)
            at net.minecraft.server.v1_6_R2.MinecraftServer.s(MinecraftServer.java:4
    86)
            at net.minecraft.server.v1_6_R2.MinecraftServer.run(MinecraftServer.java
    :419)
            at net.minecraft.server.v1_6_R2.ThreadServerApplication.run(SourceFile:5
    82)
    Caused by: java.lang.NullPointerException
            at Me.krickie.main.PotionArrows.onEntityDamageByEntity(PotionArrows.java
    :55)
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
            at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
            at java.lang.reflect.Method.invoke(Unknown Source)
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:425)
            ... 19 more
    Thats the whole error.
    Help plz?

    Help?

    bump

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

    Lactem

    What you have here is a NullPointerException. In other words, you called a method on something that didn't exist. Your problem is on line 55 of the class PoisonArrows. It would help if you paste your code with line numbers so we can see what's on line 55. Also, I think the rules say to only bump every 24 hours.
     
  8. Offline

    quinster08

    Code:java
    1. package Me.krickie.main;
    2.  
    3. import java.util.logging.Logger;
    4.  
    5. import org.bukkit.entity.Arrow;
    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.entity.EntityDamageByEntityEvent;
    11. import org.bukkit.event.entity.EntityShootBowEvent;
    12. import org.bukkit.plugin.java.JavaPlugin;
    13. import org.bukkit.potion.PotionEffect;
    14. import org.bukkit.potion.PotionEffectType;
    15.  
    16.  
    17. public class PotionArrows extends JavaPlugin
    18. implements Listener
    19. {
    20. Logger log = Logger.getLogger("Minecraft");
    21. private Object slowBowShooters;
    22.  
    23. @Override
    24. public void onEnable()
    25. {
    26. this.log.info(this + " Plugin Enabled!");
    27. getServer().getPluginManager().registerEvents(this, this);
    28. }
    29.  
    30. @Override
    31. public void onDisable() {
    32. this.log.info(this + " Plugin Disabled");
    33.  
    34. }
    35.  
    36. @EventHandler
    37. public void onEntityShootBow (EntityShootBowEvent event) {
    38. if (!event.isCancelled()) {
    39. if (event.getEntity() instanceof Player) { //if the player shooting the arrow is a player
    40. org.bukkit.inventory.ItemStack bow = event.getBow();
    41. if (bow.getItemMeta().getDisplayName().equalsIgnoreCase("slowbow")) { //if the bow name is "slowbow"
    42. }
    43. }
    44. }
    45. }
    46.  
    47. @EventHandler (priority = EventPriority.NORMAL)
    48. public void onEntityDamageByEntity (EntityDamageByEntityEvent event) {
    49. if (!event.isCancelled()) {
    50. if (event.getEntity() instanceof Player && event.getDamager() instanceof Arrow) { //If the damaged entity is a player and the damaging entity is an arrow
    51. Player damaged = (Player) event.getEntity();
    52. Arrow arrow = (Arrow) event.getDamager();
    53. if (arrow.getShooter() instanceof Player) { //If the person who shot the arrow is a player
    54. Player shooter = (Player) arrow.getShooter();
    55. if (((String) slowBowShooters).contains(shooter.getName())) {
    56. PotionEffect slowness = new PotionEffect(PotionEffectType.SLOW, 60, 3);
    57. slowness.apply(damaged);
    58. }
    59. }
    60. }
    61. }
    62. }
    63. }
    64.  


    Oh i deleted that line 55 and it gives me slowness BUT with every bow -_- how can i fix this? Help plz?

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

    ShaharSade

    u

    u can set permissions for group like set permissions only for admins then admins can shot it
    when player it wont make any slowness
     
  10. Offline

    quinster08

Thread Status:
Not open for further replies.

Share This Page