Solved Custom Sword not working.

Discussion in 'Plugin Development' started by Worthless_Hobo, Aug 22, 2014.

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

    Worthless_Hobo

    Im having some trouble with this sword, Ive got no errors, I can create it in the game, but when I hit another player it does absolutely nothing. If anyone could help me figure out whats wrong with it I would be grateful. Ive tried even just setting the sword to light people on fire but still does nothing when I hit someone.

    Code:java
    1. import org.bukkit.ChatColor;
    2. import org.bukkit.Material;
    3. import org.bukkit.entity.EntityType;
    4. import org.bukkit.entity.Player;
    5. import org.bukkit.event.Listener;
    6. import org.bukkit.event.entity.EntityDamageByEntityEvent;
    7. import org.bukkit.potion.PotionEffect;
    8. import org.bukkit.potion.PotionEffectType;
    9.  
    10. public class Thunderbolt implements Listener {
    11.  
    12. public void ThunderBolt(EntityDamageByEntityEvent event)
    13. {
    14. if(!(event.getDamager().getType() == EntityType.PLAYER))
    15. {
    16. return;
    17. }
    18. Player damager = (Player)event.getDamager();
    19. Player target = (Player)event.getEntity();
    20. if(event.getDamager() instanceof Player) {
    21. if(target instanceof Player) {
    22. damager.getItemInHand().getItemMeta().getDisplayName();
    23. damager.getItemInHand().getType();
    24. if(damager.getItemInHand().getType() == Material.GOLD_SWORD) {
    25. if(damager.getItemInHand().getItemMeta().getDisplayName() == ChatColor.GOLD +"Thunderbolt"){
    26. double Strike = Math.round(Math.random()*4);
    27. if(Strike == 3){
    28. target.getWorld().strikeLightningEffect(target.getLocation());
    29. target.addPotionEffect(new PotionEffect(PotionEffectType.HARM, 1, 1/3));
    30. }
    31. }
    32. }
    33. }
    34. }
    35. }
    36. }
     
  2. Offline

    stoneminer02

    First of all you can't get Strike to be 3 and it should be in lowercase(the first letter at least).
    Why you can't get it to be 3 you may ask? Well, cause of the *4. If it become 1, its 4. If it becomes 0, its 0. If it becomes 100 its 400..So good luck
     
  3. Offline

    molenzwiebel

    stoneminer02
    Please research before you say something. Math.random returns a value between 0 and 1, so Strike can be 4 at most.

    Worthless_Hobo
    You are comparing strings using ==. Use equals instead.
    Code:
    getDisplayName().equals(ChatColor.GOLD+"Thunderbolt")
     
  4. Offline

    fireblast709

    Worthless_Hobo
    • Missing EventHandler annotation
    • Not sure if you registered the event
    • Unchecked event.getEntity() -> Player cast
    • Possible NullPointerException when player.getItemInHand() is null
    • Possible NullPointerException when player.getItemInHand() is an AIR stack (getItemMeta() returns null for AIR stacks)
    • Possible NullPointerException when the held item has no displayname
    • String comparison issue mentioned above
    • Use Random#nextInt(int n) for random ints, just for your convenience.
     
    Dragonphase likes this.
  5. Offline

    Worthless_Hobo

    molenzwiebel and fireblast709
    Ok thanks for the help guys Ive also got one other problem that I havent been able to figure out, I have a bow that fires arrows about about 4 per second, however if their are arrows in the inventory it does the drawback event on the bow, but I dont want it to have a drawback, just to fire the arrows like a machine gun. Is there some way to cancel the drawback or stop it from happening?
     
  6. Offline

    fireblast709

    Worthless_Hobo Listen to the PlayerInteractEvent. Loop over the inventory contents, get the first arrow, take one of that arrow out of the inventory and shoot a projectile.
     
  7. Offline

    Worthless_Hobo

    Ok thanks for your help!
     
Thread Status:
Not open for further replies.

Share This Page