How to remove lightning damage

Discussion in 'Plugin Development' started by KaiPol, Nov 18, 2013.

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

    KaiPol

    Hello! Is it possible to remove lightning damage but keep the lightning?
     
  2. Offline

    Chinwe

    EntityDamageEvent, check that the cause == DamageCause.LIGHTNING and cancel it :)
     
  3. Offline

    KaiPol

    Code:java
    1. @EventHandler
    2. public void OnEntityDamageEvent(EntityDamageEvent event) {
    3. if (event.getCause() == DamageCause.LIGHTNING) {
    4. event.setCancelled(true);


    I tried this and it didn't work
    Chinwe
     
  4. Offline

    L33m4n123

    Just to make sure. Did you registered your event in the onEnable of your Main Class? :p
     
  5. Offline

    KaiPol

    Code:java
    1. getServer().getPluginManager().registerEvents(this, this);

    I think so?
     
  6. Offline

    amhokies

    It's hard to tell if this is correct without knowing what the rest of your classes are.
    If your main plugin class is the one that the Listener is in, then yes this is right. If your Listener is located in a different class, you need to pass an instance of that class into the registerEvents method. (First argument, I think)
     
  7. Offline

    L33m4n123


    First argument, yepp
     
  8. Offline

    KaiPol

    Everything is all in one class here's my code:

    package me.KaiPol.Test;

    import java.util.logging.Logger;

    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.Material;
    import org.bukkit.Sound;
    import org.bukkit.World;
    import org.bukkit.block.Block;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.EntityDamageByEntityEvent;
    import org.bukkit.event.entity.EntityDamageEvent;
    import org.bukkit.event.entity.EntityDamageEvent.DamageCause;
    import org.bukkit.event.inventory.ClickType;
    import org.bukkit.event.inventory.InventoryClickEvent;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.potion.PotionEffect;
    import org.bukkit.potion.PotionEffectType;
    import org.bukkit.scoreboard.DisplaySlot;
    import org.bukkit.scoreboard.Objective;
    import org.bukkit.scoreboard.Score;
    import org.bukkit.scoreboard.Scoreboard;
    import org.bukkit.scoreboard.ScoreboardManager;
    import org.bukkit.scoreboard.Team;

    public class test extends JavaPlugin implements Listener{
    public final Logger logger = Logger.getLogger("Minecraft");
    public static Inventory testInventory;

    @Override
    public void onEnable(){
    this.logger.info("Enabling final steps...");
    PluginManager pm = getServer().getPluginManager();
    pm.registerEvents(this.bl, this);
    getConfig().options().copyDefaults(true);
    saveConfig();
    getServer().getPluginManager().registerEvents(this, this);
    testInventory = Bukkit.createInventory(null, 9,
    "Shop!");
    }
    @Override
    public void onDisable(){
    this.logger.info("Disabled!");
    }
    public boolean onCommand(CommandSender sender, Command cmd,
    String commandLabel, final String[] args) {
    if(commandLabel.equalsIgnoreCase("bio")){
    Player player = (Player) sender;
    player.sendMessage(getConfig().getString("Message"));
    }
    if(commandLabel.equalsIgnoreCase("joinup")){
    Player player = (Player) sender;
    World world = Bukkit.getServer().getWorld("lobby");
    Location loc = new Location(world,-735, 4, 571);
    player.teleport(loc);
    player.getInventory().clear();
    player.addPotionEffect(new PotionEffect(PotionEffectType.JUMP, 200, 6));
    player.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, 200, 2));
    player.sendMessage(ChatColor.DARK_AQUA + "You joined the gamemode!");
    player.setHealth(20.0);
    player.playSound(player.getLocation(), Sound.ORB_PICKUP, 1.0F, 1);
    player.getItemInHand().setType(Material.REDSTONE_TORCH_ON);
    player.getInventory().addItem(new ItemStack(Material.WOOD_SWORD, 1));
    player.getInventory().addItem(new ItemStack(Material.BONE, 1));
    player.getInventory().addItem(new ItemStack(Material.REDSTONE_TORCH_ON, 1));
    }
    if(commandLabel.equalsIgnoreCase("shop")) {
    if(sender instanceof Player) {
    ((Player) sender).openInventory(testInventory);
    testInventory.clear();
    testInventory.addItem(new ItemStack(Material.IRON_SWORD));
    testInventory.addItem(new ItemStack(Material.ANVIL));
    testInventory.addItem(new ItemStack(Material.DRAGON_EGG));
    testInventory.addItem(new ItemStack(Material.BEACON));
    }
    }
    return false;

    }
    @EventHandler
    public void onInventoryClick(InventoryClickEvent event) {
    if(event.getInventory() == testInventory) {
    Player player = (Player) event.getWhoClicked();
    ItemStack clicked = event.getCurrentItem();
    if(clicked.getType() == Material.IRON_SWORD) {
    event.setCancelled(true);
    player.closeInventory();
    }
    else if(clicked.getType() == Material.ANVIL) {
    event.setCancelled(true);
    player.closeInventory();
    }
    else if(clicked.getType() == Material.DRAGON_EGG) {
    event.setCancelled(true);
    player.closeInventory();
    }
    else if(clicked.getType() == Material.BEACON) {
    event.setCancelled(true);
    player.closeInventory();
    }
    }
    }
    @EventHandler
    public void onPlayerInteract(PlayerInteractEvent event) {
    Player player = event.getPlayer();
    int blockId = player.getItemInHand().getType().getId();
    if(blockId == 76){
    Block block = player.getTargetBlock(null, 6);
    Location location = block.getLocation();
    World world = player.getWorld();
    world.createExplosion(player.getLocation().getX(), player.getLocation().getY(), player.getLocation().getZ(), 4F, false, false);
    if(player.getInventory().contains(Material.REDSTONE_TORCH_ON)) {
    player.getInventory().removeItem(new ItemStack(Material.REDSTONE_TORCH_ON, 1));
    }
    player.sendMessage(ChatColor.GREEN + "You have used your detonator!");
    } else if(blockId == 352){
    Block block = player.getTargetBlock(null, 10);
    Location location = block.getLocation();
    World world = player.getWorld();
    world.playSound(location,Sound.ANVIL_LAND, 1, 1);
    world.strikeLightning(location);
    }
    }
    @EventHandler
    public void onAttack(EntityDamageByEntityEvent event) {
    Player attacker = null;
    Player defender = null;
    if(event.getEntity() instanceof Player) {
    defender = (Player)event.getEntity();
    defender.addPotionEffect(new PotionEffect(PotionEffectType.BLINDNESS, 100, 2));
    defender.addPotionEffect(new PotionEffect(PotionEffectType.HARM, 1, 1));
    defender.addPotionEffect(new PotionEffect(PotionEffectType.POISON, 100, 2));
    defender.sendMessage(ChatColor.DARK_RED + "" + ChatColor.BOLD + "" + ChatColor.ITALIC + "Your health as been stolen!");
    defender.playSound(defender.getLocation(), Sound.GHAST_SCREAM, 1.0F, 1);
    }
    if(event.getDamager() instanceof Player) {
    attacker = (Player)event.getDamager();
    attacker.addPotionEffect(new PotionEffect(PotionEffectType.REGENERATION, 100, 2));
    attacker.addPotionEffect(new PotionEffect(PotionEffectType.HEAL, 1, 1));
    attacker.addPotionEffect(new PotionEffect(PotionEffectType.ABSORPTION, 100, 2));
    attacker.sendMessage(ChatColor.GREEN + "" + ChatColor.BOLD + "" + ChatColor.ITALIC + "You stole their health!");
    attacker.playSound(attacker.getLocation(), Sound.FIREWORK_BLAST, 1.0F, 1);
    }
    }
    @EventHandler
    public void OnEntityDamageEvent(EntityDamageEvent event) {
    if (event.getCause() == DamageCause.LIGHTNING) {
    event.setCancelled(true);
    }
    }
    }
     
  9. Offline

    bartboy8

    KaiPol
    Try this if you just want the effect:
    Code:
    World world = //get your world variable
    world.strikeLightningEffect(location);
     
  10. Offline

    AoH_Ruthless

    KaiPol
    It might be your operator. You are using ==, but maybe you should use .equals(); I forget the difference but that might be the cause of the problem. And for future, use the Code feature when pasting code ...
     
  11. Offline

    L33m4n123


    Source: http://stackoverflow.com/questions/1643067/whats-the-difference-between-equals-and
     
    AoH_Ruthless likes this.
  12. Offline

    Goblom

    Code:java
    1. World.strikeLightningEffect(location)


    That will do the lightning effect but cause no damage to anything.
     
  13. Offline

    minekam20

    Heres the code
    Code:java
    1. package me.KaiPol.Test;
    2.  
    3. import java.util.logging.Logger;
    4.  
    5. import org.bukkit.Bukkit;
    6. import org.bukkit.ChatColor;
    7. import org.bukkit.Location;
    8. import org.bukkit.Material;
    9. import org.bukkit.Sound;
    10. import org.bukkit.World;
    11. import org.bukkit.block.Block;
    12. import org.bukkit.command.Command;
    13. import org.bukkit.command.CommandSender;
    14. import org.bukkit.entity.Player;
    15. import org.bukkit.event.EventHandler;
    16. import org.bukkit.event.Listener;
    17. import org.bukkit.event.entity.EntityDamageByEntityEvent;
    18. import org.bukkit.event.entity.EntityDamageEvent;
    19. import org.bukkit.event.entity.EntityDamageEvent.DamageCause;
    20. import org.bukkit.event.inventory.ClickType;
    21. import org.bukkit.event.inventory.InventoryClickEvent;
    22. import org.bukkit.event.player.PlayerInteractEvent;
    23. import org.bukkit.inventory.Inventory;
    24. import org.bukkit.inventory.ItemStack;
    25. import org.bukkit.plugin.PluginManager;
    26. import org.bukkit.plugin.java.JavaPlugin;
    27. import org.bukkit.potion.PotionEffect;
    28. import org.bukkit.potion.PotionEffectType;
    29. import org.bukkit.scoreboard.DisplaySlot;
    30. import org.bukkit.scoreboard.Objective;
    31. import org.bukkit.scoreboard.Score;
    32. import org.bukkit.scoreboard.Scoreboard;
    33. import org.bukkit.scoreboard.ScoreboardManager;
    34. import org.bukkit.scoreboard.Team;
    35.  
    36. public class test extends JavaPlugin implements Listener{
    37. public final Logger logger = Logger.getLogger("Minecraft");
    38. public static Inventory testInventory;
    39.  
    40. @Override
    41. public void onEnable(){
    42. this.logger.info("Enabling final steps...");
    43. PluginManager pm = getServer().getPluginManager();
    44. pm.registerEvents(this.bl, this);
    45. getConfig().options().copyDefaults(true);
    46. saveConfig();
    47. getServer().getPluginManager().registerEvents(this, this);
    48. testInventory = Bukkit.createInventory(null, 9,
    49. "Shop!");
    50. }
    51. @Override
    52. public void onDisable(){
    53. this.logger.info("Disabled!");
    54. }
    55. public boolean onCommand(CommandSender sender, Command cmd,
    56. String commandLabel, final String[] args) {
    57. if(commandLabel.equalsIgnoreCase("bio")){
    58. Player player = (Player) sender;
    59. player.sendMessage(getConfig().getString("Message"));
    60. }
    61. if(commandLabel.equalsIgnoreCase("joinup")){
    62. Player player = (Player) sender;
    63. World world = Bukkit.getServer().getWorld("lobby");
    64. Location loc = new Location(world,-735, 4, 571);
    65. player.teleport(loc);
    66. player.getInventory().clear();
    67. player.addPotionEffect(new PotionEffect(PotionEffectType.JUMP, 200, 6));
    68. player.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, 200, 2));
    69. player.sendMessage(ChatColor.DARK_AQUA + "You joined the gamemode!");
    70. player.setHealth(20.0);
    71. player.playSound(player.getLocation(), Sound.ORB_PICKUP, 1.0F, 1);
    72. player.getItemInHand().setType(Material.REDSTONE_TORCH_ON);
    73. player.getInventory().addItem(new ItemStack(Material.WOOD_SWORD, 1));
    74. player.getInventory().addItem(new ItemStack(Material.BONE, 1));
    75. player.getInventory().addItem(new ItemStack(Material.REDSTONE_TORCH_ON, 1));
    76. }
    77. if(commandLabel.equalsIgnoreCase("shop")) {
    78. if(sender instanceof Player) {
    79. ((Player) sender).openInventory(testInventory);
    80. testInventory.clear();
    81. testInventory.addItem(new ItemStack(Material.IRON_SWORD));
    82. testInventory.addItem(new ItemStack(Material.ANVIL));
    83. testInventory.addItem(new ItemStack(Material.DRAGON_EGG));
    84. testInventory.addItem(new ItemStack(Material.BEACON));
    85. }
    86. }
    87. return false;
    88.  
    89. }
    90. @EventHandler
    91. public void onInventoryClick(InventoryClickEvent event) {
    92. if(event.getInventory() == testInventory) {
    93. Player player = (Player) event.getWhoClicked();
    94. ItemStack clicked = event.getCurrentItem();
    95. if(clicked.getType() == Material.IRON_SWORD) {
    96. event.setCancelled(true);
    97. player.closeInventory();
    98. }
    99. else if(clicked.getType() == Material.ANVIL) {
    100. event.setCancelled(true);
    101. player.closeInventory();
    102. }
    103. else if(clicked.getType() == Material.DRAGON_EGG) {
    104. event.setCancelled(true);
    105. player.closeInventory();
    106. }
    107. else if(clicked.getType() == Material.BEACON) {
    108. event.setCancelled(true);
    109. player.closeInventory();
    110. }
    111. }
    112. }
    113. @EventHandler
    114. public void onPlayerInteract(PlayerInteractEvent event) {
    115. Player player = event.getPlayer();
    116. int blockId = player.getItemInHand().getType().getId();
    117. if(blockId == 76){
    118. Block block = player.getTargetBlock(null, 6);
    119. Location location = block.getLocation();
    120. World world = player.getWorld();
    121. world.createExplosion(player.getLocation().getX(), player.getLocation().getY(), player.getLocation().getZ(), 4F, false, false);
    122. if(player.getInventory().contains(Material.REDSTONE_TORCH_ON)) {
    123. player.getInventory().removeItem(new ItemStack(Material.REDSTONE_TORCH_ON, 1));
    124. }
    125. player.sendMessage(ChatColor.GREEN + "You have used your detonator!");
    126. } else if(blockId == 352){
    127. Block block = player.getTargetBlock(null, 10);
    128. Location location = block.getLocation();
    129. World world = player.getWorld();
    130. world.playSound(location,Sound.ANVIL_LAND, 1, 1);
    131. world.strikeLightning(location);
    132. }
    133. }
    134. @EventHandler
    135. public void onAttack(EntityDamageByEntityEvent event) {
    136. Player attacker = null;
    137. Player defender = null;
    138. if(event.getEntity() instanceof Player) {
    139. defender = (Player)event.getEntity();
    140. defender.addPotionEffect(new PotionEffect(PotionEffectType.BLINDNESS, 100, 2));
    141. defender.addPotionEffect(new PotionEffect(PotionEffectType.HARM, 1, 1));
    142. defender.addPotionEffect(new PotionEffect(PotionEffectType.POISON, 100, 2));
    143. defender.sendMessage(ChatColor.DARK_RED + "" + ChatColor.BOLD + "" + ChatColor.ITALIC + "Your health as been stolen!");
    144. defender.playSound(defender.getLocation(), Sound.GHAST_SCREAM, 1.0F, 1);
    145. }
    146. if(event.getDamager() instanceof Player) {
    147. attacker = (Player)event.getDamager();
    148. attacker.addPotionEffect(new PotionEffect(PotionEffectType.REGENERATION, 100, 2));
    149. attacker.addPotionEffect(new PotionEffect(PotionEffectType.HEAL, 1, 1));
    150. attacker.addPotionEffect(new PotionEffect(PotionEffectType.ABSORPTION, 100, 2));
    151. attacker.sendMessage(ChatColor.GREEN + "" + ChatColor.BOLD + "" + ChatColor.ITALIC + "You stole their health!");
    152. attacker.playSound(attacker.getLocation(), Sound.FIREWORK_BLAST, 1.0F, 1);
    153. }
    154. }
    155. @EventHandler
    156. public void OnEntityDamageEvent(EntityDamageEvent event) {
    157. if (event.getCause() == DamageCause.LIGHTNING) {
    158. event.setCancelled(true);
    159. }
    160. }
    161. }
     
  14. Offline

    KaiPol

    Well I'm trying to turn pigs into pigmen when you right click the bone but I don't want any damage dealt.
     
  15. Offline

    bartboy8

    The strikeLightningEffect will create the lightning bolt but deal no damage.
     
  16. Offline

    maxben34

    Do bukkit.getServer().getPluginManger().registerEvents(this,this)
     
    KaiPol likes this.
  17. Offline

    KaiPol

    Yes but this doesn't turn them into pigmen
     
  18. Offline

    bartboy8

    Well just despawn the pig at the lightning strike and spawn a pigman!
     
    KaiPol likes this.
Thread Status:
Not open for further replies.

Share This Page